*/ public static function settings_tabs() { return WPStarter_Setup::settings_tabs(); } /** * Registered tabs. * * @return array */ public static function get_tabs() { $tabs = array(); // Settings tabs without settings are hidden (they can be filled via // the schema filter). With no settings at all, the first tab shows // and says so, so a new plugin still has a settings screen. $settings_tabs = self::settings_tabs(); $has_settings = false; foreach (array_keys($settings_tabs) as $slug) { if (WPStarter_Settings::fields_for_tab($slug)) { $has_settings = true; break; } } foreach ($settings_tabs as $slug => $tab) { if ($has_settings && !WPStarter_Settings::fields_for_tab($slug)) { continue; } if (!$has_settings && $tabs) { break; } $tabs[$slug] = array( 'label' => $tab['label'], 'group' => 'settings', 'render' => function () use ($slug, $tab) { WPStarter_Settings_Manager::render_tab($slug, $tab['label'], $tab['description']); }, ); } $tabs += array( 'readme' => array( 'label' => __('Read Me', 'wp-plugin-starter-template'), 'group' => 'about', 'render' => array('WPStarter_Readme_Manager', 'display_tab_content'), ), ); /** * Filter the admin tabs. * * @param array $tabs Tabs keyed by slug: label, group (settings|discover|about), * render callback, optional capability. */ $tabs = (array) apply_filters('wpstarter_admin_tabs', $tabs); // Hide tabs the current user cannot use. return array_filter($tabs, function ($tab) { return empty($tab['capability']) || current_user_can($tab['capability']); }); } /** * Active tab slug, falling back to the first registered tab. * * @return string */ public static function get_active_tab() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only navigation. $tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : ''; if (self::SEARCH === $tab && '' !== self::search_query()) { return $tab; } $tabs = self::get_tabs(); $tab = WPStarter_Settings::resolve_tab($tab); if (!isset($tabs[$tab])) { $tab = (string) key($tabs); } return $tab; } /** * Current settings search text. * * @return string */ public static function search_query() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only search. $query = isset($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : ''; return trim(function_exists('mb_substr') ? mb_substr($query, 0, 100) : substr($query, 0, 100)); } /** * URL of a tab. * * @param string $tab Tab slug. * @param array $args Extra query args. * @return string */ public static function tab_url($tab, array $args = array()) { return add_query_arg(array_merge(array('page' => self::PAGE, 'tab' => $tab), $args), admin_url('options-general.php')); } /** * Add the Settings link on the Plugins screen. * * @param array $links Action links. * @return array */ public static function plugin_action_links($links) { array_unshift($links, sprintf('%s', esc_url(add_query_arg('page', self::PAGE, admin_url('options-general.php'))), esc_html__('Settings', 'wp-plugin-starter-template'))); return $links; } /** * Register Settings → WP Plugin Starter. */ public static function register_admin_menu() { add_options_page( __('WP Plugin Starter', 'wp-plugin-starter-template'), __('WP Plugin Starter', 'wp-plugin-starter-template'), 'manage_options', self::PAGE, array(__CLASS__, 'render_settings_page') ); } /** * Enqueue the admin stylesheet and script on our screen only. * * @param string $hook Current admin page hook. */ public static function enqueue_assets($hook) { if (self::HOOK !== $hook) { return; } $tab = self::get_active_tab(); $css = file_exists(WPSTARTER_DIR . 'admin/css/wpstarter-admin.css') ? filemtime(WPSTARTER_DIR . 'admin/css/wpstarter-admin.css') : WPSTARTER_VERSION; $js = file_exists(WPSTARTER_DIR . 'admin/js/wpstarter-admin.js') ? filemtime(WPSTARTER_DIR . 'admin/js/wpstarter-admin.js') : WPSTARTER_VERSION; wp_enqueue_style('wpstarter-admin', WPSTARTER_URL . 'admin/css/wpstarter-admin.css', array('dashicons'), $css); /** * Filter the admin script's dependencies; enqueue what a tab needs. * * @param string[] $deps Script handles. * @param string $tab Active tab. */ $deps = (array) apply_filters('wpstarter_admin_script_deps', array('jquery', 'wp-a11y', 'wp-i18n'), $tab); if (self::shows_media_field($tab)) { wp_enqueue_media(); } wp_enqueue_script('wpstarter-admin', WPSTARTER_URL . 'admin/js/wpstarter-admin.js', $deps, $js, true); wp_set_script_translations('wpstarter-admin', 'wp-plugin-starter-template'); /** * Filter the data the admin script reads (wpstarterAdmin). * * @param array $data Script data. * @param string $tab Active tab. */ wp_localize_script('wpstarter-admin', 'wpstarterAdmin', (array) apply_filters('wpstarter_admin_script_data', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce(WPStarter_Settings::NONCE), 'tab' => $tab, 'i18n' => array( 'saving' => __('Saving…', 'wp-plugin-starter-template'), 'saved' => __('Saved', 'wp-plugin-starter-template'), 'saveFailed' => __('Could not save. Please try again.', 'wp-plugin-starter-template'), 'chooseImage' => __('Choose a picture', 'wp-plugin-starter-template'), 'useImage' => __('Use this picture', 'wp-plugin-starter-template'), ), ), $tab)); /** * Fires after the admin stylesheet and script are enqueued: enqueue * the plugin's own, depending on 'wpstarter-admin'. * * @param string $tab Active tab. */ do_action('wpstarter_admin_enqueue', $tab); } /** * Whether a tab (or the search results) may show a media field, which * needs the media dialog. * * @param string $tab Tab slug. * @return bool */ private static function shows_media_field($tab) { $schema = WPStarter_Settings::schema(); foreach ($schema as $field) { if (!isset($field['type']) || 'media' !== $field['type']) { continue; } if (self::SEARCH === $tab) { return true; } $field_tab = isset($field['tab']) ? $field['tab'] : ''; if (!$field_tab && isset($field['parent'], $schema[$field['parent']]['tab'])) { $field_tab = $schema[$field['parent']]['tab']; } if ($field_tab === $tab) { return true; } } return false; } /** * Render the page chrome and the active tab. */ public static function render_settings_page() { if (!current_user_can('manage_options')) { return; } $tabs = self::get_tabs(); $active = self::get_active_tab(); $links = WPStarter_Setup::header_links(); $groups = array( 'settings' => __('Settings', 'wp-plugin-starter-template'), 'discover' => __('Discover', 'wp-plugin-starter-template'), 'about' => __('About', 'wp-plugin-starter-template'), ); ?>


: core's #wpbody already carries role="main". ?>