providers
5 months ago
complex-control.class.php
5 months ago
control-holder.class.php
5 months ago
control.class.php
5 months ago
custom-element.class.php
5 months ago
form-element.class.php
5 months ago
form-layout.class.php
5 months ago
form.class.php
5 months ago
holder.class.php
5 months ago
html-builder.class.php
5 months ago
index.php
5 months ago
control.class.php
411 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The file contains the base class for all controls. |
| 4 | * |
| 5 | * @package factory-forms |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | // Exit if accessed directly |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | if ( ! class_exists( 'Wbcr_FactoryForms600_Control' ) ) { |
| 15 | |
| 16 | /** |
| 17 | * The base class for all controls. |
| 18 | * |
| 19 | * @since 1.0.0 |
| 20 | */ |
| 21 | abstract class Wbcr_FactoryForms600_Control extends Wbcr_FactoryForms600_FormElement { |
| 22 | |
| 23 | /** |
| 24 | * Is this element a control? |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | * @var bool |
| 28 | */ |
| 29 | public $is_control = true; |
| 30 | |
| 31 | /** |
| 32 | * Is this element a complex control? |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | * @var bool |
| 36 | */ |
| 37 | public $is_complex_control = false; |
| 38 | |
| 39 | /** |
| 40 | * A provider that is used to get values. |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | * @var Wbcr_IFactoryForms600_ValueProvider |
| 44 | */ |
| 45 | protected $provider = null; |
| 46 | |
| 47 | /** |
| 48 | * Create a new instance of the control. |
| 49 | * |
| 50 | * @param mixed[] $options |
| 51 | * @param FactoryForms600_Form $form |
| 52 | * @param null $provider |
| 53 | * @since 1.0.0 |
| 54 | * @return void |
| 55 | */ |
| 56 | public function __construct( $options, $form, $provider = null ) { |
| 57 | parent::__construct( $options, $form ); |
| 58 | $this->provider = $provider; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets a provider for the control. |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | * @param IFactoryForms600_ValueProvider $provider |
| 66 | * @return void |
| 67 | */ |
| 68 | public function setProvider( $provider ) { |
| 69 | $this->provider = $provider; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns a control name used to save data with a provider. |
| 74 | * |
| 75 | * The method can return if the control have several elements. |
| 76 | * |
| 77 | * @since 1.0.0 |
| 78 | * @return string[]|string|null A control name. |
| 79 | */ |
| 80 | public function getName() { |
| 81 | return isset( $this->options['name'] ) |
| 82 | ? $this->options['name'] |
| 83 | : null; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Prints a control name used to save data with a provider. |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | * @return void |
| 91 | */ |
| 92 | protected function printName() { |
| 93 | $name = $this->getName(); |
| 94 | if ( is_array( $name ) ) { |
| 95 | echo $name[0]; |
| 96 | } else { |
| 97 | echo $name; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns a control scope. |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | * @return string|null A control scope. |
| 106 | */ |
| 107 | public function getScope() { |
| 108 | return isset( $this->options['scope'] ) |
| 109 | ? $this->options['scope'] |
| 110 | : null; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Prints a control scope. |
| 115 | * |
| 116 | * @since 1.0.0 |
| 117 | * @return void |
| 118 | */ |
| 119 | protected function printScope() { |
| 120 | echo $this->getScope(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns a name of control on a form (scope + _ + name) |
| 125 | * |
| 126 | * @since 1.0.0 |
| 127 | * @param null|string $name |
| 128 | * @return array|null|string|string[] |
| 129 | */ |
| 130 | public function getNameOnForm( $name = null ) { |
| 131 | $scope = $this->getScope(); |
| 132 | $name = ! $name |
| 133 | ? $this->getName() |
| 134 | : $name; |
| 135 | |
| 136 | if ( is_array( $name ) ) { |
| 137 | $names = []; |
| 138 | foreach ( $name as $item ) { |
| 139 | $names[] = empty( $scope ) |
| 140 | ? $item |
| 141 | : $scope . '_' . $item; |
| 142 | } |
| 143 | |
| 144 | return $names; |
| 145 | } |
| 146 | |
| 147 | if ( empty( $scope ) ) { |
| 148 | return $name; |
| 149 | } |
| 150 | if ( empty( $name ) ) { |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | return $scope . '_' . $name; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Prints a control name on a form. |
| 159 | * |
| 160 | * @since 1.0.0 |
| 161 | * @return void |
| 162 | */ |
| 163 | public function printNameOnForm() { |
| 164 | $name = $this->getNameOnForm(); |
| 165 | |
| 166 | if ( is_array( $name ) ) { |
| 167 | echo $name[0]; |
| 168 | } else { |
| 169 | echo $name; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns a submit value of the control by a given name. |
| 175 | * |
| 176 | * @since 1.0.0 |
| 177 | * @param string $name |
| 178 | * @param string $sub_name |
| 179 | * @return string |
| 180 | */ |
| 181 | public function getSubmitValue( $name, $sub_name ) { |
| 182 | $name_on_form = $this->getNameOnForm( $name ); |
| 183 | |
| 184 | $raw_value = isset( $_POST[ $name_on_form ] ) |
| 185 | ? $_POST[ $name_on_form ] |
| 186 | : null; |
| 187 | |
| 188 | $value = $raw_value; |
| 189 | |
| 190 | if ( is_array( $value ) ) { |
| 191 | $value = array_map( 'sanitize_text_field', $value ); |
| 192 | $value = implode( ',', $value ); |
| 193 | } else { |
| 194 | $value = sanitize_text_field( $value ); |
| 195 | } |
| 196 | |
| 197 | return $this->filterValue( $value, $raw_value ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @param $value |
| 202 | * @param $raw_value |
| 203 | * @return mixed |
| 204 | */ |
| 205 | protected function filterValue( $value, $raw_value ) { |
| 206 | $sanitize_func = $this->getOption( 'filter_value' ); |
| 207 | |
| 208 | // if the data options is a valid callback for an object method or closure |
| 209 | if ( ! empty( $sanitize_func ) && is_callable( $sanitize_func ) ) { |
| 210 | return call_user_func_array( $sanitize_func, [ $value, $raw_value ] ); |
| 211 | } |
| 212 | |
| 213 | return $value; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * Returns an array of value to save received after submission of a form. |
| 219 | * |
| 220 | * @see getSubmitValue |
| 221 | * |
| 222 | * The array has the following format: |
| 223 | * array( |
| 224 | * 'control-name1' => 'value1', |
| 225 | * 'control-name2__sub-name1' => 'value2' |
| 226 | * 'control-name2__sub-name2' => 'value3' |
| 227 | * ) |
| 228 | * |
| 229 | * @since 3.1.0 |
| 230 | * @return mixed[] |
| 231 | */ |
| 232 | public function getValuesToSave() { |
| 233 | $values = []; |
| 234 | $name = $this->getName(); |
| 235 | |
| 236 | if ( is_array( $name ) ) { |
| 237 | $i = 0; |
| 238 | |
| 239 | foreach ( $name as $single_name ) { |
| 240 | $sub_name = $this->getSubName( $single_name ); |
| 241 | if ( ! $sub_name ) { |
| 242 | $sub_name = $i; |
| 243 | ++$i; |
| 244 | } |
| 245 | $values[ $single_name ] = $this->getSubmitValue( $single_name, $sub_name ); |
| 246 | } |
| 247 | |
| 248 | return $values; |
| 249 | } |
| 250 | |
| 251 | $values[ $name ] = $this->getSubmitValue( $name, null ); |
| 252 | |
| 253 | return $values; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns an initial value of control that is used to render the control first time. |
| 258 | * |
| 259 | * @since 1.0.0 |
| 260 | * @return mixed; |
| 261 | */ |
| 262 | public function getValue( $index = null, $multiple = false ) { |
| 263 | if ( isset( $this->options['value'] ) ) { |
| 264 | if ( is_array( $this->options['value'] ) ) { |
| 265 | if ( $index !== null ) { |
| 266 | return $this->options['value'][ $index ]; |
| 267 | } else { |
| 268 | return $this->options['value']; |
| 269 | } |
| 270 | } else { |
| 271 | return $this->options['value']; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | $default = null; |
| 276 | if ( isset( $this->options['default'] ) ) { |
| 277 | if ( is_array( $this->options['default'] ) ) { |
| 278 | if ( $index !== null ) { |
| 279 | $default = $this->options['default'][ $index ]; |
| 280 | } else { |
| 281 | $default = $this->options['default']; |
| 282 | } |
| 283 | } else { |
| 284 | $default = $this->options['default']; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if ( $this->provider ) { |
| 289 | $name = $this->getName(); |
| 290 | |
| 291 | if ( is_array( $name ) ) { |
| 292 | |
| 293 | $values = []; |
| 294 | $i = 0; |
| 295 | |
| 296 | foreach ( $name as $single_name ) { |
| 297 | $sub_name = $this->getSubName( $single_name ); |
| 298 | if ( ! $sub_name ) { |
| 299 | $sub_name = $i; |
| 300 | ++$i; |
| 301 | } |
| 302 | $values[ $sub_name ] = $this->provider->getValue( |
| 303 | $single_name, |
| 304 | isset( $default[ $sub_name ] ) |
| 305 | ? $default[ $sub_name ] |
| 306 | : null |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | if ( $index !== null ) { |
| 311 | return $values[ $index ]; |
| 312 | } |
| 313 | |
| 314 | return $values; |
| 315 | } else { |
| 316 | return $this->provider->getValue( $this->getName(), $default, $multiple ); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return $default; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Shows the control. |
| 325 | * |
| 326 | * @since 1.0.0 |
| 327 | * @return void |
| 328 | */ |
| 329 | public function render() { |
| 330 | $this->addCssClass( 'factory-from-control-' . $this->type ); |
| 331 | |
| 332 | // if the control is off, then ignore it |
| 333 | $off = $this->getOption( 'off', false ); |
| 334 | |
| 335 | if ( $off ) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | $this->beforeHtml(); |
| 340 | $this->html(); |
| 341 | $this->afterHtml(); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * A virtual method that is executed before rendering html markup of the control. |
| 346 | * |
| 347 | * @since 1.0.0 |
| 348 | * @return void |
| 349 | */ |
| 350 | protected function beforeHtml() { |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * A virtual method that is executed after rendering html markup of the control. |
| 355 | * |
| 356 | * @since 1.0.0 |
| 357 | * @return void |
| 358 | */ |
| 359 | protected function afterHtml() { |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Renders the html markup for the control. |
| 364 | * |
| 365 | * @since 1.0.0 |
| 366 | * @return void |
| 367 | */ |
| 368 | public function html() { |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Returns a layout option. |
| 373 | * |
| 374 | * @since 1.0.0 |
| 375 | * @param string $option_name A layout option to return. |
| 376 | * @param mixed $default A default value to return if the option doesn't exist. |
| 377 | * @return mixed |
| 378 | */ |
| 379 | public function getLayoutOption( $option_name, $default ) { |
| 380 | if ( ! isset( $this->options['layout'] ) ) { |
| 381 | return $default; |
| 382 | } |
| 383 | if ( ! isset( $this->options['layout'][ $option_name ] ) ) { |
| 384 | return $default; |
| 385 | } |
| 386 | |
| 387 | return $this->options['layout'][ $option_name ]; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Splits the control name by '__' and return the right part. |
| 392 | * |
| 393 | * For example, if the $control_name is 'control__color', then returns 'color'. |
| 394 | * Throws an error if the control name cannot be splitted. |
| 395 | * |
| 396 | * @since 3.1.0 |
| 397 | * @param string $control_name |
| 398 | * @return string |
| 399 | */ |
| 400 | protected function getSubName( $control_name ) { |
| 401 | |
| 402 | $parts = explode( '__', $control_name, 2 ); |
| 403 | if ( ! isset( $parts[1] ) ) { |
| 404 | return null; |
| 405 | } |
| 406 | |
| 407 | return $parts[1]; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 |