2424 * diversa da $label
2525 * - $capability => per specificare quale capability deve
2626 * possedere l'utente per visualizzare la voce del menu
27+ * - $icon => icona del menu, di default l'ingranaggio; i valori
28+ * accettati sono gli stessi di $icon_url
2729 *
2830 * Ricordati di caricare la classe nel bootstrap del tuo plugin.
2931 */
@@ -77,10 +79,27 @@ class MenuPage
7779 protected $ capability = 'manage_options ' ;
7880
7981 /**
80- * Priorità della voce di menu nel filtro WordPress admin_menu
82+ * Priorità della voce di menu nel filtro WordPress admin_menu.
83+ *
84+ * IMPORTANTE: Per i sottomenu, va impostato un valore superiore a
85+ * quello del menu parent, altrimenti il sottomenu darà un 404.
8186 */
8287 protected $ filterPriority = 10 ;
8388
89+ /**
90+ * Icona da usare per la voce di menu; va usato solo per i menu parent,
91+ * di default è l'icona dell'ingranaggio.
92+ *
93+ * Puoi passare una URL oppure uno di questi tre:
94+ * - Pass a base64-encoded SVG using a data URI, which will be colored to
95+ * match the color scheme. This should begin with 'data:image/svg+xml;base64,'.
96+ * - Pass the name of a Dashicons helper class to use a font icon,
97+ * e.g. 'dashicons-chart-pie'.
98+ * - Pass 'none' to leave div.wp-menu-image empty so an icon can be
99+ * added via CSS.
100+ */
101+ protected $ icon = '' ;
102+
84103 /**
85104 * Specifica qui i plugin che devono essere presenti affinché
86105 * la voce di menu venga mostrata; usa lo stesso formato
@@ -106,6 +125,7 @@ public function __construct( array $attributes = [] )
106125 $ this ->parentSlug = $ attributes ['parentSlug ' ] ?? $ this ->parentSlug ;
107126 $ this ->menuLabel = $ attributes ['menuLabel ' ] ?? $ this ->menuLabel ;
108127 $ this ->capability = $ attributes ['capability ' ] ?? $ this ->capability ;
128+ $ this ->icon = $ attributes ['icon ' ] ?? $ this ->icon ;
109129 $ this ->filterPriority = $ attributes ['filterPriority ' ] ?? $ this ->filterPriority ;
110130 $ this ->requiredPlugins = $ attributes ['requiredPlugins ' ] ?? $ this ->requiredPlugins ;
111131
@@ -140,13 +160,13 @@ public function admin_menu()
140160 // Caso in cui l'etichetta del menu madre è diversa da quella
141161 // della sottovoce di menu (https://wordpress.stackexchange.com/a/66499/86662)
142162 if ( ! empty ( $ this ->menuLabel ) ) {
143- add_menu_page ( $ this ->menuLabel , $ this ->menuLabel , $ this ->capability , $ this ->slug , '__return_true ' , '' , $ this ->position );
163+ add_menu_page ( $ this ->menuLabel , $ this ->menuLabel , $ this ->capability , $ this ->slug , '__return_true ' , $ this -> icon , $ this ->position );
144164 add_submenu_page ( $ this ->slug , $ this ->label , $ this ->label , $ this ->capability , $ this ->slug , [ $ this , 'view ' ] );
145165 }
146166 // Caso in cui non ci interessa differenziare, ad es. perché non ci sono
147167 // altre pagine di menu nel menu madre
148168 else {
149- add_menu_page ( $ this ->label , $ this ->label , $ this ->capability , $ this ->slug , [ $ this , 'view ' ], '' , $ this ->position );
169+ add_menu_page ( $ this ->label , $ this ->label , $ this ->capability , $ this ->slug , [ $ this , 'view ' ], $ this -> icon , $ this ->position );
150170 }
151171 }
152172 }
0 commit comments