| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstraction for creating data to pass to wp_localize_script when working with Qcformbuilder Forms REST API |
| 4 |
* |
| 5 |
* @package Caldera_Forms Modified by QuantumCloud |
| 6 |
* @author Josh Pollock <Josh@CalderaWP.com> |
| 7 |
* @license GPL-2.0+ |
| 8 |
* @link |
| 9 |
* @copyright 2017 CalderaWP LLC |
| 10 |
*/ |
| 11 |
class Qcformbuilder_Forms_API_JsConfig implements JsonSerializable{ |
| 12 |
|
| 13 |
/** |
| 14 |
* Form config |
| 15 |
* |
| 16 |
* @since 1.5.0 |
| 17 |
* |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
protected $form; |
| 21 |
|
| 22 |
/** |
| 23 |
* Data to return |
| 24 |
* |
| 25 |
* @since 1.5.0 |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $data; |
| 30 |
|
| 31 |
/** |
| 32 |
* Qcformbuilder_Forms_API_JsConfig constructor. |
| 33 |
* |
| 34 |
* @param array $form Optional, form config with tying to a specific form |
| 35 |
*/ |
| 36 |
public function __construct( array $form = array() ) { |
| 37 |
$this->form = $form; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @inheritdoc |
| 42 |
* @since 1.5.0 |
| 43 |
*/ |
| 44 |
public function jsonSerialize():mixed { |
| 45 |
return $this->toArray(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get data as array |
| 50 |
* |
| 51 |
* @since 1.5.0 |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function toArray(){ |
| 56 |
if ( empty( $this->data ) ) { |
| 57 |
$this->set_data(); |
| 58 |
} |
| 59 |
|
| 60 |
return $this->data; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Setup data to localize |
| 65 |
* |
| 66 |
* @since 1.5.0 |
| 67 |
*/ |
| 68 |
protected function set_data(){ |
| 69 |
$this->data = array( |
| 70 |
'dateFormat' => Qcformbuilder_Forms::time_format(), |
| 71 |
'api' => array( |
| 72 |
'root' => esc_url( trailingslashit( Qcformbuilder_Forms_API_Util::url() ) ), |
| 73 |
'form' => esc_url( trailingslashit( Qcformbuilder_Forms_API_Util::url( 'forms' ) ) ), |
| 74 |
'entries' => esc_url( trailingslashit( Qcformbuilder_Forms_API_Util::url( 'entries' ) ) ), |
| 75 |
'entrySettings' => esc_url( trailingslashit( Qcformbuilder_Forms_API_Util::url( 'settings/entries' ) ) ), |
| 76 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 77 |
'token' => 'false', |
| 78 |
), |
| 79 |
); |
| 80 |
|
| 81 |
if( ! empty( $this->form ) ){ |
| 82 |
$this->data[ 'formId' ] = $this->form[ 'ID' ]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Modify data to localize for API |
| 87 |
* |
| 88 |
* @since 1.5.0 |
| 89 |
* |
| 90 |
* @param array $data The data |
| 91 |
* @param array|null $form Form config if used, might be empty |
| 92 |
*/ |
| 93 |
$this->data = apply_filters( 'qcformbuilder_forms_api_js_config', $this->data, $this->form ); |
| 94 |
|
| 95 |
} |
| 96 |
} |