These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | private $pages = array(); |
||
13 | |||
14 | private $child_pages = array(); |
||
15 | |||
16 | /** |
||
17 | * Returns the *Singleton* instance of this class. |
||
18 | * |
||
19 | * @return Singleton The *Singleton* instance. |
||
20 | */ |
||
21 | public static function get_instance() |
||
22 | { |
||
23 | if( null === static::$instance ) |
||
24 | { |
||
25 | static::$instance = new static(); |
||
26 | } |
||
27 | return static::$instance; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Add a page to the admin menu. |
||
32 | * |
||
33 | * @param array $args |
||
34 | * @throws \RuntimeException |
||
35 | */ |
||
36 | public function add_page($args) |
||
37 | { |
||
38 | $slug = $args['slug']; |
||
39 | if(array_key_exists($slug,$this->pages)) |
||
40 | { |
||
41 | throw new \RuntimeException("A page with slug '$slug' has already been registered"); |
||
42 | } |
||
43 | $this->pages[$slug] = new Page($args); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Get a page from the set of registered pages. |
||
48 | * |
||
49 | * @param string $slug |
||
50 | * @return Page |
||
51 | * @throws \RuntimeException If no page was found for the given slug |
||
52 | */ |
||
53 | public function get_page($slug) |
||
54 | { |
||
55 | if(!array_key_exists($slug,$this->pages)) |
||
56 | { |
||
57 | throw new \RuntimeException("The page '$slug' does not exist"); |
||
58 | } |
||
59 | return $this->pages[$slug]; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Add a child setting page. |
||
64 | * |
||
65 | * @param array $args |
||
66 | */ |
||
67 | public function add_child_page($args) |
||
68 | { |
||
69 | $parent_slug = $args['parent_slug']; |
||
70 | if(!array_key_exists($parent_slug, $this->child_pages)) |
||
71 | { |
||
72 | $this->child_pages[$parent_slug] = array(); |
||
73 | } |
||
74 | $this->child_pages[$parent_slug][$args['slug']] = new ChildPage($args); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get a child page from the set of registered child pages. |
||
79 | * |
||
80 | * @param string $slug |
||
81 | * @param string $parent_slug |
||
82 | * @return ChildPage |
||
83 | * @throws \RuntimeException If no child page was found for the given slug/parent_slug |
||
84 | */ |
||
85 | public function get_child_page($slug, $parent_slug) |
||
86 | { |
||
87 | if(!array_key_exists($parent_slug, $this->child_pages) || |
||
88 | !array_key_exists($slug, $this->child_pages[$parent_slug])) |
||
89 | { |
||
90 | throw new \RuntimeException("The child page '$slug' does not exist for the parent '$parent_slug'"); |
||
91 | } |
||
92 | return $this->child_pages[$parent_slug][$slug]; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get the value of the given field. |
||
97 | * |
||
98 | * @param [string] $field_name |
||
0 ignored issues
–
show
|
|||
99 | * @return mixed |
||
100 | */ |
||
101 | public function get_field_value($field_name) |
||
102 | { |
||
103 | foreach($this->child_pages as $parent_slug => $child_pages) |
||
104 | { |
||
105 | try |
||
106 | { |
||
107 | return $this->get_field_value_for_parent($parent_slug, $field_name); |
||
108 | } |
||
109 | catch(\RuntimeException $e) |
||
110 | { |
||
111 | continue; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | \trigger_error("Can't find a component with the name <b>$field_name</b>"); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get the value of a field within a parent page. |
||
120 | * |
||
121 | * @param [string] $parent_slug |
||
0 ignored issues
–
show
The doc-type
[string] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. ![]() |
|||
122 | * @param [string] $field_name |
||
123 | * @throws RuntimeException if no field was found with the given name |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function get_field_value_for_parent($parent_slug, $field_name) |
||
127 | { |
||
128 | foreach($this->child_pages[$parent_slug] as $cp) |
||
129 | { |
||
130 | try |
||
131 | { |
||
132 | $component = $cp->get_component($field_name); |
||
133 | $value = \get_option($field_name, $component->default); |
||
134 | return $value; |
||
135 | } |
||
136 | catch(\RuntimeException $e) |
||
137 | { |
||
138 | continue; |
||
139 | } |
||
140 | } |
||
141 | throw new \RuntimeException("Can't find a component with the name <b>$field_name</b>"); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Register styles & scripts to be enqueued by settings child pages |
||
146 | */ |
||
147 | public function register_scripts() |
||
148 | { |
||
149 | \wp_register_style('amarkal-settings', \Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-settings.min.css')); |
||
150 | \wp_register_script('amarkal-settings',\Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-settings.min.js'),array('amarkal-ui')); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Private constructor to prevent instantiation |
||
155 | */ |
||
156 | private function __construct() |
||
157 | { |
||
158 | $this->init(); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Register scripts and initiate the request handler. |
||
163 | */ |
||
164 | private function init() |
||
165 | { |
||
166 | \add_action('admin_init',array($this,'register_scripts')); |
||
167 | |||
168 | $rh = RequestHandler::get_instance(); |
||
169 | $rh->init(); |
||
170 | } |
||
171 | } |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.