| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form abstraction for use by API responses |
| 4 |
* |
| 5 |
* Has special handling for field collections |
| 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_API_Form implements ArrayAccess { |
| 14 |
|
| 15 |
/** |
| 16 |
* Form configuration |
| 17 |
* |
| 18 |
* @since 1.5.0 |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected $form; |
| 23 |
|
| 24 |
/** |
| 25 |
* Fields of fields that can be used in this context |
| 26 |
* |
| 27 |
* @since 1.5.0 |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
protected $fields; |
| 32 |
|
| 33 |
/** |
| 34 |
* Current REST API request |
| 35 |
* |
| 36 |
* @since 1.5.0 |
| 37 |
* |
| 38 |
* @var WP_REST_Request |
| 39 |
*/ |
| 40 |
protected $request; |
| 41 |
|
| 42 |
/** |
| 43 |
* Qcformbuilder_Forms_API_Form constructor. |
| 44 |
* |
| 45 |
* @since 1.5.0 |
| 46 |
* |
| 47 |
* @param array $form Form config |
| 48 |
*/ |
| 49 |
public function __construct( array $form ) { |
| 50 |
$this->form = $form; |
| 51 |
//JOSH - don't call $this->set_fields() here, or WP_REST_Request object will not be available to filter. |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Set current REST request in object |
| 56 |
* |
| 57 |
* @since 1.5.0 |
| 58 |
* |
| 59 |
* @param WP_REST_Request $request |
| 60 |
*/ |
| 61 |
public function set_request( WP_REST_Request $request ){ |
| 62 |
$this->request = $request; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get the form config as an array |
| 67 |
* |
| 68 |
* @since 1.5.0 |
| 69 |
* |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public function toArray(){ |
| 73 |
return $this->get_form(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get the form config as an array |
| 78 |
* |
| 79 |
* @since 1.7.0 |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function get_form() |
| 84 |
{ |
| 85 |
return $this->form; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get a field config IF it should be returned in REST API response. |
| 90 |
* |
| 91 |
* @since 1.5.0 |
| 92 |
* |
| 93 |
* @param string $field_id Field ID |
| 94 |
* |
| 95 |
* @return array|null |
| 96 |
*/ |
| 97 |
public function get_field( $field_id ){ |
| 98 |
$this->maybe_set_fields(); |
| 99 |
if( $this->is_api_field( $field_id )){ |
| 100 |
return $this->is_api_field( $field_id ); |
| 101 |
} |
| 102 |
|
| 103 |
return null; |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Check if a field should be returned in REST API response. |
| 109 |
* |
| 110 |
* @since 1.5.0 |
| 111 |
* |
| 112 |
* @param string $field_id Field ID |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public function is_api_field( $field_id ){ |
| 117 |
$this->maybe_set_fields(); |
| 118 |
return isset( $this->fields[ $field_id ] ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get all fields that should be returned in REST API response. |
| 123 |
* |
| 124 |
* @since 1.5.0 |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function get_fields(){ |
| 129 |
$this->maybe_set_fields(); |
| 130 |
return $this->fields; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get all fields that should be returned in REST API response's entry list fields |
| 135 |
* |
| 136 |
* @since 1.5.0 |
| 137 |
* |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
public function get_entry_list_fields(){ |
| 141 |
$entry_list_fields = Qcformbuilder_Forms_Forms::entry_list_fields( $this->form, true ); |
| 142 |
foreach ( $entry_list_fields as $field_id => $field ){ |
| 143 |
if( ! isset( $this->fields[ $field_id ] ) ){ |
| 144 |
unset( $entry_list_fields[ $field_id ] ); |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
return $entry_list_fields; |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Set fields property |
| 155 |
* |
| 156 |
* @since 1.5.0 |
| 157 |
*/ |
| 158 |
protected function set_fields(){ |
| 159 |
$this->fields = Qcformbuilder_Forms_Forms::get_fields( $this->form, true, true ); |
| 160 |
if( ! empty( $this->fields ) ){ |
| 161 |
foreach ( $this->fields as $field_id => $field ){ |
| 162 |
|
| 163 |
/** |
| 164 |
* Prevent a field from being shown in API responses |
| 165 |
* |
| 166 |
* @since 1.5.0 |
| 167 |
* |
| 168 |
* @param bool $show If false, field is not returned. |
| 169 |
* @param string $field_id ID of field |
| 170 |
* @param array $field Field config |
| 171 |
* @param array $form Form config |
| 172 |
* @param WP_REST_Request $request Current REST API request |
| 173 |
*/ |
| 174 |
if( false == apply_filters( 'qcformbuilder_forms_api_show_field', true, $field_id, $field, $this->form, $this->request ) ){ |
| 175 |
unset( $this->fields[ $field_id ] ); |
| 176 |
unset( $this->form[ 'fields' ][ $field_id ] ); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Lazy-loader for fields property |
| 186 |
* |
| 187 |
* @since 1.5.0 |
| 188 |
*/ |
| 189 |
private function maybe_set_fields(){ |
| 190 |
if( empty( $this->fields ) ){ |
| 191 |
$this->set_fields(); |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* @inheritdoc |
| 198 |
*/ |
| 199 |
public function offsetSet($offset, $value) { |
| 200 |
if (is_null($offset)) { |
| 201 |
$this->form[] = $value; |
| 202 |
} else { |
| 203 |
$this->form[$offset] = $value; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @inheritdoc |
| 209 |
*/ |
| 210 |
public function offsetExists($offset) { |
| 211 |
return isset($this->form[$offset]); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* @inheritdoc |
| 216 |
*/ |
| 217 |
public function offsetUnset($offset) { |
| 218 |
unset($this->form[$offset]); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* @inheritdoc |
| 223 |
*/ |
| 224 |
public function offsetGet($offset) { |
| 225 |
return isset($this->form[$offset]) ? $this->form[$offset] : null; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Save form in database |
| 230 |
* |
| 231 |
* @since 1.7.0 |
| 232 |
* |
| 233 |
* @return $this |
| 234 |
*/ |
| 235 |
public function save_form(){ |
| 236 |
//Not using toArray() since it is different in subclass |
| 237 |
Qcformbuilder_Forms_Forms::save_form($this->form ); |
| 238 |
return $this; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* (re)Set form config |
| 244 |
* |
| 245 |
* Does not save. Use $this->save_form() |
| 246 |
* |
| 247 |
* @since 1.7.0 |
| 248 |
* |
| 249 |
* @param array $form Form configuration. |
| 250 |
* |
| 251 |
* @return $this |
| 252 |
*/ |
| 253 |
public function set_form(array $form ) |
| 254 |
{ |
| 255 |
$this->form = $form; |
| 256 |
return $this; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
} |