1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\Settings; |
4
|
|
|
|
5
|
|
|
class Manager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Singleton The reference to *Singleton* instance of this class |
9
|
|
|
*/ |
10
|
|
|
private static $instance; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array The list of registered settings pages |
14
|
|
|
*/ |
15
|
|
|
private $settings_pages = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns the *Singleton* instance of this class. |
19
|
|
|
* |
20
|
|
|
* @return Singleton The *Singleton* instance. |
21
|
|
|
*/ |
22
|
|
|
public static function get_instance() |
23
|
|
|
{ |
24
|
|
|
if( null === static::$instance ) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
static::$instance = new static(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
return static::$instance; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Add a page to the admin menu. |
33
|
|
|
* |
34
|
|
|
* @param array $args |
35
|
|
|
* @throws \RuntimeException |
36
|
|
|
*/ |
37
|
|
|
public function add_settings_page( $args ) |
38
|
|
|
{ |
39
|
|
|
$slug = $args['slug']; |
40
|
|
|
if(array_key_exists($slug,$this->settings_pages)) |
41
|
|
|
{ |
42
|
|
|
throw new \RuntimeException("A settings page with slug '$slug' has already been registered"); |
43
|
|
|
} |
44
|
|
|
$page = new SettingsPage($args); |
45
|
|
|
$this->settings_pages[$slug] = $page; |
46
|
|
|
return $page; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get a settings page from the list of registered settings pages. |
51
|
|
|
* |
52
|
|
|
* @param string $slug |
53
|
|
|
* @param string $parent_slug |
|
|
|
|
54
|
|
|
* @return SettingsPage |
55
|
|
|
* @throws \RuntimeException If no settings page was found for the given slug/parent_slug |
56
|
|
|
*/ |
57
|
|
|
public function get_settings_page( $slug ) |
58
|
|
|
{ |
59
|
|
|
if(!array_key_exists($slug, $this->settings_pages)) |
60
|
|
|
{ |
61
|
|
|
throw new \RuntimeException("The settings page '$slug' does not exist"); |
62
|
|
|
} |
63
|
|
|
return $this->settings_pages[$slug]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the value of a field within a given settings page. |
68
|
|
|
* |
69
|
|
|
* @param [string] $slug |
|
|
|
|
70
|
|
|
* @param [string] $field_name |
|
|
|
|
71
|
|
|
* @throws RuntimeException if no field was found with the given name |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function get_field_value($slug, $field_name) |
75
|
|
|
{ |
76
|
|
|
$sp = $this->settings_pages[$slug]; |
77
|
|
|
|
78
|
|
|
$component = $sp->get_component($field_name); |
79
|
|
|
$value = \get_option($field_name, $component->default); |
80
|
|
|
return $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Register styles & scripts to be enqueued by settings pages |
85
|
|
|
*/ |
86
|
|
|
public function register_scripts() |
87
|
|
|
{ |
88
|
|
|
\wp_register_style('amarkal-settings', \Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-settings.min.css')); |
89
|
|
|
\wp_register_script('amarkal-settings',\Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-settings.min.js'),array('amarkal-ui')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Private constructor to prevent instantiation |
94
|
|
|
*/ |
95
|
|
|
private function __construct() |
96
|
|
|
{ |
97
|
|
|
$this->init(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Register scripts and initiate the request handler. |
102
|
|
|
*/ |
103
|
|
|
private function init() |
104
|
|
|
{ |
105
|
|
|
\add_action('admin_init',array($this,'register_scripts')); |
106
|
|
|
|
107
|
|
|
$rh = RequestHandler::get_instance(); |
108
|
|
|
$rh->init(); |
109
|
|
|
} |
110
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: