| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register custom variations |
| 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 |
* Store all variations |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $all_variations = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* A dummy constructor |
| 29 |
*/ |
| 30 |
public function __construct() {} |
| 31 |
|
| 32 |
/** |
| 33 |
* Run main hooks |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function run() { |
| 38 |
// Register custom post type. |
| 39 |
add_action( 'init', [ $this, 'register_custom_post_type' ], 5 ); |
| 40 |
|
| 41 |
// 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' ] ); |
| 45 |
add_action( 'pre_get_posts', [ $this, 'pre_get_posts_order_variation_custom_columns' ], 1 ); |
| 46 |
|
| 47 |
// Only do customization on 'edit.php' page for variation. |
| 48 |
add_action( 'load-edit.php', [ $this, 'admin_listing_variation_page' ] ); |
| 49 |
|
| 50 |
// Load data for js. |
| 51 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] ); |
| 52 |
|
| 53 |
// Update template on the fly for variation post type. |
| 54 |
add_action( 'admin_init', [ $this, 'update_template_for_variation' ] ); |
| 55 |
|
| 56 |
// Add rest api endpoint to create variations. |
| 57 |
add_action( 'rest_api_init', [ $this, 'build_create_variation_endpoint' ] ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Register custom post type |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
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' ), |
| 82 |
); |
| 83 |
|
| 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', |
| 106 |
); |
| 107 |
|
| 108 |
register_post_type( 'boldblocks_variation', $variation_args ); |
| 109 |
|
| 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 |
) |
| 121 |
); |
| 122 |
|
| 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 |
) |
| 133 |
); |
| 134 |
|
| 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 |
) |
| 145 |
); |
| 146 |
|
| 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 |
) |
| 157 |
); |
| 158 |
|
| 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 |
); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Enqueue editor assets |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function enqueue_block_editor_assets() { |
| 178 |
global $boldblocks_cbb; |
| 179 |
|
| 180 |
// Custom blocks access file. |
| 181 |
$variations_asset = $boldblocks_cbb->include_file( 'build/variations.asset.php' ); |
| 182 |
|
| 183 |
// Scripts. |
| 184 |
wp_enqueue_script( |
| 185 |
'boldblocks-variations', |
| 186 |
BOLDBLOCKS_CBB_URL . '/build/variations.js', |
| 187 |
$variations_asset['dependencies'], |
| 188 |
BOLDBLOCKS_CBB_VERSION, |
| 189 |
true |
| 190 |
); |
| 191 |
|
| 192 |
// Load all variations for registration. |
| 193 |
wp_localize_script( |
| 194 |
'boldblocks-variations', |
| 195 |
'BoldblocksVariations', |
| 196 |
$this->get_all_variations() |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Update template for variation |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
public function update_template_for_variation() { |
| 206 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 207 |
if ( isset( $_GET['post'] ) ) { |
| 208 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 209 |
$post_id = absint( $_GET['post'] ); |
| 210 |
$current_post = get_post( $post_id ); |
| 211 |
|
| 212 |
if ( 'boldblocks_variation' !== $current_post->post_type ) { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
$block_name = get_post_meta( $post_id, 'boldblocks_variation_block_name', true ); |
| 217 |
if ( $block_name ) { |
| 218 |
$post_type_object = get_post_type_object( 'boldblocks_variation' ); |
| 219 |
$post_type_object->template = array( |
| 220 |
array( $block_name ), |
| 221 |
); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Get all variations |
| 228 |
* |
| 229 |
* @return array |
| 230 |
*/ |
| 231 |
public function get_all_variations() { |
| 232 |
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 |
); |
| 244 |
|
| 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 |
); |
| 259 |
} |
| 260 |
|
| 261 |
return $this->all_variations; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
/** |
| 266 |
* Build a custom endpoint to create variation |
| 267 |
* |
| 268 |
* @return void |
| 269 |
*/ |
| 270 |
public function build_create_variation_endpoint() { |
| 271 |
register_rest_route( |
| 272 |
'boldblocks/v1', |
| 273 |
'/createVariation/', |
| 274 |
array( |
| 275 |
'methods' => 'POST', |
| 276 |
'callback' => [ $this, 'create_variation' ], |
| 277 |
'permission_callback' => function () { |
| 278 |
return current_user_can( 'publish_posts' ); |
| 279 |
}, |
| 280 |
) |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Create variation |
| 286 |
* |
| 287 |
* @param WP_REST_Request $request The request object. |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function create_variation( $request ) { |
| 291 |
$args = $request->get_params(); |
| 292 |
|
| 293 |
if ( empty( $args['blockName'] ) || empty( $args['variationName'] ) || empty( $args['content'] ) || empty( $args['data'] ) ) { |
| 294 |
wp_send_json_error( __( 'Invalid request!', 'boldblocks' ), 400 ); |
| 295 |
} |
| 296 |
|
| 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 |
]; |
| 311 |
|
| 312 |
// Parse JSON object from JSON.stringify. |
| 313 |
// $data = json_decode(html_entity_decode( stripslashes ($args['data'] ) ), 1);. |
| 314 |
|
| 315 |
// Insert new boldblocks_block post item. |
| 316 |
$post_id = wp_insert_post( $post_array ); |
| 317 |
|
| 318 |
// Send the error if any. |
| 319 |
if ( is_wp_error( $post_id ) ) { |
| 320 |
wp_send_json_error( $post_id, 500 ); |
| 321 |
} |
| 322 |
|
| 323 |
wp_send_json( |
| 324 |
[ |
| 325 |
'data' => __( 'The new variation has been created!', 'boldblocks' ), |
| 326 |
'success' => true, |
| 327 |
'post' => [ 'id' => $post_id ], |
| 328 |
] |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Add custom columns to custom post type |
| 334 |
* |
| 335 |
* @param array $columns The array of columns. |
| 336 |
* @return array |
| 337 |
*/ |
| 338 |
public function add_variation_custom_columns( $columns ) { |
| 339 |
$custom_columns = array( |
| 340 |
'block_name' => esc_html__( 'Block name', 'boldblocks' ), |
| 341 |
'variation_name' => esc_html__( 'Variation name', 'boldblocks' ), |
| 342 |
); |
| 343 |
|
| 344 |
// Add after title column. |
| 345 |
$return = array_slice( $columns, 0, 2, true ) + $custom_columns + array_slice( $columns, 2, count( $columns ) - 2, true ); |
| 346 |
|
| 347 |
return $return; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Manage custom columns. |
| 352 |
* |
| 353 |
* @param string $column the column name. |
| 354 |
* @param int $post_id the post id. |
| 355 |
*/ |
| 356 |
public function manage_variation_columns( $column, $post_id ) { |
| 357 |
switch ( $column ) { |
| 358 |
case 'block_name': |
| 359 |
echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_block_name', true ) ); |
| 360 |
break; |
| 361 |
|
| 362 |
case 'variation_name': |
| 363 |
echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_name', true ) ); |
| 364 |
break; |
| 365 |
|
| 366 |
// Just break out of the switch statement for everything else. |
| 367 |
default: |
| 368 |
break; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Make custom columns sortable. |
| 374 |
* |
| 375 |
* @param array $columns The array of columns. |
| 376 |
* @return array |
| 377 |
*/ |
| 378 |
public function variation_sortable_columns( $columns ) { |
| 379 |
$columns['block_name'] = 'block_name'; |
| 380 |
$columns['variation_name'] = 'variation_name'; |
| 381 |
|
| 382 |
return $columns; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Alter the main query for sorting. |
| 387 |
* |
| 388 |
* @param WP_Query $query The main query. |
| 389 |
* @return void |
| 390 |
*/ |
| 391 |
public function pre_get_posts_order_variation_custom_columns( $query ) { |
| 392 |
// Do nothing on front end side. |
| 393 |
if ( ! is_admin() ) { |
| 394 |
return; |
| 395 |
} |
| 396 |
|
| 397 |
// Only for main query with orderby parameter. |
| 398 |
if ( $query->is_main_query() && 'boldblocks_variation' === $query->post_type && $query->get( 'orderby' ) ) { |
| 399 |
$orderby = $query->get( 'orderby' ); |
| 400 |
|
| 401 |
switch ( $orderby ) { |
| 402 |
case 'block_name': |
| 403 |
// set our query's meta_key, which is used for custom fields. |
| 404 |
$query->set( 'meta_key', 'boldblocks_variation_block_name' ); |
| 405 |
break; |
| 406 |
|
| 407 |
case 'variation_name': |
| 408 |
// set our query's meta_key, which is used for custom fields. |
| 409 |
$query->set( 'meta_key', 'boldblocks_variation_name' ); |
| 410 |
break; |
| 411 |
|
| 412 |
default: |
| 413 |
break; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Add filters for custom variation columns |
| 421 |
* |
| 422 |
* @return void |
| 423 |
*/ |
| 424 |
public function admin_listing_variation_page() { |
| 425 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 426 |
if ( isset( $_GET['post_type'] ) && 'boldblocks_variation' === $_GET['post_type'] ) { |
| 427 |
add_filter( 'request', [ $this, 'add_custom_columns_to_query_vars' ] ); |
| 428 |
add_filter( 'restrict_manage_posts', [ $this, 'add_filter_select_control' ] ); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Add custom vars to query vars |
| 434 |
* |
| 435 |
* @param array $query_vars The array of query vars. |
| 436 |
* @return array |
| 437 |
*/ |
| 438 |
public function add_custom_columns_to_query_vars( $query_vars ) { |
| 439 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 440 |
if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) { |
| 441 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 442 |
$query_vars['meta_key'] = 'boldblocks_variation_block_name'; |
| 443 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value, WordPress.Security.NonceVerification.Recommended |
| 444 |
$query_vars['meta_value'] = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) ); |
| 445 |
} |
| 446 |
|
| 447 |
return $query_vars; |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* Add a filter select control to filter variation by block name |
| 452 |
* |
| 453 |
* @return void |
| 454 |
*/ |
| 455 |
public function add_filter_select_control() { |
| 456 |
global $wpdb; |
| 457 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 458 |
$block_names = $wpdb->get_col( 'SELECT DISTINCT meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key = "boldblocks_variation_block_name" ORDER BY meta_value' ); |
| 459 |
|
| 460 |
$boldblocks_variation_filter_block = false; |
| 461 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 462 |
if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) { |
| 463 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 464 |
$boldblocks_variation_filter_block = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) ); |
| 465 |
} |
| 466 |
?> |
| 467 |
<select name="boldblocks_variation_filter_block" id="boldblocks_variation_filter_block"> |
| 468 |
<option value=""><?php esc_html_e( 'All Blocks', 'boldblocks' ); ?></option> |
| 469 |
<?php foreach ( $block_names as $block_name ) { ?> |
| 470 |
<option value="<?php echo esc_attr( $block_name ); ?>"<?php selected( $boldblocks_variation_filter_block, $block_name ); ?>><?php echo esc_attr( $block_name ); ?></option> |
| 471 |
<?php } ?> |
| 472 |
</select> |
| 473 |
<?php |
| 474 |
} |
| 475 |
} |
| 476 |
endif; |
| 477 |
|