1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\WP\Admin\Menus; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Menu Generator. |
7
|
|
|
*/ |
8
|
|
|
class SubMenu |
9
|
|
|
{ |
10
|
|
|
public $parent_slug; |
11
|
|
|
|
12
|
|
|
public $page_title; |
13
|
|
|
|
14
|
|
|
public $menu_title; |
15
|
|
|
|
16
|
|
|
public $capability; |
17
|
|
|
|
18
|
|
|
public $slug; |
19
|
|
|
|
20
|
|
|
public $callback; |
21
|
|
|
|
22
|
|
|
public $position; |
23
|
|
|
|
24
|
|
|
public $plugin_name; |
25
|
|
|
|
26
|
|
|
public function save() |
27
|
|
|
{ |
28
|
|
|
add_action('admin_menu', [$this, 'create_submenu']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public static function make($options = []) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
foreach ($options as $property => $value) { |
34
|
|
|
if (property_exists($this, $property)) { |
35
|
|
|
$this->{$property} = $value; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
add_action('admin_menu', [$this, 'create_submenu']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Register our menu page. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function create_submenu() |
47
|
|
|
{ |
48
|
|
|
if (current_user_can($this->capability)) { |
49
|
|
|
$hook = add_submenu_page( |
50
|
|
|
$this->parent_slug, |
51
|
|
|
$this->page_title, |
52
|
|
|
$this->menu_title, |
53
|
|
|
$this->capability, |
54
|
|
|
$this->slug, |
55
|
|
|
$this->callback, |
56
|
|
|
$this->icon |
|
|
|
|
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// if ( current_user_can( $this->capability ) ) { |
61
|
|
|
// $submenu[ $this->slug ][] = array( __( 'Clients', 'textdomain' ), $this->capability, 'admin.php?page=' . $this->slug . '#/clients' ); |
62
|
|
|
// $submenu[ $this->slug ][] = array( __( 'Settings', 'textdomain' ), $this->capability, 'admin.php?page=' . $this->slug . '#/settings' ); |
63
|
|
|
// } |
64
|
|
|
|
65
|
|
|
add_action('load-'.$hook, [$this, 'init_hooks']); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initialize our hooks for the admin page. |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function init_hooks() |
74
|
|
|
{ |
75
|
|
|
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Load scripts and styles for the app. |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function enqueue_scripts() |
84
|
|
|
{ |
85
|
|
|
wp_enqueue_style($this->plugin_name . '-vendors'); |
86
|
|
|
wp_enqueue_style($this->plugin_name . '-admin'); |
87
|
|
|
wp_enqueue_script($this->plugin_name . '-admin'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Render our admin page. |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function plugin_page() |
96
|
|
|
{ |
97
|
|
|
echo '<div class="wrap"><div id="'. $this->plugin_name .'-admin" csrf-token="'.csrf_token().'"></div></div>'; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.