27
27
* verrà creato un menu madre ad hoc con etichetta $label
28
28
* - $menuLabel => per specificare un'etichetta del menu
29
29
* diversa da $label
30
- * - $capabilities => per specificare quali capabilities deve
30
+ * - $capability => per specificare quale capability deve
31
31
* possedere l'utente per visualizzare la voce del menu
32
+ * e modificarne i settings (default = manage_options)
32
33
* - view() => funzione che stampa l'HTML; lascia vuoto se
33
34
* verrà usato un semplice layout dove ogni sezione è un tab.
34
35
*
@@ -69,10 +70,12 @@ abstract class SettingsPage {
69
70
* Ignorata se viene specificato un valore per $parentSlug.
70
71
*/
71
72
protected $ menuLabel = '' ;
73
+
72
74
/**
73
75
* Permessi per visualizzare la voce del menu
74
76
*/
75
- protected $ capabilities = 'manage_options ' ;
77
+ protected $ capability = 'manage_options ' ;
78
+
76
79
/**
77
80
* Priorità della voce di menu nel filtro WordPress admin_menu
78
81
*/
@@ -85,10 +88,45 @@ abstract class SettingsPage {
85
88
*/
86
89
private $ api ;
87
90
91
+ /**
92
+ * Metodo da sovrascrivere che ritorna l'array delle sezioni
93
+ * da passare a class-settings-api.php
94
+ */
95
+ abstract protected function getSections ();
96
+
97
+ /**
98
+ * Metodo da sovrascrivere che ritorna l'array dei setting
99
+ * fields da passare a class-settings-api.php
100
+ */
101
+ abstract protected function getFields ();
102
+
103
+ /**
104
+ * Registra gli hooks
105
+ */
88
106
public function __construct () {
89
107
$ this ->api = new SettingsApi ;
90
108
add_action ( 'admin_menu ' , array ( $ this , 'admin_menu ' ), $ this ->filterPriority );
91
109
add_action ( 'admin_init ' , array ( $ this , 'admin_init ' ) );
110
+ if ( $ this ->capability !== 'manage_options ' ) {
111
+ $ this ->set_write_capabilities ();
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Permetti agli utenti con la capability richiesta
117
+ * di modificare i settings
118
+ */
119
+ private function set_write_capabilities () {
120
+ $ sections = $ this ->getSections ();
121
+ $ sections_ids = array_column ( $ sections , 'id ' );
122
+ foreach ( $ sections_ids as $ option_page ) {
123
+ add_filter (
124
+ 'option_page_capability_ ' . $ option_page ,
125
+ function ( $ cap ) {
126
+ return $ this ->capability ;
127
+ }
128
+ );
129
+ }
92
130
}
93
131
94
132
/**
@@ -106,20 +144,20 @@ public function admin_init() {
106
144
public function admin_menu () {
107
145
// Se la pagina è una sottovoce di un menu madre esistente...
108
146
if ( ! empty ( $ this ->parentSlug ) ) {
109
- add_submenu_page ( $ this ->parentSlug , $ this ->label , $ this ->label , $ this ->capabilities , $ this ->slug , [ $ this , 'view ' ], $ this ->position );
147
+ add_submenu_page ( $ this ->parentSlug , $ this ->label , $ this ->label , $ this ->capability , $ this ->slug , [ $ this , 'view ' ], $ this ->position );
110
148
}
111
149
// Se il menu madre va creato...
112
150
else {
113
151
// Caso in cui l'etichetta del menu madre è diversa da quella
114
152
// della sottovoce di menu (https://wordpress.stackexchange.com/a/66499/86662)
115
153
if ( ! empty ( $ this ->menuLabel ) ) {
116
- add_menu_page ( $ this ->menuLabel , $ this ->menuLabel , $ this ->capabilities , $ this ->slug , '__return_true ' , '' , $ this ->position );
117
- add_submenu_page ( $ this ->slug , $ this ->label , $ this ->label , $ this ->capabilities , $ this ->slug , [ $ this , 'view ' ] );
154
+ add_menu_page ( $ this ->menuLabel , $ this ->menuLabel , $ this ->capability , $ this ->slug , '__return_true ' , '' , $ this ->position );
155
+ add_submenu_page ( $ this ->slug , $ this ->label , $ this ->label , $ this ->capability , $ this ->slug , [ $ this , 'view ' ] );
118
156
}
119
157
// Caso in cui non ci interessa differenziare, ad es. perché non ci sono
120
158
// altre pagine di menu nel menu madre
121
159
else {
122
- add_menu_page ( $ this ->label , $ this ->label , $ this ->capabilities , $ this ->slug , [ $ this , 'view ' ], '' , $ this ->position );
160
+ add_menu_page ( $ this ->label , $ this ->label , $ this ->capability , $ this ->slug , [ $ this , 'view ' ], '' , $ this ->position );
123
161
}
124
162
}
125
163
}
0 commit comments