| @@ -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,23 +75,21 @@ | ||
| 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 | - $imported['error'] = (string) $xml->Message; // phpcs:ignore WordPress.NamingConventions | |
| 91 | + $imported['error'] = reset( $xml->Message ); // phpcs:ignore WordPress.NamingConventions | |
| 151 | 92 | } |
| 152 | 93 | } |
| 153 | 94 | |
| 154 | 95 | return $imported; |
| @@ -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, |
| @@ -625,12 +442,8 @@ | ||
| 625 | 442 | } |
| 626 | 443 | |
| 627 | 444 | /** |
| 628 | 445 | * @since 4.06 |
| 629 | - * | |
| 630 | - * @param array $f | |
| 631 | - * | |
| 632 | - * @return void | |
| 633 | 446 | */ |
| 634 | 447 | private static function set_default_value( &$f ) { |
| 635 | 448 | $has_default = array( |
| 636 | 449 | 'text', |
| @@ -644,17 +457,15 @@ | ||
| 644 | 457 | 'password', |
| 645 | 458 | 'tag', |
| 646 | 459 | ); |
| 647 | 460 | |
| 648 | - if ( ! is_array( $f['default_value'] ) || ! in_array( $f['type'], $has_default, true ) ) { | |
| 649 | - 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 | + } | |
| 650 | 467 | } |
| 651 | - | |
| 652 | - if ( count( $f['default_value'] ) === 1 ) { | |
| 653 | - $f['default_value'] = '[' . reset( $f['default_value'] ) . ']'; | |
| 654 | - } else { | |
| 655 | - $f['default_value'] = reset( $f['default_value'] ); | |
| 656 | - } | |
| 657 | 468 | } |
| 658 | 469 | |
| 659 | 470 | /** |
| 660 | 471 | * Make sure the required indicator is set. |
| @@ -659,12 +470,8 @@ | ||
| 659 | 470 | /** |
| 660 | 471 | * Make sure the required indicator is set. |
| 661 | 472 | * |
| 662 | 473 | * @since 4.05 |
| 663 | - * | |
| 664 | - * @param array $f | |
| 665 | - * | |
| 666 | - * @return void | |
| 667 | 474 | */ |
| 668 | 475 | private static function maybe_add_required( &$f ) { |
| 669 | 476 | if ( $f['required'] && ! isset( $f['field_options']['required_indicator'] ) ) { |
| 670 | 477 | $f['field_options']['required_indicator'] = '*'; |
| @@ -674,17 +481,14 @@ | ||
| 674 | 481 | /** |
| 675 | 482 | * Update the current in_section value at the beginning of the field loop |
| 676 | 483 | * |
| 677 | 484 | * @since 2.0.25 |
| 678 | - * | |
| 679 | - * @param int $in_section | |
| 485 | + * @param int $in_section | |
| 680 | 486 | * @param array $f |
| 681 | - * | |
| 682 | - * @return void | |
| 683 | 487 | */ |
| 684 | 488 | private static function maybe_update_in_section_variable( &$in_section, &$f ) { |
| 685 | 489 | // If we're at the end of a section, switch $in_section is 0 |
| 686 | - if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ), true ) ) { | |
| 490 | + if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) { | |
| 687 | 491 | $in_section = 0; |
| 688 | 492 | } |
| 689 | 493 | |
| 690 | 494 | // Update the current field's in_section value |
| @@ -692,9 +496,9 @@ | ||
| 692 | 496 | $f['field_options']['in_section'] = $in_section; |
| 693 | 497 | } |
| 694 | 498 | |
| 695 | 499 | // If we're starting a new section, switch $in_section to ID of divider |
| 696 | - if ( $f['type'] === 'divider' ) { | |
| 500 | + if ( $f['type'] == 'divider' ) { | |
| 697 | 501 | $in_section = $f['id']; |
| 698 | 502 | } |
| 699 | 503 | } |
| 700 | 504 | |
| @@ -710,21 +514,16 @@ | ||
| 710 | 514 | if ( ! isset( $imported['forms'] ) ) { |
| 711 | 515 | return; |
| 712 | 516 | } |
| 713 | 517 | |
| 714 | - if ( $f['type'] !== 'form' && ( $f['type'] !== 'divider' || ! FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { | |
| 715 | - 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 | + } | |
| 716 | 525 | } |
| 717 | - | |
| 718 | - if ( ! FrmField::is_option_true( $f['field_options'], 'form_select' ) ) { | |
| 719 | - return; | |
| 720 | - } | |
| 721 | - | |
| 722 | - $form_select = (int) $f['field_options']['form_select']; | |
| 723 | - | |
| 724 | - if ( isset( $imported['forms'][ $form_select ] ) ) { | |
| 725 | - $f['field_options']['form_select'] = $imported['forms'][ $form_select ]; | |
| 726 | - } | |
| 727 | 526 | } |
| 728 | 527 | |
| 729 | 528 | /** |
| 730 | 529 | * Update the get_values_form setting if the form was imported |
| @@ -732,10 +531,8 @@ | ||
| 732 | 531 | * @since 2.01.0 |
| 733 | 532 | * |
| 734 | 533 | * @param array $imported |
| 735 | 534 | * @param array $f |
| 736 | - * | |
| 737 | - * @return void | |
| 738 | 535 | */ |
| 739 | 536 | private static function maybe_update_get_values_form_setting( $imported, &$f ) { |
| 740 | 537 | if ( ! isset( $imported['forms'] ) ) { |
| 741 | 538 | return; |
| @@ -740,35 +537,36 @@ | ||
| 740 | 537 | if ( ! isset( $imported['forms'] ) ) { |
| 741 | 538 | return; |
| 742 | 539 | } |
| 743 | 540 | |
| 744 | - if ( ! FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) { | |
| 745 | - 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 | + } | |
| 746 | 546 | } |
| 547 | + } | |
| 747 | 548 | |
| 748 | - $old_form = $f['field_options']['get_values_form']; | |
| 749 | - | |
| 750 | - if ( isset( $imported['forms'][ $old_form ] ) ) { | |
| 751 | - $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ]; | |
| 752 | - } | |
| 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 ); | |
| 753 | 557 | } |
| 754 | 558 | |
| 755 | 559 | /** |
| 756 | 560 | * @since 4.0 |
| 757 | - * | |
| 758 | - * @param array $f | |
| 759 | - * | |
| 760 | - * @return void | |
| 761 | 561 | */ |
| 762 | 562 | private static function migrate_placeholders( &$f ) { |
| 763 | 563 | $update_values = self::migrate_field_placeholder( $f, 'clear_on_focus' ); |
| 764 | - | |
| 765 | 564 | foreach ( $update_values as $k => $v ) { |
| 766 | 565 | $f[ $k ] = $v; |
| 767 | 566 | } |
| 768 | 567 | |
| 769 | 568 | $update_values = self::migrate_field_placeholder( $f, 'default_blank' ); |
| 770 | - | |
| 771 | 569 | foreach ( $update_values as $k => $v ) { |
| 772 | 570 | $f[ $k ] = $v; |
| 773 | 571 | } |
| 774 | 572 | } |
| @@ -777,18 +575,13 @@ | ||
| 777 | 575 | * Move clear_on_focus or default_blank to placeholder. |
| 778 | 576 | * Also called during database migration in FrmMigrate. |
| 779 | 577 | * |
| 780 | 578 | * @since 4.0 |
| 781 | - * | |
| 782 | - * @param array|object $field | |
| 783 | - * @param string $type | |
| 784 | - * | |
| 785 | 579 | * @return array |
| 786 | 580 | */ |
| 787 | 581 | public static function migrate_field_placeholder( $field, $type ) { |
| 788 | - $field = (array) $field; | |
| 582 | + $field = (array) $field; | |
| 789 | 583 | $field_options = $field['field_options']; |
| 790 | - | |
| 791 | 584 | if ( empty( $field_options[ $type ] ) || empty( $field['default_value'] ) ) { |
| 792 | 585 | return array(); |
| 793 | 586 | } |
| 794 | 587 | |
| @@ -801,34 +594,27 @@ | ||
| 801 | 594 | ); |
| 802 | 595 | |
| 803 | 596 | // If a dropdown placeholder was used, remove the option so it won't be included twice. |
| 804 | 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 | + } | |
| 805 | 603 | |
| 806 | - if ( $type !== 'default_blank' || ! is_array( $options ) ) { | |
| 807 | - return $changes; | |
| 808 | - } | |
| 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 | + } | |
| 809 | 608 | |
| 810 | - $default_value = $field['default_value']; | |
| 811 | - | |
| 812 | - if ( is_array( $default_value ) ) { | |
| 813 | - $default_value = reset( $default_value ); | |
| 814 | - } | |
| 815 | - | |
| 816 | - foreach ( $options as $opt_key => $opt ) { | |
| 817 | - if ( is_array( $opt ) ) { | |
| 818 | - $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt ); | |
| 609 | + if ( $opt == $default_value ) { | |
| 610 | + unset( $options[ $opt_key ] ); | |
| 611 | + break; | |
| 612 | + } | |
| 819 | 613 | } |
| 820 | - | |
| 821 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 822 | - if ( $opt == $default_value ) { | |
| 823 | - unset( $options[ $opt_key ] ); | |
| 824 | - break; | |
| 825 | - } | |
| 614 | + $changes['options'] = $options; | |
| 826 | 615 | } |
| 827 | 616 | |
| 828 | - $changes['options'] = $options; | |
| 829 | - // end if | |
| 830 | - | |
| 831 | 617 | return $changes; |
| 832 | 618 | } |
| 833 | 619 | |
| 834 | 620 | /** |
| @@ -837,72 +623,26 @@ | ||
| 837 | 623 | * @since 2.0.25 |
| 838 | 624 | * |
| 839 | 625 | * @param array $f |
| 840 | 626 | * @param array $imported |
| 841 | - * | |
| 842 | - * @return void | |
| 843 | 627 | */ |
| 844 | 628 | private static function create_imported_field( $f, &$imported ) { |
| 845 | - $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'] ); | |
| 846 | 631 | |
| 847 | - if ( is_callable( 'FrmProFileImport::import_attachment' ) ) { | |
| 848 | - $f = self::maybe_import_images_for_options( $f ); | |
| 849 | - } | |
| 850 | - | |
| 851 | 632 | $new_id = FrmField::create( $f ); |
| 852 | - | |
| 853 | - if ( ! $new_id ) { | |
| 854 | - return; | |
| 633 | + if ( $new_id != false ) { | |
| 634 | + $imported['imported']['fields'] ++; | |
| 635 | + do_action( 'frm_after_field_is_imported', $f, $new_id ); | |
| 855 | 636 | } |
| 856 | - | |
| 857 | - ++$imported['imported']['fields']; | |
| 858 | - do_action( 'frm_after_field_is_imported', $f, $new_id ); | |
| 859 | 637 | } |
| 860 | 638 | |
| 861 | 639 | /** |
| 862 | - * Import images for radio buttons and checkboxes from image src if available. | |
| 863 | - * | |
| 864 | - * @since 5.5.1 | |
| 865 | - * | |
| 866 | - * @param array $field | |
| 867 | - * | |
| 868 | - * @return array | |
| 869 | - */ | |
| 870 | - private static function maybe_import_images_for_options( $field ) { | |
| 871 | - if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) { | |
| 872 | - return $field; | |
| 873 | - } | |
| 874 | - | |
| 875 | - foreach ( $field['options'] as $key => $option ) { | |
| 876 | - if ( ! is_array( $option ) || empty( $option['src'] ) ) { | |
| 877 | - continue; | |
| 878 | - } | |
| 879 | - | |
| 880 | - $field_object = (object) $field; | |
| 881 | - // Fake the file type as FrmProImport::import_attachment checks for file type. | |
| 882 | - $field_object->type = 'file'; | |
| 883 | - | |
| 884 | - $image_id = FrmProFileImport::import_attachment( $option['src'], $field_object ); | |
| 885 | - // Remove the src from options as it isn't required after import. | |
| 886 | - unset( $field['options'][ $key ]['src'] ); | |
| 887 | - | |
| 888 | - if ( is_numeric( $image_id ) ) { | |
| 889 | - $field['options'][ $key ]['image'] = $image_id; | |
| 890 | - } | |
| 891 | - } | |
| 892 | - | |
| 893 | - return $field; | |
| 894 | - } | |
| 895 | - | |
| 896 | - /** | |
| 897 | 640 | * Fix field ids for fields that already exist prior to import. |
| 898 | 641 | * |
| 899 | 642 | * @since 4.07 |
| 900 | - * | |
| 901 | - * @param int $form_id | |
| 643 | + * @param int $form_id | |
| 902 | 644 | * @param array $keys_by_original_field_id |
| 903 | - * | |
| 904 | - * @return void | |
| 905 | 645 | */ |
| 906 | 646 | protected static function maybe_update_field_ids( $form_id, $keys_by_original_field_id ) { |
| 907 | 647 | global $frm_duplicate_ids; |
| 908 | 648 | |
| @@ -922,17 +662,15 @@ | ||
| 922 | 662 | $field = (array) $field; |
| 923 | 663 | $frm_duplicate_ids = $keys_by_original_field_id; |
| 924 | 664 | $after = FrmFieldsHelper::switch_field_ids( $field ); |
| 925 | 665 | |
| 926 | - if ( $before['field_options'] === $after['field_options'] ) { | |
| 927 | - continue; | |
| 928 | - } | |
| 666 | + if ( $before['field_options'] !== $after['field_options'] ) { | |
| 667 | + $frm_duplicate_ids = $field_id_by_key; | |
| 668 | + $after = FrmFieldsHelper::switch_field_ids( $after ); | |
| 929 | 669 | |
| 930 | - $frm_duplicate_ids = $field_id_by_key; | |
| 931 | - $after = FrmFieldsHelper::switch_field_ids( $after ); | |
| 932 | - | |
| 933 | - if ( $before['field_options'] !== $after['field_options'] ) { | |
| 934 | - FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) ); | |
| 670 | + if ( $before['field_options'] !== $after['field_options'] ) { | |
| 671 | + FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) ); | |
| 672 | + } | |
| 935 | 673 | } |
| 936 | 674 | } |
| 937 | 675 | |
| 938 | 676 | $frm_duplicate_ids = $former_duplicate_ids; |
| @@ -944,10 +682,8 @@ | ||
| 944 | 682 | * |
| 945 | 683 | * @since 2.0.19 |
| 946 | 684 | * |
| 947 | 685 | * @param array $form |
| 948 | - * | |
| 949 | - * @return void | |
| 950 | 686 | */ |
| 951 | 687 | private static function update_custom_style_setting_on_import( &$form ) { |
| 952 | 688 | if ( ! isset( $form['options']['custom_style'] ) ) { |
| 953 | 689 | return; |
| @@ -952,35 +688,32 @@ | ||
| 952 | 688 | if ( ! isset( $form['options']['custom_style'] ) ) { |
| 953 | 689 | return; |
| 954 | 690 | } |
| 955 | 691 | |
| 956 | - if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) { | |
| 692 | + if ( is_numeric( $form['options']['custom_style'] ) ) { | |
| 957 | 693 | // Set to default |
| 958 | 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 ); | |
| 959 | 705 | |
| 960 | - return; | |
| 961 | - } | |
| 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']; | |
| 962 | 711 | |
| 963 | - // Replace the style name with the style ID on import | |
| 964 | - global $wpdb; | |
| 965 | - $table = $wpdb->prefix . 'posts'; | |
| 966 | - $where = array( | |
| 967 | - 'post_name' => $form['options']['custom_style'], | |
| 968 | - 'post_type' => 'frm_styles', | |
| 969 | - ); | |
| 970 | - $select = 'ID'; | |
| 971 | - $style_id = FrmDb::get_var( $table, $where, $select ); | |
| 972 | - | |
| 973 | - if ( $style_id ) { | |
| 974 | - $form['options']['custom_style'] = $style_id; | |
| 975 | - return; | |
| 712 | + // Set to default | |
| 713 | + $form['options']['custom_style'] = 1; | |
| 714 | + } | |
| 976 | 715 | } |
| 977 | - | |
| 978 | - // Save the old style to maybe update after styles import | |
| 979 | - $form['options']['old_style'] = $form['options']['custom_style']; | |
| 980 | - | |
| 981 | - // Set to default | |
| 982 | - $form['options']['custom_style'] = 1; | |
| 983 | 716 | } |
| 984 | 717 | |
| 985 | 718 | /** |
| 986 | 719 | * After styles are imported, check for any forms that were linked |
| @@ -986,40 +719,25 @@ | ||
| 986 | 719 | * After styles are imported, check for any forms that were linked |
| 987 | 720 | * and link them back up. |
| 988 | 721 | * |
| 989 | 722 | * @since 2.2.7 |
| 990 | - * | |
| 991 | - * @param int|string $form_id | |
| 992 | - * | |
| 993 | - * @return void | |
| 994 | 723 | */ |
| 995 | 724 | private static function update_custom_style_setting_after_import( $form_id ) { |
| 996 | 725 | $form = FrmForm::getOne( $form_id ); |
| 997 | 726 | |
| 998 | - if ( ! $form || ! isset( $form->options['old_style'] ) ) { | |
| 999 | - 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 | + } | |
| 1000 | 736 | } |
| 1001 | - | |
| 1002 | - $form = (array) $form; | |
| 1003 | - $saved_style = $form['options']['custom_style']; | |
| 1004 | - $form['options']['custom_style'] = $form['options']['old_style']; | |
| 1005 | - self::update_custom_style_setting_on_import( $form ); | |
| 1006 | - $has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 1007 | - | |
| 1008 | - if ( $has_changed ) { | |
| 1009 | - FrmForm::update( $form['id'], $form ); | |
| 1010 | - } | |
| 1011 | 737 | } |
| 1012 | 738 | |
| 1013 | - /** | |
| 1014 | - * Handle import for all posts. This includes views, styles, pages, and form actions. | |
| 1015 | - * | |
| 1016 | - * @param SimpleXMLElement $views | |
| 1017 | - * @param array $imported | |
| 1018 | - * | |
| 1019 | - * @return array | |
| 1020 | - */ | |
| 1021 | - public static function import_xml_views( $views, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 739 | + public static function import_xml_views( $views, $imported ) { | |
| 1022 | 740 | $imported['posts'] = array(); |
| 1023 | 741 | $form_action_type = FrmFormActionsController::$action_post_type; |
| 1024 | 742 | |
| 1025 | 743 | $post_types = array( |
| @@ -1027,11 +745,8 @@ | ||
| 1027 | 745 | $form_action_type => 'actions', |
| 1028 | 746 | 'frm_styles' => 'styles', |
| 1029 | 747 | ); |
| 1030 | 748 | |
| 1031 | - $view_ids = array(); | |
| 1032 | - $posts_with_shortcodes = array(); | |
| 1033 | - | |
| 1034 | 749 | foreach ( $views as $item ) { |
| 1035 | 750 | $post = array( |
| 1036 | 751 | 'post_title' => (string) $item->title, |
| 1037 | 752 | 'post_name' => (string) $item->post_name, |
| @@ -1054,15 +769,8 @@ | ||
| 1054 | 769 | 'layout' => array(), |
| 1055 | 770 | 'tax_input' => array(), |
| 1056 | 771 | ); |
| 1057 | 772 | |
| 1058 | - $post['post_content'] = self::switch_form_ids( $post['post_content'], $imported['forms'] ); | |
| 1059 | - | |
| 1060 | - // Fix issue with line breaks appearing in descriptions as "rn". | |
| 1061 | - if ( $post['post_type'] === $form_action_type ) { | |
| 1062 | - $post['post_content'] = str_replace( '\\\\r\\\\n', '\\r\\n', $post['post_content'] ); | |
| 1063 | - } | |
| 1064 | - | |
| 1065 | 773 | $old_id = $post['post_id']; |
| 1066 | 774 | self::populate_post( $post, $item, $imported ); |
| 1067 | 775 | |
| 1068 | 776 | unset( $item ); |
| @@ -1067,246 +775,85 @@ | ||
| 1067 | 775 | |
| 1068 | 776 | unset( $item ); |
| 1069 | 777 | |
| 1070 | 778 | $post_id = false; |
| 1071 | - | |
| 1072 | - if ( $post['post_type'] === $form_action_type ) { | |
| 779 | + if ( $post['post_type'] == $form_action_type ) { | |
| 1073 | 780 | $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] ); |
| 1074 | - | |
| 1075 | - if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) { | |
| 781 | + if ( $action_control && is_object( $action_control ) ) { | |
| 1076 | 782 | $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] ); |
| 1077 | 783 | } |
| 1078 | 784 | unset( $action_control ); |
| 1079 | - } elseif ( $post['post_type'] === 'frm_styles' ) { | |
| 785 | + } elseif ( $post['post_type'] == 'frm_styles' ) { | |
| 1080 | 786 | // Properly encode post content before inserting the post |
| 1081 | 787 | $post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] ); |
| 788 | + $custom_css = isset( $post['post_content']['custom_css'] ) ? $post['post_content']['custom_css'] : ''; | |
| 1082 | 789 | $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] ); |
| 1083 | - // Store template data additionally to a postmeta that can be used to revert the style to default template style. | |
| 1084 | - $post['postmeta']['frm_style_default'] = $post['post_content']; | |
| 1085 | 790 | |
| 1086 | 791 | // Create/update post now |
| 1087 | 792 | $post_id = wp_insert_post( $post ); |
| 793 | + self::maybe_update_custom_css( $custom_css ); | |
| 1088 | 794 | } else { |
| 1089 | 795 | if ( $post['post_type'] === 'frm_display' ) { |
| 1090 | 796 | $post['post_content'] = self::maybe_prepare_json_view_content( $post['post_content'] ); |
| 1091 | - } elseif ( 'page' === $post['post_type'] && isset( $imported['posts'][ $post['post_parent'] ] ) ) { | |
| 1092 | - $post['post_parent'] = $imported['posts'][ $post['post_parent'] ]; | |
| 1093 | 797 | } |
| 1094 | 798 | // Create/update post now |
| 1095 | 799 | $post_id = wp_insert_post( $post ); |
| 1096 | - }//end if | |
| 800 | + } | |
| 1097 | 801 | |
| 1098 | 802 | if ( ! is_numeric( $post_id ) ) { |
| 1099 | 803 | continue; |
| 1100 | 804 | } |
| 1101 | 805 | |
| 1102 | - if ( str_contains( $post['post_content'], '[display-frm-data' ) || str_contains( $post['post_content'], '[formidable' ) ) { | |
| 1103 | - $posts_with_shortcodes[ $post_id ] = $post; | |
| 1104 | - } | |
| 1105 | - | |
| 1106 | 806 | self::update_postmeta( $post, $post_id ); |
| 1107 | 807 | self::update_layout( $post, $post_id ); |
| 1108 | 808 | |
| 1109 | 809 | $this_type = 'posts'; |
| 1110 | - | |
| 1111 | 810 | if ( isset( $post_types[ $post['post_type'] ] ) ) { |
| 1112 | 811 | $this_type = $post_types[ $post['post_type'] ]; |
| 1113 | 812 | } |
| 1114 | 813 | |
| 1115 | - if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) { | |
| 1116 | - ++$imported['updated'][ $this_type ]; | |
| 814 | + if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) { | |
| 815 | + $imported['updated'][ $this_type ] ++; | |
| 1117 | 816 | } else { |
| 1118 | - ++$imported['imported'][ $this_type ]; | |
| 817 | + $imported['imported'][ $this_type ] ++; | |
| 1119 | 818 | } |
| 1120 | 819 | |
| 1121 | - $imported['posts'][ $old_id ] = $post_id; | |
| 820 | + $imported['posts'][ (int) $old_id ] = $post_id; | |
| 1122 | 821 | |
| 1123 | - if ( $post['post_type'] === 'frm_display' ) { | |
| 1124 | - $view_ids[ $old_id ] = $post_id; | |
| 1125 | - } | |
| 1126 | - | |
| 1127 | 822 | do_action( 'frm_after_import_view', $post_id, $post ); |
| 1128 | 823 | |
| 1129 | 824 | unset( $post ); |
| 1130 | - }//end foreach | |
| 1131 | - | |
| 1132 | - if ( $posts_with_shortcodes && $view_ids ) { | |
| 1133 | - self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ); | |
| 1134 | 825 | } |
| 1135 | - unset( $posts_with_shortcodes, $view_ids ); | |
| 1136 | 826 | |
| 1137 | - if ( ! empty( $imported['forms'] ) ) { | |
| 1138 | - // Clear imported forms style cache to make sure the new styles are applied to the forms | |
| 1139 | - self::clear_forms_style_caches( $imported['forms'] ); | |
| 1140 | - } | |
| 1141 | - | |
| 1142 | 827 | self::maybe_update_stylesheet( $imported ); |
| 1143 | 828 | |
| 1144 | - flush_rewrite_rules(); | |
| 1145 | - | |
| 1146 | 829 | return $imported; |
| 1147 | 830 | } |
| 1148 | 831 | |
| 1149 | 832 | /** |
| 1150 | - * Clears styles from cache for imported forms | |
| 1151 | - * | |
| 1152 | - * @param array $imported_forms | |
| 1153 | - * | |
| 1154 | - * @return void | |
| 1155 | - */ | |
| 1156 | - private static function clear_forms_style_caches( $imported_forms ) { | |
| 1157 | - $where = array( | |
| 1158 | - 'id' => $imported_forms, | |
| 1159 | - 'options LIKE' => '"old_style"', | |
| 1160 | - ); | |
| 1161 | - $forms = FrmDb::get_results( 'frm_forms', $where ); | |
| 1162 | - | |
| 1163 | - foreach ( $forms as $form ) { | |
| 1164 | - FrmAppHelper::unserialize_or_decode( $form->options ); | |
| 1165 | - | |
| 1166 | - if ( ! $form->options ) { | |
| 1167 | - continue; | |
| 1168 | - } | |
| 1169 | - | |
| 1170 | - $where = array( | |
| 1171 | - 'post_name' => $form->options['old_style'], | |
| 1172 | - 'post_type' => FrmStylesController::$post_type, | |
| 1173 | - ); | |
| 1174 | - | |
| 1175 | - $select = 'ID'; | |
| 1176 | - $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' ); | |
| 1177 | - FrmDb::delete_cache_and_transient( $cache_key, 'post' ); | |
| 1178 | - } | |
| 1179 | - } | |
| 1180 | - | |
| 1181 | - /** | |
| 1182 | - * Replace old form ids with new ones in a string. | |
| 1183 | - * | |
| 1184 | - * @param string $string | |
| 1185 | - * @param array<int> $form_ids new form ids indexed by old form id. | |
| 1186 | - * | |
| 1187 | - * @return string | |
| 1188 | - */ | |
| 1189 | - private static function switch_form_ids( $string, $form_ids ) { | |
| 1190 | - if ( ! str_contains( $string, '[formidable' ) ) { | |
| 1191 | - // Skip string replacing if there are no form shortcodes in string. | |
| 1192 | - return $string; | |
| 1193 | - } | |
| 1194 | - | |
| 1195 | - foreach ( $form_ids as $old_id => $new_id ) { | |
| 1196 | - $string = str_replace( | |
| 1197 | - array( | |
| 1198 | - '[formidable id="' . $old_id . '"', | |
| 1199 | - '[formidable id=' . $old_id . ']', | |
| 1200 | - '[formidable id=' . $old_id . ' ', | |
| 1201 | - '"formId":"' . $old_id . '"', | |
| 1202 | - ), | |
| 1203 | - array( | |
| 1204 | - '[formidable id="' . $new_id . '"', | |
| 1205 | - '[formidable id=' . $new_id . ']', | |
| 1206 | - '[formidable id=' . $new_id . ' ', | |
| 1207 | - '"formId":"' . $new_id . '"', | |
| 1208 | - ), | |
| 1209 | - $string | |
| 1210 | - ); | |
| 1211 | - } | |
| 1212 | - | |
| 1213 | - return $string; | |
| 1214 | - } | |
| 1215 | - | |
| 1216 | - /** | |
| 1217 | - * @param array<array> $posts_with_shortcodes indexed by current post id. | |
| 1218 | - * @param array<int> $view_ids new view ids indexed by old view id. | |
| 1219 | - * | |
| 1220 | - * @return void | |
| 1221 | - */ | |
| 1222 | - private static function maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ) { | |
| 1223 | - foreach ( $posts_with_shortcodes as $imported_post_id => $post ) { | |
| 1224 | - $post_content = self::switch_view_ids( $post['post_content'], $view_ids ); | |
| 1225 | - | |
| 1226 | - if ( $post_content === $post['post_content'] ) { | |
| 1227 | - continue; | |
| 1228 | - } | |
| 1229 | - | |
| 1230 | - wp_update_post( | |
| 1231 | - array( | |
| 1232 | - 'ID' => $imported_post_id, | |
| 1233 | - 'post_content' => $post_content, | |
| 1234 | - ) | |
| 1235 | - ); | |
| 1236 | - } | |
| 1237 | - } | |
| 1238 | - | |
| 1239 | - /** | |
| 1240 | - * Replace old view ids with new ones in a string. | |
| 1241 | - * | |
| 1242 | - * @param string $string | |
| 1243 | - * @param array<int> $view_ids new view ids indexed by old view id. | |
| 1244 | - * | |
| 1245 | - * @return string | |
| 1246 | - */ | |
| 1247 | - private static function switch_view_ids( $string, $view_ids ) { | |
| 1248 | - if ( ! str_contains( $string, '[display-frm-data' ) ) { | |
| 1249 | - // Skip string replacing if there are no view shortcodes in string. | |
| 1250 | - return $string; | |
| 1251 | - } | |
| 1252 | - | |
| 1253 | - foreach ( $view_ids as $old_id => $new_id ) { | |
| 1254 | - $string = str_replace( | |
| 1255 | - array( | |
| 1256 | - '[display-frm-data id="' . $old_id . '"', | |
| 1257 | - '[display-frm-data id=' . $old_id . ']', | |
| 1258 | - '[display-frm-data id=' . $old_id . ' ', | |
| 1259 | - '"viewId":"' . $old_id . '"', | |
| 1260 | - ), | |
| 1261 | - array( | |
| 1262 | - '[display-frm-data id="' . $new_id . '"', | |
| 1263 | - '[display-frm-data id=' . $new_id . ']', | |
| 1264 | - '[display-frm-data id=' . $new_id . ' ', | |
| 1265 | - '"viewId":"' . $new_id . '"', | |
| 1266 | - ), | |
| 1267 | - $string | |
| 1268 | - ); | |
| 1269 | - unset( $old_id, $new_id ); | |
| 1270 | - } | |
| 1271 | - | |
| 1272 | - return $string; | |
| 1273 | - } | |
| 1274 | - | |
| 1275 | - /** | |
| 1276 | 833 | * @param string $content |
| 1277 | - * | |
| 1278 | 834 | * @return string |
| 1279 | 835 | */ |
| 1280 | 836 | private static function maybe_prepare_json_view_content( $content ) { |
| 1281 | 837 | $maybe_decoded = FrmAppHelper::maybe_json_decode( $content ); |
| 1282 | - | |
| 1283 | 838 | if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) { |
| 1284 | 839 | return FrmAppHelper::prepare_and_encode( $maybe_decoded ); |
| 1285 | 840 | } |
| 1286 | - | |
| 1287 | 841 | return $content; |
| 1288 | 842 | } |
| 1289 | 843 | |
| 1290 | - /** | |
| 1291 | - * @param array $post | |
| 1292 | - * @param object $item | |
| 1293 | - * @param array $imported | |
| 1294 | - * | |
| 1295 | - * @return void | |
| 1296 | - */ | |
| 1297 | 844 | private static function populate_post( &$post, $item, $imported ) { |
| 1298 | 845 | if ( isset( $item->attachment_url ) ) { |
| 1299 | 846 | $post['attachment_url'] = (string) $item->attachment_url; |
| 1300 | 847 | } |
| 1301 | 848 | |
| 1302 | - if ( $post['post_type'] === FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { | |
| 1303 | - // Update to new form id | |
| 849 | + if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { | |
| 850 | + // update to new form id | |
| 1304 | 851 | $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ]; |
| 1305 | 852 | } |
| 1306 | 853 | |
| 1307 | 854 | // Don't allow default styles to take over a site's default style |
| 1308 | - if ( 'frm_styles' === $post['post_type'] ) { | |
| 855 | + if ( 'frm_styles' == $post['post_type'] ) { | |
| 1309 | 856 | $post['menu_order'] = 0; |
| 1310 | 857 | } |
| 1311 | 858 | |
| 1312 | 859 | foreach ( $item->postmeta as $meta ) { |
| @@ -1328,9 +875,9 @@ | ||
| 1328 | 875 | * @param array $post |
| 1329 | 876 | * @param stdClass $meta |
| 1330 | 877 | * @param array $imported |
| 1331 | 878 | */ |
| 1332 | - private static function populate_postmeta( &$post, $meta, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 879 | + private static function populate_postmeta( &$post, $meta, $imported ) { | |
| 1333 | 880 | global $frm_duplicate_ids; |
| 1334 | 881 | |
| 1335 | 882 | $m = array( |
| 1336 | 883 | 'key' => (string) $meta->meta_key, |
| @@ -1336,19 +883,20 @@ | ||
| 1336 | 883 | 'key' => (string) $meta->meta_key, |
| 1337 | 884 | 'value' => (string) $meta->meta_value, |
| 1338 | 885 | ); |
| 1339 | 886 | |
| 1340 | - // Switch old form and field ids to new ones. | |
| 887 | + //switch old form and field ids to new ones | |
| 1341 | 888 | if ( 'frm_form_id' === $m['key'] && isset( $imported['forms'][ (int) $m['value'] ] ) ) { |
| 1342 | 889 | $m['value'] = $imported['forms'][ (int) $m['value'] ]; |
| 1343 | 890 | } else { |
| 1344 | 891 | $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] ); |
| 1345 | 892 | |
| 1346 | - if ( $frm_duplicate_ids ) { | |
| 893 | + if ( ! empty( $frm_duplicate_ids ) ) { | |
| 1347 | 894 | if ( 'frm_dyncontent' === $m['key'] ) { |
| 1348 | 895 | $m['value'] = self::maybe_prepare_json_view_content( $m['value'] ); |
| 1349 | 896 | $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] ); |
| 1350 | 897 | } elseif ( 'frm_options' === $m['key'] ) { |
| 898 | + | |
| 1351 | 899 | foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) { |
| 1352 | 900 | if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) { |
| 1353 | 901 | $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
| 1354 | 902 | } |
| @@ -1353,38 +901,10 @@ | ||
| 1353 | 901 | $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
| 1354 | 902 | } |
| 1355 | 903 | } |
| 1356 | 904 | |
| 1357 | - if ( ! empty( $m['value']['map_address_fields'] ) ) { | |
| 1358 | - foreach ( $m['value']['map_address_fields'] as $address_field_key => $address_field_id ) { | |
| 1359 | - if ( isset( $frm_duplicate_ids[ $address_field_id ] ) ) { | |
| 1360 | - $m['value']['map_address_fields'][ $address_field_key ] = $frm_duplicate_ids[ $address_field_id ]; | |
| 1361 | - } | |
| 1362 | - } | |
| 1363 | - } | |
| 1364 | - | |
| 1365 | - if ( ! empty( $m['value']['calendar_options'] ) ) { | |
| 1366 | - foreach ( $m['value']['calendar_options'] as $calendar_option_group_key => $calendar_option ) { | |
| 1367 | - if ( isset( $frm_duplicate_ids[ $calendar_option['value'] ] ) ) { | |
| 1368 | - $m['value']['calendar_options'][ $calendar_option_group_key ]['value'] = $frm_duplicate_ids[ $calendar_option['value'] ]; | |
| 1369 | - } | |
| 1370 | - } | |
| 1371 | - } | |
| 1372 | - | |
| 1373 | - if ( ! empty( $m['value']['timeline_options'] ) ) { | |
| 1374 | - foreach ( $m['value']['timeline_options'] as $timeline_option_group_key => $timeline_group_option ) { | |
| 1375 | - foreach ( $timeline_group_option as $timeline_option_key => $timeline_option ) { | |
| 1376 | - // @mago-expect lint:excessive-nesting | |
| 1377 | - if ( isset( $frm_duplicate_ids[ $timeline_option ] ) ) { | |
| 1378 | - $m['value']['timeline_options'][ $timeline_option_group_key ][ $timeline_option_key ] = $frm_duplicate_ids[ $timeline_option ]; | |
| 1379 | - } | |
| 1380 | - } | |
| 1381 | - } | |
| 1382 | - } | |
| 1383 | - | |
| 1384 | 905 | $check_dup_array = array(); |
| 1385 | - | |
| 1386 | - if ( ! empty( $m['value']['order_by'] ) ) { | |
| 906 | + if ( isset( $m['value']['order_by'] ) && ! empty( $m['value']['order_by'] ) ) { | |
| 1387 | 907 | if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) { |
| 1388 | 908 | $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ]; |
| 1389 | 909 | } elseif ( is_array( $m['value']['order_by'] ) ) { |
| 1390 | 910 | $check_dup_array[] = 'order_by'; |
| @@ -1390,9 +910,9 @@ | ||
| 1390 | 910 | $check_dup_array[] = 'order_by'; |
| 1391 | 911 | } |
| 1392 | 912 | } |
| 1393 | 913 | |
| 1394 | - if ( ! empty( $m['value']['where'] ) ) { | |
| 914 | + if ( isset( $m['value']['where'] ) && ! empty( $m['value']['where'] ) ) { | |
| 1395 | 915 | $check_dup_array[] = 'where'; |
| 1396 | 916 | } |
| 1397 | 917 | |
| 1398 | 918 | foreach ( $check_dup_array as $check_k ) { |
| @@ -1402,11 +922,11 @@ | ||
| 1402 | 922 | } |
| 1403 | 923 | unset( $mk, $mv ); |
| 1404 | 924 | } |
| 1405 | 925 | } |
| 1406 | - }//end if | |
| 1407 | - }//end if | |
| 1408 | - }//end if | |
| 926 | + } | |
| 927 | + } | |
| 928 | + } | |
| 1409 | 929 | |
| 1410 | 930 | if ( ! is_array( $m['value'] ) ) { |
| 1411 | 931 | $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] ); |
| 1412 | 932 | } |
| @@ -1413,14 +933,8 @@ | ||
| 1413 | 933 | |
| 1414 | 934 | $post['postmeta'][ (string) $meta->meta_key ] = $m['value']; |
| 1415 | 935 | } |
| 1416 | 936 | |
| 1417 | - /** | |
| 1418 | - * @param array $post | |
| 1419 | - * @param object $layout | |
| 1420 | - * | |
| 1421 | - * @return void | |
| 1422 | - */ | |
| 1423 | 937 | private static function populate_layout( &$post, $layout ) { |
| 1424 | 938 | $post['layout'][ (string) $layout->type ] = (string) $layout->data; |
| 1425 | 939 | } |
| 1426 | 940 | |
| @@ -1426,25 +940,22 @@ | ||
| 1426 | 940 | |
| 1427 | 941 | /** |
| 1428 | 942 | * Add terms to post |
| 1429 | 943 | * |
| 1430 | - * @param array $post By reference. | |
| 1431 | - * @param object $item The XML object data. | |
| 944 | + * @param array $post by reference | |
| 945 | + * @param object $item The XML object data | |
| 1432 | 946 | */ |
| 1433 | 947 | private static function populate_taxonomies( &$post, $item ) { |
| 1434 | 948 | foreach ( $item->category as $c ) { |
| 1435 | 949 | $att = $c->attributes(); |
| 1436 | - | |
| 1437 | 950 | if ( ! isset( $att['nicename'] ) ) { |
| 1438 | 951 | continue; |
| 1439 | 952 | } |
| 1440 | 953 | |
| 1441 | 954 | $taxonomy = (string) $att['domain']; |
| 1442 | - | |
| 1443 | 955 | if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 1444 | 956 | $name = (string) $att['nicename']; |
| 1445 | 957 | $h_term = get_term_by( 'slug', $name, $taxonomy ); |
| 1446 | - | |
| 1447 | 958 | if ( $h_term ) { |
| 1448 | 959 | $name = $h_term->term_id; |
| 1449 | 960 | } |
| 1450 | 961 | unset( $h_term ); |
| @@ -1457,17 +968,13 @@ | ||
| 1457 | 968 | } |
| 1458 | 969 | |
| 1459 | 970 | $post['tax_input'][ $taxonomy ][] = $name; |
| 1460 | 971 | unset( $name ); |
| 1461 | - }//end foreach | |
| 972 | + } | |
| 1462 | 973 | } |
| 1463 | 974 | |
| 1464 | 975 | /** |
| 1465 | - * Edit post if the key and created time match. | |
| 1466 | - * | |
| 1467 | - * @param array $post By reference. | |
| 1468 | - * | |
| 1469 | - * @return void | |
| 976 | + * Edit post if the key and created time match | |
| 1470 | 977 | */ |
| 1471 | 978 | private static function maybe_editing_post( &$post ) { |
| 1472 | 979 | $match_by = array( |
| 1473 | 980 | 'post_type' => $post['post_type'], |
| @@ -1475,9 +982,9 @@ | ||
| 1475 | 982 | 'post_status' => $post['post_status'], |
| 1476 | 983 | 'posts_per_page' => 1, |
| 1477 | 984 | ); |
| 1478 | 985 | |
| 1479 | - if ( in_array( $post['post_status'], array( 'trash', 'draft' ), true ) ) { | |
| 986 | + if ( in_array( $post['post_status'], array( 'trash', 'draft' ) ) ) { | |
| 1480 | 987 | $match_by['include'] = $post['post_id']; |
| 1481 | 988 | unset( $match_by['name'] ); |
| 1482 | 989 | } |
| 1483 | 990 | |
| @@ -1482,50 +989,32 @@ | ||
| 1482 | 989 | } |
| 1483 | 990 | |
| 1484 | 991 | $editing = get_posts( $match_by ); |
| 1485 | 992 | |
| 1486 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1487 | - if ( $editing && current( $editing )->post_date == $post['post_date'] ) { | |
| 1488 | - // Set the id of the post to edit | |
| 993 | + if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) { | |
| 994 | + // set the id of the post to edit | |
| 1489 | 995 | $post['ID'] = current( $editing )->ID; |
| 1490 | 996 | } |
| 1491 | 997 | } |
| 1492 | 998 | |
| 1493 | - /** | |
| 1494 | - * @param array $post | |
| 1495 | - * @param int $post_id | |
| 1496 | - * | |
| 1497 | - * @return void | |
| 1498 | - */ | |
| 1499 | 999 | private static function update_postmeta( &$post, $post_id ) { |
| 1500 | 1000 | foreach ( $post['postmeta'] as $k => $v ) { |
| 1501 | - switch ( $k ) { | |
| 1502 | - case '_edit_last': | |
| 1503 | - $v = FrmAppHelper::get_user_id_param( $v ); | |
| 1504 | - break; | |
| 1001 | + if ( '_edit_last' == $k ) { | |
| 1002 | + $v = FrmAppHelper::get_user_id_param( $v ); | |
| 1003 | + } elseif ( '_thumbnail_id' == $k && FrmAppHelper::pro_is_installed() ) { | |
| 1004 | + // Change the attachment ID. | |
| 1005 | + $field_obj = FrmFieldFactory::get_field_type( 'file' ); | |
| 1006 | + $v = $field_obj->get_file_id( $v ); | |
| 1007 | + } | |
| 1505 | 1008 | |
| 1506 | - case '_thumbnail_id': | |
| 1507 | - if ( FrmAppHelper::pro_is_installed() ) { | |
| 1508 | - // Change the attachment ID. | |
| 1509 | - $field_obj = FrmFieldFactory::get_field_type( 'file' ); | |
| 1510 | - $v = $field_obj->get_file_id( $v ); | |
| 1511 | - } | |
| 1512 | - break; | |
| 1009 | + if ( 'frm_dyncontent' === $k && is_array( $v ) ) { | |
| 1010 | + $v = json_encode( $v ); | |
| 1011 | + } | |
| 1513 | 1012 | |
| 1514 | - case 'frm_dyncontent': | |
| 1515 | - if ( is_array( $v ) ) { | |
| 1516 | - $v = json_encode( $v ); | |
| 1517 | - } | |
| 1518 | - break; | |
| 1013 | + update_post_meta( $post_id, $k, $v ); | |
| 1519 | 1014 | |
| 1520 | - case 'frm_param': | |
| 1521 | - add_rewrite_endpoint( $v, EP_PERMALINK | EP_PAGES ); | |
| 1522 | - break; | |
| 1523 | - }//end switch | |
| 1524 | - | |
| 1525 | - update_post_meta( $post_id, $k, $v ); | |
| 1526 | 1015 | unset( $k, $v ); |
| 1527 | - }//end foreach | |
| 1016 | + } | |
| 1528 | 1017 | } |
| 1529 | 1018 | |
| 1530 | 1019 | /** |
| 1531 | 1020 | * @param array $post |
| @@ -1531,83 +1020,61 @@ | ||
| 1531 | 1020 | * @param array $post |
| 1532 | 1021 | * @param int $post_id |
| 1533 | 1022 | */ |
| 1534 | 1023 | private static function update_layout( &$post, $post_id ) { |
| 1535 | - if ( ! is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) { | |
| 1536 | - return; | |
| 1024 | + if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) { | |
| 1025 | + $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array(); | |
| 1026 | + $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array(); | |
| 1027 | + if ( $listing_layout || $detail_layout ) { | |
| 1028 | + FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout ); | |
| 1029 | + } | |
| 1537 | 1030 | } |
| 1538 | - | |
| 1539 | - $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array(); | |
| 1540 | - $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array(); | |
| 1541 | - | |
| 1542 | - if ( $listing_layout || $detail_layout ) { | |
| 1543 | - FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout ); | |
| 1544 | - } | |
| 1545 | 1031 | } |
| 1546 | 1032 | |
| 1547 | 1033 | /** |
| 1548 | - * @param array $imported | |
| 1034 | + * If a template includes custom css, let's include it. | |
| 1035 | + * The custom css is included on the default style. | |
| 1549 | 1036 | * |
| 1550 | - * @return void | |
| 1037 | + * @since 2.03 | |
| 1551 | 1038 | */ |
| 1552 | - private static function maybe_update_stylesheet( $imported ) { | |
| 1553 | - $new_styles = ! empty( $imported['imported']['styles'] ); | |
| 1554 | - $updated_styles = ! empty( $imported['updated']['styles'] ); | |
| 1555 | - | |
| 1556 | - if ( ! $new_styles && ! $updated_styles ) { | |
| 1039 | + private static function maybe_update_custom_css( $custom_css ) { | |
| 1040 | + if ( empty( $custom_css ) ) { | |
| 1557 | 1041 | return; |
| 1558 | 1042 | } |
| 1559 | 1043 | |
| 1560 | - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { | |
| 1561 | - $frm_style = new FrmStyle(); | |
| 1562 | - $frm_style->update( 'default' ); | |
| 1563 | - } | |
| 1044 | + $frm_style = new FrmStyle(); | |
| 1045 | + $default_style = $frm_style->get_default_style(); | |
| 1046 | + $default_style->post_content['custom_css'] .= "\r\n\r\n" . $custom_css; | |
| 1047 | + $frm_style->save( $default_style ); | |
| 1048 | + } | |
| 1564 | 1049 | |
| 1565 | - foreach ( $imported['forms'] as $form_id ) { | |
| 1566 | - self::update_custom_style_setting_after_import( $form_id ); | |
| 1050 | + private static function maybe_update_stylesheet( $imported ) { | |
| 1051 | + $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] ); | |
| 1052 | + $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] ); | |
| 1053 | + if ( $new_styles || $updated_styles ) { | |
| 1054 | + if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { | |
| 1055 | + $frm_style = new FrmStyle(); | |
| 1056 | + $frm_style->update( 'default' ); | |
| 1057 | + } | |
| 1058 | + foreach ( $imported['forms'] as $form_id ) { | |
| 1059 | + self::update_custom_style_setting_after_import( $form_id ); | |
| 1060 | + } | |
| 1567 | 1061 | } |
| 1568 | 1062 | } |
| 1569 | 1063 | |
| 1570 | 1064 | /** |
| 1571 | - * @param mixed $result | |
| 1572 | 1065 | * @param string $message |
| 1573 | - * @param array $errors | |
| 1574 | - * | |
| 1575 | - * @return void | |
| 1576 | 1066 | */ |
| 1577 | 1067 | public static function parse_message( $result, &$message, &$errors ) { |
| 1578 | 1068 | if ( is_wp_error( $result ) ) { |
| 1579 | 1069 | $errors[] = $result->get_error_message(); |
| 1580 | - | |
| 1581 | - // Remove the SimpleXML_parse_error from the WP_Error object to avoid | |
| 1582 | - // displaying duplicate error messages from $result->get_error_message() | |
| 1583 | - $error_details = array(); | |
| 1584 | - | |
| 1585 | - foreach ( $result->get_error_codes() as $error_code ) { | |
| 1586 | - // Clone WP_Error data because WP_Error removes all error messages and data | |
| 1587 | - // Associated with the specified error code when an item is removed. | |
| 1588 | - // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source | |
| 1589 | - $error_details = $result->get_error_data( $error_code ); | |
| 1590 | - | |
| 1591 | - if ( $error_code === 'SimpleXML_parse_error' ) { | |
| 1592 | - $result->remove( $error_code ); | |
| 1593 | - break; | |
| 1594 | - } | |
| 1595 | - } | |
| 1596 | - | |
| 1597 | - if ( $error_details ) { | |
| 1598 | - $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) ); | |
| 1599 | - } | |
| 1600 | - | |
| 1070 | + } elseif ( ! $result ) { | |
| 1601 | 1071 | return; |
| 1602 | - }//end if | |
| 1603 | - | |
| 1604 | - if ( ! $result ) { | |
| 1605 | - return; | |
| 1606 | 1072 | } |
| 1607 | 1073 | |
| 1608 | 1074 | if ( ! is_array( $result ) ) { |
| 1609 | 1075 | $message = is_string( $result ) ? $result : htmlentities( print_r( $result, 1 ) ); |
| 1076 | + | |
| 1610 | 1077 | return; |
| 1611 | 1078 | } |
| 1612 | 1079 | |
| 1613 | 1080 | $t_strings = array( |
| @@ -1615,57 +1082,37 @@ | ||
| 1615 | 1082 | 'updated' => __( 'Updated', 'formidable' ), |
| 1616 | 1083 | ); |
| 1617 | 1084 | |
| 1618 | 1085 | $message = '<ul>'; |
| 1619 | - | |
| 1620 | 1086 | foreach ( $result as $type => $results ) { |
| 1621 | 1087 | if ( ! isset( $t_strings[ $type ] ) ) { |
| 1622 | - // Only print imported and updated | |
| 1088 | + // only print imported and updated | |
| 1623 | 1089 | continue; |
| 1624 | 1090 | } |
| 1625 | 1091 | |
| 1626 | 1092 | $s_message = array(); |
| 1627 | - | |
| 1628 | 1093 | foreach ( $results as $k => $m ) { |
| 1629 | 1094 | self::item_count_message( $m, $k, $s_message ); |
| 1630 | 1095 | unset( $k, $m ); |
| 1631 | 1096 | } |
| 1632 | 1097 | |
| 1633 | - if ( ! $s_message ) { | |
| 1634 | - continue; | |
| 1098 | + if ( ! empty( $s_message ) ) { | |
| 1099 | + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; | |
| 1100 | + $message .= implode( ', ', $s_message ); | |
| 1101 | + $message .= '</li>'; | |
| 1635 | 1102 | } |
| 1103 | + } | |
| 1636 | 1104 | |
| 1637 | - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; | |
| 1638 | - $message .= implode( ', ', $s_message ); | |
| 1639 | - $message .= '</li>'; | |
| 1640 | - }//end foreach | |
| 1641 | - | |
| 1642 | - if ( $message === '<ul>' ) { | |
| 1105 | + if ( $message == '<ul>' ) { | |
| 1643 | 1106 | $message = ''; |
| 1644 | 1107 | $errors[] = __( 'Nothing was imported or updated', 'formidable' ); |
| 1108 | + } else { | |
| 1109 | + self::add_form_link_to_message( $result, $message ); | |
| 1645 | 1110 | |
| 1646 | - return; | |
| 1111 | + $message .= '</ul>'; | |
| 1647 | 1112 | } |
| 1648 | - | |
| 1649 | - self::add_form_link_to_message( $result, $message ); | |
| 1650 | - | |
| 1651 | - /** | |
| 1652 | - * @since 5.3 | |
| 1653 | - * | |
| 1654 | - * @param string $message | |
| 1655 | - * @param array $result | |
| 1656 | - */ | |
| 1657 | - $message = apply_filters( 'frm_xml_parsed_message', $message, $result ); | |
| 1658 | - $message .= '</ul>'; | |
| 1659 | 1113 | } |
| 1660 | 1114 | |
| 1661 | - /** | |
| 1662 | - * @param int $m | |
| 1663 | - * @param string $type | |
| 1664 | - * @param array<string> $s_message | |
| 1665 | - * | |
| 1666 | - * @return void | |
| 1667 | - */ | |
| 1668 | 1115 | public static function item_count_message( $m, $type, &$s_message ) { |
| 1669 | 1116 | if ( ! $m ) { |
| 1670 | 1117 | return; |
| 1671 | 1118 | } |
| @@ -1679,9 +1126,9 @@ | ||
| 1679 | 1126 | 'items' => sprintf( _n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m ), |
| 1680 | 1127 | /* translators: %1$s: Number of items */ |
| 1681 | 1128 | 'views' => sprintf( _n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m ), |
| 1682 | 1129 | /* translators: %1$s: Number of items */ |
| 1683 | - 'posts' => sprintf( _n( '%1$s Page/Post', '%1$s Pages/Posts', $m, 'formidable' ), $m ), | |
| 1130 | + 'posts' => sprintf( _n( '%1$s Post', '%1$s Posts', $m, 'formidable' ), $m ), | |
| 1684 | 1131 | /* translators: %1$s: Number of items */ |
| 1685 | 1132 | 'styles' => sprintf( _n( '%1$s Style', '%1$s Styles', $m, 'formidable' ), $m ), |
| 1686 | 1133 | /* translators: %1$s: Number of items */ |
| 1687 | 1134 | 'terms' => sprintf( _n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m ), |
| @@ -1688,24 +1135,9 @@ | ||
| 1688 | 1135 | /* translators: %1$s: Number of items */ |
| 1689 | 1136 | 'actions' => sprintf( _n( '%1$s Form Action', '%1$s Form Actions', $m, 'formidable' ), $m ), |
| 1690 | 1137 | ); |
| 1691 | 1138 | |
| 1692 | - if ( isset( $strings[ $type ] ) ) { | |
| 1693 | - $s_message[] = $strings[ $type ]; | |
| 1694 | - return; | |
| 1695 | - } | |
| 1696 | - | |
| 1697 | - $string = ' ' . $m . ' ' . ucfirst( $type ); | |
| 1698 | - | |
| 1699 | - /** | |
| 1700 | - * @since 5.3 | |
| 1701 | - * | |
| 1702 | - * @param string $string Message string for imported item. | |
| 1703 | - * @param int $m Number of item that was imported. | |
| 1704 | - * } | |
| 1705 | - */ | |
| 1706 | - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m ); | |
| 1707 | - $s_message[] = $string; | |
| 1139 | + $s_message[] = isset( $strings[ $type ] ) ? $strings[ $type ] : ' ' . $m . ' ' . ucfirst( $type ); | |
| 1708 | 1140 | } |
| 1709 | 1141 | |
| 1710 | 1142 | /** |
| 1711 | 1143 | * If a single form was imported, include a link in the success message. |
| @@ -1710,28 +1142,24 @@ | ||
| 1710 | 1142 | /** |
| 1711 | 1143 | * If a single form was imported, include a link in the success message. |
| 1712 | 1144 | * |
| 1713 | 1145 | * @since 4.0 |
| 1714 | - * | |
| 1715 | 1146 | * @param array $result The response from the XML import. |
| 1716 | 1147 | * @param string $message The response shown on the page after import. |
| 1717 | 1148 | */ |
| 1718 | 1149 | private static function add_form_link_to_message( $result, &$message ) { |
| 1719 | 1150 | $total_forms = $result['imported']['forms'] + $result['updated']['forms']; |
| 1720 | - | |
| 1721 | 1151 | if ( $total_forms > 1 ) { |
| 1722 | 1152 | return; |
| 1723 | 1153 | } |
| 1724 | 1154 | |
| 1725 | 1155 | $primary_form = reset( $result['forms'] ); |
| 1156 | + if ( ! empty( $primary_form ) ) { | |
| 1157 | + $primary_form = FrmForm::getOne( $primary_form ); | |
| 1158 | + $form_id = empty( $primary_form->parent_form_id ) ? $primary_form->id : $primary_form->parent_form_id; | |
| 1726 | 1159 | |
| 1727 | - if ( ! $primary_form ) { | |
| 1728 | - return; | |
| 1160 | + $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>'; | |
| 1729 | 1161 | } |
| 1730 | - | |
| 1731 | - $primary_form = FrmForm::getOne( $primary_form ); | |
| 1732 | - $form_id = ! empty( $primary_form->parent_form_id ) ? $primary_form->parent_form_id : $primary_form->id; | |
| 1733 | - $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>'; | |
| 1734 | 1162 | } |
| 1735 | 1163 | |
| 1736 | 1164 | /** |
| 1737 | 1165 | * Prepare the form options for export |
| @@ -1744,17 +1172,22 @@ | ||
| 1744 | 1172 | */ |
| 1745 | 1173 | public static function prepare_form_options_for_export( $options ) { |
| 1746 | 1174 | FrmAppHelper::unserialize_or_decode( $options ); |
| 1747 | 1175 | // Change custom_style to the post_name instead of ID (1 may be a string) |
| 1748 | - $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1749 | - | |
| 1176 | + $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; | |
| 1750 | 1177 | if ( $not_default ) { |
| 1751 | 1178 | global $wpdb; |
| 1752 | - $table = $wpdb->prefix . 'posts'; | |
| 1753 | - $where = array( 'ID' => $options['custom_style'] ); | |
| 1754 | - $select = 'post_name'; | |
| 1755 | - $style_name = FrmDb::get_var( $table, $where, $select ); | |
| 1756 | - $options['custom_style'] = $style_name ? $style_name : 1; | |
| 1179 | + $table = $wpdb->prefix . 'posts'; | |
| 1180 | + $where = array( 'ID' => $options['custom_style'] ); | |
| 1181 | + $select = 'post_name'; | |
| 1182 | + | |
| 1183 | + $style_name = FrmDb::get_var( $table, $where, $select ); | |
| 1184 | + | |
| 1185 | + if ( $style_name ) { | |
| 1186 | + $options['custom_style'] = $style_name; | |
| 1187 | + } else { | |
| 1188 | + $options['custom_style'] = 1; | |
| 1189 | + } | |
| 1757 | 1190 | } |
| 1758 | 1191 | self::remove_default_form_options( $options ); |
| 1759 | 1192 | $options = serialize( $options ); |
| 1760 | 1193 | |
| @@ -1765,16 +1198,11 @@ | ||
| 1765 | 1198 | * If the saved value is the same as the default, remove it from the export |
| 1766 | 1199 | * This keeps file size down and prevents overriding global settings after import |
| 1767 | 1200 | * |
| 1768 | 1201 | * @since 3.06 |
| 1769 | - * | |
| 1770 | - * @param array $options By reference. | |
| 1771 | - * | |
| 1772 | - * @return void | |
| 1773 | 1202 | */ |
| 1774 | 1203 | private static function remove_default_form_options( &$options ) { |
| 1775 | 1204 | $defaults = FrmFormsHelper::get_default_opts(); |
| 1776 | - | |
| 1777 | 1205 | if ( is_callable( 'FrmProFormsHelper::get_default_opts' ) ) { |
| 1778 | 1206 | $defaults += FrmProFormsHelper::get_default_opts(); |
| 1779 | 1207 | } |
| 1780 | 1208 | self::remove_defaults( $defaults, $options ); |
| @@ -1783,16 +1211,11 @@ | ||
| 1783 | 1211 | /** |
| 1784 | 1212 | * Remove extra settings from field to keep file size down |
| 1785 | 1213 | * |
| 1786 | 1214 | * @since 3.06 |
| 1787 | - * | |
| 1788 | - * @param object $field | |
| 1789 | - * | |
| 1790 | - * @return void | |
| 1791 | 1215 | */ |
| 1792 | 1216 | public static function prepare_field_for_export( &$field ) { |
| 1793 | 1217 | self::remove_default_field_options( $field ); |
| 1794 | - self::add_image_src_to_image_options( $field ); | |
| 1795 | 1218 | } |
| 1796 | 1219 | |
| 1797 | 1220 | /** |
| 1798 | 1221 | * Remove defaults from field options too |
| @@ -1797,16 +1220,11 @@ | ||
| 1797 | 1220 | /** |
| 1798 | 1221 | * Remove defaults from field options too |
| 1799 | 1222 | * |
| 1800 | 1223 | * @since 3.06 |
| 1801 | - * | |
| 1802 | - * @param object $field | |
| 1803 | - * | |
| 1804 | - * @return void | |
| 1805 | 1224 | */ |
| 1806 | 1225 | private static function remove_default_field_options( &$field ) { |
| 1807 | 1226 | $defaults = self::default_field_options( $field->type ); |
| 1808 | - | |
| 1809 | 1227 | if ( empty( $defaults['blank'] ) ) { |
| 1810 | 1228 | $global_settings = new FrmSettings(); |
| 1811 | 1229 | $global_defaults = $global_settings->default_options(); |
| 1812 | 1230 | $defaults['blank'] = $global_defaults['blank_msg']; |
| @@ -1829,57 +1247,15 @@ | ||
| 1829 | 1247 | $field->field_options = serialize( $options ); |
| 1830 | 1248 | } |
| 1831 | 1249 | |
| 1832 | 1250 | /** |
| 1833 | - * Add image "src" key to each image option so the image can be imported to another website. | |
| 1834 | - * | |
| 1835 | - * @since 5.5.1 | |
| 1836 | - * | |
| 1837 | - * @param stdClass $field | |
| 1838 | - * | |
| 1839 | - * @return void | |
| 1840 | - */ | |
| 1841 | - private static function add_image_src_to_image_options( $field ) { | |
| 1842 | - if ( empty( $field->options ) || ! str_contains( $field->options, 'image' ) ) { | |
| 1843 | - return; | |
| 1844 | - } | |
| 1845 | - | |
| 1846 | - $updated = false; | |
| 1847 | - $options = $field->options; | |
| 1848 | - FrmAppHelper::unserialize_or_decode( $options ); | |
| 1849 | - | |
| 1850 | - if ( ! $options || ! is_array( $options ) ) { | |
| 1851 | - return; | |
| 1852 | - } | |
| 1853 | - | |
| 1854 | - foreach ( $options as $key => $option ) { | |
| 1855 | - if ( ! is_array( $option ) || empty( $option['image'] ) ) { | |
| 1856 | - continue; | |
| 1857 | - } | |
| 1858 | - | |
| 1859 | - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] ); | |
| 1860 | - $updated = true; | |
| 1861 | - } | |
| 1862 | - | |
| 1863 | - if ( $updated ) { | |
| 1864 | - $field->options = maybe_serialize( $options ); | |
| 1865 | - } | |
| 1866 | - } | |
| 1867 | - | |
| 1868 | - /** | |
| 1869 | 1251 | * @since 3.06.03 |
| 1870 | - * | |
| 1871 | - * @param string $type | |
| 1872 | - * | |
| 1873 | - * @return array | |
| 1874 | 1252 | */ |
| 1875 | 1253 | private static function default_field_options( $type ) { |
| 1876 | 1254 | $defaults = FrmFieldsHelper::get_default_field_options( $type ); |
| 1877 | - | |
| 1878 | 1255 | if ( empty( $defaults['custom_html'] ) ) { |
| 1879 | 1256 | $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type ); |
| 1880 | 1257 | } |
| 1881 | - | |
| 1882 | 1258 | return $defaults; |
| 1883 | 1259 | } |
| 1884 | 1260 | |
| 1885 | 1261 | /** |
| @@ -1886,13 +1262,8 @@ | ||
| 1886 | 1262 | * Compare the default array to the saved values and |
| 1887 | 1263 | * remove if they are the same |
| 1888 | 1264 | * |
| 1889 | 1265 | * @since 3.06 |
| 1890 | - * | |
| 1891 | - * @param array $defaults | |
| 1892 | - * @param array $saved | |
| 1893 | - * | |
| 1894 | - * @return void | |
| 1895 | 1266 | */ |
| 1896 | 1267 | private static function remove_defaults( $defaults, &$saved ) { |
| 1897 | 1268 | foreach ( $saved as $key => $value ) { |
| 1898 | 1269 | if ( isset( $defaults[ $key ] ) && $defaults[ $key ] === $value ) { |
| @@ -1904,14 +1275,8 @@ | ||
| 1904 | 1275 | /** |
| 1905 | 1276 | * The line endings may prevent html from being equal when it should |
| 1906 | 1277 | * |
| 1907 | 1278 | * @since 3.06 |
| 1908 | - * | |
| 1909 | - * @param string $html_name | |
| 1910 | - * @param array $defaults | |
| 1911 | - * @param array $options | |
| 1912 | - * | |
| 1913 | - * @return void | |
| 1914 | 1279 | */ |
| 1915 | 1280 | private static function remove_default_html( $html_name, $defaults, &$options ) { |
| 1916 | 1281 | if ( ! isset( $options[ $html_name ] ) || ! isset( $defaults[ $html_name ] ) ) { |
| 1917 | 1282 | return; |
| @@ -1918,35 +1283,27 @@ | ||
| 1918 | 1283 | } |
| 1919 | 1284 | |
| 1920 | 1285 | $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] ); |
| 1921 | 1286 | $default_html = $defaults[ $html_name ]; |
| 1922 | - | |
| 1923 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1924 | 1287 | if ( $old_html == $default_html ) { |
| 1925 | 1288 | unset( $options[ $html_name ] ); |
| 1289 | + | |
| 1926 | 1290 | return; |
| 1927 | 1291 | } |
| 1928 | 1292 | |
| 1929 | 1293 | // Account for some of the older field default HTML. |
| 1930 | 1294 | $default_html = str_replace( ' id="frm_desc_field_[key]"', '', $default_html ); |
| 1931 | - | |
| 1932 | - if ( $old_html === $default_html ) { | |
| 1295 | + if ( $old_html == $default_html ) { | |
| 1933 | 1296 | unset( $options[ $html_name ] ); |
| 1934 | 1297 | } |
| 1935 | 1298 | } |
| 1936 | 1299 | |
| 1937 | - /** | |
| 1938 | - * @param string $str | |
| 1939 | - * | |
| 1940 | - * @return string | |
| 1941 | - */ | |
| 1942 | 1300 | public static function cdata( $str ) { |
| 1943 | 1301 | FrmAppHelper::unserialize_or_decode( $str ); |
| 1944 | - | |
| 1945 | 1302 | if ( is_array( $str ) ) { |
| 1946 | 1303 | $str = json_encode( $str ); |
| 1947 | - } elseif ( FrmAppHelper::is_valid_utf8( $str ) === false ) { | |
| 1948 | - $str = FrmAppHelper::maybe_utf8_encode( $str ); | |
| 1304 | + } elseif ( seems_utf8( $str ) === false ) { | |
| 1305 | + $str = utf8_encode( $str ); | |
| 1949 | 1306 | } |
| 1950 | 1307 | |
| 1951 | 1308 | if ( is_numeric( $str ) ) { |
| 1952 | 1309 | return $str; |
| @@ -1954,9 +1311,11 @@ | ||
| 1954 | 1311 | |
| 1955 | 1312 | self::remove_invalid_characters_from_xml( $str ); |
| 1956 | 1313 | |
| 1957 | 1314 | // $str = ent2ncr(esc_html( $str)); |
| 1958 | - return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 1315 | + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 1316 | + | |
| 1317 | + return $str; | |
| 1959 | 1318 | } |
| 1960 | 1319 | |
| 1961 | 1320 | /** |
| 1962 | 1321 | * Remove <US> character (unit separator) from exported strings |
| @@ -1963,10 +1322,8 @@ | ||
| 1963 | 1322 | * |
| 1964 | 1323 | * @since 2.0.22 |
| 1965 | 1324 | * |
| 1966 | 1325 | * @param string $str |
| 1967 | - * | |
| 1968 | - * @return void | |
| 1969 | 1326 | */ |
| 1970 | 1327 | private static function remove_invalid_characters_from_xml( &$str ) { |
| 1971 | 1328 | // Remove <US> character |
| 1972 | 1329 | $str = str_replace( '\x1F', '', $str ); |
| @@ -1971,18 +1328,8 @@ | ||
| 1971 | 1328 | // Remove <US> character |
| 1972 | 1329 | $str = str_replace( '\x1F', '', $str ); |
| 1973 | 1330 | } |
| 1974 | 1331 | |
| 1975 | - /** | |
| 1976 | - * Migrate form settings to actions | |
| 1977 | - * | |
| 1978 | - * @param array $form_options | |
| 1979 | - * @param int $form_id | |
| 1980 | - * @param array $imported | |
| 1981 | - * @param bool $switch | |
| 1982 | - * | |
| 1983 | - * @return void | |
| 1984 | - */ | |
| 1985 | 1332 | public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) { |
| 1986 | 1333 | // Get post type |
| 1987 | 1334 | $post_type = FrmFormActionsController::$action_post_type; |
| 1988 | 1335 | |
| @@ -2000,16 +1347,12 @@ | ||
| 2000 | 1347 | |
| 2001 | 1348 | /** |
| 2002 | 1349 | * Migrate post settings to form action |
| 2003 | 1350 | * |
| 2004 | - * @param array $form_options | |
| 2005 | - * @param int $form_id | |
| 2006 | 1351 | * @param string $post_type |
| 2007 | - * @param array $imported | |
| 2008 | - * @param bool $switch | |
| 2009 | 1352 | */ |
| 2010 | 1353 | private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
| 2011 | - if ( empty( $form_options['create_post'] ) ) { | |
| 1354 | + if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) { | |
| 2012 | 1355 | return; |
| 2013 | 1356 | } |
| 2014 | 1357 | |
| 2015 | 1358 | $new_action = array( |
| @@ -2061,9 +1404,8 @@ | ||
| 2061 | 1404 | $array_fields = array( 'post_category', 'post_custom_fields' ); |
| 2062 | 1405 | |
| 2063 | 1406 | $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields ); |
| 2064 | 1407 | } |
| 2065 | - | |
| 2066 | 1408 | $new_action['post_content'] = json_encode( $new_action['post_content'] ); |
| 2067 | 1409 | |
| 2068 | 1410 | $exists = get_posts( |
| 2069 | 1411 | array( |
| @@ -2073,15 +1415,13 @@ | ||
| 2073 | 1415 | 'numberposts' => 1, |
| 2074 | 1416 | ) |
| 2075 | 1417 | ); |
| 2076 | 1418 | |
| 2077 | - if ( $exists ) { | |
| 2078 | - return; | |
| 1419 | + if ( ! $exists ) { | |
| 1420 | + // this isn't an email, but we need to use a class that will always be included | |
| 1421 | + FrmDb::save_json_post( $new_action ); | |
| 1422 | + $imported['imported']['actions'] ++; | |
| 2079 | 1423 | } |
| 2080 | - | |
| 2081 | - // This isn't an email, but we need to use a class that will always be included | |
| 2082 | - FrmDb::save_json_post( $new_action ); | |
| 2083 | - ++$imported['imported']['actions']; | |
| 2084 | 1424 | } |
| 2085 | 1425 | |
| 2086 | 1426 | /** |
| 2087 | 1427 | * Switch old field IDs for new field IDs in emails and post |
| @@ -2087,11 +1427,11 @@ | ||
| 2087 | 1427 | * Switch old field IDs for new field IDs in emails and post |
| 2088 | 1428 | * |
| 2089 | 1429 | * @since 2.0 |
| 2090 | 1430 | * |
| 2091 | - * @param array $post_content Check for old field IDs. | |
| 2092 | - * @param array $basic_fields Fields with string or int saved. | |
| 2093 | - * @param array $array_fields Fields with arrays saved. | |
| 1431 | + * @param array $post_content - check for old field IDs | |
| 1432 | + * @param array $basic_fields - fields with string or int saved | |
| 1433 | + * @param array $array_fields - fields with arrays saved | |
| 2094 | 1434 | * |
| 2095 | 1435 | * @return string $post_content - new field IDs |
| 2096 | 1436 | */ |
| 2097 | 1437 | private static function switch_action_field_ids( $post_content, $basic_fields, $array_fields = array() ) { |
| @@ -2098,9 +1438,9 @@ | ||
| 2098 | 1438 | global $frm_duplicate_ids; |
| 2099 | 1439 | |
| 2100 | 1440 | // If there aren't IDs that were switched, end now |
| 2101 | 1441 | if ( ! $frm_duplicate_ids ) { |
| 2102 | - return null; | |
| 1442 | + return; | |
| 2103 | 1443 | } |
| 2104 | 1444 | |
| 2105 | 1445 | // Get old IDs |
| 2106 | 1446 | $old = array_keys( $frm_duplicate_ids ); |
| @@ -2109,13 +1449,11 @@ | ||
| 2109 | 1449 | $new = array_values( $frm_duplicate_ids ); |
| 2110 | 1450 | |
| 2111 | 1451 | // Do a str_replace with each item to set the new IDs |
| 2112 | 1452 | foreach ( $post_content as $key => $setting ) { |
| 2113 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 2114 | 1453 | if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) { |
| 2115 | 1454 | // Replace old IDs with new IDs |
| 2116 | 1455 | $post_content[ $key ] = str_replace( $old, $new, $setting ); |
| 2117 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 2118 | 1456 | } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) { |
| 2119 | 1457 | foreach ( $setting as $k => $val ) { |
| 2120 | 1458 | // Replace old IDs with new IDs |
| 2121 | 1459 | $post_content[ $key ][ $k ] = str_replace( $old, $new, $val ); |
| @@ -2126,19 +1464,8 @@ | ||
| 2126 | 1464 | |
| 2127 | 1465 | return $post_content; |
| 2128 | 1466 | } |
| 2129 | 1467 | |
| 2130 | - /** | |
| 2131 | - * Migrate email settings to action | |
| 2132 | - * | |
| 2133 | - * @param array $form_options | |
| 2134 | - * @param int $form_id | |
| 2135 | - * @param string $post_type | |
| 2136 | - * @param array $imported | |
| 2137 | - * @param bool $switch | |
| 2138 | - * | |
| 2139 | - * @return void | |
| 2140 | - */ | |
| 2141 | 1468 | private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
| 2142 | 1469 | // No old notifications or autoresponders to carry over |
| 2143 | 1470 | if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) { |
| 2144 | 1471 | return; |
| @@ -2152,9 +1479,9 @@ | ||
| 2152 | 1479 | |
| 2153 | 1480 | // Migrate autoresponders |
| 2154 | 1481 | self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications ); |
| 2155 | 1482 | |
| 2156 | - if ( ! $notifications ) { | |
| 1483 | + if ( empty( $notifications ) ) { | |
| 2157 | 1484 | return; |
| 2158 | 1485 | } |
| 2159 | 1486 | |
| 2160 | 1487 | foreach ( $notifications as $new_notification ) { |
| @@ -2165,8 +1492,9 @@ | ||
| 2165 | 1492 | $new_notification['post_status'] = 'publish'; |
| 2166 | 1493 | |
| 2167 | 1494 | // Switch field IDs and keys, if needed |
| 2168 | 1495 | if ( $switch ) { |
| 1496 | + | |
| 2169 | 1497 | // Switch field IDs in email conditional logic |
| 2170 | 1498 | self::switch_email_condition_field_ids( $new_notification['post_content'] ); |
| 2171 | 1499 | |
| 2172 | 1500 | // Switch all other field IDs in email |
| @@ -2171,9 +1499,8 @@ | ||
| 2171 | 1499 | |
| 2172 | 1500 | // Switch all other field IDs in email |
| 2173 | 1501 | $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] ); |
| 2174 | 1502 | } |
| 2175 | - | |
| 2176 | 1503 | $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] ); |
| 2177 | 1504 | |
| 2178 | 1505 | $exists = get_posts( |
| 2179 | 1506 | array( |
| @@ -2183,14 +1510,14 @@ | ||
| 2183 | 1510 | 'numberposts' => 1, |
| 2184 | 1511 | ) |
| 2185 | 1512 | ); |
| 2186 | 1513 | |
| 2187 | - if ( ! $exists ) { | |
| 1514 | + if ( empty( $exists ) ) { | |
| 2188 | 1515 | FrmDb::save_json_post( $new_notification ); |
| 2189 | - ++$imported['imported']['actions']; | |
| 1516 | + $imported['imported']['actions'] ++; | |
| 2190 | 1517 | } |
| 2191 | 1518 | unset( $new_notification ); |
| 2192 | - }//end foreach | |
| 1519 | + } | |
| 2193 | 1520 | |
| 2194 | 1521 | self::remove_deprecated_notification_settings( $form_id, $form_options ); |
| 2195 | 1522 | } |
| 2196 | 1523 | |
| @@ -2199,44 +1526,32 @@ | ||
| 2199 | 1526 | * |
| 2200 | 1527 | * @since 2.05 |
| 2201 | 1528 | * |
| 2202 | 1529 | * @param int|string $form_id |
| 2203 | - * @param array $form_options | |
| 2204 | - * | |
| 2205 | - * @return void | |
| 1530 | + * @param array $form_options | |
| 2206 | 1531 | */ |
| 2207 | 1532 | private static function remove_deprecated_notification_settings( $form_id, $form_options ) { |
| 2208 | 1533 | $delete_settings = array( 'notification', 'autoresponder', 'email_to' ); |
| 2209 | - | |
| 2210 | 1534 | foreach ( $delete_settings as $index ) { |
| 2211 | 1535 | if ( isset( $form_options[ $index ] ) ) { |
| 2212 | 1536 | unset( $form_options[ $index ] ); |
| 2213 | 1537 | } |
| 2214 | 1538 | } |
| 2215 | - | |
| 2216 | 1539 | FrmForm::update( $form_id, array( 'options' => $form_options ) ); |
| 2217 | 1540 | } |
| 2218 | 1541 | |
| 2219 | - /** | |
| 2220 | - * Migrate notifications to action | |
| 2221 | - * | |
| 2222 | - * @param array $form_options | |
| 2223 | - * @param int $form_id | |
| 2224 | - * @param array $notifications | |
| 2225 | - * | |
| 2226 | - * @return void | |
| 2227 | - */ | |
| 2228 | 1542 | private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) { |
| 2229 | - if ( ! isset( $form_options['notification'] ) && ! empty( $form_options['email_to'] ) ) { | |
| 2230 | - // Add old settings into notification array | |
| 1543 | + if ( ! isset( $form_options['notification'] ) && isset( $form_options['email_to'] ) && ! empty( $form_options['email_to'] ) ) { | |
| 1544 | + // add old settings into notification array | |
| 2231 | 1545 | $form_options['notification'] = array( 0 => $form_options ); |
| 2232 | 1546 | } elseif ( isset( $form_options['notification']['email_to'] ) ) { |
| 2233 | - // Make sure it's in the correct format | |
| 1547 | + // make sure it's in the correct format | |
| 2234 | 1548 | $form_options['notification'] = array( 0 => $form_options['notification'] ); |
| 2235 | 1549 | } |
| 2236 | 1550 | |
| 2237 | 1551 | if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) { |
| 2238 | 1552 | foreach ( $form_options['notification'] as $email_key => $notification ) { |
| 1553 | + | |
| 2239 | 1554 | $atts = array( |
| 2240 | 1555 | 'email_to' => '', |
| 2241 | 1556 | 'reply_to' => '', |
| 2242 | 1557 | 'reply_to_name' => '', |
| @@ -2247,9 +1562,9 @@ | ||
| 2247 | 1562 | |
| 2248 | 1563 | // Format the email data |
| 2249 | 1564 | self::format_email_data( $atts, $notification ); |
| 2250 | 1565 | |
| 2251 | - if ( ! empty( $notification['twilio'] ) ) { | |
| 1566 | + if ( isset( $notification['twilio'] ) && $notification['twilio'] ) { | |
| 2252 | 1567 | do_action( 'frm_create_twilio_action', $atts, $notification ); |
| 2253 | 1568 | } |
| 2254 | 1569 | |
| 2255 | 1570 | // Setup the new notification |
| @@ -2256,20 +1571,12 @@ | ||
| 2256 | 1571 | $new_notification = array(); |
| 2257 | 1572 | self::setup_new_notification( $new_notification, $notification, $atts ); |
| 2258 | 1573 | |
| 2259 | 1574 | $notifications[] = $new_notification; |
| 2260 | - }//end foreach | |
| 2261 | - }//end if | |
| 1575 | + } | |
| 1576 | + } | |
| 2262 | 1577 | } |
| 2263 | 1578 | |
| 2264 | - /** | |
| 2265 | - * Format email data | |
| 2266 | - * | |
| 2267 | - * @param array $atts | |
| 2268 | - * @param array $notification | |
| 2269 | - * | |
| 2270 | - * @return void | |
| 2271 | - */ | |
| 2272 | 1579 | private static function format_email_data( &$atts, $notification ) { |
| 2273 | 1580 | // Format email_to |
| 2274 | 1581 | self::format_email_to_data( $atts, $notification ); |
| 2275 | 1582 | |
| @@ -2277,16 +1584,14 @@ | ||
| 2277 | 1584 | $reply_fields = array( |
| 2278 | 1585 | 'reply_to' => '', |
| 2279 | 1586 | 'reply_to_name' => '', |
| 2280 | 1587 | ); |
| 2281 | - | |
| 2282 | 1588 | foreach ( $reply_fields as $f => $val ) { |
| 2283 | 1589 | if ( isset( $notification[ $f ] ) ) { |
| 2284 | 1590 | $atts[ $f ] = $notification[ $f ]; |
| 2285 | - | |
| 2286 | - if ( 'custom' === $notification[ $f ] ) { | |
| 1591 | + if ( 'custom' == $notification[ $f ] ) { | |
| 2287 | 1592 | $atts[ $f ] = $notification[ 'cust_' . $f ]; |
| 2288 | - } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) { | |
| 1593 | + } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) { | |
| 2289 | 1594 | $atts[ $f ] = '[' . $atts[ $f ] . ']'; |
| 2290 | 1595 | } |
| 2291 | 1596 | } |
| 2292 | 1597 | unset( $f, $val ); |
| @@ -2293,27 +1598,21 @@ | ||
| 2293 | 1598 | } |
| 2294 | 1599 | |
| 2295 | 1600 | // Format event |
| 2296 | 1601 | $atts['event'] = array( 'create' ); |
| 2297 | - | |
| 2298 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 2299 | 1602 | if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) { |
| 2300 | 1603 | $atts['event'][] = 'update'; |
| 2301 | - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1604 | + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { | |
| 2302 | 1605 | $atts['event'] = array( 'update' ); |
| 2303 | 1606 | } |
| 2304 | 1607 | } |
| 2305 | 1608 | |
| 2306 | - /** | |
| 2307 | - * Format email_to data | |
| 2308 | - * | |
| 2309 | - * @param array $atts | |
| 2310 | - * @param array $notification | |
| 2311 | - * | |
| 2312 | - * @return void | |
| 2313 | - */ | |
| 2314 | 1609 | private static function format_email_to_data( &$atts, $notification ) { |
| 2315 | - $atts['email_to'] = isset( $notification['email_to'] ) ? preg_split( '/ (,|;) /', $notification['email_to'] ) : array(); | |
| 1610 | + if ( isset( $notification['email_to'] ) ) { | |
| 1611 | + $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] ); | |
| 1612 | + } else { | |
| 1613 | + $atts['email_to'] = array(); | |
| 1614 | + } | |
| 2316 | 1615 | |
| 2317 | 1616 | if ( isset( $notification['also_email_to'] ) ) { |
| 2318 | 1617 | $email_fields = (array) $notification['also_email_to']; |
| 2319 | 1618 | $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] ); |
| @@ -2320,36 +1619,24 @@ | ||
| 2320 | 1619 | unset( $email_fields ); |
| 2321 | 1620 | } |
| 2322 | 1621 | |
| 2323 | 1622 | foreach ( $atts['email_to'] as $key => $email_field ) { |
| 1623 | + | |
| 2324 | 1624 | if ( is_numeric( $email_field ) ) { |
| 2325 | 1625 | $atts['email_to'][ $key ] = '[' . $email_field . ']'; |
| 2326 | 1626 | } |
| 2327 | 1627 | |
| 2328 | - if ( ! str_contains( $email_field, '|' ) ) { | |
| 2329 | - continue; | |
| 1628 | + if ( strpos( $email_field, '|' ) ) { | |
| 1629 | + $email_opt = explode( '|', $email_field ); | |
| 1630 | + if ( isset( $email_opt[0] ) ) { | |
| 1631 | + $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']'; | |
| 1632 | + } | |
| 1633 | + unset( $email_opt ); | |
| 2330 | 1634 | } |
| 2331 | - | |
| 2332 | - $email_opt = explode( '|', $email_field ); | |
| 2333 | - | |
| 2334 | - if ( isset( $email_opt[0] ) ) { | |
| 2335 | - $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']'; | |
| 2336 | - } | |
| 2337 | - unset( $email_opt ); | |
| 2338 | 1635 | } |
| 2339 | - | |
| 2340 | 1636 | $atts['email_to'] = implode( ', ', $atts['email_to'] ); |
| 2341 | 1637 | } |
| 2342 | 1638 | |
| 2343 | - /** | |
| 2344 | - * Setup new notification | |
| 2345 | - * | |
| 2346 | - * @param array $new_notification | |
| 2347 | - * @param array $notification | |
| 2348 | - * @param array $atts | |
| 2349 | - * | |
| 2350 | - * @return void | |
| 2351 | - */ | |
| 2352 | 1639 | private static function setup_new_notification( &$new_notification, $notification, $atts ) { |
| 2353 | 1640 | // Set up new notification |
| 2354 | 1641 | $new_notification = array( |
| 2355 | 1642 | 'post_content' => array( |
| @@ -2360,13 +1647,12 @@ | ||
| 2360 | 1647 | ); |
| 2361 | 1648 | |
| 2362 | 1649 | // Add more fields to the new notification |
| 2363 | 1650 | $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' ); |
| 2364 | - | |
| 2365 | 1651 | foreach ( $add_fields as $add_field ) { |
| 2366 | 1652 | if ( isset( $notification[ $add_field ] ) ) { |
| 2367 | 1653 | $new_notification['post_content'][ $add_field ] = $notification[ $add_field ]; |
| 2368 | - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) { | |
| 1654 | + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) { | |
| 2369 | 1655 | $new_notification['post_content'][ $add_field ] = 0; |
| 2370 | 1656 | } else { |
| 2371 | 1657 | $new_notification['post_content'][ $add_field ] = ''; |
| 2372 | 1658 | } |
| @@ -2377,9 +1663,9 @@ | ||
| 2377 | 1663 | $new_notification['post_content']['reply_to'] = $atts['reply_to']; |
| 2378 | 1664 | |
| 2379 | 1665 | // Set from |
| 2380 | 1666 | if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) { |
| 2381 | - $new_notification['post_content']['from'] = ( ! empty( $atts['reply_to_name'] ) ? $atts['reply_to_name'] : '[sitename]' ) . ' <' . ( ! empty( $atts['reply_to'] ) ? $atts['reply_to'] : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 1667 | + $new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>'; | |
| 2382 | 1668 | } |
| 2383 | 1669 | } |
| 2384 | 1670 | |
| 2385 | 1671 | /** |
| @@ -2384,90 +1670,72 @@ | ||
| 2384 | 1670 | |
| 2385 | 1671 | /** |
| 2386 | 1672 | * Switch field IDs in pre-2.0 email conditional logic |
| 2387 | 1673 | * |
| 2388 | - * @param array $post_content Pass by reference. | |
| 2389 | - * | |
| 2390 | - * @return void | |
| 1674 | + * @param $post_content array, pass by reference | |
| 2391 | 1675 | */ |
| 2392 | 1676 | private static function switch_email_condition_field_ids( &$post_content ) { |
| 2393 | 1677 | // Switch field IDs in conditional logic |
| 2394 | - if ( ! isset( $post_content['conditions'] ) || ! is_array( $post_content['conditions'] ) ) { | |
| 2395 | - return; | |
| 2396 | - } | |
| 2397 | - | |
| 2398 | - foreach ( $post_content['conditions'] as $email_key => $val ) { | |
| 2399 | - if ( is_numeric( $email_key ) ) { | |
| 2400 | - $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) ); | |
| 1678 | + if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) { | |
| 1679 | + foreach ( $post_content['conditions'] as $email_key => $val ) { | |
| 1680 | + if ( is_numeric( $email_key ) ) { | |
| 1681 | + $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) ); | |
| 1682 | + } | |
| 1683 | + unset( $email_key, $val ); | |
| 2401 | 1684 | } |
| 2402 | - unset( $email_key, $val ); | |
| 2403 | 1685 | } |
| 2404 | 1686 | } |
| 2405 | 1687 | |
| 2406 | - /** | |
| 2407 | - * Migrate autoresponder to action | |
| 2408 | - * | |
| 2409 | - * @param array $form_options | |
| 2410 | - * @param int $form_id | |
| 2411 | - * @param array $notifications | |
| 2412 | - * | |
| 2413 | - * @return void | |
| 2414 | - */ | |
| 2415 | 1688 | private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) { |
| 2416 | - if ( empty( $form_options['auto_responder'] ) || empty( $form_options['ar_email_message'] ) ) { | |
| 2417 | - return; | |
| 2418 | - } | |
| 1689 | + if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) { | |
| 1690 | + // migrate autoresponder | |
| 2419 | 1691 | |
| 2420 | - // Migrate autoresponder | |
| 1692 | + $email_field = isset( $form_options['ar_email_to'] ) ? $form_options['ar_email_to'] : 0; | |
| 1693 | + if ( strpos( $email_field, '|' ) ) { | |
| 1694 | + // data from entries field | |
| 1695 | + $email_field = explode( '|', $email_field ); | |
| 1696 | + if ( isset( $email_field[1] ) ) { | |
| 1697 | + $email_field = $email_field[1]; | |
| 1698 | + } | |
| 1699 | + } | |
| 1700 | + if ( is_numeric( $email_field ) && ! empty( $email_field ) ) { | |
| 1701 | + $email_field = '[' . $email_field . ']'; | |
| 1702 | + } | |
| 2421 | 1703 | |
| 2422 | - $email_field = $form_options['ar_email_to'] ?? 0; | |
| 1704 | + $notification = $form_options; | |
| 1705 | + $new_notification2 = array( | |
| 1706 | + 'post_content' => array( | |
| 1707 | + 'email_message' => $notification['ar_email_message'], | |
| 1708 | + 'email_subject' => isset( $notification['ar_email_subject'] ) ? $notification['ar_email_subject'] : '', | |
| 1709 | + 'email_to' => $email_field, | |
| 1710 | + 'plain_text' => isset( $notification['ar_plain_text'] ) ? $notification['ar_plain_text'] : 0, | |
| 1711 | + 'inc_user_info' => 0, | |
| 1712 | + ), | |
| 1713 | + 'post_name' => $form_id . '_email_' . count( $notifications ), | |
| 1714 | + ); | |
| 2423 | 1715 | |
| 2424 | - if ( str_contains( $email_field, '|' ) ) { | |
| 2425 | - // Data from entries field | |
| 2426 | - $email_field = explode( '|', $email_field ); | |
| 1716 | + $reply_to = isset( $notification['ar_reply_to'] ) ? $notification['ar_reply_to'] : ''; | |
| 1717 | + $reply_to_name = isset( $notification['ar_reply_to_name'] ) ? $notification['ar_reply_to_name'] : ''; | |
| 2427 | 1718 | |
| 2428 | - if ( isset( $email_field[1] ) ) { | |
| 2429 | - $email_field = $email_field[1]; | |
| 1719 | + if ( ! empty( $reply_to ) ) { | |
| 1720 | + $new_notification2['post_content']['reply_to'] = $reply_to; | |
| 2430 | 1721 | } |
| 2431 | - } | |
| 2432 | 1722 | |
| 2433 | - if ( is_numeric( $email_field ) && $email_field ) { | |
| 2434 | - $email_field = '[' . $email_field . ']'; | |
| 2435 | - } | |
| 1723 | + if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) { | |
| 1724 | + $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>'; | |
| 1725 | + } | |
| 2436 | 1726 | |
| 2437 | - $notification = $form_options; | |
| 2438 | - $new_notification2 = array( | |
| 2439 | - 'post_content' => array( | |
| 2440 | - 'email_message' => $notification['ar_email_message'], | |
| 2441 | - 'email_subject' => $notification['ar_email_subject'] ?? '', | |
| 2442 | - 'email_to' => $email_field, | |
| 2443 | - 'plain_text' => $notification['ar_plain_text'] ?? 0, | |
| 2444 | - 'inc_user_info' => 0, | |
| 2445 | - ), | |
| 2446 | - 'post_name' => $form_id . '_email_' . count( $notifications ), | |
| 2447 | - ); | |
| 2448 | - | |
| 2449 | - $reply_to = $notification['ar_reply_to'] ?? ''; | |
| 2450 | - $reply_to_name = $notification['ar_reply_to_name'] ?? ''; | |
| 2451 | - | |
| 2452 | - if ( $reply_to ) { | |
| 2453 | - $new_notification2['post_content']['reply_to'] = $reply_to; | |
| 1727 | + $notifications[] = $new_notification2; | |
| 1728 | + unset( $new_notification2 ); | |
| 2454 | 1729 | } |
| 2455 | - | |
| 2456 | - if ( $reply_to || $reply_to_name ) { | |
| 2457 | - $new_notification2['post_content']['from'] = ( $reply_to_name ? $reply_to_name : '[sitename]' ) . ' <' . ( $reply_to ? $reply_to : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 2458 | - } | |
| 2459 | - | |
| 2460 | - $notifications[] = $new_notification2; | |
| 2461 | - unset( $new_notification2 ); | |
| 2462 | 1730 | } |
| 2463 | 1731 | |
| 2464 | 1732 | /** |
| 2465 | 1733 | * PHP 8 backward compatibility for the libxml_disable_entity_loader function |
| 2466 | 1734 | * |
| 2467 | - * @param bool $loader | |
| 1735 | + * @param boolean $disable | |
| 2468 | 1736 | * |
| 2469 | - * @return bool | |
| 1737 | + * @return boolean | |
| 2470 | 1738 | */ |
| 2471 | 1739 | public static function maybe_libxml_disable_entity_loader( $loader ) { |
| 2472 | 1740 | if ( version_compare( phpversion(), '8.0', '<' ) && function_exists( 'libxml_disable_entity_loader' ) ) { |
| 2473 | 1741 | $loader = libxml_disable_entity_loader( $loader ); // phpcs:disable Generic.PHP.DeprecatedFunctions.Deprecated |
| @@ -2475,32 +1743,9 @@ | ||
| 2475 | 1743 | |
| 2476 | 1744 | return $loader; |
| 2477 | 1745 | } |
| 2478 | 1746 | |
| 2479 | - /** | |
| 2480 | - * PHP 8 backward compatibility for the libxml_disable_entity_loader function | |
| 2481 | - * | |
| 2482 | - * @return bool | |
| 2483 | - */ | |
| 2484 | 1747 | public static function check_if_libxml_disable_entity_loader_exists() { |
| 2485 | 1748 | return version_compare( phpversion(), '8.0', '<' ) && ! function_exists( 'libxml_disable_entity_loader' ); |
| 2486 | 1749 | } |
| 1750 | +} | |
| 2487 | 1751 | |
| 2488 | - /** | |
| 2489 | - * Get the file types allowed for importing. | |
| 2490 | - * This is used in the "accept" attribute for the file upload input on the XML import page. | |
| 2491 | - * | |
| 2492 | - * @since 6.8.3 | |
| 2493 | - * | |
| 2494 | - * @return array | |
| 2495 | - */ | |
| 2496 | - public static function get_supported_upload_file_types() { | |
| 2497 | - $file_types = array( '.xml' ); | |
| 2498 | - | |
| 2499 | - if ( FrmAppHelper::pro_is_installed() ) { | |
| 2500 | - // CSV Importing is only available in Pro. | |
| 2501 | - $file_types[] = '.csv'; | |
| 2502 | - } | |
| 2503 | - | |
| 2504 | - return $file_types; | |
| 2505 | - } | |
| 2506 | -} | |