| @@ -4,19 +4,8 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmXMLHelper { |
| 7 | 7 | |
| 8 | - /** | |
| 9 | - * @var bool true if importing an XML from API, false if importing an XML file manually. | |
| 10 | - */ | |
| 11 | - private static $installing_template = false; | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * @param array|string $opt | |
| 15 | - * @param string $padding | |
| 16 | - * | |
| 17 | - * @return void | |
| 18 | - */ | |
| 19 | 8 | public static function get_xml_values( $opt, $padding ) { |
| 20 | 9 | if ( is_array( $opt ) ) { |
| 21 | 10 | foreach ( $opt as $ok => $ov ) { |
| 22 | 11 | echo "\n" . esc_html( $padding ); |
| @@ -22,9 +11,8 @@ | ||
| 22 | 11 | echo "\n" . esc_html( $padding ); |
| 23 | 12 | $tag = ( is_numeric( $ok ) ? 'key:' : '' ) . $ok; |
| 24 | 13 | echo '<' . esc_html( $tag ) . '>'; |
| 25 | 14 | self::get_xml_values( $ov, $padding . ' ' ); |
| 26 | - | |
| 27 | 15 | if ( is_array( $ov ) ) { |
| 28 | 16 | echo "\n" . esc_html( $padding ); |
| 29 | 17 | } |
| 30 | 18 | echo '</' . esc_html( $tag ) . '>'; |
| @@ -29,17 +17,12 @@ | ||
| 29 | 17 | } |
| 30 | 18 | echo '</' . esc_html( $tag ) . '>'; |
| 31 | 19 | } |
| 32 | 20 | } else { |
| 33 | - echo self::cdata( $opt ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 21 | + echo self::cdata( $opt ); // WPCS: XSS ok. | |
| 34 | 22 | } |
| 35 | 23 | } |
| 36 | 24 | |
| 37 | - /** | |
| 38 | - * @param string $file | |
| 39 | - * | |
| 40 | - * @return array|WP_Error The items imported | |
| 41 | - */ | |
| 42 | 25 | public static function import_xml( $file ) { |
| 43 | 26 | if ( ! defined( 'WP_IMPORTING' ) ) { |
| 44 | 27 | define( 'WP_IMPORTING', true ); |
| 45 | 28 | } |
| @@ -44,26 +27,13 @@ | ||
| 44 | 27 | define( 'WP_IMPORTING', true ); |
| 45 | 28 | } |
| 46 | 29 | |
| 47 | 30 | 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() ); | |
| 31 | + return new WP_Error( 'SimpleXML_parse_error', __( 'Your server does not have XML enabled', 'formidable' ), libxml_get_errors() ); | |
| 55 | 32 | } |
| 56 | 33 | |
| 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 | - | |
| 34 | + $dom = new DOMDocument(); | |
| 35 | + $success = $dom->loadXML( file_get_contents( $file ) ); | |
| 66 | 36 | if ( ! $success ) { |
| 67 | 37 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
| 68 | 38 | } |
| 69 | 39 | |
| @@ -73,9 +43,9 @@ | ||
| 73 | 43 | |
| 74 | 44 | $xml = simplexml_import_dom( $dom ); |
| 75 | 45 | unset( $dom ); |
| 76 | 46 | |
| 77 | - // Halt if loading produces an error | |
| 47 | + // halt if loading produces an error | |
| 78 | 48 | if ( ! $xml ) { |
| 79 | 49 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
| 80 | 50 | } |
| 81 | 51 | |
| @@ -82,73 +52,35 @@ | ||
| 82 | 52 | return self::import_xml_now( $xml ); |
| 83 | 53 | } |
| 84 | 54 | |
| 85 | 55 | /** |
| 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 | 56 | * Add terms, forms (form and field ids), posts (post ids), and entries to db, in that order |
| 118 | 57 | * |
| 119 | 58 | * @since 3.06 |
| 120 | - * | |
| 121 | - * @param object $xml | |
| 122 | - * @param bool $installing_template | |
| 123 | - * | |
| 124 | 59 | * @return array The number of items imported |
| 125 | 60 | */ |
| 126 | - public static function import_xml_now( $xml, $installing_template = false ) { | |
| 61 | + public static function import_xml_now( $xml ) { | |
| 127 | 62 | if ( ! defined( 'WP_IMPORTING' ) ) { |
| 128 | 63 | define( 'WP_IMPORTING', true ); |
| 129 | 64 | } |
| 130 | 65 | |
| 131 | - self::$installing_template = $installing_template; | |
| 132 | - $imported = self::pre_import_data(); | |
| 66 | + $imported = self::pre_import_data(); | |
| 133 | 67 | |
| 134 | 68 | foreach ( array( 'term', 'form', 'view' ) as $item_type ) { |
| 135 | 69 | // Grab cats, tags, and terms, or forms or posts. |
| 136 | - if ( ! isset( $xml->{$item_type} ) ) { | |
| 137 | - continue; | |
| 70 | + if ( isset( $xml->{$item_type} ) ) { | |
| 71 | + $function_name = 'import_xml_' . $item_type . 's'; | |
| 72 | + $imported = self::$function_name( $xml->{$item_type}, $imported ); | |
| 73 | + unset( $function_name, $xml->{$item_type} ); | |
| 138 | 74 | } |
| 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 | 75 | } |
| 144 | 76 | |
| 145 | 77 | $imported = apply_filters( 'frm_importing_xml', $imported, $xml ); |
| 146 | 78 | |
| 147 | - if ( empty( $imported['form_status'] ) ) { | |
| 79 | + if ( ! isset( $imported['form_status'] ) || empty( $imported['form_status'] ) ) { | |
| 148 | 80 | // Check for an error message in the XML. |
| 149 | 81 | if ( isset( $xml->Code ) && isset( $xml->Message ) ) { // phpcs:ignore WordPress.NamingConventions |
| 150 | - $imported['error'] = (string) $xml->Message; // phpcs:ignore WordPress.NamingConventions | |
| 82 | + $imported['error'] = reset( $xml->Message ); // phpcs:ignore WordPress.NamingConventions | |
| 151 | 83 | } |
| 152 | 84 | } |
| 153 | 85 | |
| 154 | 86 | return $imported; |
| @@ -155,9 +87,8 @@ | ||
| 155 | 87 | } |
| 156 | 88 | |
| 157 | 89 | /** |
| 158 | 90 | * @since 3.06 |
| 159 | - * | |
| 160 | 91 | * @return array |
| 161 | 92 | */ |
| 162 | 93 | private static function pre_import_data() { |
| 163 | 94 | $defaults = array( |
| @@ -177,14 +108,8 @@ | ||
| 177 | 108 | 'terms' => array(), |
| 178 | 109 | ); |
| 179 | 110 | } |
| 180 | 111 | |
| 181 | - /** | |
| 182 | - * @param SimpleXMLElement $terms | |
| 183 | - * @param array $imported | |
| 184 | - * | |
| 185 | - * @return array | |
| 186 | - */ | |
| 187 | 112 | public static function import_xml_terms( $terms, $imported ) { |
| 188 | 113 | foreach ( $terms as $t ) { |
| 189 | 114 | if ( term_exists( (string) $t->term_slug, (string) $t->term_taxonomy ) ) { |
| 190 | 115 | continue; |
| @@ -197,19 +122,19 @@ | ||
| 197 | 122 | (string) $t->term_taxonomy, |
| 198 | 123 | array( |
| 199 | 124 | 'slug' => (string) $t->term_slug, |
| 200 | 125 | 'description' => (string) $t->term_description, |
| 201 | - 'parent' => $parent ? $parent : 0, | |
| 126 | + 'parent' => empty( $parent ) ? 0 : $parent, | |
| 202 | 127 | ) |
| 203 | 128 | ); |
| 204 | 129 | |
| 205 | 130 | if ( $term && is_array( $term ) ) { |
| 206 | - ++$imported['imported']['terms']; | |
| 131 | + $imported['imported']['terms'] ++; | |
| 207 | 132 | $imported['terms'][ (int) $t->term_id ] = $term['term_id']; |
| 208 | 133 | } |
| 209 | 134 | |
| 210 | 135 | unset( $term, $t ); |
| 211 | - }//end foreach | |
| 136 | + } | |
| 212 | 137 | |
| 213 | 138 | return $imported; |
| 214 | 139 | } |
| 215 | 140 | |
| @@ -214,30 +139,23 @@ | ||
| 214 | 139 | } |
| 215 | 140 | |
| 216 | 141 | /** |
| 217 | 142 | * @since 2.0.8 |
| 218 | - * | |
| 219 | - * @param object $t | |
| 220 | - * | |
| 221 | - * @return int|string | |
| 222 | 143 | */ |
| 223 | 144 | private static function get_term_parent_id( $t ) { |
| 224 | 145 | $parent = (string) $t->term_parent; |
| 225 | - | |
| 226 | - if ( $parent ) { | |
| 146 | + if ( ! empty( $parent ) ) { | |
| 227 | 147 | $parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy ); |
| 228 | - $parent = $parent ? $parent['term_id'] : 0; | |
| 148 | + if ( $parent ) { | |
| 149 | + $parent = $parent['term_id']; | |
| 150 | + } else { | |
| 151 | + $parent = 0; | |
| 152 | + } | |
| 229 | 153 | } |
| 230 | 154 | |
| 231 | 155 | return $parent; |
| 232 | 156 | } |
| 233 | 157 | |
| 234 | - /** | |
| 235 | - * @param SimpleXMLElement $forms | |
| 236 | - * @param array $imported | |
| 237 | - * | |
| 238 | - * @return array | |
| 239 | - */ | |
| 240 | 158 | public static function import_xml_forms( $forms, $imported ) { |
| 241 | 159 | $child_forms = array(); |
| 242 | 160 | |
| 243 | 161 | // Import child forms first |
| @@ -247,13 +165,13 @@ | ||
| 247 | 165 | $form = self::fill_form( $item ); |
| 248 | 166 | |
| 249 | 167 | self::update_custom_style_setting_on_import( $form ); |
| 250 | 168 | |
| 251 | - $this_form = self::maybe_get_form( $form ); | |
| 169 | + $this_form = self::maybe_get_form( $form ); | |
| 170 | + | |
| 252 | 171 | $old_id = false; |
| 253 | 172 | $form_fields = false; |
| 254 | - | |
| 255 | - if ( $this_form ) { | |
| 173 | + if ( ! empty( $this_form ) ) { | |
| 256 | 174 | $form_id = $this_form->id; |
| 257 | 175 | $old_id = $this_form->id; |
| 258 | 176 | self::update_form( $this_form, $form, $imported ); |
| 259 | 177 | |
| @@ -259,13 +177,12 @@ | ||
| 259 | 177 | |
| 260 | 178 | $form_fields = self::get_form_fields( $form_id ); |
| 261 | 179 | } else { |
| 262 | 180 | $form_id = FrmForm::create( $form ); |
| 263 | - | |
| 264 | 181 | if ( $form_id ) { |
| 265 | 182 | if ( empty( $form['parent_form_id'] ) ) { |
| 266 | 183 | // Don't include the repeater form in the imported count. |
| 267 | - ++$imported['imported']['forms']; | |
| 184 | + $imported['imported']['forms'] ++; | |
| 268 | 185 | } |
| 269 | 186 | |
| 270 | 187 | // Keep track of whether this specific form was updated or not. |
| 271 | 188 | $imported['form_status'][ $form_id ] = 'imported'; |
| @@ -290,35 +207,15 @@ | ||
| 290 | 207 | |
| 291 | 208 | do_action( 'frm_after_import_form', $form_id, $form ); |
| 292 | 209 | |
| 293 | 210 | unset( $form, $item ); |
| 294 | - }//end foreach | |
| 211 | + } | |
| 295 | 212 | |
| 296 | 213 | self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms ); |
| 297 | 214 | |
| 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 | 215 | return $imported; |
| 314 | 216 | } |
| 315 | 217 | |
| 316 | - /** | |
| 317 | - * @param object $item | |
| 318 | - * | |
| 319 | - * @return array | |
| 320 | - */ | |
| 321 | 218 | private static function fill_form( $item ) { |
| 322 | 219 | $form = array( |
| 323 | 220 | 'id' => (int) $item->id, |
| 324 | 221 | 'form_key' => (string) $item->form_key, |
| @@ -338,57 +235,32 @@ | ||
| 338 | 235 | } |
| 339 | 236 | |
| 340 | 237 | $form['options'] = FrmAppHelper::maybe_json_decode( $form['options'] ); |
| 341 | 238 | |
| 342 | - if ( self::$installing_template ) { | |
| 343 | - // Templates don't necessarily have antispam on, but we want our templates to all have antispam on by default. | |
| 344 | - $form['options']['antispam'] = 1; | |
| 345 | - } | |
| 346 | - | |
| 347 | 239 | return $form; |
| 348 | 240 | } |
| 349 | 241 | |
| 350 | - /** | |
| 351 | - * @param array $form | |
| 352 | - * | |
| 353 | - * @return false|object | |
| 354 | - */ | |
| 355 | 242 | private static function maybe_get_form( $form ) { |
| 356 | - // If template, allow to edit if form keys match, otherwise, creation date must also match | |
| 243 | + // if template, allow to edit if form keys match, otherwise, creation date must also match | |
| 357 | 244 | $edit_query = array( |
| 358 | 245 | 'form_key' => $form['form_key'], |
| 359 | 246 | 'is_template' => $form['is_template'], |
| 360 | 247 | ); |
| 361 | - | |
| 362 | 248 | if ( ! $form['is_template'] ) { |
| 363 | 249 | $edit_query['created_at'] = $form['created_at']; |
| 364 | 250 | } |
| 365 | 251 | |
| 366 | 252 | $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form ); |
| 367 | - $form = FrmForm::getAll( $edit_query, '', 1 ); | |
| 368 | 253 | |
| 369 | - if ( is_object( $form ) && $form->status === 'trash' ) { | |
| 370 | - FrmForm::destroy( $form->id ); | |
| 371 | - return false; | |
| 372 | - } | |
| 373 | - | |
| 374 | - return $form; | |
| 254 | + return FrmForm::getAll( $edit_query, '', 1 ); | |
| 375 | 255 | } |
| 376 | 256 | |
| 377 | - /** | |
| 378 | - * @param object $this_form | |
| 379 | - * @param array $form | |
| 380 | - * @param array $imported | |
| 381 | - * | |
| 382 | - * @return void | |
| 383 | - */ | |
| 384 | 257 | private static function update_form( $this_form, $form, &$imported ) { |
| 385 | 258 | $form_id = $this_form->id; |
| 386 | 259 | FrmForm::update( $form_id, $form ); |
| 387 | - | |
| 388 | 260 | if ( empty( $form['parent_form_id'] ) ) { |
| 389 | 261 | // Don't include the repeater form in the updated count. |
| 390 | - ++$imported['updated']['forms']; | |
| 262 | + $imported['updated']['forms'] ++; | |
| 391 | 263 | } |
| 392 | 264 | |
| 393 | 265 | // Keep track of whether this specific form was updated or not |
| 394 | 266 | $imported['form_status'][ $form_id ] = 'updated'; |
| @@ -393,43 +265,32 @@ | ||
| 393 | 265 | // Keep track of whether this specific form was updated or not |
| 394 | 266 | $imported['form_status'][ $form_id ] = 'updated'; |
| 395 | 267 | } |
| 396 | 268 | |
| 397 | - /** | |
| 398 | - * @param int|string $form_id | |
| 399 | - * | |
| 400 | - * @return array | |
| 401 | - */ | |
| 402 | 269 | private static function get_form_fields( $form_id ) { |
| 403 | 270 | $form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' ); |
| 404 | 271 | $old_fields = array(); |
| 405 | - | |
| 406 | 272 | foreach ( $form_fields as $f ) { |
| 407 | 273 | $old_fields[ $f->id ] = $f; |
| 408 | 274 | $old_fields[ $f->field_key ] = $f->id; |
| 409 | 275 | unset( $f ); |
| 410 | 276 | } |
| 277 | + $form_fields = $old_fields; | |
| 411 | 278 | |
| 412 | - return $old_fields; | |
| 279 | + return $form_fields; | |
| 413 | 280 | } |
| 414 | 281 | |
| 415 | 282 | /** |
| 416 | 283 | * 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 | 284 | */ |
| 422 | 285 | 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 ); | |
| 286 | + if ( ! empty( $form_fields ) ) { | |
| 287 | + foreach ( $form_fields as $field ) { | |
| 288 | + if ( is_object( $field ) ) { | |
| 289 | + FrmField::destroy( $field->id ); | |
| 290 | + } | |
| 291 | + unset( $field ); | |
| 430 | 292 | } |
| 431 | - unset( $field ); | |
| 432 | 293 | } |
| 433 | 294 | } |
| 434 | 295 | |
| 435 | 296 | /** |
| @@ -460,10 +321,10 @@ | ||
| 460 | 321 | * Keep track of all imported child forms |
| 461 | 322 | * |
| 462 | 323 | * @since 2.0.16 |
| 463 | 324 | * |
| 464 | - * @param int $form_id | |
| 465 | - * @param int $parent_form_id | |
| 325 | + * @param int $form_id | |
| 326 | + * @param int $parent_form_id | |
| 466 | 327 | * @param array $child_forms |
| 467 | 328 | */ |
| 468 | 329 | private static function track_imported_child_forms( $form_id, $parent_form_id, &$child_forms ) { |
| 469 | 330 | if ( $parent_form_id ) { |
| @@ -481,16 +342,14 @@ | ||
| 481 | 342 | * @param array $child_forms |
| 482 | 343 | */ |
| 483 | 344 | private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) { |
| 484 | 345 | 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; | |
| 346 | + if ( isset( $imported_forms[ $old_parent_form_id ] ) && (int) $imported_forms[ $old_parent_form_id ] !== (int) $old_parent_form_id ) { | |
| 347 | + // Update all children with this old parent_form_id | |
| 348 | + $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ]; | |
| 349 | + FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) ); | |
| 350 | + do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id ); | |
| 487 | 351 | } |
| 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 | 352 | } |
| 494 | 353 | } |
| 495 | 354 | |
| 496 | 355 | /** |
| @@ -498,14 +357,8 @@ | ||
| 498 | 357 | * |
| 499 | 358 | * @since 2.0.13 |
| 500 | 359 | * |
| 501 | 360 | * 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 | 361 | */ |
| 509 | 362 | private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$form_fields, &$imported ) { |
| 510 | 363 | $in_section = 0; |
| 511 | 364 | $keys_by_original_field_id = array(); |
| @@ -519,45 +372,40 @@ | ||
| 519 | 372 | self::maybe_update_form_select( $f, $imported ); |
| 520 | 373 | self::maybe_update_get_values_form_setting( $imported, $f ); |
| 521 | 374 | self::migrate_placeholders( $f ); |
| 522 | 375 | |
| 523 | - if ( $this_form ) { | |
| 524 | - // Check for field to edit by field id | |
| 376 | + if ( ! empty( $this_form ) ) { | |
| 377 | + // check for field to edit by field id | |
| 525 | 378 | if ( isset( $form_fields[ $f['id'] ] ) ) { |
| 526 | 379 | FrmField::update( $f['id'], $f ); |
| 527 | - ++$imported['updated']['fields']; | |
| 380 | + $imported['updated']['fields'] ++; | |
| 528 | 381 | |
| 529 | 382 | unset( $form_fields[ $f['id'] ] ); |
| 530 | 383 | |
| 531 | - // Unset old field key. | |
| 384 | + //unset old field key | |
| 532 | 385 | if ( isset( $form_fields[ $f['field_key'] ] ) ) { |
| 533 | 386 | unset( $form_fields[ $f['field_key'] ] ); |
| 534 | 387 | } |
| 535 | 388 | } elseif ( isset( $form_fields[ $f['field_key'] ] ) ) { |
| 536 | 389 | $keys_by_original_field_id[ $f['id'] ] = $f['field_key']; |
| 537 | - $old_field_id = $f['id']; | |
| 538 | 390 | |
| 539 | - // Check for field to edit by field key | |
| 391 | + // check for field to edit by field key | |
| 540 | 392 | unset( $f['id'] ); |
| 541 | 393 | |
| 542 | 394 | FrmField::update( $form_fields[ $f['field_key'] ], $f ); |
| 543 | - ++$imported['updated']['fields']; | |
| 395 | + $imported['updated']['fields'] ++; | |
| 544 | 396 | |
| 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'] ] ); | |
| 397 | + unset( $form_fields[ $form_fields[ $f['field_key'] ] ] ); //unset old field id | |
| 398 | + unset( $form_fields[ $f['field_key'] ] ); //unset old field key | |
| 552 | 399 | } else { |
| 553 | - // If no matching field id or key in this form, create the field. | |
| 400 | + // if no matching field id or key in this form, create the field | |
| 554 | 401 | self::create_imported_field( $f, $imported ); |
| 555 | - }//end if | |
| 402 | + } | |
| 556 | 403 | } else { |
| 404 | + | |
| 557 | 405 | self::create_imported_field( $f, $imported ); |
| 558 | - }//end if | |
| 559 | - }//end foreach | |
| 406 | + } | |
| 407 | + } | |
| 560 | 408 | |
| 561 | 409 | if ( $keys_by_original_field_id ) { |
| 562 | 410 | self::maybe_update_field_ids( $form_id, $keys_by_original_field_id ); |
| 563 | 411 | } |
| @@ -562,53 +410,8 @@ | ||
| 562 | 410 | self::maybe_update_field_ids( $form_id, $keys_by_original_field_id ); |
| 563 | 411 | } |
| 564 | 412 | } |
| 565 | 413 | |
| 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 | 414 | private static function fill_field( $field, $form_id ) { |
| 612 | 415 | return array( |
| 613 | 416 | 'id' => (int) $field->id, |
| 614 | 417 | 'field_key' => (string) $field->field_key, |
| @@ -625,12 +428,8 @@ | ||
| 625 | 428 | } |
| 626 | 429 | |
| 627 | 430 | /** |
| 628 | 431 | * @since 4.06 |
| 629 | - * | |
| 630 | - * @param array $f | |
| 631 | - * | |
| 632 | - * @return void | |
| 633 | 432 | */ |
| 634 | 433 | private static function set_default_value( &$f ) { |
| 635 | 434 | $has_default = array( |
| 636 | 435 | 'text', |
| @@ -644,17 +443,15 @@ | ||
| 644 | 443 | 'password', |
| 645 | 444 | 'tag', |
| 646 | 445 | ); |
| 647 | 446 | |
| 648 | - if ( ! is_array( $f['default_value'] ) || ! in_array( $f['type'], $has_default, true ) ) { | |
| 649 | - return; | |
| 447 | + if ( is_array( $f['default_value'] ) && in_array( $f['type'], $has_default, true ) ) { | |
| 448 | + if ( count( $f['default_value'] ) === 1 ) { | |
| 449 | + $f['default_value'] = '[' . reset( $f['default_value'] ) . ']'; | |
| 450 | + } else { | |
| 451 | + $f['default_value'] = reset( $f['default_value'] ); | |
| 452 | + } | |
| 650 | 453 | } |
| 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 | 454 | } |
| 658 | 455 | |
| 659 | 456 | /** |
| 660 | 457 | * Make sure the required indicator is set. |
| @@ -659,12 +456,8 @@ | ||
| 659 | 456 | /** |
| 660 | 457 | * Make sure the required indicator is set. |
| 661 | 458 | * |
| 662 | 459 | * @since 4.05 |
| 663 | - * | |
| 664 | - * @param array $f | |
| 665 | - * | |
| 666 | - * @return void | |
| 667 | 460 | */ |
| 668 | 461 | private static function maybe_add_required( &$f ) { |
| 669 | 462 | if ( $f['required'] && ! isset( $f['field_options']['required_indicator'] ) ) { |
| 670 | 463 | $f['field_options']['required_indicator'] = '*'; |
| @@ -674,17 +467,14 @@ | ||
| 674 | 467 | /** |
| 675 | 468 | * Update the current in_section value at the beginning of the field loop |
| 676 | 469 | * |
| 677 | 470 | * @since 2.0.25 |
| 678 | - * | |
| 679 | - * @param int $in_section | |
| 471 | + * @param int $in_section | |
| 680 | 472 | * @param array $f |
| 681 | - * | |
| 682 | - * @return void | |
| 683 | 473 | */ |
| 684 | 474 | private static function maybe_update_in_section_variable( &$in_section, &$f ) { |
| 685 | 475 | // 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 ) ) { | |
| 476 | + if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) { | |
| 687 | 477 | $in_section = 0; |
| 688 | 478 | } |
| 689 | 479 | |
| 690 | 480 | // Update the current field's in_section value |
| @@ -692,9 +482,9 @@ | ||
| 692 | 482 | $f['field_options']['in_section'] = $in_section; |
| 693 | 483 | } |
| 694 | 484 | |
| 695 | 485 | // If we're starting a new section, switch $in_section to ID of divider |
| 696 | - if ( $f['type'] === 'divider' ) { | |
| 486 | + if ( $f['type'] == 'divider' ) { | |
| 697 | 487 | $in_section = $f['id']; |
| 698 | 488 | } |
| 699 | 489 | } |
| 700 | 490 | |
| @@ -710,21 +500,16 @@ | ||
| 710 | 500 | if ( ! isset( $imported['forms'] ) ) { |
| 711 | 501 | return; |
| 712 | 502 | } |
| 713 | 503 | |
| 714 | - if ( $f['type'] !== 'form' && ( $f['type'] !== 'divider' || ! FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { | |
| 715 | - return; | |
| 504 | + if ( $f['type'] == 'form' || ( $f['type'] == 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { | |
| 505 | + if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) { | |
| 506 | + $form_select = (int) $f['field_options']['form_select']; | |
| 507 | + if ( isset( $imported['forms'][ $form_select ] ) ) { | |
| 508 | + $f['field_options']['form_select'] = $imported['forms'][ $form_select ]; | |
| 509 | + } | |
| 510 | + } | |
| 716 | 511 | } |
| 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 | 512 | } |
| 728 | 513 | |
| 729 | 514 | /** |
| 730 | 515 | * Update the get_values_form setting if the form was imported |
| @@ -732,10 +517,8 @@ | ||
| 732 | 517 | * @since 2.01.0 |
| 733 | 518 | * |
| 734 | 519 | * @param array $imported |
| 735 | 520 | * @param array $f |
| 736 | - * | |
| 737 | - * @return void | |
| 738 | 521 | */ |
| 739 | 522 | private static function maybe_update_get_values_form_setting( $imported, &$f ) { |
| 740 | 523 | if ( ! isset( $imported['forms'] ) ) { |
| 741 | 524 | return; |
| @@ -740,35 +523,36 @@ | ||
| 740 | 523 | if ( ! isset( $imported['forms'] ) ) { |
| 741 | 524 | return; |
| 742 | 525 | } |
| 743 | 526 | |
| 744 | - if ( ! FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) { | |
| 745 | - return; | |
| 527 | + if ( FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) { | |
| 528 | + $old_form = $f['field_options']['get_values_form']; | |
| 529 | + if ( isset( $imported['forms'][ $old_form ] ) ) { | |
| 530 | + $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ]; | |
| 531 | + } | |
| 746 | 532 | } |
| 533 | + } | |
| 747 | 534 | |
| 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 | - } | |
| 535 | + /** | |
| 536 | + * If field settings have been migrated, update the values during import. | |
| 537 | + * | |
| 538 | + * @since 4.0 | |
| 539 | + */ | |
| 540 | + private static function run_field_migrations( &$f ) { | |
| 541 | + self::migrate_placeholders( $f ); | |
| 542 | + $f = apply_filters( 'frm_import_xml_field', $f ); | |
| 753 | 543 | } |
| 754 | 544 | |
| 755 | 545 | /** |
| 756 | 546 | * @since 4.0 |
| 757 | - * | |
| 758 | - * @param array $f | |
| 759 | - * | |
| 760 | - * @return void | |
| 761 | 547 | */ |
| 762 | 548 | private static function migrate_placeholders( &$f ) { |
| 763 | 549 | $update_values = self::migrate_field_placeholder( $f, 'clear_on_focus' ); |
| 764 | - | |
| 765 | 550 | foreach ( $update_values as $k => $v ) { |
| 766 | 551 | $f[ $k ] = $v; |
| 767 | 552 | } |
| 768 | 553 | |
| 769 | 554 | $update_values = self::migrate_field_placeholder( $f, 'default_blank' ); |
| 770 | - | |
| 771 | 555 | foreach ( $update_values as $k => $v ) { |
| 772 | 556 | $f[ $k ] = $v; |
| 773 | 557 | } |
| 774 | 558 | } |
| @@ -777,18 +561,13 @@ | ||
| 777 | 561 | * Move clear_on_focus or default_blank to placeholder. |
| 778 | 562 | * Also called during database migration in FrmMigrate. |
| 779 | 563 | * |
| 780 | 564 | * @since 4.0 |
| 781 | - * | |
| 782 | - * @param array|object $field | |
| 783 | - * @param string $type | |
| 784 | - * | |
| 785 | 565 | * @return array |
| 786 | 566 | */ |
| 787 | 567 | public static function migrate_field_placeholder( $field, $type ) { |
| 788 | - $field = (array) $field; | |
| 568 | + $field = (array) $field; | |
| 789 | 569 | $field_options = $field['field_options']; |
| 790 | - | |
| 791 | 570 | if ( empty( $field_options[ $type ] ) || empty( $field['default_value'] ) ) { |
| 792 | 571 | return array(); |
| 793 | 572 | } |
| 794 | 573 | |
| @@ -801,34 +580,27 @@ | ||
| 801 | 580 | ); |
| 802 | 581 | |
| 803 | 582 | // If a dropdown placeholder was used, remove the option so it won't be included twice. |
| 804 | 583 | $options = $field['options']; |
| 584 | + if ( $type === 'default_blank' && is_array( $options ) ) { | |
| 585 | + $default_value = $field['default_value']; | |
| 586 | + if ( is_array( $default_value ) ) { | |
| 587 | + $default_value = reset( $default_value ); | |
| 588 | + } | |
| 805 | 589 | |
| 806 | - if ( $type !== 'default_blank' || ! is_array( $options ) ) { | |
| 807 | - return $changes; | |
| 808 | - } | |
| 590 | + foreach ( $options as $opt_key => $opt ) { | |
| 591 | + if ( is_array( $opt ) ) { | |
| 592 | + $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) ); | |
| 593 | + } | |
| 809 | 594 | |
| 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 ); | |
| 595 | + if ( $opt == $default_value ) { | |
| 596 | + unset( $options[ $opt_key ] ); | |
| 597 | + break; | |
| 598 | + } | |
| 819 | 599 | } |
| 820 | - | |
| 821 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 822 | - if ( $opt == $default_value ) { | |
| 823 | - unset( $options[ $opt_key ] ); | |
| 824 | - break; | |
| 825 | - } | |
| 600 | + $changes['options'] = $options; | |
| 826 | 601 | } |
| 827 | 602 | |
| 828 | - $changes['options'] = $options; | |
| 829 | - // end if | |
| 830 | - | |
| 831 | 603 | return $changes; |
| 832 | 604 | } |
| 833 | 605 | |
| 834 | 606 | /** |
| @@ -837,72 +609,26 @@ | ||
| 837 | 609 | * @since 2.0.25 |
| 838 | 610 | * |
| 839 | 611 | * @param array $f |
| 840 | 612 | * @param array $imported |
| 841 | - * | |
| 842 | - * @return void | |
| 843 | 613 | */ |
| 844 | 614 | private static function create_imported_field( $f, &$imported ) { |
| 845 | - $f = self::update_field_options_with_defaults( $f ); | |
| 615 | + $defaults = self::default_field_options( $f['type'] ); | |
| 616 | + $f['field_options'] = array_merge( $defaults, $f['field_options'] ); | |
| 846 | 617 | |
| 847 | - if ( is_callable( 'FrmProFileImport::import_attachment' ) ) { | |
| 848 | - $f = self::maybe_import_images_for_options( $f ); | |
| 849 | - } | |
| 850 | - | |
| 851 | 618 | $new_id = FrmField::create( $f ); |
| 852 | - | |
| 853 | - if ( ! $new_id ) { | |
| 854 | - return; | |
| 619 | + if ( $new_id != false ) { | |
| 620 | + $imported['imported']['fields'] ++; | |
| 621 | + do_action( 'frm_after_field_is_imported', $f, $new_id ); | |
| 855 | 622 | } |
| 856 | - | |
| 857 | - ++$imported['imported']['fields']; | |
| 858 | - do_action( 'frm_after_field_is_imported', $f, $new_id ); | |
| 859 | 623 | } |
| 860 | 624 | |
| 861 | 625 | /** |
| 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 | 626 | * Fix field ids for fields that already exist prior to import. |
| 898 | 627 | * |
| 899 | 628 | * @since 4.07 |
| 900 | - * | |
| 901 | - * @param int $form_id | |
| 629 | + * @param int $form_id | |
| 902 | 630 | * @param array $keys_by_original_field_id |
| 903 | - * | |
| 904 | - * @return void | |
| 905 | 631 | */ |
| 906 | 632 | protected static function maybe_update_field_ids( $form_id, $keys_by_original_field_id ) { |
| 907 | 633 | global $frm_duplicate_ids; |
| 908 | 634 | |
| @@ -922,17 +648,15 @@ | ||
| 922 | 648 | $field = (array) $field; |
| 923 | 649 | $frm_duplicate_ids = $keys_by_original_field_id; |
| 924 | 650 | $after = FrmFieldsHelper::switch_field_ids( $field ); |
| 925 | 651 | |
| 926 | - if ( $before['field_options'] === $after['field_options'] ) { | |
| 927 | - continue; | |
| 928 | - } | |
| 652 | + if ( $before['field_options'] !== $after['field_options'] ) { | |
| 653 | + $frm_duplicate_ids = $field_id_by_key; | |
| 654 | + $after = FrmFieldsHelper::switch_field_ids( $after ); | |
| 929 | 655 | |
| 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'] ) ); | |
| 656 | + if ( $before['field_options'] !== $after['field_options'] ) { | |
| 657 | + FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) ); | |
| 658 | + } | |
| 935 | 659 | } |
| 936 | 660 | } |
| 937 | 661 | |
| 938 | 662 | $frm_duplicate_ids = $former_duplicate_ids; |
| @@ -944,10 +668,8 @@ | ||
| 944 | 668 | * |
| 945 | 669 | * @since 2.0.19 |
| 946 | 670 | * |
| 947 | 671 | * @param array $form |
| 948 | - * | |
| 949 | - * @return void | |
| 950 | 672 | */ |
| 951 | 673 | private static function update_custom_style_setting_on_import( &$form ) { |
| 952 | 674 | if ( ! isset( $form['options']['custom_style'] ) ) { |
| 953 | 675 | return; |
| @@ -952,35 +674,32 @@ | ||
| 952 | 674 | if ( ! isset( $form['options']['custom_style'] ) ) { |
| 953 | 675 | return; |
| 954 | 676 | } |
| 955 | 677 | |
| 956 | - if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) { | |
| 678 | + if ( is_numeric( $form['options']['custom_style'] ) ) { | |
| 957 | 679 | // Set to default |
| 958 | 680 | $form['options']['custom_style'] = 1; |
| 681 | + } else { | |
| 682 | + // Replace the style name with the style ID on import | |
| 683 | + global $wpdb; | |
| 684 | + $table = $wpdb->prefix . 'posts'; | |
| 685 | + $where = array( | |
| 686 | + 'post_name' => $form['options']['custom_style'], | |
| 687 | + 'post_type' => 'frm_styles', | |
| 688 | + ); | |
| 689 | + $select = 'ID'; | |
| 690 | + $style_id = FrmDb::get_var( $table, $where, $select ); | |
| 959 | 691 | |
| 960 | - return; | |
| 961 | - } | |
| 692 | + if ( $style_id ) { | |
| 693 | + $form['options']['custom_style'] = $style_id; | |
| 694 | + } else { | |
| 695 | + // save the old style to maybe update after styles import | |
| 696 | + $form['options']['old_style'] = $form['options']['custom_style']; | |
| 962 | 697 | |
| 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; | |
| 698 | + // Set to default | |
| 699 | + $form['options']['custom_style'] = 1; | |
| 700 | + } | |
| 976 | 701 | } |
| 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 | 702 | } |
| 984 | 703 | |
| 985 | 704 | /** |
| 986 | 705 | * After styles are imported, check for any forms that were linked |
| @@ -986,40 +705,25 @@ | ||
| 986 | 705 | * After styles are imported, check for any forms that were linked |
| 987 | 706 | * and link them back up. |
| 988 | 707 | * |
| 989 | 708 | * @since 2.2.7 |
| 990 | - * | |
| 991 | - * @param int|string $form_id | |
| 992 | - * | |
| 993 | - * @return void | |
| 994 | 709 | */ |
| 995 | 710 | private static function update_custom_style_setting_after_import( $form_id ) { |
| 996 | 711 | $form = FrmForm::getOne( $form_id ); |
| 997 | 712 | |
| 998 | - if ( ! $form || ! isset( $form->options['old_style'] ) ) { | |
| 999 | - return; | |
| 713 | + if ( $form && isset( $form->options['old_style'] ) ) { | |
| 714 | + $form = (array) $form; | |
| 715 | + $saved_style = $form['options']['custom_style']; | |
| 716 | + $form['options']['custom_style'] = $form['options']['old_style']; | |
| 717 | + self::update_custom_style_setting_on_import( $form ); | |
| 718 | + $has_changed = ( $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style'] ); | |
| 719 | + if ( $has_changed ) { | |
| 720 | + FrmForm::update( $form['id'], $form ); | |
| 721 | + } | |
| 1000 | 722 | } |
| 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 | 723 | } |
| 1012 | 724 | |
| 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 | |
| 725 | + public static function import_xml_views( $views, $imported ) { | |
| 1022 | 726 | $imported['posts'] = array(); |
| 1023 | 727 | $form_action_type = FrmFormActionsController::$action_post_type; |
| 1024 | 728 | |
| 1025 | 729 | $post_types = array( |
| @@ -1027,11 +731,8 @@ | ||
| 1027 | 731 | $form_action_type => 'actions', |
| 1028 | 732 | 'frm_styles' => 'styles', |
| 1029 | 733 | ); |
| 1030 | 734 | |
| 1031 | - $view_ids = array(); | |
| 1032 | - $posts_with_shortcodes = array(); | |
| 1033 | - | |
| 1034 | 735 | foreach ( $views as $item ) { |
| 1035 | 736 | $post = array( |
| 1036 | 737 | 'post_title' => (string) $item->title, |
| 1037 | 738 | 'post_name' => (string) $item->post_name, |
| @@ -1054,15 +755,8 @@ | ||
| 1054 | 755 | 'layout' => array(), |
| 1055 | 756 | 'tax_input' => array(), |
| 1056 | 757 | ); |
| 1057 | 758 | |
| 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 | 759 | $old_id = $post['post_id']; |
| 1066 | 760 | self::populate_post( $post, $item, $imported ); |
| 1067 | 761 | |
| 1068 | 762 | unset( $item ); |
| @@ -1067,246 +761,85 @@ | ||
| 1067 | 761 | |
| 1068 | 762 | unset( $item ); |
| 1069 | 763 | |
| 1070 | 764 | $post_id = false; |
| 1071 | - | |
| 1072 | - if ( $post['post_type'] === $form_action_type ) { | |
| 765 | + if ( $post['post_type'] == $form_action_type ) { | |
| 1073 | 766 | $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] ); |
| 1074 | - | |
| 1075 | - if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) { | |
| 767 | + if ( $action_control && is_object( $action_control ) ) { | |
| 1076 | 768 | $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] ); |
| 1077 | 769 | } |
| 1078 | 770 | unset( $action_control ); |
| 1079 | - } elseif ( $post['post_type'] === 'frm_styles' ) { | |
| 771 | + } elseif ( $post['post_type'] == 'frm_styles' ) { | |
| 1080 | 772 | // Properly encode post content before inserting the post |
| 1081 | 773 | $post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] ); |
| 774 | + $custom_css = isset( $post['post_content']['custom_css'] ) ? $post['post_content']['custom_css'] : ''; | |
| 1082 | 775 | $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 | 776 | |
| 1086 | 777 | // Create/update post now |
| 1087 | 778 | $post_id = wp_insert_post( $post ); |
| 779 | + self::maybe_update_custom_css( $custom_css ); | |
| 1088 | 780 | } else { |
| 1089 | 781 | if ( $post['post_type'] === 'frm_display' ) { |
| 1090 | 782 | $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 | 783 | } |
| 1094 | 784 | // Create/update post now |
| 1095 | 785 | $post_id = wp_insert_post( $post ); |
| 1096 | - }//end if | |
| 786 | + } | |
| 1097 | 787 | |
| 1098 | 788 | if ( ! is_numeric( $post_id ) ) { |
| 1099 | 789 | continue; |
| 1100 | 790 | } |
| 1101 | 791 | |
| 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 | 792 | self::update_postmeta( $post, $post_id ); |
| 1107 | 793 | self::update_layout( $post, $post_id ); |
| 1108 | 794 | |
| 1109 | 795 | $this_type = 'posts'; |
| 1110 | - | |
| 1111 | 796 | if ( isset( $post_types[ $post['post_type'] ] ) ) { |
| 1112 | 797 | $this_type = $post_types[ $post['post_type'] ]; |
| 1113 | 798 | } |
| 1114 | 799 | |
| 1115 | - if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) { | |
| 1116 | - ++$imported['updated'][ $this_type ]; | |
| 800 | + if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) { | |
| 801 | + $imported['updated'][ $this_type ] ++; | |
| 1117 | 802 | } else { |
| 1118 | - ++$imported['imported'][ $this_type ]; | |
| 803 | + $imported['imported'][ $this_type ] ++; | |
| 1119 | 804 | } |
| 1120 | 805 | |
| 1121 | - $imported['posts'][ $old_id ] = $post_id; | |
| 806 | + $imported['posts'][ (int) $old_id ] = $post_id; | |
| 1122 | 807 | |
| 1123 | - if ( $post['post_type'] === 'frm_display' ) { | |
| 1124 | - $view_ids[ $old_id ] = $post_id; | |
| 1125 | - } | |
| 1126 | - | |
| 1127 | 808 | do_action( 'frm_after_import_view', $post_id, $post ); |
| 1128 | 809 | |
| 1129 | 810 | 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 | 811 | } |
| 1135 | - unset( $posts_with_shortcodes, $view_ids ); | |
| 1136 | 812 | |
| 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 | 813 | self::maybe_update_stylesheet( $imported ); |
| 1143 | 814 | |
| 1144 | - flush_rewrite_rules(); | |
| 1145 | - | |
| 1146 | 815 | return $imported; |
| 1147 | 816 | } |
| 1148 | 817 | |
| 1149 | 818 | /** |
| 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 | 819 | * @param string $content |
| 1277 | - * | |
| 1278 | 820 | * @return string |
| 1279 | 821 | */ |
| 1280 | 822 | private static function maybe_prepare_json_view_content( $content ) { |
| 1281 | 823 | $maybe_decoded = FrmAppHelper::maybe_json_decode( $content ); |
| 1282 | - | |
| 1283 | 824 | if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) { |
| 1284 | 825 | return FrmAppHelper::prepare_and_encode( $maybe_decoded ); |
| 1285 | 826 | } |
| 1286 | - | |
| 1287 | 827 | return $content; |
| 1288 | 828 | } |
| 1289 | 829 | |
| 1290 | - /** | |
| 1291 | - * @param array $post | |
| 1292 | - * @param object $item | |
| 1293 | - * @param array $imported | |
| 1294 | - * | |
| 1295 | - * @return void | |
| 1296 | - */ | |
| 1297 | 830 | private static function populate_post( &$post, $item, $imported ) { |
| 1298 | 831 | if ( isset( $item->attachment_url ) ) { |
| 1299 | 832 | $post['attachment_url'] = (string) $item->attachment_url; |
| 1300 | 833 | } |
| 1301 | 834 | |
| 1302 | - if ( $post['post_type'] === FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { | |
| 1303 | - // Update to new form id | |
| 835 | + if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { | |
| 836 | + // update to new form id | |
| 1304 | 837 | $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ]; |
| 1305 | 838 | } |
| 1306 | 839 | |
| 1307 | 840 | // Don't allow default styles to take over a site's default style |
| 1308 | - if ( 'frm_styles' === $post['post_type'] ) { | |
| 841 | + if ( 'frm_styles' == $post['post_type'] ) { | |
| 1309 | 842 | $post['menu_order'] = 0; |
| 1310 | 843 | } |
| 1311 | 844 | |
| 1312 | 845 | foreach ( $item->postmeta as $meta ) { |
| @@ -1328,9 +861,9 @@ | ||
| 1328 | 861 | * @param array $post |
| 1329 | 862 | * @param stdClass $meta |
| 1330 | 863 | * @param array $imported |
| 1331 | 864 | */ |
| 1332 | - private static function populate_postmeta( &$post, $meta, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 865 | + private static function populate_postmeta( &$post, $meta, $imported ) { | |
| 1333 | 866 | global $frm_duplicate_ids; |
| 1334 | 867 | |
| 1335 | 868 | $m = array( |
| 1336 | 869 | 'key' => (string) $meta->meta_key, |
| @@ -1336,19 +869,20 @@ | ||
| 1336 | 869 | 'key' => (string) $meta->meta_key, |
| 1337 | 870 | 'value' => (string) $meta->meta_value, |
| 1338 | 871 | ); |
| 1339 | 872 | |
| 1340 | - // Switch old form and field ids to new ones. | |
| 873 | + //switch old form and field ids to new ones | |
| 1341 | 874 | if ( 'frm_form_id' === $m['key'] && isset( $imported['forms'][ (int) $m['value'] ] ) ) { |
| 1342 | 875 | $m['value'] = $imported['forms'][ (int) $m['value'] ]; |
| 1343 | 876 | } else { |
| 1344 | 877 | $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] ); |
| 1345 | 878 | |
| 1346 | - if ( $frm_duplicate_ids ) { | |
| 879 | + if ( ! empty( $frm_duplicate_ids ) ) { | |
| 1347 | 880 | if ( 'frm_dyncontent' === $m['key'] ) { |
| 1348 | 881 | $m['value'] = self::maybe_prepare_json_view_content( $m['value'] ); |
| 1349 | 882 | $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] ); |
| 1350 | 883 | } elseif ( 'frm_options' === $m['key'] ) { |
| 884 | + | |
| 1351 | 885 | foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) { |
| 1352 | 886 | if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) { |
| 1353 | 887 | $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
| 1354 | 888 | } |
| @@ -1353,38 +887,10 @@ | ||
| 1353 | 887 | $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
| 1354 | 888 | } |
| 1355 | 889 | } |
| 1356 | 890 | |
| 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 | 891 | $check_dup_array = array(); |
| 1385 | - | |
| 1386 | - if ( ! empty( $m['value']['order_by'] ) ) { | |
| 892 | + if ( isset( $m['value']['order_by'] ) && ! empty( $m['value']['order_by'] ) ) { | |
| 1387 | 893 | if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) { |
| 1388 | 894 | $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ]; |
| 1389 | 895 | } elseif ( is_array( $m['value']['order_by'] ) ) { |
| 1390 | 896 | $check_dup_array[] = 'order_by'; |
| @@ -1390,9 +896,9 @@ | ||
| 1390 | 896 | $check_dup_array[] = 'order_by'; |
| 1391 | 897 | } |
| 1392 | 898 | } |
| 1393 | 899 | |
| 1394 | - if ( ! empty( $m['value']['where'] ) ) { | |
| 900 | + if ( isset( $m['value']['where'] ) && ! empty( $m['value']['where'] ) ) { | |
| 1395 | 901 | $check_dup_array[] = 'where'; |
| 1396 | 902 | } |
| 1397 | 903 | |
| 1398 | 904 | foreach ( $check_dup_array as $check_k ) { |
| @@ -1402,11 +908,11 @@ | ||
| 1402 | 908 | } |
| 1403 | 909 | unset( $mk, $mv ); |
| 1404 | 910 | } |
| 1405 | 911 | } |
| 1406 | - }//end if | |
| 1407 | - }//end if | |
| 1408 | - }//end if | |
| 912 | + } | |
| 913 | + } | |
| 914 | + } | |
| 1409 | 915 | |
| 1410 | 916 | if ( ! is_array( $m['value'] ) ) { |
| 1411 | 917 | $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] ); |
| 1412 | 918 | } |
| @@ -1413,14 +919,8 @@ | ||
| 1413 | 919 | |
| 1414 | 920 | $post['postmeta'][ (string) $meta->meta_key ] = $m['value']; |
| 1415 | 921 | } |
| 1416 | 922 | |
| 1417 | - /** | |
| 1418 | - * @param array $post | |
| 1419 | - * @param object $layout | |
| 1420 | - * | |
| 1421 | - * @return void | |
| 1422 | - */ | |
| 1423 | 923 | private static function populate_layout( &$post, $layout ) { |
| 1424 | 924 | $post['layout'][ (string) $layout->type ] = (string) $layout->data; |
| 1425 | 925 | } |
| 1426 | 926 | |
| @@ -1426,25 +926,22 @@ | ||
| 1426 | 926 | |
| 1427 | 927 | /** |
| 1428 | 928 | * Add terms to post |
| 1429 | 929 | * |
| 1430 | - * @param array $post By reference. | |
| 1431 | - * @param object $item The XML object data. | |
| 930 | + * @param array $post by reference | |
| 931 | + * @param object $item The XML object data | |
| 1432 | 932 | */ |
| 1433 | 933 | private static function populate_taxonomies( &$post, $item ) { |
| 1434 | 934 | foreach ( $item->category as $c ) { |
| 1435 | 935 | $att = $c->attributes(); |
| 1436 | - | |
| 1437 | 936 | if ( ! isset( $att['nicename'] ) ) { |
| 1438 | 937 | continue; |
| 1439 | 938 | } |
| 1440 | 939 | |
| 1441 | 940 | $taxonomy = (string) $att['domain']; |
| 1442 | - | |
| 1443 | 941 | if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 1444 | 942 | $name = (string) $att['nicename']; |
| 1445 | 943 | $h_term = get_term_by( 'slug', $name, $taxonomy ); |
| 1446 | - | |
| 1447 | 944 | if ( $h_term ) { |
| 1448 | 945 | $name = $h_term->term_id; |
| 1449 | 946 | } |
| 1450 | 947 | unset( $h_term ); |
| @@ -1457,17 +954,13 @@ | ||
| 1457 | 954 | } |
| 1458 | 955 | |
| 1459 | 956 | $post['tax_input'][ $taxonomy ][] = $name; |
| 1460 | 957 | unset( $name ); |
| 1461 | - }//end foreach | |
| 958 | + } | |
| 1462 | 959 | } |
| 1463 | 960 | |
| 1464 | 961 | /** |
| 1465 | - * Edit post if the key and created time match. | |
| 1466 | - * | |
| 1467 | - * @param array $post By reference. | |
| 1468 | - * | |
| 1469 | - * @return void | |
| 962 | + * Edit post if the key and created time match | |
| 1470 | 963 | */ |
| 1471 | 964 | private static function maybe_editing_post( &$post ) { |
| 1472 | 965 | $match_by = array( |
| 1473 | 966 | 'post_type' => $post['post_type'], |
| @@ -1475,9 +968,9 @@ | ||
| 1475 | 968 | 'post_status' => $post['post_status'], |
| 1476 | 969 | 'posts_per_page' => 1, |
| 1477 | 970 | ); |
| 1478 | 971 | |
| 1479 | - if ( in_array( $post['post_status'], array( 'trash', 'draft' ), true ) ) { | |
| 972 | + if ( in_array( $post['post_status'], array( 'trash', 'draft' ) ) ) { | |
| 1480 | 973 | $match_by['include'] = $post['post_id']; |
| 1481 | 974 | unset( $match_by['name'] ); |
| 1482 | 975 | } |
| 1483 | 976 | |
| @@ -1482,50 +975,32 @@ | ||
| 1482 | 975 | } |
| 1483 | 976 | |
| 1484 | 977 | $editing = get_posts( $match_by ); |
| 1485 | 978 | |
| 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 | |
| 979 | + if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) { | |
| 980 | + // set the id of the post to edit | |
| 1489 | 981 | $post['ID'] = current( $editing )->ID; |
| 1490 | 982 | } |
| 1491 | 983 | } |
| 1492 | 984 | |
| 1493 | - /** | |
| 1494 | - * @param array $post | |
| 1495 | - * @param int $post_id | |
| 1496 | - * | |
| 1497 | - * @return void | |
| 1498 | - */ | |
| 1499 | 985 | private static function update_postmeta( &$post, $post_id ) { |
| 1500 | 986 | foreach ( $post['postmeta'] as $k => $v ) { |
| 1501 | - switch ( $k ) { | |
| 1502 | - case '_edit_last': | |
| 1503 | - $v = FrmAppHelper::get_user_id_param( $v ); | |
| 1504 | - break; | |
| 987 | + if ( '_edit_last' == $k ) { | |
| 988 | + $v = FrmAppHelper::get_user_id_param( $v ); | |
| 989 | + } elseif ( '_thumbnail_id' == $k && FrmAppHelper::pro_is_installed() ) { | |
| 990 | + // Change the attachment ID. | |
| 991 | + $field_obj = FrmFieldFactory::get_field_type( 'file' ); | |
| 992 | + $v = $field_obj->get_file_id( $v ); | |
| 993 | + } | |
| 1505 | 994 | |
| 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; | |
| 995 | + if ( 'frm_dyncontent' === $k && is_array( $v ) ) { | |
| 996 | + $v = json_encode( $v ); | |
| 997 | + } | |
| 1513 | 998 | |
| 1514 | - case 'frm_dyncontent': | |
| 1515 | - if ( is_array( $v ) ) { | |
| 1516 | - $v = json_encode( $v ); | |
| 1517 | - } | |
| 1518 | - break; | |
| 999 | + update_post_meta( $post_id, $k, $v ); | |
| 1519 | 1000 | |
| 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 | 1001 | unset( $k, $v ); |
| 1527 | - }//end foreach | |
| 1002 | + } | |
| 1528 | 1003 | } |
| 1529 | 1004 | |
| 1530 | 1005 | /** |
| 1531 | 1006 | * @param array $post |
| @@ -1531,83 +1006,61 @@ | ||
| 1531 | 1006 | * @param array $post |
| 1532 | 1007 | * @param int $post_id |
| 1533 | 1008 | */ |
| 1534 | 1009 | private static function update_layout( &$post, $post_id ) { |
| 1535 | - if ( ! is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) { | |
| 1536 | - return; | |
| 1010 | + if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) { | |
| 1011 | + $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array(); | |
| 1012 | + $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array(); | |
| 1013 | + if ( $listing_layout || $detail_layout ) { | |
| 1014 | + FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout ); | |
| 1015 | + } | |
| 1537 | 1016 | } |
| 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 | 1017 | } |
| 1546 | 1018 | |
| 1547 | 1019 | /** |
| 1548 | - * @param array $imported | |
| 1020 | + * If a template includes custom css, let's include it. | |
| 1021 | + * The custom css is included on the default style. | |
| 1549 | 1022 | * |
| 1550 | - * @return void | |
| 1023 | + * @since 2.03 | |
| 1551 | 1024 | */ |
| 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 ) { | |
| 1025 | + private static function maybe_update_custom_css( $custom_css ) { | |
| 1026 | + if ( empty( $custom_css ) ) { | |
| 1557 | 1027 | return; |
| 1558 | 1028 | } |
| 1559 | 1029 | |
| 1560 | - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { | |
| 1561 | - $frm_style = new FrmStyle(); | |
| 1562 | - $frm_style->update( 'default' ); | |
| 1563 | - } | |
| 1030 | + $frm_style = new FrmStyle(); | |
| 1031 | + $default_style = $frm_style->get_default_style(); | |
| 1032 | + $default_style->post_content['custom_css'] .= "\r\n\r\n" . $custom_css; | |
| 1033 | + $frm_style->save( $default_style ); | |
| 1034 | + } | |
| 1564 | 1035 | |
| 1565 | - foreach ( $imported['forms'] as $form_id ) { | |
| 1566 | - self::update_custom_style_setting_after_import( $form_id ); | |
| 1036 | + private static function maybe_update_stylesheet( $imported ) { | |
| 1037 | + $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] ); | |
| 1038 | + $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] ); | |
| 1039 | + if ( $new_styles || $updated_styles ) { | |
| 1040 | + if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { | |
| 1041 | + $frm_style = new FrmStyle(); | |
| 1042 | + $frm_style->update( 'default' ); | |
| 1043 | + } | |
| 1044 | + foreach ( $imported['forms'] as $form_id ) { | |
| 1045 | + self::update_custom_style_setting_after_import( $form_id ); | |
| 1046 | + } | |
| 1567 | 1047 | } |
| 1568 | 1048 | } |
| 1569 | 1049 | |
| 1570 | 1050 | /** |
| 1571 | - * @param mixed $result | |
| 1572 | 1051 | * @param string $message |
| 1573 | - * @param array $errors | |
| 1574 | - * | |
| 1575 | - * @return void | |
| 1576 | 1052 | */ |
| 1577 | 1053 | public static function parse_message( $result, &$message, &$errors ) { |
| 1578 | 1054 | if ( is_wp_error( $result ) ) { |
| 1579 | 1055 | $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 | - | |
| 1056 | + } elseif ( ! $result ) { | |
| 1601 | 1057 | return; |
| 1602 | - }//end if | |
| 1603 | - | |
| 1604 | - if ( ! $result ) { | |
| 1605 | - return; | |
| 1606 | 1058 | } |
| 1607 | 1059 | |
| 1608 | 1060 | if ( ! is_array( $result ) ) { |
| 1609 | 1061 | $message = is_string( $result ) ? $result : htmlentities( print_r( $result, 1 ) ); |
| 1062 | + | |
| 1610 | 1063 | return; |
| 1611 | 1064 | } |
| 1612 | 1065 | |
| 1613 | 1066 | $t_strings = array( |
| @@ -1615,57 +1068,37 @@ | ||
| 1615 | 1068 | 'updated' => __( 'Updated', 'formidable' ), |
| 1616 | 1069 | ); |
| 1617 | 1070 | |
| 1618 | 1071 | $message = '<ul>'; |
| 1619 | - | |
| 1620 | 1072 | foreach ( $result as $type => $results ) { |
| 1621 | 1073 | if ( ! isset( $t_strings[ $type ] ) ) { |
| 1622 | - // Only print imported and updated | |
| 1074 | + // only print imported and updated | |
| 1623 | 1075 | continue; |
| 1624 | 1076 | } |
| 1625 | 1077 | |
| 1626 | 1078 | $s_message = array(); |
| 1627 | - | |
| 1628 | 1079 | foreach ( $results as $k => $m ) { |
| 1629 | 1080 | self::item_count_message( $m, $k, $s_message ); |
| 1630 | 1081 | unset( $k, $m ); |
| 1631 | 1082 | } |
| 1632 | 1083 | |
| 1633 | - if ( ! $s_message ) { | |
| 1634 | - continue; | |
| 1084 | + if ( ! empty( $s_message ) ) { | |
| 1085 | + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; | |
| 1086 | + $message .= implode( ', ', $s_message ); | |
| 1087 | + $message .= '</li>'; | |
| 1635 | 1088 | } |
| 1089 | + } | |
| 1636 | 1090 | |
| 1637 | - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; | |
| 1638 | - $message .= implode( ', ', $s_message ); | |
| 1639 | - $message .= '</li>'; | |
| 1640 | - }//end foreach | |
| 1641 | - | |
| 1642 | - if ( $message === '<ul>' ) { | |
| 1091 | + if ( $message == '<ul>' ) { | |
| 1643 | 1092 | $message = ''; |
| 1644 | 1093 | $errors[] = __( 'Nothing was imported or updated', 'formidable' ); |
| 1094 | + } else { | |
| 1095 | + self::add_form_link_to_message( $result, $message ); | |
| 1645 | 1096 | |
| 1646 | - return; | |
| 1097 | + $message .= '</ul>'; | |
| 1647 | 1098 | } |
| 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 | 1099 | } |
| 1660 | 1100 | |
| 1661 | - /** | |
| 1662 | - * @param int $m | |
| 1663 | - * @param string $type | |
| 1664 | - * @param array<string> $s_message | |
| 1665 | - * | |
| 1666 | - * @return void | |
| 1667 | - */ | |
| 1668 | 1101 | public static function item_count_message( $m, $type, &$s_message ) { |
| 1669 | 1102 | if ( ! $m ) { |
| 1670 | 1103 | return; |
| 1671 | 1104 | } |
| @@ -1679,9 +1112,9 @@ | ||
| 1679 | 1112 | 'items' => sprintf( _n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m ), |
| 1680 | 1113 | /* translators: %1$s: Number of items */ |
| 1681 | 1114 | 'views' => sprintf( _n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m ), |
| 1682 | 1115 | /* translators: %1$s: Number of items */ |
| 1683 | - 'posts' => sprintf( _n( '%1$s Page/Post', '%1$s Pages/Posts', $m, 'formidable' ), $m ), | |
| 1116 | + 'posts' => sprintf( _n( '%1$s Post', '%1$s Posts', $m, 'formidable' ), $m ), | |
| 1684 | 1117 | /* translators: %1$s: Number of items */ |
| 1685 | 1118 | 'styles' => sprintf( _n( '%1$s Style', '%1$s Styles', $m, 'formidable' ), $m ), |
| 1686 | 1119 | /* translators: %1$s: Number of items */ |
| 1687 | 1120 | 'terms' => sprintf( _n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m ), |
| @@ -1688,24 +1121,9 @@ | ||
| 1688 | 1121 | /* translators: %1$s: Number of items */ |
| 1689 | 1122 | 'actions' => sprintf( _n( '%1$s Form Action', '%1$s Form Actions', $m, 'formidable' ), $m ), |
| 1690 | 1123 | ); |
| 1691 | 1124 | |
| 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; | |
| 1125 | + $s_message[] = isset( $strings[ $type ] ) ? $strings[ $type ] : ' ' . $m . ' ' . ucfirst( $type ); | |
| 1708 | 1126 | } |
| 1709 | 1127 | |
| 1710 | 1128 | /** |
| 1711 | 1129 | * If a single form was imported, include a link in the success message. |
| @@ -1710,28 +1128,24 @@ | ||
| 1710 | 1128 | /** |
| 1711 | 1129 | * If a single form was imported, include a link in the success message. |
| 1712 | 1130 | * |
| 1713 | 1131 | * @since 4.0 |
| 1714 | - * | |
| 1715 | 1132 | * @param array $result The response from the XML import. |
| 1716 | 1133 | * @param string $message The response shown on the page after import. |
| 1717 | 1134 | */ |
| 1718 | 1135 | private static function add_form_link_to_message( $result, &$message ) { |
| 1719 | 1136 | $total_forms = $result['imported']['forms'] + $result['updated']['forms']; |
| 1720 | - | |
| 1721 | 1137 | if ( $total_forms > 1 ) { |
| 1722 | 1138 | return; |
| 1723 | 1139 | } |
| 1724 | 1140 | |
| 1725 | 1141 | $primary_form = reset( $result['forms'] ); |
| 1142 | + if ( ! empty( $primary_form ) ) { | |
| 1143 | + $primary_form = FrmForm::getOne( $primary_form ); | |
| 1144 | + $form_id = empty( $primary_form->parent_form_id ) ? $primary_form->id : $primary_form->parent_form_id; | |
| 1726 | 1145 | |
| 1727 | - if ( ! $primary_form ) { | |
| 1728 | - return; | |
| 1146 | + $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>'; | |
| 1729 | 1147 | } |
| 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 | 1148 | } |
| 1735 | 1149 | |
| 1736 | 1150 | /** |
| 1737 | 1151 | * Prepare the form options for export |
| @@ -1744,17 +1158,22 @@ | ||
| 1744 | 1158 | */ |
| 1745 | 1159 | public static function prepare_form_options_for_export( $options ) { |
| 1746 | 1160 | FrmAppHelper::unserialize_or_decode( $options ); |
| 1747 | 1161 | // 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 | - | |
| 1162 | + $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; | |
| 1750 | 1163 | if ( $not_default ) { |
| 1751 | 1164 | 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; | |
| 1165 | + $table = $wpdb->prefix . 'posts'; | |
| 1166 | + $where = array( 'ID' => $options['custom_style'] ); | |
| 1167 | + $select = 'post_name'; | |
| 1168 | + | |
| 1169 | + $style_name = FrmDb::get_var( $table, $where, $select ); | |
| 1170 | + | |
| 1171 | + if ( $style_name ) { | |
| 1172 | + $options['custom_style'] = $style_name; | |
| 1173 | + } else { | |
| 1174 | + $options['custom_style'] = 1; | |
| 1175 | + } | |
| 1757 | 1176 | } |
| 1758 | 1177 | self::remove_default_form_options( $options ); |
| 1759 | 1178 | $options = serialize( $options ); |
| 1760 | 1179 | |
| @@ -1765,16 +1184,11 @@ | ||
| 1765 | 1184 | * If the saved value is the same as the default, remove it from the export |
| 1766 | 1185 | * This keeps file size down and prevents overriding global settings after import |
| 1767 | 1186 | * |
| 1768 | 1187 | * @since 3.06 |
| 1769 | - * | |
| 1770 | - * @param array $options By reference. | |
| 1771 | - * | |
| 1772 | - * @return void | |
| 1773 | 1188 | */ |
| 1774 | 1189 | private static function remove_default_form_options( &$options ) { |
| 1775 | 1190 | $defaults = FrmFormsHelper::get_default_opts(); |
| 1776 | - | |
| 1777 | 1191 | if ( is_callable( 'FrmProFormsHelper::get_default_opts' ) ) { |
| 1778 | 1192 | $defaults += FrmProFormsHelper::get_default_opts(); |
| 1779 | 1193 | } |
| 1780 | 1194 | self::remove_defaults( $defaults, $options ); |
| @@ -1783,16 +1197,11 @@ | ||
| 1783 | 1197 | /** |
| 1784 | 1198 | * Remove extra settings from field to keep file size down |
| 1785 | 1199 | * |
| 1786 | 1200 | * @since 3.06 |
| 1787 | - * | |
| 1788 | - * @param object $field | |
| 1789 | - * | |
| 1790 | - * @return void | |
| 1791 | 1201 | */ |
| 1792 | 1202 | public static function prepare_field_for_export( &$field ) { |
| 1793 | 1203 | self::remove_default_field_options( $field ); |
| 1794 | - self::add_image_src_to_image_options( $field ); | |
| 1795 | 1204 | } |
| 1796 | 1205 | |
| 1797 | 1206 | /** |
| 1798 | 1207 | * Remove defaults from field options too |
| @@ -1797,16 +1206,11 @@ | ||
| 1797 | 1206 | /** |
| 1798 | 1207 | * Remove defaults from field options too |
| 1799 | 1208 | * |
| 1800 | 1209 | * @since 3.06 |
| 1801 | - * | |
| 1802 | - * @param object $field | |
| 1803 | - * | |
| 1804 | - * @return void | |
| 1805 | 1210 | */ |
| 1806 | 1211 | private static function remove_default_field_options( &$field ) { |
| 1807 | 1212 | $defaults = self::default_field_options( $field->type ); |
| 1808 | - | |
| 1809 | 1213 | if ( empty( $defaults['blank'] ) ) { |
| 1810 | 1214 | $global_settings = new FrmSettings(); |
| 1811 | 1215 | $global_defaults = $global_settings->default_options(); |
| 1812 | 1216 | $defaults['blank'] = $global_defaults['blank_msg']; |
| @@ -1829,57 +1233,15 @@ | ||
| 1829 | 1233 | $field->field_options = serialize( $options ); |
| 1830 | 1234 | } |
| 1831 | 1235 | |
| 1832 | 1236 | /** |
| 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 | 1237 | * @since 3.06.03 |
| 1870 | - * | |
| 1871 | - * @param string $type | |
| 1872 | - * | |
| 1873 | - * @return array | |
| 1874 | 1238 | */ |
| 1875 | 1239 | private static function default_field_options( $type ) { |
| 1876 | 1240 | $defaults = FrmFieldsHelper::get_default_field_options( $type ); |
| 1877 | - | |
| 1878 | 1241 | if ( empty( $defaults['custom_html'] ) ) { |
| 1879 | 1242 | $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type ); |
| 1880 | 1243 | } |
| 1881 | - | |
| 1882 | 1244 | return $defaults; |
| 1883 | 1245 | } |
| 1884 | 1246 | |
| 1885 | 1247 | /** |
| @@ -1886,13 +1248,8 @@ | ||
| 1886 | 1248 | * Compare the default array to the saved values and |
| 1887 | 1249 | * remove if they are the same |
| 1888 | 1250 | * |
| 1889 | 1251 | * @since 3.06 |
| 1890 | - * | |
| 1891 | - * @param array $defaults | |
| 1892 | - * @param array $saved | |
| 1893 | - * | |
| 1894 | - * @return void | |
| 1895 | 1252 | */ |
| 1896 | 1253 | private static function remove_defaults( $defaults, &$saved ) { |
| 1897 | 1254 | foreach ( $saved as $key => $value ) { |
| 1898 | 1255 | if ( isset( $defaults[ $key ] ) && $defaults[ $key ] === $value ) { |
| @@ -1904,14 +1261,8 @@ | ||
| 1904 | 1261 | /** |
| 1905 | 1262 | * The line endings may prevent html from being equal when it should |
| 1906 | 1263 | * |
| 1907 | 1264 | * @since 3.06 |
| 1908 | - * | |
| 1909 | - * @param string $html_name | |
| 1910 | - * @param array $defaults | |
| 1911 | - * @param array $options | |
| 1912 | - * | |
| 1913 | - * @return void | |
| 1914 | 1265 | */ |
| 1915 | 1266 | private static function remove_default_html( $html_name, $defaults, &$options ) { |
| 1916 | 1267 | if ( ! isset( $options[ $html_name ] ) || ! isset( $defaults[ $html_name ] ) ) { |
| 1917 | 1268 | return; |
| @@ -1918,35 +1269,27 @@ | ||
| 1918 | 1269 | } |
| 1919 | 1270 | |
| 1920 | 1271 | $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] ); |
| 1921 | 1272 | $default_html = $defaults[ $html_name ]; |
| 1922 | - | |
| 1923 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1924 | 1273 | if ( $old_html == $default_html ) { |
| 1925 | 1274 | unset( $options[ $html_name ] ); |
| 1275 | + | |
| 1926 | 1276 | return; |
| 1927 | 1277 | } |
| 1928 | 1278 | |
| 1929 | 1279 | // Account for some of the older field default HTML. |
| 1930 | 1280 | $default_html = str_replace( ' id="frm_desc_field_[key]"', '', $default_html ); |
| 1931 | - | |
| 1932 | - if ( $old_html === $default_html ) { | |
| 1281 | + if ( $old_html == $default_html ) { | |
| 1933 | 1282 | unset( $options[ $html_name ] ); |
| 1934 | 1283 | } |
| 1935 | 1284 | } |
| 1936 | 1285 | |
| 1937 | - /** | |
| 1938 | - * @param string $str | |
| 1939 | - * | |
| 1940 | - * @return string | |
| 1941 | - */ | |
| 1942 | 1286 | public static function cdata( $str ) { |
| 1943 | 1287 | FrmAppHelper::unserialize_or_decode( $str ); |
| 1944 | - | |
| 1945 | 1288 | if ( is_array( $str ) ) { |
| 1946 | 1289 | $str = json_encode( $str ); |
| 1947 | - } elseif ( FrmAppHelper::is_valid_utf8( $str ) === false ) { | |
| 1948 | - $str = FrmAppHelper::maybe_utf8_encode( $str ); | |
| 1290 | + } elseif ( seems_utf8( $str ) === false ) { | |
| 1291 | + $str = utf8_encode( $str ); | |
| 1949 | 1292 | } |
| 1950 | 1293 | |
| 1951 | 1294 | if ( is_numeric( $str ) ) { |
| 1952 | 1295 | return $str; |
| @@ -1954,9 +1297,11 @@ | ||
| 1954 | 1297 | |
| 1955 | 1298 | self::remove_invalid_characters_from_xml( $str ); |
| 1956 | 1299 | |
| 1957 | 1300 | // $str = ent2ncr(esc_html( $str)); |
| 1958 | - return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 1301 | + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 1302 | + | |
| 1303 | + return $str; | |
| 1959 | 1304 | } |
| 1960 | 1305 | |
| 1961 | 1306 | /** |
| 1962 | 1307 | * Remove <US> character (unit separator) from exported strings |
| @@ -1963,10 +1308,8 @@ | ||
| 1963 | 1308 | * |
| 1964 | 1309 | * @since 2.0.22 |
| 1965 | 1310 | * |
| 1966 | 1311 | * @param string $str |
| 1967 | - * | |
| 1968 | - * @return void | |
| 1969 | 1312 | */ |
| 1970 | 1313 | private static function remove_invalid_characters_from_xml( &$str ) { |
| 1971 | 1314 | // Remove <US> character |
| 1972 | 1315 | $str = str_replace( '\x1F', '', $str ); |
| @@ -1971,18 +1314,8 @@ | ||
| 1971 | 1314 | // Remove <US> character |
| 1972 | 1315 | $str = str_replace( '\x1F', '', $str ); |
| 1973 | 1316 | } |
| 1974 | 1317 | |
| 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 | 1318 | public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) { |
| 1986 | 1319 | // Get post type |
| 1987 | 1320 | $post_type = FrmFormActionsController::$action_post_type; |
| 1988 | 1321 | |
| @@ -2000,16 +1333,12 @@ | ||
| 2000 | 1333 | |
| 2001 | 1334 | /** |
| 2002 | 1335 | * Migrate post settings to form action |
| 2003 | 1336 | * |
| 2004 | - * @param array $form_options | |
| 2005 | - * @param int $form_id | |
| 2006 | 1337 | * @param string $post_type |
| 2007 | - * @param array $imported | |
| 2008 | - * @param bool $switch | |
| 2009 | 1338 | */ |
| 2010 | 1339 | private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
| 2011 | - if ( empty( $form_options['create_post'] ) ) { | |
| 1340 | + if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) { | |
| 2012 | 1341 | return; |
| 2013 | 1342 | } |
| 2014 | 1343 | |
| 2015 | 1344 | $new_action = array( |
| @@ -2061,9 +1390,8 @@ | ||
| 2061 | 1390 | $array_fields = array( 'post_category', 'post_custom_fields' ); |
| 2062 | 1391 | |
| 2063 | 1392 | $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields ); |
| 2064 | 1393 | } |
| 2065 | - | |
| 2066 | 1394 | $new_action['post_content'] = json_encode( $new_action['post_content'] ); |
| 2067 | 1395 | |
| 2068 | 1396 | $exists = get_posts( |
| 2069 | 1397 | array( |
| @@ -2073,15 +1401,13 @@ | ||
| 2073 | 1401 | 'numberposts' => 1, |
| 2074 | 1402 | ) |
| 2075 | 1403 | ); |
| 2076 | 1404 | |
| 2077 | - if ( $exists ) { | |
| 2078 | - return; | |
| 1405 | + if ( ! $exists ) { | |
| 1406 | + // this isn't an email, but we need to use a class that will always be included | |
| 1407 | + FrmDb::save_json_post( $new_action ); | |
| 1408 | + $imported['imported']['actions'] ++; | |
| 2079 | 1409 | } |
| 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 | 1410 | } |
| 2085 | 1411 | |
| 2086 | 1412 | /** |
| 2087 | 1413 | * Switch old field IDs for new field IDs in emails and post |
| @@ -2087,11 +1413,11 @@ | ||
| 2087 | 1413 | * Switch old field IDs for new field IDs in emails and post |
| 2088 | 1414 | * |
| 2089 | 1415 | * @since 2.0 |
| 2090 | 1416 | * |
| 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. | |
| 1417 | + * @param array $post_content - check for old field IDs | |
| 1418 | + * @param array $basic_fields - fields with string or int saved | |
| 1419 | + * @param array $array_fields - fields with arrays saved | |
| 2094 | 1420 | * |
| 2095 | 1421 | * @return string $post_content - new field IDs |
| 2096 | 1422 | */ |
| 2097 | 1423 | private static function switch_action_field_ids( $post_content, $basic_fields, $array_fields = array() ) { |
| @@ -2098,9 +1424,9 @@ | ||
| 2098 | 1424 | global $frm_duplicate_ids; |
| 2099 | 1425 | |
| 2100 | 1426 | // If there aren't IDs that were switched, end now |
| 2101 | 1427 | if ( ! $frm_duplicate_ids ) { |
| 2102 | - return null; | |
| 1428 | + return; | |
| 2103 | 1429 | } |
| 2104 | 1430 | |
| 2105 | 1431 | // Get old IDs |
| 2106 | 1432 | $old = array_keys( $frm_duplicate_ids ); |
| @@ -2109,13 +1435,11 @@ | ||
| 2109 | 1435 | $new = array_values( $frm_duplicate_ids ); |
| 2110 | 1436 | |
| 2111 | 1437 | // Do a str_replace with each item to set the new IDs |
| 2112 | 1438 | foreach ( $post_content as $key => $setting ) { |
| 2113 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 2114 | 1439 | if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) { |
| 2115 | 1440 | // Replace old IDs with new IDs |
| 2116 | 1441 | $post_content[ $key ] = str_replace( $old, $new, $setting ); |
| 2117 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 2118 | 1442 | } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) { |
| 2119 | 1443 | foreach ( $setting as $k => $val ) { |
| 2120 | 1444 | // Replace old IDs with new IDs |
| 2121 | 1445 | $post_content[ $key ][ $k ] = str_replace( $old, $new, $val ); |
| @@ -2126,19 +1450,8 @@ | ||
| 2126 | 1450 | |
| 2127 | 1451 | return $post_content; |
| 2128 | 1452 | } |
| 2129 | 1453 | |
| 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 | 1454 | private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
| 2142 | 1455 | // No old notifications or autoresponders to carry over |
| 2143 | 1456 | if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) { |
| 2144 | 1457 | return; |
| @@ -2152,9 +1465,9 @@ | ||
| 2152 | 1465 | |
| 2153 | 1466 | // Migrate autoresponders |
| 2154 | 1467 | self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications ); |
| 2155 | 1468 | |
| 2156 | - if ( ! $notifications ) { | |
| 1469 | + if ( empty( $notifications ) ) { | |
| 2157 | 1470 | return; |
| 2158 | 1471 | } |
| 2159 | 1472 | |
| 2160 | 1473 | foreach ( $notifications as $new_notification ) { |
| @@ -2165,8 +1478,9 @@ | ||
| 2165 | 1478 | $new_notification['post_status'] = 'publish'; |
| 2166 | 1479 | |
| 2167 | 1480 | // Switch field IDs and keys, if needed |
| 2168 | 1481 | if ( $switch ) { |
| 1482 | + | |
| 2169 | 1483 | // Switch field IDs in email conditional logic |
| 2170 | 1484 | self::switch_email_condition_field_ids( $new_notification['post_content'] ); |
| 2171 | 1485 | |
| 2172 | 1486 | // Switch all other field IDs in email |
| @@ -2171,9 +1485,8 @@ | ||
| 2171 | 1485 | |
| 2172 | 1486 | // Switch all other field IDs in email |
| 2173 | 1487 | $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] ); |
| 2174 | 1488 | } |
| 2175 | - | |
| 2176 | 1489 | $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] ); |
| 2177 | 1490 | |
| 2178 | 1491 | $exists = get_posts( |
| 2179 | 1492 | array( |
| @@ -2183,14 +1496,14 @@ | ||
| 2183 | 1496 | 'numberposts' => 1, |
| 2184 | 1497 | ) |
| 2185 | 1498 | ); |
| 2186 | 1499 | |
| 2187 | - if ( ! $exists ) { | |
| 1500 | + if ( empty( $exists ) ) { | |
| 2188 | 1501 | FrmDb::save_json_post( $new_notification ); |
| 2189 | - ++$imported['imported']['actions']; | |
| 1502 | + $imported['imported']['actions'] ++; | |
| 2190 | 1503 | } |
| 2191 | 1504 | unset( $new_notification ); |
| 2192 | - }//end foreach | |
| 1505 | + } | |
| 2193 | 1506 | |
| 2194 | 1507 | self::remove_deprecated_notification_settings( $form_id, $form_options ); |
| 2195 | 1508 | } |
| 2196 | 1509 | |
| @@ -2199,44 +1512,32 @@ | ||
| 2199 | 1512 | * |
| 2200 | 1513 | * @since 2.05 |
| 2201 | 1514 | * |
| 2202 | 1515 | * @param int|string $form_id |
| 2203 | - * @param array $form_options | |
| 2204 | - * | |
| 2205 | - * @return void | |
| 1516 | + * @param array $form_options | |
| 2206 | 1517 | */ |
| 2207 | 1518 | private static function remove_deprecated_notification_settings( $form_id, $form_options ) { |
| 2208 | 1519 | $delete_settings = array( 'notification', 'autoresponder', 'email_to' ); |
| 2209 | - | |
| 2210 | 1520 | foreach ( $delete_settings as $index ) { |
| 2211 | 1521 | if ( isset( $form_options[ $index ] ) ) { |
| 2212 | 1522 | unset( $form_options[ $index ] ); |
| 2213 | 1523 | } |
| 2214 | 1524 | } |
| 2215 | - | |
| 2216 | 1525 | FrmForm::update( $form_id, array( 'options' => $form_options ) ); |
| 2217 | 1526 | } |
| 2218 | 1527 | |
| 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 | 1528 | 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 | |
| 1529 | + if ( ! isset( $form_options['notification'] ) && isset( $form_options['email_to'] ) && ! empty( $form_options['email_to'] ) ) { | |
| 1530 | + // add old settings into notification array | |
| 2231 | 1531 | $form_options['notification'] = array( 0 => $form_options ); |
| 2232 | 1532 | } elseif ( isset( $form_options['notification']['email_to'] ) ) { |
| 2233 | - // Make sure it's in the correct format | |
| 1533 | + // make sure it's in the correct format | |
| 2234 | 1534 | $form_options['notification'] = array( 0 => $form_options['notification'] ); |
| 2235 | 1535 | } |
| 2236 | 1536 | |
| 2237 | 1537 | if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) { |
| 2238 | 1538 | foreach ( $form_options['notification'] as $email_key => $notification ) { |
| 1539 | + | |
| 2239 | 1540 | $atts = array( |
| 2240 | 1541 | 'email_to' => '', |
| 2241 | 1542 | 'reply_to' => '', |
| 2242 | 1543 | 'reply_to_name' => '', |
| @@ -2247,9 +1548,9 @@ | ||
| 2247 | 1548 | |
| 2248 | 1549 | // Format the email data |
| 2249 | 1550 | self::format_email_data( $atts, $notification ); |
| 2250 | 1551 | |
| 2251 | - if ( ! empty( $notification['twilio'] ) ) { | |
| 1552 | + if ( isset( $notification['twilio'] ) && $notification['twilio'] ) { | |
| 2252 | 1553 | do_action( 'frm_create_twilio_action', $atts, $notification ); |
| 2253 | 1554 | } |
| 2254 | 1555 | |
| 2255 | 1556 | // Setup the new notification |
| @@ -2256,20 +1557,12 @@ | ||
| 2256 | 1557 | $new_notification = array(); |
| 2257 | 1558 | self::setup_new_notification( $new_notification, $notification, $atts ); |
| 2258 | 1559 | |
| 2259 | 1560 | $notifications[] = $new_notification; |
| 2260 | - }//end foreach | |
| 2261 | - }//end if | |
| 1561 | + } | |
| 1562 | + } | |
| 2262 | 1563 | } |
| 2263 | 1564 | |
| 2264 | - /** | |
| 2265 | - * Format email data | |
| 2266 | - * | |
| 2267 | - * @param array $atts | |
| 2268 | - * @param array $notification | |
| 2269 | - * | |
| 2270 | - * @return void | |
| 2271 | - */ | |
| 2272 | 1565 | private static function format_email_data( &$atts, $notification ) { |
| 2273 | 1566 | // Format email_to |
| 2274 | 1567 | self::format_email_to_data( $atts, $notification ); |
| 2275 | 1568 | |
| @@ -2277,16 +1570,14 @@ | ||
| 2277 | 1570 | $reply_fields = array( |
| 2278 | 1571 | 'reply_to' => '', |
| 2279 | 1572 | 'reply_to_name' => '', |
| 2280 | 1573 | ); |
| 2281 | - | |
| 2282 | 1574 | foreach ( $reply_fields as $f => $val ) { |
| 2283 | 1575 | if ( isset( $notification[ $f ] ) ) { |
| 2284 | 1576 | $atts[ $f ] = $notification[ $f ]; |
| 2285 | - | |
| 2286 | - if ( 'custom' === $notification[ $f ] ) { | |
| 1577 | + if ( 'custom' == $notification[ $f ] ) { | |
| 2287 | 1578 | $atts[ $f ] = $notification[ 'cust_' . $f ]; |
| 2288 | - } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) { | |
| 1579 | + } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) { | |
| 2289 | 1580 | $atts[ $f ] = '[' . $atts[ $f ] . ']'; |
| 2290 | 1581 | } |
| 2291 | 1582 | } |
| 2292 | 1583 | unset( $f, $val ); |
| @@ -2293,27 +1584,21 @@ | ||
| 2293 | 1584 | } |
| 2294 | 1585 | |
| 2295 | 1586 | // Format event |
| 2296 | 1587 | $atts['event'] = array( 'create' ); |
| 2297 | - | |
| 2298 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 2299 | 1588 | if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) { |
| 2300 | 1589 | $atts['event'][] = 'update'; |
| 2301 | - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1590 | + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { | |
| 2302 | 1591 | $atts['event'] = array( 'update' ); |
| 2303 | 1592 | } |
| 2304 | 1593 | } |
| 2305 | 1594 | |
| 2306 | - /** | |
| 2307 | - * Format email_to data | |
| 2308 | - * | |
| 2309 | - * @param array $atts | |
| 2310 | - * @param array $notification | |
| 2311 | - * | |
| 2312 | - * @return void | |
| 2313 | - */ | |
| 2314 | 1595 | private static function format_email_to_data( &$atts, $notification ) { |
| 2315 | - $atts['email_to'] = isset( $notification['email_to'] ) ? preg_split( '/ (,|;) /', $notification['email_to'] ) : array(); | |
| 1596 | + if ( isset( $notification['email_to'] ) ) { | |
| 1597 | + $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] ); | |
| 1598 | + } else { | |
| 1599 | + $atts['email_to'] = array(); | |
| 1600 | + } | |
| 2316 | 1601 | |
| 2317 | 1602 | if ( isset( $notification['also_email_to'] ) ) { |
| 2318 | 1603 | $email_fields = (array) $notification['also_email_to']; |
| 2319 | 1604 | $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] ); |
| @@ -2320,36 +1605,24 @@ | ||
| 2320 | 1605 | unset( $email_fields ); |
| 2321 | 1606 | } |
| 2322 | 1607 | |
| 2323 | 1608 | foreach ( $atts['email_to'] as $key => $email_field ) { |
| 1609 | + | |
| 2324 | 1610 | if ( is_numeric( $email_field ) ) { |
| 2325 | 1611 | $atts['email_to'][ $key ] = '[' . $email_field . ']'; |
| 2326 | 1612 | } |
| 2327 | 1613 | |
| 2328 | - if ( ! str_contains( $email_field, '|' ) ) { | |
| 2329 | - continue; | |
| 1614 | + if ( strpos( $email_field, '|' ) ) { | |
| 1615 | + $email_opt = explode( '|', $email_field ); | |
| 1616 | + if ( isset( $email_opt[0] ) ) { | |
| 1617 | + $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']'; | |
| 1618 | + } | |
| 1619 | + unset( $email_opt ); | |
| 2330 | 1620 | } |
| 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 | 1621 | } |
| 2339 | - | |
| 2340 | 1622 | $atts['email_to'] = implode( ', ', $atts['email_to'] ); |
| 2341 | 1623 | } |
| 2342 | 1624 | |
| 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 | 1625 | private static function setup_new_notification( &$new_notification, $notification, $atts ) { |
| 2353 | 1626 | // Set up new notification |
| 2354 | 1627 | $new_notification = array( |
| 2355 | 1628 | 'post_content' => array( |
| @@ -2360,13 +1633,12 @@ | ||
| 2360 | 1633 | ); |
| 2361 | 1634 | |
| 2362 | 1635 | // Add more fields to the new notification |
| 2363 | 1636 | $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' ); |
| 2364 | - | |
| 2365 | 1637 | foreach ( $add_fields as $add_field ) { |
| 2366 | 1638 | if ( isset( $notification[ $add_field ] ) ) { |
| 2367 | 1639 | $new_notification['post_content'][ $add_field ] = $notification[ $add_field ]; |
| 2368 | - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) { | |
| 1640 | + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) { | |
| 2369 | 1641 | $new_notification['post_content'][ $add_field ] = 0; |
| 2370 | 1642 | } else { |
| 2371 | 1643 | $new_notification['post_content'][ $add_field ] = ''; |
| 2372 | 1644 | } |
| @@ -2377,9 +1649,9 @@ | ||
| 2377 | 1649 | $new_notification['post_content']['reply_to'] = $atts['reply_to']; |
| 2378 | 1650 | |
| 2379 | 1651 | // Set from |
| 2380 | 1652 | 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 | |
| 1653 | + $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 | 1654 | } |
| 2383 | 1655 | } |
| 2384 | 1656 | |
| 2385 | 1657 | /** |
| @@ -2384,90 +1656,72 @@ | ||
| 2384 | 1656 | |
| 2385 | 1657 | /** |
| 2386 | 1658 | * Switch field IDs in pre-2.0 email conditional logic |
| 2387 | 1659 | * |
| 2388 | - * @param array $post_content Pass by reference. | |
| 2389 | - * | |
| 2390 | - * @return void | |
| 1660 | + * @param $post_content array, pass by reference | |
| 2391 | 1661 | */ |
| 2392 | 1662 | private static function switch_email_condition_field_ids( &$post_content ) { |
| 2393 | 1663 | // 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' ) ); | |
| 1664 | + if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) { | |
| 1665 | + foreach ( $post_content['conditions'] as $email_key => $val ) { | |
| 1666 | + if ( is_numeric( $email_key ) ) { | |
| 1667 | + $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) ); | |
| 1668 | + } | |
| 1669 | + unset( $email_key, $val ); | |
| 2401 | 1670 | } |
| 2402 | - unset( $email_key, $val ); | |
| 2403 | 1671 | } |
| 2404 | 1672 | } |
| 2405 | 1673 | |
| 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 | 1674 | 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 | - } | |
| 1675 | + if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) { | |
| 1676 | + // migrate autoresponder | |
| 2419 | 1677 | |
| 2420 | - // Migrate autoresponder | |
| 1678 | + $email_field = isset( $form_options['ar_email_to'] ) ? $form_options['ar_email_to'] : 0; | |
| 1679 | + if ( strpos( $email_field, '|' ) ) { | |
| 1680 | + // data from entries field | |
| 1681 | + $email_field = explode( '|', $email_field ); | |
| 1682 | + if ( isset( $email_field[1] ) ) { | |
| 1683 | + $email_field = $email_field[1]; | |
| 1684 | + } | |
| 1685 | + } | |
| 1686 | + if ( is_numeric( $email_field ) && ! empty( $email_field ) ) { | |
| 1687 | + $email_field = '[' . $email_field . ']'; | |
| 1688 | + } | |
| 2421 | 1689 | |
| 2422 | - $email_field = $form_options['ar_email_to'] ?? 0; | |
| 1690 | + $notification = $form_options; | |
| 1691 | + $new_notification2 = array( | |
| 1692 | + 'post_content' => array( | |
| 1693 | + 'email_message' => $notification['ar_email_message'], | |
| 1694 | + 'email_subject' => isset( $notification['ar_email_subject'] ) ? $notification['ar_email_subject'] : '', | |
| 1695 | + 'email_to' => $email_field, | |
| 1696 | + 'plain_text' => isset( $notification['ar_plain_text'] ) ? $notification['ar_plain_text'] : 0, | |
| 1697 | + 'inc_user_info' => 0, | |
| 1698 | + ), | |
| 1699 | + 'post_name' => $form_id . '_email_' . count( $notifications ), | |
| 1700 | + ); | |
| 2423 | 1701 | |
| 2424 | - if ( str_contains( $email_field, '|' ) ) { | |
| 2425 | - // Data from entries field | |
| 2426 | - $email_field = explode( '|', $email_field ); | |
| 1702 | + $reply_to = isset( $notification['ar_reply_to'] ) ? $notification['ar_reply_to'] : ''; | |
| 1703 | + $reply_to_name = isset( $notification['ar_reply_to_name'] ) ? $notification['ar_reply_to_name'] : ''; | |
| 2427 | 1704 | |
| 2428 | - if ( isset( $email_field[1] ) ) { | |
| 2429 | - $email_field = $email_field[1]; | |
| 1705 | + if ( ! empty( $reply_to ) ) { | |
| 1706 | + $new_notification2['post_content']['reply_to'] = $reply_to; | |
| 2430 | 1707 | } |
| 2431 | - } | |
| 2432 | 1708 | |
| 2433 | - if ( is_numeric( $email_field ) && $email_field ) { | |
| 2434 | - $email_field = '[' . $email_field . ']'; | |
| 2435 | - } | |
| 1709 | + if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) { | |
| 1710 | + $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>'; | |
| 1711 | + } | |
| 2436 | 1712 | |
| 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; | |
| 1713 | + $notifications[] = $new_notification2; | |
| 1714 | + unset( $new_notification2 ); | |
| 2454 | 1715 | } |
| 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 | 1716 | } |
| 2463 | 1717 | |
| 2464 | 1718 | /** |
| 2465 | 1719 | * PHP 8 backward compatibility for the libxml_disable_entity_loader function |
| 2466 | 1720 | * |
| 2467 | - * @param bool $loader | |
| 1721 | + * @param boolean $disable | |
| 2468 | 1722 | * |
| 2469 | - * @return bool | |
| 1723 | + * @return boolean | |
| 2470 | 1724 | */ |
| 2471 | 1725 | public static function maybe_libxml_disable_entity_loader( $loader ) { |
| 2472 | 1726 | if ( version_compare( phpversion(), '8.0', '<' ) && function_exists( 'libxml_disable_entity_loader' ) ) { |
| 2473 | 1727 | $loader = libxml_disable_entity_loader( $loader ); // phpcs:disable Generic.PHP.DeprecatedFunctions.Deprecated |
| @@ -2475,32 +1729,9 @@ | ||
| 2475 | 1729 | |
| 2476 | 1730 | return $loader; |
| 2477 | 1731 | } |
| 2478 | 1732 | |
| 2479 | - /** | |
| 2480 | - * PHP 8 backward compatibility for the libxml_disable_entity_loader function | |
| 2481 | - * | |
| 2482 | - * @return bool | |
| 2483 | - */ | |
| 2484 | 1733 | public static function check_if_libxml_disable_entity_loader_exists() { |
| 2485 | 1734 | return version_compare( phpversion(), '8.0', '<' ) && ! function_exists( 'libxml_disable_entity_loader' ); |
| 2486 | 1735 | } |
| 1736 | +} | |
| 2487 | 1737 | |
| 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 | -} | |