PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.3
3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 All 196 releases
convertkit / includes / blocks / class-convertkit-block-product.php

class-convertkit-block-product.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.3, at includes/blocks/class-convertkit-block-product.php

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