PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.7.5
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.7.5
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.5, at includes/variations.php

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