Skip to content

Commit 919715b

Browse files
committed
Fix bug capability
1 parent fce5cf2 commit 919715b

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

src/class-settings-page.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
* verrà creato un menu madre ad hoc con etichetta $label
2828
* - $menuLabel => per specificare un'etichetta del menu
2929
* diversa da $label
30-
* - $capabilities => per specificare quali capabilities deve
30+
* - $capability => per specificare quale capability deve
3131
* possedere l'utente per visualizzare la voce del menu
32+
* e modificarne i settings (default = manage_options)
3233
* - view() => funzione che stampa l'HTML; lascia vuoto se
3334
* verrà usato un semplice layout dove ogni sezione è un tab.
3435
*
@@ -69,10 +70,12 @@ abstract class SettingsPage {
6970
* Ignorata se viene specificato un valore per $parentSlug.
7071
*/
7172
protected $menuLabel = '';
73+
7274
/**
7375
* Permessi per visualizzare la voce del menu
7476
*/
75-
protected $capabilities = 'manage_options';
77+
protected $capability = 'manage_options';
78+
7679
/**
7780
* Priorità della voce di menu nel filtro WordPress admin_menu
7881
*/
@@ -85,10 +88,45 @@ abstract class SettingsPage {
8588
*/
8689
private $api;
8790

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+
*/
88106
public function __construct() {
89107
$this->api = new SettingsApi;
90108
add_action( 'admin_menu', array( $this, 'admin_menu' ), $this->filterPriority );
91109
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+
}
92130
}
93131

94132
/**
@@ -106,20 +144,20 @@ public function admin_init() {
106144
public function admin_menu() {
107145
// Se la pagina è una sottovoce di un menu madre esistente...
108146
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 );
110148
}
111149
// Se il menu madre va creato...
112150
else {
113151
// Caso in cui l'etichetta del menu madre è diversa da quella
114152
// della sottovoce di menu (https://wordpress.stackexchange.com/a/66499/86662)
115153
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' ] );
118156
}
119157
// Caso in cui non ci interessa differenziare, ad es. perché non ci sono
120158
// altre pagine di menu nel menu madre
121159
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 );
123161
}
124162
}
125163
}

0 commit comments

Comments
 (0)