| 1 |
<?php |
| 2 |
namespace Elementor\Core\Admin\Menu; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Base_Object; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
abstract class Base extends Base_Object { |
| 11 |
|
| 12 |
private $args; |
| 13 |
|
| 14 |
private $options; |
| 15 |
|
| 16 |
private $submenus = []; |
| 17 |
|
| 18 |
abstract protected function get_init_args(); |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
$this->init_args(); |
| 22 |
|
| 23 |
$this->init_options(); |
| 24 |
|
| 25 |
add_action( 'admin_menu', function() { |
| 26 |
$this->register(); |
| 27 |
} ); |
| 28 |
|
| 29 |
if ( $this->options['separator'] ) { |
| 30 |
add_action( 'admin_menu', function() { |
| 31 |
$this->add_menu_separator(); |
| 32 |
} ); |
| 33 |
|
| 34 |
add_filter( 'custom_menu_order', '__return_true' ); |
| 35 |
|
| 36 |
add_filter( 'menu_order', function( $menu_order ) { |
| 37 |
return $this->rearrange_menu_separator( $menu_order ); |
| 38 |
} ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public function get_args( $arg = null ) { |
| 43 |
return self::get_items( $this->args, $arg ); |
| 44 |
} |
| 45 |
|
| 46 |
public function add_submenu( $submenu_args ) { |
| 47 |
$default_submenu_args = [ |
| 48 |
'page_title' => '', |
| 49 |
'capability' => $this->args['capability'], |
| 50 |
'function' => null, |
| 51 |
'index' => null, |
| 52 |
]; |
| 53 |
|
| 54 |
$this->submenus[] = array_merge( $default_submenu_args, $submenu_args ); |
| 55 |
} |
| 56 |
|
| 57 |
protected function get_init_options() { |
| 58 |
return []; |
| 59 |
} |
| 60 |
|
| 61 |
protected function register_default_submenus() {} |
| 62 |
|
| 63 |
protected function register() { |
| 64 |
$args = $this->args; |
| 65 |
|
| 66 |
add_menu_page( $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'], $args['icon_url'], $args['position'] ); |
| 67 |
|
| 68 |
$this->register_default_submenus(); |
| 69 |
|
| 70 |
do_action( 'elementor/admin/menu_registered/' . $args['menu_slug'], $this ); |
| 71 |
|
| 72 |
usort( $this->submenus, function( $a, $b ) { |
| 73 |
return $a['index'] - $b['index']; |
| 74 |
} ); |
| 75 |
|
| 76 |
foreach ( $this->submenus as $index => $submenu_item ) { |
| 77 |
$submenu_args = [ |
| 78 |
$args['menu_slug'], |
| 79 |
$submenu_item['page_title'], |
| 80 |
$submenu_item['menu_title'], |
| 81 |
$submenu_item['capability'], |
| 82 |
$submenu_item['menu_slug'], |
| 83 |
$submenu_item['function'], |
| 84 |
]; |
| 85 |
|
| 86 |
if ( 0 === $submenu_item['index'] ) { |
| 87 |
$submenu_args[] = 0; |
| 88 |
} |
| 89 |
|
| 90 |
add_submenu_page( ...$submenu_args ); |
| 91 |
|
| 92 |
if ( ! empty( $submenu_item['class'] ) ) { |
| 93 |
global $submenu; |
| 94 |
|
| 95 |
$submenu[ $args['menu_slug'] ][ $index + 1 ][4] = $submenu_item['class']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private function init_args() { |
| 101 |
$default_args = [ |
| 102 |
'function' => null, |
| 103 |
'icon_url' => null, |
| 104 |
'position' => null, |
| 105 |
]; |
| 106 |
|
| 107 |
$this->args = array_merge( $default_args, $this->get_init_args() ); |
| 108 |
} |
| 109 |
|
| 110 |
private function init_options() { |
| 111 |
$default_options = [ |
| 112 |
'separator' => false, |
| 113 |
]; |
| 114 |
|
| 115 |
$this->options = array_merge( $default_options, $this->get_init_options() ); |
| 116 |
} |
| 117 |
|
| 118 |
private function add_menu_separator() { |
| 119 |
global $menu; |
| 120 |
|
| 121 |
$slug = $this->args['menu_slug']; |
| 122 |
|
| 123 |
$menu[] = [ '', 'read', 'separator-' . $slug, '', 'wp-menu-separator ' . $slug ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 124 |
} |
| 125 |
|
| 126 |
private function rearrange_menu_separator( $menu_order ) { |
| 127 |
// Initialize our custom order array. |
| 128 |
$custom_menu_order = []; |
| 129 |
|
| 130 |
$slug = $this->args['menu_slug']; |
| 131 |
|
| 132 |
$separator_name = 'separator-' . $slug; |
| 133 |
|
| 134 |
// Get the index of our custom separator. |
| 135 |
$custom_separator = array_search( $separator_name, $menu_order, true ); |
| 136 |
|
| 137 |
// Loop through menu order and do some rearranging. |
| 138 |
foreach ( $menu_order as $item ) { |
| 139 |
if ( $slug === $item ) { |
| 140 |
$custom_menu_order[] = $separator_name; |
| 141 |
$custom_menu_order[] = $item; |
| 142 |
|
| 143 |
unset( $menu_order[ $custom_separator ] ); |
| 144 |
} elseif ( $separator_name !== $item ) { |
| 145 |
$custom_menu_order[] = $item; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Return order. |
| 150 |
return $custom_menu_order; |
| 151 |
} |
| 152 |
} |
| 153 |
|