| 1 |
<?php |
| 2 |
|
| 3 |
namespace Carbon_Fields\Widget; |
| 4 |
|
| 5 |
use Carbon_Fields\Helper\Helper; |
| 6 |
use Carbon_Fields\Container\Container; |
| 7 |
use Carbon_Fields\Datastore\Datastore; |
| 8 |
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
| 9 |
|
| 10 |
/** |
| 11 |
* Widget, datastore and container handler class. |
| 12 |
*/ |
| 13 |
abstract class Widget extends \WP_Widget { |
| 14 |
public static $registered_widget_ids = array(); |
| 15 |
|
| 16 |
/** |
| 17 |
* Widget Datastore |
| 18 |
* |
| 19 |
* @var \Carbon_Fields\Datastore\Widget_Datastore |
| 20 |
*/ |
| 21 |
protected $datastore; |
| 22 |
|
| 23 |
/** |
| 24 |
* Determines if widget wrapper html should be printed |
| 25 |
* |
| 26 |
* @see widget() |
| 27 |
* @var bool |
| 28 |
*/ |
| 29 |
protected $print_wrappers = true; |
| 30 |
|
| 31 |
/** |
| 32 |
* Control options to pass to WordPress Widget constructor |
| 33 |
* |
| 34 |
* @see setup() |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
protected $widget_control_options = array( 'width' => 295 ); |
| 38 |
|
| 39 |
/** |
| 40 |
* Array of Carbon Fields for the widget |
| 41 |
* |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
protected $custom_fields = array(); |
| 45 |
|
| 46 |
/** |
| 47 |
* String prefix for widget ids |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
protected $widget_id_prefix = 'carbon_fields_'; |
| 52 |
|
| 53 |
/** |
| 54 |
* Create the widget. |
| 55 |
* A wrapper around the default WP widget constructor. |
| 56 |
* |
| 57 |
* @param string $widget_id Widget id |
| 58 |
* @param string $title Widget name |
| 59 |
* @param string $description Widget description |
| 60 |
* @param array $fields Array of fields |
| 61 |
* @param string $classname String of CSS classes |
| 62 |
*/ |
| 63 |
public function setup( $widget_id, $title, $description, $fields, $classname = '' ) { |
| 64 |
\Carbon_Fields\Carbon_Fields::verify_boot(); |
| 65 |
$widget_id = $this->widget_id_prefix . $widget_id; |
| 66 |
|
| 67 |
$this->datastore = Datastore::make( 'widget' ); |
| 68 |
if ( empty( $title ) ) { |
| 69 |
Incorrect_Syntax_Exception::raise( 'Empty widget title is not supported' ); |
| 70 |
} |
| 71 |
|
| 72 |
$this->add_fields( $fields ); |
| 73 |
|
| 74 |
$this->register_widget_id( $widget_id ); |
| 75 |
|
| 76 |
# Generate Classes |
| 77 |
if ( ! is_array( $classname ) ) { |
| 78 |
$classname = (array) $classname; |
| 79 |
} |
| 80 |
$classname[] = $widget_id; |
| 81 |
$classname = array_filter( $classname ); |
| 82 |
$classname = implode( ' ', $classname ); |
| 83 |
|
| 84 |
$widget_options = array( |
| 85 |
'description' => $description, |
| 86 |
'classname' => $classname, |
| 87 |
'widget_ID' => $widget_id, |
| 88 |
); |
| 89 |
|
| 90 |
parent::__construct( $widget_id, $title, $widget_options, $this->widget_control_options ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Updates a particular instance of a widget. |
| 95 |
* |
| 96 |
* @param array $new_instance New settings for this instance as input by the user via |
| 97 |
* WP_Widget::form(). |
| 98 |
* @param array $old_instance Old settings for this instance. |
| 99 |
* @return array Settings to save or bool false to cancel saving. |
| 100 |
*/ |
| 101 |
public function update( $new_instance, $old_instance ) { |
| 102 |
// Support compacted input |
| 103 |
$new_instance = Helper::expand_compacted_input( $new_instance ); |
| 104 |
$compact_key = 'widget-' . $this->id_base; |
| 105 |
if ( ! empty( $new_instance[ $compact_key ][0] ) ) { |
| 106 |
$new_instance = array_merge( $new_instance, $new_instance[ $compact_key ][0] ); |
| 107 |
unset( $new_instance[ $compact_key ] ); |
| 108 |
} |
| 109 |
|
| 110 |
// Handle update |
| 111 |
$this->datastore->import_storage( $old_instance ); |
| 112 |
|
| 113 |
foreach ( $this->custom_fields as $field ) { |
| 114 |
$field->set_value_from_input( $new_instance ); |
| 115 |
$field->save(); |
| 116 |
} |
| 117 |
|
| 118 |
return $this->datastore->export_storage(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Outputs the settings update form. |
| 123 |
* |
| 124 |
* @param array $instance Current settings. |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function form( $instance ) { |
| 128 |
$this->datastore->import_storage( $instance ); |
| 129 |
|
| 130 |
$this->register_container( true ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Registers the container definition for the widget. |
| 135 |
* |
| 136 |
* @param boolean $render Whether the form should be rendered |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function register_container( $render = false ) { |
| 140 |
$custom_fields = array(); |
| 141 |
|
| 142 |
foreach ( $this->custom_fields as $field ) { |
| 143 |
$tmp_field = clone $field; |
| 144 |
$tmp_field->load(); |
| 145 |
|
| 146 |
$field_name = $this->get_field_name( $tmp_field->get_name() ); |
| 147 |
$tmp_field->set_name( $field_name ); |
| 148 |
|
| 149 |
$custom_fields[] = $tmp_field; |
| 150 |
} |
| 151 |
|
| 152 |
$container = Container::factory( 'widget', $this->id, $this->id ) |
| 153 |
->add_fields( $custom_fields ) |
| 154 |
->init(); |
| 155 |
|
| 156 |
if ( $render ) { |
| 157 |
$container->render(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Echoes the widget content. |
| 163 |
* Sub-classes can over-ride this method to generate their widget code |
| 164 |
* but it is best to override front_end(). |
| 165 |
* |
| 166 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 167 |
* 'before_widget', and 'after_widget'. |
| 168 |
* @param array $instance The settings for the particular instance of the widget. |
| 169 |
*/ |
| 170 |
public function widget( $args, $instance ) { |
| 171 |
$this->datastore->import_storage( $instance ); |
| 172 |
|
| 173 |
if ( $this->print_wrappers ) { |
| 174 |
echo $args['before_widget']; |
| 175 |
} |
| 176 |
|
| 177 |
$instance_values = array(); |
| 178 |
foreach ( $this->custom_fields as $field ) { |
| 179 |
$clone = clone $field; |
| 180 |
$clone->load(); |
| 181 |
$instance_values[ $clone->get_base_name() ] = $clone->get_formatted_value(); |
| 182 |
} |
| 183 |
$this->front_end( $args, $instance_values ); |
| 184 |
|
| 185 |
if ( $this->print_wrappers ) { |
| 186 |
echo $args['after_widget']; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* The actual content of the widget. |
| 192 |
* Generally should be overriden by the specific widget classes. |
| 193 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 194 |
* 'before_widget', and 'after_widget'. |
| 195 |
* @param array $instance The settings for the particular instance of the widget. |
| 196 |
*/ |
| 197 |
public function front_end( $args, $instance ) { } |
| 198 |
|
| 199 |
/** |
| 200 |
* Append array of fields to the current fields set. All items of the array |
| 201 |
* must be instances of Field and their names should be unique for all |
| 202 |
* Carbon containers. |
| 203 |
* |
| 204 |
* @param array $fields |
| 205 |
*/ |
| 206 |
public function add_fields( $fields ) { |
| 207 |
foreach ( $fields as $field ) { |
| 208 |
if ( ! ( $field instanceof \Carbon_Fields\Field\Field ) ) { |
| 209 |
Incorrect_Syntax_Exception::raise( 'Object must be of type Carbon_Fields\\Field\\Field' ); |
| 210 |
return; |
| 211 |
} |
| 212 |
$this->register_field_name( $field->get_name() ); |
| 213 |
$field->set_name_prefix( '' ); |
| 214 |
$field->set_datastore( $this->datastore, true ); |
| 215 |
} |
| 216 |
$this->custom_fields = array_merge( $this->custom_fields, $fields ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Verify widget field names are unique. |
| 221 |
* |
| 222 |
* @param string $name Field name |
| 223 |
* @return boolean |
| 224 |
*/ |
| 225 |
public function register_field_name( $name ) { |
| 226 |
static $registered_field_names = array(); |
| 227 |
|
| 228 |
if ( in_array( $name, $registered_field_names ) ) { |
| 229 |
Incorrect_Syntax_Exception::raise( 'Field name "' . $name . '" already registered' ); |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
$registered_field_names[] = $name; |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Verify widget IDs are unique. |
| 239 |
* |
| 240 |
* @param string $id Widget ID |
| 241 |
*/ |
| 242 |
public function register_widget_id( $id ) { |
| 243 |
if ( in_array( $id, static::$registered_widget_ids ) ) { |
| 244 |
Incorrect_Syntax_Exception::raise( 'Widget with ID "' . $id . '" already registered. Please change the widget title' ); |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
static::$registered_widget_ids[] = $id; |
| 249 |
} |
| 250 |
} |
| 251 |
|