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\Widget; |
||
4 | |||
5 | abstract class AbstractWidget extends \WP_Widget |
||
6 | { |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
7 | private $config; |
||
8 | |||
9 | public function __construct() |
||
10 | { |
||
11 | $config = $this->get_config(); |
||
12 | |||
13 | parent::__construct( |
||
14 | $config['id'], |
||
15 | $config['name'], |
||
16 | $config['widget_options'], |
||
17 | $config['control_options'] |
||
18 | ); |
||
19 | } |
||
20 | |||
21 | public function form( $instance ) |
||
22 | { |
||
23 | $values = array_merge($this->get_default_field_values(),$instance); |
||
24 | $config = $this->get_config(); |
||
25 | |||
26 | foreach( $config['fields'] as $field_args ) |
||
27 | { |
||
28 | $field_args['value'] = $values[$field_args['name']]; |
||
29 | $field_args['id'] = $this->get_field_id($field_args['name']); |
||
30 | $field_args['name'] = $this->get_field_name($field_args['name']); |
||
31 | |||
32 | $field = new FormField($field_args); |
||
33 | echo $field->render(); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | public function update( $new_instance, $old_instance ) |
||
38 | { |
||
39 | $instance = array_merge($this->get_default_field_values(), $new_instance); |
||
40 | $config = $this->get_config(); |
||
41 | |||
42 | foreach( $config['fields'] as $field ) |
||
43 | { |
||
44 | $name = $field['name']; |
||
45 | $value = $new_instance[$name]; |
||
46 | $instance[$name] = $value; |
||
47 | } |
||
48 | return $instance; |
||
49 | } |
||
50 | |||
51 | private function get_default_field_values() |
||
52 | { |
||
53 | $defaults = array(); |
||
54 | $config = $this->get_config(); |
||
55 | foreach( $config['fields'] as $field ) |
||
56 | { |
||
57 | $defaults[$field['name']] = $field['default']; |
||
58 | } |
||
59 | return $defaults; |
||
60 | } |
||
61 | |||
62 | private function default_config() |
||
63 | { |
||
64 | return array( |
||
65 | 'id' => null, |
||
66 | 'name' => null, |
||
67 | 'widget_options' => array(), |
||
68 | 'control_options' => array(), |
||
69 | 'fields' => array() |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | private function get_config() |
||
74 | { |
||
75 | if( !isset($this->config) ) |
||
76 | { |
||
77 | $this->config = array_merge( |
||
78 | $this->default_config(), |
||
79 | $this->config() |
||
80 | ); |
||
81 | |||
82 | if( null === $this->config['id'] ) |
||
83 | { |
||
84 | throw new \RuntimeException('No \'id\' was sepcified in the widget configuration'); |
||
85 | } |
||
86 | } |
||
87 | return $this->config; |
||
88 | } |
||
89 | |||
90 | abstract public function config(); |
||
91 | } |