| 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 |
'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 |
] |
| 139 |
); |
| 140 |
|
| 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 |
] |
| 156 |
); |
| 157 |
|
| 158 |
$this->register_meta( |
| 159 |
'boldblocks_variation_is_transformable', |
| 160 |
[ |
| 161 |
'type' => 'boolean', |
| 162 |
'default' => false, |
| 163 |
] |
| 164 |
); |
| 165 |
|
| 166 |
$this->register_meta( |
| 167 |
'boldblocks_variation_hide_from_inserter', |
| 168 |
[ |
| 169 |
'type' => 'boolean', |
| 170 |
'default' => false, |
| 171 |
] |
| 172 |
); |
| 173 |
|
| 174 |
$this->register_meta( |
| 175 |
'boldblocks_variation_data', |
| 176 |
[ |
| 177 |
'default' => '{}', |
| 178 |
] |
| 179 |
); |
| 180 |
|
| 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 |
] |
| 194 |
); |
| 195 |
|
| 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 |
] |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 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 ); |
| 217 |
} |
| 218 |
|
| 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 |
/** |
| 274 |
* Enqueue editor assets |
| 275 |
* |
| 276 |
* @return void |
| 277 |
*/ |
| 278 |
public function enqueue_block_editor_assets() { |
| 279 |
// Variations access file. |
| 280 |
$variations_asset = $this->the_plugin_instance->include_file( 'build/variations.asset.php' ); |
| 281 |
|
| 282 |
// Scripts. |
| 283 |
wp_enqueue_script( |
| 284 |
'boldblocks-variations', |
| 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 ] |
| 289 |
); |
| 290 |
|
| 291 |
// Add translation. |
| 292 |
wp_set_script_translations( 'boldblocks-variations', 'content-blocks-builder' ); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Update template for variation |
| 297 |
* |
| 298 |
* @return void |
| 299 |
*/ |
| 300 |
public function update_template_for_variation() { |
| 301 |
global $pagenow; |
| 302 |
|
| 303 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 304 |
if ( 'post.php' === $pagenow && isset( $_GET['post'] ) ) { |
| 305 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 306 |
$post_id = absint( $_GET['post'] ); |
| 307 |
$current_post = get_post( $post_id ); |
| 308 |
|
| 309 |
if ( ! $current_post instanceof \WP_Post || $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 = [ [ $block_name ] ]; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Get all variations |
| 323 |
* |
| 324 |
* @return array |
| 325 |
*/ |
| 326 |
public function get_all_variations() { |
| 327 |
if ( ! $this->all_variations ) { |
| 328 |
$this->all_variations = $this->get_posts( $this->the_plugin_instance->is_debug_mode() ); |
| 329 |
} |
| 330 |
|
| 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(); |
| 343 |
} |
| 344 |
|
| 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; |
| 359 |
} |
| 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 |
); |
| 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 |
|
| 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 |
/** |
| 445 |
* Build a custom endpoint to create variation |
| 446 |
* |
| 447 |
* @return void |
| 448 |
*/ |
| 449 |
public function build_create_variation_endpoint() { |
| 450 |
register_rest_route( |
| 451 |
'boldblocks/v1', |
| 452 |
'/createVariation/', |
| 453 |
array( |
| 454 |
'methods' => 'POST', |
| 455 |
'callback' => [ $this, 'create_variation' ], |
| 456 |
'permission_callback' => function () { |
| 457 |
return current_user_can( 'manage_options' ); |
| 458 |
}, |
| 459 |
) |
| 460 |
); |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Create variation |
| 465 |
* |
| 466 |
* @param WP_REST_Request $request The request object. |
| 467 |
* @return void |
| 468 |
*/ |
| 469 |
public function create_variation( $request ) { |
| 470 |
$args = $request->get_params(); |
| 471 |
|
| 472 |
if ( empty( $args['title'] ) || empty( $args['content'] ) || empty( $args['meta'] ) ) { |
| 473 |
wp_send_json_error( __( 'Invalid request!', 'content-blocks-builder' ), 400 ); |
| 474 |
} |
| 475 |
|
| 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 |
} |
| 480 |
|
| 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 ); |
| 484 |
|
| 485 |
// Create a hook for new variation created. |
| 486 |
do_action( 'boldblocks_variation_created', $response->data ); |
| 487 |
|
| 488 |
if ( is_wp_error( $response ) ) { |
| 489 |
wp_send_json_error( $response ); |
| 490 |
} |
| 491 |
|
| 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 ); |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* Add custom columns to custom post type |
| 508 |
* |
| 509 |
* @param array $columns The array of columns. |
| 510 |
* @return array |
| 511 |
*/ |
| 512 |
public function add_variation_custom_columns( $columns ) { |
| 513 |
$custom_columns = array( |
| 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' ), |
| 517 |
); |
| 518 |
|
| 519 |
// Add after title column. |
| 520 |
$return = array_slice( $columns, 0, 2, true ) + $custom_columns + array_slice( $columns, 2, count( $columns ) - 2, true ); |
| 521 |
|
| 522 |
return $return; |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* Manage custom columns. |
| 527 |
* |
| 528 |
* @param string $column the column name. |
| 529 |
* @param int $post_id the post id. |
| 530 |
*/ |
| 531 |
public function manage_variation_columns( $column, $post_id ) { |
| 532 |
switch ( $column ) { |
| 533 |
case 'block_name': |
| 534 |
echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_block_name', true ) ); |
| 535 |
break; |
| 536 |
|
| 537 |
case 'variation_name': |
| 538 |
echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_name', true ) ); |
| 539 |
break; |
| 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 |
|
| 545 |
// Just break out of the switch statement for everything else. |
| 546 |
default: |
| 547 |
break; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* Make custom columns sortable. |
| 553 |
* |
| 554 |
* @param array $columns The array of columns. |
| 555 |
* @return array |
| 556 |
*/ |
| 557 |
public function variation_sortable_columns( $columns ) { |
| 558 |
$columns['block_name'] = 'block_name'; |
| 559 |
$columns['variation_name'] = 'variation_name'; |
| 560 |
|
| 561 |
return $columns; |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* Alter the main query for sorting. |
| 566 |
* |
| 567 |
* @param WP_Query $query The main query. |
| 568 |
* @return void |
| 569 |
*/ |
| 570 |
public function pre_get_posts_order_variation_custom_columns( $query ) { |
| 571 |
// Do nothing on front end side. |
| 572 |
if ( ! is_admin() ) { |
| 573 |
return; |
| 574 |
} |
| 575 |
|
| 576 |
// Only for main query with orderby parameter. |
| 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 |
|
| 584 |
$orderby = $query->get( 'orderby' ); |
| 585 |
|
| 586 |
switch ( $orderby ) { |
| 587 |
case 'block_name': |
| 588 |
// set our query's meta_key, which is used for custom fields. |
| 589 |
$query->set( 'meta_key', 'boldblocks_variation_block_name' ); |
| 590 |
$query->set( 'orderby', 'meta_value' ); |
| 591 |
break; |
| 592 |
|
| 593 |
case 'variation_name': |
| 594 |
// set our query's meta_key, which is used for custom fields. |
| 595 |
$query->set( 'meta_key', 'boldblocks_variation_name' ); |
| 596 |
$query->set( 'orderby', 'meta_value' ); |
| 597 |
break; |
| 598 |
|
| 599 |
default: |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Add filters for custom variation columns |
| 607 |
* |
| 608 |
* @return void |
| 609 |
*/ |
| 610 |
public function admin_listing_variation_page() { |
| 611 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 612 |
if ( isset( $_GET['post_type'] ) && $this->post_type === $_GET['post_type'] ) { |
| 613 |
add_filter( 'request', [ $this, 'add_custom_columns_to_query_vars' ] ); |
| 614 |
add_filter( 'restrict_manage_posts', [ $this, 'add_filter_select_control' ] ); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Add custom vars to query vars |
| 620 |
* |
| 621 |
* @param array $query_vars The array of query vars. |
| 622 |
* @return array |
| 623 |
*/ |
| 624 |
public function add_custom_columns_to_query_vars( $query_vars ) { |
| 625 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 626 |
if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) { |
| 627 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 628 |
$query_vars['meta_key'] = 'boldblocks_variation_block_name'; |
| 629 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value, WordPress.Security.NonceVerification.Recommended |
| 630 |
$query_vars['meta_value'] = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) ); |
| 631 |
} |
| 632 |
|
| 633 |
return $query_vars; |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* Add a filter select control to filter variation by block name |
| 638 |
* |
| 639 |
* @return void |
| 640 |
*/ |
| 641 |
public function add_filter_select_control() { |
| 642 |
global $wpdb; |
| 643 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 644 |
$block_names = $wpdb->get_col( 'SELECT DISTINCT meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key = "boldblocks_variation_block_name" ORDER BY meta_value' ); |
| 645 |
|
| 646 |
$boldblocks_variation_filter_block = false; |
| 647 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 648 |
if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) { |
| 649 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 650 |
$boldblocks_variation_filter_block = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) ); |
| 651 |
} |
| 652 |
?> |
| 653 |
<select name="boldblocks_variation_filter_block" id="boldblocks_variation_filter_block"> |
| 654 |
<option value=""><?php esc_html_e( 'All Blocks', 'content-blocks-builder' ); ?></option> |
| 655 |
<?php foreach ( $block_names as $block_name ) { ?> |
| 656 |
<option value="<?php echo esc_attr( $block_name ); ?>"<?php selected( $boldblocks_variation_filter_block, $block_name ); ?>><?php echo esc_attr( $block_name ); ?></option> |
| 657 |
<?php } ?> |
| 658 |
</select> |
| 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 ); |
| 780 |
} |
| 781 |
} |
| 782 |
endif; |
| 783 |
|