← All changes
|
admin/importers/class-convertkit-admin-importer.php
+93
-38
3.3.3
→
3.4.4
View file →
| @@ -32,8 +32,17 @@ | ||
| 32 | 32 | */ |
| 33 | 33 | public $title = ''; |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | + * Holds the importer description. | |
| 37 | + * | |
| 38 | + * @since 3.3.5 | |
| 39 | + * | |
| 40 | + * @var string | |
| 41 | + */ | |
| 42 | + public $description = ''; | |
| 43 | + | |
| 44 | + /** | |
| 36 | 45 | * Holds the shortcode name for the third party Form plugin. |
| 37 | 46 | * |
| 38 | 47 | * @since 3.1.0 |
| 39 | 48 | * |
| @@ -41,13 +50,17 @@ | ||
| 41 | 50 | */ |
| 42 | 51 | public $shortcode_name = ''; |
| 43 | 52 | |
| 44 | 53 | /** |
| 45 | - * Holds the shortcode ID attribute name for the third party Form plugin. | |
| 54 | + * Holds the shortcode ID attribute name(s) for the third party Form plugin. | |
| 46 | 55 | * |
| 56 | + * Accepts a string for a single attribute (e.g. 'form'), or an array of | |
| 57 | + * strings for plugins where the form ID may appear under multiple attribute | |
| 58 | + * names (e.g. ['form', 'id']). | |
| 59 | + * | |
| 47 | 60 | * @since 3.1.0 |
| 48 | 61 | * |
| 49 | - * @var bool|string | |
| 62 | + * @var bool|string|array | |
| 50 | 63 | */ |
| 51 | 64 | public $shortcode_id_attribute = false; |
| 52 | 65 | |
| 53 | 66 | /** |
| @@ -59,13 +72,17 @@ | ||
| 59 | 72 | */ |
| 60 | 73 | public $block_name = ''; |
| 61 | 74 | |
| 62 | 75 | /** |
| 63 | - * Holds the block ID attribute name for the third party Form plugin. | |
| 76 | + * Holds the block ID attribute name(s) for the third party Form plugin. | |
| 64 | 77 | * |
| 78 | + * Accepts a string for a single attribute (e.g. 'form'), or an array of | |
| 79 | + * strings for plugins where the form ID may appear under multiple attribute | |
| 80 | + * names (e.g. ['form', 'id']). | |
| 81 | + * | |
| 65 | 82 | * @since 3.1.6 |
| 66 | 83 | * |
| 67 | - * @var bool|string | |
| 84 | + * @var bool|string|array | |
| 68 | 85 | */ |
| 69 | 86 | public $block_id_attribute = false; |
| 70 | 87 | |
| 71 | 88 | /** |
| @@ -98,11 +115,12 @@ | ||
| 98 | 115 | } |
| 99 | 116 | |
| 100 | 117 | // Add this importer to the list of importers. |
| 101 | 118 | $importers[ $this->name ] = array( |
| 102 | - 'name' => $this->name, | |
| 103 | - 'title' => $this->title, | |
| 104 | - 'forms' => $this->get_forms(), | |
| 119 | + 'name' => $this->name, | |
| 120 | + 'title' => $this->title, | |
| 121 | + 'description' => $this->description, | |
| 122 | + 'forms' => $this->get_forms(), | |
| 105 | 123 | ); |
| 106 | 124 | |
| 107 | 125 | return $importers; |
| 108 | 126 | |
| @@ -275,23 +293,38 @@ | ||
| 275 | 293 | if ( ! $this->shortcode_id_attribute ) { |
| 276 | 294 | $pattern = '/\[' // Start regex with an opening square bracket. |
| 277 | 295 | . preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars. |
| 278 | 296 | . '[^\]]*?\]/i'; // Match any other characters (non-greedy) up to the closing square bracket, case-insensitive. |
| 279 | - } else { | |
| 297 | + | |
| 298 | + return preg_replace( | |
| 299 | + $pattern, | |
| 300 | + '[convertkit_form form="' . $form_id . '"]', | |
| 301 | + $content | |
| 302 | + ); | |
| 303 | + } | |
| 304 | + | |
| 305 | + // Normalise the shortcode ID attribute to an array, so importers can | |
| 306 | + // declare a single attribute (string) or multiple attributes (array). | |
| 307 | + $id_attributes = (array) $this->shortcode_id_attribute; | |
| 308 | + | |
| 309 | + // Run a replacement pass per attribute name. | |
| 310 | + foreach ( $id_attributes as $id_attribute ) { | |
| 280 | 311 | $pattern = '/\[' // Start regex with an opening square bracket. |
| 281 | 312 | . preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars. |
| 282 | 313 | . '[^\]]*?' // Match any characters that are not a closing square bracket, non-greedy. |
| 283 | - . '\b' . preg_quote( $this->shortcode_id_attribute, '/' ) // Match the id attribute word boundary and escape as needed. | |
| 314 | + . '\b' . preg_quote( $id_attribute, '/' ) // Match the id attribute word boundary and escape as needed. | |
| 284 | 315 | . '\s*=\s*' // Match optional whitespace around an equals sign. |
| 285 | 316 | . '(?:"' . preg_quote( (string) $third_party_form_id, '/' ) . '"|\'' . preg_quote( (string) $third_party_form_id, '/' ) . '\'|' . preg_quote( (string) $third_party_form_id, '/' ) . ')' // Match the form ID, double quotes, single quotes or unquoted. |
| 286 | 317 | . '[^\]]*?\]/i'; // Match any other characters (non-greedy) up to the closing square bracket, case-insensitive. |
| 318 | + | |
| 319 | + $content = preg_replace( | |
| 320 | + $pattern, | |
| 321 | + '[convertkit_form form="' . $form_id . '"]', | |
| 322 | + $content | |
| 323 | + ); | |
| 287 | 324 | } |
| 288 | 325 | |
| 289 | - return preg_replace( | |
| 290 | - $pattern, | |
| 291 | - '[convertkit_form id="' . $form_id . '"]', | |
| 292 | - $content | |
| 293 | - ); | |
| 326 | + return $content; | |
| 294 | 327 | |
| 295 | 328 | } |
| 296 | 329 | |
| 297 | 330 | /** |
| @@ -358,31 +391,39 @@ | ||
| 358 | 391 | |
| 359 | 392 | return array(); |
| 360 | 393 | } |
| 361 | 394 | |
| 362 | - // Legacy: Extract where attribute is required. | |
| 363 | - $pattern = '/\[' // Start regex with an opening square bracket. | |
| 364 | - . preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars. | |
| 365 | - . '(?:\s+[^\]]*)?' // Optionally match any attributes (key/value pairs), non-greedy. | |
| 366 | - . preg_quote( $this->shortcode_id_attribute, '/' ) // Match the id attribute name. | |
| 367 | - . '\s*=\s*' // Optional whitespace, equals sign, optional whitespace. | |
| 368 | - . '(?:"([^"]+)"|\'([^\']+)\'|([^\s\]]+))' // Capture double quoted, single quoted or unquoted value. | |
| 369 | - . '[^\]]*?\]/i'; // Match up to closing bracket, case-insensitive. | |
| 395 | + // Normalise the shortcode ID attribute to an array, so importers can | |
| 396 | + // declare a single attribute (string) or multiple attributes (array). | |
| 397 | + $id_attributes = (array) $this->shortcode_id_attribute; | |
| 370 | 398 | |
| 371 | - preg_match_all( $pattern, $content, $matches ); | |
| 399 | + // Extract form IDs, running a match pass per attribute name. | |
| 400 | + $form_ids = array(); | |
| 401 | + foreach ( $id_attributes as $id_attribute ) { | |
| 402 | + $pattern = '/\[' // Start regex with an opening square bracket. | |
| 403 | + . preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars. | |
| 404 | + . '(?:\s+[^\]]*)?' // Optionally match any attributes (key/value pairs), non-greedy. | |
| 405 | + . preg_quote( $id_attribute, '/' ) // Match the id attribute name. | |
| 406 | + . '\s*=\s*' // Optional whitespace, equals sign, optional whitespace. | |
| 407 | + . '(?:"([^"]+)"|\'([^\']+)\'|([^\s\]]+))' // Capture double quoted, single quoted or unquoted value. | |
| 408 | + . '[^\]]*?\]/i'; // Match up to closing bracket, case-insensitive. | |
| 372 | 409 | |
| 373 | - // Extract form IDs: They could be in either $matches[1] (double quoted), $matches[2] (single quoted) or $matches[3] (unquoted). | |
| 374 | - $form_ids = array_values( | |
| 375 | - array_filter( | |
| 376 | - array_merge( | |
| 377 | - isset( $matches[1] ) ? $matches[1] : array(), | |
| 378 | - isset( $matches[2] ) ? $matches[2] : array(), | |
| 379 | - isset( $matches[3] ) ? $matches[3] : array() | |
| 410 | + preg_match_all( $pattern, $content, $matches ); | |
| 411 | + | |
| 412 | + // Extract form IDs: They could be in either $matches[1] (double quoted), $matches[2] (single quoted) or $matches[3] (unquoted). | |
| 413 | + $form_ids = array_merge( | |
| 414 | + $form_ids, | |
| 415 | + array_filter( | |
| 416 | + array_merge( | |
| 417 | + isset( $matches[1] ) ? $matches[1] : array(), | |
| 418 | + isset( $matches[2] ) ? $matches[2] : array(), | |
| 419 | + isset( $matches[3] ) ? $matches[3] : array() | |
| 420 | + ) | |
| 380 | 421 | ) |
| 381 | - ) | |
| 382 | - ); | |
| 422 | + ); | |
| 423 | + } | |
| 383 | 424 | |
| 384 | - return $form_ids; | |
| 425 | + return array_values( array_unique( $form_ids ) ); | |
| 385 | 426 | |
| 386 | 427 | } |
| 387 | 428 | |
| 388 | 429 | /** |
| @@ -503,15 +544,29 @@ | ||
| 503 | 544 | |
| 504 | 545 | // If the block ID attribute is not set, the third party Plugin doesn't use IDs, |
| 505 | 546 | // so there's no need to check the $third_party_form_id matches the block attribute. |
| 506 | 547 | if ( $this->block_id_attribute ) { |
| 507 | - // Skip if the attribute doesn't exist i.e. the block was not configured. | |
| 508 | - if ( ! array_key_exists( $this->block_id_attribute, $block['attrs'] ) ) { | |
| 509 | - continue; | |
| 548 | + // Normalise the block ID attribute to an array, so importers can | |
| 549 | + // declare a single attribute (string) or multiple attributes (array). | |
| 550 | + $id_attributes = (array) $this->block_id_attribute; | |
| 551 | + | |
| 552 | + // Check if at least one attribute contains the third party form ID. | |
| 553 | + $matched = false; | |
| 554 | + foreach ( $id_attributes as $id_attribute ) { | |
| 555 | + if ( ! array_key_exists( $id_attribute, $block['attrs'] ) ) { | |
| 556 | + continue; | |
| 557 | + } | |
| 558 | + | |
| 559 | + if ( stripos( $block['attrs'][ $id_attribute ], (string) $third_party_form_id ) === false ) { | |
| 560 | + continue; | |
| 561 | + } | |
| 562 | + | |
| 563 | + $matched = true; | |
| 564 | + break; | |
| 510 | 565 | } |
| 511 | 566 | |
| 512 | - // Skip if the third party form ID doesn't exist within the third party form block's attribute. | |
| 513 | - if ( stripos( $block['attrs'][ $this->block_id_attribute ], (string) $third_party_form_id ) === false ) { | |
| 567 | + // Skip if none of the configured ID attributes match. | |
| 568 | + if ( ! $matched ) { | |
| 514 | 569 | continue; |
| 515 | 570 | } |
| 516 | 571 | } |
| 517 | 572 | |