Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/SETTINGS-TAB-EXTENSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion docs/STAGING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
139 changes: 117 additions & 22 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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;
});
}

Expand Down Expand Up @@ -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 "<menu>_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;
}
}
}

/**
Expand All @@ -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' )
);
}
Expand All @@ -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 ) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
?>
Expand Down
77 changes: 65 additions & 12 deletions includes/Frontend/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace FrontBlocks\Frontend;

use FrontBlocks\Admin\Settings;

defined( 'ABSPATH' ) || exit;

/**
Expand All @@ -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.
*/
Expand All @@ -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' ) );
Expand Down Expand Up @@ -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' ),
Expand All @@ -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 );
Expand Down Expand Up @@ -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).
*
Expand Down
Loading
Loading