| @@ -9,14 +9,8 @@ | ||
| 9 | 9 | * @var bool true if importing an XML from API, false if importing an XML file manually. |
| 10 | 10 | */ |
| 11 | 11 | private static $installing_template = false; |
| 12 | 12 | |
| 13 | - /** | |
| 14 | - * @param array|string $opt | |
| 15 | - * @param string $padding | |
| 16 | - * | |
| 17 | - * @return void | |
| 18 | - */ | |
| 19 | 13 | public static function get_xml_values( $opt, $padding ) { |
| 20 | 14 | if ( is_array( $opt ) ) { |
| 21 | 15 | foreach ( $opt as $ok => $ov ) { |
| 22 | 16 | echo "\n" . esc_html( $padding ); |
| @@ -22,9 +16,8 @@ | ||
| 22 | 16 | echo "\n" . esc_html( $padding ); |
| 23 | 17 | $tag = ( is_numeric( $ok ) ? 'key:' : '' ) . $ok; |
| 24 | 18 | echo '<' . esc_html( $tag ) . '>'; |
| 25 | 19 | self::get_xml_values( $ov, $padding . ' ' ); |
| 26 | - | |
| 27 | 20 | if ( is_array( $ov ) ) { |
| 28 | 21 | echo "\n" . esc_html( $padding ); |
| 29 | 22 | } |
| 30 | 23 | echo '</' . esc_html( $tag ) . '>'; |
| @@ -35,9 +28,8 @@ | ||
| 35 | 28 | } |
| 36 | 29 | |
| 37 | 30 | /** |
| 38 | 31 | * @param string $file |
| 39 | - * | |
| 40 | 32 | * @return array|WP_Error The items imported |
| 41 | 33 | */ |
| 42 | 34 | public static function import_xml( $file ) { |
| 43 | 35 | if ( ! defined( 'WP_IMPORTING' ) ) { |
| @@ -46,10 +38,10 @@ | ||
| 46 | 38 | |
| 47 | 39 | if ( ! class_exists( 'DOMDocument' ) ) { |
| 48 | 40 | $error_message = sprintf( |
| 49 | 41 | /* translators: 1: Documentation link */ |
| 50 | - __( 'In order to install XML, your server must have DOMDocument installed. Follow our documentation on %1$s to ensure DOMDocument is properly set up and XML support is enabled.', 'formidable' ), // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 51 | - '<a href="https://formidableforms.com/knowledgebase/import-forms-entries-and-views/#kb-your-server-does-not-have-xml-enabled" target="_blank">Importing Forms, Entries, and Views</a>' // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 42 | + __( 'In order to install XML, your server must have DOMDocument installed. Follow our documentation on %1$s to ensure DOMDocument is properly set up and XML support is enabled.', 'formidable' ), | |
| 43 | + '<a href="https://formidableforms.com/knowledgebase/import-forms-entries-and-views/#kb-your-server-does-not-have-xml-enabled" target="_blank">Importing Forms, Entries, and Views</a>' | |
| 52 | 44 | ); |
| 53 | 45 | |
| 54 | 46 | return new WP_Error( 'SimpleXML_parse_error', $error_message, libxml_get_errors() ); |
| 55 | 47 | } |
| @@ -61,9 +53,8 @@ | ||
| 61 | 53 | |
| 62 | 54 | // LIBXML_COMPACT activates small nodes allocation optimization. |
| 63 | 55 | // Use LIBXML_PARSEHUGE to avoid "parser error : internal error: Huge input lookup" for large (300MB) files. |
| 64 | 56 | $success = $dom->loadXML( $xml_string, LIBXML_COMPACT | LIBXML_PARSEHUGE ); |
| 65 | - | |
| 66 | 57 | if ( ! $success ) { |
| 67 | 58 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
| 68 | 59 | } |
| 69 | 60 | |
| @@ -73,9 +64,9 @@ | ||
| 73 | 64 | |
| 74 | 65 | $xml = simplexml_import_dom( $dom ); |
| 75 | 66 | unset( $dom ); |
| 76 | 67 | |
| 77 | - // Halt if loading produces an error | |
| 68 | + // halt if loading produces an error | |
| 78 | 69 | if ( ! $xml ) { |
| 79 | 70 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
| 80 | 71 | } |
| 81 | 72 | |
| @@ -85,13 +76,12 @@ | ||
| 85 | 76 | /** |
| 86 | 77 | * @since 6.2.3 |
| 87 | 78 | * |
| 88 | 79 | * @param string $xml_string |
| 89 | - * | |
| 90 | 80 | * @return void |
| 91 | 81 | */ |
| 92 | 82 | private static function maybe_fix_xml( &$xml_string ) { |
| 93 | - if ( ! str_starts_with( $xml_string, '<?xml' ) ) { | |
| 83 | + if ( '<?xml' !== substr( $xml_string, 0, 5 ) ) { | |
| 94 | 84 | // Some XML files have may have unexpected characters at the start. |
| 95 | 85 | $xml_string = substr( $xml_string, strpos( $xml_string, '<?xml' ) ); |
| 96 | 86 | } |
| 97 | 87 | |
| @@ -98,20 +88,17 @@ | ||
| 98 | 88 | // The Equity theme adds a <meta name="generator" content="Equity 1.7.13" /> tag using the "the_generator" filter. |
| 99 | 89 | // Strip that out as it breaks the XML import. |
| 100 | 90 | $channel_start_position = strpos( $xml_string, '<channel>' ); |
| 101 | 91 | $content_before_channel_tag = substr( $xml_string, 0, $channel_start_position ); |
| 102 | - | |
| 103 | - if ( str_starts_with( $content_before_channel_tag, '<meta name="generator" ' ) ) { | |
| 104 | - return; | |
| 92 | + if ( 0 !== strpos( $content_before_channel_tag, '<meta name="generator" ' ) ) { | |
| 93 | + $content_before_channel_tag = preg_replace( | |
| 94 | + '/<meta\s+[^>]*name="generator"[^>]*\/>/i', | |
| 95 | + '', | |
| 96 | + $content_before_channel_tag, | |
| 97 | + 1 | |
| 98 | + ); | |
| 99 | + $xml_string = $content_before_channel_tag . substr( $xml_string, $channel_start_position ); | |
| 105 | 100 | } |
| 106 | - | |
| 107 | - $content_before_channel_tag = preg_replace( | |
| 108 | - '/<meta\s+[^>]*name="generator"[^>]*\/>/i', | |
| 109 | - '', | |
| 110 | - $content_before_channel_tag, | |
| 111 | - 1 | |
| 112 | - ); | |
| 113 | - $xml_string = $content_before_channel_tag . substr( $xml_string, $channel_start_position ); | |
| 114 | 101 | } |
| 115 | 102 | |
| 116 | 103 | /** |
| 117 | 104 | * Add terms, forms (form and field ids), posts (post ids), and entries to db, in that order |
| @@ -119,9 +106,8 @@ | ||
| 119 | 106 | * @since 3.06 |
| 120 | 107 | * |
| 121 | 108 | * @param object $xml |
| 122 | 109 | * @param bool $installing_template |
| 123 | - * | |
| 124 | 110 | * @return array The number of items imported |
| 125 | 111 | */ |
| 126 | 112 | public static function import_xml_now( $xml, $installing_template = false ) { |
| 127 | 113 | if ( ! defined( 'WP_IMPORTING' ) ) { |
| @@ -132,15 +118,13 @@ | ||
| 132 | 118 | $imported = self::pre_import_data(); |
| 133 | 119 | |
| 134 | 120 | foreach ( array( 'term', 'form', 'view' ) as $item_type ) { |
| 135 | 121 | // Grab cats, tags, and terms, or forms or posts. |
| 136 | - if ( ! isset( $xml->{$item_type} ) ) { | |
| 137 | - continue; | |
| 122 | + if ( isset( $xml->{$item_type} ) ) { | |
| 123 | + $function_name = 'import_xml_' . $item_type . 's'; | |
| 124 | + $imported = self::$function_name( $xml->{$item_type}, $imported ); | |
| 125 | + unset( $function_name, $xml->{$item_type} ); | |
| 138 | 126 | } |
| 139 | - | |
| 140 | - $function_name = 'import_xml_' . $item_type . 's'; | |
| 141 | - $imported = self::$function_name( $xml->{$item_type}, $imported ); | |
| 142 | - unset( $function_name, $xml->{$item_type} ); | |
| 143 | 127 | } |
| 144 | 128 | |
| 145 | 129 | $imported = apply_filters( 'frm_importing_xml', $imported, $xml ); |
| 146 | 130 | |
| @@ -155,9 +139,8 @@ | ||
| 155 | 139 | } |
| 156 | 140 | |
| 157 | 141 | /** |
| 158 | 142 | * @since 3.06 |
| 159 | - * | |
| 160 | 143 | * @return array |
| 161 | 144 | */ |
| 162 | 145 | private static function pre_import_data() { |
| 163 | 146 | $defaults = array( |
| @@ -180,9 +163,8 @@ | ||
| 180 | 163 | |
| 181 | 164 | /** |
| 182 | 165 | * @param SimpleXMLElement $terms |
| 183 | 166 | * @param array $imported |
| 184 | - * | |
| 185 | 167 | * @return array |
| 186 | 168 | */ |
| 187 | 169 | public static function import_xml_terms( $terms, $imported ) { |
| 188 | 170 | foreach ( $terms as $t ) { |
| @@ -197,9 +179,9 @@ | ||
| 197 | 179 | (string) $t->term_taxonomy, |
| 198 | 180 | array( |
| 199 | 181 | 'slug' => (string) $t->term_slug, |
| 200 | 182 | 'description' => (string) $t->term_description, |
| 201 | - 'parent' => $parent ? $parent : 0, | |
| 183 | + 'parent' => empty( $parent ) ? 0 : $parent, | |
| 202 | 184 | ) |
| 203 | 185 | ); |
| 204 | 186 | |
| 205 | 187 | if ( $term && is_array( $term ) ) { |
| @@ -216,17 +198,19 @@ | ||
| 216 | 198 | /** |
| 217 | 199 | * @since 2.0.8 |
| 218 | 200 | * |
| 219 | 201 | * @param object $t |
| 220 | - * | |
| 221 | 202 | * @return int|string |
| 222 | 203 | */ |
| 223 | 204 | private static function get_term_parent_id( $t ) { |
| 224 | 205 | $parent = (string) $t->term_parent; |
| 225 | - | |
| 226 | - if ( $parent ) { | |
| 206 | + if ( ! empty( $parent ) ) { | |
| 227 | 207 | $parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy ); |
| 228 | - $parent = $parent ? $parent['term_id'] : 0; | |
| 208 | + if ( $parent ) { | |
| 209 | + $parent = $parent['term_id']; | |
| 210 | + } else { | |
| 211 | + $parent = 0; | |
| 212 | + } | |
| 229 | 213 | } |
| 230 | 214 | |
| 231 | 215 | return $parent; |
| 232 | 216 | } |
| @@ -233,9 +217,8 @@ | ||
| 233 | 217 | |
| 234 | 218 | /** |
| 235 | 219 | * @param SimpleXMLElement $forms |
| 236 | 220 | * @param array $imported |
| 237 | - * | |
| 238 | 221 | * @return array |
| 239 | 222 | */ |
| 240 | 223 | public static function import_xml_forms( $forms, $imported ) { |
| 241 | 224 | $child_forms = array(); |
| @@ -247,13 +230,13 @@ | ||
| 247 | 230 | $form = self::fill_form( $item ); |
| 248 | 231 | |
| 249 | 232 | self::update_custom_style_setting_on_import( $form ); |
| 250 | 233 | |
| 251 | - $this_form = self::maybe_get_form( $form ); | |
| 234 | + $this_form = self::maybe_get_form( $form ); | |
| 235 | + | |
| 252 | 236 | $old_id = false; |
| 253 | 237 | $form_fields = false; |
| 254 | - | |
| 255 | - if ( $this_form ) { | |
| 238 | + if ( ! empty( $this_form ) ) { | |
| 256 | 239 | $form_id = $this_form->id; |
| 257 | 240 | $old_id = $this_form->id; |
| 258 | 241 | self::update_form( $this_form, $form, $imported ); |
| 259 | 242 | |
| @@ -259,9 +242,8 @@ | ||
| 259 | 242 | |
| 260 | 243 | $form_fields = self::get_form_fields( $form_id ); |
| 261 | 244 | } else { |
| 262 | 245 | $form_id = FrmForm::create( $form ); |
| 263 | - | |
| 264 | 246 | if ( $form_id ) { |
| 265 | 247 | if ( empty( $form['parent_form_id'] ) ) { |
| 266 | 248 | // Don't include the repeater form in the imported count. |
| 267 | 249 | ++$imported['imported']['forms']; |
| @@ -297,13 +279,8 @@ | ||
| 297 | 279 | |
| 298 | 280 | return $imported; |
| 299 | 281 | } |
| 300 | 282 | |
| 301 | - /** | |
| 302 | - * @param object $item | |
| 303 | - * | |
| 304 | - * @return array | |
| 305 | - */ | |
| 306 | 283 | private static function fill_form( $item ) { |
| 307 | 284 | $form = array( |
| 308 | 285 | 'id' => (int) $item->id, |
| 309 | 286 | 'form_key' => (string) $item->form_key, |
| @@ -331,27 +308,21 @@ | ||
| 331 | 308 | |
| 332 | 309 | return $form; |
| 333 | 310 | } |
| 334 | 311 | |
| 335 | - /** | |
| 336 | - * @param array $form | |
| 337 | - * | |
| 338 | - * @return false|object | |
| 339 | - */ | |
| 340 | 312 | private static function maybe_get_form( $form ) { |
| 341 | - // If template, allow to edit if form keys match, otherwise, creation date must also match | |
| 313 | + // if template, allow to edit if form keys match, otherwise, creation date must also match | |
| 342 | 314 | $edit_query = array( |
| 343 | 315 | 'form_key' => $form['form_key'], |
| 344 | 316 | 'is_template' => $form['is_template'], |
| 345 | 317 | ); |
| 346 | - | |
| 347 | 318 | if ( ! $form['is_template'] ) { |
| 348 | 319 | $edit_query['created_at'] = $form['created_at']; |
| 349 | 320 | } |
| 350 | 321 | |
| 351 | 322 | $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form ); |
| 352 | - $form = FrmForm::getAll( $edit_query, '', 1 ); | |
| 353 | 323 | |
| 324 | + $form = FrmForm::getAll( $edit_query, '', 1 ); | |
| 354 | 325 | if ( is_object( $form ) && $form->status === 'trash' ) { |
| 355 | 326 | FrmForm::destroy( $form->id ); |
| 356 | 327 | return false; |
| 357 | 328 | } |
| @@ -358,19 +329,11 @@ | ||
| 358 | 329 | |
| 359 | 330 | return $form; |
| 360 | 331 | } |
| 361 | 332 | |
| 362 | - /** | |
| 363 | - * @param object $this_form | |
| 364 | - * @param array $form | |
| 365 | - * @param array $imported | |
| 366 | - * | |
| 367 | - * @return void | |
| 368 | - */ | |
| 369 | 333 | private static function update_form( $this_form, $form, &$imported ) { |
| 370 | 334 | $form_id = $this_form->id; |
| 371 | 335 | FrmForm::update( $form_id, $form ); |
| 372 | - | |
| 373 | 336 | if ( empty( $form['parent_form_id'] ) ) { |
| 374 | 337 | // Don't include the repeater form in the updated count. |
| 375 | 338 | ++$imported['updated']['forms']; |
| 376 | 339 | } |
| @@ -378,43 +341,32 @@ | ||
| 378 | 341 | // Keep track of whether this specific form was updated or not |
| 379 | 342 | $imported['form_status'][ $form_id ] = 'updated'; |
| 380 | 343 | } |
| 381 | 344 | |
| 382 | - /** | |
| 383 | - * @param int|string $form_id | |
| 384 | - * | |
| 385 | - * @return array | |
| 386 | - */ | |
| 387 | 345 | private static function get_form_fields( $form_id ) { |
| 388 | 346 | $form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' ); |
| 389 | 347 | $old_fields = array(); |
| 390 | - | |
| 391 | 348 | foreach ( $form_fields as $f ) { |
| 392 | 349 | $old_fields[ $f->id ] = $f; |
| 393 | 350 | $old_fields[ $f->field_key ] = $f->id; |
| 394 | 351 | unset( $f ); |
| 395 | 352 | } |
| 353 | + $form_fields = $old_fields; | |
| 396 | 354 | |
| 397 | - return $old_fields; | |
| 355 | + return $form_fields; | |
| 398 | 356 | } |
| 399 | 357 | |
| 400 | 358 | /** |
| 401 | 359 | * Delete any fields attached to this form that were not included in the template |
| 402 | - * | |
| 403 | - * @param array $form_fields | |
| 404 | - * | |
| 405 | - * @return void | |
| 406 | 360 | */ |
| 407 | 361 | private static function delete_removed_fields( $form_fields ) { |
| 408 | - if ( ! $form_fields ) { | |
| 409 | - return; | |
| 410 | - } | |
| 411 | - | |
| 412 | - foreach ( $form_fields as $field ) { | |
| 413 | - if ( is_object( $field ) ) { | |
| 414 | - FrmField::destroy( $field->id ); | |
| 362 | + if ( ! empty( $form_fields ) ) { | |
| 363 | + foreach ( $form_fields as $field ) { | |
| 364 | + if ( is_object( $field ) ) { | |
| 365 | + FrmField::destroy( $field->id ); | |
| 366 | + } | |
| 367 | + unset( $field ); | |
| 415 | 368 | } |
| 416 | - unset( $field ); | |
| 417 | 369 | } |
| 418 | 370 | } |
| 419 | 371 | |
| 420 | 372 | /** |
| @@ -466,16 +418,14 @@ | ||
| 466 | 418 | * @param array $child_forms |
| 467 | 419 | */ |
| 468 | 420 | private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) { |
| 469 | 421 | foreach ( $child_forms as $child_form_id => $old_parent_form_id ) { |
| 470 | - if ( ! isset( $imported_forms[ $old_parent_form_id ] ) || (int) $imported_forms[ $old_parent_form_id ] === (int) $old_parent_form_id ) { | |
| 471 | - continue; | |
| 422 | + if ( isset( $imported_forms[ $old_parent_form_id ] ) && (int) $imported_forms[ $old_parent_form_id ] !== (int) $old_parent_form_id ) { | |
| 423 | + // Update all children with this old parent_form_id | |
| 424 | + $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ]; | |
| 425 | + FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) ); | |
| 426 | + do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id ); | |
| 472 | 427 | } |
| 473 | - | |
| 474 | - // Update all children with this old parent_form_id | |
| 475 | - $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ]; | |
| 476 | - FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) ); | |
| 477 | - do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id ); | |
| 478 | 428 | } |
| 479 | 429 | } |
| 480 | 430 | |
| 481 | 431 | /** |
| @@ -483,14 +433,8 @@ | ||
| 483 | 433 | * |
| 484 | 434 | * @since 2.0.13 |
| 485 | 435 | * |
| 486 | 436 | * TODO: Cut down on params |
| 487 | - * | |
| 488 | - * @param SimpleXMLElement $xml_fields | |
| 489 | - * @param int|string $form_id | |
| 490 | - * @param object $this_form | |
| 491 | - * @param array $form_fields | |
| 492 | - * @param array $imported | |
| 493 | 437 | */ |
| 494 | 438 | private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$form_fields, &$imported ) { |
| 495 | 439 | $in_section = 0; |
| 496 | 440 | $keys_by_original_field_id = array(); |
| @@ -504,10 +448,10 @@ | ||
| 504 | 448 | self::maybe_update_form_select( $f, $imported ); |
| 505 | 449 | self::maybe_update_get_values_form_setting( $imported, $f ); |
| 506 | 450 | self::migrate_placeholders( $f ); |
| 507 | 451 | |
| 508 | - if ( $this_form ) { | |
| 509 | - // Check for field to edit by field id | |
| 452 | + if ( ! empty( $this_form ) ) { | |
| 453 | + // check for field to edit by field id | |
| 510 | 454 | if ( isset( $form_fields[ $f['id'] ] ) ) { |
| 511 | 455 | FrmField::update( $f['id'], $f ); |
| 512 | 456 | ++$imported['updated']['fields']; |
| 513 | 457 | |
| @@ -518,11 +462,12 @@ | ||
| 518 | 462 | unset( $form_fields[ $f['field_key'] ] ); |
| 519 | 463 | } |
| 520 | 464 | } elseif ( isset( $form_fields[ $f['field_key'] ] ) ) { |
| 521 | 465 | $keys_by_original_field_id[ $f['id'] ] = $f['field_key']; |
| 522 | - $old_field_id = $f['id']; | |
| 523 | 466 | |
| 524 | - // Check for field to edit by field key | |
| 467 | + $old_field_id = $f['id']; | |
| 468 | + | |
| 469 | + // check for field to edit by field key | |
| 525 | 470 | unset( $f['id'] ); |
| 526 | 471 | |
| 527 | 472 | FrmField::update( $form_fields[ $f['field_key'] ], $f ); |
| 528 | 473 | ++$imported['updated']['fields']; |
| @@ -538,8 +483,9 @@ | ||
| 538 | 483 | // If no matching field id or key in this form, create the field. |
| 539 | 484 | self::create_imported_field( $f, $imported ); |
| 540 | 485 | }//end if |
| 541 | 486 | } else { |
| 487 | + | |
| 542 | 488 | self::create_imported_field( $f, $imported ); |
| 543 | 489 | }//end if |
| 544 | 490 | }//end foreach |
| 545 | 491 | |
| @@ -551,9 +497,8 @@ | ||
| 551 | 497 | /** |
| 552 | 498 | * @since 6.8.4 |
| 553 | 499 | * |
| 554 | 500 | * @param array $field_array |
| 555 | - * | |
| 556 | 501 | * @return array |
| 557 | 502 | */ |
| 558 | 503 | private static function update_field_options_with_defaults( $field_array ) { |
| 559 | 504 | $defaults = self::default_field_options( $field_array['type'] ); |
| @@ -586,14 +531,8 @@ | ||
| 586 | 531 | */ |
| 587 | 532 | do_action( 'frm_after_existing_field_is_imported', $field_array, $form_fields[ $field_array['field_key'] ], $old_field_id ); |
| 588 | 533 | } |
| 589 | 534 | |
| 590 | - /** | |
| 591 | - * @param object $field | |
| 592 | - * @param int|string $form_id | |
| 593 | - * | |
| 594 | - * @return array | |
| 595 | - */ | |
| 596 | 535 | private static function fill_field( $field, $form_id ) { |
| 597 | 536 | return array( |
| 598 | 537 | 'id' => (int) $field->id, |
| 599 | 538 | 'field_key' => (string) $field->field_key, |
| @@ -610,12 +549,8 @@ | ||
| 610 | 549 | } |
| 611 | 550 | |
| 612 | 551 | /** |
| 613 | 552 | * @since 4.06 |
| 614 | - * | |
| 615 | - * @param array $f | |
| 616 | - * | |
| 617 | - * @return void | |
| 618 | 553 | */ |
| 619 | 554 | private static function set_default_value( &$f ) { |
| 620 | 555 | $has_default = array( |
| 621 | 556 | 'text', |
| @@ -629,17 +564,15 @@ | ||
| 629 | 564 | 'password', |
| 630 | 565 | 'tag', |
| 631 | 566 | ); |
| 632 | 567 | |
| 633 | - if ( ! is_array( $f['default_value'] ) || ! in_array( $f['type'], $has_default, true ) ) { | |
| 634 | - return; | |
| 568 | + if ( is_array( $f['default_value'] ) && in_array( $f['type'], $has_default, true ) ) { | |
| 569 | + if ( count( $f['default_value'] ) === 1 ) { | |
| 570 | + $f['default_value'] = '[' . reset( $f['default_value'] ) . ']'; | |
| 571 | + } else { | |
| 572 | + $f['default_value'] = reset( $f['default_value'] ); | |
| 573 | + } | |
| 635 | 574 | } |
| 636 | - | |
| 637 | - if ( count( $f['default_value'] ) === 1 ) { | |
| 638 | - $f['default_value'] = '[' . reset( $f['default_value'] ) . ']'; | |
| 639 | - } else { | |
| 640 | - $f['default_value'] = reset( $f['default_value'] ); | |
| 641 | - } | |
| 642 | 575 | } |
| 643 | 576 | |
| 644 | 577 | /** |
| 645 | 578 | * Make sure the required indicator is set. |
| @@ -644,12 +577,8 @@ | ||
| 644 | 577 | /** |
| 645 | 578 | * Make sure the required indicator is set. |
| 646 | 579 | * |
| 647 | 580 | * @since 4.05 |
| 648 | - * | |
| 649 | - * @param array $f | |
| 650 | - * | |
| 651 | - * @return void | |
| 652 | 581 | */ |
| 653 | 582 | private static function maybe_add_required( &$f ) { |
| 654 | 583 | if ( $f['required'] && ! isset( $f['field_options']['required_indicator'] ) ) { |
| 655 | 584 | $f['field_options']['required_indicator'] = '*'; |
| @@ -659,17 +588,14 @@ | ||
| 659 | 588 | /** |
| 660 | 589 | * Update the current in_section value at the beginning of the field loop |
| 661 | 590 | * |
| 662 | 591 | * @since 2.0.25 |
| 663 | - * | |
| 664 | 592 | * @param int $in_section |
| 665 | 593 | * @param array $f |
| 666 | - * | |
| 667 | - * @return void | |
| 668 | 594 | */ |
| 669 | 595 | private static function maybe_update_in_section_variable( &$in_section, &$f ) { |
| 670 | 596 | // If we're at the end of a section, switch $in_section is 0 |
| 671 | - if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ), true ) ) { | |
| 597 | + if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) { | |
| 672 | 598 | $in_section = 0; |
| 673 | 599 | } |
| 674 | 600 | |
| 675 | 601 | // Update the current field's in_section value |
| @@ -695,21 +621,16 @@ | ||
| 695 | 621 | if ( ! isset( $imported['forms'] ) ) { |
| 696 | 622 | return; |
| 697 | 623 | } |
| 698 | 624 | |
| 699 | - if ( $f['type'] !== 'form' && ( $f['type'] !== 'divider' || ! FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { | |
| 700 | - return; | |
| 625 | + if ( $f['type'] === 'form' || ( $f['type'] === 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { | |
| 626 | + if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) { | |
| 627 | + $form_select = (int) $f['field_options']['form_select']; | |
| 628 | + if ( isset( $imported['forms'][ $form_select ] ) ) { | |
| 629 | + $f['field_options']['form_select'] = $imported['forms'][ $form_select ]; | |
| 630 | + } | |
| 631 | + } | |
| 701 | 632 | } |
| 702 | - | |
| 703 | - if ( ! FrmField::is_option_true( $f['field_options'], 'form_select' ) ) { | |
| 704 | - return; | |
| 705 | - } | |
| 706 | - | |
| 707 | - $form_select = (int) $f['field_options']['form_select']; | |
| 708 | - | |
| 709 | - if ( isset( $imported['forms'][ $form_select ] ) ) { | |
| 710 | - $f['field_options']['form_select'] = $imported['forms'][ $form_select ]; | |
| 711 | - } | |
| 712 | 633 | } |
| 713 | 634 | |
| 714 | 635 | /** |
| 715 | 636 | * Update the get_values_form setting if the form was imported |
| @@ -717,10 +638,8 @@ | ||
| 717 | 638 | * @since 2.01.0 |
| 718 | 639 | * |
| 719 | 640 | * @param array $imported |
| 720 | 641 | * @param array $f |
| 721 | - * | |
| 722 | - * @return void | |
| 723 | 642 | */ |
| 724 | 643 | private static function maybe_update_get_values_form_setting( $imported, &$f ) { |
| 725 | 644 | if ( ! isset( $imported['forms'] ) ) { |
| 726 | 645 | return; |
| @@ -725,35 +644,36 @@ | ||
| 725 | 644 | if ( ! isset( $imported['forms'] ) ) { |
| 726 | 645 | return; |
| 727 | 646 | } |
| 728 | 647 | |
| 729 | - if ( ! FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) { | |
| 730 | - return; | |
| 648 | + if ( FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) { | |
| 649 | + $old_form = $f['field_options']['get_values_form']; | |
| 650 | + if ( isset( $imported['forms'][ $old_form ] ) ) { | |
| 651 | + $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ]; | |
| 652 | + } | |
| 731 | 653 | } |
| 654 | + } | |
| 732 | 655 | |
| 733 | - $old_form = $f['field_options']['get_values_form']; | |
| 734 | - | |
| 735 | - if ( isset( $imported['forms'][ $old_form ] ) ) { | |
| 736 | - $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ]; | |
| 737 | - } | |
| 656 | + /** | |
| 657 | + * If field settings have been migrated, update the values during import. | |
| 658 | + * | |
| 659 | + * @since 4.0 | |
| 660 | + */ | |
| 661 | + private static function run_field_migrations( &$f ) { | |
| 662 | + self::migrate_placeholders( $f ); | |
| 663 | + $f = apply_filters( 'frm_import_xml_field', $f ); | |
| 738 | 664 | } |
| 739 | 665 | |
| 740 | 666 | /** |
| 741 | 667 | * @since 4.0 |
| 742 | - * | |
| 743 | - * @param array $f | |
| 744 | - * | |
| 745 | - * @return void | |
| 746 | 668 | */ |
| 747 | 669 | private static function migrate_placeholders( &$f ) { |
| 748 | 670 | $update_values = self::migrate_field_placeholder( $f, 'clear_on_focus' ); |
| 749 | - | |
| 750 | 671 | foreach ( $update_values as $k => $v ) { |
| 751 | 672 | $f[ $k ] = $v; |
| 752 | 673 | } |
| 753 | 674 | |
| 754 | 675 | $update_values = self::migrate_field_placeholder( $f, 'default_blank' ); |
| 755 | - | |
| 756 | 676 | foreach ( $update_values as $k => $v ) { |
| 757 | 677 | $f[ $k ] = $v; |
| 758 | 678 | } |
| 759 | 679 | } |
| @@ -762,18 +682,13 @@ | ||
| 762 | 682 | * Move clear_on_focus or default_blank to placeholder. |
| 763 | 683 | * Also called during database migration in FrmMigrate. |
| 764 | 684 | * |
| 765 | 685 | * @since 4.0 |
| 766 | - * | |
| 767 | - * @param array|object $field | |
| 768 | - * @param string $type | |
| 769 | - * | |
| 770 | 686 | * @return array |
| 771 | 687 | */ |
| 772 | 688 | public static function migrate_field_placeholder( $field, $type ) { |
| 773 | 689 | $field = (array) $field; |
| 774 | 690 | $field_options = $field['field_options']; |
| 775 | - | |
| 776 | 691 | if ( empty( $field_options[ $type ] ) || empty( $field['default_value'] ) ) { |
| 777 | 692 | return array(); |
| 778 | 693 | } |
| 779 | 694 | |
| @@ -786,34 +701,27 @@ | ||
| 786 | 701 | ); |
| 787 | 702 | |
| 788 | 703 | // If a dropdown placeholder was used, remove the option so it won't be included twice. |
| 789 | 704 | $options = $field['options']; |
| 705 | + if ( $type === 'default_blank' && is_array( $options ) ) { | |
| 706 | + $default_value = $field['default_value']; | |
| 707 | + if ( is_array( $default_value ) ) { | |
| 708 | + $default_value = reset( $default_value ); | |
| 709 | + } | |
| 790 | 710 | |
| 791 | - if ( $type !== 'default_blank' || ! is_array( $options ) ) { | |
| 792 | - return $changes; | |
| 793 | - } | |
| 711 | + foreach ( $options as $opt_key => $opt ) { | |
| 712 | + if ( is_array( $opt ) ) { | |
| 713 | + $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) ); | |
| 714 | + } | |
| 794 | 715 | |
| 795 | - $default_value = $field['default_value']; | |
| 796 | - | |
| 797 | - if ( is_array( $default_value ) ) { | |
| 798 | - $default_value = reset( $default_value ); | |
| 799 | - } | |
| 800 | - | |
| 801 | - foreach ( $options as $opt_key => $opt ) { | |
| 802 | - if ( is_array( $opt ) ) { | |
| 803 | - $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt ); | |
| 716 | + if ( $opt == $default_value ) { | |
| 717 | + unset( $options[ $opt_key ] ); | |
| 718 | + break; | |
| 719 | + } | |
| 804 | 720 | } |
| 805 | - | |
| 806 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 807 | - if ( $opt == $default_value ) { | |
| 808 | - unset( $options[ $opt_key ] ); | |
| 809 | - break; | |
| 810 | - } | |
| 721 | + $changes['options'] = $options; | |
| 811 | 722 | } |
| 812 | 723 | |
| 813 | - $changes['options'] = $options; | |
| 814 | - // end if | |
| 815 | - | |
| 816 | 724 | return $changes; |
| 817 | 725 | } |
| 818 | 726 | |
| 819 | 727 | /** |
| @@ -822,10 +730,8 @@ | ||
| 822 | 730 | * @since 2.0.25 |
| 823 | 731 | * |
| 824 | 732 | * @param array $f |
| 825 | 733 | * @param array $imported |
| 826 | - * | |
| 827 | - * @return void | |
| 828 | 734 | */ |
| 829 | 735 | private static function create_imported_field( $f, &$imported ) { |
| 830 | 736 | $f = self::update_field_options_with_defaults( $f ); |
| 831 | 737 | |
| @@ -833,15 +739,12 @@ | ||
| 833 | 739 | $f = self::maybe_import_images_for_options( $f ); |
| 834 | 740 | } |
| 835 | 741 | |
| 836 | 742 | $new_id = FrmField::create( $f ); |
| 837 | - | |
| 838 | - if ( ! $new_id ) { | |
| 839 | - return; | |
| 743 | + if ( $new_id != false ) { | |
| 744 | + ++$imported['imported']['fields']; | |
| 745 | + do_action( 'frm_after_field_is_imported', $f, $new_id ); | |
| 840 | 746 | } |
| 841 | - | |
| 842 | - ++$imported['imported']['fields']; | |
| 843 | - do_action( 'frm_after_field_is_imported', $f, $new_id ); | |
| 844 | 747 | } |
| 845 | 748 | |
| 846 | 749 | /** |
| 847 | 750 | * Import images for radio buttons and checkboxes from image src if available. |
| @@ -848,9 +751,8 @@ | ||
| 848 | 751 | * |
| 849 | 752 | * @since 5.5.1 |
| 850 | 753 | * |
| 851 | 754 | * @param array $field |
| 852 | - * | |
| 853 | 755 | * @return array |
| 854 | 756 | */ |
| 855 | 757 | private static function maybe_import_images_for_options( $field ) { |
| 856 | 758 | if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) { |
| @@ -881,13 +783,10 @@ | ||
| 881 | 783 | /** |
| 882 | 784 | * Fix field ids for fields that already exist prior to import. |
| 883 | 785 | * |
| 884 | 786 | * @since 4.07 |
| 885 | - * | |
| 886 | 787 | * @param int $form_id |
| 887 | 788 | * @param array $keys_by_original_field_id |
| 888 | - * | |
| 889 | - * @return void | |
| 890 | 789 | */ |
| 891 | 790 | protected static function maybe_update_field_ids( $form_id, $keys_by_original_field_id ) { |
| 892 | 791 | global $frm_duplicate_ids; |
| 893 | 792 | |
| @@ -907,17 +806,15 @@ | ||
| 907 | 806 | $field = (array) $field; |
| 908 | 807 | $frm_duplicate_ids = $keys_by_original_field_id; |
| 909 | 808 | $after = FrmFieldsHelper::switch_field_ids( $field ); |
| 910 | 809 | |
| 911 | - if ( $before['field_options'] === $after['field_options'] ) { | |
| 912 | - continue; | |
| 913 | - } | |
| 810 | + if ( $before['field_options'] !== $after['field_options'] ) { | |
| 811 | + $frm_duplicate_ids = $field_id_by_key; | |
| 812 | + $after = FrmFieldsHelper::switch_field_ids( $after ); | |
| 914 | 813 | |
| 915 | - $frm_duplicate_ids = $field_id_by_key; | |
| 916 | - $after = FrmFieldsHelper::switch_field_ids( $after ); | |
| 917 | - | |
| 918 | - if ( $before['field_options'] !== $after['field_options'] ) { | |
| 919 | - FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) ); | |
| 814 | + if ( $before['field_options'] !== $after['field_options'] ) { | |
| 815 | + FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) ); | |
| 816 | + } | |
| 920 | 817 | } |
| 921 | 818 | } |
| 922 | 819 | |
| 923 | 820 | $frm_duplicate_ids = $former_duplicate_ids; |
| @@ -929,10 +826,8 @@ | ||
| 929 | 826 | * |
| 930 | 827 | * @since 2.0.19 |
| 931 | 828 | * |
| 932 | 829 | * @param array $form |
| 933 | - * | |
| 934 | - * @return void | |
| 935 | 830 | */ |
| 936 | 831 | private static function update_custom_style_setting_on_import( &$form ) { |
| 937 | 832 | if ( ! isset( $form['options']['custom_style'] ) ) { |
| 938 | 833 | return; |
| @@ -940,32 +835,29 @@ | ||
| 940 | 835 | |
| 941 | 836 | if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) { |
| 942 | 837 | // Set to default |
| 943 | 838 | $form['options']['custom_style'] = 1; |
| 839 | + } else { | |
| 840 | + // Replace the style name with the style ID on import | |
| 841 | + global $wpdb; | |
| 842 | + $table = $wpdb->prefix . 'posts'; | |
| 843 | + $where = array( | |
| 844 | + 'post_name' => $form['options']['custom_style'], | |
| 845 | + 'post_type' => 'frm_styles', | |
| 846 | + ); | |
| 847 | + $select = 'ID'; | |
| 848 | + $style_id = FrmDb::get_var( $table, $where, $select ); | |
| 944 | 849 | |
| 945 | - return; | |
| 946 | - } | |
| 850 | + if ( $style_id ) { | |
| 851 | + $form['options']['custom_style'] = $style_id; | |
| 852 | + } else { | |
| 853 | + // save the old style to maybe update after styles import | |
| 854 | + $form['options']['old_style'] = $form['options']['custom_style']; | |
| 947 | 855 | |
| 948 | - // Replace the style name with the style ID on import | |
| 949 | - global $wpdb; | |
| 950 | - $table = $wpdb->prefix . 'posts'; | |
| 951 | - $where = array( | |
| 952 | - 'post_name' => $form['options']['custom_style'], | |
| 953 | - 'post_type' => 'frm_styles', | |
| 954 | - ); | |
| 955 | - $select = 'ID'; | |
| 956 | - $style_id = FrmDb::get_var( $table, $where, $select ); | |
| 957 | - | |
| 958 | - if ( $style_id ) { | |
| 959 | - $form['options']['custom_style'] = $style_id; | |
| 960 | - return; | |
| 961 | - } | |
| 962 | - | |
| 963 | - // Save the old style to maybe update after styles import | |
| 964 | - $form['options']['old_style'] = $form['options']['custom_style']; | |
| 965 | - | |
| 966 | - // Set to default | |
| 967 | - $form['options']['custom_style'] = 1; | |
| 856 | + // Set to default | |
| 857 | + $form['options']['custom_style'] = 1; | |
| 858 | + } | |
| 859 | + }//end if | |
| 968 | 860 | } |
| 969 | 861 | |
| 970 | 862 | /** |
| 971 | 863 | * After styles are imported, check for any forms that were linked |
| @@ -971,29 +863,22 @@ | ||
| 971 | 863 | * After styles are imported, check for any forms that were linked |
| 972 | 864 | * and link them back up. |
| 973 | 865 | * |
| 974 | 866 | * @since 2.2.7 |
| 975 | - * | |
| 976 | - * @param int|string $form_id | |
| 977 | - * | |
| 978 | - * @return void | |
| 979 | 867 | */ |
| 980 | 868 | private static function update_custom_style_setting_after_import( $form_id ) { |
| 981 | 869 | $form = FrmForm::getOne( $form_id ); |
| 982 | 870 | |
| 983 | - if ( ! $form || ! isset( $form->options['old_style'] ) ) { | |
| 984 | - return; | |
| 871 | + if ( $form && isset( $form->options['old_style'] ) ) { | |
| 872 | + $form = (array) $form; | |
| 873 | + $saved_style = $form['options']['custom_style']; | |
| 874 | + $form['options']['custom_style'] = $form['options']['old_style']; | |
| 875 | + self::update_custom_style_setting_on_import( $form ); | |
| 876 | + $has_changed = ( $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style'] ); | |
| 877 | + if ( $has_changed ) { | |
| 878 | + FrmForm::update( $form['id'], $form ); | |
| 879 | + } | |
| 985 | 880 | } |
| 986 | - | |
| 987 | - $form = (array) $form; | |
| 988 | - $saved_style = $form['options']['custom_style']; | |
| 989 | - $form['options']['custom_style'] = $form['options']['old_style']; | |
| 990 | - self::update_custom_style_setting_on_import( $form ); | |
| 991 | - $has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 992 | - | |
| 993 | - if ( $has_changed ) { | |
| 994 | - FrmForm::update( $form['id'], $form ); | |
| 995 | - } | |
| 996 | 881 | } |
| 997 | 882 | |
| 998 | 883 | /** |
| 999 | 884 | * Handle import for all posts. This includes views, styles, pages, and form actions. |
| @@ -999,12 +884,11 @@ | ||
| 999 | 884 | * Handle import for all posts. This includes views, styles, pages, and form actions. |
| 1000 | 885 | * |
| 1001 | 886 | * @param SimpleXMLElement $views |
| 1002 | 887 | * @param array $imported |
| 1003 | - * | |
| 1004 | 888 | * @return array |
| 1005 | 889 | */ |
| 1006 | - public static function import_xml_views( $views, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 890 | + public static function import_xml_views( $views, $imported ) { | |
| 1007 | 891 | $imported['posts'] = array(); |
| 1008 | 892 | $form_action_type = FrmFormActionsController::$action_post_type; |
| 1009 | 893 | |
| 1010 | 894 | $post_types = array( |
| @@ -1041,13 +925,8 @@ | ||
| 1041 | 925 | ); |
| 1042 | 926 | |
| 1043 | 927 | $post['post_content'] = self::switch_form_ids( $post['post_content'], $imported['forms'] ); |
| 1044 | 928 | |
| 1045 | - // Fix issue with line breaks appearing in descriptions as "rn". | |
| 1046 | - if ( $post['post_type'] === $form_action_type ) { | |
| 1047 | - $post['post_content'] = str_replace( '\\\\r\\\\n', '\\r\\n', $post['post_content'] ); | |
| 1048 | - } | |
| 1049 | - | |
| 1050 | 929 | $old_id = $post['post_id']; |
| 1051 | 930 | self::populate_post( $post, $item, $imported ); |
| 1052 | 931 | |
| 1053 | 932 | unset( $item ); |
| @@ -1052,12 +931,10 @@ | ||
| 1052 | 931 | |
| 1053 | 932 | unset( $item ); |
| 1054 | 933 | |
| 1055 | 934 | $post_id = false; |
| 1056 | - | |
| 1057 | 935 | if ( $post['post_type'] === $form_action_type ) { |
| 1058 | 936 | $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] ); |
| 1059 | - | |
| 1060 | 937 | if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) { |
| 1061 | 938 | $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] ); |
| 1062 | 939 | } |
| 1063 | 940 | unset( $action_control ); |
| @@ -1083,9 +960,9 @@ | ||
| 1083 | 960 | if ( ! is_numeric( $post_id ) ) { |
| 1084 | 961 | continue; |
| 1085 | 962 | } |
| 1086 | 963 | |
| 1087 | - if ( str_contains( $post['post_content'], '[display-frm-data' ) || str_contains( $post['post_content'], '[formidable' ) ) { | |
| 964 | + if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) { | |
| 1088 | 965 | $posts_with_shortcodes[ $post_id ] = $post; |
| 1089 | 966 | } |
| 1090 | 967 | |
| 1091 | 968 | self::update_postmeta( $post, $post_id ); |
| @@ -1091,14 +968,13 @@ | ||
| 1091 | 968 | self::update_postmeta( $post, $post_id ); |
| 1092 | 969 | self::update_layout( $post, $post_id ); |
| 1093 | 970 | |
| 1094 | 971 | $this_type = 'posts'; |
| 1095 | - | |
| 1096 | 972 | if ( isset( $post_types[ $post['post_type'] ] ) ) { |
| 1097 | 973 | $this_type = $post_types[ $post['post_type'] ]; |
| 1098 | 974 | } |
| 1099 | 975 | |
| 1100 | - if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) { | |
| 976 | + if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) { | |
| 1101 | 977 | ++$imported['updated'][ $this_type ]; |
| 1102 | 978 | } else { |
| 1103 | 979 | ++$imported['imported'][ $this_type ]; |
| 1104 | 980 | } |
| @@ -1119,9 +995,9 @@ | ||
| 1119 | 995 | } |
| 1120 | 996 | unset( $posts_with_shortcodes, $view_ids ); |
| 1121 | 997 | |
| 1122 | 998 | if ( ! empty( $imported['forms'] ) ) { |
| 1123 | - // Clear imported forms style cache to make sure the new styles are applied to the forms | |
| 999 | + // clear imported forms style cache to make sure the new styles are applied to the forms | |
| 1124 | 1000 | self::clear_forms_style_caches( $imported['forms'] ); |
| 1125 | 1001 | } |
| 1126 | 1002 | |
| 1127 | 1003 | self::maybe_update_stylesheet( $imported ); |
| @@ -1134,10 +1010,8 @@ | ||
| 1134 | 1010 | /** |
| 1135 | 1011 | * Clears styles from cache for imported forms |
| 1136 | 1012 | * |
| 1137 | 1013 | * @param array $imported_forms |
| 1138 | - * | |
| 1139 | - * @return void | |
| 1140 | 1014 | */ |
| 1141 | 1015 | private static function clear_forms_style_caches( $imported_forms ) { |
| 1142 | 1016 | $where = array( |
| 1143 | 1017 | 'id' => $imported_forms, |
| @@ -1146,19 +1020,18 @@ | ||
| 1146 | 1020 | $forms = FrmDb::get_results( 'frm_forms', $where ); |
| 1147 | 1021 | |
| 1148 | 1022 | foreach ( $forms as $form ) { |
| 1149 | 1023 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 1150 | - | |
| 1151 | 1024 | if ( ! $form->options ) { |
| 1152 | 1025 | continue; |
| 1153 | 1026 | } |
| 1154 | - | |
| 1155 | 1027 | $where = array( |
| 1156 | 1028 | 'post_name' => $form->options['old_style'], |
| 1157 | 1029 | 'post_type' => FrmStylesController::$post_type, |
| 1158 | 1030 | ); |
| 1159 | 1031 | |
| 1160 | - $select = 'ID'; | |
| 1032 | + $select = 'ID'; | |
| 1033 | + | |
| 1161 | 1034 | $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' ); |
| 1162 | 1035 | FrmDb::delete_cache_and_transient( $cache_key, 'post' ); |
| 1163 | 1036 | } |
| 1164 | 1037 | } |
| @@ -1167,13 +1040,12 @@ | ||
| 1167 | 1040 | * Replace old form ids with new ones in a string. |
| 1168 | 1041 | * |
| 1169 | 1042 | * @param string $string |
| 1170 | 1043 | * @param array<int> $form_ids new form ids indexed by old form id. |
| 1171 | - * | |
| 1172 | 1044 | * @return string |
| 1173 | 1045 | */ |
| 1174 | 1046 | private static function switch_form_ids( $string, $form_ids ) { |
| 1175 | - if ( ! str_contains( $string, '[formidable' ) ) { | |
| 1047 | + if ( false === strpos( $string, '[formidable' ) ) { | |
| 1176 | 1048 | // Skip string replacing if there are no form shortcodes in string. |
| 1177 | 1049 | return $string; |
| 1178 | 1050 | } |
| 1179 | 1051 | |
| @@ -1200,15 +1072,13 @@ | ||
| 1200 | 1072 | |
| 1201 | 1073 | /** |
| 1202 | 1074 | * @param array<array> $posts_with_shortcodes indexed by current post id. |
| 1203 | 1075 | * @param array<int> $view_ids new view ids indexed by old view id. |
| 1204 | - * | |
| 1205 | 1076 | * @return void |
| 1206 | 1077 | */ |
| 1207 | 1078 | private static function maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ) { |
| 1208 | 1079 | foreach ( $posts_with_shortcodes as $imported_post_id => $post ) { |
| 1209 | 1080 | $post_content = self::switch_view_ids( $post['post_content'], $view_ids ); |
| 1210 | - | |
| 1211 | 1081 | if ( $post_content === $post['post_content'] ) { |
| 1212 | 1082 | continue; |
| 1213 | 1083 | } |
| 1214 | 1084 | |
| @@ -1225,13 +1095,12 @@ | ||
| 1225 | 1095 | * Replace old view ids with new ones in a string. |
| 1226 | 1096 | * |
| 1227 | 1097 | * @param string $string |
| 1228 | 1098 | * @param array<int> $view_ids new view ids indexed by old view id. |
| 1229 | - * | |
| 1230 | 1099 | * @return string |
| 1231 | 1100 | */ |
| 1232 | 1101 | private static function switch_view_ids( $string, $view_ids ) { |
| 1233 | - if ( ! str_contains( $string, '[display-frm-data' ) ) { | |
| 1102 | + if ( false === strpos( $string, '[display-frm-data' ) ) { | |
| 1234 | 1103 | // Skip string replacing if there are no view shortcodes in string. |
| 1235 | 1104 | return $string; |
| 1236 | 1105 | } |
| 1237 | 1106 | |
| @@ -1258,40 +1127,30 @@ | ||
| 1258 | 1127 | } |
| 1259 | 1128 | |
| 1260 | 1129 | /** |
| 1261 | 1130 | * @param string $content |
| 1262 | - * | |
| 1263 | 1131 | * @return string |
| 1264 | 1132 | */ |
| 1265 | 1133 | private static function maybe_prepare_json_view_content( $content ) { |
| 1266 | 1134 | $maybe_decoded = FrmAppHelper::maybe_json_decode( $content ); |
| 1267 | - | |
| 1268 | 1135 | if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) { |
| 1269 | 1136 | return FrmAppHelper::prepare_and_encode( $maybe_decoded ); |
| 1270 | 1137 | } |
| 1271 | - | |
| 1272 | 1138 | return $content; |
| 1273 | 1139 | } |
| 1274 | 1140 | |
| 1275 | - /** | |
| 1276 | - * @param array $post | |
| 1277 | - * @param object $item | |
| 1278 | - * @param array $imported | |
| 1279 | - * | |
| 1280 | - * @return void | |
| 1281 | - */ | |
| 1282 | 1141 | private static function populate_post( &$post, $item, $imported ) { |
| 1283 | 1142 | if ( isset( $item->attachment_url ) ) { |
| 1284 | 1143 | $post['attachment_url'] = (string) $item->attachment_url; |
| 1285 | 1144 | } |
| 1286 | 1145 | |
| 1287 | - if ( $post['post_type'] === FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { | |
| 1288 | - // Update to new form id | |
| 1146 | + if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { | |
| 1147 | + // update to new form id | |
| 1289 | 1148 | $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ]; |
| 1290 | 1149 | } |
| 1291 | 1150 | |
| 1292 | 1151 | // Don't allow default styles to take over a site's default style |
| 1293 | - if ( 'frm_styles' === $post['post_type'] ) { | |
| 1152 | + if ( 'frm_styles' == $post['post_type'] ) { | |
| 1294 | 1153 | $post['menu_order'] = 0; |
| 1295 | 1154 | } |
| 1296 | 1155 | |
| 1297 | 1156 | foreach ( $item->postmeta as $meta ) { |
| @@ -1313,9 +1172,9 @@ | ||
| 1313 | 1172 | * @param array $post |
| 1314 | 1173 | * @param stdClass $meta |
| 1315 | 1174 | * @param array $imported |
| 1316 | 1175 | */ |
| 1317 | - private static function populate_postmeta( &$post, $meta, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 1176 | + private static function populate_postmeta( &$post, $meta, $imported ) { | |
| 1318 | 1177 | global $frm_duplicate_ids; |
| 1319 | 1178 | |
| 1320 | 1179 | $m = array( |
| 1321 | 1180 | 'key' => (string) $meta->meta_key, |
| @@ -1327,13 +1186,14 @@ | ||
| 1327 | 1186 | $m['value'] = $imported['forms'][ (int) $m['value'] ]; |
| 1328 | 1187 | } else { |
| 1329 | 1188 | $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] ); |
| 1330 | 1189 | |
| 1331 | - if ( $frm_duplicate_ids ) { | |
| 1190 | + if ( ! empty( $frm_duplicate_ids ) ) { | |
| 1332 | 1191 | if ( 'frm_dyncontent' === $m['key'] ) { |
| 1333 | 1192 | $m['value'] = self::maybe_prepare_json_view_content( $m['value'] ); |
| 1334 | 1193 | $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] ); |
| 1335 | 1194 | } elseif ( 'frm_options' === $m['key'] ) { |
| 1195 | + | |
| 1336 | 1196 | foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) { |
| 1337 | 1197 | if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) { |
| 1338 | 1198 | $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
| 1339 | 1199 | } |
| @@ -1354,21 +1214,9 @@ | ||
| 1354 | 1214 | } |
| 1355 | 1215 | } |
| 1356 | 1216 | } |
| 1357 | 1217 | |
| 1358 | - if ( ! empty( $m['value']['timeline_options'] ) ) { | |
| 1359 | - foreach ( $m['value']['timeline_options'] as $timeline_option_group_key => $timeline_group_option ) { | |
| 1360 | - foreach ( $timeline_group_option as $timeline_option_key => $timeline_option ) { | |
| 1361 | - // @mago-expect lint:excessive-nesting | |
| 1362 | - if ( isset( $frm_duplicate_ids[ $timeline_option ] ) ) { | |
| 1363 | - $m['value']['timeline_options'][ $timeline_option_group_key ][ $timeline_option_key ] = $frm_duplicate_ids[ $timeline_option ]; | |
| 1364 | - } | |
| 1365 | - } | |
| 1366 | - } | |
| 1367 | - } | |
| 1368 | - | |
| 1369 | 1218 | $check_dup_array = array(); |
| 1370 | - | |
| 1371 | 1219 | if ( ! empty( $m['value']['order_by'] ) ) { |
| 1372 | 1220 | if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) { |
| 1373 | 1221 | $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ]; |
| 1374 | 1222 | } elseif ( is_array( $m['value']['order_by'] ) ) { |
| @@ -1398,14 +1246,8 @@ | ||
| 1398 | 1246 | |
| 1399 | 1247 | $post['postmeta'][ (string) $meta->meta_key ] = $m['value']; |
| 1400 | 1248 | } |
| 1401 | 1249 | |
| 1402 | - /** | |
| 1403 | - * @param array $post | |
| 1404 | - * @param object $layout | |
| 1405 | - * | |
| 1406 | - * @return void | |
| 1407 | - */ | |
| 1408 | 1250 | private static function populate_layout( &$post, $layout ) { |
| 1409 | 1251 | $post['layout'][ (string) $layout->type ] = (string) $layout->data; |
| 1410 | 1252 | } |
| 1411 | 1253 | |
| @@ -1417,19 +1259,16 @@ | ||
| 1417 | 1259 | */ |
| 1418 | 1260 | private static function populate_taxonomies( &$post, $item ) { |
| 1419 | 1261 | foreach ( $item->category as $c ) { |
| 1420 | 1262 | $att = $c->attributes(); |
| 1421 | - | |
| 1422 | 1263 | if ( ! isset( $att['nicename'] ) ) { |
| 1423 | 1264 | continue; |
| 1424 | 1265 | } |
| 1425 | 1266 | |
| 1426 | 1267 | $taxonomy = (string) $att['domain']; |
| 1427 | - | |
| 1428 | 1268 | if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 1429 | 1269 | $name = (string) $att['nicename']; |
| 1430 | 1270 | $h_term = get_term_by( 'slug', $name, $taxonomy ); |
| 1431 | - | |
| 1432 | 1271 | if ( $h_term ) { |
| 1433 | 1272 | $name = $h_term->term_id; |
| 1434 | 1273 | } |
| 1435 | 1274 | unset( $h_term ); |
| @@ -1447,12 +1286,8 @@ | ||
| 1447 | 1286 | } |
| 1448 | 1287 | |
| 1449 | 1288 | /** |
| 1450 | 1289 | * Edit post if the key and created time match. |
| 1451 | - * | |
| 1452 | - * @param array $post By reference. | |
| 1453 | - * | |
| 1454 | - * @return void | |
| 1455 | 1290 | */ |
| 1456 | 1291 | private static function maybe_editing_post( &$post ) { |
| 1457 | 1292 | $match_by = array( |
| 1458 | 1293 | 'post_type' => $post['post_type'], |
| @@ -1467,11 +1302,10 @@ | ||
| 1467 | 1302 | } |
| 1468 | 1303 | |
| 1469 | 1304 | $editing = get_posts( $match_by ); |
| 1470 | 1305 | |
| 1471 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1472 | - if ( $editing && current( $editing )->post_date == $post['post_date'] ) { | |
| 1473 | - // Set the id of the post to edit | |
| 1306 | + if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) { | |
| 1307 | + // set the id of the post to edit | |
| 1474 | 1308 | $post['ID'] = current( $editing )->ID; |
| 1475 | 1309 | } |
| 1476 | 1310 | } |
| 1477 | 1311 | |
| @@ -1477,9 +1311,8 @@ | ||
| 1477 | 1311 | |
| 1478 | 1312 | /** |
| 1479 | 1313 | * @param array $post |
| 1480 | 1314 | * @param int $post_id |
| 1481 | - * | |
| 1482 | 1315 | * @return void |
| 1483 | 1316 | */ |
| 1484 | 1317 | private static function update_postmeta( &$post, $post_id ) { |
| 1485 | 1318 | foreach ( $post['postmeta'] as $k => $v ) { |
| @@ -1516,41 +1349,29 @@ | ||
| 1516 | 1349 | * @param array $post |
| 1517 | 1350 | * @param int $post_id |
| 1518 | 1351 | */ |
| 1519 | 1352 | private static function update_layout( &$post, $post_id ) { |
| 1520 | - if ( ! is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) { | |
| 1521 | - return; | |
| 1353 | + if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) { | |
| 1354 | + $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array(); | |
| 1355 | + $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array(); | |
| 1356 | + if ( $listing_layout || $detail_layout ) { | |
| 1357 | + FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout ); | |
| 1358 | + } | |
| 1522 | 1359 | } |
| 1523 | - | |
| 1524 | - $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array(); | |
| 1525 | - $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array(); | |
| 1526 | - | |
| 1527 | - if ( $listing_layout || $detail_layout ) { | |
| 1528 | - FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout ); | |
| 1529 | - } | |
| 1530 | 1360 | } |
| 1531 | 1361 | |
| 1532 | - /** | |
| 1533 | - * @param array $imported | |
| 1534 | - * | |
| 1535 | - * @return void | |
| 1536 | - */ | |
| 1537 | 1362 | private static function maybe_update_stylesheet( $imported ) { |
| 1538 | 1363 | $new_styles = ! empty( $imported['imported']['styles'] ); |
| 1539 | 1364 | $updated_styles = ! empty( $imported['updated']['styles'] ); |
| 1540 | - | |
| 1541 | - if ( ! $new_styles && ! $updated_styles ) { | |
| 1542 | - return; | |
| 1365 | + if ( $new_styles || $updated_styles ) { | |
| 1366 | + if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { | |
| 1367 | + $frm_style = new FrmStyle(); | |
| 1368 | + $frm_style->update( 'default' ); | |
| 1369 | + } | |
| 1370 | + foreach ( $imported['forms'] as $form_id ) { | |
| 1371 | + self::update_custom_style_setting_after_import( $form_id ); | |
| 1372 | + } | |
| 1543 | 1373 | } |
| 1544 | - | |
| 1545 | - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { | |
| 1546 | - $frm_style = new FrmStyle(); | |
| 1547 | - $frm_style->update( 'default' ); | |
| 1548 | - } | |
| 1549 | - | |
| 1550 | - foreach ( $imported['forms'] as $form_id ) { | |
| 1551 | - self::update_custom_style_setting_after_import( $form_id ); | |
| 1552 | - } | |
| 1553 | 1374 | } |
| 1554 | 1375 | |
| 1555 | 1376 | /** |
| 1556 | 1377 | * @param mixed $result |
| @@ -1555,10 +1376,8 @@ | ||
| 1555 | 1376 | /** |
| 1556 | 1377 | * @param mixed $result |
| 1557 | 1378 | * @param string $message |
| 1558 | 1379 | * @param array $errors |
| 1559 | - * | |
| 1560 | - * @return void | |
| 1561 | 1380 | */ |
| 1562 | 1381 | public static function parse_message( $result, &$message, &$errors ) { |
| 1563 | 1382 | if ( is_wp_error( $result ) ) { |
| 1564 | 1383 | $errors[] = $result->get_error_message(); |
| @@ -1564,16 +1383,15 @@ | ||
| 1564 | 1383 | $errors[] = $result->get_error_message(); |
| 1565 | 1384 | |
| 1566 | 1385 | // Remove the SimpleXML_parse_error from the WP_Error object to avoid |
| 1567 | 1386 | // displaying duplicate error messages from $result->get_error_message() |
| 1387 | + $error_codes = $result->get_error_codes(); | |
| 1568 | 1388 | $error_details = array(); |
| 1569 | - | |
| 1570 | - foreach ( $result->get_error_codes() as $error_code ) { | |
| 1389 | + foreach ( $error_codes as $error_code ) { | |
| 1571 | 1390 | // Clone WP_Error data because WP_Error removes all error messages and data |
| 1572 | - // Associated with the specified error code when an item is removed. | |
| 1391 | + // associated with the specified error code when an item is removed. | |
| 1573 | 1392 | // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source |
| 1574 | 1393 | $error_details = $result->get_error_data( $error_code ); |
| 1575 | - | |
| 1576 | 1394 | if ( $error_code === 'SimpleXML_parse_error' ) { |
| 1577 | 1395 | $result->remove( $error_code ); |
| 1578 | 1396 | break; |
| 1579 | 1397 | } |
| @@ -1578,9 +1396,9 @@ | ||
| 1578 | 1396 | break; |
| 1579 | 1397 | } |
| 1580 | 1398 | } |
| 1581 | 1399 | |
| 1582 | - if ( $error_details ) { | |
| 1400 | + if ( ! empty( $error_details ) ) { | |
| 1583 | 1401 | $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) ); |
| 1584 | 1402 | } |
| 1585 | 1403 | |
| 1586 | 1404 | return; |
| @@ -1600,48 +1418,42 @@ | ||
| 1600 | 1418 | 'updated' => __( 'Updated', 'formidable' ), |
| 1601 | 1419 | ); |
| 1602 | 1420 | |
| 1603 | 1421 | $message = '<ul>'; |
| 1604 | - | |
| 1605 | 1422 | foreach ( $result as $type => $results ) { |
| 1606 | 1423 | if ( ! isset( $t_strings[ $type ] ) ) { |
| 1607 | - // Only print imported and updated | |
| 1424 | + // only print imported and updated | |
| 1608 | 1425 | continue; |
| 1609 | 1426 | } |
| 1610 | 1427 | |
| 1611 | 1428 | $s_message = array(); |
| 1612 | - | |
| 1613 | 1429 | foreach ( $results as $k => $m ) { |
| 1614 | 1430 | self::item_count_message( $m, $k, $s_message ); |
| 1615 | 1431 | unset( $k, $m ); |
| 1616 | 1432 | } |
| 1617 | 1433 | |
| 1618 | - if ( ! $s_message ) { | |
| 1619 | - continue; | |
| 1434 | + if ( ! empty( $s_message ) ) { | |
| 1435 | + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; | |
| 1436 | + $message .= implode( ', ', $s_message ); | |
| 1437 | + $message .= '</li>'; | |
| 1620 | 1438 | } |
| 1439 | + } | |
| 1621 | 1440 | |
| 1622 | - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; | |
| 1623 | - $message .= implode( ', ', $s_message ); | |
| 1624 | - $message .= '</li>'; | |
| 1625 | - }//end foreach | |
| 1626 | - | |
| 1627 | 1441 | if ( $message === '<ul>' ) { |
| 1628 | 1442 | $message = ''; |
| 1629 | 1443 | $errors[] = __( 'Nothing was imported or updated', 'formidable' ); |
| 1444 | + } else { | |
| 1445 | + self::add_form_link_to_message( $result, $message ); | |
| 1630 | 1446 | |
| 1631 | - return; | |
| 1447 | + /** | |
| 1448 | + * @since 5.3 | |
| 1449 | + * | |
| 1450 | + * @param string $message | |
| 1451 | + * @param array $result | |
| 1452 | + */ | |
| 1453 | + $message = apply_filters( 'frm_xml_parsed_message', $message, $result ); | |
| 1454 | + $message .= '</ul>'; | |
| 1632 | 1455 | } |
| 1633 | - | |
| 1634 | - self::add_form_link_to_message( $result, $message ); | |
| 1635 | - | |
| 1636 | - /** | |
| 1637 | - * @since 5.3 | |
| 1638 | - * | |
| 1639 | - * @param string $message | |
| 1640 | - * @param array $result | |
| 1641 | - */ | |
| 1642 | - $message = apply_filters( 'frm_xml_parsed_message', $message, $result ); | |
| 1643 | - $message .= '</ul>'; | |
| 1644 | 1456 | } |
| 1645 | 1457 | |
| 1646 | 1458 | /** |
| 1647 | 1459 | * @param int $m |
| @@ -1646,10 +1458,8 @@ | ||
| 1646 | 1458 | /** |
| 1647 | 1459 | * @param int $m |
| 1648 | 1460 | * @param string $type |
| 1649 | 1461 | * @param array<string> $s_message |
| 1650 | - * | |
| 1651 | - * @return void | |
| 1652 | 1462 | */ |
| 1653 | 1463 | public static function item_count_message( $m, $type, &$s_message ) { |
| 1654 | 1464 | if ( ! $m ) { |
| 1655 | 1465 | return; |
| @@ -1675,22 +1485,21 @@ | ||
| 1675 | 1485 | ); |
| 1676 | 1486 | |
| 1677 | 1487 | if ( isset( $strings[ $type ] ) ) { |
| 1678 | 1488 | $s_message[] = $strings[ $type ]; |
| 1679 | - return; | |
| 1489 | + } else { | |
| 1490 | + $string = ' ' . $m . ' ' . ucfirst( $type ); | |
| 1491 | + | |
| 1492 | + /** | |
| 1493 | + * @since 5.3 | |
| 1494 | + * | |
| 1495 | + * @param string $string Message string for imported item. | |
| 1496 | + * @param int $m Number of item that was imported. | |
| 1497 | + * } | |
| 1498 | + */ | |
| 1499 | + $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m ); | |
| 1500 | + $s_message[] = $string; | |
| 1680 | 1501 | } |
| 1681 | - | |
| 1682 | - $string = ' ' . $m . ' ' . ucfirst( $type ); | |
| 1683 | - | |
| 1684 | - /** | |
| 1685 | - * @since 5.3 | |
| 1686 | - * | |
| 1687 | - * @param string $string Message string for imported item. | |
| 1688 | - * @param int $m Number of item that was imported. | |
| 1689 | - * } | |
| 1690 | - */ | |
| 1691 | - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m ); | |
| 1692 | - $s_message[] = $string; | |
| 1693 | 1502 | } |
| 1694 | 1503 | |
| 1695 | 1504 | /** |
| 1696 | 1505 | * If a single form was imported, include a link in the success message. |
| @@ -1695,28 +1504,24 @@ | ||
| 1695 | 1504 | /** |
| 1696 | 1505 | * If a single form was imported, include a link in the success message. |
| 1697 | 1506 | * |
| 1698 | 1507 | * @since 4.0 |
| 1699 | - * | |
| 1700 | 1508 | * @param array $result The response from the XML import. |
| 1701 | 1509 | * @param string $message The response shown on the page after import. |
| 1702 | 1510 | */ |
| 1703 | 1511 | private static function add_form_link_to_message( $result, &$message ) { |
| 1704 | 1512 | $total_forms = $result['imported']['forms'] + $result['updated']['forms']; |
| 1705 | - | |
| 1706 | 1513 | if ( $total_forms > 1 ) { |
| 1707 | 1514 | return; |
| 1708 | 1515 | } |
| 1709 | 1516 | |
| 1710 | 1517 | $primary_form = reset( $result['forms'] ); |
| 1518 | + if ( ! empty( $primary_form ) ) { | |
| 1519 | + $primary_form = FrmForm::getOne( $primary_form ); | |
| 1520 | + $form_id = empty( $primary_form->parent_form_id ) ? $primary_form->id : $primary_form->parent_form_id; | |
| 1711 | 1521 | |
| 1712 | - if ( ! $primary_form ) { | |
| 1713 | - return; | |
| 1522 | + $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>'; | |
| 1714 | 1523 | } |
| 1715 | - | |
| 1716 | - $primary_form = FrmForm::getOne( $primary_form ); | |
| 1717 | - $form_id = ! empty( $primary_form->parent_form_id ) ? $primary_form->parent_form_id : $primary_form->id; | |
| 1718 | - $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>'; | |
| 1719 | 1524 | } |
| 1720 | 1525 | |
| 1721 | 1526 | /** |
| 1722 | 1527 | * Prepare the form options for export |
| @@ -1729,17 +1534,22 @@ | ||
| 1729 | 1534 | */ |
| 1730 | 1535 | public static function prepare_form_options_for_export( $options ) { |
| 1731 | 1536 | FrmAppHelper::unserialize_or_decode( $options ); |
| 1732 | 1537 | // Change custom_style to the post_name instead of ID (1 may be a string) |
| 1733 | - $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1734 | - | |
| 1538 | + $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; | |
| 1735 | 1539 | if ( $not_default ) { |
| 1736 | 1540 | global $wpdb; |
| 1737 | - $table = $wpdb->prefix . 'posts'; | |
| 1738 | - $where = array( 'ID' => $options['custom_style'] ); | |
| 1739 | - $select = 'post_name'; | |
| 1740 | - $style_name = FrmDb::get_var( $table, $where, $select ); | |
| 1741 | - $options['custom_style'] = $style_name ? $style_name : 1; | |
| 1541 | + $table = $wpdb->prefix . 'posts'; | |
| 1542 | + $where = array( 'ID' => $options['custom_style'] ); | |
| 1543 | + $select = 'post_name'; | |
| 1544 | + | |
| 1545 | + $style_name = FrmDb::get_var( $table, $where, $select ); | |
| 1546 | + | |
| 1547 | + if ( $style_name ) { | |
| 1548 | + $options['custom_style'] = $style_name; | |
| 1549 | + } else { | |
| 1550 | + $options['custom_style'] = 1; | |
| 1551 | + } | |
| 1742 | 1552 | } |
| 1743 | 1553 | self::remove_default_form_options( $options ); |
| 1744 | 1554 | $options = serialize( $options ); |
| 1745 | 1555 | |
| @@ -1750,16 +1560,11 @@ | ||
| 1750 | 1560 | * If the saved value is the same as the default, remove it from the export |
| 1751 | 1561 | * This keeps file size down and prevents overriding global settings after import |
| 1752 | 1562 | * |
| 1753 | 1563 | * @since 3.06 |
| 1754 | - * | |
| 1755 | - * @param array $options By reference. | |
| 1756 | - * | |
| 1757 | - * @return void | |
| 1758 | 1564 | */ |
| 1759 | 1565 | private static function remove_default_form_options( &$options ) { |
| 1760 | 1566 | $defaults = FrmFormsHelper::get_default_opts(); |
| 1761 | - | |
| 1762 | 1567 | if ( is_callable( 'FrmProFormsHelper::get_default_opts' ) ) { |
| 1763 | 1568 | $defaults += FrmProFormsHelper::get_default_opts(); |
| 1764 | 1569 | } |
| 1765 | 1570 | self::remove_defaults( $defaults, $options ); |
| @@ -1768,12 +1573,8 @@ | ||
| 1768 | 1573 | /** |
| 1769 | 1574 | * Remove extra settings from field to keep file size down |
| 1770 | 1575 | * |
| 1771 | 1576 | * @since 3.06 |
| 1772 | - * | |
| 1773 | - * @param object $field | |
| 1774 | - * | |
| 1775 | - * @return void | |
| 1776 | 1577 | */ |
| 1777 | 1578 | public static function prepare_field_for_export( &$field ) { |
| 1778 | 1579 | self::remove_default_field_options( $field ); |
| 1779 | 1580 | self::add_image_src_to_image_options( $field ); |
| @@ -1782,16 +1583,11 @@ | ||
| 1782 | 1583 | /** |
| 1783 | 1584 | * Remove defaults from field options too |
| 1784 | 1585 | * |
| 1785 | 1586 | * @since 3.06 |
| 1786 | - * | |
| 1787 | - * @param object $field | |
| 1788 | - * | |
| 1789 | - * @return void | |
| 1790 | 1587 | */ |
| 1791 | 1588 | private static function remove_default_field_options( &$field ) { |
| 1792 | 1589 | $defaults = self::default_field_options( $field->type ); |
| 1793 | - | |
| 1794 | 1590 | if ( empty( $defaults['blank'] ) ) { |
| 1795 | 1591 | $global_settings = new FrmSettings(); |
| 1796 | 1592 | $global_defaults = $global_settings->default_options(); |
| 1797 | 1593 | $defaults['blank'] = $global_defaults['blank_msg']; |
| @@ -1819,13 +1615,12 @@ | ||
| 1819 | 1615 | * |
| 1820 | 1616 | * @since 5.5.1 |
| 1821 | 1617 | * |
| 1822 | 1618 | * @param stdClass $field |
| 1823 | - * | |
| 1824 | 1619 | * @return void |
| 1825 | 1620 | */ |
| 1826 | 1621 | private static function add_image_src_to_image_options( $field ) { |
| 1827 | - if ( empty( $field->options ) || ! str_contains( $field->options, 'image' ) ) { | |
| 1622 | + if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) { | |
| 1828 | 1623 | return; |
| 1829 | 1624 | } |
| 1830 | 1625 | |
| 1831 | 1626 | $updated = false; |
| @@ -1836,14 +1631,12 @@ | ||
| 1836 | 1631 | return; |
| 1837 | 1632 | } |
| 1838 | 1633 | |
| 1839 | 1634 | foreach ( $options as $key => $option ) { |
| 1840 | - if ( ! is_array( $option ) || empty( $option['image'] ) ) { | |
| 1841 | - continue; | |
| 1635 | + if ( is_array( $option ) && ! empty( $option['image'] ) ) { | |
| 1636 | + $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] ); | |
| 1637 | + $updated = true; | |
| 1842 | 1638 | } |
| 1843 | - | |
| 1844 | - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] ); | |
| 1845 | - $updated = true; | |
| 1846 | 1639 | } |
| 1847 | 1640 | |
| 1848 | 1641 | if ( $updated ) { |
| 1849 | 1642 | $field->options = maybe_serialize( $options ); |
| @@ -1851,20 +1644,14 @@ | ||
| 1851 | 1644 | } |
| 1852 | 1645 | |
| 1853 | 1646 | /** |
| 1854 | 1647 | * @since 3.06.03 |
| 1855 | - * | |
| 1856 | - * @param string $type | |
| 1857 | - * | |
| 1858 | - * @return array | |
| 1859 | 1648 | */ |
| 1860 | 1649 | private static function default_field_options( $type ) { |
| 1861 | 1650 | $defaults = FrmFieldsHelper::get_default_field_options( $type ); |
| 1862 | - | |
| 1863 | 1651 | if ( empty( $defaults['custom_html'] ) ) { |
| 1864 | 1652 | $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type ); |
| 1865 | 1653 | } |
| 1866 | - | |
| 1867 | 1654 | return $defaults; |
| 1868 | 1655 | } |
| 1869 | 1656 | |
| 1870 | 1657 | /** |
| @@ -1871,13 +1658,8 @@ | ||
| 1871 | 1658 | * Compare the default array to the saved values and |
| 1872 | 1659 | * remove if they are the same |
| 1873 | 1660 | * |
| 1874 | 1661 | * @since 3.06 |
| 1875 | - * | |
| 1876 | - * @param array $defaults | |
| 1877 | - * @param array $saved | |
| 1878 | - * | |
| 1879 | - * @return void | |
| 1880 | 1662 | */ |
| 1881 | 1663 | private static function remove_defaults( $defaults, &$saved ) { |
| 1882 | 1664 | foreach ( $saved as $key => $value ) { |
| 1883 | 1665 | if ( isset( $defaults[ $key ] ) && $defaults[ $key ] === $value ) { |
| @@ -1889,14 +1671,8 @@ | ||
| 1889 | 1671 | /** |
| 1890 | 1672 | * The line endings may prevent html from being equal when it should |
| 1891 | 1673 | * |
| 1892 | 1674 | * @since 3.06 |
| 1893 | - * | |
| 1894 | - * @param string $html_name | |
| 1895 | - * @param array $defaults | |
| 1896 | - * @param array $options | |
| 1897 | - * | |
| 1898 | - * @return void | |
| 1899 | 1675 | */ |
| 1900 | 1676 | private static function remove_default_html( $html_name, $defaults, &$options ) { |
| 1901 | 1677 | if ( ! isset( $options[ $html_name ] ) || ! isset( $defaults[ $html_name ] ) ) { |
| 1902 | 1678 | return; |
| @@ -1903,34 +1679,26 @@ | ||
| 1903 | 1679 | } |
| 1904 | 1680 | |
| 1905 | 1681 | $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] ); |
| 1906 | 1682 | $default_html = $defaults[ $html_name ]; |
| 1907 | - | |
| 1908 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1909 | 1683 | if ( $old_html == $default_html ) { |
| 1910 | 1684 | unset( $options[ $html_name ] ); |
| 1685 | + | |
| 1911 | 1686 | return; |
| 1912 | 1687 | } |
| 1913 | 1688 | |
| 1914 | 1689 | // Account for some of the older field default HTML. |
| 1915 | 1690 | $default_html = str_replace( ' id="frm_desc_field_[key]"', '', $default_html ); |
| 1916 | - | |
| 1917 | - if ( $old_html === $default_html ) { | |
| 1691 | + if ( $old_html == $default_html ) { | |
| 1918 | 1692 | unset( $options[ $html_name ] ); |
| 1919 | 1693 | } |
| 1920 | 1694 | } |
| 1921 | 1695 | |
| 1922 | - /** | |
| 1923 | - * @param string $str | |
| 1924 | - * | |
| 1925 | - * @return string | |
| 1926 | - */ | |
| 1927 | 1696 | public static function cdata( $str ) { |
| 1928 | 1697 | FrmAppHelper::unserialize_or_decode( $str ); |
| 1929 | - | |
| 1930 | 1698 | if ( is_array( $str ) ) { |
| 1931 | 1699 | $str = json_encode( $str ); |
| 1932 | - } elseif ( FrmAppHelper::is_valid_utf8( $str ) === false ) { | |
| 1700 | + } elseif ( seems_utf8( $str ) === false ) { | |
| 1933 | 1701 | $str = FrmAppHelper::maybe_utf8_encode( $str ); |
| 1934 | 1702 | } |
| 1935 | 1703 | |
| 1936 | 1704 | if ( is_numeric( $str ) ) { |
| @@ -1939,9 +1707,11 @@ | ||
| 1939 | 1707 | |
| 1940 | 1708 | self::remove_invalid_characters_from_xml( $str ); |
| 1941 | 1709 | |
| 1942 | 1710 | // $str = ent2ncr(esc_html( $str)); |
| 1943 | - return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 1711 | + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 1712 | + | |
| 1713 | + return $str; | |
| 1944 | 1714 | } |
| 1945 | 1715 | |
| 1946 | 1716 | /** |
| 1947 | 1717 | * Remove <US> character (unit separator) from exported strings |
| @@ -1948,10 +1718,8 @@ | ||
| 1948 | 1718 | * |
| 1949 | 1719 | * @since 2.0.22 |
| 1950 | 1720 | * |
| 1951 | 1721 | * @param string $str |
| 1952 | - * | |
| 1953 | - * @return void | |
| 1954 | 1722 | */ |
| 1955 | 1723 | private static function remove_invalid_characters_from_xml( &$str ) { |
| 1956 | 1724 | // Remove <US> character |
| 1957 | 1725 | $str = str_replace( '\x1F', '', $str ); |
| @@ -1956,18 +1724,8 @@ | ||
| 1956 | 1724 | // Remove <US> character |
| 1957 | 1725 | $str = str_replace( '\x1F', '', $str ); |
| 1958 | 1726 | } |
| 1959 | 1727 | |
| 1960 | - /** | |
| 1961 | - * Migrate form settings to actions | |
| 1962 | - * | |
| 1963 | - * @param array $form_options | |
| 1964 | - * @param int $form_id | |
| 1965 | - * @param array $imported | |
| 1966 | - * @param bool $switch | |
| 1967 | - * | |
| 1968 | - * @return void | |
| 1969 | - */ | |
| 1970 | 1728 | public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) { |
| 1971 | 1729 | // Get post type |
| 1972 | 1730 | $post_type = FrmFormActionsController::$action_post_type; |
| 1973 | 1731 | |
| @@ -1992,9 +1750,9 @@ | ||
| 1992 | 1750 | * @param array $imported |
| 1993 | 1751 | * @param bool $switch |
| 1994 | 1752 | */ |
| 1995 | 1753 | private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
| 1996 | - if ( empty( $form_options['create_post'] ) ) { | |
| 1754 | + if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) { | |
| 1997 | 1755 | return; |
| 1998 | 1756 | } |
| 1999 | 1757 | |
| 2000 | 1758 | $new_action = array( |
| @@ -2046,9 +1804,8 @@ | ||
| 2046 | 1804 | $array_fields = array( 'post_category', 'post_custom_fields' ); |
| 2047 | 1805 | |
| 2048 | 1806 | $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields ); |
| 2049 | 1807 | } |
| 2050 | - | |
| 2051 | 1808 | $new_action['post_content'] = json_encode( $new_action['post_content'] ); |
| 2052 | 1809 | |
| 2053 | 1810 | $exists = get_posts( |
| 2054 | 1811 | array( |
| @@ -2058,15 +1815,13 @@ | ||
| 2058 | 1815 | 'numberposts' => 1, |
| 2059 | 1816 | ) |
| 2060 | 1817 | ); |
| 2061 | 1818 | |
| 2062 | - if ( $exists ) { | |
| 2063 | - return; | |
| 1819 | + if ( ! $exists ) { | |
| 1820 | + // this isn't an email, but we need to use a class that will always be included | |
| 1821 | + FrmDb::save_json_post( $new_action ); | |
| 1822 | + ++$imported['imported']['actions']; | |
| 2064 | 1823 | } |
| 2065 | - | |
| 2066 | - // This isn't an email, but we need to use a class that will always be included | |
| 2067 | - FrmDb::save_json_post( $new_action ); | |
| 2068 | - ++$imported['imported']['actions']; | |
| 2069 | 1824 | } |
| 2070 | 1825 | |
| 2071 | 1826 | /** |
| 2072 | 1827 | * Switch old field IDs for new field IDs in emails and post |
| @@ -2083,9 +1838,9 @@ | ||
| 2083 | 1838 | global $frm_duplicate_ids; |
| 2084 | 1839 | |
| 2085 | 1840 | // If there aren't IDs that were switched, end now |
| 2086 | 1841 | if ( ! $frm_duplicate_ids ) { |
| 2087 | - return null; | |
| 1842 | + return; | |
| 2088 | 1843 | } |
| 2089 | 1844 | |
| 2090 | 1845 | // Get old IDs |
| 2091 | 1846 | $old = array_keys( $frm_duplicate_ids ); |
| @@ -2094,13 +1849,11 @@ | ||
| 2094 | 1849 | $new = array_values( $frm_duplicate_ids ); |
| 2095 | 1850 | |
| 2096 | 1851 | // Do a str_replace with each item to set the new IDs |
| 2097 | 1852 | foreach ( $post_content as $key => $setting ) { |
| 2098 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 2099 | 1853 | if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) { |
| 2100 | 1854 | // Replace old IDs with new IDs |
| 2101 | 1855 | $post_content[ $key ] = str_replace( $old, $new, $setting ); |
| 2102 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 2103 | 1856 | } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) { |
| 2104 | 1857 | foreach ( $setting as $k => $val ) { |
| 2105 | 1858 | // Replace old IDs with new IDs |
| 2106 | 1859 | $post_content[ $key ][ $k ] = str_replace( $old, $new, $val ); |
| @@ -2111,19 +1864,8 @@ | ||
| 2111 | 1864 | |
| 2112 | 1865 | return $post_content; |
| 2113 | 1866 | } |
| 2114 | 1867 | |
| 2115 | - /** | |
| 2116 | - * Migrate email settings to action | |
| 2117 | - * | |
| 2118 | - * @param array $form_options | |
| 2119 | - * @param int $form_id | |
| 2120 | - * @param string $post_type | |
| 2121 | - * @param array $imported | |
| 2122 | - * @param bool $switch | |
| 2123 | - * | |
| 2124 | - * @return void | |
| 2125 | - */ | |
| 2126 | 1868 | private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
| 2127 | 1869 | // No old notifications or autoresponders to carry over |
| 2128 | 1870 | if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) { |
| 2129 | 1871 | return; |
| @@ -2137,9 +1879,9 @@ | ||
| 2137 | 1879 | |
| 2138 | 1880 | // Migrate autoresponders |
| 2139 | 1881 | self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications ); |
| 2140 | 1882 | |
| 2141 | - if ( ! $notifications ) { | |
| 1883 | + if ( empty( $notifications ) ) { | |
| 2142 | 1884 | return; |
| 2143 | 1885 | } |
| 2144 | 1886 | |
| 2145 | 1887 | foreach ( $notifications as $new_notification ) { |
| @@ -2150,8 +1892,9 @@ | ||
| 2150 | 1892 | $new_notification['post_status'] = 'publish'; |
| 2151 | 1893 | |
| 2152 | 1894 | // Switch field IDs and keys, if needed |
| 2153 | 1895 | if ( $switch ) { |
| 1896 | + | |
| 2154 | 1897 | // Switch field IDs in email conditional logic |
| 2155 | 1898 | self::switch_email_condition_field_ids( $new_notification['post_content'] ); |
| 2156 | 1899 | |
| 2157 | 1900 | // Switch all other field IDs in email |
| @@ -2156,9 +1899,8 @@ | ||
| 2156 | 1899 | |
| 2157 | 1900 | // Switch all other field IDs in email |
| 2158 | 1901 | $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] ); |
| 2159 | 1902 | } |
| 2160 | - | |
| 2161 | 1903 | $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] ); |
| 2162 | 1904 | |
| 2163 | 1905 | $exists = get_posts( |
| 2164 | 1906 | array( |
| @@ -2168,9 +1910,9 @@ | ||
| 2168 | 1910 | 'numberposts' => 1, |
| 2169 | 1911 | ) |
| 2170 | 1912 | ); |
| 2171 | 1913 | |
| 2172 | - if ( ! $exists ) { | |
| 1914 | + if ( empty( $exists ) ) { | |
| 2173 | 1915 | FrmDb::save_json_post( $new_notification ); |
| 2174 | 1916 | ++$imported['imported']['actions']; |
| 2175 | 1917 | } |
| 2176 | 1918 | unset( $new_notification ); |
| @@ -2185,43 +1927,31 @@ | ||
| 2185 | 1927 | * @since 2.05 |
| 2186 | 1928 | * |
| 2187 | 1929 | * @param int|string $form_id |
| 2188 | 1930 | * @param array $form_options |
| 2189 | - * | |
| 2190 | - * @return void | |
| 2191 | 1931 | */ |
| 2192 | 1932 | private static function remove_deprecated_notification_settings( $form_id, $form_options ) { |
| 2193 | 1933 | $delete_settings = array( 'notification', 'autoresponder', 'email_to' ); |
| 2194 | - | |
| 2195 | 1934 | foreach ( $delete_settings as $index ) { |
| 2196 | 1935 | if ( isset( $form_options[ $index ] ) ) { |
| 2197 | 1936 | unset( $form_options[ $index ] ); |
| 2198 | 1937 | } |
| 2199 | 1938 | } |
| 2200 | - | |
| 2201 | 1939 | FrmForm::update( $form_id, array( 'options' => $form_options ) ); |
| 2202 | 1940 | } |
| 2203 | 1941 | |
| 2204 | - /** | |
| 2205 | - * Migrate notifications to action | |
| 2206 | - * | |
| 2207 | - * @param array $form_options | |
| 2208 | - * @param int $form_id | |
| 2209 | - * @param array $notifications | |
| 2210 | - * | |
| 2211 | - * @return void | |
| 2212 | - */ | |
| 2213 | 1942 | private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) { |
| 2214 | 1943 | if ( ! isset( $form_options['notification'] ) && ! empty( $form_options['email_to'] ) ) { |
| 2215 | - // Add old settings into notification array | |
| 1944 | + // add old settings into notification array | |
| 2216 | 1945 | $form_options['notification'] = array( 0 => $form_options ); |
| 2217 | 1946 | } elseif ( isset( $form_options['notification']['email_to'] ) ) { |
| 2218 | - // Make sure it's in the correct format | |
| 1947 | + // make sure it's in the correct format | |
| 2219 | 1948 | $form_options['notification'] = array( 0 => $form_options['notification'] ); |
| 2220 | 1949 | } |
| 2221 | 1950 | |
| 2222 | 1951 | if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) { |
| 2223 | 1952 | foreach ( $form_options['notification'] as $email_key => $notification ) { |
| 1953 | + | |
| 2224 | 1954 | $atts = array( |
| 2225 | 1955 | 'email_to' => '', |
| 2226 | 1956 | 'reply_to' => '', |
| 2227 | 1957 | 'reply_to_name' => '', |
| @@ -2232,9 +1962,9 @@ | ||
| 2232 | 1962 | |
| 2233 | 1963 | // Format the email data |
| 2234 | 1964 | self::format_email_data( $atts, $notification ); |
| 2235 | 1965 | |
| 2236 | - if ( ! empty( $notification['twilio'] ) ) { | |
| 1966 | + if ( isset( $notification['twilio'] ) && $notification['twilio'] ) { | |
| 2237 | 1967 | do_action( 'frm_create_twilio_action', $atts, $notification ); |
| 2238 | 1968 | } |
| 2239 | 1969 | |
| 2240 | 1970 | // Setup the new notification |
| @@ -2245,16 +1975,8 @@ | ||
| 2245 | 1975 | }//end foreach |
| 2246 | 1976 | }//end if |
| 2247 | 1977 | } |
| 2248 | 1978 | |
| 2249 | - /** | |
| 2250 | - * Format email data | |
| 2251 | - * | |
| 2252 | - * @param array $atts | |
| 2253 | - * @param array $notification | |
| 2254 | - * | |
| 2255 | - * @return void | |
| 2256 | - */ | |
| 2257 | 1979 | private static function format_email_data( &$atts, $notification ) { |
| 2258 | 1980 | // Format email_to |
| 2259 | 1981 | self::format_email_to_data( $atts, $notification ); |
| 2260 | 1982 | |
| @@ -2262,16 +1984,14 @@ | ||
| 2262 | 1984 | $reply_fields = array( |
| 2263 | 1985 | 'reply_to' => '', |
| 2264 | 1986 | 'reply_to_name' => '', |
| 2265 | 1987 | ); |
| 2266 | - | |
| 2267 | 1988 | foreach ( $reply_fields as $f => $val ) { |
| 2268 | 1989 | if ( isset( $notification[ $f ] ) ) { |
| 2269 | 1990 | $atts[ $f ] = $notification[ $f ]; |
| 2270 | - | |
| 2271 | - if ( 'custom' === $notification[ $f ] ) { | |
| 1991 | + if ( 'custom' == $notification[ $f ] ) { | |
| 2272 | 1992 | $atts[ $f ] = $notification[ 'cust_' . $f ]; |
| 2273 | - } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) { | |
| 1993 | + } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) { | |
| 2274 | 1994 | $atts[ $f ] = '[' . $atts[ $f ] . ']'; |
| 2275 | 1995 | } |
| 2276 | 1996 | } |
| 2277 | 1997 | unset( $f, $val ); |
| @@ -2278,27 +1998,21 @@ | ||
| 2278 | 1998 | } |
| 2279 | 1999 | |
| 2280 | 2000 | // Format event |
| 2281 | 2001 | $atts['event'] = array( 'create' ); |
| 2282 | - | |
| 2283 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 2284 | 2002 | if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) { |
| 2285 | 2003 | $atts['event'][] = 'update'; |
| 2286 | - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 2004 | + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { | |
| 2287 | 2005 | $atts['event'] = array( 'update' ); |
| 2288 | 2006 | } |
| 2289 | 2007 | } |
| 2290 | 2008 | |
| 2291 | - /** | |
| 2292 | - * Format email_to data | |
| 2293 | - * | |
| 2294 | - * @param array $atts | |
| 2295 | - * @param array $notification | |
| 2296 | - * | |
| 2297 | - * @return void | |
| 2298 | - */ | |
| 2299 | 2009 | private static function format_email_to_data( &$atts, $notification ) { |
| 2300 | - $atts['email_to'] = isset( $notification['email_to'] ) ? preg_split( '/ (,|;) /', $notification['email_to'] ) : array(); | |
| 2010 | + if ( isset( $notification['email_to'] ) ) { | |
| 2011 | + $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] ); | |
| 2012 | + } else { | |
| 2013 | + $atts['email_to'] = array(); | |
| 2014 | + } | |
| 2301 | 2015 | |
| 2302 | 2016 | if ( isset( $notification['also_email_to'] ) ) { |
| 2303 | 2017 | $email_fields = (array) $notification['also_email_to']; |
| 2304 | 2018 | $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] ); |
| @@ -2305,36 +2019,24 @@ | ||
| 2305 | 2019 | unset( $email_fields ); |
| 2306 | 2020 | } |
| 2307 | 2021 | |
| 2308 | 2022 | foreach ( $atts['email_to'] as $key => $email_field ) { |
| 2023 | + | |
| 2309 | 2024 | if ( is_numeric( $email_field ) ) { |
| 2310 | 2025 | $atts['email_to'][ $key ] = '[' . $email_field . ']'; |
| 2311 | 2026 | } |
| 2312 | 2027 | |
| 2313 | - if ( ! str_contains( $email_field, '|' ) ) { | |
| 2314 | - continue; | |
| 2028 | + if ( strpos( $email_field, '|' ) ) { | |
| 2029 | + $email_opt = explode( '|', $email_field ); | |
| 2030 | + if ( isset( $email_opt[0] ) ) { | |
| 2031 | + $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']'; | |
| 2032 | + } | |
| 2033 | + unset( $email_opt ); | |
| 2315 | 2034 | } |
| 2316 | - | |
| 2317 | - $email_opt = explode( '|', $email_field ); | |
| 2318 | - | |
| 2319 | - if ( isset( $email_opt[0] ) ) { | |
| 2320 | - $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']'; | |
| 2321 | - } | |
| 2322 | - unset( $email_opt ); | |
| 2323 | 2035 | } |
| 2324 | - | |
| 2325 | 2036 | $atts['email_to'] = implode( ', ', $atts['email_to'] ); |
| 2326 | 2037 | } |
| 2327 | 2038 | |
| 2328 | - /** | |
| 2329 | - * Setup new notification | |
| 2330 | - * | |
| 2331 | - * @param array $new_notification | |
| 2332 | - * @param array $notification | |
| 2333 | - * @param array $atts | |
| 2334 | - * | |
| 2335 | - * @return void | |
| 2336 | - */ | |
| 2337 | 2039 | private static function setup_new_notification( &$new_notification, $notification, $atts ) { |
| 2338 | 2040 | // Set up new notification |
| 2339 | 2041 | $new_notification = array( |
| 2340 | 2042 | 'post_content' => array( |
| @@ -2345,13 +2047,12 @@ | ||
| 2345 | 2047 | ); |
| 2346 | 2048 | |
| 2347 | 2049 | // Add more fields to the new notification |
| 2348 | 2050 | $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' ); |
| 2349 | - | |
| 2350 | 2051 | foreach ( $add_fields as $add_field ) { |
| 2351 | 2052 | if ( isset( $notification[ $add_field ] ) ) { |
| 2352 | 2053 | $new_notification['post_content'][ $add_field ] = $notification[ $add_field ]; |
| 2353 | - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) { | |
| 2054 | + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) { | |
| 2354 | 2055 | $new_notification['post_content'][ $add_field ] = 0; |
| 2355 | 2056 | } else { |
| 2356 | 2057 | $new_notification['post_content'][ $add_field ] = ''; |
| 2357 | 2058 | } |
| @@ -2362,9 +2063,9 @@ | ||
| 2362 | 2063 | $new_notification['post_content']['reply_to'] = $atts['reply_to']; |
| 2363 | 2064 | |
| 2364 | 2065 | // Set from |
| 2365 | 2066 | if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) { |
| 2366 | - $new_notification['post_content']['from'] = ( ! empty( $atts['reply_to_name'] ) ? $atts['reply_to_name'] : '[sitename]' ) . ' <' . ( ! empty( $atts['reply_to'] ) ? $atts['reply_to'] : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 2067 | + $new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>'; | |
| 2367 | 2068 | } |
| 2368 | 2069 | } |
| 2369 | 2070 | |
| 2370 | 2071 | /** |
| @@ -2370,81 +2071,63 @@ | ||
| 2370 | 2071 | /** |
| 2371 | 2072 | * Switch field IDs in pre-2.0 email conditional logic |
| 2372 | 2073 | * |
| 2373 | 2074 | * @param array $post_content Pass by reference. |
| 2374 | - * | |
| 2375 | - * @return void | |
| 2376 | 2075 | */ |
| 2377 | 2076 | private static function switch_email_condition_field_ids( &$post_content ) { |
| 2378 | 2077 | // Switch field IDs in conditional logic |
| 2379 | - if ( ! isset( $post_content['conditions'] ) || ! is_array( $post_content['conditions'] ) ) { | |
| 2380 | - return; | |
| 2381 | - } | |
| 2382 | - | |
| 2383 | - foreach ( $post_content['conditions'] as $email_key => $val ) { | |
| 2384 | - if ( is_numeric( $email_key ) ) { | |
| 2385 | - $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) ); | |
| 2078 | + if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) { | |
| 2079 | + foreach ( $post_content['conditions'] as $email_key => $val ) { | |
| 2080 | + if ( is_numeric( $email_key ) ) { | |
| 2081 | + $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) ); | |
| 2082 | + } | |
| 2083 | + unset( $email_key, $val ); | |
| 2386 | 2084 | } |
| 2387 | - unset( $email_key, $val ); | |
| 2388 | 2085 | } |
| 2389 | 2086 | } |
| 2390 | 2087 | |
| 2391 | - /** | |
| 2392 | - * Migrate autoresponder to action | |
| 2393 | - * | |
| 2394 | - * @param array $form_options | |
| 2395 | - * @param int $form_id | |
| 2396 | - * @param array $notifications | |
| 2397 | - * | |
| 2398 | - * @return void | |
| 2399 | - */ | |
| 2400 | 2088 | private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) { |
| 2401 | - if ( empty( $form_options['auto_responder'] ) || empty( $form_options['ar_email_message'] ) ) { | |
| 2402 | - return; | |
| 2403 | - } | |
| 2089 | + if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) { | |
| 2090 | + // migrate autoresponder | |
| 2404 | 2091 | |
| 2405 | - // Migrate autoresponder | |
| 2092 | + $email_field = isset( $form_options['ar_email_to'] ) ? $form_options['ar_email_to'] : 0; | |
| 2093 | + if ( strpos( $email_field, '|' ) ) { | |
| 2094 | + // data from entries field | |
| 2095 | + $email_field = explode( '|', $email_field ); | |
| 2096 | + if ( isset( $email_field[1] ) ) { | |
| 2097 | + $email_field = $email_field[1]; | |
| 2098 | + } | |
| 2099 | + } | |
| 2100 | + if ( is_numeric( $email_field ) && ! empty( $email_field ) ) { | |
| 2101 | + $email_field = '[' . $email_field . ']'; | |
| 2102 | + } | |
| 2406 | 2103 | |
| 2407 | - $email_field = $form_options['ar_email_to'] ?? 0; | |
| 2104 | + $notification = $form_options; | |
| 2105 | + $new_notification2 = array( | |
| 2106 | + 'post_content' => array( | |
| 2107 | + 'email_message' => $notification['ar_email_message'], | |
| 2108 | + 'email_subject' => isset( $notification['ar_email_subject'] ) ? $notification['ar_email_subject'] : '', | |
| 2109 | + 'email_to' => $email_field, | |
| 2110 | + 'plain_text' => isset( $notification['ar_plain_text'] ) ? $notification['ar_plain_text'] : 0, | |
| 2111 | + 'inc_user_info' => 0, | |
| 2112 | + ), | |
| 2113 | + 'post_name' => $form_id . '_email_' . count( $notifications ), | |
| 2114 | + ); | |
| 2408 | 2115 | |
| 2409 | - if ( str_contains( $email_field, '|' ) ) { | |
| 2410 | - // Data from entries field | |
| 2411 | - $email_field = explode( '|', $email_field ); | |
| 2116 | + $reply_to = isset( $notification['ar_reply_to'] ) ? $notification['ar_reply_to'] : ''; | |
| 2117 | + $reply_to_name = isset( $notification['ar_reply_to_name'] ) ? $notification['ar_reply_to_name'] : ''; | |
| 2412 | 2118 | |
| 2413 | - if ( isset( $email_field[1] ) ) { | |
| 2414 | - $email_field = $email_field[1]; | |
| 2119 | + if ( ! empty( $reply_to ) ) { | |
| 2120 | + $new_notification2['post_content']['reply_to'] = $reply_to; | |
| 2415 | 2121 | } |
| 2416 | - } | |
| 2417 | 2122 | |
| 2418 | - if ( is_numeric( $email_field ) && $email_field ) { | |
| 2419 | - $email_field = '[' . $email_field . ']'; | |
| 2420 | - } | |
| 2123 | + if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) { | |
| 2124 | + $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>'; | |
| 2125 | + } | |
| 2421 | 2126 | |
| 2422 | - $notification = $form_options; | |
| 2423 | - $new_notification2 = array( | |
| 2424 | - 'post_content' => array( | |
| 2425 | - 'email_message' => $notification['ar_email_message'], | |
| 2426 | - 'email_subject' => $notification['ar_email_subject'] ?? '', | |
| 2427 | - 'email_to' => $email_field, | |
| 2428 | - 'plain_text' => $notification['ar_plain_text'] ?? 0, | |
| 2429 | - 'inc_user_info' => 0, | |
| 2430 | - ), | |
| 2431 | - 'post_name' => $form_id . '_email_' . count( $notifications ), | |
| 2432 | - ); | |
| 2433 | - | |
| 2434 | - $reply_to = $notification['ar_reply_to'] ?? ''; | |
| 2435 | - $reply_to_name = $notification['ar_reply_to_name'] ?? ''; | |
| 2436 | - | |
| 2437 | - if ( $reply_to ) { | |
| 2438 | - $new_notification2['post_content']['reply_to'] = $reply_to; | |
| 2439 | - } | |
| 2440 | - | |
| 2441 | - if ( $reply_to || $reply_to_name ) { | |
| 2442 | - $new_notification2['post_content']['from'] = ( $reply_to_name ? $reply_to_name : '[sitename]' ) . ' <' . ( $reply_to ? $reply_to : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 2443 | - } | |
| 2444 | - | |
| 2445 | - $notifications[] = $new_notification2; | |
| 2446 | - unset( $new_notification2 ); | |
| 2127 | + $notifications[] = $new_notification2; | |
| 2128 | + unset( $new_notification2 ); | |
| 2129 | + }//end if | |
| 2447 | 2130 | } |
| 2448 | 2131 | |
| 2449 | 2132 | /** |
| 2450 | 2133 | * PHP 8 backward compatibility for the libxml_disable_entity_loader function |
| @@ -2460,13 +2143,8 @@ | ||
| 2460 | 2143 | |
| 2461 | 2144 | return $loader; |
| 2462 | 2145 | } |
| 2463 | 2146 | |
| 2464 | - /** | |
| 2465 | - * PHP 8 backward compatibility for the libxml_disable_entity_loader function | |
| 2466 | - * | |
| 2467 | - * @return bool | |
| 2468 | - */ | |
| 2469 | 2147 | public static function check_if_libxml_disable_entity_loader_exists() { |
| 2470 | 2148 | return version_compare( phpversion(), '8.0', '<' ) && ! function_exists( 'libxml_disable_entity_loader' ); |
| 2471 | 2149 | } |
| 2472 | 2150 | |
| @@ -2479,13 +2157,11 @@ | ||
| 2479 | 2157 | * @return array |
| 2480 | 2158 | */ |
| 2481 | 2159 | public static function get_supported_upload_file_types() { |
| 2482 | 2160 | $file_types = array( '.xml' ); |
| 2483 | - | |
| 2484 | 2161 | if ( FrmAppHelper::pro_is_installed() ) { |
| 2485 | 2162 | // CSV Importing is only available in Pro. |
| 2486 | 2163 | $file_types[] = '.csv'; |
| 2487 | 2164 | } |
| 2488 | - | |
| 2489 | 2165 | return $file_types; |
| 2490 | 2166 | } |
| 2491 | 2167 | } |