amarkal /
amarkal-widget
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
Loading history...
|
|||
| 7 | /** |
||
| 8 | * @var array The configuration array |
||
| 9 | */ |
||
| 10 | private $config; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var \Amarkal\UI\Form Amarkal UI for data processing |
||
| 14 | */ |
||
| 15 | private $ui_form; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Get the user config and call the parent constructor |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | $config = $this->get_config(); |
||
| 23 | |||
| 24 | parent::__construct( |
||
| 25 | $config['id'], |
||
| 26 | $config['name'], |
||
| 27 | $config['widget_options'], |
||
| 28 | $config['control_options'] |
||
| 29 | ); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Generates the administration form for the widget |
||
| 34 | * |
||
| 35 | * @param array $instance The array of keys and values for the widget |
||
| 36 | */ |
||
| 37 | public function form( $instance ) |
||
| 38 | { |
||
| 39 | $form = $this->get_form(); |
||
| 40 | $components = $form->get_components(); |
||
| 41 | |||
| 42 | $form->update($instance); |
||
| 43 | |||
| 44 | // Set the widget-specific names and ids |
||
| 45 | foreach( $components as $component ) |
||
| 46 | { |
||
| 47 | $component->original_name = $component->name; |
||
| 48 | $component->id = $this->get_field_id($component->name); |
||
| 49 | $component->name = $this->get_field_name($component->name); |
||
| 50 | } |
||
| 51 | |||
| 52 | include __DIR__.'/Form.phtml'; |
||
| 53 | |||
| 54 | // Use the original names again (for when the components update) |
||
| 55 | foreach( $components as $component ) |
||
| 56 | { |
||
| 57 | $component->id = $component->original_name; |
||
| 58 | $component->name = $component->original_name; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Process the widget's options before they are saved into the db |
||
| 64 | * |
||
| 65 | * @param array $new_instance The previous instance of values before the update. |
||
| 66 | * @param array $old_instance The new instance of values to be generated via the update. |
||
| 67 | */ |
||
| 68 | public function update( $new_instance, $old_instance ) |
||
| 69 | { |
||
| 70 | $form = $this->get_form(); |
||
| 71 | return $form->update($new_instance, $old_instance); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get the Amarkal UI form. |
||
| 76 | * |
||
| 77 | * @return \Amarkal\UI\Form |
||
| 78 | */ |
||
| 79 | private function get_form() |
||
| 80 | { |
||
| 81 | if( !isset($this->ui_form) ) |
||
| 82 | { |
||
| 83 | $config = $this->get_config(); |
||
| 84 | $this->ui_form = new \Amarkal\UI\Form($config['fields']); |
||
| 85 | } |
||
| 86 | return $this->ui_form; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the default widget configuration. |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | private function default_config() |
||
| 95 | { |
||
| 96 | return array( |
||
| 97 | 'id' => null, |
||
| 98 | 'name' => null, |
||
| 99 | 'widget_options' => array(), |
||
| 100 | 'control_options' => array(), |
||
| 101 | 'fields' => array() |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the configuraiton array, merging between the user and the default |
||
| 107 | * configuration values. |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | * @throws \RuntimeException if the configuration is missing the ID argument |
||
| 111 | */ |
||
| 112 | private function get_config() |
||
| 113 | { |
||
| 114 | if( !isset($this->config) ) |
||
| 115 | { |
||
| 116 | $this->config = array_merge( |
||
| 117 | $this->default_config(), |
||
| 118 | $this->config() |
||
| 119 | ); |
||
| 120 | |||
| 121 | if( null === $this->config['id'] ) |
||
| 122 | { |
||
| 123 | throw new \RuntimeException('No \'id\' was sepcified in the widget configuration'); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | return $this->config; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The user configuration array. Must be implemented in the child class. |
||
| 131 | */ |
||
| 132 | abstract public function config(); |
||
| 133 | } |