← All changes
|
includes/blocks/class-convertkit-block-product.php
+164
-99
2.2.0
→
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_scripts_editor_and_frontend', array( $this, 'enqueue_scripts' ) ); |
| 35 | 35 | add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) ); |
| @@ -36,19 +36,8 @@ | ||
| 36 | 36 | |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | - * Enqueues scripts for this Gutenberg Block in the editor view. | |
| 41 | - * | |
| 42 | - * @since 1.9.8.5 | |
| 43 | - */ | |
| 44 | - public function enqueue_scripts_editor() { | |
| 45 | - | |
| 46 | - wp_enqueue_script( 'convertkit-gutenberg-block-product', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-product.js', array( 'convertkit-gutenberg' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 47 | - | |
| 48 | - } | |
| 49 | - | |
| 50 | - /** | |
| 51 | 40 | * Enqueues scripts for this Gutenberg Block in the editor and frontend views. |
| 52 | 41 | * |
| 53 | 42 | * @since 1.9.8.5 |
| 54 | 43 | */ |
| @@ -74,10 +63,13 @@ | ||
| 74 | 63 | * @since 1.9.8.5 |
| 75 | 64 | */ |
| 76 | 65 | public function enqueue_styles() { |
| 77 | 66 | |
| 78 | - wp_enqueue_style( 'convertkit-button', CONVERTKIT_PLUGIN_URL . 'resources/frontend/css/button.css', array(), CONVERTKIT_PLUGIN_VERSION ); | |
| 67 | + convertkit_enqueue_frontend_css(); | |
| 79 | 68 | |
| 69 | + // Enqueue the block button CSS. | |
| 70 | + wp_enqueue_style( 'wp-block-button' ); | |
| 71 | + | |
| 80 | 72 | } |
| 81 | 73 | |
| 82 | 74 | /** |
| 83 | 75 | * Returns this block's programmatic name, excluding the convertkit- prefix. |
| @@ -88,8 +80,9 @@ | ||
| 88 | 80 | |
| 89 | 81 | /** |
| 90 | 82 | * This will register as: |
| 91 | 83 | * - a shortcode, with the name [convertkit_product]. |
| 84 | + * - a shortcode, with the name [kit_product]. | |
| 92 | 85 | * - a Gutenberg block, with the name convertkit/product. |
| 93 | 86 | */ |
| 94 | 87 | return 'product'; |
| 95 | 88 | |
| @@ -95,8 +88,43 @@ | ||
| 95 | 88 | |
| 96 | 89 | } |
| 97 | 90 | |
| 98 | 91 | /** |
| 92 | + * Returns this block's title. | |
| 93 | + * | |
| 94 | + * @since 3.1.1 | |
| 95 | + */ | |
| 96 | + public function get_title() { | |
| 97 | + | |
| 98 | + return __( 'Kit Product', 'convertkit' ); | |
| 99 | + | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Returns this block's plural title. | |
| 104 | + * | |
| 105 | + * @since 3.4.0 | |
| 106 | + * | |
| 107 | + * @return string | |
| 108 | + */ | |
| 109 | + public function get_title_plural() { | |
| 110 | + | |
| 111 | + return __( 'Kit Products', 'convertkit' ); | |
| 112 | + | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 116 | + * Returns this block's icon. | |
| 117 | + * | |
| 118 | + * @since 3.1.1 | |
| 119 | + */ | |
| 120 | + public function get_icon() { | |
| 121 | + | |
| 122 | + return 'resources/backend/images/block-icon-product.svg'; | |
| 123 | + | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 99 | 127 | * Returns this block's Title, Icon, Categories, Keywords and properties. |
| 100 | 128 | * |
| 101 | 129 | * @since 1.9.8.5 |
| 102 | 130 | */ |
| @@ -101,42 +129,62 @@ | ||
| 101 | 129 | * @since 1.9.8.5 |
| 102 | 130 | */ |
| 103 | 131 | public function get_overview() { |
| 104 | 132 | |
| 133 | + $convertkit_products = new ConvertKit_Resource_Products( 'block_edit' ); | |
| 134 | + $settings = new ConvertKit_Settings(); | |
| 135 | + | |
| 105 | 136 | return array( |
| 106 | - 'title' => __( 'ConvertKit Product', 'convertkit' ), | |
| 107 | - 'description' => __( 'Displays a button to purchase a ConvertKit product.', 'convertkit' ), | |
| 108 | - 'icon' => 'resources/backend/images/block-icon-product.png', | |
| 109 | - 'category' => 'convertkit', | |
| 110 | - 'keywords' => array( | |
| 137 | + 'title' => $this->get_title(), | |
| 138 | + 'description' => __( 'Displays a button to purchase a Kit product.', 'convertkit' ), | |
| 139 | + 'icon' => $this->get_icon(), | |
| 140 | + 'category' => 'convertkit', | |
| 141 | + 'keywords' => array( | |
| 111 | 142 | __( 'ConvertKit', 'convertkit' ), |
| 143 | + __( 'Kit', 'convertkit' ), | |
| 112 | 144 | __( 'Product', 'convertkit' ), |
| 113 | 145 | ), |
| 114 | 146 | |
| 115 | 147 | // Function to call when rendering as a block or a shortcode on the frontend web site. |
| 116 | - 'render_callback' => array( $this, 'render' ), | |
| 148 | + 'render_callback' => array( $this, 'render' ), | |
| 117 | 149 | |
| 118 | 150 | // Shortcode: TinyMCE / QuickTags Modal Width and Height. |
| 119 | - 'modal' => array( | |
| 120 | - 'width' => 500, | |
| 121 | - 'height' => 290, | |
| 151 | + 'modal' => array( | |
| 152 | + 'width' => 600, | |
| 153 | + 'height' => 518, | |
| 122 | 154 | ), |
| 123 | 155 | |
| 124 | 156 | // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals. |
| 125 | - 'shortcode_include_closing_tag' => false, | |
| 157 | + 'shortcode_include_closing_tag' => false, | |
| 126 | 158 | |
| 127 | 159 | // Gutenberg: Block Icon in Editor. |
| 128 | - 'gutenberg_icon' => file_get_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-product.svg' ), /* phpcs:ignore */ | |
| 160 | + 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-product.svg' ), | |
| 129 | 161 | |
| 130 | 162 | // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg. |
| 131 | - 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-product.png', | |
| 163 | + 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-product.png', | |
| 132 | 164 | |
| 133 | - // Gutenberg: Help description, displayed when no settings defined for a newly added Block. | |
| 134 | - 'gutenberg_help_description' => __( 'Select a Product using the Product option in the Gutenberg sidebar.', 'convertkit' ), | |
| 165 | + // Help descriptions, displayed when no Access Token / resources exist and this block/shortcode is added. | |
| 166 | + 'no_access_token' => array( | |
| 167 | + 'notice' => __( 'Not connected to Kit.', 'convertkit' ), | |
| 168 | + 'link' => convertkit_get_setup_wizard_plugin_link(), | |
| 169 | + 'link_text' => __( 'Click here to connect your Kit account.', 'convertkit' ), | |
| 170 | + 'instruction_text' => __( 'Connect your Kit account at Settings > Kit, and then refresh this page to select a product.', 'convertkit' ), | |
| 171 | + ), | |
| 172 | + 'no_resources' => array( | |
| 173 | + 'notice' => __( 'No products exist in Kit.', 'convertkit' ), | |
| 174 | + 'link' => convertkit_get_new_product_url(), | |
| 175 | + 'link_text' => __( 'Click here to create your first product.', 'convertkit' ), | |
| 176 | + 'instruction_text' => __( 'Add a product to your Kit account, and then refresh this page to select a product.', 'convertkit' ), | |
| 177 | + ), | |
| 135 | 178 | |
| 136 | - // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor. | |
| 137 | - // If not defined, render_callback above will be used. | |
| 138 | - 'gutenberg_preview_render_callback' => 'convertKitGutenbergProductBlockRenderPreview', | |
| 179 | + // Gutenberg: Help description, displayed when the defined attribute has not been set for the block. | |
| 180 | + 'gutenberg_help_description' => __( 'Select a Product using the Product option in the Gutenberg sidebar.', 'convertkit' ), | |
| 181 | + 'gutenberg_help_description_attribute' => 'product', | |
| 182 | + | |
| 183 | + // Whether an Access Token exists in the Plugin, and are the required resources (products) available. | |
| 184 | + // If no Access Token is specified in the Plugin's settings, render the "Not Connected" output. | |
| 185 | + 'has_access_token' => $settings->has_access_and_refresh_token(), | |
| 186 | + 'has_resources' => $convertkit_products->exist(), | |
| 139 | 187 | ); |
| 140 | 188 | |
| 141 | 189 | } |
| 142 | 190 | |
| @@ -148,57 +196,47 @@ | ||
| 148 | 196 | public function get_attributes() { |
| 149 | 197 | |
| 150 | 198 | return array( |
| 151 | 199 | // Block attributes. |
| 152 | - 'product' => array( | |
| 200 | + 'product' => array( | |
| 153 | 201 | 'type' => 'string', |
| 154 | 202 | 'default' => $this->get_default_value( 'product' ), |
| 155 | 203 | ), |
| 156 | - 'text' => array( | |
| 204 | + 'text' => array( | |
| 157 | 205 | 'type' => 'string', |
| 158 | 206 | 'default' => $this->get_default_value( 'text' ), |
| 159 | 207 | ), |
| 208 | + 'discount_code' => array( | |
| 209 | + 'type' => 'string', | |
| 210 | + 'default' => $this->get_default_value( 'discount_code' ), | |
| 211 | + ), | |
| 212 | + 'checkout' => array( | |
| 213 | + 'type' => 'boolean', | |
| 214 | + 'default' => $this->get_default_value( 'checkout' ), | |
| 215 | + ), | |
| 216 | + 'disable_modal_on_mobile' => array( | |
| 217 | + 'type' => 'boolean', | |
| 218 | + 'default' => $this->get_default_value( 'disable_modal_on_mobile' ), | |
| 219 | + ), | |
| 160 | 220 | |
| 161 | 221 | // The below are built in Gutenberg attributes registered in get_supports(). |
| 162 | 222 | |
| 163 | - // Color. | |
| 164 | - 'backgroundColor' => array( | |
| 223 | + // get_supports() style, color and typography attributes. | |
| 224 | + 'style' => array( | |
| 225 | + 'type' => 'object', | |
| 226 | + ), | |
| 227 | + 'backgroundColor' => array( | |
| 165 | 228 | 'type' => 'string', |
| 166 | 229 | ), |
| 167 | - 'textColor' => array( | |
| 230 | + 'textColor' => array( | |
| 168 | 231 | 'type' => 'string', |
| 169 | 232 | ), |
| 170 | - | |
| 171 | - // Typography. | |
| 172 | - 'fontSize' => array( | |
| 233 | + 'fontSize' => array( | |
| 173 | 234 | 'type' => 'string', |
| 174 | 235 | ), |
| 175 | 236 | |
| 176 | - // Spacing/Dimensions > Padding. | |
| 177 | - 'style' => array( | |
| 178 | - 'type' => 'object', | |
| 179 | - 'visualizers' => array( | |
| 180 | - 'type' => 'object', | |
| 181 | - 'padding' => array( | |
| 182 | - 'type' => 'object', | |
| 183 | - 'top' => array( | |
| 184 | - 'type' => 'boolean', | |
| 185 | - ), | |
| 186 | - 'bottom' => array( | |
| 187 | - 'type' => 'boolean', | |
| 188 | - ), | |
| 189 | - 'left' => array( | |
| 190 | - 'type' => 'boolean', | |
| 191 | - ), | |
| 192 | - 'right' => array( | |
| 193 | - 'type' => 'boolean', | |
| 194 | - ), | |
| 195 | - ), | |
| 196 | - ), | |
| 197 | - ), | |
| 198 | - | |
| 199 | 237 | // Always required for Gutenberg. |
| 200 | - 'is_gutenberg_example' => array( | |
| 238 | + 'is_gutenberg_example' => array( | |
| 201 | 239 | 'type' => 'boolean', |
| 202 | 240 | 'default' => false, |
| 203 | 241 | ), |
| 204 | 242 | ); |
| @@ -216,19 +254,23 @@ | ||
| 216 | 254 | |
| 217 | 255 | return array( |
| 218 | 256 | 'className' => true, |
| 219 | 257 | 'color' => array( |
| 220 | - 'background' => true, | |
| 221 | - 'text' => true, | |
| 258 | + 'background' => true, | |
| 259 | + 'text' => true, | |
| 260 | + | |
| 261 | + // Don't apply styles to the block editor's div element. | |
| 262 | + // This ensures what's rendered in the Gutenberg editor matches the frontend output for styling. | |
| 263 | + // See: https://github.com/WordPress/gutenberg/issues/32417. | |
| 264 | + '__experimentalSkipSerialization' => true, | |
| 222 | 265 | ), |
| 223 | 266 | 'typography' => array( |
| 224 | - 'fontSize' => true, | |
| 267 | + 'fontSize' => true, | |
| 268 | + 'lineHeight' => true, | |
| 225 | 269 | ), |
| 226 | 270 | 'spacing' => array( |
| 227 | - 'padding' => array( | |
| 228 | - 'horizontal', | |
| 229 | - 'vertical', | |
| 230 | - ), | |
| 271 | + 'margin' => true, | |
| 272 | + 'padding' => true, | |
| 231 | 273 | ), |
| 232 | 274 | ); |
| 233 | 275 | |
| 234 | 276 | } |
| @@ -241,13 +283,8 @@ | ||
| 241 | 283 | * @return bool|array |
| 242 | 284 | */ |
| 243 | 285 | public function get_fields() { |
| 244 | 286 | |
| 245 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 246 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 247 | - return false; | |
| 248 | - } | |
| 249 | - | |
| 250 | 287 | // Get ConvertKit Products. |
| 251 | 288 | $products = array(); |
| 252 | 289 | $convertkit_products = new ConvertKit_Resource_Products(); |
| 253 | 290 | if ( $convertkit_products->exist() ) { |
| @@ -258,26 +295,42 @@ | ||
| 258 | 295 | |
| 259 | 296 | // Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included |
| 260 | 297 | // automatically by Gutenberg. |
| 261 | 298 | return array( |
| 262 | - 'product' => array( | |
| 263 | - 'label' => __( 'Product', 'convertkit' ), | |
| 264 | - 'type' => 'select', | |
| 265 | - 'values' => $products, | |
| 299 | + 'product' => array( | |
| 300 | + 'label' => __( 'Product', 'convertkit' ), | |
| 301 | + 'type' => 'resource', | |
| 302 | + 'resource' => 'products', | |
| 303 | + 'values' => $products, | |
| 266 | 304 | ), |
| 267 | - 'text' => array( | |
| 305 | + 'text' => array( | |
| 268 | 306 | 'label' => __( 'Button Text', 'convertkit' ), |
| 269 | 307 | 'type' => 'text', |
| 270 | 308 | 'description' => __( 'The text to display for the button.', 'convertkit' ), |
| 271 | 309 | ), |
| 310 | + 'discount_code' => array( | |
| 311 | + 'label' => __( 'Discount Code', 'convertkit' ), | |
| 312 | + 'type' => 'text', | |
| 313 | + 'description' => __( 'Optional: A discount code to include. Must be defined in the Kit Product.', 'convertkit' ), | |
| 314 | + ), | |
| 315 | + 'checkout' => array( | |
| 316 | + 'label' => __( 'Load checkout step', 'convertkit' ), | |
| 317 | + 'type' => 'toggle', | |
| 318 | + 'description' => __( 'If enabled, immediately loads the checkout screen, instead of the Kit Product description.', 'convertkit' ), | |
| 319 | + ), | |
| 320 | + 'disable_modal_on_mobile' => array( | |
| 321 | + 'label' => __( 'Disable modal on mobile', 'convertkit' ), | |
| 322 | + 'type' => 'toggle', | |
| 323 | + 'description' => __( 'Recommended if the Kit Product is a digital download being purchased on mobile, to ensure the subscriber can immediately download the PDF once purchased.', 'convertkit' ), | |
| 324 | + ), | |
| 272 | 325 | |
| 273 | 326 | // These fields will only display on the shortcode, and are deliberately not registered in get_attributes(), |
| 274 | 327 | // because Gutenberg will register its own color pickers for link, background and text. |
| 275 | - 'background_color' => array( | |
| 328 | + 'background_color' => array( | |
| 276 | 329 | 'label' => __( 'Background color', 'convertkit' ), |
| 277 | 330 | 'type' => 'color', |
| 278 | 331 | ), |
| 279 | - 'text_color' => array( | |
| 332 | + 'text_color' => array( | |
| 280 | 333 | 'label' => __( 'Text color', 'convertkit' ), |
| 281 | 334 | 'type' => 'color', |
| 282 | 335 | ), |
| 283 | 336 | ); |
| @@ -292,13 +345,8 @@ | ||
| 292 | 345 | * @return bool|array |
| 293 | 346 | */ |
| 294 | 347 | public function get_panels() { |
| 295 | 348 | |
| 296 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 297 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 298 | - return false; | |
| 299 | - } | |
| 300 | - | |
| 301 | 349 | // Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included |
| 302 | 350 | // automatically by Gutenberg. |
| 303 | 351 | return array( |
| 304 | 352 | 'general' => array( |
| @@ -305,8 +353,11 @@ | ||
| 305 | 353 | 'label' => __( 'General', 'convertkit' ), |
| 306 | 354 | 'fields' => array( |
| 307 | 355 | 'product', |
| 308 | 356 | 'text', |
| 357 | + 'discount_code', | |
| 358 | + 'checkout', | |
| 359 | + 'disable_modal_on_mobile', | |
| 309 | 360 | 'background_color', |
| 310 | 361 | 'text_color', |
| 311 | 362 | ), |
| 312 | 363 | ), |
| @@ -323,18 +374,21 @@ | ||
| 323 | 374 | */ |
| 324 | 375 | public function get_default_values() { |
| 325 | 376 | |
| 326 | 377 | return array( |
| 327 | - 'product' => '', | |
| 328 | - 'text' => __( 'Buy my product', 'convertkit' ), | |
| 329 | - 'background_color' => '', | |
| 330 | - 'text_color' => '', | |
| 378 | + 'product' => '', | |
| 379 | + 'text' => __( 'Buy my product', 'convertkit' ), | |
| 380 | + 'discount_code' => '', | |
| 381 | + 'checkout' => false, | |
| 382 | + 'disable_modal_on_mobile' => false, | |
| 383 | + 'background_color' => '', | |
| 384 | + 'text_color' => '', | |
| 331 | 385 | |
| 332 | 386 | // Built-in Gutenberg block attributes. |
| 333 | - 'backgroundColor' => '', | |
| 334 | - 'textColor' => '', | |
| 335 | - 'fontSize' => '', | |
| 336 | - 'style' => array( | |
| 387 | + 'backgroundColor' => '', | |
| 388 | + 'textColor' => '', | |
| 389 | + 'fontSize' => '', | |
| 390 | + 'style' => array( | |
| 337 | 391 | 'visualizers' => array( |
| 338 | 392 | 'padding' => array( |
| 339 | 393 | 'top' => '', |
| 340 | 394 | 'bottom' => '', |
| @@ -351,10 +405,10 @@ | ||
| 351 | 405 | * Returns the block's output, based on the supplied configuration attributes. |
| 352 | 406 | * |
| 353 | 407 | * @since 1.9.8.5 |
| 354 | 408 | * |
| 355 | - * @param array $atts Block / Shortcode Attributes. | |
| 356 | - * @return string Output | |
| 409 | + * @param array $atts Block / Shortcode / Page Builder Module Attributes. | |
| 410 | + * @return string | |
| 357 | 411 | */ |
| 358 | 412 | public function render( $atts ) { |
| 359 | 413 | |
| 360 | 414 | // Parse attributes, defining fallback defaults if required |
| @@ -367,11 +421,22 @@ | ||
| 367 | 421 | // Get Products Resource. |
| 368 | 422 | $convertkit_products = new ConvertKit_Resource_Products(); |
| 369 | 423 | |
| 370 | 424 | // Build HTML. |
| 371 | - $html = $convertkit_products->get_html( $atts['product'], $atts['text'], $atts['_css_classes'], $atts['_css_styles'], $this->is_block_editor_request() ); | |
| 425 | + $html = $convertkit_products->get_html( | |
| 426 | + $atts['product'], | |
| 427 | + $atts['text'], | |
| 428 | + array( | |
| 429 | + 'discount_code' => $atts['discount_code'], | |
| 430 | + 'checkout' => $atts['checkout'], | |
| 431 | + 'disable_modal' => ( $atts['disable_modal_on_mobile'] && wp_is_mobile() ), | |
| 432 | + 'css_classes' => $this->get_css_classes( array( 'wp-block-button__link', 'wp-element-button' ) ), | |
| 433 | + 'css_styles' => $this->get_css_styles( $atts ), | |
| 434 | + 'return_as_span' => $this->is_block_editor_request(), | |
| 435 | + ) | |
| 436 | + ); | |
| 372 | 437 | |
| 373 | - // Bail if an error occured. | |
| 438 | + // Bail if an error occurred. | |
| 374 | 439 | if ( is_wp_error( $html ) ) { |
| 375 | 440 | if ( $settings->debug_enabled() ) { |
| 376 | 441 | return '<!-- ' . $html->get_error_message() . ' -->'; |
| 377 | 442 | } |