27
27
*
28
28
* Ricordati di caricare la classe nel bootstrap del tuo plugin.
29
29
*/
30
- abstract class MenuPage
30
+ class MenuPage
31
31
{
32
32
/**
33
33
* Nome della pagina, apparirà sia come titolo della pagina che
@@ -50,7 +50,7 @@ abstract class MenuPage
50
50
* options-general.php).
51
51
*
52
52
* Se lasci vuoto, verrà creato appositamente un nuovo menu
53
- * madre con slug $parentSlug e con etichetta $label.
53
+ * madre con slug $slug e con etichetta $label.
54
54
*/
55
55
protected $ parentSlug = '' ;
56
56
@@ -59,7 +59,8 @@ abstract class MenuPage
59
59
* con $label, oppure se il menu non ha altre pagine oltre
60
60
* questa.
61
61
*
62
- * Ignorata se viene specificato un valore per $parentSlug.
62
+ * Ignorata se viene specificato un valore per $parentSlug,
63
+ * perché in quel caso la label è quella del parent.
63
64
*/
64
65
protected $ menuLabel = '' ;
65
66
@@ -76,20 +77,34 @@ abstract class MenuPage
76
77
/**
77
78
* Registra gli hooks
78
79
*/
79
- public function __construct () {
80
+ public function __construct ( array $ attributes = [] )
81
+ {
82
+ // Parse arguments
83
+ $ this ->label = $ attributes ['label ' ] ?? $ this ->label ;
84
+ $ this ->slug = $ attributes ['slug ' ] ?? $ this ->slug ;
85
+ $ this ->position = $ attributes ['position ' ] ?? $ this ->position ;
86
+ $ this ->parentSlug = $ attributes ['parentSlug ' ] ?? $ this ->parentSlug ;
87
+ $ this ->menuLabel = $ attributes ['menuLabel ' ] ?? $ this ->menuLabel ;
88
+ $ this ->capability = $ attributes ['capability ' ] ?? $ this ->capability ;
89
+ $ this ->filterPriority = $ attributes ['filterPriority ' ] ?? $ this ->filterPriority ;
90
+
91
+ // Validate
80
92
if ( ! $ this ->label ) {
81
93
throw new \Exception ( 'Label non definita! ' );
82
94
}
83
95
if ( ! $ this ->slug ) {
84
96
throw new \Exception ( 'Slug non definita! ' );
85
97
}
98
+
99
+ // Crea pagina di menu
86
100
add_action ( 'admin_menu ' , array ( $ this , 'admin_menu ' ), $ this ->filterPriority );
87
101
}
88
102
89
103
/**
90
104
* Aggiungi alla sidebar la voce di menu
91
105
*/
92
- public function admin_menu () {
106
+ public function admin_menu ()
107
+ {
93
108
// Se la pagina è una sottovoce di un menu madre esistente...
94
109
if ( ! empty ( $ this ->parentSlug ) ) {
95
110
add_submenu_page ( $ this ->parentSlug , $ this ->label , $ this ->label , $ this ->capability , $ this ->slug , [ $ this , 'view ' ], $ this ->position );
@@ -114,5 +129,11 @@ public function admin_menu() {
114
129
* Funzione usata per renderizzare l'HTML; deve fare
115
130
* echo di qualcosa.
116
131
*/
117
- abstract public function view ();
132
+ public function view ()
133
+ {
134
+ echo '<h1>Hello world</h1> ' ;
135
+ echo '<p> ' ;
136
+ echo 'Questa pagina può essere vista solo da chi ha la capability ' . $ this ->capability ;
137
+ echo '</p> ' ;
138
+ }
118
139
}
0 commit comments