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