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