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

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