| @@ -26,8 +26,11 @@ | ||
| 26 | 26 | |
| 27 | 27 | // Register this as a Gutenberg block in the ConvertKit Plugin. |
| 28 | 28 | add_filter( 'convertkit_blocks', array( $this, 'register' ) ); |
| 29 | 29 | |
| 30 | + // Register this block's MCP abilities. | |
| 31 | + add_filter( 'convertkit_abilities', array( $this, 'register_abilities' ) ); | |
| 32 | + | |
| 30 | 33 | // Enqueue scripts for this Gutenberg Block in the editor view. |
| 31 | 34 | add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) ); |
| 32 | 35 | |
| 33 | 36 | // Enqueue styles for this Gutenberg Block in the editor view. |
| @@ -82,9 +85,11 @@ | ||
| 82 | 85 | |
| 83 | 86 | /** |
| 84 | 87 | * This will register as: |
| 85 | 88 | * - a shortcode, with the name [convertkit_form]. |
| 89 | + * - a shortcode, with the name [kit_form]. | |
| 86 | 90 | * - a shortcode, with the name [convertkit], for backward compat. |
| 91 | + * - a shortcode, with the name [kit]. | |
| 87 | 92 | * - a Gutenberg block, with the name convertkit/form. |
| 88 | 93 | */ |
| 89 | 94 | return 'form'; |
| 90 | 95 | |
| @@ -101,8 +106,21 @@ | ||
| 101 | 106 | |
| 102 | 107 | } |
| 103 | 108 | |
| 104 | 109 | /** |
| 110 | + * Returns this block's plural title. | |
| 111 | + * | |
| 112 | + * @since 3.4.0 | |
| 113 | + * | |
| 114 | + * @return string | |
| 115 | + */ | |
| 116 | + public function get_title_plural() { | |
| 117 | + | |
| 118 | + return __( 'Kit Forms', 'convertkit' ); | |
| 119 | + | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 105 | 123 | * Returns this block's icon. |
| 106 | 124 | * |
| 107 | 125 | * @since 3.1.1 |
| 108 | 126 | */ |
| @@ -261,32 +279,44 @@ | ||
| 261 | 279 | * @return bool|array |
| 262 | 280 | */ |
| 263 | 281 | public function get_fields() { |
| 264 | 282 | |
| 265 | - // Get ConvertKit Forms. | |
| 283 | + // Get ConvertKit Forms. Non-legacy forms populate the sidebar dropdown; | |
| 284 | + // legacy forms are exposed separately as a fallback so the sidebar can | |
| 285 | + // keep displaying a previously-saved legacy form as the current | |
| 286 | + // selection without offering other legacy forms as new choices. | |
| 266 | 287 | $forms = array(); |
| 288 | + $legacy_forms = array(); | |
| 267 | 289 | $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 268 | 290 | if ( $convertkit_forms->exist() ) { |
| 269 | 291 | foreach ( $convertkit_forms->get() as $form ) { |
| 270 | - // Legacy forms don't include a `format` key, so define them as inline. | |
| 271 | - $forms[ absint( $form['id'] ) ] = sprintf( | |
| 292 | + $label = sprintf( | |
| 272 | 293 | '%s [%s]', |
| 273 | 294 | sanitize_text_field( $form['name'] ), |
| 295 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 274 | 296 | ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) |
| 275 | 297 | ); |
| 298 | + | |
| 299 | + if ( ! empty( $form['format'] ) ) { | |
| 300 | + $forms[ absint( $form['id'] ) ] = $label; | |
| 301 | + } else { | |
| 302 | + $legacy_forms[ absint( $form['id'] ) ] = $label; | |
| 303 | + } | |
| 276 | 304 | } |
| 277 | 305 | } |
| 278 | 306 | |
| 279 | 307 | return array( |
| 280 | 308 | 'form' => array( |
| 281 | - 'label' => __( 'Form', 'convertkit' ), | |
| 282 | - 'type' => 'resource', | |
| 283 | - 'resource' => 'forms', | |
| 284 | - 'values' => $forms, | |
| 285 | - 'data' => array( | |
| 309 | + 'label' => __( 'Form', 'convertkit' ), | |
| 310 | + 'type' => 'resource', | |
| 311 | + 'resource' => 'forms', | |
| 312 | + 'values' => $forms, | |
| 313 | + 'legacy_values' => $legacy_forms, | |
| 314 | + 'data' => array( | |
| 286 | 315 | // Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format |
| 287 | 316 | // (modal, slide in, sticky bar) and output a message in the block editor for the preview to explain |
| 288 | - // why some formats cannot be previewed. | |
| 317 | + // why some formats cannot be previewed. Includes legacy forms so the preview code can still find | |
| 318 | + // them when a saved block references a legacy form. | |
| 289 | 319 | 'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ), |
| 290 | 320 | ), |
| 291 | 321 | ), |
| 292 | 322 | ); |
| @@ -381,9 +411,9 @@ | ||
| 381 | 411 | // Get Form HTML. |
| 382 | 412 | $forms = new ConvertKit_Resource_Forms( 'output_form' ); |
| 383 | 413 | $form = $forms->get_html( $form_id, $post_id ); |
| 384 | 414 | |
| 385 | - // If an error occured, it might be that we're requesting a Form ID that exists in ConvertKit | |
| 415 | + // If an error occurred, it might be that we're requesting a Form ID that exists in ConvertKit | |
| 386 | 416 | // but does not yet exist in the Plugin's Form Resources. |
| 387 | 417 | // If so, refresh the Form Resources and try again. |
| 388 | 418 | if ( is_wp_error( $form ) && $form->get_error_data() === 404 ) { |
| 389 | 419 | // Refresh Forms from the API. |
| @@ -388,9 +418,9 @@ | ||
| 388 | 418 | if ( is_wp_error( $form ) && $form->get_error_data() === 404 ) { |
| 389 | 419 | // Refresh Forms from the API. |
| 390 | 420 | $result = $forms->refresh(); |
| 391 | 421 | |
| 392 | - // Bail if an error occured. | |
| 422 | + // Bail if an error occurred. | |
| 393 | 423 | if ( is_wp_error( $result ) ) { |
| 394 | 424 | if ( $settings->debug_enabled() ) { |
| 395 | 425 | return '<!-- ' . $result->get_error_message() . ' --> <!-- ' . $form->get_error_message() . ' -->'; |
| 396 | 426 | } |
| @@ -402,9 +432,9 @@ | ||
| 402 | 432 | // Get Form HTML again. |
| 403 | 433 | $form = $forms->get_html( $form_id, $post_id ); |
| 404 | 434 | } |
| 405 | 435 | |
| 406 | - // If an error still occured, the shortcode might be from the ConvertKit App for a Legacy Form ID | |
| 436 | + // If an error still occurred, the shortcode might be from the ConvertKit App for a Legacy Form ID | |
| 407 | 437 | // These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented |
| 408 | 438 | // to us in the API. |
| 409 | 439 | // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]). |
| 410 | 440 | // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources. |