| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sets the defaults specified by a shortcode, unique for each shortcode usage |
| 4 |
* |
| 5 |
* Solves problem where two shortcodes of same form have different defaults field sets, see: https://github.com/QcformbuilderWP/Qcformbuilder-Forms/issues/1499 |
| 6 |
* |
| 7 |
* @package Caldera_Forms Modified by QuantumCloud |
| 8 |
* @author Josh Pollock <Josh@CalderaWP.com> |
| 9 |
* @license GPL-2.0+ |
| 10 |
* @link |
| 11 |
* @copyright 2017 CalderaWP LLC |
| 12 |
*/ |
| 13 |
class Qcformbuilder_Forms_Shortcode_Defaults { |
| 14 |
|
| 15 |
/** |
| 16 |
* Form ID this object acts on |
| 17 |
* |
| 18 |
* @since 1.5.0.9 |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $form_id; |
| 23 |
|
| 24 |
/** |
| 25 |
* Defaults to add to fields |
| 26 |
* |
| 27 |
* @since 1.5.0.9 |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
protected $defaults; |
| 32 |
|
| 33 |
/** |
| 34 |
* Qcformbuilder_Forms_Shortcode_Defaults constructor. |
| 35 |
* |
| 36 |
* @since 1.5.0.9 |
| 37 |
* |
| 38 |
* @param string $form_id ID of form |
| 39 |
* @param array $defaults Defaults |
| 40 |
*/ |
| 41 |
public function __construct( $form_id, $defaults ){ |
| 42 |
$this->form_id = $form_id; |
| 43 |
$this->defaults = $defaults; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Add the filter |
| 49 |
* |
| 50 |
* @since 1.5.0.9 |
| 51 |
*/ |
| 52 |
public function add_hooks(){ |
| 53 |
add_filter( 'qcformbuilder_forms_render_get_field', array( $this, 'set_default' ), 19, 2 ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Remove the filter |
| 58 |
* |
| 59 |
* @since 1.5.0.9 |
| 60 |
*/ |
| 61 |
public function remove_hooks(){ |
| 62 |
remove_filter( 'qcformbuilder_forms_render_get_field', array( $this, 'set_default' ), 19 ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Set field default |
| 67 |
* |
| 68 |
* @since 1.5.0.9 |
| 69 |
* |
| 70 |
* @uses "qcformbuilder_forms_render_get_field" filter |
| 71 |
* |
| 72 |
* @param array $field Field config |
| 73 |
* @param array $form Form config |
| 74 |
* |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function set_default( $field, $form ){ |
| 78 |
if( array_key_exists( $field[ 'ID' ], $this->defaults ) && $form[ 'ID' ] == $this->form_id ){ |
| 79 |
$field[ 'config' ][ 'default' ] = $this->defaults[ $field[ 'ID' ] ]; |
| 80 |
} |
| 81 |
|
| 82 |
return $field; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
} |