actions
5 years ago
admin
5 years ago
blocks
5 years ago
classes
5 years ago
compatibility
5 years ago
dev-mode
5 years ago
exceptions
5 years ago
form-messages
5 years ago
form-patterns
5 years ago
form-response
5 years ago
gateways
5 years ago
generators
5 years ago
integrations
5 years ago
presets
5 years ago
request
5 years ago
shortcodes
5 years ago
widgets
5 years ago
autoloader.php
5 years ago
file-upload.php
5 years ago
form-handler.php
5 years ago
form-manager.php
5 years ago
form-messages-builder.php
5 years ago
form-messages-manager.php
5 years ago
form-preset.php
5 years ago
live-form.php
5 years ago
plugin.php
5 years ago
post-type.php
5 years ago
request-handler.php
5 years ago
form-preset.php
517 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder; |
| 4 | |
| 5 | use Jet_Form_Builder\Classes\Tools; |
| 6 | |
| 7 | /** |
| 8 | * Captcha manager class |
| 9 | */ |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Define Forms_Preset class |
| 18 | */ |
| 19 | class Form_Preset { |
| 20 | |
| 21 | public static $instance; |
| 22 | |
| 23 | private $form_id = null; |
| 24 | private $data = null; |
| 25 | private $source = null; |
| 26 | private $defaults = array( |
| 27 | 'enabled' => false, |
| 28 | 'from' => 'post', |
| 29 | 'post_from' => 'current_post', |
| 30 | 'user_from' => 'current_user', |
| 31 | 'query_var' => '_post_id', |
| 32 | 'fields_map' => array(), |
| 33 | ); |
| 34 | |
| 35 | private function __construct() { |
| 36 | } |
| 37 | |
| 38 | public function set_form_id( $form_id ) { |
| 39 | $this->form_id = $form_id; |
| 40 | $this->set_data(); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Sanitize preset source |
| 46 | * |
| 47 | * @return [type] [description] |
| 48 | */ |
| 49 | public function sanitize_source() { |
| 50 | |
| 51 | if ( empty( $this->data['enabled'] ) ) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | $from = ! empty( $this->data['from'] ) ? $this->data['from'] : $this->defaults['from']; |
| 56 | $source = $this->get_source(); |
| 57 | |
| 58 | if ( ! $source ) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | if ( 'post' === $from ) { |
| 63 | |
| 64 | if ( ! is_user_logged_in() ) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if ( absint( $source->post_author ) !== get_current_user_id() && ! current_user_can( 'edit_others_posts' ) ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | } elseif ( 'user' === $from ) { |
| 74 | |
| 75 | if ( ! is_user_logged_in() ) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if ( get_current_user_id() !== $source->ID && ! current_user_can( 'edit_users' ) ) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | public function is_repeater_val( $value ) { |
| 90 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 91 | $value = array_values( $value ); |
| 92 | |
| 93 | return is_array( $value[0] ); |
| 94 | } else { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function get_key_from_map( $map, $repeater_key ) { |
| 100 | |
| 101 | foreach ( $map as $field => $data ) { |
| 102 | |
| 103 | $prop = ! empty( $data['prop'] ) ? $data['prop'] : 'post_title'; |
| 104 | |
| 105 | if ( 'post_meta' === $prop && ! empty( $data['key'] ) && $repeater_key == $data['key'] ) { |
| 106 | return $field; |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | return $repeater_key; |
| 112 | } |
| 113 | |
| 114 | public function parse_dynamic_preset( $value ) { |
| 115 | |
| 116 | $dynamic_preset = json_decode( $value, true ); |
| 117 | |
| 118 | if ( empty( $dynamic_preset ) || empty( $dynamic_preset['jet_preset'] ) ) { |
| 119 | return false; |
| 120 | } else { |
| 121 | return $dynamic_preset; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public function field_dynamic_preset( $args ) { |
| 126 | |
| 127 | if ( empty( $args['default'] ) ) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return $this->parse_dynamic_preset( $args['default'] ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns preset value |
| 136 | * |
| 137 | * @param string $field |
| 138 | * @param array $args |
| 139 | * @param bool|array $dynamic_preset |
| 140 | * |
| 141 | * @return array |
| 142 | */ |
| 143 | public function get_preset_value( $field = null, $args = array(), $dynamic_preset = false ) { |
| 144 | |
| 145 | $result = array( |
| 146 | 'rewrite' => false, |
| 147 | 'value' => null, |
| 148 | ); |
| 149 | |
| 150 | if ( ! $field ) { |
| 151 | return $result; |
| 152 | } |
| 153 | |
| 154 | if ( ! empty( $dynamic_preset ) ) { |
| 155 | $data = $dynamic_preset; |
| 156 | } else { |
| 157 | $data = $this->data; |
| 158 | } |
| 159 | |
| 160 | if ( empty( $data['enabled'] ) && ! $dynamic_preset ) { |
| 161 | return $result; |
| 162 | } |
| 163 | |
| 164 | $source = $this->get_source( $dynamic_preset ); |
| 165 | |
| 166 | $from = ! empty( $data['from'] ) ? $data['from'] : $this->defaults['from']; |
| 167 | $map = ! empty( $data['fields_map'] ) ? $data['fields_map'] : $this->defaults['fields_map']; |
| 168 | |
| 169 | if ( ! empty( $dynamic_preset ) ) { |
| 170 | $map = array( |
| 171 | $field => array( |
| 172 | 'prop' => ! empty( $data['current_field_prop'] ) ? $data['current_field_prop'] : '', |
| 173 | 'key' => ! empty( $data['current_field_key'] ) ? $data['current_field_key'] : '', |
| 174 | ), |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | if ( ! $source ) { |
| 179 | |
| 180 | if ( ! empty( $dynamic_preset ) ) { |
| 181 | $result['rewrite'] = true; |
| 182 | $result['value'] = ''; |
| 183 | } |
| 184 | |
| 185 | return $result; |
| 186 | } |
| 187 | |
| 188 | if ( empty( $map[ $field ] ) || ( empty( $map[ $field ]['prop'] ) && empty( $map[ $field ]['key'] ) ) ) { |
| 189 | |
| 190 | if ( ! empty( $dynamic_preset ) ) { |
| 191 | $result['rewrite'] = true; |
| 192 | $result['value'] = ''; |
| 193 | } |
| 194 | |
| 195 | return $result; |
| 196 | } |
| 197 | |
| 198 | $field_data = $map[ $field ]; |
| 199 | $value = null; |
| 200 | $array_allowed = in_array( $args['type'], array( 'checkboxes' ) ) || ! empty( $args['array_allowed'] ); |
| 201 | |
| 202 | if ( 'post' === $from ) { |
| 203 | |
| 204 | if ( absint( $source->post_author ) !== get_current_user_id() && ! current_user_can( 'edit_others_posts' ) ) { |
| 205 | return $result; |
| 206 | } |
| 207 | |
| 208 | $prop = ! empty( $field_data['prop'] ) ? $field_data['prop'] : 'post_title'; |
| 209 | |
| 210 | if ( 'post_meta' === $prop ) { |
| 211 | |
| 212 | if ( ! empty( $field_data['key'] ) ) { |
| 213 | |
| 214 | $value = get_post_meta( $source->ID, $field_data['key'], true ); |
| 215 | |
| 216 | if ( $this->is_repeater_val( $value ) ) { |
| 217 | |
| 218 | $prepared_value = array(); |
| 219 | |
| 220 | foreach ( $value as $index => $row ) { |
| 221 | |
| 222 | $prepared_row = array(); |
| 223 | |
| 224 | foreach ( $row as $item_key => $item_value ) { |
| 225 | |
| 226 | $item_key = $this->get_key_from_map( $map, $item_key ); |
| 227 | |
| 228 | $prepared_row[ $item_key ] = $item_value; |
| 229 | } |
| 230 | |
| 231 | $prepared_value[] = $prepared_row; |
| 232 | |
| 233 | } |
| 234 | $value = $prepared_value; |
| 235 | } else if ( function_exists( 'jet_engine' ) |
| 236 | && jet_engine()->relations |
| 237 | && jet_engine()->relations->is_relation_key( $field_data['key'] ) ) { |
| 238 | |
| 239 | $info = jet_engine()->relations->get_relation_info( $field_data['key'] ); |
| 240 | |
| 241 | if ( ! $info ) { |
| 242 | return $result; |
| 243 | } |
| 244 | |
| 245 | $args = array( |
| 246 | 'post_id' => $source->ID, |
| 247 | 'post_type_1' => $info['post_type_1'], |
| 248 | 'post_type_2' => $info['post_type_2'], |
| 249 | ); |
| 250 | |
| 251 | if ( $source->post_type === $info['post_type_1'] ) { |
| 252 | $args['from'] = $info['post_type_2']; |
| 253 | } else { |
| 254 | $args['from'] = $info['post_type_1']; |
| 255 | } |
| 256 | |
| 257 | $value = jet_engine()->relations->get_related_posts( $args ); |
| 258 | |
| 259 | } else { |
| 260 | $value = get_post_meta( $source->ID, $field_data['key'], true ); |
| 261 | } |
| 262 | |
| 263 | } else { |
| 264 | return $result; |
| 265 | } |
| 266 | } elseif ( 'post_terms' === $prop ) { |
| 267 | if ( ! empty( $field_data['key'] ) ) { |
| 268 | |
| 269 | $value = wp_get_post_terms( $source->ID, $field_data['key'] ); |
| 270 | |
| 271 | if ( empty( $value ) || is_wp_error( $value ) ) { |
| 272 | return $result; |
| 273 | } else { |
| 274 | if ( $array_allowed ) { |
| 275 | $value = array_map( function ( $term ) { |
| 276 | return strval( $term->term_id ); |
| 277 | }, $value ); |
| 278 | } else { |
| 279 | $value = $value[0]; |
| 280 | $value = $value->term_id; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | } else { |
| 285 | return $result; |
| 286 | } |
| 287 | } elseif ( 'post_thumb' === $prop ) { |
| 288 | $value = get_post_thumbnail_id( $source->ID ); |
| 289 | } else { |
| 290 | $value = isset( $source->$prop ) ? $source->$prop : null; |
| 291 | } |
| 292 | |
| 293 | } elseif ( 'user' === $from ) { |
| 294 | |
| 295 | if ( ! $source || is_wp_error( $source ) ) { |
| 296 | return $result; |
| 297 | } |
| 298 | |
| 299 | if ( ! is_user_logged_in() ) { |
| 300 | return $result; |
| 301 | } |
| 302 | |
| 303 | if ( get_current_user_id() !== $source->ID && ! current_user_can( 'edit_users' ) ) { |
| 304 | return $result; |
| 305 | } |
| 306 | |
| 307 | $prop = ! empty( $field_data['prop'] ) ? $field_data['prop'] : 'post_title'; |
| 308 | |
| 309 | if ( 'user_meta' === $prop ) { |
| 310 | if ( ! empty( $field_data['key'] ) ) { |
| 311 | $value = get_user_meta( $source->ID, $field_data['key'], true ); |
| 312 | } else { |
| 313 | return $result; |
| 314 | } |
| 315 | } else { |
| 316 | |
| 317 | // adjust props |
| 318 | switch ( $prop ) { |
| 319 | case 'email': |
| 320 | $prop = 'user_email'; |
| 321 | break; |
| 322 | |
| 323 | case 'login': |
| 324 | $prop = 'user_login'; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | if ( isset( $source->data->$prop ) ) { |
| 329 | $value = $source->data->$prop; |
| 330 | } elseif ( isset( $source->$prop ) ) { |
| 331 | $value = $source->$prop; |
| 332 | } else { |
| 333 | $value = null; |
| 334 | } |
| 335 | |
| 336 | } |
| 337 | |
| 338 | } elseif ( 'option_page' === $from ) { |
| 339 | |
| 340 | $key = ! empty( $field_data['key'] ) ? $field_data['key'] : false; |
| 341 | |
| 342 | if ( $key ) { |
| 343 | $value = jet_engine()->listings->data->get_option( $key ); |
| 344 | } |
| 345 | |
| 346 | } else { |
| 347 | |
| 348 | $key = ! empty( $field_data['key'] ) ? $field_data['key'] : false; |
| 349 | |
| 350 | if ( $key && is_array( $source ) ) { |
| 351 | $value = isset( $source[ $key ] ) ? $source[ $key ] : null; |
| 352 | } |
| 353 | |
| 354 | $value = apply_filters( 'jet-form-builder/preset-value/' . $from, $value, $field_data, $data ); |
| 355 | |
| 356 | } |
| 357 | |
| 358 | // Prepare value for date field |
| 359 | if ( 'date' === $args['type'] && Tools::is_valid_timestamp( $value ) ) { |
| 360 | $value = date_i18n( 'Y-m-d', $value ); |
| 361 | } |
| 362 | |
| 363 | if ( null === $value && ! empty( $dynamic_preset ) ) { |
| 364 | $value = ''; |
| 365 | } |
| 366 | |
| 367 | if ( null !== $value ) { |
| 368 | $result['rewrite'] = true; |
| 369 | $result['value'] = $value; |
| 370 | } |
| 371 | |
| 372 | return $result; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Returns field value |
| 377 | * |
| 378 | * @return [type] [description] |
| 379 | */ |
| 380 | public function get_field_value( $field = null, $args = array() ) { |
| 381 | |
| 382 | $result = array( |
| 383 | 'rewrite' => false, |
| 384 | 'value' => null, |
| 385 | ); |
| 386 | |
| 387 | if ( ! $field ) { |
| 388 | return $result; |
| 389 | } |
| 390 | $dynamic_preset = $this->field_dynamic_preset( $args ); |
| 391 | |
| 392 | return $this->get_preset_value( $field, $args, $dynamic_preset ); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Returns field source |
| 397 | * |
| 398 | * @return [type] [description] |
| 399 | */ |
| 400 | public function get_source( $dynamic_preset = false ) { |
| 401 | |
| 402 | if ( null !== $this->source && ! $dynamic_preset ) { |
| 403 | return $this->source; |
| 404 | } |
| 405 | |
| 406 | if ( ! empty( $dynamic_preset ) ) { |
| 407 | $data = $dynamic_preset; |
| 408 | } else { |
| 409 | $data = $this->data; |
| 410 | } |
| 411 | |
| 412 | $from = ! empty( $data['from'] ) ? $data['from'] : $this->defaults['from']; |
| 413 | $source = $this->source; |
| 414 | |
| 415 | switch ( $from ) { |
| 416 | case 'query_vars': |
| 417 | $source = Tools::maybe_recursive_sanitize( $_GET ); |
| 418 | break; |
| 419 | |
| 420 | case 'user': |
| 421 | $user_from = ! empty( $data['user_from'] ) ? $data['user_from'] : $this->defaults['user_from']; |
| 422 | |
| 423 | if ( 'current_user' === $user_from ) { |
| 424 | if ( is_user_logged_in() ) { |
| 425 | $source = wp_get_current_user(); |
| 426 | } |
| 427 | } else { |
| 428 | |
| 429 | $var = ! empty( $data['query_var'] ) ? $data['query_var'] : $this->defaults['query_var']; |
| 430 | $user_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? absint( $_REQUEST[ $var ] ) : false; |
| 431 | |
| 432 | $source = get_user_by( 'ID', $user_id ); |
| 433 | |
| 434 | } |
| 435 | |
| 436 | break; |
| 437 | |
| 438 | default: |
| 439 | |
| 440 | $post_from = ! empty( $data['post_from'] ) ? $data['post_from'] : $this->defaults['post_from']; |
| 441 | |
| 442 | if ( 'current_post' === $post_from ) { |
| 443 | $post_id = get_the_ID(); |
| 444 | } else { |
| 445 | $var = ! empty( $data['query_var'] ) ? $data['query_var'] : $this->defaults['query_var']; |
| 446 | $post_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? absint( $_REQUEST[ $var ] ) : false; |
| 447 | } |
| 448 | |
| 449 | if ( $post_id ) { |
| 450 | $source = get_post( $post_id ); |
| 451 | } |
| 452 | |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | if ( ! $dynamic_preset ) { |
| 457 | $this->source = $source; |
| 458 | |
| 459 | return $this->source; |
| 460 | } else { |
| 461 | return $source; |
| 462 | } |
| 463 | |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Try to get values from request if passed |
| 468 | * |
| 469 | * @param [type] $args [description] |
| 470 | * |
| 471 | * @return [type] [description] |
| 472 | */ |
| 473 | public function maybe_adjust_value( $args ) { |
| 474 | |
| 475 | $value = isset( $args['default'] ) ? $args['default'] : false; |
| 476 | $request_val = ! empty( $_REQUEST['values'] ) ? Tools::maybe_recursive_sanitize( $_REQUEST['values'] ) : array(); |
| 477 | |
| 478 | if ( ! empty( $request_val[ $args['name'] ] ) ) { |
| 479 | $value = $request_val[ $args['name'] ]; |
| 480 | } |
| 481 | |
| 482 | return $value; |
| 483 | |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Returns captcha settings for passed form ID |
| 488 | * |
| 489 | * @return [type] [description] |
| 490 | */ |
| 491 | public function set_data() { |
| 492 | $this->data = Plugin::instance()->post_type->get_preset( $this->form_id ); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Instance. |
| 497 | * |
| 498 | * Ensures only one instance of the plugin class is loaded or can be loaded. |
| 499 | * |
| 500 | * @return Form_Preset An instance of the class. |
| 501 | * @since 1.0.0 |
| 502 | * @access public |
| 503 | * @static |
| 504 | */ |
| 505 | public static function instance() { |
| 506 | |
| 507 | if ( is_null( self::$instance ) ) { |
| 508 | self::$instance = new self(); |
| 509 | } |
| 510 | |
| 511 | return self::$instance; |
| 512 | } |
| 513 | |
| 514 | } |
| 515 | |
| 516 | |
| 517 |