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