diff --git a/AGENTS.md b/AGENTS.md index e8da06fb..4f907c30 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -106,7 +106,7 @@ Always run `composer lint` and `composer phpstan` before considering PHP work do 1. **`frontblocks.php`** — entry point; defines constants (`FRBL_VERSION`, `FRBL_PLUGIN`, `FRBL_PLUGIN_URL`, `FRBL_PLUGIN_PATH`), loads Composer autoloader, hooks `plugins_loaded` → singleton `FrontBlocks\Plugin_Main::get_instance()`. 2. **`includes/Plugin_Main.php`** — singleton; `load_modules()` instantiates all frontend and admin feature classes. 3. **`includes/Frontend/*.php`** — each feature is a self-contained class; constructor calls `init_hooks()` which registers the WordPress actions/filters it needs (block filtering for block-related features; other hooks — uploads, `wp_head`, `template_redirect`, etc. — for general site-tool features). -4. **`includes/Admin/Settings.php`** — settings page at `themes.php?page=frontblocks-settings`. +4. **`includes/Admin/Settings.php`** — the FrontBlocks menu (`admin.php?page=frontblocks-settings`) with a submenu for each settings tab. Build links with `Settings::get_settings_url( $tab )` and recognize its screens with `Settings::is_settings_screen( $screen_id )`, never with a hard-coded URL or screen ID. ### Module Pattern diff --git a/docs/COOKIE-NOTICE.md b/docs/COOKIE-NOTICE.md index 2cbbc1b8..d386dfc7 100644 --- a/docs/COOKIE-NOTICE.md +++ b/docs/COOKIE-NOTICE.md @@ -13,7 +13,7 @@ and other tracking integrations) has been removed from FrontBlocks. Sites that had it enabled will show no cookie banner at all until FrontConsent is installed — this is intentional, to push migration forward. -**Appearance → FrontBlocks → Cookie Notice** is now a static promo panel: it +**FrontBlocks → Cookie Notice** is now a static promo panel: it explains the move and links to install FrontConsent. If FrontConsent isn't installed yet, that link only installs it — WordPress still requires a separate activation step (a confirmation screen with an "Activate Plugin" diff --git a/docs/GOOGLE-SIGNIN.md b/docs/GOOGLE-SIGNIN.md index 9d7a0ca3..6514d680 100644 --- a/docs/GOOGLE-SIGNIN.md +++ b/docs/GOOGLE-SIGNIN.md @@ -14,7 +14,7 @@ password. It uses [Google Identity Services](https://developers.google.com/ident on the browser side and verifies the resulting ID token server-side — no third-party PHP library or Google API client is required. -Enable it from **Appearance → FrontBlocks → Google Sign-In**. The button +Enable it from **FrontBlocks → Social Login**. The button stays hidden everywhere until a Client ID and Client Secret are set there. ## Settings diff --git a/docs/IMAGE-MANAGEMENT.md b/docs/IMAGE-MANAGEMENT.md index 9a89a903..d8766f1f 100644 --- a/docs/IMAGE-MANAGEMENT.md +++ b/docs/IMAGE-MANAGEMENT.md @@ -13,7 +13,7 @@ intermediate image sizes WordPress generates on upload, and automatically serves modern image formats (WebP/AVIF) on the frontend when the server supports them. -Enable it from **Appearance → FrontBlocks → Image Management**. +Enable it from **FrontBlocks → Image Management**. ## Settings diff --git a/docs/REDUNDANT-PLUGINS.md b/docs/REDUNDANT-PLUGINS.md index f8d62be8..4d8c67d9 100644 --- a/docs/REDUNDANT-PLUGINS.md +++ b/docs/REDUNDANT-PLUGINS.md @@ -27,7 +27,7 @@ add_filter( 'frontblocks_redundant_plugins', function ( $entries ) { 'shortpixel-image-optimiser/wp-shortpixel.php' => 'ShortPixel Image Optimizer', ), // Optional: link shown on the notice for more information. - 'doc_url' => admin_url( 'themes.php?page=frontblocks-settings' ), + 'doc_url' => \FrontBlocks\Admin\Settings::get_settings_url(), ); return $entries; diff --git a/docs/SETTINGS-TAB-EXTENSIONS.md b/docs/SETTINGS-TAB-EXTENSIONS.md index 343aa368..5b46333f 100644 --- a/docs/SETTINGS-TAB-EXTENSIONS.md +++ b/docs/SETTINGS-TAB-EXTENSIONS.md @@ -1,6 +1,6 @@ # Extending FrontBlocks Settings Tabs -FrontBlocks exposes two hooks for companion plugins that need a dedicated tab on the **Appearance → FrontBlocks** settings screen. +FrontBlocks exposes two hooks for companion plugins that need a dedicated tab on the **FrontBlocks** settings screen. Every tab also gets an entry in the FrontBlocks menu of the admin sidebar. ## Add a tab and its panel @@ -31,4 +31,19 @@ function my_plugin_render_settings_tab() { } ``` +## The FrontBlocks menu + +FrontBlocks has a menu of its own in the admin sidebar, right below Appearance, with a submenu entry for each tab. Each entry is a page of its own, so a tab has a URL that can be linked, bookmarked and opened from the sidebar: + +| Tab id | Page slug | URL | +| --- | --- | --- | +| `blocks` (the first tab) | `frontblocks-settings` | `admin.php?page=frontblocks-settings` | +| any other, for example `my-plugin` | `frontblocks-settings-my-plugin` | `admin.php?page=frontblocks-settings-my-plugin` | + +- Your tab appears in the submenu automatically. Add `'menu' => false` to the tab definition to keep it out of the submenu. It is still a tab of the screen. +- Link to a tab with `\FrontBlocks\Admin\Settings::get_settings_url( 'my-plugin' )`, not with a hard-coded URL. +- Recognize the settings screens with `\FrontBlocks\Admin\Settings::is_settings_screen( $hook_or_screen_id )`. The screen ID of a tab is not the same on every tab, and it was different when the screen lived under Appearance. Do not compare it with a fixed string such as `appearance_page_frontblocks-settings`. +- Check that the method exists before you call it if your plugin must work with older versions of FrontBlocks, which had the page under Appearance (`themes.php?page=frontblocks-settings`). +- The old Appearance URL still works. It redirects to the new menu and keeps its query arguments and its `#tab`. + The panel action runs after FrontBlocks closes its settings form. If your tab submits data, provide its own form, nonce, capability checks, and sanitization. diff --git a/docs/TESTING-INSTRUCTIONS.md b/docs/TESTING-INSTRUCTIONS.md index 0cd49475..97b93e60 100644 --- a/docs/TESTING-INSTRUCTIONS.md +++ b/docs/TESTING-INSTRUCTIONS.md @@ -22,7 +22,7 @@ Apariencia → Frontblocks O directamente: ``` -http://tu-sitio.local/wp-admin/themes.php?page=frontblocks-settings +http://tu-sitio.local/wp-admin/admin.php?page=frontblocks-settings ``` --- diff --git a/frontblocks.php b/frontblocks.php index db1619c6..23602575 100644 --- a/frontblocks.php +++ b/frontblocks.php @@ -83,7 +83,7 @@ function frbl_plugin_activation_redirect() { // Redirect to settings page. if ( get_option( 'frbl_activation_redirect', false ) ) { delete_option( 'frbl_activation_redirect' ); - wp_safe_redirect( admin_url( 'themes.php?page=frontblocks-settings' ) ); + wp_safe_redirect( class_exists( '\FrontBlocks\Admin\Settings' ) ? \FrontBlocks\Admin\Settings::get_settings_url() : admin_url( 'admin.php?page=frontblocks-settings' ) ); exit; } } diff --git a/includes/Admin/CookieNoticeDeprecationNotice.php b/includes/Admin/CookieNoticeDeprecationNotice.php index 4025e59b..b292bf62 100644 --- a/includes/Admin/CookieNoticeDeprecationNotice.php +++ b/includes/Admin/CookieNoticeDeprecationNotice.php @@ -139,8 +139,8 @@ public function render_notice() { return; } - $allowed_screens = array( 'appearance_page_frontblocks-settings', 'dashboard', 'plugins' ); - if ( ! in_array( $screen->id, $allowed_screens, true ) ) { + $allowed_screens = array( 'dashboard', 'plugins' ); + if ( ! in_array( $screen->id, $allowed_screens, true ) && ! Settings::is_settings_screen( $screen->id ) ) { return; } diff --git a/includes/Admin/ImageManagement.php b/includes/Admin/ImageManagement.php index d3fe7ebe..38a4b599 100644 --- a/includes/Admin/ImageManagement.php +++ b/includes/Admin/ImageManagement.php @@ -417,7 +417,7 @@ public function sanitize_settings( $value, $option ) { * @return void */ public function enqueue_assets( $hook ) { - if ( 'appearance_page_' . $this->page_slug !== $hook ) { + if ( ! Settings::is_settings_screen( $hook ) ) { return; } diff --git a/includes/Admin/RedundantPlugins.php b/includes/Admin/RedundantPlugins.php index 168ee1b4..56dcf68e 100644 --- a/includes/Admin/RedundantPlugins.php +++ b/includes/Admin/RedundantPlugins.php @@ -101,7 +101,7 @@ private static function get_default_entries() { 'safe-svg/safe-svg.php' => 'Safe SVG', 'svg-support/svg-support.php' => 'SVG Support', ), - 'doc_url' => admin_url( 'themes.php?page=frontblocks-settings' ), + 'doc_url' => Settings::get_settings_url(), ), 'login-google' => array( 'feature' => __( 'Google Login', 'frontblocks' ), @@ -110,7 +110,7 @@ private static function get_default_entries() { 'login-with-google/login-with-google.php' => 'Login with Google', 'login-with/login-with.php' => 'Login with', ), - 'doc_url' => admin_url( 'themes.php?page=frontblocks-settings' ), + 'doc_url' => Settings::get_settings_url( 'google-signin' ), ), 'staging-mode' => array( 'feature' => __( 'Staging Mode', 'frontblocks' ), @@ -119,7 +119,7 @@ private static function get_default_entries() { 'plugins' => array( '360-orbit-staging-guard/360-orbit-staging-guard.php' => '360 Orbit Staging Guard', ), - 'doc_url' => admin_url( 'themes.php?page=frontblocks-settings' ), + 'doc_url' => Settings::get_settings_url( 'maintenance' ), ), ); } @@ -140,8 +140,8 @@ public function render_notices() { return; } - $allowed_screens = array( 'appearance_page_frontblocks-settings', 'dashboard', 'plugins' ); - if ( ! in_array( $screen->id, $allowed_screens, true ) && false === strpos( $screen->id, 'frontblocks' ) ) { + $allowed_screens = array( 'dashboard', 'plugins' ); + if ( ! in_array( $screen->id, $allowed_screens, true ) && ! Settings::is_settings_screen( $screen->id ) && false === strpos( $screen->id, 'frontblocks' ) ) { return; } diff --git a/includes/Admin/ReviewNotice.php b/includes/Admin/ReviewNotice.php index 4a1176b8..b51130bc 100644 --- a/includes/Admin/ReviewNotice.php +++ b/includes/Admin/ReviewNotice.php @@ -51,8 +51,8 @@ public function review_notice() { } if ( ! $debug ) { - $allowed_screens = array( 'appearance_page_frontblocks-settings', 'dashboard' ); - if ( ! in_array( $screen->id, $allowed_screens, true ) && false === strpos( $screen->id, 'frontblocks' ) ) { + $allowed_screens = array( 'dashboard' ); + if ( ! in_array( $screen->id, $allowed_screens, true ) && ! Settings::is_settings_screen( $screen->id ) && false === strpos( $screen->id, 'frontblocks' ) ) { return; } } diff --git a/includes/Admin/Settings.php b/includes/Admin/Settings.php index ea1992a6..7555bec6 100644 --- a/includes/Admin/Settings.php +++ b/includes/Admin/Settings.php @@ -270,7 +270,7 @@ class Settings { * * @var string */ - private $page_slug = 'frontblocks-settings'; + private $page_slug = self::PAGE_SLUG; /** * Is license valid. @@ -287,6 +287,7 @@ public function __construct() { $this->is_license_valid = function_exists( 'frblp_is_license_valid' ) && frblp_is_license_valid(); add_action( 'admin_menu', array( $this, 'register_menu' ) ); + add_action( 'admin_menu', array( $this, 'redirect_legacy_url' ), 99 ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); add_action( 'admin_head', array( $this, 'add_menu_icon_styles' ) ); @@ -318,7 +319,7 @@ public function add_menu_icon_styles() { * @return void */ public function enqueue_admin_styles( $hook ) { - if ( 'appearance_page_' . $this->page_slug !== $hook ) { + if ( ! self::is_settings_screen( $hook ) ) { return; } @@ -668,6 +669,62 @@ function updateScrollTopVisibility() { var tabPanels = document.querySelectorAll('[data-tab-panel]'); var saveBar = document.querySelector('.frbl-settings-save-bar'); + var wrapper = document.querySelector('.frbl-settings-wrapper'); + var tabPages = {}; + try { + tabPages = JSON.parse(wrapper ? wrapper.getAttribute('data-tab-pages') : '{}') || {}; + } catch (error) { + tabPages = {}; + } + + // Highlight the FrontBlocks submenu entry of the tab being shown. + function syncMenu(tabId) { + var menu = document.getElementById('toplevel_page_frontblocks-settings'); + var page = tabPages[tabId]; + + if (! menu || ! page) { + return; + } + + menu.querySelectorAll('.wp-submenu li').forEach(function (item) { + var link = item.querySelector('a'); + var match = false; + + if (link) { + try { + match = new URL(link.href, window.location.href).searchParams.get('page') === page; + } catch (error) { + match = false; + } + } + + item.classList.toggle('current', match); + if (link) { + link.classList.toggle('current', match); + if (match) { + link.setAttribute('aria-current', 'page'); + } else { + link.removeAttribute('aria-current'); + } + } + }); + } + + // Point the address bar at the page of the tab, so a reload or a copied link opens it. + function updateUrl(tabId) { + var page = tabPages[tabId]; + + if (! page || ! window.history || ! window.history.replaceState) { + return; + } + + var url = new URL(window.location.href); + url.search = ''; + url.hash = ''; + url.searchParams.set('page', page); + window.history.replaceState(null, '', url.toString()); + } + function activateTab(tabId) { var found = false; @@ -693,11 +750,14 @@ function activateTab(tabId) { btn.classList.toggle('is-active', active); btn.setAttribute('aria-selected', active ? 'true' : 'false'); }); + + syncMenu(tabId); } tabButtons.forEach(function (btn) { btn.addEventListener('click', function () { activateTab(btn.getAttribute('data-tab-target')); + updateUrl(btn.getAttribute('data-tab-target')); }); }); @@ -713,6 +773,10 @@ function activateTab(tabId) { }); var initialTab = 'blocks'; + var defaultTab = wrapper ? wrapper.getAttribute('data-default-tab') : ''; + if (defaultTab && document.querySelector('[data-tab-panel=\"' + defaultTab + '\"]')) { + initialTab = defaultTab; + } var hash = window.location.hash.replace('#', ''); if (hash === 'frontblocks_section_license') { hash = 'license'; @@ -722,6 +786,17 @@ function activateTab(tabId) { } activateTab(initialTab); + // A link to a tab with a #hash, followed while the screen is open. + window.addEventListener('hashchange', function () { + var next = window.location.hash.replace('#', ''); + if (next === 'frontblocks_section_license') { + next = 'license'; + } + if (next && document.querySelector('[data-tab-panel=\"' + next + '\"]')) { + activateTab(next); + } + }); + // Keep the active tab across a save (WordPress redirects back to // the referring URL after options.php processes the form). var form = document.getElementById('frbl-settings-form'); @@ -808,18 +883,163 @@ function markDirty() { } /** - * Register options page as dedicated menu. + * Slug of the settings page, and of its first tab. + * + * @since 1.6.0 + * @var string + */ + const PAGE_SLUG = 'frontblocks-settings'; + + /** + * Tab that opens when none is requested. + * + * @since 1.6.0 + * @var string + */ + const DEFAULT_TAB = 'blocks'; + + /** + * Capability needed to open the settings screen and its menu. + * + * @since 1.6.0 + * @var string + */ + const CAPABILITY = 'edit_theme_options'; + + /** + * Slug of the menu page that opens a tab. + * + * The first tab uses the slug of the menu itself, every other tab adds its id. + * + * @since 1.6.0 + * @param string $tab Tab id, empty for the first tab. + * @return string + */ + public static function get_page_slug( $tab = '' ) { + $tab = sanitize_key( $tab ); + + return '' === $tab || self::DEFAULT_TAB === $tab ? self::PAGE_SLUG : self::PAGE_SLUG . '-' . $tab; + } + + /** + * Tab id behind the slug of a menu page. + * + * @since 1.6.0 + * @param string $page Menu page slug. + * @return string The tab id, or an empty string when the slug is not a settings page. + */ + public static function get_tab_from_page( $page ) { + if ( ! is_string( $page ) ) { + return ''; + } + + if ( self::PAGE_SLUG === $page ) { + return self::DEFAULT_TAB; + } + + $prefix = self::PAGE_SLUG . '-'; + + return 0 === strpos( $page, $prefix ) ? substr( $page, strlen( $prefix ) ) : ''; + } + + /** + * URL of the settings screen, optionally on a given tab. + * + * Use it instead of building the URL by hand, so a link keeps working wherever the menu is. + * + * @since 1.6.0 + * @param string $tab Tab id, empty for the first tab. + * @return string + */ + public static function get_settings_url( $tab = '' ) { + return admin_url( 'admin.php?page=' . self::get_page_slug( $tab ) ); + } + + /** + * Whether an admin screen ID (or hook suffix) is one of the settings screens. + * + * It matches the screens of every tab, and the one the page had under Appearance. + * + * @since 1.6.0 + * @param mixed $screen_id Screen ID or hook suffix. + * @return bool + */ + public static function is_settings_screen( $screen_id ) { + return is_string( $screen_id ) && false !== strpos( $screen_id, self::PAGE_SLUG ); + } + + /** + * Register the FrontBlocks menu, with a submenu for each tab. * * @return void */ public function register_menu() { - add_theme_page( + $icon_path = FRBL_PLUGIN_PATH . 'assets/admin/icons/icon-menu.svg'; + $icon = file_exists( $icon_path ) ? FRBL_PLUGIN_URL . 'assets/admin/icons/icon-menu.svg' : 'dashicons-block-default'; + + // The page callback goes on the first submenu, so the page is not rendered twice. + add_menu_page( __( 'FrontBlocks Settings', 'frontblocks' ), __( 'FrontBlocks', 'frontblocks' ), - 'edit_theme_options', - $this->page_slug, - array( $this, 'render_page' ) + self::CAPABILITY, + self::PAGE_SLUG, + '', + $icon, + 60.5 ); + + foreach ( $this->get_tabs() as $tab ) { + if ( ! isset( $tab['id'], $tab['label'] ) || ! is_string( $tab['id'] ) || ! is_string( $tab['label'] ) || '' === $tab['id'] ) { + continue; + } + + // A companion plugin can keep a tab out of the menu with 'menu' => false. + if ( isset( $tab['menu'] ) && false === $tab['menu'] ) { + continue; + } + + add_submenu_page( + self::PAGE_SLUG, + /* translators: %s: name of the settings tab, for example Backup */ + sprintf( __( 'FrontBlocks: %s', 'frontblocks' ), $tab['label'] ), + $tab['label'], + self::CAPABILITY, + self::get_page_slug( $tab['id'] ), + array( $this, 'render_page' ) + ); + } + } + + /** + * Send the old settings URL, under Appearance, to the FrontBlocks menu. + * + * Bookmarks, links in other plugins and the notices of older versions point at + * themes.php?page=frontblocks-settings. The query arguments and the #tab of the + * old link are kept, so a link that opened a tab or showed a notice still does. + * + * @since 1.6.0 + * @return void + */ + public function redirect_legacy_url() { + global $pagenow; + + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only reads the page being asked for, nothing is processed. + $page = isset( $_GET['page'] ) && is_string( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; + + if ( 'themes.php' !== $pagenow || self::PAGE_SLUG !== $page || ! current_user_can( self::CAPABILITY ) ) { + return; + } + + $args = array(); + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only forwards sanitized values to the new URL. + foreach ( $_GET as $key => $value ) { + if ( 'page' !== $key && is_string( $value ) ) { + $args[ sanitize_key( $key ) ] = sanitize_text_field( wp_unslash( $value ) ); + } + } + + wp_safe_redirect( add_query_arg( $args, self::get_settings_url() ) ); + exit; } /** @@ -1179,30 +1399,13 @@ function () use ( $frblp_license ) { } /** - * Render settings page. + * Tabs of the settings screen, which are also the entries of the submenu. * - * @return void + * @since 1.6.0 + * @param array $counts Tab id => array( toggles on, toggles total ), to show next to a tab. The settings fields they come from exist once the page renders, not when the menu is registered. + * @return array */ - public function render_page() { - if ( ! current_user_can( 'edit_theme_options' ) ) { - return; - } - - global $wp_settings_sections; - - $sections = isset( $wp_settings_sections[ $this->page_slug ] ) ? (array) $wp_settings_sections[ $this->page_slug ] : array(); - $has_cpt_tab = false; - - foreach ( $sections as $section ) { - if ( 'frontblocks_section_custom_post_types' === $section['id'] ) { - $has_cpt_tab = true; - break; - } - } - - list( $features_on, $features_total ) = $this->count_section_toggles( 'frontblocks_section_features' ); - list( $woo_on, $woo_total ) = $this->count_section_toggles( 'frontblocks_section_woocommerce_features' ); - + private function get_tabs( $counts = array() ) { $tabs = array( array( 'id' => 'blocks', @@ -1215,8 +1418,6 @@ public function render_page() { array( 'id' => 'optional', 'label' => __( 'Optional features', 'frontblocks' ), - 'on' => $features_on, - 'total' => $features_total, ), array( 'id' => 'image-management', @@ -1229,8 +1430,6 @@ public function render_page() { array( 'id' => 'woocommerce', 'label' => __( 'WooCommerce', 'frontblocks' ), - 'on' => $woo_on, - 'total' => $woo_total, ), array( 'id' => 'cookies', @@ -1250,19 +1449,72 @@ public function render_page() { ), ); + foreach ( $tabs as $index => $tab ) { + if ( isset( $counts[ $tab['id'] ] ) ) { + $tabs[ $index ]['on'] = $counts[ $tab['id'] ][0]; + $tabs[ $index ]['total'] = $counts[ $tab['id'] ][1]; + } + } + /** * Filters the settings tabs displayed in the FrontBlocks admin screen. * * Companion plugins can add a tab by appending an array with an `id` and * `label` key, then render its matching panel on - * `frontblocks_settings_tab_panels`. + * `frontblocks_settings_tab_panels`. Every tab also gets an entry in the + * FrontBlocks menu. Add `'menu' => false` to keep a tab out of it. * * @since 1.5.3 * @param array $tabs Settings tabs. */ $tabs = apply_filters( 'frontblocks_settings_tabs', $tabs ); + + return is_array( $tabs ) ? $tabs : array(); + } + + /** + * Render settings page. + * + * @return void + */ + public function render_page() { + if ( ! current_user_can( 'edit_theme_options' ) ) { + return; + } + + global $wp_settings_sections; + + $sections = isset( $wp_settings_sections[ $this->page_slug ] ) ? (array) $wp_settings_sections[ $this->page_slug ] : array(); + $has_cpt_tab = false; + + foreach ( $sections as $section ) { + if ( 'frontblocks_section_custom_post_types' === $section['id'] ) { + $has_cpt_tab = true; + break; + } + } + + list( $features_on, $features_total ) = $this->count_section_toggles( 'frontblocks_section_features' ); + list( $woo_on, $woo_total ) = $this->count_section_toggles( 'frontblocks_section_woocommerce_features' ); + + $tabs = $this->get_tabs( + array( + 'optional' => array( $features_on, $features_total ), + 'woocommerce' => array( $woo_on, $woo_total ), + ) + ); + + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only reads which tab to open first, nothing is processed. + $requested_page = isset( $_GET['page'] ) && is_string( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : self::PAGE_SLUG; + $default_tab = self::get_tab_from_page( $requested_page ); + $tab_pages = array(); + foreach ( $tabs as $tab ) { + if ( isset( $tab['id'] ) && is_string( $tab['id'] ) ) { + $tab_pages[ $tab['id'] ] = self::get_page_slug( $tab['id'] ); + } + } ?> -
+
diff --git a/includes/Frontend/Maintenance.php b/includes/Frontend/Maintenance.php index 18be6f9c..38b7f993 100644 --- a/includes/Frontend/Maintenance.php +++ b/includes/Frontend/Maintenance.php @@ -49,7 +49,7 @@ public function add_admin_bar_node( $wp_admin_bar ) { array( 'id' => 'frbl-maintenance-status', 'title' => '' . esc_html__( 'Maintenance: ON', 'frontblocks' ) . '', - 'href' => admin_url( 'themes.php?page=frontblocks-settings' ), + 'href' => class_exists( '\FrontBlocks\Admin\Settings' ) ? \FrontBlocks\Admin\Settings::get_settings_url( 'maintenance' ) : admin_url( 'admin.php?page=frontblocks-settings-maintenance' ), 'meta' => array( 'title' => esc_attr__( 'Maintenance mode is currently enabled. Click to manage it.', 'frontblocks' ), ), diff --git a/includes/Frontend/Staging.php b/includes/Frontend/Staging.php index 8c85c847..ea08f892 100644 --- a/includes/Frontend/Staging.php +++ b/includes/Frontend/Staging.php @@ -885,7 +885,7 @@ public function add_admin_bar_node( $wp_admin_bar ) { ); if ( current_user_can( 'manage_options' ) ) { - $node['href'] = admin_url( 'themes.php?page=frontblocks-settings' ); + $node['href'] = class_exists( '\FrontBlocks\Admin\Settings' ) ? \FrontBlocks\Admin\Settings::get_settings_url( 'maintenance' ) : admin_url( 'admin.php?page=frontblocks-settings-maintenance' ); } $wp_admin_bar->add_node( $node ); @@ -963,7 +963,7 @@ public function render_admin_notice() { } $screen = get_current_screen(); - if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'appearance_page_frontblocks-settings' ), true ) ) { + if ( ! $screen || ( 'dashboard' !== $screen->id && ! \FrontBlocks\Admin\Settings::is_settings_screen( $screen->id ) ) ) { return; } diff --git a/includes/GoogleSignIn/Settings.php b/includes/GoogleSignIn/Settings.php index 25b9b649..5225f622 100644 --- a/includes/GoogleSignIn/Settings.php +++ b/includes/GoogleSignIn/Settings.php @@ -137,7 +137,7 @@ public function maybe_save() { * @return string */ private function settings_url() { - return admin_url( 'themes.php?page=' . self::PAGE_SLUG . '#google-signin' ); + return \FrontBlocks\Admin\Settings::get_settings_url( 'google-signin' ); } /** diff --git a/readme.txt b/readme.txt index 0a07e03a..59dc5c12 100644 --- a/readme.txt +++ b/readme.txt @@ -224,6 +224,7 @@ More information in the [FrontBlocks PRO](https://close.technology/en/wordpress- == Changelog == = Unreleased = +* Changed: FrontBlocks has a menu of its own in the admin sidebar, with a submenu entry for each settings tab, instead of a page under Appearance. The old Appearance URL redirects to the new menu, keeping its arguments and tab. Companion plugins can link to a tab with `Settings::get_settings_url()` and recognize its screens with `Settings::is_settings_screen()`. * Added: Staging Mode. It is activated automatically on domains starting with `beta.` (filterable), when the environment type is `staging` or `development`, with a manual toggle in the Maintenance tab, or with the `FRBL_STAGING` constant (`false` forces it off). It blocks indexing (noindex, X-Robots-Tag, closed robots.txt, no sitemaps), blocks outgoing email with a log (or redirects it to one address), can restrict the frontend to logged-in users, adds a STAGING admin bar node, a [STAGING] title prefix and notices, adds WooCommerce safety (test site notice, manual Subscriptions renewals, paused webhooks), can pause WP-Cron and Action Scheduler, block requests to selected hosts and remove Google Analytics/Tag Manager snippets. Add-ons can check `frontblocks_is_staging()`. * Changed: Removed the legacy Cookie Notice runtime (banner, AJAX consent endpoints, and GTM/GA4/other tracking integrations) from FrontBlocks — a hard cutover, not a transition. Cookie consent now lives entirely in the standalone FrontConsent plugin; the Cookie Notice settings tab remains as a promo panel pointing to it. * Added: Include/exclude category filters for the Product Categories block, with support for nested (parent/child) categories. diff --git a/tests/Unit/AdminImageManagementTest.php b/tests/Unit/AdminImageManagementTest.php index aa61dfda..78cd86f8 100644 --- a/tests/Unit/AdminImageManagementTest.php +++ b/tests/Unit/AdminImageManagementTest.php @@ -695,7 +695,7 @@ public function test_enqueue_assets_only_enqueues_on_its_own_settings_page() { $this->assertFalse( wp_script_is( 'frontblocks-image-management', 'enqueued' ) ); $this->assertFalse( wp_style_is( 'frontblocks-image-management', 'enqueued' ) ); - $this->admin->enqueue_assets( 'appearance_page_frontblocks-settings' ); + $this->admin->enqueue_assets( 'toplevel_page_frontblocks-settings' ); $this->assertTrue( wp_script_is( 'frontblocks-image-management', 'enqueued' ) ); $this->assertTrue( wp_style_is( 'frontblocks-image-management', 'enqueued' ) ); diff --git a/tests/Unit/SettingsMenuTest.php b/tests/Unit/SettingsMenuTest.php new file mode 100644 index 00000000..e0ad0333 --- /dev/null +++ b/tests/Unit/SettingsMenuTest.php @@ -0,0 +1,505 @@ +saved = array( $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, $pagenow ); + $menu = array(); + $submenu = array(); + + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + $this->settings = new Settings(); + } + + public function tear_down() { + global $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, $pagenow; + list( $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, $pagenow ) = $this->saved; + + unset( $_GET['page'] ); + $_GET = array(); + remove_all_filters( 'frontblocks_settings_tabs' ); + remove_all_filters( 'wp_redirect' ); + remove_all_actions( 'toplevel_page_frontblocks-settings' ); + delete_option( 'frbl_activation_redirect' ); + delete_option( 'frontblocks_settings' ); + wp_dequeue_style( 'frontblocks-admin-settings' ); + + parent::tear_down(); + } + + /** + * Slugs of the submenu entries of the FrontBlocks menu, in order. + * + * @return string[] + */ + private function submenu_slugs() { + global $submenu; + + return isset( $submenu['frontblocks-settings'] ) ? wp_list_pluck( $submenu['frontblocks-settings'], 2 ) : array(); + } + + /** + * Run the legacy redirect and return the URL it sends the visitor to. + * + * @return string|null The location, or null when nothing was redirected. + */ + private function legacy_redirect_location() { + add_filter( + 'wp_redirect', + function ( $location ) { + throw new RuntimeException( $location ); + } + ); + + try { + $this->settings->redirect_legacy_url(); + } catch ( RuntimeException $e ) { + return $e->getMessage(); + } + + return null; + } + + /* + * --------------------------------------------------------------- + * The menu + * --------------------------------------------------------------- + */ + + public function test_the_settings_are_a_menu_of_their_own_and_no_longer_under_appearance() { + global $menu, $submenu; + + $this->settings->register_menu(); + + $entry = null; + foreach ( $menu as $position => $item ) { + if ( 'frontblocks-settings' === $item[2] ) { + $entry = array( $position, $item ); + } + } + + $this->assertNotNull( $entry, 'The FrontBlocks menu must exist.' ); + $this->assertSame( 'edit_theme_options', $entry[1][1] ); + $this->assertSame( 'FrontBlocks', $entry[1][0] ); + $this->assertStringEndsWith( 'assets/admin/icons/icon-menu.svg', $entry[1][6] ); + $this->assertEquals( 60.5, (float) $entry[0], 'It goes right below Appearance.' ); + + $under_appearance = isset( $submenu['themes.php'] ) ? wp_list_pluck( $submenu['themes.php'], 2 ) : array(); + $this->assertNotContains( 'frontblocks-settings', $under_appearance ); + } + + public function test_every_tab_has_a_submenu_entry_in_the_order_of_the_tabs() { + global $submenu; + + $this->settings->register_menu(); + + $this->assertSame( + array( + 'frontblocks-settings', + 'frontblocks-settings-pro', + 'frontblocks-settings-optional', + 'frontblocks-settings-image-management', + 'frontblocks-settings-maintenance', + 'frontblocks-settings-woocommerce', + 'frontblocks-settings-cookies', + 'frontblocks-settings-google-signin', + 'frontblocks-settings-cpt', + 'frontblocks-settings-license', + ), + $this->submenu_slugs() + ); + + $labels = wp_list_pluck( $submenu['frontblocks-settings'], 0 ); + $this->assertSame( 'Blocks', $labels[0], 'The first entry replaces the menu title as the way to the first tab.' ); + $this->assertContains( 'Image Management', $labels ); + $this->assertContains( 'Social Login', $labels ); + foreach ( $submenu['frontblocks-settings'] as $item ) { + $this->assertSame( 'edit_theme_options', $item[1] ); + } + } + + public function test_each_entry_has_its_own_page_title() { + global $submenu; + + $this->settings->register_menu(); + + $titles = wp_list_pluck( $submenu['frontblocks-settings'], 3 ); + $this->assertContains( 'FrontBlocks: Maintenance', $titles ); + $this->assertCount( count( $titles ), array_unique( $titles ) ); + } + + public function test_a_tab_added_by_a_companion_plugin_gets_a_submenu_entry() { + add_filter( + 'frontblocks_settings_tabs', + function ( $tabs ) { + $tabs[] = array( + 'id' => 'leads', + 'label' => 'Leads', + ); + return $tabs; + } + ); + + $this->settings->register_menu(); + + $this->assertContains( 'frontblocks-settings-leads', $this->submenu_slugs() ); + } + + public function test_a_tab_can_stay_out_of_the_menu() { + add_filter( + 'frontblocks_settings_tabs', + function ( $tabs ) { + $tabs[] = array( + 'id' => 'hidden-one', + 'label' => 'Hidden one', + 'menu' => false, + ); + return $tabs; + } + ); + + $this->settings->register_menu(); + + $this->assertNotContains( 'frontblocks-settings-hidden-one', $this->submenu_slugs() ); + } + + public function test_tabs_that_are_not_valid_are_left_out_of_the_menu() { + add_filter( + 'frontblocks_settings_tabs', + function ( $tabs ) { + $tabs[] = array( 'label' => 'No id' ); + $tabs[] = array( 'id' => 'no-label' ); + $tabs[] = array( + 'id' => 5, + 'label' => 'Number', + ); + $tabs[] = array( + 'id' => '', + 'label' => 'Empty id', + ); + return $tabs; + } + ); + + $this->settings->register_menu(); + + $this->assertCount( 10, $this->submenu_slugs() ); + } + + public function test_a_filter_that_returns_something_else_than_tabs_leaves_the_menu_without_entries() { + add_filter( 'frontblocks_settings_tabs', '__return_false' ); + + $this->settings->register_menu(); + + $this->assertSame( array(), $this->submenu_slugs() ); + } + + public function test_the_page_is_rendered_once_per_entry_and_not_twice_for_the_first() { + global $wp_filter; + + $this->settings->register_menu(); + + $this->assertCount( 1, $wp_filter['toplevel_page_frontblocks-settings']->callbacks[10] ); + $this->assertNotFalse( has_action( get_plugin_page_hookname( 'frontblocks-settings-maintenance', 'frontblocks-settings' ), array( $this->settings, 'render_page' ) ) ); + } + + /* + * --------------------------------------------------------------- + * URLs and screens + * --------------------------------------------------------------- + */ + + public function test_page_slug_of_a_tab() { + $this->assertSame( 'frontblocks-settings', Settings::get_page_slug() ); + $this->assertSame( 'frontblocks-settings', Settings::get_page_slug( 'blocks' ) ); + $this->assertSame( 'frontblocks-settings-backup', Settings::get_page_slug( 'backup' ) ); + $this->assertSame( 'frontblocks-settings-image-management', Settings::get_page_slug( 'image-management' ) ); + $this->assertSame( 'frontblocks-settings-evilscript', Settings::get_page_slug( 'EVIL" thing', + 'list' => array( 'nested' ), + 'Weird Key!' => 'x', + ); + + $location = $this->legacy_redirect_location(); + + $this->assertStringNotContainsString( 'assertStringNotContainsString( 'list', $location ); + $this->assertStringContainsString( 'gsi-error=Bad%20thing', $location ); + $this->assertStringContainsString( 'weirdkey=x', $location ); + } + + public function test_the_old_url_never_leaves_the_site() { + global $pagenow; + $pagenow = 'themes.php'; + $_GET = array( + 'page' => 'frontblocks-settings', + 'redirect' => 'https://evil.example/', + ); + + $location = $this->legacy_redirect_location(); + + $this->assertStringStartsWith( admin_url( 'admin.php' ), $location ); + } + + public function test_other_pages_are_not_redirected() { + global $pagenow; + + $pagenow = 'themes.php'; + $_GET = array( 'page' => 'custom-background' ); + $this->assertNull( $this->legacy_redirect_location() ); + + $pagenow = 'themes.php'; + $_GET = array(); + $this->assertNull( $this->legacy_redirect_location() ); + + // The same page slug on another admin file is not the old settings URL. + $pagenow = 'admin.php'; + $_GET = array( 'page' => 'frontblocks-settings' ); + $this->assertNull( $this->legacy_redirect_location() ); + + $pagenow = 'themes.php'; + $_GET = array( 'page' => array( 'frontblocks-settings' ) ); + $this->assertNull( $this->legacy_redirect_location() ); + } + + public function test_the_old_url_is_left_to_wordpress_for_users_who_cannot_open_the_settings() { + global $pagenow; + $pagenow = 'themes.php'; + $_GET = array( 'page' => 'frontblocks-settings' ); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); + + $this->assertNull( $this->legacy_redirect_location() ); + } + + public function test_the_redirect_runs_while_the_menu_is_built() { + $this->assertSame( 99, has_action( 'admin_menu', array( $this->settings, 'redirect_legacy_url' ) ) ); + $this->assertSame( 10, has_action( 'admin_menu', array( $this->settings, 'register_menu' ) ) ); + } + + /* + * --------------------------------------------------------------- + * Links in the rest of the plugin + * --------------------------------------------------------------- + */ + + public function test_google_sign_in_returns_to_its_tab() { + $settings = new \FrontBlocks\GoogleSignIn\Settings(); + $method = new ReflectionMethod( $settings, 'settings_url' ); + $method->setAccessible( true ); + + $this->assertSame( admin_url( 'admin.php?page=frontblocks-settings-google-signin' ), $method->invoke( $settings ) ); + } + + public function test_the_admin_bar_maintenance_link_opens_the_maintenance_tab() { + $nodes = array(); + $bar = new class( $nodes ) { + public $nodes = array(); + public function __construct( &$nodes ) { + $this->nodes = &$nodes; + } + public function add_node( $args ) { + $this->nodes[] = $args; + } + }; + + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + ( new Maintenance() )->add_admin_bar_node( $bar ); + + $this->assertSame( admin_url( 'admin.php?page=frontblocks-settings-maintenance' ), $nodes[0]['href'] ); + } + + public function test_the_activation_redirect_opens_the_new_menu() { + update_option( 'frbl_activation_redirect', true ); + $_GET = array(); + + add_filter( + 'wp_redirect', + function ( $location ) { + throw new RuntimeException( $location ); + } + ); + + try { + frbl_plugin_activation_redirect(); + $this->fail( 'The activation redirect must run.' ); + } catch ( RuntimeException $e ) { + $this->assertSame( admin_url( 'admin.php?page=frontblocks-settings' ), $e->getMessage() ); + } + } + + public function test_redundant_plugin_notices_link_to_the_new_menu() { + $method = new ReflectionMethod( \FrontBlocks\Admin\RedundantPlugins::class, 'get_default_entries' ); + $method->setAccessible( true ); + $entries = $method->invoke( null ); + + $this->assertSame( admin_url( 'admin.php?page=frontblocks-settings' ), $entries['svg-upload']['doc_url'] ); + $this->assertSame( admin_url( 'admin.php?page=frontblocks-settings-google-signin' ), $entries['login-google']['doc_url'] ); + } + + public function test_the_cookie_notice_migration_notice_shows_on_every_settings_screen() { + update_option( 'frontblocks_settings', array( 'enable_cookie_notice' => true ) ); + $notice = new \FrontBlocks\Admin\CookieNoticeDeprecationNotice(); + + foreach ( array( 'toplevel_page_frontblocks-settings', 'frontblocks_page_frontblocks-settings-cookies' ) as $screen ) { + set_current_screen( $screen ); + ob_start(); + $notice->render_notice(); + $this->assertNotSame( '', ob_get_clean(), $screen ); + } + + set_current_screen( 'edit-post' ); + ob_start(); + $notice->render_notice(); + $this->assertSame( '', ob_get_clean(), 'It stays out of unrelated screens.' ); + } +} diff --git a/tests/Unit/SettingsTest.php b/tests/Unit/SettingsTest.php index b32743ac..64546b80 100644 --- a/tests/Unit/SettingsTest.php +++ b/tests/Unit/SettingsTest.php @@ -176,27 +176,6 @@ public function test_deactivate_short_description_and_move_content_can_both_be_o $this->assertFalse( $sanitized['move_content_to_short_description'] ); } - public function test_register_menu_adds_the_settings_page_under_appearance() { - global $submenu; - - set_current_screen( 'dashboard' ); - wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); - - $this->settings->register_menu(); - - $this->assertArrayHasKey( 'themes.php', $submenu ); - - $found = false; - foreach ( $submenu['themes.php'] as $item ) { - if ( 'frontblocks-settings' === $item[2] ) { - $found = true; - break; - } - } - - $this->assertTrue( $found, 'Expected a "frontblocks-settings" entry under the Appearance menu.' ); - } - public function test_settings_tab_extensions_render_matching_tabs_and_panels() { wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); @@ -253,7 +232,7 @@ public function test_enqueue_admin_styles_does_nothing_outside_the_settings_page } public function test_enqueue_admin_styles_enqueues_assets_on_the_settings_page_hook() { - $this->settings->enqueue_admin_styles( 'appearance_page_frontblocks-settings' ); + $this->settings->enqueue_admin_styles( 'toplevel_page_frontblocks-settings' ); $this->assertTrue( wp_style_is( 'frontblocks-admin-settings', 'enqueued' ) ); diff --git a/tests/Unit/StagingTest.php b/tests/Unit/StagingTest.php index 59b8debd..ca0fb5d5 100644 --- a/tests/Unit/StagingTest.php +++ b/tests/Unit/StagingTest.php @@ -712,7 +712,7 @@ public function test_admin_notice_shows_on_the_dashboard_and_the_settings_screen $staging = $this->boot_staging(); wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); - foreach ( array( 'dashboard', 'appearance_page_frontblocks-settings' ) as $screen ) { + foreach ( array( 'dashboard', 'toplevel_page_frontblocks-settings', 'frontblocks_page_frontblocks-settings-maintenance', 'appearance_page_frontblocks-settings' ) as $screen ) { set_current_screen( $screen ); $html = $this->capture( array( $staging, 'render_admin_notice' ) );