settings = $settings; } /** * URL of a Tools tab. * * @param string $tab Tab id. * @return string */ public static function url( string $tab = self::DEFAULT_TAB ): string { return add_query_arg( array( 'page' => Menu::SLUG . '-tools', 'tab' => $tab, ), admin_url( 'admin.php' ) ); } /** * Every tab, ordered by priority. * * @return array */ private function tabs(): array { $tabs = array( self::DEFAULT_TAB => array( 'label' => __( 'General', 'all-in-one-utilities' ), 'description' => __( 'Reset any of this plugin\'s settings tabs back to their defaults.', 'all-in-one-utilities' ), 'tips' => array( __( 'Only tabs you have saved at least once can be reset; the rest already use their defaults.', 'all-in-one-utilities' ), __( 'Changed your mind? "Undo last reset" puts back exactly what the last reset removed.', 'all-in-one-utilities' ), ), 'priority' => 10, 'owner' => 'free', ), ImportExportScreen::TAB => array( 'label' => __( 'Import/Export', 'all-in-one-utilities' ), 'description' => __( 'Move this plugin\'s settings between sites, or keep a copy before big changes.', 'all-in-one-utilities' ), 'tips' => array( __( 'Export the tabs you want as a JSON file, then import that file on another site running this plugin.', 'all-in-one-utilities' ), __( 'Importing changes only the tabs in the file, and every value is checked the same way as when you save it on the Settings screen.', 'all-in-one-utilities' ), ), 'priority' => 15, 'owner' => 'free', ), ); /** * Filters the Tools screen's tabs. Render a tab's content on the * `aiou_tools_tab_{id}` action. * * @since 2.1.0 * * @param array $tabs Tabs keyed by id, each with 'label', and optionally * 'description', 'tips' (string[]), 'priority' and * 'owner' ('free', 'pro', 'woo' or 'tools'). */ $tabs = (array) apply_filters( 'aiou_tools_tabs', $tabs ); foreach ( $tabs as $id => $tab ) { $tabs[ $id ] = array( 'label' => (string) ( $tab['label'] ?? $id ), 'description' => (string) ( $tab['description'] ?? '' ), 'tips' => array_map( 'strval', (array) ( $tab['tips'] ?? array() ) ), 'priority' => (int) ( $tab['priority'] ?? 50 ), 'owner' => (string) ( $tab['owner'] ?? 'free' ), ); } $tabs = Sort::by_priority( $tabs ); return $tabs; } /** * Render the screen. * * @return void */ public function render(): void { $notice = get_transient( 'aiou_tools_notice_' . get_current_user_id() ); delete_transient( 'aiou_tools_notice_' . get_current_user_id() ); $tabs = $this->tabs(); $current = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab selector. if ( ! isset( $tabs[ $current ] ) ) { $current = self::DEFAULT_TAB; } ?>
nav_groups( $tabs, $current ), $this->search_index( $tabs ) ); ?>

render_reset(); /** * Fires on the Tools screen's General tab, for tools that * aren't on a tab of their own (e.g. the Woo Add-on's). * * @since 2.0.0 */ do_action( 'aiou_tools_widgets' ); } if ( ImportExportScreen::TAB === $current ) { ( new ImportExportScreen( $this->settings ) )->render(); } /** * Fires to render one Tools tab's content. * * @since 2.1.0 */ do_action( 'aiou_tools_tab_' . $current ); ?>
$tabs Every tab. * @param string $current Current tab id. * @return array> */ private function nav_groups( array $tabs, string $current ): array { $labels = array( 'free' => array( __( 'General tools', 'all-in-one-utilities' ), 'dashicons-admin-tools' ), 'pro' => array( __( 'Pro tools', 'all-in-one-utilities' ), 'dashicons-star-filled' ), 'woo' => array( __( 'WooCommerce', 'all-in-one-utilities' ), 'dashicons-cart' ), 'tools' => array( __( 'Utility tools', 'all-in-one-utilities' ), 'dashicons-hammer' ), ); $groups = array(); foreach ( $tabs as $tab_id => $tab ) { $owner = $tab['owner']; if ( ! isset( $groups[ $owner ] ) ) { $groups[ $owner ] = array( 'id' => 'tools-' . $owner, 'label' => $labels[ $owner ][0] ?? ucfirst( $owner ), 'icon' => $labels[ $owner ][1] ?? 'dashicons-admin-tools', 'open' => false, 'items' => array(), ); } $groups[ $owner ]['items'][] = array( 'label' => $tab['label'], 'url' => self::url( $tab_id ), 'current' => $tab_id === $current, ); if ( $tab_id === $current ) { $groups[ $owner ]['open'] = true; } } return array_values( $groups ); } /** * Quick-search entries: every tab, matched on its name, description * and tips. * * @param array $tabs Every tab. * @return array> */ private function search_index( array $tabs ): array { $index = array(); foreach ( $tabs as $tab_id => $tab ) { $index[] = array( 'label' => $tab['label'], 'context' => __( 'Tools', 'all-in-one-utilities' ), 'url' => self::url( $tab_id ), 'keywords' => $tab['description'] . ' ' . implode( ' ', $tab['tips'] ), ); } return $index; } /** * Label for a settings page group in the reset list. * * @param string $page Registry page key. * @return string */ private function page_label( string $page ): string { $labels = array( 'default' => __( 'Settings', 'all-in-one-utilities' ), 'woo' => __( 'WooCommerce', 'all-in-one-utilities' ), ); return $labels[ $page ] ?? ucwords( str_replace( array( '-', '_' ), ' ', $page ) ); } /** * The "Reset settings" section: every settings tab as a checklist row, * grouped by the screen it lives on, so any number can be reset at once. * * @return void */ private function render_reset(): void { $groups = array(); $customised = 0; foreach ( $this->settings->get_tabs() as $tab_id => $tab ) { $saved = get_option( $this->settings->option_name( $tab_id ), null ); $count = is_array( $saved ) ? count( $saved ) : 0; $groups[ $tab['page'] ][ $tab_id ] = array( 'label' => $tab['label'], 'saved' => null !== $saved, 'sections' => $count, 'total' => count( $this->settings->get_sections( $tab_id ) ), ); if ( null !== $saved ) { ++$customised; } } // Free's own Settings screen first, then any add-on's page. if ( isset( $groups['default'] ) ) { $groups = array( 'default' => $groups['default'] ) + $groups; } $snapshot = Tools::snapshot(); ?>

settings->get_tabs() ) ); ?>

$tabs ) : ?>

page_label( (string) $page ) ); ?>

$tab ) : ?>
/>