| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Classes\Arguments; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 7 |
use Jet_Form_Builder\Classes\Tools; |
| 8 |
use Jet_Form_Builder\Plugin; |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @property int form_id |
| 17 |
* @property string submit_type |
| 18 |
* @property string required_mark |
| 19 |
* @property string fields_layout |
| 20 |
* @property bool enable_progress |
| 21 |
* @property string fields_label_tag |
| 22 |
* @property string markup_type |
| 23 |
* @property string load_nonce |
| 24 |
* @property bool use_csrf |
| 25 |
* @property string validation_type |
| 26 |
* @property string clear |
| 27 |
* |
| 28 |
* Class Form_Arguments |
| 29 |
* @package Jet_Form_Builder\Classes\Arguments |
| 30 |
*/ |
| 31 |
class Form_Arguments implements Arrayable { |
| 32 |
|
| 33 |
const SETTER_PREFIX = 'set_'; |
| 34 |
|
| 35 |
public $props = array( |
| 36 |
'form_id' => '', |
| 37 |
'submit_type' => '', |
| 38 |
'required_mark' => '', |
| 39 |
'fields_layout' => '', |
| 40 |
'enable_progress' => null, |
| 41 |
'fields_label_tag' => '', |
| 42 |
'markup_type' => '', |
| 43 |
'load_nonce' => '', |
| 44 |
'use_csrf' => '', |
| 45 |
'validation_type' => '', |
| 46 |
'clear' => '', |
| 47 |
); |
| 48 |
|
| 49 |
public function __construct( $form_id = 0 ) { |
| 50 |
$this->set_form_id( (int) $form_id ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @param int|string $form_id |
| 55 |
* |
| 56 |
* @return Form_Arguments |
| 57 |
*/ |
| 58 |
public function set_form_id( $form_id ): Form_Arguments { |
| 59 |
$this->form_id = $form_id; |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* It turns out the inheritance of such an image |
| 66 |
* Incoming Form Attributes (from block/widget/shortcode) |
| 67 |
* (if not) -> Individual Form Arguments |
| 68 |
* (if not) -> Default PHP Values |
| 69 |
* |
| 70 |
* @param array $block_attributes |
| 71 |
* |
| 72 |
* @return $this |
| 73 |
*/ |
| 74 |
public function load( array $block_attributes = array() ): Form_Arguments { |
| 75 |
$jf_args = Plugin::instance()->post_type->get_args( $this->form_id ); |
| 76 |
|
| 77 |
$attributes_from_post_type = array_diff( $jf_args, $this->to_array() ); |
| 78 |
$form_block_or_widget = array_diff( $block_attributes, $this->to_array() ); |
| 79 |
|
| 80 |
/** |
| 81 |
* @var $render_attributes array |
| 82 |
*/ |
| 83 |
$render_attributes = array_merge( |
| 84 |
...apply_filters( |
| 85 |
'jet-form-builder/form-render/attributes', |
| 86 |
array( |
| 87 |
Default_Form_Arguments::arguments(), |
| 88 |
$attributes_from_post_type, |
| 89 |
$form_block_or_widget, |
| 90 |
) |
| 91 |
) |
| 92 |
); |
| 93 |
|
| 94 |
$this->set_from_array( $render_attributes ); |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param mixed $enable_progress |
| 101 |
*/ |
| 102 |
public function set_enable_progress( $enable_progress ) { |
| 103 |
$this->enable_progress = (bool) $enable_progress; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @param null $clear |
| 108 |
*/ |
| 109 |
public function set_clear( $clear ) { |
| 110 |
$this->clear = (bool) $clear; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param null $use_csrf |
| 115 |
*/ |
| 116 |
public function set_use_csrf( $use_csrf ) { |
| 117 |
$this->use_csrf = (bool) $use_csrf; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @param mixed $fields_label_tag |
| 122 |
*/ |
| 123 |
public function set_fields_label_tag( $fields_label_tag ) { |
| 124 |
$fields_label_tag = strtolower( $fields_label_tag ); |
| 125 |
|
| 126 |
if ( ! in_array( $fields_label_tag, array( 'label', 'div' ), true ) ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$this->fields_label_tag = $fields_label_tag; |
| 131 |
} |
| 132 |
|
| 133 |
public function set_markup_type( $markup_type ) { |
| 134 |
$markup_type = strtolower( $markup_type ); |
| 135 |
if ( ! in_array( $markup_type, array( 'div', 'fieldset' ), true ) ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
$this->markup_type = $markup_type; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @param mixed $fields_layout |
| 143 |
*/ |
| 144 |
public function set_fields_layout( $fields_layout ) { |
| 145 |
$fields_layout = strtolower( $fields_layout ); |
| 146 |
|
| 147 |
if ( ! in_array( $fields_layout, array( 'column', 'row' ), true ) ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$this->fields_layout = $fields_layout; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param mixed $load_nonce |
| 156 |
*/ |
| 157 |
public function set_load_nonce( $load_nonce ) { |
| 158 |
$load_nonce = strtolower( $load_nonce ); |
| 159 |
|
| 160 |
if ( ! in_array( $load_nonce, array( 'render', 'hide' ), true ) ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
$this->load_nonce = $load_nonce; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @param mixed $required_mark |
| 169 |
*/ |
| 170 |
public function set_required_mark( $required_mark ) { |
| 171 |
$this->required_mark = $required_mark; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @param mixed $submit_type |
| 176 |
*/ |
| 177 |
public function set_submit_type( $submit_type ) { |
| 178 |
$submit_type = strtolower( $submit_type ); |
| 179 |
|
| 180 |
if ( ! in_array( $submit_type, array( 'reload', 'ajax' ), true ) ) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$this->submit_type = $submit_type; |
| 185 |
} |
| 186 |
|
| 187 |
public function to_array(): array { |
| 188 |
return array( |
| 189 |
'form_id' => $this->form_id, |
| 190 |
'submit_type' => $this->submit_type, |
| 191 |
'required_mark' => $this->required_mark, |
| 192 |
'fields_layout' => $this->fields_layout, |
| 193 |
'fields_label_tag' => $this->fields_label_tag, |
| 194 |
'markup_type' => $this->markup_type, |
| 195 |
'enable_progress' => $this->enable_progress, |
| 196 |
'clear' => $this->clear, |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
public function set_from_array( array $attributes ) { |
| 201 |
foreach ( $attributes as $attr => $value ) { |
| 202 |
if ( ! array_key_exists( $attr, $this->props ) ) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
$setter = array( $this, self::SETTER_PREFIX . $attr ); |
| 206 |
|
| 207 |
if ( is_callable( $setter ) ) { |
| 208 |
call_user_func( $setter, $value ); |
| 209 |
|
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
$this->props[ $attr ] = $value; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @param $property |
| 219 |
* @param false $default |
| 220 |
* |
| 221 |
* @return mixed |
| 222 |
*/ |
| 223 |
public function argument( $property, $if_empty = '' ) { |
| 224 |
if ( ! array_key_exists( $property, $this->props ) ) { |
| 225 |
return $if_empty; |
| 226 |
} |
| 227 |
$value = $this->props[ $property ]; |
| 228 |
|
| 229 |
return ( is_null( $value ) || '' === $value ) ? $if_empty : $value; |
| 230 |
} |
| 231 |
|
| 232 |
public function is_use_nonce(): bool { |
| 233 |
return 'render' === $this->load_nonce; |
| 234 |
} |
| 235 |
|
| 236 |
public function is_use_csrf() { |
| 237 |
return $this->use_csrf; |
| 238 |
} |
| 239 |
|
| 240 |
public static function arguments(): array { |
| 241 |
return ( new static() )->to_array(); |
| 242 |
} |
| 243 |
|
| 244 |
public static function get_options( $for_elementor = false ): array { |
| 245 |
$submit_type = array( |
| 246 |
'' => __( 'Default', 'jet-form-builder' ), |
| 247 |
'reload' => __( 'Page Reload', 'jet-form-builder' ), |
| 248 |
'ajax' => __( 'AJAX', 'jet-form-builder' ), |
| 249 |
); |
| 250 |
$fields_layout = array( |
| 251 |
'' => __( 'Default', 'jet-form-builder' ), |
| 252 |
'column' => __( 'Column', 'jet-form-builder' ), |
| 253 |
'row' => __( 'Row', 'jet-form-builder' ), |
| 254 |
); |
| 255 |
|
| 256 |
$label_tag = array( |
| 257 |
'div' => __( 'div', 'jet-form-builder' ), |
| 258 |
'label' => __( 'label', 'jet-form-builder' ), |
| 259 |
); |
| 260 |
|
| 261 |
$markup_type = array( |
| 262 |
'div' => __( 'div', 'jet-form-builder' ), |
| 263 |
'fieldset' => __( 'fieldset', 'jet-form-builder' ), |
| 264 |
); |
| 265 |
|
| 266 |
$load_nonce = array( |
| 267 |
'hide' => __( 'Disable', 'jet-form-builder' ), |
| 268 |
'render' => __( 'WordPress Nonce', 'jet-form-builder' ), |
| 269 |
'csrf' => __( 'JetFormBuilder Token', 'jet-form-builder' ), |
| 270 |
); |
| 271 |
|
| 272 |
if ( ! $for_elementor ) { |
| 273 |
$submit_type = Tools::prepare_list_for_js( $submit_type ); |
| 274 |
$fields_layout = Tools::prepare_list_for_js( $fields_layout ); |
| 275 |
$label_tag = Tools::prepare_list_for_js( $label_tag ); |
| 276 |
$markup_type = Tools::prepare_list_for_js( $markup_type ); |
| 277 |
$load_nonce = Tools::prepare_list_for_js( $load_nonce ); |
| 278 |
} |
| 279 |
|
| 280 |
return array( |
| 281 |
'submit_type' => $submit_type, |
| 282 |
'fields_layout' => $fields_layout, |
| 283 |
'fields_label_tag' => $label_tag, |
| 284 |
'markup_type' => $markup_type, |
| 285 |
'load_nonce' => $load_nonce, |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* @return string |
| 291 |
*/ |
| 292 |
public function get_submit_type(): string { |
| 293 |
// phpcs:ignore Universal.Operators.DisallowShortTernary.Found |
| 294 |
return $this->submit_type ?: 'reload'; |
| 295 |
} |
| 296 |
|
| 297 |
public function __get( $name ) { |
| 298 |
if ( ! array_key_exists( $name, $this->props ) ) { |
| 299 |
return null; |
| 300 |
} |
| 301 |
|
| 302 |
return $this->props[ $name ]; |
| 303 |
} |
| 304 |
|
| 305 |
public function __set( $name, $value ) { |
| 306 |
if ( ! array_key_exists( $name, $this->props ) ) { |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
$this->props[ $name ] = $value; |
| 311 |
} |
| 312 |
|
| 313 |
} |
| 314 |
|