Skip to content

Commit 8c2804f

Browse files
committed
Classe wrapper SettingsPage
1 parent 15c5340 commit 8c2804f

File tree

6 files changed

+149
-210
lines changed

6 files changed

+149
-210
lines changed

plugin.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

readme.md

Lines changed: 5 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,8 @@
1-
What is this?
2-
---------------
1+
# Come funziona
2+
Estendi la classa `Idearia\SettingsPage` per creare nuovi menu o sottomenu in WordPress.
33

4-
It's a PHP class wrapper for handling WordPress [Settings API](http://codex.wordpress.org/Settings_API). Gives a very handy way to build theme or plugins option panel.
4+
# Per avere più controllo
55

6+
La classe `Idearia\SettingsPage` è un wrapper di `Idearia\SettingsApi` che ne semplifica l'utilizzo, che a sua volta è un wrapper della [Settings API di WordPress](https://codex.wordpress.org/Settings_API).
67

7-
## Package Installation (via Composer)
8-
9-
To install this package, edit your `composer.json` file:
10-
11-
```js
12-
{
13-
"require": {
14-
"tareq1988/wordpress-settings-api-class": "dev-master"
15-
}
16-
}
17-
```
18-
19-
Now run:
20-
21-
`$ composer install`
22-
23-
Usage Example
24-
---------------
25-
26-
Checkout the [examples](https://github.com/tareq1988/wordpress-settings-api-class/tree/master/example) folder for OOP and procedural example. They were called in [plugin.php](https://github.com/tareq1988/wordpress-settings-api-class/blob/master/plugin.php) file.
27-
28-
A detailed tutorial can be found [here](https://tareq.co/2012/06/wordpress-settings-api-php-class/).
29-
30-
#### Retrieving saved options
31-
32-
```php
33-
/**
34-
* Get the value of a settings field
35-
*
36-
* @param string $option settings field name
37-
* @param string $section the section name this field belongs to
38-
* @param string $default default text if it's not found
39-
*
40-
* @return mixed
41-
*/
42-
function prefix_get_option( $option, $section, $default = '' ) {
43-
44-
$options = get_option( $section );
45-
46-
if ( isset( $options[$option] ) ) {
47-
return $options[$option];
48-
}
49-
50-
return $default;
51-
}
52-
```
53-
54-
Screenshot
55-
----------------------
56-
57-
![Option Panel](https://github.com/tareq1988/wordpress-settings-api-class/raw/master/screenshot-1.png "The options panel build on the fly using the PHP Class")
58-
59-
60-
61-
Frequently Asked Questions
62-
---------------
63-
64-
#### What this plugin for?
65-
66-
It's mainly a plugin that demonstrates the Settings API PHP class
67-
68-
#### Whats the facility?
69-
70-
A plugin or theme developer can build their options panel with Settings API easily
71-
72-
#### What is Settings API ?
73-
74-
Settings API is a functionality from WordPress that helps developers to save their options data very easily and securely.
75-
More about [Settings API](http://codex.wordpress.org/Settings_API).
76-
77-
78-
Changelog:
79-
----------------------
80-
```
81-
v1.3 (27 September, 2016)
82-
------------------------
83-
- [new] Placeholder support for text and textarea input
84-
- [new] min, max and step support for number field
85-
- [fix] Empty multicheck saving warning
86-
- [improved] Don't show the navigation if only one section exists
87-
88-
v1.1 (23 April, 2015)
89-
------------------------
90-
- [new] Folder structure updated
91-
- [new] composer support added
92-
- [new] Number field added
93-
- [new] URL field added
94-
- [improved] wysiwyg field responsive support. Allow to pass options to wp_editor
95-
- [new] WP Media uploader added
96-
97-
v1.0 (16 July, 2014)
98-
------------------------
99-
- [new] color, password and wysiwyg example added on plugin settings
100-
- [new] Color Picker added
101-
- [improved] Allow to set description for section
102-
- Some other old fixes ;)
103-
```
8+
Per avere maggiore controllo sulle opzioni, puoi usare direttamente la classe `Idearia\SettingsApi`.

readme.txt

Lines changed: 0 additions & 84 deletions
This file was deleted.

screenshot-1.png

-390 KB
Binary file not shown.

src/class.settings-api.php renamed to src/class-settings-api.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
<?php
22

3+
namespace Idearia;
4+
35
/**
4-
* weDevs Settings API wrapper class
5-
*
6-
* @version 1.3 (27-Sep-2016)
7-
*
8-
* @author Tareq Hasan <tareq@weDevs.com>
9-
* @link https://tareq.co Tareq Hasan
10-
* @example example/oop-example.php How to use the class
6+
* Settings API wrapper class
117
*/
12-
if ( !class_exists( 'WeDevs_Settings_API' ) ):
13-
class WeDevs_Settings_API {
8+
class SettingsApi {
149

1510
/**
1611
* settings sections array
@@ -667,6 +662,4 @@ function _style_fix() {
667662
endif;
668663
}
669664

670-
}
671-
672-
endif;
665+
}

src/class-settings-page.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
// phpcs:disable WordPress.Security.NonceVerification.Recommended
3+
4+
namespace Idearia;
5+
6+
defined( 'ABSPATH' ) || exit;
7+
8+
/**
9+
* Crea una pagina di opzioni di WordPress e mostrala come
10+
* voce di menu a se stante oppure come sottovoce di un menu
11+
* esistente.
12+
*
13+
* Per creare una pagina estendi questa classe e sovrascrivi
14+
* i seguenti CAMPI OBBLIGATORI:
15+
*
16+
* - $label => titolo della pagina e nome della voce di menu
17+
* - $slug => slug unico della voce di menu
18+
* - getSections() => funzione che ritorna le sezioni della pagina,
19+
* solitamente renderizzate come tabs
20+
* - getFields() => funzione che ritorna le opzioni vere e proprie,
21+
* raggruppate per sezione
22+
*
23+
* Per maggiore controllo sovrascrivi i seguenti CAMPI OPZIONALI:
24+
* - $position => posizione relativa della voce di menu
25+
* - $parentSlug => se vuoi che la pagina sia una sottovoce di un
26+
* menu esistente, specifica qui lo slug del menu; altrimenti
27+
* verrà creato un menu madre ad hoc con etichetta $label
28+
* - $menuLabel => per specificare un'etichetta del menu
29+
* diversa da $label
30+
* - view() => funzione che stampa l'HTML; lascia vuoto se
31+
* verrà usato un semplice layout dove ogni sezione è un tab.
32+
*
33+
* Ricordati di caricare la classe nel bootstrap del tuo plugin.
34+
*/
35+
abstract class SettingsPage {
36+
37+
/**
38+
* Nome della pagina, apparirà sia come titolo della pagina che
39+
* come etichetta della voce di menu
40+
*/
41+
protected $label; // obbligatorio
42+
43+
/**
44+
* Slug della pagina
45+
*/
46+
protected $slug; // obbligatorio
47+
48+
/**
49+
* Posizione della voce di menu nella sidebar
50+
*/
51+
protected $position = null;
52+
53+
/**
54+
* Menu madre a cui far appartenere questa pagina (ad es.
55+
* options-general.php).
56+
*
57+
* Se lasci vuoto, verrà creato appositamente un nuovo menu
58+
* madre con slug $parentSlug e con etichetta $label.
59+
*/
60+
protected $parentSlug = '';
61+
62+
/**
63+
* Etichetta del menu madre. Lascia vuoto se vuoi che coincida
64+
* con $label, oppure se il menu non ha altre pagine oltre
65+
* questa.
66+
*
67+
* Ignorata se viene specificato un valore per $parentSlug.
68+
*/
69+
protected $menuLabel = '';
70+
/**
71+
* Priorità della voce di menu nel filtro WordPress admin_menu
72+
*/
73+
protected $filterPriority = 10;
74+
75+
/**
76+
* Istanza della classe che si interfaccia con le
77+
* Settings API di WordPress; verrà impostato nel
78+
* constructor automaticamente
79+
*/
80+
private $api;
81+
82+
public function __construct() {
83+
$this->api = new SettingsApi;
84+
add_action( 'admin_menu', array( $this, 'admin_menu' ), $this->filterPriority );
85+
add_action( 'admin_init', array( $this, 'admin_init' ) );
86+
}
87+
88+
/**
89+
* Callback che registra tutti i nostri settings usando l'API
90+
*/
91+
public function admin_init() {
92+
$this->api->set_sections( $this->getSections() );
93+
$this->api->set_fields( $this->getFields() );
94+
$this->api->admin_init();
95+
}
96+
97+
/**
98+
* Aggiungi alla sidebar la voce di menu
99+
*/
100+
public function admin_menu() {
101+
// Se la pagina è una sottovoce di un menu madre esistente...
102+
if ( ! empty( $this->parentSlug ) ) {
103+
add_submenu_page( $this->parentSlug, $this->label, $this->label, 'manage_options', $this->slug, [ $this, 'view' ], $this->position );
104+
}
105+
// Se il menu madre va creato...
106+
else {
107+
// Caso in cui l'etichetta del menu madre è diversa da quella
108+
// della sottovoce di menu (https://wordpress.stackexchange.com/a/66499/86662)
109+
if ( ! empty( $this->menuLabel ) ) {
110+
add_menu_page( $this->menuLabel, $this->menuLabel, 'manage_options', $this->slug, '__return_true', '', $this->position );
111+
add_submenu_page( $this->slug, $this->label, $this->label, 'manage_options', $this->slug, [ $this, 'view' ] );
112+
}
113+
// Caso in cui non ci interessa differenziare, ad es. perché non ci sono
114+
// altre pagine di menu nel menu madre
115+
else {
116+
add_menu_page( $this->label, $this->label, 'manage_options', $this->slug, [ $this, 'view' ], '', $this->position );
117+
}
118+
}
119+
}
120+
121+
/**
122+
* Funzione usata per renderizzare l'HTML; di default usiamo
123+
* il layout a tabs.
124+
*/
125+
public function view() {
126+
$this->view_template_tabs();
127+
}
128+
129+
/**
130+
* Template che mostra un tab per ogni sezione
131+
*/
132+
protected function view_template_tabs() {
133+
echo '<div class="wrap ' . 'admin-options-' . $this->slug . '">';
134+
$this->api->show_navigation();
135+
$this->api->show_forms();
136+
echo '</div>';
137+
}
138+
139+
}

0 commit comments

Comments
 (0)