diff --git a/docs/SETTINGS-TAB-EXTENSIONS.md b/docs/SETTINGS-TAB-EXTENSIONS.md index 5b46333f..4f103118 100644 --- a/docs/SETTINGS-TAB-EXTENSIONS.md +++ b/docs/SETTINGS-TAB-EXTENSIONS.md @@ -6,6 +6,8 @@ FrontBlocks exposes two hooks for companion plugins that need a dedicated tab on Use the `frontblocks_settings_tabs` filter to append a tab definition. Each tab requires a unique `id` and a user-facing `label`. +Add the filter before the `admin_menu` action runs: at the top level of your plugin file as in the example below, or on `plugins_loaded` or `init`. WordPress builds the admin menu before `admin_init`, so a tab added on `admin_init` or later gets no menu entry and no page of its own. + Use the `frontblocks_settings_tab_panels` action to output its panel. The panel must use the same ID in its `data-tab-panel` attribute and include the `frbl-tab-panel` class. Add the `hidden` attribute so the standard tab script controls its initial visibility. ```php @@ -40,10 +42,12 @@ FrontBlocks has a menu of its own in the admin sidebar, right below Appearance, | `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. +- 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: it has no page of its own, so clicking it keeps the current page and adds its `#tab` to the URL. +- The first entry, `admin.php?page=frontblocks-settings`, is always in the submenu, even if a filter removes the `blocks` tab or hides it. - 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`. +- A post type of your plugin can live in the same menu: register it with `'show_in_menu' => \FrontBlocks\Admin\Settings::PAGE_SLUG`. Its entry goes after the settings tabs. WordPress does not add the taxonomy entries of a post type shown in another menu, so add them with `add_submenu_page()` (FrontBlocks does this for the event categories). With older versions of FrontBlocks, keep `'show_in_menu' => true`. 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/STAGING.md b/docs/STAGING.md index 2d41c8a8..a5997090 100644 --- a/docs/STAGING.md +++ b/docs/STAGING.md @@ -4,7 +4,7 @@ Staging Mode applies a set of safety measures when a site is a staging or copy e Everything is applied at runtime through filters. FrontBlocks never changes a stored option (`blog_public`, gateway settings...), so the behaviour is reversible and is not carried over by a database copy. -The implementation is `FrontBlocks\Frontend\Staging` (`includes/Frontend/Staging.php`). Its settings live in the **Maintenance** tab of the FrontBlocks settings (Appearance → FrontBlocks), in the **Staging Mode** section below Maintenance Mode. That section also shows why staging mode is active. +The implementation is `FrontBlocks\Frontend\Staging` (`includes/Frontend/Staging.php`). Its settings live in the **Maintenance** tab of the FrontBlocks settings (FrontBlocks → Maintenance in the admin menu), in the **Staging Mode** section below Maintenance Mode. That section also shows why staging mode is active. ## When it is active diff --git a/includes/Admin/Settings.php b/includes/Admin/Settings.php index 8c1cd46d..05ee4518 100644 --- a/includes/Admin/Settings.php +++ b/includes/Admin/Settings.php @@ -286,7 +286,11 @@ public function __construct() { // Check license via FrontBlocks PRO helper function. $this->is_license_valid = function_exists( 'frblp_is_license_valid' ) && frblp_is_license_valid(); - add_action( 'admin_menu', array( $this, 'register_menu' ) ); + // Before WordPress adds the entries of the post types shown in this menu (priority 10), + // so the settings come first and the menu opens them. + add_action( 'admin_menu', array( $this, 'register_menu' ), 9 ); + // After every other entry, so License closes the menu. + add_action( 'admin_menu', array( $this, 'move_license_entry_last' ), 98 ); 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' ) ); @@ -361,7 +365,8 @@ function updateMutualExclusion() { } else { moveContentCheckbox.disabled = !isLicenseValid; if (moveContentWrapper) { - moveContentWrapper.style.opacity = isLicenseValid ? '1' : '0.5'; + // Leave the locked PRO look to the .frbl-feature-pro styles. + moveContentWrapper.style.opacity = ''; moveContentWrapper.style.filter = ''; const toggle = moveContentWrapper.querySelector('.frbl-toggle'); if (toggle) { @@ -385,7 +390,8 @@ function updateMutualExclusion() { } else { deactivateCheckbox.disabled = !isLicenseValid; if (deactivateWrapper) { - deactivateWrapper.style.opacity = isLicenseValid ? '1' : '0.5'; + // Leave the locked PRO look to the .frbl-feature-pro styles. + deactivateWrapper.style.opacity = ''; deactivateWrapper.style.filter = ''; const toggle = deactivateWrapper.querySelector('.frbl-toggle'); if (toggle) { @@ -711,17 +717,22 @@ function syncMenu(tabId) { } // Point the address bar at the page of the tab, so a reload or a copied link opens it. + // A tab without a page of its own keeps the current page and is opened by its #hash. function updateUrl(tabId) { var page = tabPages[tabId]; - if (! page || ! window.history || ! window.history.replaceState) { + if (! tabId || ! window.history || ! window.history.replaceState) { return; } var url = new URL(window.location.href); - url.search = ''; - url.hash = ''; - url.searchParams.set('page', page); + if (page) { + url.search = ''; + url.hash = ''; + url.searchParams.set('page', page); + } else { + url.hash = tabId; + } window.history.replaceState(null, '', url.toString()); } @@ -807,7 +818,20 @@ function activateTab(tabId) { var active = document.querySelector('.frbl-tab-btn.is-active'); var tabId = active ? active.getAttribute('data-tab-target') : initialTab; var url = referer.value.split('#')[0]; - referer.value = url + '#' + tabId; + var page = tabPages[tabId]; + + // Return to the page of the active tab, so the URL and the tab match. + if (page) { + try { + var parsed = new URL(url, window.location.href); + parsed.searchParams.set('page', page); + url = parsed.pathname + parsed.search; + } catch (error) { + page = ''; + } + } + + referer.value = page ? url : url + '#' + tabId; }); } @@ -965,7 +989,63 @@ public static function get_settings_url( $tab = '' ) { * @return bool */ public static function is_settings_screen( $screen_id ) { - return is_string( $screen_id ) && false !== strpos( $screen_id, self::PAGE_SLUG ); + if ( ! is_string( $screen_id ) ) { + return false; + } + + // The page slug, or the page slug and a tab id, alone or after the "_page_" prefix of a hook. + $pattern = '/^(?:[a-z0-9_-]+_page_)?' . preg_quote( self::PAGE_SLUG, '/' ) . '(?:-[a-z0-9_-]+)?$/'; + + return 1 === preg_match( $pattern, $screen_id ); + } + + /** + * Slug of the menu page of a tab, when the tab gets one. + * + * The menu and the tab script both follow this rule, so the script never points + * the address bar at a page that was not registered. + * + * @since 1.6.0 + * @param mixed $tab Tab definition, as returned by the frontblocks_settings_tabs filter. + * @return string The page slug, or an empty string when the tab has no menu page. + */ + private static function get_tab_menu_page( $tab ) { + if ( ! is_array( $tab ) || ! isset( $tab['id'], $tab['label'] ) || ! is_string( $tab['id'] ) || ! is_string( $tab['label'] ) || '' === sanitize_key( $tab['id'] ) ) { + return ''; + } + + // A companion plugin can keep a tab out of the menu with 'menu' => false. + if ( isset( $tab['menu'] ) && false === $tab['menu'] ) { + return ''; + } + + return self::get_page_slug( $tab['id'] ); + } + + /** + * Move the License entry to the end of the FrontBlocks menu. + * + * The post types shown in the menu (Testimonials, Events, Popups) are added by + * WordPress after the settings tabs, so License would end up in the middle. + * + * @since 1.6.0 + * @return void + */ + public function move_license_entry_last() { + global $submenu; + + if ( empty( $submenu[ self::PAGE_SLUG ] ) || ! is_array( $submenu[ self::PAGE_SLUG ] ) ) { + return; + } + + $license = self::get_page_slug( 'license' ); + foreach ( $submenu[ self::PAGE_SLUG ] as $position => $entry ) { + if ( isset( $entry[2] ) && $license === $entry[2] ) { + unset( $submenu[ self::PAGE_SLUG ][ $position ] ); + $submenu[ self::PAGE_SLUG ][] = $entry; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Reorders the entries of this plugin's own menu. + return; + } + } } /** @@ -988,23 +1068,29 @@ public function register_menu() { 60.5 ); + $entries = array(); foreach ( $this->get_tabs() as $tab ) { - if ( ! isset( $tab['id'], $tab['label'] ) || ! is_string( $tab['id'] ) || ! is_string( $tab['label'] ) || '' === $tab['id'] ) { - continue; - } + $page = self::get_tab_menu_page( $tab ); - // A companion plugin can keep a tab out of the menu with 'menu' => false. - if ( isset( $tab['menu'] ) && false === $tab['menu'] ) { - continue; + if ( '' !== $page && ! isset( $entries[ $page ] ) ) { + $entries[ $page ] = $tab['label']; } + } + // The page of the menu itself always gets the first entry, even when a filter + // removes or hides the first tab, so its URL never opens an empty page. + if ( ! isset( $entries[ self::PAGE_SLUG ] ) ) { + $entries = array( self::PAGE_SLUG => __( 'Settings', 'frontblocks' ) ) + $entries; + } + + foreach ( $entries as $page => $label ) { add_submenu_page( self::PAGE_SLUG, /* translators: %s: name of the settings tab, for example Backup */ - sprintf( __( 'FrontBlocks: %s', 'frontblocks' ), $tab['label'] ), - $tab['label'], + sprintf( __( 'FrontBlocks: %s', 'frontblocks' ), $label ), + $label, self::CAPABILITY, - self::get_page_slug( $tab['id'] ), + $page, array( $this, 'render_page' ) ); } @@ -1030,6 +1116,12 @@ public function redirect_legacy_url() { return; } + // A form still posting to the old URL would lose its data in a redirect, so only links are sent on. + $method = isset( $_SERVER['REQUEST_METHOD'] ) && is_string( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET'; + if ( 'GET' !== $method && 'HEAD' !== $method ) { + return; + } + $args = array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only forwards sanitized values to the new URL. foreach ( $_GET as $key => $value ) { @@ -1038,7 +1130,8 @@ public function redirect_legacy_url() { } } - wp_safe_redirect( add_query_arg( $args, self::get_settings_url() ) ); + // The values are encoded, as add_query_arg() does not, so '&', '#', '=' and '+' survive. + wp_safe_redirect( add_query_arg( array_map( 'rawurlencode', $args ), self::get_settings_url() ) ); exit; } @@ -1478,7 +1571,7 @@ private function get_tabs( $counts = array() ) { * @return void */ public function render_page() { - if ( ! current_user_can( 'edit_theme_options' ) ) { + if ( ! current_user_can( self::CAPABILITY ) ) { return; } @@ -1509,8 +1602,10 @@ public function render_page() { $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'] ); + // Only the tabs with a page of their own in the menu, so a reload of the address bar opens a registered page. + $tab_page = self::get_tab_menu_page( $tab ); + if ( '' !== $tab_page ) { + $tab_pages[ $tab['id'] ] = $tab_page; } } ?> diff --git a/includes/Frontend/Events.php b/includes/Frontend/Events.php index 51ce0a6d..d22cb692 100644 --- a/includes/Frontend/Events.php +++ b/includes/Frontend/Events.php @@ -10,6 +10,8 @@ namespace FrontBlocks\Frontend; +use FrontBlocks\Admin\Settings; + defined( 'ABSPATH' ) || exit; /** @@ -32,6 +34,13 @@ class Events { */ private $option_events_type = 'events_type'; + /** + * Screen of the event categories, as an entry of the FrontBlocks menu. + * + * @var string + */ + const CATEGORY_SCREEN = 'edit-tags.php?taxonomy=event_category&post_type=event'; + /** * Constructor. */ @@ -47,6 +56,10 @@ public function __construct() { add_action( 'init', array( $this, 'register_cpt_event' ) ); add_action( 'init', array( $this, 'register_taxonomy_event_category' ) ); + // Events live in the FrontBlocks menu, with their categories next to them. + add_action( 'admin_menu', array( $this, 'add_category_submenu' ) ); + add_filter( 'parent_file', array( $this, 'highlight_category_submenu' ) ); + // Meta boxes for CPT. add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); add_action( 'save_post_event', array( $this, 'save_meta' ) ); @@ -87,7 +100,7 @@ public function register_cpt_event() { 'name' => __( 'Events', 'frontblocks' ), 'singular_name' => __( 'Event', 'frontblocks' ), 'menu_name' => __( 'Events', 'frontblocks' ), - 'all_items' => __( 'All events', 'frontblocks' ), + 'all_items' => __( 'Events', 'frontblocks' ), 'add_new' => __( 'Add event', 'frontblocks' ), 'add_new_item' => __( 'Add new event', 'frontblocks' ), 'edit_item' => __( 'Edit event', 'frontblocks' ), @@ -98,17 +111,16 @@ public function register_cpt_event() { ); $args = array( - 'label' => __( 'Events', 'frontblocks' ), - 'labels' => $labels, - 'public' => true, - 'show_ui' => true, - 'show_in_rest' => true, - 'has_archive' => true, - 'menu_position' => 6, - 'menu_icon' => 'dashicons-calendar-alt', - 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ), - 'rewrite' => array( 'slug' => 'evento' ), - 'taxonomies' => array( 'post_tag', 'event_category' ), + 'label' => __( 'Events', 'frontblocks' ), + 'labels' => $labels, + 'public' => true, + 'show_ui' => true, + 'show_in_menu' => Settings::PAGE_SLUG, + 'show_in_rest' => true, + 'has_archive' => true, + 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ), + 'rewrite' => array( 'slug' => 'evento' ), + 'taxonomies' => array( 'post_tag', 'event_category' ), ); register_post_type( 'event', $args ); @@ -143,6 +155,47 @@ public function register_taxonomy_event_category() { register_taxonomy( 'event_category', array( 'event' ), $args ); } + /** + * Add the event categories to the FrontBlocks menu. + * + * WordPress only adds the taxonomy entries of a post type that has a menu of its own, + * so the categories need an entry of their own once events are in the FrontBlocks menu. + * + * @since 1.6.0 + * @return void + */ + public function add_category_submenu() { + $taxonomy = get_taxonomy( 'event_category' ); + if ( ! $taxonomy ) { + return; + } + + add_submenu_page( + Settings::PAGE_SLUG, + $taxonomy->labels->name, + $taxonomy->labels->name, + $taxonomy->cap->manage_terms, + self::CATEGORY_SCREEN + ); + } + + /** + * Keep the FrontBlocks menu open on the event categories screen. + * + * @since 1.6.0 + * @param string $parent_file Parent file of the current admin screen. + * @return string + */ + public function highlight_category_submenu( $parent_file ) { + global $taxnow; + + if ( 'event_category' === $taxnow ) { + return Settings::PAGE_SLUG; + } + + return $parent_file; + } + /** * Add meta boxes for event data (CPT). * diff --git a/includes/Frontend/Testimonials.php b/includes/Frontend/Testimonials.php index e0a5e785..1c7ab3b9 100644 --- a/includes/Frontend/Testimonials.php +++ b/includes/Frontend/Testimonials.php @@ -10,6 +10,8 @@ namespace FrontBlocks\Frontend; +use FrontBlocks\Admin\Settings; + defined( 'ABSPATH' ) || exit; /** @@ -96,7 +98,7 @@ public function register_cpt_testimonials() { 'name' => _x( 'Testimonials', 'Post Type General Name', 'frontblocks' ), 'singular_name' => _x( 'Testimonial', 'Post Type Singular Name', 'frontblocks' ), 'menu_name' => __( 'Testimonials', 'frontblocks' ), - 'all_items' => __( 'All Testimonials', 'frontblocks' ), + 'all_items' => __( 'Testimonials', 'frontblocks' ), 'add_new_item' => __( 'Add New Testimonial', 'frontblocks' ), 'add_new' => __( 'Add New', 'frontblocks' ), 'new_item' => __( 'New Testimonial', 'frontblocks' ), @@ -108,15 +110,13 @@ public function register_cpt_testimonials() { ); $args = array( - 'label' => __( 'Testimonials', 'frontblocks' ), - 'labels' => $labels, - 'supports' => array( 'title', 'editor', 'thumbnail' ), - 'public' => true, - 'show_ui' => true, - 'show_in_menu' => true, - 'menu_position' => 5, - 'menu_icon' => 'dashicons-testimonial', - 'can_export' => true, + 'label' => __( 'Testimonials', 'frontblocks' ), + 'labels' => $labels, + 'supports' => array( 'title', 'editor', 'thumbnail' ), + 'public' => true, + 'show_ui' => true, + 'show_in_menu' => Settings::PAGE_SLUG, + 'can_export' => true, ); register_post_type( 'fbrl_testimonial', $args ); diff --git a/includes/GoogleSignIn/GoogleSignIn.php b/includes/GoogleSignIn/GoogleSignIn.php index baf036f8..697def18 100644 --- a/includes/GoogleSignIn/GoogleSignIn.php +++ b/includes/GoogleSignIn/GoogleSignIn.php @@ -544,7 +544,7 @@ private function not_configured_notice() { return ''; } - return '

' . esc_html__( 'Google Sign-In is not configured yet. Add your Client ID and Client Secret under Appearance → FrontBlocks → Google Sign-In.', 'frontblocks' ) . '

'; + return '

' . esc_html__( 'Google Sign-In is not configured yet. Add your Client ID and Client Secret under FrontBlocks → Social Login.', 'frontblocks' ) . '

'; } /** diff --git a/readme.txt b/readme.txt index 59dc5c12..aa7a1240 100644 --- a/readme.txt +++ b/readme.txt @@ -224,7 +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()`. +* 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. Testimonials and Events (with their categories) move into the same menu. 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/EventsTest.php b/tests/Unit/EventsTest.php index 3b8b85dd..b17b12c3 100644 --- a/tests/Unit/EventsTest.php +++ b/tests/Unit/EventsTest.php @@ -116,6 +116,68 @@ public function test_register_cpt_event_registers_the_event_post_type() { unregister_post_type( 'event' ); } + public function test_events_live_in_the_frontblocks_menu() { + update_option( 'frontblocks_settings', array( 'enable_events' => true ) ); + $events = new Events(); + $events->register_cpt_event(); + + $this->assertSame( 'frontblocks-settings', get_post_type_object( 'event' )->show_in_menu ); + $this->assertNotFalse( has_action( 'admin_menu', array( $events, 'add_category_submenu' ) ) ); + $this->assertNotFalse( has_filter( 'parent_file', array( $events, 'highlight_category_submenu' ) ) ); + + unregister_post_type( 'event' ); + } + + public function test_event_categories_get_an_entry_in_the_frontblocks_menu() { + global $submenu; + $saved = $submenu; + $submenu = array(); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + update_option( 'frontblocks_settings', array( 'enable_events' => true ) ); + $events = new Events(); + $events->register_cpt_event(); + $events->register_taxonomy_event_category(); + $events->add_category_submenu(); + + $entries = isset( $submenu['frontblocks-settings'] ) ? $submenu['frontblocks-settings'] : array(); + $submenu = $saved; + unregister_taxonomy( 'event_category' ); + unregister_post_type( 'event' ); + + $this->assertCount( 1, $entries ); + $this->assertSame( 'edit-tags.php?taxonomy=event_category&post_type=event', $entries[0][2] ); + $this->assertSame( 'manage_categories', $entries[0][1] ); + } + + public function test_no_category_entry_without_the_taxonomy() { + global $submenu; + $saved = $submenu; + $submenu = array(); + + $events = new Events(); + $events->add_category_submenu(); + + $entries = isset( $submenu['frontblocks-settings'] ) ? $submenu['frontblocks-settings'] : array(); + $submenu = $saved; + + $this->assertSame( array(), $entries ); + } + + public function test_the_menu_stays_open_on_the_event_categories_screen() { + global $taxnow; + $saved = $taxnow; + $events = new Events(); + + $taxnow = 'event_category'; + $this->assertSame( 'frontblocks-settings', $events->highlight_category_submenu( 'edit.php?post_type=event' ) ); + + $taxnow = 'category'; + $this->assertSame( 'edit.php', $events->highlight_category_submenu( 'edit.php' ) ); + + $taxnow = $saved; + } + public function test_register_taxonomy_registers_event_category() { update_option( 'frontblocks_settings', array( 'enable_events' => true ) ); $events = new Events(); diff --git a/tests/Unit/SettingsMenuTest.php b/tests/Unit/SettingsMenuTest.php index e0ad0333..76e88b1c 100644 --- a/tests/Unit/SettingsMenuTest.php +++ b/tests/Unit/SettingsMenuTest.php @@ -26,7 +26,7 @@ public function set_up() { parent::set_up(); global $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, $pagenow; - $this->saved = array( $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, $pagenow ); + $this->saved = array( $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, $pagenow, isset( $_SERVER['REQUEST_METHOD'] ) ? $_SERVER['REQUEST_METHOD'] : null ); $menu = array(); $submenu = array(); @@ -36,7 +36,12 @@ public function set_up() { 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; + list( $menu, $submenu, $admin_page_hooks, $_registered_pages, $_parent_pages, $pagenow, $request_method ) = $this->saved; + if ( null === $request_method ) { + unset( $_SERVER['REQUEST_METHOD'] ); + } else { + $_SERVER['REQUEST_METHOD'] = $request_method; + } unset( $_GET['page'] ); $_GET = array(); @@ -209,12 +214,50 @@ function ( $tabs ) { $this->assertCount( 10, $this->submenu_slugs() ); } - public function test_a_filter_that_returns_something_else_than_tabs_leaves_the_menu_without_entries() { + public function test_a_filter_that_returns_something_else_than_tabs_leaves_only_the_main_entry() { add_filter( 'frontblocks_settings_tabs', '__return_false' ); $this->settings->register_menu(); - $this->assertSame( array(), $this->submenu_slugs() ); + $this->assertSame( array( 'frontblocks-settings' ), $this->submenu_slugs() ); + } + + public function test_the_main_page_is_registered_even_when_the_first_tab_is_removed_or_hidden() { + global $wp_filter; + + $filters = array( + 'removed' => function ( $tabs ) { + return array_values( + array_filter( + $tabs, + function ( $tab ) { + return 'blocks' !== $tab['id']; + } + ) + ); + }, + 'hidden' => function ( $tabs ) { + $tabs[0]['menu'] = false; + return $tabs; + }, + ); + + foreach ( $filters as $case => $filter ) { + global $submenu; + $submenu = array(); + remove_all_filters( 'frontblocks_settings_tabs' ); + remove_all_actions( 'toplevel_page_frontblocks-settings' ); + add_filter( 'frontblocks_settings_tabs', $filter ); + + $this->settings->register_menu(); + + $slugs = $this->submenu_slugs(); + $this->assertSame( 'frontblocks-settings', $slugs[0], $case ); + $this->assertCount( 1, array_keys( $slugs, 'frontblocks-settings', true ), $case ); + $this->assertContains( 'frontblocks-settings-pro', $slugs, $case ); + $this->assertSame( 10, has_action( 'toplevel_page_frontblocks-settings', array( $this->settings, 'render_page' ) ), $case ); + $this->assertCount( 1, $wp_filter['toplevel_page_frontblocks-settings']->callbacks[10], $case ); + } } public function test_the_page_is_rendered_once_per_entry_and_not_twice_for_the_first() { @@ -267,7 +310,24 @@ public function test_which_screens_are_settings_screens() { $this->assertTrue( Settings::is_settings_screen( 'frontblocks_page_frontblocks-settings-image-management' ) ); $this->assertTrue( Settings::is_settings_screen( 'appearance_page_frontblocks-settings' ), 'The screen the page had under Appearance.' ); - foreach ( array( 'dashboard', 'plugins', 'edit-post', 'toplevel_page_woocommerce', '', null, array(), 5 ) as $other ) { + $this->assertTrue( Settings::is_settings_screen( 'frontblocks-settings' ), 'The page slug alone.' ); + + $others = array( + 'dashboard', + 'plugins', + 'edit-post', + 'toplevel_page_woocommerce', + 'edit-frontblocks-settings-log', + 'toplevel_page_my-frontblocks-settings', + 'frontblocks_page_frontblocks-settingsx', + 'frontblocks-settings_page_other', + 'frontblocks_page_frontblocks-settings-backup/extra', + '', + null, + array(), + 5, + ); + foreach ( $others as $other ) { $this->assertFalse( Settings::is_settings_screen( $other ), wp_json_encode( $other ) ); } } @@ -324,6 +384,64 @@ public function test_the_page_hands_the_script_the_page_of_each_tab() { $this->assertSame( 'frontblocks-settings-google-signin', $pages['google-signin'] ); } + public function test_tabs_without_a_menu_page_are_not_handed_to_the_script() { + add_filter( + 'frontblocks_settings_tabs', + function ( $tabs ) { + $tabs[] = array( + 'id' => 'hidden-one', + 'label' => 'Hidden one', + 'menu' => false, + ); + $tabs[] = array( + 'id' => 'no-label', + 'label' => 5, + ); + $tabs[] = array( + 'id' => '!!!', + 'label' => 'Empty slug', + ); + $tabs[] = array( + 'id' => 'leads', + 'label' => 'Leads', + ); + return $tabs; + } + ); + + $html = $this->render_page_for( 'frontblocks-settings' ); + + $this->assertSame( 1, preg_match( '/data-tab-pages="([^"]*)"/', $html, $found ) ); + $pages = json_decode( html_entity_decode( $found[1], ENT_QUOTES ), true ); + + $this->assertArrayNotHasKey( 'hidden-one', $pages, 'A tab kept out of the menu has no page.' ); + $this->assertArrayNotHasKey( 'no-label', $pages, 'A tab the menu skips has no page.' ); + $this->assertArrayNotHasKey( '!!!', $pages, 'A tab the menu skips has no page.' ); + $this->assertSame( 'frontblocks-settings-leads', $pages['leads'] ); + + // Every page handed to the script is one the menu registers. + $this->settings->register_menu(); + $this->assertSame( array(), array_diff( array_values( $pages ), $this->submenu_slugs() ) ); + } + + public function test_a_hidden_first_tab_is_not_handed_to_the_script() { + add_filter( + 'frontblocks_settings_tabs', + function ( $tabs ) { + $tabs[0]['menu'] = false; + return $tabs; + } + ); + + $html = $this->render_page_for( 'frontblocks-settings' ); + + $this->assertSame( 1, preg_match( '/data-tab-pages="([^"]*)"/', $html, $found ) ); + $pages = json_decode( html_entity_decode( $found[1], ENT_QUOTES ), true ); + + $this->assertArrayNotHasKey( 'blocks', $pages ); + $this->assertSame( 'frontblocks-settings-pro', $pages['pro'] ); + } + public function test_the_page_still_shows_the_toggle_counts_of_the_tabs() { $html = $this->render_page_for( 'frontblocks-settings' ); @@ -339,6 +457,7 @@ public function test_the_tab_script_follows_the_menu() { $this->assertStringContainsString( 'function updateUrl', $script ); $this->assertStringContainsString( "addEventListener('hashchange'", $script ); $this->assertStringContainsString( 'toplevel_page_frontblocks-settings', $script ); + $this->assertStringContainsString( "parsed.searchParams.set('page', page)", $script, 'A save returns to the page of the active tab.' ); $this->assertNotSame( '', $html ); } @@ -378,6 +497,45 @@ public function test_the_old_url_sanitizes_what_it_forwards() { $this->assertStringContainsString( 'weirdkey=x', $location ); } + public function test_the_old_url_keeps_reserved_characters_of_the_values() { + global $pagenow; + $pagenow = 'themes.php'; + $_GET = array( + 'page' => 'frontblocks-settings', + 'gsi-error' => 'a&b=c#d+e', + ); + + $location = $this->legacy_redirect_location(); + + $this->assertStringContainsString( 'gsi-error=a%26b%3Dc%23d%2Be', $location ); + $this->assertStringNotContainsString( '#', $location ); + + $query = array(); + wp_parse_str( (string) wp_parse_url( $location, PHP_URL_QUERY ), $query ); + $this->assertSame( 'a&b=c#d+e', $query['gsi-error'] ); + $this->assertSame( 'frontblocks-settings', $query['page'] ); + } + + public function test_only_get_and_head_requests_to_the_old_url_are_redirected() { + global $pagenow; + + foreach ( array( 'GET', 'HEAD' ) as $method ) { + $pagenow = 'themes.php'; + $_GET = array( 'page' => 'frontblocks-settings' ); + $_SERVER['REQUEST_METHOD'] = $method; + $this->assertSame( admin_url( 'admin.php?page=frontblocks-settings' ), $this->legacy_redirect_location(), $method ); + remove_all_filters( 'wp_redirect' ); + } + + foreach ( array( 'POST', 'PUT' ) as $method ) { + $pagenow = 'themes.php'; + $_GET = array( 'page' => 'frontblocks-settings' ); + $_SERVER['REQUEST_METHOD'] = $method; + $this->assertNull( $this->legacy_redirect_location(), $method ); + remove_all_filters( 'wp_redirect' ); + } + } + public function test_the_old_url_never_leaves_the_site() { global $pagenow; $pagenow = 'themes.php'; @@ -423,7 +581,61 @@ public function test_the_old_url_is_left_to_wordpress_for_users_who_cannot_open_ 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' ) ) ); + $this->assertSame( 9, has_action( 'admin_menu', array( $this->settings, 'register_menu' ) ) ); + $this->assertSame( 98, has_action( 'admin_menu', array( $this->settings, 'move_license_entry_last' ) ) ); + } + + public function test_post_types_of_the_menu_come_after_the_settings() { + register_post_type( + 'frbl_menu_test', + array( + 'label' => 'Menu test', + 'show_ui' => true, + 'show_in_menu' => 'frontblocks-settings', + ) + ); + + // The order of admin_menu: the settings at 9, then WordPress adds the post types at 10. + $this->settings->register_menu(); + _add_post_type_submenus(); + unregister_post_type( 'frbl_menu_test' ); + + $slugs = $this->submenu_slugs(); + $this->assertSame( 'frontblocks-settings', $slugs[0], 'The menu must open the settings.' ); + $this->assertSame( 'edit.php?post_type=frbl_menu_test', end( $slugs ) ); + } + + public function test_license_closes_the_menu_after_the_post_types() { + register_post_type( + 'frbl_menu_test', + array( + 'label' => 'Menu test', + 'show_ui' => true, + 'show_in_menu' => 'frontblocks-settings', + ) + ); + + $this->settings->register_menu(); + _add_post_type_submenus(); + $this->settings->move_license_entry_last(); + unregister_post_type( 'frbl_menu_test' ); + + $slugs = array_values( $this->submenu_slugs() ); + $this->assertSame( 'frontblocks-settings', $slugs[0] ); + $this->assertSame( 'frontblocks-settings-license', end( $slugs ) ); + $this->assertContains( 'edit.php?post_type=frbl_menu_test', $slugs ); + $this->assertSame( 1, count( array_keys( $slugs, 'frontblocks-settings-license', true ) ) ); + } + + public function test_moving_license_is_harmless_without_the_menu_or_the_entry() { + global $submenu; + + $this->settings->move_license_entry_last(); + $this->assertSame( array(), $this->submenu_slugs() ); + + $submenu['frontblocks-settings'] = array( array( 'Blocks', 'edit_theme_options', 'frontblocks-settings' ) ); + $this->settings->move_license_entry_last(); + $this->assertSame( array( 'frontblocks-settings' ), $this->submenu_slugs() ); } /* diff --git a/tests/Unit/SettingsTest.php b/tests/Unit/SettingsTest.php index 587c214f..85508e3d 100644 --- a/tests/Unit/SettingsTest.php +++ b/tests/Unit/SettingsTest.php @@ -259,10 +259,11 @@ private function get_admin_inline_script() { public function test_the_admin_script_has_no_class_selector_with_an_unescaped_colon() { $script = $this->get_admin_inline_script(); - $found = preg_match_all( "/(?:closest|querySelector|querySelectorAll|matches)\(\s*'([^']*)'/", $script, $matches ); + // Single quotes, double quotes and template literals. + $found = preg_match_all( '/(?:closest|querySelector|querySelectorAll|matches)\(\s*([\'"`])(.*?)\1/', $script, $matches ); $this->assertGreaterThan( 5, $found, 'The script must be found, with its selectors.' ); - foreach ( $matches[1] as $selector ) { + foreach ( $matches[2] as $selector ) { // Attribute values can hold anything, only classes and ids can break. $selector = preg_replace( '/\[[^\]]*\]/', '', $selector ); @@ -277,6 +278,11 @@ public function test_the_short_description_options_dim_the_card_that_holds_them( $this->assertStringContainsString( "deactivateCheckbox.closest('.frbl-feature-card')", $script ); $this->assertStringContainsString( "moveContentCheckbox.closest('.frbl-feature-card')", $script ); + // Without a license the card already looks locked (.frbl-feature-pro), so the + // script must not dim the whole card, badge and title included, on top of it. + $this->assertStringNotContainsString( "isLicenseValid ? '1' : '0.5'", $script ); + $this->assertStringContainsString( "moveContentWrapper.style.opacity = '';", $script ); + $this->assertStringContainsString( "deactivateWrapper.style.opacity = '';", $script ); wp_dequeue_style( 'frontblocks-admin-settings' ); } diff --git a/tests/Unit/TestimonialsTest.php b/tests/Unit/TestimonialsTest.php index efc0735d..3cca9a4e 100644 --- a/tests/Unit/TestimonialsTest.php +++ b/tests/Unit/TestimonialsTest.php @@ -68,6 +68,8 @@ public function test_register_cpt_testimonials_registers_the_expected_post_type( $this->assertTrue( post_type_supports( 'fbrl_testimonial', 'title' ) ); $this->assertTrue( post_type_supports( 'fbrl_testimonial', 'editor' ) ); $this->assertTrue( post_type_supports( 'fbrl_testimonial', 'thumbnail' ) ); + // Testimonials live in the FrontBlocks menu, not in a menu of their own. + $this->assertSame( 'frontblocks-settings', $post_type_object->show_in_menu ); } public function test_save_metabox_stars_ignores_request_without_valid_nonce() {