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