Skip to content

Commit f5071e9

Browse files
author
Guido W. Pettinari
committed
Reso MenuPage instanziabile
1 parent 471a5fd commit f5071e9

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/class-menu-page.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* Ricordati di caricare la classe nel bootstrap del tuo plugin.
2929
*/
30-
abstract class MenuPage
30+
class MenuPage
3131
{
3232
/**
3333
* Nome della pagina, apparirà sia come titolo della pagina che
@@ -50,7 +50,7 @@ abstract class MenuPage
5050
* options-general.php).
5151
*
5252
* 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.
5454
*/
5555
protected $parentSlug = '';
5656

@@ -59,7 +59,8 @@ abstract class MenuPage
5959
* con $label, oppure se il menu non ha altre pagine oltre
6060
* questa.
6161
*
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.
6364
*/
6465
protected $menuLabel = '';
6566

@@ -76,20 +77,34 @@ abstract class MenuPage
7677
/**
7778
* Registra gli hooks
7879
*/
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
8092
if ( ! $this->label ) {
8193
throw new \Exception( 'Label non definita!' );
8294
}
8395
if ( ! $this->slug ) {
8496
throw new \Exception( 'Slug non definita!' );
8597
}
98+
99+
// Crea pagina di menu
86100
add_action( 'admin_menu', array( $this, 'admin_menu' ), $this->filterPriority );
87101
}
88102

89103
/**
90104
* Aggiungi alla sidebar la voce di menu
91105
*/
92-
public function admin_menu() {
106+
public function admin_menu()
107+
{
93108
// Se la pagina è una sottovoce di un menu madre esistente...
94109
if ( ! empty( $this->parentSlug ) ) {
95110
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() {
114129
* Funzione usata per renderizzare l'HTML; deve fare
115130
* echo di qualcosa.
116131
*/
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+
}
118139
}

0 commit comments

Comments
 (0)