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