PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.7.7
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.7.7
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / includes / variations.php

variations.php in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts 2.7.7, at includes/variations.php

778 lines 22.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Register custom variations
4 *
5 * @package BoldBlocks
6 * @author Phi Phan <mrphipv@gmail.com>
7 * @copyright Copyright (c) 2022, Phi Phan
8 */
9
10 namespace BoldBlocks;
11
12 // Exit if accessed directly.
13 defined( 'ABSPATH' ) || exit;
14
15 if ( ! class_exists( Variations::class ) ) :
16 /**
17 * Manage custom block variations
18 */
19 class Variations extends CoreComponent {
20 /**
21 * Post type
22 *
23 * @var string
24 */
25 protected $post_type = 'boldblocks_variation';
26
27 /**
28 * Post type helper instance
29 *
30 * @var PostType
31 */
32 protected $post_type_helper;
33
34 /**
35 * Store all variations
36 *
37 * @var array
38 */
39 protected $all_variations = [];
40
41 /**
42 * Max items that can query
43 *
44 * @var integer
45 */
46 protected $max_items = 200;
47
48 /**
49 * List of custom style for variations
50 *
51 * @var array
52 */
53 private $variation_style_array = [];
54
55 /**
56 * Constructor
57 */
58 public function __construct( $the_plugin_instance ) {
59 parent::__construct( $the_plugin_instance );
60
61 $this->post_type_helper = PostType::get_instance();
62 }
63
64 /**
65 * Run main hooks
66 *
67 * @return void
68 */
69 public function run() {
70 // Register custom post type.
71 add_action( 'init', [ $this, 'register_custom_post_type' ], 5 );
72
73 // Custom variation columns.
74 add_filter( 'manage_edit-' . $this->post_type . '_columns', [ $this, 'add_variation_custom_columns' ] );
75 add_action( 'manage_' . $this->post_type . '_posts_custom_column', [ $this, 'manage_variation_columns' ], 10, 2 );
76 add_filter( 'manage_edit-' . $this->post_type . '_sortable_columns', [ $this, 'variation_sortable_columns' ] );
77 add_action( 'pre_get_posts', [ $this, 'pre_get_posts_order_variation_custom_columns' ], 1 );
78
79 // Load custom block variation styles.
80 add_action( 'init', [ $this, 'load_custom_block_variation_styles' ] );
81
82 // Only do customization on 'edit.php' page for variation.
83 add_action( 'load-edit.php', [ $this, 'admin_listing_variation_page' ] );
84
85 // Enqueue scripts for variations.
86 add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
87
88 // Update template on the fly for variation post type.
89 add_action( 'admin_init', [ $this, 'update_template_for_variation' ] );
90
91 // Add rest api endpoint to create variations.
92 add_action( 'rest_api_init', [ $this, 'build_create_variation_endpoint' ] );
93
94 // Add meta fields to meta revisioning.
95 add_filter( 'boldblocks_post_revision_meta_keys', [ $this, 'add_fields_to_revision_meta_keys' ] );
96
97 // Handle save post.
98 add_action( 'save_post_' . $this->post_type, [ $this, 'save_post' ], 10, 2 );
99
100 // Clear the transient cache on upgraded.
101 add_action( 'cbb_version_upgraded', [ $this, 'clear_transient_cache' ] );
102
103 // Enqueue styles for the iframe editor.
104 add_filter( 'block_editor_settings_all', [ $this, 'enqueue_style_for_the_editor' ] );
105
106 // Enqueue custom scripts & styles for custom variations.
107 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_custom_variation_scripts' ] );
108
109 // Force to remove custom fields.
110 add_action( 'add_meta_boxes', [ $this, 'remove_meta_boxes' ] );
111 }
112
113 /**
114 * Register custom post type
115 *
116 * @return void
117 */
118 public function register_custom_post_type() {
119 // Register variation type.
120 $this->post_type_helper->register_post_type(
121 $this->post_type,
122 [
123 'rest_base' => 'boldblocks-variations',
124 'show_in_menu' => 'edit.php?post_type=boldblocks_block',
125 'show_in_admin_bar' => false,
126 'capabilities' => array(
127 'create_posts' => 'do_not_allow',
128 'edit_posts' => 'manage_options',
129 ),
130 'map_meta_cap' => true,
131 ],
132 [
133 'name' => _x( 'Block Variations', 'post type general name', 'content-blocks-builder' ),
134 'singular_name' => _x( 'Block Variation', 'post type singular name', 'content-blocks-builder' ),
135 'all_items' => __( 'All Variations', 'content-blocks-builder' ),
136 ]
137 );
138
139 // Meta fields.
140 $this->register_meta( 'boldblocks_variation_block_name' );
141
142 $this->register_meta( 'boldblocks_variation_name' );
143
144 $this->register_meta( 'boldblocks_variation_description' );
145
146 $this->register_meta( 'boldblocks_variation_icon' );
147
148 $this->register_meta(
149 'boldblocks_variation_is_default',
150 [
151 'type' => 'boolean',
152 'default' => false,
153 ]
154 );
155
156 $this->register_meta(
157 'boldblocks_variation_is_transformable',
158 [
159 'type' => 'boolean',
160 'default' => false,
161 ]
162 );
163
164 $this->register_meta(
165 'boldblocks_variation_hide_from_inserter',
166 [
167 'type' => 'boolean',
168 'default' => false,
169 ]
170 );
171
172 $this->register_meta(
173 'boldblocks_variation_data',
174 [
175 'default' => '{}',
176 ]
177 );
178
179 $this->register_meta(
180 'boldblocks_variation_dependent_blocks',
181 [
182 'show_in_rest' => array(
183 'schema' => array(
184 'items' => array(
185 'type' => 'string',
186 ),
187 ),
188 ),
189 'type' => 'array',
190 'default' => [],
191 ]
192 );
193
194 $this->register_meta( 'boldblocks_variation_custom_style' );
195
196 // Register a block style for a custom variation.
197 $this->register_meta(
198 'boldblocks_variation_enable_style',
199 [
200 'type' => 'boolean',
201 'default' => false,
202 ]
203 );
204 }
205
206 /**
207 * Register meta field
208 *
209 * @param string $meta_key
210 * @param array $args
211 * @return void
212 */
213 private function register_meta( $meta_key, $args = [] ) {
214 $this->post_type_helper->register_meta( $this->post_type, $meta_key, $args );
215 }
216
217 /**
218 * Remove custom meta boxes
219 *
220 * @param string $post_type
221 * @return void
222 */
223 public function remove_meta_boxes( $post_type ) {
224 if ( $post_type === $this->post_type ) {
225 remove_meta_box( 'postcustom', false, 'normal' );
226 }
227 }
228
229 /**
230 * Load all custom variation styles
231 *
232 * @return void
233 */
234 public function load_custom_block_variation_styles() {
235 // Load all custom variations.
236 $all_custom_variations = $this->get_all_variations();
237
238 foreach ( $all_custom_variations as $block_variation ) {
239 $variation_handle = false;
240 $custom_style = $block_variation['custom_style'] ?? false;
241 if ( $custom_style ) {
242 $variation_handle = $block_variation['variationClass'];
243 wp_register_style( $variation_handle, '' );
244 wp_add_inline_style( $variation_handle, $custom_style );
245
246 wp_enqueue_block_style( $block_variation['blockName'], [ 'handle' => $variation_handle ] );
247
248 // Save custom style.
249 $this->save_variation_style( $variation_handle, $custom_style, $block_variation );
250 }
251
252 if ( $block_variation['enable_block_style'] ?? false ) {
253 $style_args = [
254 'name' => str_replace( '/', '-', $block_variation['variationName'] ?? '' ),
255 'label' => $block_variation['title'] ?? '',
256 ];
257
258 if ( $variation_handle ) {
259 $style_args['style_handle'] = $variation_handle;
260 }
261
262 register_block_style(
263 $block_variation['blockName'] ?? '',
264 $style_args
265 );
266 }
267 }
268 }
269
270 /**
271 * Enqueue editor assets
272 *
273 * @return void
274 */
275 public function enqueue_block_editor_assets() {
276 // Variations access file.
277 $variations_asset = $this->the_plugin_instance->include_file( 'build/variations.asset.php' );
278
279 // Scripts.
280 wp_enqueue_script(
281 'boldblocks-variations',
282 $this->the_plugin_instance->get_file_uri( '/build/variations.js' ),
283 $variations_asset['dependencies'] ?? [],
284 $this->the_plugin_instance->get_script_version( $variations_asset ),
285 [ 'in_footer' => true ]
286 );
287
288 // Add translation.
289 wp_set_script_translations( 'boldblocks-variations', 'content-blocks-builder' );
290 }
291
292 /**
293 * Update template for variation
294 *
295 * @return void
296 */
297 public function update_template_for_variation() {
298 global $pagenow;
299
300 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
301 if ( 'post.php' === $pagenow && isset( $_GET['post'] ) ) {
302 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
303 $post_id = absint( $_GET['post'] );
304 $current_post = get_post( $post_id );
305
306 if ( ! $current_post || ! $current_post instanceof \WP_Post ) {
307 return;
308 }
309
310 if ( $this->post_type !== $current_post->post_type ) {
311 return;
312 }
313
314 $block_name = get_post_meta( $post_id, 'boldblocks_variation_block_name', true );
315 if ( $block_name ) {
316 $post_type_object = get_post_type_object( $this->post_type );
317 $post_type_object->template = array(
318 array( $block_name ),
319 );
320 }
321 }
322 }
323
324 /**
325 * Get all variations
326 *
327 * @return array
328 */
329 public function get_all_variations() {
330 if ( ! $this->all_variations ) {
331 $this->all_variations = $this->get_posts( $this->the_plugin_instance->is_debug_mode() );
332 }
333
334 return $this->all_variations;
335 }
336
337 /**
338 * Query posts and cache the results.
339 *
340 * @param bool $force_refresh Optional. Whether to force the cache to be refreshed. Default false.
341 * @return array Array of WP_Post objects.
342 */
343 public function get_posts( $force_refresh = false ) {
344 if ( $force_refresh ) {
345 return $this->query_posts();
346 }
347
348 $cache_key = 'bb_variation_posts';
349 $posts = get_transient( $cache_key );
350
351 // If nothing is found, build the object.
352 if ( false === $posts ) {
353 // Query posts.
354 $posts = $this->query_posts();
355
356 if ( ! is_wp_error( $posts ) && count( $posts ) > 0 ) {
357 set_transient( $cache_key, $posts, HOUR_IN_SECONDS );
358 }
359 }
360
361 return $posts;
362 }
363
364 /**
365 * Query and parse variations
366 *
367 * @return array
368 */
369 public function query_posts() {
370 $raw_posts = get_posts(
371 [
372 'post_type' => $this->post_type,
373 'posts_per_page' => $this->get_max_items(),
374 'orderby' => [
375 'menu_order' => 'DESC',
376 'date' => 'DESC',
377 ],
378 ]
379 );
380
381 $custom_style_handler = $this->the_plugin_instance->get_component( CustomStyle::class );
382
383 $variation_posts = array_map(
384 function( $item ) use ( $custom_style_handler ) {
385 $variation_name = get_post_meta( $item->ID, 'boldblocks_variation_name', true );
386 $variation_class = 'is-style-' . str_replace( '/', '-', $variation_name );
387
388 $custom_style = get_post_meta( $item->ID, 'boldblocks_variation_custom_style', true );
389 if ( $custom_style ) {
390 $custom_style = $custom_style_handler->refine_custom_value( $custom_style, [ 'selector' => '.' . $variation_class ], 'CSS' );
391 }
392
393 return [
394 'id' => $item->ID,
395 'blockName' => get_post_meta( $item->ID, 'boldblocks_variation_block_name', true ),
396 'variationName' => $variation_name,
397 'title' => $item->post_title,
398 'postContent' => $item->post_content,
399 'description' => get_post_meta( $item->ID, 'boldblocks_variation_description', true ),
400 'blockIcon' => get_post_meta( $item->ID, 'boldblocks_variation_icon', true ),
401 'isDefault' => ! ! get_post_meta( $item->ID, 'boldblocks_variation_is_default', true ),
402 'isTransformable' => ! ! get_post_meta( $item->ID, 'boldblocks_variation_is_transformable', true ),
403 'hideFromInserter' => ! ! get_post_meta( $item->ID, 'boldblocks_variation_hide_from_inserter', true ),
404 'variationData' => get_post_meta( $item->ID, 'boldblocks_variation_data', true ),
405 'dependentBlocks' => get_post_meta( $item->ID, 'boldblocks_variation_dependent_blocks', true ),
406 'variationClass' => $variation_class,
407 'custom_style' => $custom_style,
408 'enable_block_style' => ! ! get_post_meta( $item->ID, 'boldblocks_variation_enable_style', true ),
409 ];
410 },
411 $raw_posts
412 );
413
414 return $variation_posts;
415 }
416
417 /**
418 * Handle save post
419 *
420 * @param int $post_id
421 * @param WP_Post $post
422 * @return void
423 */
424 public function save_post( $post_id, $post ) {
425 // Ignore auto-draft status.
426 if ( 'auto-draft' === $post->post_status ) {
427 return;
428 }
429
430 // Ignore autosave.
431 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
432 return;
433 }
434
435 $this->clear_transient_cache();
436 }
437
438 /**
439 * Clear transient cache
440 *
441 * @return void
442 */
443 public function clear_transient_cache() {
444 delete_transient( 'bb_variation_posts' );
445 }
446
447 /**
448 * Build a custom endpoint to create variation
449 *
450 * @return void
451 */
452 public function build_create_variation_endpoint() {
453 register_rest_route(
454 'boldblocks/v1',
455 '/createVariation/',
456 array(
457 'methods' => 'POST',
458 'callback' => [ $this, 'create_variation' ],
459 'permission_callback' => function () {
460 return current_user_can( 'publish_posts' );
461 },
462 )
463 );
464 }
465
466 /**
467 * Create variation
468 *
469 * @param WP_REST_Request $request The request object.
470 * @return void
471 */
472 public function create_variation( $request ) {
473 $args = $request->get_params();
474
475 if ( empty( $args['title'] ) || empty( $args['content'] ) || empty( $args['meta'] ) ) {
476 wp_send_json_error( __( 'Invalid request!', 'content-blocks-builder' ), 400 );
477 }
478
479 // Using Posts controller to create a custom variation.
480 $controller = new \WP_REST_Posts_Controller( $this->post_type );
481 $response = $controller->create_item( $request );
482
483 // Create a hook for new variation created.
484 do_action( 'boldblocks_variation_created', $response->data );
485
486 if ( is_wp_error( $response ) ) {
487 wp_send_json_error( $response );
488 }
489
490 if ( $response->status === 201 ) {
491 wp_send_json(
492 [
493 'data' => __( 'The new variation has been created!', 'content-blocks-builder' ),
494 'success' => true,
495 'post' => $response->data,
496 ]
497 );
498 }
499 }
500
501 /**
502 * Add custom columns to custom post type
503 *
504 * @param array $columns The array of columns.
505 * @return array
506 */
507 public function add_variation_custom_columns( $columns ) {
508 $custom_columns = array(
509 'block_name' => esc_html__( 'Block name', 'content-blocks-builder' ),
510 'variation_name' => esc_html__( 'Variation name', 'content-blocks-builder' ),
511 'is_default' => esc_html__( 'Is default?', 'content-blocks-builder' ),
512 );
513
514 // Add after title column.
515 $return = array_slice( $columns, 0, 2, true ) + $custom_columns + array_slice( $columns, 2, count( $columns ) - 2, true );
516
517 return $return;
518 }
519
520 /**
521 * Manage custom columns.
522 *
523 * @param string $column the column name.
524 * @param int $post_id the post id.
525 */
526 public function manage_variation_columns( $column, $post_id ) {
527 switch ( $column ) {
528 case 'block_name':
529 echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_block_name', true ) );
530 break;
531
532 case 'variation_name':
533 echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_name', true ) );
534 break;
535
536 case 'is_default':
537 echo get_post_meta( $post_id, 'boldblocks_variation_is_default', true ) ? esc_html__( 'Yes', 'content-blocks-builder' ) : esc_html__( 'No', 'content-blocks-builder' );
538 break;
539
540 // Just break out of the switch statement for everything else.
541 default:
542 break;
543 }
544 }
545
546 /**
547 * Make custom columns sortable.
548 *
549 * @param array $columns The array of columns.
550 * @return array
551 */
552 public function variation_sortable_columns( $columns ) {
553 $columns['block_name'] = 'block_name';
554 $columns['variation_name'] = 'variation_name';
555
556 return $columns;
557 }
558
559 /**
560 * Alter the main query for sorting.
561 *
562 * @param WP_Query $query The main query.
563 * @return void
564 */
565 public function pre_get_posts_order_variation_custom_columns( $query ) {
566 // Do nothing on front end side.
567 if ( ! is_admin() ) {
568 return;
569 }
570
571 // Only for main query with orderby parameter.
572 if ( $query->is_main_query() && $this->post_type === $query->get( 'post_type' ) && $query->get( 'orderby' ) ) {
573 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
574 if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) {
575 // Ignore if we're filtering.
576 return;
577 }
578
579 $orderby = $query->get( 'orderby' );
580
581 switch ( $orderby ) {
582 case 'block_name':
583 // set our query's meta_key, which is used for custom fields.
584 $query->set( 'meta_key', 'boldblocks_variation_block_name' );
585 $query->set( 'orderby', 'meta_value' );
586 break;
587
588 case 'variation_name':
589 // set our query's meta_key, which is used for custom fields.
590 $query->set( 'meta_key', 'boldblocks_variation_name' );
591 $query->set( 'orderby', 'meta_value' );
592 break;
593
594 default:
595 break;
596 }
597 }
598
599 }
600
601 /**
602 * Add filters for custom variation columns
603 *
604 * @return void
605 */
606 public function admin_listing_variation_page() {
607 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
608 if ( isset( $_GET['post_type'] ) && $this->post_type === $_GET['post_type'] ) {
609 add_filter( 'request', [ $this, 'add_custom_columns_to_query_vars' ] );
610 add_filter( 'restrict_manage_posts', [ $this, 'add_filter_select_control' ] );
611 }
612 }
613
614 /**
615 * Add custom vars to query vars
616 *
617 * @param array $query_vars The array of query vars.
618 * @return array
619 */
620 public function add_custom_columns_to_query_vars( $query_vars ) {
621 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
622 if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) {
623 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
624 $query_vars['meta_key'] = 'boldblocks_variation_block_name';
625 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value, WordPress.Security.NonceVerification.Recommended
626 $query_vars['meta_value'] = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) );
627 }
628
629 return $query_vars;
630 }
631
632 /**
633 * Add a filter select control to filter variation by block name
634 *
635 * @return void
636 */
637 public function add_filter_select_control() {
638 global $wpdb;
639 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
640 $block_names = $wpdb->get_col( 'SELECT DISTINCT meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key = "boldblocks_variation_block_name" ORDER BY meta_value' );
641
642 $boldblocks_variation_filter_block = false;
643 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
644 if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) {
645 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
646 $boldblocks_variation_filter_block = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) );
647 }
648 ?>
649 <select name="boldblocks_variation_filter_block" id="boldblocks_variation_filter_block">
650 <option value=""><?php esc_html_e( 'All Blocks', 'content-blocks-builder' ); ?></option>
651 <?php foreach ( $block_names as $block_name ) { ?>
652 <option value="<?php echo esc_attr( $block_name ); ?>"<?php selected( $boldblocks_variation_filter_block, $block_name ); ?>><?php echo esc_attr( $block_name ); ?></option>
653 <?php } ?>
654 </select>
655 <?php
656 }
657
658 /**
659 * Add custom block's meta fields to meta revisioning.
660 *
661 * @param array $fields
662 * @return array
663 */
664 public function add_fields_to_revision_meta_keys( $fields ) {
665 return array_merge(
666 $fields,
667 [
668 'boldblocks_variation_description',
669 'boldblocks_variation_icon',
670 'boldblocks_variation_data',
671 'boldblocks_variation_custom_style',
672 ]
673 );
674 }
675
676 /**
677 * Get style by variation name
678 *
679 * @param string $variation_name
680 * @param string $style
681 * @param array $block_variation
682 * @return string
683 */
684 private function save_variation_style( $variation_name, $style, $block_variation ) {
685 if ( ! isset( $this->variation_style_array[ $variation_name ] ) ) {
686 if ( 'core/html' === ( $block_variation['blockName'] ?? '' ) ) {
687 $style = str_replace( $block_variation['variationClass'], 'cbb-html-preview', $style );
688 }
689
690 $this->variation_style_array[ $variation_name ] = $style;
691 }
692 }
693
694 /**
695 * Enqueue scripts/styles for the editor
696 *
697 * @param array $editor_settings
698 * @return void
699 */
700 public function enqueue_style_for_the_editor( $editor_settings ) {
701 $style = '';
702 if ( $this->variation_style_array ) {
703 foreach ( $this->variation_style_array as $variation_style ) {
704 if ( $variation_style ) {
705 $style .= $variation_style;
706 }
707 }
708 }
709
710 if ( $style ) {
711 $editor_settings['styles'][] = [ 'css' => $style ];
712 }
713
714 return $editor_settings;
715 }
716
717 /**
718 * Enqueue scripts for custom variations when editing them
719 *
720 * @param string $hook_suffix
721 * @return void
722 */
723 public function enqueue_custom_variation_scripts( $hook_suffix ) {
724 global $post;
725 $screen = get_current_screen();
726 if ( 'post.php' === $hook_suffix && $this->post_type === $screen->post_type ) {
727 // Styles.
728 $custom_style_handler = $this->the_plugin_instance->get_component( CustomStyle::class );
729 $custom_style = get_post_meta( $post->ID, 'boldblocks_variation_custom_style', true );
730
731 if ( $custom_style ) {
732 $block_name = get_post_meta( $post->ID, 'boldblocks_variation_block_name', true );
733 if ( 0 === strpos( $block_name, 'core/' ) ) {
734 $block_name = str_replace( 'core/', '', $block_name );
735 }
736 $block_class = '.wp-block-post-content > .wp-block-' . str_replace( '/', '-', $block_name );
737 $custom_style = $custom_style_handler->refine_custom_value( $custom_style, [ 'selector' => $block_class ], 'CSS' );
738 $custom_style .= '.is-selected,.has-child-selected,.has-child-selected * {animation:none!important;}';
739
740 $variation_handle = 'edit-variation-style';
741 wp_register_style( $variation_handle, '' );
742 wp_add_inline_style( $variation_handle, $custom_style );
743 wp_enqueue_style( $variation_handle );
744 }
745 }
746 }
747
748 /**
749 * Get the block name for the current variation
750 *
751 * @return string
752 */
753 public function get_block_name() {
754 global $pagenow;
755
756 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
757 if ( 'post.php' === $pagenow && isset( $_GET['post'] ) ) {
758 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
759 $post_id = absint( $_GET['post'] );
760 $current_post = get_post( $post_id );
761
762 if ( ! $current_post || ! $current_post instanceof \WP_Post || $this->post_type !== $current_post->post_type ) {
763 return;
764 }
765
766 return get_post_meta( $post_id, 'boldblocks_variation_block_name', true );
767 }
768 }
769
770 /**
771 * Get max items
772 */
773 public function get_max_items() {
774 return apply_filters( 'cbb_get_max_items', $this->max_items, $this->post_type );
775 }
776 }
777 endif;
778