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