@@ -25,16 +25,16 @@ discard block |
||
25 | 25 | * The __set magic method is overridden here to apply value & name changes to |
26 | 26 | * child components. |
27 | 27 | */ |
28 | - public function __set( $name, $value ) |
|
28 | + public function __set($name, $value) |
|
29 | 29 | { |
30 | 30 | parent::__set($name, $value); |
31 | 31 | |
32 | - if( 'value' === $name ) |
|
32 | + if ('value' === $name) |
|
33 | 33 | { |
34 | 34 | $this->set_value($value); |
35 | 35 | } |
36 | 36 | |
37 | - if( 'name' === $name ) |
|
37 | + if ('name' === $name) |
|
38 | 38 | { |
39 | 39 | $this->set_name($value); |
40 | 40 | } |
@@ -46,9 +46,9 @@ discard block |
||
46 | 46 | * @param array $value |
47 | 47 | * @return void |
48 | 48 | */ |
49 | - public function set_value( array $value ) |
|
49 | + public function set_value(array $value) |
|
50 | 50 | { |
51 | - foreach($value as $n => $v) |
|
51 | + foreach ($value as $n => $v) |
|
52 | 52 | { |
53 | 53 | $component = $this->get_component($n); |
54 | 54 | $component->value = $v; |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | * @param string $name |
62 | 62 | * @return void |
63 | 63 | */ |
64 | - public function set_name( $name ) |
|
64 | + public function set_name($name) |
|
65 | 65 | { |
66 | - foreach($this->components as $c) |
|
66 | + foreach ($this->components as $c) |
|
67 | 67 | { |
68 | 68 | $c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template); |
69 | 69 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | */ |
92 | 92 | public function required_arguments() |
93 | 93 | { |
94 | - return array('name','components','template'); |
|
94 | + return array('name', 'components', 'template'); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | */ |
110 | 110 | public function parse_template() |
111 | 111 | { |
112 | - return preg_replace_callback('/\{\{([a-zA-Z\d-_]+)\}\}/', function($a){ |
|
112 | + return preg_replace_callback('/\{\{([a-zA-Z\d-_]+)\}\}/', function($a) { |
|
113 | 113 | $component = $this->get_component($a[1]); |
114 | 114 | return $component->render(); |
115 | 115 | }, $this->model['template']); |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | */ |
125 | 125 | public function get_name() |
126 | 126 | { |
127 | - if('' !== $this->parent_name) |
|
127 | + if ('' !== $this->parent_name) |
|
128 | 128 | { |
129 | 129 | return "{$this->parent_name}[{$this->name}]"; |
130 | 130 | } |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | |
134 | 134 | public function filter($v) |
135 | 135 | { |
136 | - foreach($this->components as $component) |
|
136 | + foreach ($this->components as $component) |
|
137 | 137 | { |
138 | - if($component instanceof FilterableComponentInterface && |
|
138 | + if ($component instanceof FilterableComponentInterface && |
|
139 | 139 | \is_callable($component->filter)) |
140 | 140 | { |
141 | 141 | $n = $component->name; |
@@ -146,15 +146,15 @@ discard block |
||
146 | 146 | return $v; |
147 | 147 | } |
148 | 148 | |
149 | - public function validation($v,&$e) |
|
149 | + public function validation($v, &$e) |
|
150 | 150 | { |
151 | - foreach($this->components as $component) |
|
151 | + foreach ($this->components as $component) |
|
152 | 152 | { |
153 | - if($component instanceof ValidatableComponentInterface && |
|
153 | + if ($component instanceof ValidatableComponentInterface && |
|
154 | 154 | \is_callable($component->validation)) |
155 | 155 | { |
156 | 156 | $n = $component->name; |
157 | - if(!$component->validation($v[$n],$e)) |
|
157 | + if (!$component->validation($v[$n], $e)) |
|
158 | 158 | { |
159 | 159 | return false; |
160 | 160 | } |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | */ |
169 | 169 | protected function on_created() |
170 | 170 | { |
171 | - foreach( $this->model['components'] as $args ) |
|
171 | + foreach ($this->model['components'] as $args) |
|
172 | 172 | { |
173 | 173 | $this->components[$args['name']] = $this->create_component($args); |
174 | 174 | } |
@@ -179,16 +179,16 @@ discard block |
||
179 | 179 | * @param type $args |
180 | 180 | * @return type |
181 | 181 | */ |
182 | - private function create_component( $args ) |
|
182 | + private function create_component($args) |
|
183 | 183 | { |
184 | 184 | $type = $args['type']; |
185 | 185 | |
186 | - if('composite' === $type) |
|
186 | + if ('composite' === $type) |
|
187 | 187 | { |
188 | 188 | $args['parent_name'] = $this->get_name(); |
189 | 189 | } |
190 | 190 | |
191 | - $c = \Amarkal\UI\ComponentFactory::create( $type, $args ); |
|
191 | + $c = \Amarkal\UI\ComponentFactory::create($type, $args); |
|
192 | 192 | |
193 | 193 | // Apply the composite name template |
194 | 194 | $c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template); |
@@ -203,9 +203,9 @@ discard block |
||
203 | 203 | * @return UI\AbstractComponent |
204 | 204 | * @throws \RuntimeException If there's no child component corresponding to the given name |
205 | 205 | */ |
206 | - private function get_component( $name ) |
|
206 | + private function get_component($name) |
|
207 | 207 | { |
208 | - if(!array_key_exists($name, $this->components)) |
|
208 | + if (!array_key_exists($name, $this->components)) |
|
209 | 209 | { |
210 | 210 | throw new \RuntimeException("Composite sub-component not found with name $name"); |
211 | 211 | } |
@@ -24,13 +24,13 @@ discard block |
||
24 | 24 | 'multi' => false, |
25 | 25 | 'data' => array(), |
26 | 26 | 'default' => array(), |
27 | - 'filter' => array( $this, 'filter' ) |
|
27 | + 'filter' => array($this, 'filter') |
|
28 | 28 | ); |
29 | 29 | } |
30 | 30 | |
31 | 31 | public function required_arguments() |
32 | 32 | { |
33 | - return array('name','data'); |
|
33 | + return array('name', 'data'); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | public function get_template_path() |
@@ -49,16 +49,16 @@ discard block |
||
49 | 49 | */ |
50 | 50 | public function filter($v) |
51 | 51 | { |
52 | - if($this->multi && !\is_array($v)) |
|
52 | + if ($this->multi && !\is_array($v)) |
|
53 | 53 | { |
54 | 54 | return \explode(',', $v); |
55 | 55 | } |
56 | 56 | return $v; |
57 | 57 | } |
58 | 58 | |
59 | - protected function is_selected( $value ) |
|
59 | + protected function is_selected($value) |
|
60 | 60 | { |
61 | - if($this->multi) |
|
61 | + if ($this->multi) |
|
62 | 62 | { |
63 | 63 | return \in_array($value, $this->value); |
64 | 64 | } |
@@ -12,15 +12,15 @@ discard block |
||
12 | 12 | */ |
13 | 13 | |
14 | 14 | // Prevent direct file access |
15 | -defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
|
15 | +defined('ABSPATH') or die('No script kiddies please!'); |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Prevent loading the library more than once |
19 | 19 | */ |
20 | -if( defined( 'AMARKAL_UI' ) ) return false; |
|
21 | -define( 'AMARKAL_UI', true ); |
|
20 | +if (defined('AMARKAL_UI')) return false; |
|
21 | +define('AMARKAL_UI', true); |
|
22 | 22 | |
23 | -if(!function_exists('amarkal_ui_render')) |
|
23 | +if (!function_exists('amarkal_ui_render')) |
|
24 | 24 | { |
25 | 25 | /** |
26 | 26 | * Render a UI component. |
@@ -30,14 +30,14 @@ discard block |
||
30 | 30 | * @param array $props The component's properties |
31 | 31 | * @return string The rendered HTML |
32 | 32 | */ |
33 | - function amarkal_ui_render( $type, array $props = array() ) |
|
33 | + function amarkal_ui_render($type, array $props = array()) |
|
34 | 34 | { |
35 | - $component = Amarkal\UI\ComponentFactory::create( $type, $props ); |
|
35 | + $component = Amarkal\UI\ComponentFactory::create($type, $props); |
|
36 | 36 | return $component->render(); |
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | -if(!function_exists('amarkal_ui_register_component')) |
|
40 | +if (!function_exists('amarkal_ui_register_component')) |
|
41 | 41 | { |
42 | 42 | /** |
43 | 43 | * Register a custom UI component. The registered component's class should |
@@ -47,13 +47,13 @@ discard block |
||
47 | 47 | * to one of the core component's type, it will override the core component. |
48 | 48 | * @param string $class_name The component's class name. |
49 | 49 | */ |
50 | - function amarkal_ui_register_component( $type, $class_name ) |
|
50 | + function amarkal_ui_register_component($type, $class_name) |
|
51 | 51 | { |
52 | - Amarkal\UI\ComponentFactory::register( $type, $class_name ); |
|
52 | + Amarkal\UI\ComponentFactory::register($type, $class_name); |
|
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
56 | -if(!function_exists('amarkal_ui_register_scripts')) |
|
56 | +if (!function_exists('amarkal_ui_register_scripts')) |
|
57 | 57 | { |
58 | 58 | /** |
59 | 59 | * Register Amarkal UI styles & scripts. These scripts are later enqueued by |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | */ |
62 | 62 | function amarkal_ui_register_scripts() |
63 | 63 | { |
64 | - \wp_register_script('amarkal-ui',Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-ui.min.js'),array('jquery'),false,true); |
|
65 | - \wp_register_script('ace-editor','https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.8/ace.js',array('jquery'),false,true); |
|
66 | - \wp_register_style('amarkal-ui',Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-ui.min.css'),array()); |
|
64 | + \wp_register_script('amarkal-ui', Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-ui.min.js'), array('jquery'), false, true); |
|
65 | + \wp_register_script('ace-editor', 'https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.8/ace.js', array('jquery'), false, true); |
|
66 | + \wp_register_style('amarkal-ui', Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-ui.min.css'), array()); |
|
67 | 67 | } |
68 | - \add_action('admin_init','amarkal_ui_register_scripts'); |
|
68 | + \add_action('admin_init', 'amarkal_ui_register_scripts'); |
|
69 | 69 | } |
70 | 70 | \ No newline at end of file |
@@ -32,7 +32,7 @@ |
||
32 | 32 | |
33 | 33 | public function required_arguments() |
34 | 34 | { |
35 | - return array('name','language'); |
|
35 | + return array('name', 'language'); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | public function get_template_path() |
@@ -21,7 +21,7 @@ |
||
21 | 21 | |
22 | 22 | public function required_arguments() |
23 | 23 | { |
24 | - return array('value','min','max'); |
|
24 | + return array('value', 'min', 'max'); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | public function get_template_path() |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @param array $components |
21 | 21 | */ |
22 | - public function __construct( array $components = array() ) |
|
22 | + public function __construct(array $components = array()) |
|
23 | 23 | { |
24 | 24 | $this->add_components($components); |
25 | 25 | } |
@@ -30,12 +30,12 @@ discard block |
||
30 | 30 | * @param string $name |
31 | 31 | * @return AbstractComponent |
32 | 32 | */ |
33 | - public function get_by_name( $name ) |
|
33 | + public function get_by_name($name) |
|
34 | 34 | { |
35 | 35 | $filtered = $this->filter(function($c) use ($name) { |
36 | 36 | return $c instanceof ValueComponentInterface && $c->name === $name; |
37 | 37 | }); |
38 | - if(count($filtered) < 1) |
|
38 | + if (count($filtered) < 1) |
|
39 | 39 | { |
40 | 40 | throw new \RuntimeException("No component with the name <b>$name</b> could be found."); |
41 | 41 | } |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | * @param string $type |
49 | 49 | * @return AbstractComponent[] |
50 | 50 | */ |
51 | - public function get_by_type( $type ) |
|
51 | + public function get_by_type($type) |
|
52 | 52 | { |
53 | 53 | return $this->filter(function($c) use ($type) { |
54 | 54 | return $c->component_type === $type; |
@@ -107,9 +107,9 @@ discard block |
||
107 | 107 | * @param AbstractComponent[] $components |
108 | 108 | * @return void |
109 | 109 | */ |
110 | - public function add_components( array $components ) |
|
110 | + public function add_components(array $components) |
|
111 | 111 | { |
112 | - foreach( $components as $component ) |
|
112 | + foreach ($components as $component) |
|
113 | 113 | { |
114 | 114 | $this->add_component($component); |
115 | 115 | } |
@@ -121,9 +121,9 @@ discard block |
||
121 | 121 | * @param AbstractComponent[] $args |
122 | 122 | * @return void |
123 | 123 | */ |
124 | - public function add_component( array $args ) |
|
124 | + public function add_component(array $args) |
|
125 | 125 | { |
126 | - if(!array_key_exists('type', $args)) |
|
126 | + if (!array_key_exists('type', $args)) |
|
127 | 127 | { |
128 | 128 | throw new \RuntimeException("Component configuration arrays must have a <b>type</b> argument"); |
129 | 129 | } |
@@ -140,16 +140,16 @@ discard block |
||
140 | 140 | * @param AbstractComponent $comp |
141 | 141 | * @return void |
142 | 142 | */ |
143 | - private function verify_name_uniqueness( AbstractComponent $comp ) |
|
143 | + private function verify_name_uniqueness(AbstractComponent $comp) |
|
144 | 144 | { |
145 | - if(!($comp instanceof ValueComponentInterface)) |
|
145 | + if (!($comp instanceof ValueComponentInterface)) |
|
146 | 146 | { |
147 | 147 | return; |
148 | 148 | } |
149 | 149 | |
150 | 150 | foreach ($this->components as $c) |
151 | 151 | { |
152 | - if($c->name === $comp->name) |
|
152 | + if ($c->name === $comp->name) |
|
153 | 153 | { |
154 | 154 | throw new \RuntimeException("Duplicate component name detected for the name <b>{$c->name}</b>."); |
155 | 155 | } |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | * @param function $callable |
163 | 163 | * @return array |
164 | 164 | */ |
165 | - private function filter( $callable ) |
|
165 | + private function filter($callable) |
|
166 | 166 | { |
167 | 167 | // array_values is needed in order to reindex the array after filtering it |
168 | 168 | return array_values(array_filter($this->components, $callable)); |
@@ -59,9 +59,9 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @param ComponentList $component_list |
61 | 61 | */ |
62 | - public function __construct( ComponentList $component_list = null ) |
|
62 | + public function __construct(ComponentList $component_list = null) |
|
63 | 63 | { |
64 | - if(null === $component_list) |
|
64 | + if (null === $component_list) |
|
65 | 65 | { |
66 | 66 | $component_list = new ComponentList(); |
67 | 67 | } |
@@ -85,16 +85,16 @@ discard block |
||
85 | 85 | * |
86 | 86 | * @return array The updated values array. |
87 | 87 | */ |
88 | - public function update( array $new_instance = array(), array $old_instance = array() ) |
|
88 | + public function update(array $new_instance = array(), array $old_instance = array()) |
|
89 | 89 | { |
90 | - $this->old_instance = array_merge($this->get_defaults(),$old_instance); |
|
91 | - $this->new_instance = array_merge($this->old_instance,$new_instance); |
|
90 | + $this->old_instance = array_merge($this->get_defaults(), $old_instance); |
|
91 | + $this->new_instance = array_merge($this->old_instance, $new_instance); |
|
92 | 92 | $this->final_instance = $this->new_instance; |
93 | 93 | |
94 | - foreach ( $this->component_list->get_value_components() as $component ) |
|
94 | + foreach ($this->component_list->get_value_components() as $component) |
|
95 | 95 | { |
96 | 96 | // Update individual fields, as well as the composite parent field. |
97 | - $this->update_component( $component ); |
|
97 | + $this->update_component($component); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | return $this->final_instance; |
@@ -118,9 +118,9 @@ discard block |
||
118 | 118 | * @param array $components |
119 | 119 | * @return void |
120 | 120 | */ |
121 | - public function reset_components( array $components ) |
|
121 | + public function reset_components(array $components) |
|
122 | 122 | { |
123 | - foreach( $components as $component ) |
|
123 | + foreach ($components as $component) |
|
124 | 124 | { |
125 | 125 | $this->reset_component($component); |
126 | 126 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | * @param AbstractComponent $component |
134 | 134 | * @return void |
135 | 135 | */ |
136 | - public function reset_component( $component ) |
|
136 | + public function reset_component($component) |
|
137 | 137 | { |
138 | 138 | $component->value = $component->default; |
139 | 139 | $this->final_instance[$component->name] = $component->default; |
@@ -168,21 +168,21 @@ discard block |
||
168 | 168 | * |
169 | 169 | * @param ValueComponentInterface $component The component to validate. |
170 | 170 | */ |
171 | - private function update_component( ValueComponentInterface $component ) |
|
171 | + private function update_component(ValueComponentInterface $component) |
|
172 | 172 | { |
173 | 173 | $component->value = $this->final_instance[$component->name]; |
174 | 174 | |
175 | 175 | // Skip if this field is disabled |
176 | - if( $this->is_disabled($component) ) |
|
176 | + if ($this->is_disabled($component)) |
|
177 | 177 | { |
178 | 178 | return; |
179 | 179 | } |
180 | 180 | |
181 | 181 | // Apply user-defined filter |
182 | - $this->filter( $component ); |
|
182 | + $this->filter($component); |
|
183 | 183 | |
184 | 184 | // Validate value |
185 | - $this->validate( $component ); |
|
185 | + $this->validate($component); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | * @param UI\AbstractComponent $component |
192 | 192 | * @return boolean |
193 | 193 | */ |
194 | - private function is_disabled( $component ) |
|
194 | + private function is_disabled($component) |
|
195 | 195 | { |
196 | 196 | return |
197 | 197 | $component instanceof DisableableComponentInterface && |
@@ -206,13 +206,13 @@ discard block |
||
206 | 206 | * |
207 | 207 | * @param UI\AbstractComponent $component |
208 | 208 | */ |
209 | - private function filter( $component ) |
|
209 | + private function filter($component) |
|
210 | 210 | { |
211 | - if( $component instanceof FilterableComponentInterface ) |
|
211 | + if ($component instanceof FilterableComponentInterface) |
|
212 | 212 | { |
213 | 213 | $filter = $component->filter; |
214 | 214 | |
215 | - if( is_callable( $filter ) ) |
|
215 | + if (is_callable($filter)) |
|
216 | 216 | { |
217 | 217 | $component->value = \call_user_func_array($filter, array($this->final_instance[$component->name])); |
218 | 218 | $this->final_instance[$component->name] = $component->value; |
@@ -228,22 +228,22 @@ discard block |
||
228 | 228 | * |
229 | 229 | * @param UI\AbstractComponent $component The component to validate. |
230 | 230 | */ |
231 | - private function validate( $component ) |
|
231 | + private function validate($component) |
|
232 | 232 | { |
233 | - if( !($component instanceof ValidatableComponentInterface) ) return; |
|
233 | + if (!($component instanceof ValidatableComponentInterface)) return; |
|
234 | 234 | |
235 | 235 | $name = $component->name; |
236 | 236 | $validate = $component->validation; |
237 | 237 | |
238 | 238 | $component->validity = $component::VALID; |
239 | 239 | |
240 | - if(is_callable($validate)) |
|
240 | + if (is_callable($validate)) |
|
241 | 241 | { |
242 | 242 | $error = ''; |
243 | 243 | $valid = \call_user_func_array($validate, array($this->final_instance[$name], &$error)); |
244 | 244 | |
245 | 245 | // Invalid input, use old instance or default value |
246 | - if ( true !== $valid ) |
|
246 | + if (true !== $valid) |
|
247 | 247 | { |
248 | 248 | $this->errors[$name] = $error ? $error : ValidatableComponentInterface::DEFAULT_MESSAGE; |
249 | 249 | $component->value = $this->old_instance[$name]; |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | { |
263 | 263 | $defaults = array(); |
264 | 264 | |
265 | - foreach( $this->component_list->get_value_components() as $component ) |
|
265 | + foreach ($this->component_list->get_value_components() as $component) |
|
266 | 266 | { |
267 | 267 | $defaults[$component->name] = $component->default; |
268 | 268 | } |