← All changes
|
includes/blocks/class-convertkit-block-form-trigger.php
+109
-95
2.2.6
→
3.4.3
View file →
| @@ -26,10 +26,10 @@ | ||
| 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 | - // Enqueue scripts and styles for this Gutenberg Block in the editor view. | |
| 31 | - add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) ); | |
| 30 | + // Register this block's MCP abilities. | |
| 31 | + add_filter( 'convertkit_abilities', array( $this, 'register_abilities' ) ); | |
| 32 | 32 | |
| 33 | 33 | // Enqueue scripts and styles for this Gutenberg Block in the editor and frontend views. |
| 34 | 34 | add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) ); |
| 35 | 35 | |
| @@ -35,19 +35,8 @@ | ||
| 35 | 35 | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | - * Enqueues scripts for this Gutenberg Block in the editor view. | |
| 40 | - * | |
| 41 | - * @since 2.2.0 | |
| 42 | - */ | |
| 43 | - public function enqueue_scripts_editor() { | |
| 44 | - | |
| 45 | - wp_enqueue_script( 'convertkit-gutenberg-block-form-trigger', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-form-trigger.js', array( 'convertkit-gutenberg' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 46 | - | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | 39 | * Enqueues styles for this Gutenberg Block in the editor and frontend views. |
| 51 | 40 | * |
| 52 | 41 | * @since 2.2.0 |
| 53 | 42 | */ |
| @@ -52,10 +41,13 @@ | ||
| 52 | 41 | * @since 2.2.0 |
| 53 | 42 | */ |
| 54 | 43 | public function enqueue_styles() { |
| 55 | 44 | |
| 56 | - wp_enqueue_style( 'convertkit-button', CONVERTKIT_PLUGIN_URL . 'resources/frontend/css/button.css', array(), CONVERTKIT_PLUGIN_VERSION ); | |
| 45 | + convertkit_enqueue_frontend_css(); | |
| 57 | 46 | |
| 47 | + // Enqueue the block button CSS. | |
| 48 | + wp_enqueue_style( 'wp-block-button' ); | |
| 49 | + | |
| 58 | 50 | } |
| 59 | 51 | |
| 60 | 52 | /** |
| 61 | 53 | * Returns this block's programmatic name, excluding the convertkit- prefix. |
| @@ -73,8 +65,43 @@ | ||
| 73 | 65 | |
| 74 | 66 | } |
| 75 | 67 | |
| 76 | 68 | /** |
| 69 | + * Returns this block's title. | |
| 70 | + * | |
| 71 | + * @since 3.1.1 | |
| 72 | + */ | |
| 73 | + public function get_title() { | |
| 74 | + | |
| 75 | + return __( 'Kit Form Trigger', 'convertkit' ); | |
| 76 | + | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Returns this block's plural title. | |
| 81 | + * | |
| 82 | + * @since 3.4.0 | |
| 83 | + * | |
| 84 | + * @return string | |
| 85 | + */ | |
| 86 | + public function get_title_plural() { | |
| 87 | + | |
| 88 | + return __( 'Kit Form Triggers', 'convertkit' ); | |
| 89 | + | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Returns this block's icon. | |
| 94 | + * | |
| 95 | + * @since 3.1.1 | |
| 96 | + */ | |
| 97 | + public function get_icon() { | |
| 98 | + | |
| 99 | + return 'resources/backend/images/block-icon-formtrigger.svg'; | |
| 100 | + | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 77 | 104 | * Returns this block's Title, Icon, Categories, Keywords and properties. |
| 78 | 105 | * |
| 79 | 106 | * @since 2.2.0 |
| 80 | 107 | */ |
| @@ -83,56 +110,59 @@ | ||
| 83 | 110 | $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 84 | 111 | $settings = new ConvertKit_Settings(); |
| 85 | 112 | |
| 86 | 113 | return array( |
| 87 | - 'title' => __( 'ConvertKit Form Trigger', 'convertkit' ), | |
| 88 | - 'description' => __( 'Displays a modal, sticky bar or slide in form to display when the button is pressed.', 'convertkit' ), | |
| 89 | - 'icon' => 'resources/backend/images/block-icon-formtrigger.svg', | |
| 90 | - 'category' => 'convertkit', | |
| 91 | - 'keywords' => array( | |
| 114 | + 'title' => $this->get_title(), | |
| 115 | + 'description' => __( 'Displays a modal, sticky bar or slide in form to display when the button is pressed.', 'convertkit' ), | |
| 116 | + 'icon' => $this->get_icon(), | |
| 117 | + 'category' => 'convertkit', | |
| 118 | + 'keywords' => array( | |
| 92 | 119 | __( 'ConvertKit', 'convertkit' ), |
| 120 | + __( 'Kit', 'convertkit' ), | |
| 93 | 121 | __( 'Form', 'convertkit' ), |
| 94 | 122 | ), |
| 95 | 123 | |
| 96 | 124 | // Function to call when rendering as a block or a shortcode on the frontend web site. |
| 97 | - 'render_callback' => array( $this, 'render' ), | |
| 125 | + 'render_callback' => array( $this, 'render' ), | |
| 98 | 126 | |
| 99 | 127 | // Shortcode: TinyMCE / QuickTags Modal Width and Height. |
| 100 | - 'modal' => array( | |
| 128 | + 'modal' => array( | |
| 101 | 129 | 'width' => 500, |
| 102 | - 'height' => 352, | |
| 130 | + 'height' => 282, | |
| 103 | 131 | ), |
| 104 | 132 | |
| 105 | 133 | // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals. |
| 106 | - 'shortcode_include_closing_tag' => false, | |
| 134 | + 'shortcode_include_closing_tag' => false, | |
| 107 | 135 | |
| 108 | 136 | // Gutenberg: Block Icon in Editor. |
| 109 | - 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-formtrigger.svg' ), | |
| 137 | + 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-formtrigger.svg' ), | |
| 110 | 138 | |
| 111 | 139 | // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg. |
| 112 | - 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-formtrigger.png', | |
| 140 | + 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-formtrigger.png', | |
| 113 | 141 | |
| 114 | 142 | // Help descriptions, displayed when no API key / resources exist and this block/shortcode is added. |
| 115 | - 'no_api_key' => array( | |
| 116 | - 'notice' => __( 'No API Key specified.', 'convertkit' ), | |
| 117 | - 'link' => convertkit_get_setup_wizard_plugin_link(), | |
| 118 | - 'link_text' => __( 'Click here to add your API Key.', 'convertkit' ), | |
| 143 | + 'no_access_token' => array( | |
| 144 | + 'notice' => __( 'Not connected to Kit.', 'convertkit' ), | |
| 145 | + 'link' => convertkit_get_setup_wizard_plugin_link(), | |
| 146 | + 'link_text' => __( 'Click here to connect your Kit account.', 'convertkit' ), | |
| 147 | + 'instruction_text' => __( 'Connect your Kit account at Settings > Kit, and then refresh this page to select a form.', 'convertkit' ), | |
| 119 | 148 | ), |
| 120 | - 'no_resources' => array( | |
| 121 | - 'notice' => __( 'No modal, sticky bar or slide in forms exist in ConvertKit.', 'convertkit' ), | |
| 122 | - 'link' => convertkit_get_new_form_url(), | |
| 123 | - 'link_text' => __( 'Click here to create a form.', 'convertkit' ), | |
| 149 | + 'no_resources' => array( | |
| 150 | + 'notice' => __( 'No modal, sticky bar or slide in forms exist in Kit.', 'convertkit' ), | |
| 151 | + 'link' => convertkit_get_new_form_url(), | |
| 152 | + 'link_text' => __( 'Click here to create a form.', 'convertkit' ), | |
| 153 | + 'instruction_text' => __( 'Add a non-inline form to your Kit account, and then refresh this page to select a form.', 'convertkit' ), | |
| 124 | 154 | ), |
| 125 | - 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ), | |
| 126 | 155 | |
| 127 | - // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor. | |
| 128 | - // If not defined, render_callback above will be used. | |
| 129 | - 'gutenberg_preview_render_callback' => 'convertKitGutenbergFormTriggerBlockRenderPreview', | |
| 156 | + // The attribute to check if a value exists when rendering the block in the editor, | |
| 157 | + // and the message to display if no value exists. | |
| 158 | + 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ), | |
| 159 | + 'gutenberg_help_description_attribute' => 'form', | |
| 130 | 160 | |
| 131 | 161 | // Whether an API Key exists in the Plugin, and are the required resources (non-inline forms) available. |
| 132 | 162 | // If no API Key is specified in the Plugin's settings, render the "No API Key" output. |
| 133 | - 'has_api_key' => $settings->has_api_key_and_secret(), | |
| 134 | - 'has_resources' => $convertkit_forms->non_inline_exist(), | |
| 163 | + 'has_access_token' => $settings->has_access_and_refresh_token(), | |
| 164 | + 'has_resources' => $convertkit_forms->non_inline_exist(), | |
| 135 | 165 | ); |
| 136 | 166 | |
| 137 | 167 | } |
| 138 | 168 | |
| @@ -155,9 +185,12 @@ | ||
| 155 | 185 | ), |
| 156 | 186 | |
| 157 | 187 | // The below are built in Gutenberg attributes registered in get_supports(). |
| 158 | 188 | |
| 159 | - // Color. | |
| 189 | + // get_supports() style, color and typography attributes. | |
| 190 | + 'style' => array( | |
| 191 | + 'type' => 'object', | |
| 192 | + ), | |
| 160 | 193 | 'backgroundColor' => array( |
| 161 | 194 | 'type' => 'string', |
| 162 | 195 | ), |
| 163 | 196 | 'textColor' => array( |
| @@ -162,37 +195,12 @@ | ||
| 162 | 195 | ), |
| 163 | 196 | 'textColor' => array( |
| 164 | 197 | 'type' => 'string', |
| 165 | 198 | ), |
| 166 | - | |
| 167 | - // Typography. | |
| 168 | 199 | 'fontSize' => array( |
| 169 | 200 | 'type' => 'string', |
| 170 | 201 | ), |
| 171 | 202 | |
| 172 | - // Spacing/Dimensions > Padding. | |
| 173 | - 'style' => array( | |
| 174 | - 'type' => 'object', | |
| 175 | - 'visualizers' => array( | |
| 176 | - 'type' => 'object', | |
| 177 | - 'padding' => array( | |
| 178 | - 'type' => 'object', | |
| 179 | - 'top' => array( | |
| 180 | - 'type' => 'boolean', | |
| 181 | - ), | |
| 182 | - 'bottom' => array( | |
| 183 | - 'type' => 'boolean', | |
| 184 | - ), | |
| 185 | - 'left' => array( | |
| 186 | - 'type' => 'boolean', | |
| 187 | - ), | |
| 188 | - 'right' => array( | |
| 189 | - 'type' => 'boolean', | |
| 190 | - ), | |
| 191 | - ), | |
| 192 | - ), | |
| 193 | - ), | |
| 194 | - | |
| 195 | 203 | // Always required for Gutenberg. |
| 196 | 204 | 'is_gutenberg_example' => array( |
| 197 | 205 | 'type' => 'boolean', |
| 198 | 206 | 'default' => false, |
| @@ -212,19 +220,23 @@ | ||
| 212 | 220 | |
| 213 | 221 | return array( |
| 214 | 222 | 'className' => true, |
| 215 | 223 | 'color' => array( |
| 216 | - 'background' => true, | |
| 217 | - 'text' => true, | |
| 224 | + 'background' => true, | |
| 225 | + 'text' => true, | |
| 226 | + | |
| 227 | + // Don't apply styles to the block editor's div element. | |
| 228 | + // This ensures what's rendered in the Gutenberg editor matches the frontend output for styling. | |
| 229 | + // See: https://github.com/WordPress/gutenberg/issues/32417. | |
| 230 | + '__experimentalSkipSerialization' => true, | |
| 218 | 231 | ), |
| 219 | 232 | 'typography' => array( |
| 220 | - 'fontSize' => true, | |
| 233 | + 'fontSize' => true, | |
| 234 | + 'lineHeight' => true, | |
| 221 | 235 | ), |
| 222 | 236 | 'spacing' => array( |
| 223 | - 'padding' => array( | |
| 224 | - 'horizontal', | |
| 225 | - 'vertical', | |
| 226 | - ), | |
| 237 | + 'margin' => true, | |
| 238 | + 'padding' => true, | |
| 227 | 239 | ), |
| 228 | 240 | ); |
| 229 | 241 | |
| 230 | 242 | } |
| @@ -237,19 +249,19 @@ | ||
| 237 | 249 | * @return bool|array |
| 238 | 250 | */ |
| 239 | 251 | public function get_fields() { |
| 240 | 252 | |
| 241 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 242 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 243 | - return false; | |
| 244 | - } | |
| 245 | - | |
| 246 | 253 | // Get non-inline ConvertKit Forms. |
| 247 | 254 | $forms = array(); |
| 248 | 255 | $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 249 | 256 | if ( $convertkit_forms->non_inline_exist() ) { |
| 250 | 257 | foreach ( $convertkit_forms->get_non_inline() as $form ) { |
| 251 | - $forms[ absint( $form['id'] ) ] = sanitize_text_field( $form['name'] ); | |
| 258 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 259 | + $forms[ absint( $form['id'] ) ] = sprintf( | |
| 260 | + '%s [%s]', | |
| 261 | + sanitize_text_field( $form['name'] ), | |
| 262 | + ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) | |
| 263 | + ); | |
| 252 | 264 | } |
| 253 | 265 | } |
| 254 | 266 | |
| 255 | 267 | // Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included |
| @@ -256,11 +268,12 @@ | ||
| 256 | 268 | // automatically by Gutenberg. |
| 257 | 269 | return array( |
| 258 | 270 | 'form' => array( |
| 259 | 271 | 'label' => __( 'Form', 'convertkit' ), |
| 260 | - 'type' => 'select', | |
| 272 | + 'type' => 'resource', | |
| 273 | + 'resource' => 'forms', | |
| 261 | 274 | 'values' => $forms, |
| 262 | - 'description' => __( 'The modal, sticky bar or slide in form to display when the button is pressed. To embed a form, use the ConvertKit Form block instead.', 'convertkit' ), | |
| 275 | + 'description' => __( 'The modal, sticky bar or slide in form to display when the button is pressed. To embed a form, use the Kit Form block instead.', 'convertkit' ), | |
| 263 | 276 | ), |
| 264 | 277 | 'text' => array( |
| 265 | 278 | 'label' => __( 'Button Text', 'convertkit' ), |
| 266 | 279 | 'type' => 'text', |
| @@ -289,13 +302,8 @@ | ||
| 289 | 302 | * @return bool|array |
| 290 | 303 | */ |
| 291 | 304 | public function get_panels() { |
| 292 | 305 | |
| 293 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 294 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 295 | - return false; | |
| 296 | - } | |
| 297 | - | |
| 298 | 306 | // Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included |
| 299 | 307 | // automatically by Gutenberg. |
| 300 | 308 | return array( |
| 301 | 309 | 'general' => array( |
| @@ -348,10 +356,10 @@ | ||
| 348 | 356 | * Returns the block's output, based on the supplied configuration attributes. |
| 349 | 357 | * |
| 350 | 358 | * @since 2.2.0 |
| 351 | 359 | * |
| 352 | - * @param array $atts Block / Shortcode Attributes. | |
| 353 | - * @return string Output | |
| 360 | + * @param array $atts Block / Shortcode / Page Builder Module Attributes. | |
| 361 | + * @return string | |
| 354 | 362 | */ |
| 355 | 363 | public function render( $atts ) { |
| 356 | 364 | |
| 357 | 365 | // Parse attributes, defining fallback defaults if required |
| @@ -361,11 +369,17 @@ | ||
| 361 | 369 | // Setup Settings class. |
| 362 | 370 | $settings = new ConvertKit_Settings(); |
| 363 | 371 | |
| 364 | 372 | // Build HTML. |
| 365 | - $html = $this->get_html( $atts['form'], $atts['text'], $atts['_css_classes'], $atts['_css_styles'], $this->is_block_editor_request() ); | |
| 373 | + $html = $this->get_html( | |
| 374 | + $atts['form'], | |
| 375 | + $atts['text'], | |
| 376 | + $this->get_css_classes( array( 'wp-block-button__link', 'wp-element-button' ) ), | |
| 377 | + $this->get_css_styles( $atts ), | |
| 378 | + $this->is_block_editor_request() | |
| 379 | + ); | |
| 366 | 380 | |
| 367 | - // Bail if an error occured. | |
| 381 | + // Bail if an error occurred. | |
| 368 | 382 | if ( is_wp_error( $html ) ) { |
| 369 | 383 | if ( $settings->debug_enabled() ) { |
| 370 | 384 | return '<!-- ' . $html->get_error_message() . ' -->'; |
| 371 | 385 | } |
| @@ -394,9 +408,9 @@ | ||
| 394 | 408 | * |
| 395 | 409 | * @param int $id Form ID. |
| 396 | 410 | * @param string $button_text Button Text. |
| 397 | 411 | * @param array $css_classes CSS classes to apply to link (typically included when using Gutenberg). |
| 398 | - * @param array $css_styles CSS inline styles to apply to link (typically included when using Gutenberg). | |
| 412 | + * @param array $css_styles CSS inline styles to apply to link (typically included when using Shortcode or third party page builder module / widget). | |
| 399 | 413 | * @param bool $return_as_span If true, returns a <span> instead of <a>. Useful for the block editor so that the element is interactible. |
| 400 | 414 | * @return WP_Error|string Button HTML |
| 401 | 415 | */ |
| 402 | 416 | private function get_html( $id, $button_text, $css_classes = array(), $css_styles = array(), $return_as_span = false ) { |
| @@ -415,9 +429,9 @@ | ||
| 415 | 429 | return new WP_Error( |
| 416 | 430 | 'convertkit_block_form_trigger_get_html', |
| 417 | 431 | sprintf( |
| 418 | 432 | /* translators: ConvertKit Form ID */ |
| 419 | - __( 'ConvertKit Form ID %s does not exist on ConvertKit.', 'convertkit' ), | |
| 433 | + __( 'Kit Form ID %s does not exist on Kit.', 'convertkit' ), | |
| 420 | 434 | $id |
| 421 | 435 | ) |
| 422 | 436 | ); |
| 423 | 437 | } |
| @@ -427,9 +441,9 @@ | ||
| 427 | 441 | return new WP_Error( |
| 428 | 442 | 'convertkit_block_form_trigger_get_html', |
| 429 | 443 | sprintf( |
| 430 | 444 | /* translators: ConvertKit Form ID */ |
| 431 | - __( 'ConvertKit Form ID %s has no uid property.', 'convertkit' ), | |
| 445 | + __( 'Kit Form ID %s has no uid property.', 'convertkit' ), | |
| 432 | 446 | $id |
| 433 | 447 | ) |
| 434 | 448 | ); |
| 435 | 449 | } |
| @@ -437,9 +451,9 @@ | ||
| 437 | 451 | return new WP_Error( |
| 438 | 452 | 'convertkit_block_form_trigger_get_html', |
| 439 | 453 | sprintf( |
| 440 | 454 | /* translators: ConvertKit Form ID */ |
| 441 | - __( 'ConvertKit Form ID %s has no embed_js property.', 'convertkit' ), | |
| 455 | + __( 'Kit Form ID %s has no embed_js property.', 'convertkit' ), | |
| 442 | 456 | $id |
| 443 | 457 | ) |
| 444 | 458 | ); |
| 445 | 459 | } |
| @@ -452,9 +466,9 @@ | ||
| 452 | 466 | } else { |
| 453 | 467 | $html .= '<a data-formkit-toggle="' . esc_attr( $form['uid'] ) . '" href="' . esc_url( $form['embed_url'] ) . '"'; |
| 454 | 468 | } |
| 455 | 469 | |
| 456 | - $html .= ' class="wp-block-button__link ' . implode( ' ', map_deep( $css_classes, 'sanitize_html_class' ) ) . '" style="' . implode( ';', map_deep( $css_styles, 'esc_attr' ) ) . '">'; | |
| 470 | + $html .= ' class="' . implode( ' ', map_deep( $css_classes, 'sanitize_html_class' ) ) . '" style="' . implode( ';', map_deep( $css_styles, 'esc_attr' ) ) . '">'; | |
| 457 | 471 | $html .= esc_html( $button_text ); |
| 458 | 472 | |
| 459 | 473 | if ( $return_as_span ) { |
| 460 | 474 | $html .= '</span>'; |
| @@ -466,9 +480,9 @@ | ||
| 466 | 480 | |
| 467 | 481 | // Register the script, so it's only loaded once for this non-inline form across the entire page. |
| 468 | 482 | add_filter( |
| 469 | 483 | 'convertkit_output_scripts_footer', |
| 470 | - function( $scripts ) use ( $form ) { | |
| 484 | + function ( $scripts ) use ( $form ) { | |
| 471 | 485 | |
| 472 | 486 | $scripts[] = array( |
| 473 | 487 | 'async' => true, |
| 474 | 488 | 'data-uid' => $form['uid'], |