Skip to content

Commit

Permalink
refactor: wizards menu order handling
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe committed Oct 25, 2024
1 parent 26a6106 commit 406bc54
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 131 deletions.
6 changes: 3 additions & 3 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private function includes() {

include_once NEWSPACK_ABSPATH . 'includes/wizards/class-setup-wizard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-components-demo.php';

include_once NEWSPACK_ABSPATH . 'includes/wizards/traits/trait-wizards-admin-header.php';

// Newspack Wizards and Sections.
Expand All @@ -140,8 +140,8 @@ private function includes() {

include_once NEWSPACK_ABSPATH . 'includes/wizards/class-setup-wizard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-components-demo.php';
// Advertising Wizard.

// Advertising Wizard.
include_once NEWSPACK_ABSPATH . 'includes/wizards/advertising/class-advertising-display-ads.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/advertising/class-advertising-sponsors.php';

Expand Down
46 changes: 41 additions & 5 deletions includes/class-wizards.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,25 @@ public static function init() {
'settings' => new Settings(),
// v2 Information Architecture.
'newspack-dashboard' => new Newspack_Dashboard(),
'newspack-settings' => new Newspack_Settings(
'newspack-settings' => new Newspack_Settings(
[
'sections' => [
'custom-events' => 'Newspack\Wizards\Newspack\Custom_Events_Section',
'social-pixels' => 'Newspack\Wizards\Newspack\Pixels_Section',
'recirculation' => 'Newspack\Wizards\Newspack\Recirculation_Section',
],
]
]
),
'advertising-display-ads' => new Advertising_Display_Ads(),
'advertising-sponsors' => new Advertising_Sponsors(),
'network' => new Network_Wizard(),
'newsletters' => new Newsletters_Wizard(),
];

// Not needed in $wizards[] since it's just for Admin Headers, not full react pages.
new Network_Wizard();
new Newsletters_Wizard();
// Allow custom menu order.
add_filter( 'custom_menu_order', '__return_true' );
// Fix menu order for wizards with parent menu items.
add_filter( 'menu_order', [ __CLASS__, 'menu_order' ], 11 );
}

/**
Expand Down Expand Up @@ -134,5 +137,38 @@ public static function is_completed( $wizard_slug ) {

return false;
}

/**
* Update menu order for wizards with parent menu items.
*
* @param array $menu_order The current menu order.
*
* @return array The updated menu order.
*/
public static function menu_order( $menu_order ) {
$index = array_search( 'newspack-dashboard', $menu_order, true );
if ( false === $index ) {
return $menu_order;
}
$ordered_wizards = [];
foreach ( self::$wizards as $slug => $wizard ) {
if ( ! empty( $wizard->parent_menu ) && ! empty( $wizard->menu_order ) ) {
$ordered_wizards[ $wizard->menu_order ] = $wizard->parent_menu;
}
}
if ( empty( $ordered_wizards ) ) {
return $menu_order;
}
ksort( $ordered_wizards );
foreach ( array_reverse( $ordered_wizards ) as $menu_item ) {
$key = array_search( $menu_item, $menu_order, true );
if ( false === $key ) {
continue;
}
array_splice( $menu_order, $key, 1 );
array_splice( $menu_order, $index + 1, 0, $menu_item );
}
return $menu_order;
}
}
Wizards::init();
17 changes: 15 additions & 2 deletions includes/wizards/advertising/class-advertising-display-ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ class Advertising_Display_Ads extends Wizard {
),
);

/**
* The parent menu item name.
*
* @var string
*/
public $parent_menu = 'advertising-display-ads';

/**
* Order relative to the Newspack Dashboard menu item.
*
* @var int
*/
public $menu_order = 4;

/**
* Constructor.
*/
Expand Down Expand Up @@ -546,8 +560,7 @@ public function add_page() {
$this->capability,
$this->slug,
array( $this, 'render_wizard' ),
$icon,
3.5
$icon
);
add_submenu_page(
$this->slug,
Expand Down
27 changes: 10 additions & 17 deletions includes/wizards/advertising/class-advertising-sponsors.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ class Advertising_Sponsors extends Wizard {

/**
* Newspack Sponsors CPT name.
*
*
* @var string
*/
const CPT_NAME = 'newspack_spnsrs_cpt';

/**
* Sponsors CPT list path.
*
*
* @var string
*/
const URL = 'edit.php?post_type=newspack_spnsrs_cpt';

/**
/**
* Advertising Page path.
*
*
* @var string
*/
const PARENT_URL = 'admin.php?page=advertising-display-ads';
Expand All @@ -47,13 +47,6 @@ class Advertising_Sponsors extends Wizard {
*/
protected $capability = 'manage_options';

/**
* High menu priority since we need core registrations to exist before we can modify them.
*
* @var int
*/
protected $menu_priority = 99;

/**
* Advertising_Sponsors Constructor.
*/
Expand Down Expand Up @@ -83,8 +76,8 @@ public function __construct() {
'textContent' => esc_html__( 'Settings', 'newspack-plugin' ),
'href' => admin_url( static::URL . '&page=newspack-sponsors-settings-admin' ),
],
],
'title' => $this->get_name(),
],
'title' => $this->get_name(),
]
);
}
Expand Down Expand Up @@ -174,19 +167,19 @@ public function update_sponsors_cpt_args( $args, $post_type ) {
* Parent file filter. Used to determine active menu items.
*
* @param string $parent_file Parent file to be overridden.
* @return string
* @return string
*/
public function parent_file( $parent_file ) {
global $pagenow, $typenow;

if ( in_array( $pagenow, [ 'post.php', 'post-new.php' ] ) && $typenow === static::CPT_NAME ) {
return 'advertising-display-ads';
}

if ( isset( $_GET['page'] ) && $_GET['page'] === 'newspack-sponsors-settings-admin' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return static::PARENT_URL;
}

return $parent_file;
}

Expand All @@ -200,7 +193,7 @@ public function submenu_file( $submenu_file ) {
if ( isset( $_GET['page'] ) && $_GET['page'] === 'newspack-sponsors-settings-admin' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return static::URL;
}

return $submenu_file;
}
}
7 changes: 0 additions & 7 deletions includes/wizards/class-components-demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class Components_Demo extends Wizard {
*/
protected $capability = 'manage_options';

/**
* Priority setting for ordering admin submenu items.
*
* @var int.
*/
protected $menu_priority = 100;

/**
* Whether the wizard should be displayed in the Newspack submenu.
*
Expand Down
44 changes: 22 additions & 22 deletions includes/wizards/class-network-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class Network_Wizard extends Wizard {
*/
private $screen_type = '';

/**
* The parent menu item name.
*
* @var string
*/
public $parent_menu = 'newspack-network';

/**
* Order relative to the Newspack Dashboard menu item.
*
* @var int
*/
public $menu_order = 5;

/**
* Constructor.
*/
Expand All @@ -38,7 +52,7 @@ public function __construct() {
if ( ! is_plugin_active( 'newspack-network/newspack-network.php' ) ) {
return;
}

// Admin screens based on Newspack Network plugin's admin pages and post types.
$this->admin_screens = [
'page' => [
Expand All @@ -55,8 +69,8 @@ public function __construct() {
],
];

// Move entire Network Menu. Use a high priority to load after Network Plugin itself loads.
add_action( 'admin_menu', [ $this, 'move_menu' ], 99 );
// Update menu information.
add_action( 'admin_menu', [ $this, 'modify_menu' ], 11 );

// Use current_screen for better detection of which admin screen we might be on.
add_action( 'current_screen', [ $this, 'current_screen' ] );
Expand All @@ -77,7 +91,7 @@ public function current_screen() {
}

// Check for admin post type screen: Listings page and classic editor (add new + edit), but not block editor.
if ( ! empty( $current_screen->post_type )
if ( ! empty( $current_screen->post_type )
&& ! empty( $this->admin_screens['post_type'][ $current_screen->post_type ] )
&& false === $current_screen->is_block_editor ) {
$this->slug = $current_screen->post_type;
Expand Down Expand Up @@ -120,10 +134,9 @@ public function is_wizard_page() {
*
* @return void
*/
public function move_menu() {

public function modify_menu() {
global $menu;

// Find the Newspack Network menu item in the admin menu.
$network_key = null;
foreach ( $menu as $k => $v ) {
Expand All @@ -133,29 +146,16 @@ public function move_menu() {
break;
}
}

// Verify a key was found.
if ( empty( $network_key ) ) {
return;
}

// Adjust the network menu attributes.
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$menu[ $network_key ][0] = __( 'Network', 'newspack-plugin' );
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$menu[ $network_key ][6] = 'data:image/svg+xml;base64,' . base64_encode( '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path fill="none" stroke="none" d="M12 3.3c-4.8 0-8.8 3.9-8.8 8.8 0 4.8 3.9 8.8 8.8 8.8 4.8 0 8.8-3.9 8.8-8.8s-4-8.8-8.8-8.8zm6.5 5.5h-2.6C15.4 7.3 14.8 6 14 5c2 .6 3.6 2 4.5 3.8zm.7 3.2c0 .6-.1 1.2-.2 1.8h-2.9c.1-.6.1-1.2.1-1.8s-.1-1.2-.1-1.8H19c.2.6.2 1.2.2 1.8zM12 18.7c-1-.7-1.8-1.9-2.3-3.5h4.6c-.5 1.6-1.3 2.9-2.3 3.5zm-2.6-4.9c-.1-.6-.1-1.1-.1-1.8 0-.6.1-1.2.1-1.8h5.2c.1.6.1 1.1.1 1.8s-.1 1.2-.1 1.8H9.4zM4.8 12c0-.6.1-1.2.2-1.8h2.9c-.1.6-.1 1.2-.1 1.8 0 .6.1 1.2.1 1.8H5c-.2-.6-.2-1.2-.2-1.8zM12 5.3c1 .7 1.8 1.9 2.3 3.5H9.7c.5-1.6 1.3-2.9 2.3-3.5zM10 5c-.8 1-1.4 2.3-1.8 3.8H5.5C6.4 7 8 5.6 10 5zM5.5 15.3h2.6c.4 1.5 1 2.8 1.8 3.7-1.8-.6-3.5-2-4.4-3.7zM14 19c.8-1 1.4-2.2 1.8-3.7h2.6C17.6 17 16 18.4 14 19z"></path></svg>' );

// Try to move the network item to a higher position near "Newspack".
$new_position = '3.9';

// if position/key collision, keep increasing increment.
while ( array_key_exists( $new_position, $menu ) ) {
$new_position .= '9';
}

// Move network menu in the array.
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$menu[ $new_position ] = $menu[ $network_key ];
unset( $menu[ $network_key ] );
}
}
57 changes: 10 additions & 47 deletions includes/wizards/class-newsletters-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ class Newsletters_Wizard extends Wizard {
private $admin_screens = [];

/**
* Must be run after Newsletters Plugin.
* The parent menu item name.
*
* @var int.
* @var string
*/
protected $menu_priority = 11;
public $parent_menu = 'edit.php?post_type=newspack_nl_cpt';

/**
* Order relative to the Newspack Dashboard menu item.
*
* @var int
*/
public $menu_order = 2;

/**
* Constructor.
Expand Down Expand Up @@ -94,10 +101,6 @@ public function __construct() {
* Adjusts the Newsletters menu. Called from parent constructor 'admin_menu'.
*/
public function add_page() {

// Move the entire Newsletters CPT menu.
$this->move_cpt_menu();

// Remove "Add New" menu item.
remove_submenu_page( 'edit.php?post_type=' . Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT, 'post-new.php?post_type=' . Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT );

Expand Down Expand Up @@ -270,46 +273,6 @@ public function is_wizard_page() {
return isset( $this->admin_screens[ $this->get_screen_slug() ] );
}

/**
* Move CPT Menu using a decimal value. (CPT objects only allow integer positions).
*
* @return void
*/
private function move_cpt_menu() {

// @todo: Is there a better way to set a CPT Menu position to a decimal value????

global $menu;

// Look for the Newsletters parent menu in the admin menu.
$current_position = null;
foreach ( $menu as $position => $item ) {
// Test each item until found.
if ( $item[2] === 'edit.php?post_type=' . Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT ) {
$current_position = $position;
break;
}
}

// Verify a key was found.
if ( empty( $current_position ) ) {
return;
}

// Move the item to a higher position near "Newspack".
$new_position = '3.3';

// if position/key collision, keep increasing increment... 3.3 => 3.33 => 3.333 ...
while ( array_key_exists( $new_position, $menu ) ) {
$new_position .= '3';
}

// Move menu in the array.
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$menu[ $new_position ] = $menu[ $current_position ];
unset( $menu[ $current_position ] );
}

/**
* Callback when Newsletters CPT is registered.
*
Expand Down
Loading

0 comments on commit 406bc54

Please sign in to comment.