| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | |
| 4 | 4 | use Elementor\Core\Base\Base_Object; |
| 5 | 5 | |
| 6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | - exit; // Exit if accessed directly. | |
| 7 | + exit; // Exit if accessed directly | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | abstract class Base extends Base_Object { |
| 11 | 11 | |
| @@ -10,8 +10,10 @@ | ||
| 10 | 10 | abstract class Base extends Base_Object { |
| 11 | 11 | |
| 12 | 12 | private $args; |
| 13 | 13 | |
| 14 | + private $options; | |
| 15 | + | |
| 14 | 16 | private $submenus = []; |
| 15 | 17 | |
| 16 | 18 | abstract protected function get_init_args(); |
| 17 | 19 | |
| @@ -17,11 +19,25 @@ | ||
| 17 | 19 | |
| 18 | 20 | public function __construct() { |
| 19 | 21 | $this->init_args(); |
| 20 | 22 | |
| 23 | + $this->init_options(); | |
| 24 | + | |
| 21 | 25 | add_action( 'admin_menu', function() { |
| 22 | 26 | $this->register(); |
| 23 | 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 | + } | |
| 24 | 40 | } |
| 25 | 41 | |
| 26 | 42 | public function get_args( $arg = null ) { |
| 27 | 43 | return self::get_items( $this->args, $arg ); |
| @@ -37,8 +53,12 @@ | ||
| 37 | 53 | |
| 38 | 54 | $this->submenus[] = array_merge( $default_submenu_args, $submenu_args ); |
| 39 | 55 | } |
| 40 | 56 | |
| 57 | + protected function get_init_options() { | |
| 58 | + return []; | |
| 59 | + } | |
| 60 | + | |
| 41 | 61 | protected function register_default_submenus() {} |
| 42 | 62 | |
| 43 | 63 | protected function register() { |
| 44 | 64 | $args = $this->args; |
| @@ -84,6 +104,49 @@ | ||
| 84 | 104 | 'position' => null, |
| 85 | 105 | ]; |
| 86 | 106 | |
| 87 | 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; | |
| 88 | 151 | } |
| 89 | 152 | } |