| 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 $submenus = []; |
| 15 |
|
| 16 |
abstract protected function get_init_args(); |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->init_args(); |
| 20 |
|
| 21 |
add_action( 'admin_menu', function() { |
| 22 |
$this->register(); |
| 23 |
} ); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_args( $arg = null ) { |
| 27 |
return self::get_items( $this->args, $arg ); |
| 28 |
} |
| 29 |
|
| 30 |
public function add_submenu( $submenu_args ) { |
| 31 |
$default_submenu_args = [ |
| 32 |
'page_title' => '', |
| 33 |
'capability' => $this->args['capability'], |
| 34 |
'function' => null, |
| 35 |
'index' => null, |
| 36 |
]; |
| 37 |
|
| 38 |
$this->submenus[] = array_merge( $default_submenu_args, $submenu_args ); |
| 39 |
} |
| 40 |
|
| 41 |
protected function register_default_submenus() {} |
| 42 |
|
| 43 |
protected function register() { |
| 44 |
$args = $this->args; |
| 45 |
|
| 46 |
add_menu_page( $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'], $args['icon_url'], $args['position'] ); |
| 47 |
|
| 48 |
$this->register_default_submenus(); |
| 49 |
|
| 50 |
do_action( 'elementor/admin/menu_registered/' . $args['menu_slug'], $this ); |
| 51 |
|
| 52 |
usort( $this->submenus, function( $a, $b ) { |
| 53 |
return $a['index'] - $b['index']; |
| 54 |
} ); |
| 55 |
|
| 56 |
foreach ( $this->submenus as $index => $submenu_item ) { |
| 57 |
$submenu_args = [ |
| 58 |
$args['menu_slug'], |
| 59 |
$submenu_item['page_title'], |
| 60 |
$submenu_item['menu_title'], |
| 61 |
$submenu_item['capability'], |
| 62 |
$submenu_item['menu_slug'], |
| 63 |
$submenu_item['function'], |
| 64 |
]; |
| 65 |
|
| 66 |
if ( 0 === $submenu_item['index'] ) { |
| 67 |
$submenu_args[] = 0; |
| 68 |
} |
| 69 |
|
| 70 |
add_submenu_page( ...$submenu_args ); |
| 71 |
|
| 72 |
if ( ! empty( $submenu_item['class'] ) ) { |
| 73 |
global $submenu; |
| 74 |
|
| 75 |
$submenu[ $args['menu_slug'] ][ $index + 1 ][4] = $submenu_item['class']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
private function init_args() { |
| 81 |
$default_args = [ |
| 82 |
'function' => null, |
| 83 |
'icon_url' => null, |
| 84 |
'position' => null, |
| 85 |
]; |
| 86 |
|
| 87 |
$this->args = array_merge( $default_args, $this->get_init_args() ); |
| 88 |
} |
| 89 |
} |
| 90 |
|