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

435 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Register custom variation
4 *
5 * @package BoldBlocks
6 * @author Phi Phan <mrphipv@gmail.com>
7 * @copyright Copyright (c) 2021, Phi Phan
8 */
9
10 namespace BoldBlocks;
11
12 // Exit if accessed directly.
13 defined( 'ABSPATH' ) || exit;
14
15 if ( ! class_exists( 'Variations' ) ) :
16 /**
17 * Manage custom block variations
18 */
19 class Variations {
20 /**
21 * A dummy constructor
22 */
23 public function __construct() {}
24
25 /**
26 * Run main hooks
27 *
28 * @return void
29 */
30 public function run() {
31 // Register custom post type.
32 add_action( 'init', [ $this, 'register_custom_post_type' ], 5 );
33
34 // Custom variation columns.
35 add_filter( 'manage_edit-boldblocks_variation_columns', [ $this, 'add_variation_custom_columns' ] );
36 add_action( 'manage_boldblocks_variation_posts_custom_column', [ $this, 'manage_variation_columns' ], 10, 2 );
37 add_filter( 'manage_edit-boldblocks_variation_sortable_columns', [ $this, 'variation_sortable_columns' ] );
38 add_action( 'pre_get_posts', [ $this, 'pre_get_posts_order_variation_custom_columns' ], 1 );
39
40 // Only do customization on 'edit.php' page for variation.
41 add_action( 'load-edit.php', [ $this, 'admin_listing_variation_page' ] );
42
43 // Load data for js.
44 add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
45
46 // Update template on the fly for variation post type.
47 add_action( 'admin_init', [ $this, 'update_template_for_variation' ] );
48
49 // Add rest api endpoint to create variations.
50 add_action( 'rest_api_init', [ $this, 'build_create_variation_endpoint' ] );
51 }
52
53 /**
54 * Register custom post type
55 *
56 * @return void
57 */
58 public function register_custom_post_type() {
59 // Block variations.
60 $variation_labels = array(
61 'name' => _x( 'Block Variations', 'post type general name', 'boldblocks' ),
62 'singular_name' => _x( 'Block Variation', 'post type singular name', 'boldblocks' ),
63 'menu_name' => _x( 'Block Variations', 'admin menu', 'boldblocks' ),
64 'name_admin_bar' => _x( 'Block Variations', 'add new on admin bar', 'boldblocks' ),
65 'add_new' => _x( 'Add New', 'item', 'boldblocks' ),
66 'add_new_item' => __( 'Add New Block Variation', 'boldblocks' ),
67 'new_item' => __( 'New Block Variation', 'boldblocks' ),
68 'edit_item' => __( 'Edit Block Variation', 'boldblocks' ),
69 'view_item' => __( 'View Block Variation', 'boldblocks' ),
70 'all_items' => __( 'All Block Variations', 'boldblocks' ),
71 'search_items' => __( 'Search Block Variations', 'boldblocks' ),
72 'parent_item_colon' => __( 'Parent Block Variation:', 'boldblocks' ),
73 'not_found' => __( 'No items found.', 'boldblocks' ),
74 'not_found_in_trash' => __( 'No items found in trash.', 'boldblocks' ),
75 );
76
77 $variation_args = array(
78 'labels' => $variation_labels,
79 'description' => __( 'All custom block variations', 'boldblocks' ),
80 'public' => true,
81 'publicly_queryable' => false,
82 'exclude_from_search' => true,
83 'show_ui' => true,
84 'show_in_menu' => true,
85 'show_in_nav_menus' => false,
86 'show_in_admin_bar' => false,
87 'rewrite' => array( 'slug' => 'variation' ),
88 'capability_type' => 'post',
89 'capabilities' => array(
90 'create_posts' => 'do_not_allow',
91 ),
92 'map_meta_cap' => true,
93 'has_archive' => false,
94 'hierarchical' => false,
95 'menu_position' => 5,
96 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'custom-fields', 'revisions' ),
97 'show_in_rest' => true,
98 'template_lock' => 'insert',
99 );
100
101 register_post_type( 'boldblocks_variation', $variation_args );
102
103 // Meta fields.
104 register_meta(
105 'post',
106 'boldblocks_variation_block_name',
107 array(
108 'object_subtype' => 'boldblocks_variation',
109 'show_in_rest' => true,
110 'type' => 'string',
111 'default' => '',
112 'single' => true,
113 )
114 );
115
116 register_meta(
117 'post',
118 'boldblocks_variation_name',
119 array(
120 'object_subtype' => 'boldblocks_variation',
121 'show_in_rest' => true,
122 'type' => 'string',
123 'default' => '',
124 'single' => true,
125 )
126 );
127
128 register_meta(
129 'post',
130 'boldblocks_variation_data',
131 array(
132 'object_subtype' => 'boldblocks_variation',
133 'show_in_rest' => true,
134 'type' => 'string',
135 'default' => '{}',
136 'single' => true,
137 )
138 );
139 }
140
141 /**
142 * Enqueue editor assets
143 *
144 * @return void
145 */
146 public function enqueue_block_editor_assets() {
147 global $boldblocks_cbb;
148
149 // Custom blocks access file.
150 $variations_asset = $boldblocks_cbb->include_file( 'build/variations.asset.php' );
151
152 // Scripts.
153 wp_enqueue_script(
154 'boldblocks-variations',
155 BOLDBLOCKS_CBB_URL . '/build/variations.js',
156 $variations_asset['dependencies'],
157 $variations_asset['version'],
158 true
159 );
160
161 // Load all variations for registration.
162 wp_localize_script(
163 'boldblocks-variations',
164 'BoldblocksVariations',
165 $this->get_all_variations()
166 );
167 }
168
169 /**
170 * Update template for variation
171 *
172 * @return void
173 */
174 public function update_template_for_variation() {
175 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
176 if ( isset( $_GET['post'] ) ) {
177 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
178 $post_id = absint( $_GET['post'] );
179 $current_post = get_post( $post_id );
180
181 if ( 'boldblocks_variation' !== $current_post->post_type ) {
182 return;
183 }
184
185 $block_name = get_post_meta( $post_id, 'boldblocks_variation_block_name', true );
186 if ( $block_name ) {
187 $post_type_object = get_post_type_object( 'boldblocks_variation' );
188 $post_type_object->template = array(
189 array( $block_name ),
190 );
191 }
192 }
193 }
194
195 /**
196 * Get all variations
197 *
198 * @return array
199 */
200 public function get_all_variations() {
201 $variation_posts = get_posts(
202 [
203 'post_type' => 'boldblocks_variation',
204 'posts_per_page' => -1,
205 'post_status' => 'publish',
206 ]
207 );
208
209 return array_map(
210 function( $item ) {
211 return [
212 'blockName' => get_post_meta( $item->ID, 'boldblocks_variation_block_name', true ),
213 'variationName' => get_post_meta( $item->ID, 'boldblocks_variation_name', true ),
214 'title' => $item->post_title,
215 'postContent' => $item->post_content,
216 'description' => $item->post_excerpt,
217 'variationData' => get_post_meta( $item->ID, 'boldblocks_variation_data', true ),
218 ];
219 },
220 $variation_posts
221 );
222 }
223
224
225 /**
226 * Build a custom endpoint to create variation
227 *
228 * @return void
229 */
230 public function build_create_variation_endpoint() {
231 register_rest_route(
232 'boldblocks/v1',
233 '/createVariation/',
234 array(
235 'methods' => 'POST',
236 'callback' => [ $this, 'create_variation' ],
237 'permission_callback' => function () {
238 return current_user_can( 'publish_posts' );
239 },
240 )
241 );
242 }
243
244 /**
245 * Create variation
246 *
247 * @param WP_REST_Request $request The request object.
248 * @return void
249 */
250 public function create_variation( $request ) {
251 $args = $request->get_params();
252
253 if ( empty( $args['blockName'] ) || empty( $args['variationName'] ) || empty( $args['content'] ) || empty( $args['data'] ) ) {
254 wp_send_json_error( __( 'Invalid request!', 'boldblocks' ), 400 );
255 }
256
257 // Build post array.
258 $post_array = [
259 'post_type' => 'boldblocks_variation',
260 'post_title' => $args['title'] ?? __( 'No Title', 'boldblocks' ),
261 'post_content' => $args['content'] ?? '',
262 'post_status' => 'publish',
263 'meta_input' => [
264 'boldblocks_variation_block_name' => $args['blockName'] ?? '',
265 'boldblocks_variation_name' => $args['variationName'] ?? '',
266 'boldblocks_variation_data' => $args['data'],
267 ],
268 ];
269
270 // Parse JSON object from JSON.stringify.
271 // $data = json_decode(html_entity_decode( stripslashes ($args['data'] ) ), 1);.
272
273 // Insert new boldblocks_block post item.
274 $post_id = wp_insert_post( $post_array );
275
276 // Send the error if any.
277 if ( is_wp_error( $post_id ) ) {
278 wp_send_json_error( $post_id, 500 );
279 }
280
281 wp_send_json(
282 [
283 'data' => __( 'The new variation has been created!', 'boldblocks' ),
284 'success' => true,
285 'post' => [ 'id' => $post_id ],
286 ]
287 );
288 }
289
290 /**
291 * Add custom columns to custom post type
292 *
293 * @param array $columns The array of columns.
294 * @return array
295 */
296 public function add_variation_custom_columns( $columns ) {
297 $custom_columns = array(
298 'block_name' => esc_html__( 'Block name', 'boldblocks' ),
299 'variation_name' => esc_html__( 'Variation name', 'boldblocks' ),
300 );
301
302 // Add after title column.
303 $return = array_slice( $columns, 0, 2, true ) + $custom_columns + array_slice( $columns, 2, count( $columns ) - 2, true );
304
305 return $return;
306 }
307
308 /**
309 * Manage custom columns.
310 *
311 * @param string $column the column name.
312 * @param int $post_id the post id.
313 */
314 public function manage_variation_columns( $column, $post_id ) {
315 switch ( $column ) {
316 case 'block_name':
317 echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_block_name', true ) );
318 break;
319
320 case 'variation_name':
321 echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_name', true ) );
322 break;
323
324 // Just break out of the switch statement for everything else.
325 default:
326 break;
327 }
328 }
329
330 /**
331 * Make custom columns sortable.
332 *
333 * @param array $columns The array of columns.
334 * @return array
335 */
336 public function variation_sortable_columns( $columns ) {
337 $columns['block_name'] = 'block_name';
338 $columns['variation_name'] = 'variation_name';
339
340 return $columns;
341 }
342
343 /**
344 * Alter the main query for sorting.
345 *
346 * @param WP_Query $query The main query.
347 * @return void
348 */
349 public function pre_get_posts_order_variation_custom_columns( $query ) {
350 // Do nothing on front end side.
351 if ( ! is_admin() ) {
352 return;
353 }
354
355 // Only for main query with orderby parameter.
356 if ( $query->is_main_query() && 'boldblocks_variation' === $query->post_type && $query->get( 'orderby' ) ) {
357 $orderby = $query->get( 'orderby' );
358
359 switch ( $orderby ) {
360 case 'block_name':
361 // set our query's meta_key, which is used for custom fields.
362 $query->set( 'meta_key', 'boldblocks_variation_block_name' );
363 break;
364
365 case 'variation_name':
366 // set our query's meta_key, which is used for custom fields.
367 $query->set( 'meta_key', 'boldblocks_variation_name' );
368 break;
369
370 default:
371 break;
372 }
373 }
374
375 }
376
377 /**
378 * Add filters for custom variation columns
379 *
380 * @return void
381 */
382 public function admin_listing_variation_page() {
383 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
384 if ( isset( $_GET['post_type'] ) && 'boldblocks_variation' === $_GET['post_type'] ) {
385 add_filter( 'request', [ $this, 'add_custom_columns_to_query_vars' ] );
386 add_filter( 'restrict_manage_posts', [ $this, 'add_filter_select_control' ] );
387 }
388 }
389
390 /**
391 * Add custom vars to query vars
392 *
393 * @param array $query_vars The array of query vars.
394 * @return array
395 */
396 public function add_custom_columns_to_query_vars( $query_vars ) {
397 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
398 if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) {
399 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
400 $query_vars['meta_key'] = 'boldblocks_variation_block_name';
401 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value, WordPress.Security.NonceVerification.Recommended
402 $query_vars['meta_value'] = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) );
403 }
404
405 return $query_vars;
406 }
407
408 /**
409 * Add a filter select control to filter variation by block name
410 *
411 * @return void
412 */
413 public function add_filter_select_control() {
414 global $wpdb;
415 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
416 $block_names = $wpdb->get_col( 'SELECT DISTINCT meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key = "boldblocks_variation_block_name" ORDER BY meta_value' );
417
418 $boldblocks_variation_filter_block = false;
419 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
420 if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) {
421 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
422 $boldblocks_variation_filter_block = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) );
423 }
424 ?>
425 <select name="boldblocks_variation_filter_block" id="boldblocks_variation_filter_block">
426 <option value=""><?php esc_html_e( 'All Blocks', 'boldblocks' ); ?></option>
427 <?php foreach ( $block_names as $block_name ) { ?>
428 <option value="<?php echo esc_attr( $block_name ); ?>"<?php selected( $boldblocks_variation_filter_block, $block_name ); ?>><?php echo esc_attr( $block_name ); ?></option>
429 <?php } ?>
430 </select>
431 <?php
432 }
433 }
434 endif;
435