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