| 1 |
<?php |
| 2 |
/** |
| 3 |
* Library |
| 4 |
* |
| 5 |
* @package BoldBlocks |
| 6 |
* @author Phi Phan <mrphipv@gmail.com> |
| 7 |
* @copyright Copyright (c) 2023, Phi Phan |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace BoldBlocks; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
if ( ! class_exists( Library::class ) ) : |
| 16 |
/** |
| 17 |
* Create/edit custom content blocks. |
| 18 |
*/ |
| 19 |
class Library extends CoreComponent { |
| 20 |
/** |
| 21 |
* The library url |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $library_url = 'https://boldpatterns.net'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the library url |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
public function get_library_url() { |
| 33 |
if ( defined( 'CBB_LIBRARY_URL' ) ) { |
| 34 |
return CBB_LIBRARY_URL; |
| 35 |
} |
| 36 |
|
| 37 |
return $this->library_url; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Run main hooks |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function run() { |
| 46 |
// Create the block library page. |
| 47 |
add_action( 'admin_menu', [ $this, 'admin_menu_add_block_library_page' ] ); |
| 48 |
|
| 49 |
// Add the header of the block library page. |
| 50 |
add_action( 'in_admin_header', [ $this, 'in_admin_header_block_library' ] ); |
| 51 |
|
| 52 |
// Create the variation library page. |
| 53 |
add_action( 'admin_menu', [ $this, 'admin_menu_add_variation_library_page' ] ); |
| 54 |
|
| 55 |
// Add the header of the variation library page. |
| 56 |
add_action( 'in_admin_header', [ $this, 'in_admin_header_variation_library' ] ); |
| 57 |
|
| 58 |
// Clear the blocks cache on upgraded. |
| 59 |
add_action( 'cbb/version_upgraded', [ $this, 'clear_library_cache' ] ); |
| 60 |
|
| 61 |
// Enqueue block library scripts. |
| 62 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_block_library_scripts' ] ); |
| 63 |
|
| 64 |
// Add rest api endpoint to query all blocks. |
| 65 |
add_action( 'rest_api_init', [ $this, 'register_block_library_get_blocks_endpoint' ] ); |
| 66 |
|
| 67 |
// Add rest api endpoint to query all block keywords. |
| 68 |
add_action( 'rest_api_init', [ $this, 'register_get_block_keywords_from_library_endpoint' ] ); |
| 69 |
|
| 70 |
// Enqueue variation library scripts. |
| 71 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_variation_library_scripts' ] ); |
| 72 |
|
| 73 |
// Add rest api endpoint to query all variations. |
| 74 |
add_action( 'rest_api_init', [ $this, 'register_variation_library_get_variations_endpoint' ] ); |
| 75 |
|
| 76 |
// Add rest api endpoint to query all variation keywords. |
| 77 |
add_action( 'rest_api_init', [ $this, 'register_get_variation_keywords_from_library_endpoint' ] ); |
| 78 |
|
| 79 |
// Enqueue pattern library scripts. |
| 80 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_pattern_library_scripts' ] ); |
| 81 |
|
| 82 |
// Add rest api endpoint to query all patterns. |
| 83 |
add_action( 'rest_api_init', [ $this, 'register_pattern_library_get_patterns_endpoint' ] ); |
| 84 |
|
| 85 |
// Add rest api endpoint to query all pattern keywords. |
| 86 |
add_action( 'rest_api_init', [ $this, 'register_get_pattern_keywords_from_library_endpoint' ] ); |
| 87 |
|
| 88 |
// Change the parameter for the rest api. |
| 89 |
add_filter( 'rest_boldblocks_block_collection_params', [ $this, 'rest_library_item_collection_params' ], 10, 2 ); |
| 90 |
add_filter( 'rest_boldblocks_variation_collection_params', [ $this, 'rest_library_item_collection_params' ], 10, 2 ); |
| 91 |
add_filter( 'rest_boldblocks_pattern_collection_params', [ $this, 'rest_library_item_collection_params' ], 10, 2 ); |
| 92 |
|
| 93 |
// Load all max items. |
| 94 |
add_filter( 'rest_boldblocks_block_query', [ $this, 'rest_library_item_query' ], 10, 2 ); |
| 95 |
add_filter( 'rest_boldblocks_variation_query', [ $this, 'rest_library_item_query' ], 10, 2 ); |
| 96 |
add_filter( 'rest_boldblocks_pattern_query', [ $this, 'rest_library_item_query' ], 10, 2 ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Create the admin page for the block library |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function admin_menu_add_block_library_page() { |
| 105 |
// Make "Custom Blocks" as parent slug. |
| 106 |
$parent_slug = 'edit.php?post_type=boldblocks_block'; |
| 107 |
|
| 108 |
add_submenu_page( |
| 109 |
$parent_slug, |
| 110 |
__( 'Block Library', 'content-blocks-builder' ), |
| 111 |
__( 'Block Library', 'content-blocks-builder' ), |
| 112 |
'manage_options', |
| 113 |
'cbb-block-library', |
| 114 |
function () { |
| 115 |
?> |
| 116 |
<div class="wrap"> |
| 117 |
<h2 class="screen-reader-text">Block Library</h2> |
| 118 |
<div class="boldblocks-settings js-boldblocks-settings-root"></div> |
| 119 |
</div> |
| 120 |
<?php |
| 121 |
}, |
| 122 |
2 |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Print the header of the block library page. |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function in_admin_header_block_library() { |
| 132 |
$screen = get_current_screen(); |
| 133 |
if ( 'boldblocks_block_page_cbb-block-library' === $screen->base ) { |
| 134 |
?> |
| 135 |
<div class="cbb-settings-header"> |
| 136 |
<h1><strong><?php esc_html_e( 'Block Library', 'content-blocks-builder' ); ?></strong></h1> |
| 137 |
</div> |
| 138 |
<?php |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Create the admin page for the variation libary |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function admin_menu_add_variation_library_page() { |
| 148 |
// Make "Custom Blocks" as parent slug. |
| 149 |
$parent_slug = 'edit.php?post_type=boldblocks_block'; |
| 150 |
|
| 151 |
add_submenu_page( |
| 152 |
$parent_slug, |
| 153 |
__( 'Variation Library', 'content-blocks-builder' ), |
| 154 |
__( 'Variation Library', 'content-blocks-builder' ), |
| 155 |
'manage_options', |
| 156 |
'cbb-variation-library', |
| 157 |
function () { |
| 158 |
?> |
| 159 |
<div class="wrap"> |
| 160 |
<h2 class="screen-reader-text">Variation Library</h2> |
| 161 |
<div class="boldblocks-settings js-boldblocks-settings-root"></div> |
| 162 |
</div> |
| 163 |
<?php |
| 164 |
}, |
| 165 |
4 |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Print the header of the variation library page. |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function in_admin_header_variation_library() { |
| 175 |
$screen = get_current_screen(); |
| 176 |
if ( 'boldblocks_block_page_cbb-variation-library' === $screen->base ) { |
| 177 |
?> |
| 178 |
<div class="cbb-settings-header"> |
| 179 |
<h1><strong><?php esc_html_e( 'Variation Library', 'content-blocks-builder' ); ?></strong></h1> |
| 180 |
</div> |
| 181 |
<?php |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Update library cache. |
| 187 |
*/ |
| 188 |
private function update_library_cache( $data, $cache_key, $expired_time = DAY_IN_SECONDS ) { |
| 189 |
set_transient( $cache_key, $data, $expired_time ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Clear library cache |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
public function clear_library_cache() { |
| 198 |
delete_transient( $this->get_library_block_cache_key() ); |
| 199 |
delete_transient( $this->get_library_block_keywords_cache_key() ); |
| 200 |
|
| 201 |
delete_transient( $this->get_library_variation_cache_key() ); |
| 202 |
delete_transient( $this->get_library_variation_keywords_cache_key() ); |
| 203 |
|
| 204 |
delete_transient( $this->get_library_pattern_cache_key() ); |
| 205 |
delete_transient( $this->get_library_pattern_keywords_cache_key() ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Define blocks cache key |
| 210 |
* |
| 211 |
* @return string |
| 212 |
*/ |
| 213 |
private function get_library_block_cache_key() { |
| 214 |
return 'bb_blocks_store'; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Define block keywords cache key |
| 219 |
* |
| 220 |
* @return string |
| 221 |
*/ |
| 222 |
private function get_library_block_keywords_cache_key() { |
| 223 |
return 'bb_block_keywords_store'; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Enqueue scripts for the block library page. |
| 228 |
* |
| 229 |
* @param string $hook_suffix |
| 230 |
* @return void |
| 231 |
*/ |
| 232 |
public function enqueue_block_library_scripts( $hook_suffix ) { |
| 233 |
// Only load scripts for the block library page. |
| 234 |
if ( 'boldblocks_block_page_cbb-block-library' === $hook_suffix ) { |
| 235 |
$custom_blocks_handle = $this->the_plugin_instance->get_component( CustomBlocks::class )->custom_blocks_handle; |
| 236 |
$block_registry = \WP_Block_Type_Registry::get_instance(); |
| 237 |
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { |
| 238 |
// Front-end and editor scripts. |
| 239 |
foreach ( $block_type->script_handles as $script_handle ) { |
| 240 |
wp_enqueue_script( $script_handle ); |
| 241 |
} |
| 242 |
|
| 243 |
// Editor scripts. |
| 244 |
foreach ( $block_type->editor_script_handles as $editor_script_handle ) { |
| 245 |
wp_enqueue_script( $editor_script_handle ); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Asset files. |
| 250 |
$block_library_asset = $this->the_plugin_instance->include_file( 'build/block-library.asset.php' ); |
| 251 |
$settings_asset = $this->the_plugin_instance->include_file( 'build/settings.asset.php' ); |
| 252 |
|
| 253 |
// Register custom blocks. |
| 254 |
$this->the_plugin_instance->get_component( CustomBlocks::class )->register_scripts(); |
| 255 |
wp_enqueue_script( $custom_blocks_handle ); |
| 256 |
|
| 257 |
$block_library_handle = 'boldblocks-block-library'; |
| 258 |
|
| 259 |
// Enqueue scripts. |
| 260 |
wp_enqueue_script( |
| 261 |
$block_library_handle, |
| 262 |
$this->the_plugin_instance->get_file_uri( 'build/block-library.js' ), |
| 263 |
array_merge( $block_library_asset['dependencies'] ?? [], [ $custom_blocks_handle ] ), |
| 264 |
$this->the_plugin_instance->get_script_version( $block_library_asset ), |
| 265 |
[ 'in_footer' => true ] |
| 266 |
); |
| 267 |
|
| 268 |
// For debuging. |
| 269 |
$this->the_plugin_instance->enqueue_debug_information( $block_library_handle ); |
| 270 |
|
| 271 |
// Load block library url. |
| 272 |
wp_add_inline_script( $block_library_handle, 'var CBBBlockLibrary=' . wp_json_encode( [ 'URL' => $this->get_library_url() ] ), 'before' ); |
| 273 |
|
| 274 |
// Enqueue style. |
| 275 |
wp_enqueue_style( |
| 276 |
'boldblocks-settings', |
| 277 |
$this->the_plugin_instance->get_file_uri( 'build/settings.css' ), |
| 278 |
[], |
| 279 |
$this->the_plugin_instance->get_script_version( $settings_asset ) |
| 280 |
); |
| 281 |
|
| 282 |
// Load components style. |
| 283 |
wp_enqueue_style( 'wp-components' ); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Register an endpoint to query blocks. |
| 289 |
* |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
public function register_block_library_get_blocks_endpoint() { |
| 293 |
register_rest_route( |
| 294 |
'boldblocks/v1', |
| 295 |
'/getBlocks/', |
| 296 |
array( |
| 297 |
'methods' => 'GET', |
| 298 |
'callback' => [ $this, 'get_blocks_from_library' ], |
| 299 |
'permission_callback' => '__return_true', |
| 300 |
) |
| 301 |
); |
| 302 |
|
| 303 |
register_rest_route( |
| 304 |
'boldblocks/v1', |
| 305 |
'/getFullBlockData/', |
| 306 |
array( |
| 307 |
'methods' => 'GET', |
| 308 |
'callback' => [ $this, 'get_full_block_library_data' ], |
| 309 |
'permission_callback' => '__return_true', |
| 310 |
) |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Query blocks. |
| 316 |
* |
| 317 |
* @param WP_Rest_Request $request |
| 318 |
* @return WP_Rest_Response |
| 319 |
*/ |
| 320 |
public function get_blocks_from_library( $request ) { |
| 321 |
// Get all blocks. |
| 322 |
$data = $this->get_all_blocks_from_library(); |
| 323 |
|
| 324 |
if ( is_array( $data ) ) { |
| 325 |
return wp_send_json( array_values( $data ) ); |
| 326 |
} |
| 327 |
|
| 328 |
return wp_send_json( $data ); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Build blocks data |
| 333 |
* |
| 334 |
* @return array |
| 335 |
*/ |
| 336 |
private function get_all_blocks_from_library() { |
| 337 |
// Get the WP Version global. |
| 338 |
global $wp_version; |
| 339 |
$cache_key = $this->get_library_block_cache_key(); |
| 340 |
$data = get_transient( $cache_key ); |
| 341 |
|
| 342 |
if ( $this->is_force_refresh() || false === $data ) { |
| 343 |
$response = wp_remote_get( |
| 344 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks-blocks', |
| 345 |
[ |
| 346 |
'timeout' => 120, |
| 347 |
'sslverify' => false, |
| 348 |
'body' => [ |
| 349 |
'_load_all' => 1, |
| 350 |
'_fields' => |
| 351 |
'id,slug,meta,menu_order,order,title,description,keywords,thumbnail,boldblocks_block_keywords,keywordIds,is_pro,has_pro_features', |
| 352 |
'api_version' => 2, |
| 353 |
'cbb_version' => $this->the_plugin_instance->version, |
| 354 |
'wp_version' => $wp_version, |
| 355 |
], |
| 356 |
] |
| 357 |
); |
| 358 |
|
| 359 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 360 |
|
| 361 |
if ( 200 === absint( $response_code ) ) { |
| 362 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 363 |
if ( $data ) { |
| 364 |
// Re-index the data. |
| 365 |
$data = array_column( $data, null, 'id' ); |
| 366 |
|
| 367 |
// Update cache. |
| 368 |
$this->update_library_cache( $data, $cache_key ); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
return $data; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Get full data for blocks |
| 378 |
* |
| 379 |
* @param WP_Rest_Request $request |
| 380 |
* @return array |
| 381 |
*/ |
| 382 |
public function get_full_block_library_data( $request ) { |
| 383 |
// Return array. |
| 384 |
$data = []; |
| 385 |
|
| 386 |
// Get all blocks. |
| 387 |
$all_blocks = $this->get_all_blocks_from_library(); |
| 388 |
|
| 389 |
$block_ids = $request->get_param( 'blockIds' ); |
| 390 |
|
| 391 |
$response = wp_remote_get( |
| 392 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks-blocks', |
| 393 |
[ |
| 394 |
'timeout' => 120, |
| 395 |
'sslverify' => false, |
| 396 |
'body' => [ |
| 397 |
'_fields' => |
| 398 |
'id,slug,meta,menu_order,order,title,content,description,keywords,thumbnail,libraryBlocks,libraryVariations,variations,parentVariations,boldblocks_block_keywords,keywordIds,is_pro,has_pro_features', |
| 399 |
'api_version' => 2, |
| 400 |
'include_data' => true, |
| 401 |
'include' => $block_ids, |
| 402 |
], |
| 403 |
] |
| 404 |
); |
| 405 |
|
| 406 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 407 |
|
| 408 |
if ( 200 === absint( $response_code ) ) { |
| 409 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 410 |
|
| 411 |
if ( $data ) { |
| 412 |
// Update cache. |
| 413 |
foreach ( $data as $post ) { |
| 414 |
$all_blocks[ $post['id'] ] = $post; |
| 415 |
} |
| 416 |
|
| 417 |
// Update cache. |
| 418 |
$this->update_library_cache( $all_blocks, $this->get_library_block_cache_key() ); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
return $data; |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Register an endpoint to query block keywords. |
| 427 |
* |
| 428 |
* @return array |
| 429 |
*/ |
| 430 |
public function register_get_block_keywords_from_library_endpoint() { |
| 431 |
register_rest_route( |
| 432 |
'boldblocks/v1', |
| 433 |
'/getBlockKeywords/', |
| 434 |
array( |
| 435 |
'methods' => 'GET', |
| 436 |
'callback' => [ $this, 'get_block_keywords_from_library' ], |
| 437 |
'permission_callback' => '__return_true', |
| 438 |
) |
| 439 |
); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Query block keywords. |
| 444 |
* |
| 445 |
* @param WP_Rest_Request $request |
| 446 |
* @return WP_Rest_Response |
| 447 |
*/ |
| 448 |
public function get_block_keywords_from_library( $request ) { |
| 449 |
$cache_key = $this->get_library_block_keywords_cache_key(); |
| 450 |
$data = get_transient( $cache_key ); |
| 451 |
|
| 452 |
if ( false === $data ) { |
| 453 |
$response = wp_remote_get( |
| 454 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks_block_keywords', |
| 455 |
[ |
| 456 |
'timeout' => 120, |
| 457 |
'sslverify' => false, |
| 458 |
'body' => [ |
| 459 |
'per_page' => 100, |
| 460 |
'orderby' => 'order', |
| 461 |
'order' => 'desc', |
| 462 |
'hide_empty' => 1, |
| 463 |
'_fields' => 'id,count,name,slug', |
| 464 |
], |
| 465 |
] |
| 466 |
); |
| 467 |
|
| 468 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 469 |
|
| 470 |
if ( 200 === absint( $response_code ) ) { |
| 471 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 472 |
|
| 473 |
if ( ! empty( $data ) ) { |
| 474 |
set_transient( $cache_key, $data, 7 * DAY_IN_SECONDS ); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
return wp_send_json( $data ); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Define blocks cache key |
| 484 |
* |
| 485 |
* @return string |
| 486 |
*/ |
| 487 |
private function get_library_variation_cache_key() { |
| 488 |
return 'bb_variation_store'; |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Define blocks cache key |
| 493 |
* |
| 494 |
* @return string |
| 495 |
*/ |
| 496 |
private function get_library_variation_keywords_cache_key() { |
| 497 |
return 'bb_variation_keywords_store'; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Enqueue scripts for the variation library page. |
| 502 |
* |
| 503 |
* @param string $hook_suffix |
| 504 |
* @return void |
| 505 |
*/ |
| 506 |
public function enqueue_variation_library_scripts( $hook_suffix ) { |
| 507 |
// Only load scripts for the variation library page. |
| 508 |
if ( 'boldblocks_block_page_cbb-variation-library' === $hook_suffix ) { |
| 509 |
$custom_blocks_handle = $this->the_plugin_instance->get_component( CustomBlocks::class )->custom_blocks_handle; |
| 510 |
$block_registry = \WP_Block_Type_Registry::get_instance(); |
| 511 |
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { |
| 512 |
// Front-end and editor scripts. |
| 513 |
foreach ( $block_type->script_handles as $script_handle ) { |
| 514 |
wp_enqueue_script( $script_handle ); |
| 515 |
} |
| 516 |
|
| 517 |
// Editor scripts. |
| 518 |
foreach ( $block_type->editor_script_handles as $editor_script_handle ) { |
| 519 |
wp_enqueue_script( $editor_script_handle ); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
// Asset files. |
| 524 |
$variation_library_asset = $this->the_plugin_instance->include_file( 'build/variation-library.asset.php' ); |
| 525 |
$settings_asset = $this->the_plugin_instance->include_file( 'build/settings.asset.php' ); |
| 526 |
|
| 527 |
// Register custom blocks. |
| 528 |
$blocks_component = $this->the_plugin_instance->get_component( CustomBlocks::class ); |
| 529 |
$blocks_component->register_scripts(); |
| 530 |
wp_enqueue_script( $custom_blocks_handle ); |
| 531 |
|
| 532 |
$variation_library_handle = 'boldblocks-variation-library'; |
| 533 |
|
| 534 |
// Enqueue scripts. |
| 535 |
wp_enqueue_script( |
| 536 |
$variation_library_handle, |
| 537 |
$this->the_plugin_instance->get_file_uri( 'build/variation-library.js' ), |
| 538 |
array_merge( $variation_library_asset['dependencies'] ?? [], [ $custom_blocks_handle ] ), |
| 539 |
$this->the_plugin_instance->get_script_version( $variation_library_asset ), |
| 540 |
[ 'in_footer' => true ] |
| 541 |
); |
| 542 |
|
| 543 |
// For debuging. |
| 544 |
$this->the_plugin_instance->enqueue_debug_information( $variation_library_handle ); |
| 545 |
|
| 546 |
// Load block library url. |
| 547 |
wp_add_inline_script( $variation_library_handle, 'var CBBVariationLibrary=' . wp_json_encode( [ 'URL' => $this->get_library_url() ] ), 'before' ); |
| 548 |
|
| 549 |
// Enqueue style. |
| 550 |
wp_enqueue_style( |
| 551 |
'boldblocks-settings', |
| 552 |
$this->the_plugin_instance->get_file_uri( 'build/settings.css' ), |
| 553 |
[], |
| 554 |
$this->the_plugin_instance->get_script_version( $settings_asset ) |
| 555 |
); |
| 556 |
|
| 557 |
// Load components style. |
| 558 |
wp_enqueue_style( 'wp-components' ); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Register an endpoint to query variations. |
| 564 |
* |
| 565 |
* @return array |
| 566 |
*/ |
| 567 |
public function register_variation_library_get_variations_endpoint() { |
| 568 |
register_rest_route( |
| 569 |
'boldblocks/v1', |
| 570 |
'/getVariations/', |
| 571 |
array( |
| 572 |
'methods' => 'GET', |
| 573 |
'callback' => [ $this, 'get_variations_from_library' ], |
| 574 |
'permission_callback' => '__return_true', |
| 575 |
) |
| 576 |
); |
| 577 |
|
| 578 |
register_rest_route( |
| 579 |
'boldblocks/v1', |
| 580 |
'/getFullVariationData/', |
| 581 |
array( |
| 582 |
'methods' => 'GET', |
| 583 |
'callback' => [ $this, 'get_full_variation_library_data' ], |
| 584 |
'permission_callback' => '__return_true', |
| 585 |
) |
| 586 |
); |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Query variations. |
| 591 |
* |
| 592 |
* @param WP_Rest_Request $request |
| 593 |
* @return WP_Rest_Response |
| 594 |
*/ |
| 595 |
public function get_variations_from_library( $request ) { |
| 596 |
// Get all variations. |
| 597 |
$data = $this->get_all_variations_from_library(); |
| 598 |
|
| 599 |
if ( is_array( $data ) ) { |
| 600 |
return wp_send_json( array_values( $data ) ); |
| 601 |
} |
| 602 |
|
| 603 |
return wp_send_json( $data ); |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
* Build variations data |
| 608 |
* |
| 609 |
* @return array |
| 610 |
*/ |
| 611 |
private function get_all_variations_from_library() { |
| 612 |
// Get the WP Version global. |
| 613 |
global $wp_version; |
| 614 |
$cache_key = $this->get_library_variation_cache_key(); |
| 615 |
$data = get_transient( $cache_key ); |
| 616 |
|
| 617 |
if ( $this->is_force_refresh() || false === $data ) { |
| 618 |
$response = wp_remote_get( |
| 619 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks-variations', |
| 620 |
[ |
| 621 |
'timeout' => 120, |
| 622 |
'sslverify' => false, |
| 623 |
'body' => [ |
| 624 |
'_load_all' => 1, |
| 625 |
'_fields' => |
| 626 |
'id,slug,meta,menu_order,order,title,description,keywords,thumbnail,boldblocks_variation_keywords,keywordIds,is_pro,has_pro_features', |
| 627 |
'api_version' => 2, |
| 628 |
'cbb_version' => $this->the_plugin_instance->version, |
| 629 |
'wp_version' => $wp_version, |
| 630 |
], |
| 631 |
] |
| 632 |
); |
| 633 |
|
| 634 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 635 |
|
| 636 |
if ( 200 === absint( $response_code ) ) { |
| 637 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 638 |
if ( $data ) { |
| 639 |
// Re-index the data. |
| 640 |
$data = array_column( $data, null, 'id' ); |
| 641 |
|
| 642 |
// Update cache. |
| 643 |
$this->update_library_cache( $data, $cache_key ); |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
return $data; |
| 649 |
} |
| 650 |
|
| 651 |
/** |
| 652 |
* Get full data for variations |
| 653 |
* |
| 654 |
* @param WP_Rest_Request $request |
| 655 |
* @return array |
| 656 |
*/ |
| 657 |
public function get_full_variation_library_data( $request ) { |
| 658 |
// Return array. |
| 659 |
$data = []; |
| 660 |
|
| 661 |
// Get all variations. |
| 662 |
$all_variations = $this->get_all_variations_from_library(); |
| 663 |
|
| 664 |
$variation_ids = $request->get_param( 'variationIds' ); |
| 665 |
|
| 666 |
$response = wp_remote_get( |
| 667 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks-variations', |
| 668 |
[ |
| 669 |
'timeout' => 120, |
| 670 |
'sslverify' => false, |
| 671 |
'body' => [ |
| 672 |
'_fields' => |
| 673 |
'id,slug,meta,menu_order,order,title,content,description,keywords,thumbnail,boldblocks_variation_keywords,keywordIds,is_pro,has_pro_features,libraryBlocks,libraryVariations', |
| 674 |
'api_version' => 2, |
| 675 |
'include_data' => true, |
| 676 |
'include' => $variation_ids, |
| 677 |
], |
| 678 |
] |
| 679 |
); |
| 680 |
|
| 681 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 682 |
|
| 683 |
if ( 200 === absint( $response_code ) ) { |
| 684 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 685 |
|
| 686 |
if ( $data ) { |
| 687 |
// Update cache. |
| 688 |
foreach ( $data as $post ) { |
| 689 |
$all_variations[ $post['id'] ] = $post; |
| 690 |
} |
| 691 |
|
| 692 |
// Update cache. |
| 693 |
$this->update_library_cache( $all_variations, $this->get_library_variation_cache_key() ); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
return $data; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* Register an endpoint to query variation keywords. |
| 702 |
* |
| 703 |
* @return array |
| 704 |
*/ |
| 705 |
public function register_get_variation_keywords_from_library_endpoint() { |
| 706 |
register_rest_route( |
| 707 |
'boldblocks/v1', |
| 708 |
'/getVariationKeywords/', |
| 709 |
array( |
| 710 |
'methods' => 'GET', |
| 711 |
'callback' => [ $this, 'get_variation_keywords_from_library' ], |
| 712 |
'permission_callback' => '__return_true', |
| 713 |
) |
| 714 |
); |
| 715 |
} |
| 716 |
|
| 717 |
/** |
| 718 |
* Query variation keywords. |
| 719 |
* |
| 720 |
* @param WP_Rest_Request $request |
| 721 |
* @return WP_Rest_Response |
| 722 |
*/ |
| 723 |
public function get_variation_keywords_from_library( $request ) { |
| 724 |
$cache_key = $this->get_library_variation_keywords_cache_key(); |
| 725 |
$data = get_transient( $cache_key ); |
| 726 |
|
| 727 |
if ( false === $data ) { |
| 728 |
$response = wp_remote_get( |
| 729 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks_variation_keywords', |
| 730 |
[ |
| 731 |
'timeout' => 120, |
| 732 |
'sslverify' => false, |
| 733 |
'body' => [ |
| 734 |
'per_page' => 100, |
| 735 |
'orderby' => 'order', |
| 736 |
'order' => 'desc', |
| 737 |
'hide_empty' => 1, |
| 738 |
'_fields' => 'id,count,name,slug', |
| 739 |
], |
| 740 |
] |
| 741 |
); |
| 742 |
|
| 743 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 744 |
|
| 745 |
if ( 200 === absint( $response_code ) ) { |
| 746 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 747 |
|
| 748 |
if ( ! empty( $data ) ) { |
| 749 |
set_transient( $cache_key, $data, 7 * DAY_IN_SECONDS ); |
| 750 |
} |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
return wp_send_json( $data ); |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Define patterns cache key |
| 759 |
* |
| 760 |
* @return string |
| 761 |
*/ |
| 762 |
private function get_library_pattern_cache_key() { |
| 763 |
return 'bb_patterns_store'; |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* Define pattern keywords cache key |
| 768 |
* |
| 769 |
* @return string |
| 770 |
*/ |
| 771 |
private function get_library_pattern_keywords_cache_key() { |
| 772 |
return 'bb_pattern_keywords_store'; |
| 773 |
} |
| 774 |
|
| 775 |
/** |
| 776 |
* Enqueue scripts for patterns from the library. |
| 777 |
* |
| 778 |
* @return void |
| 779 |
*/ |
| 780 |
public function enqueue_pattern_library_scripts() { |
| 781 |
$screen = get_current_screen(); |
| 782 |
|
| 783 |
// Bail if we're editing blocks or variations. |
| 784 |
if ( $screen && in_array( $screen->post_type, [ 'boldblocks_block', 'boldblocks_variation' ], true ) ) { |
| 785 |
return; |
| 786 |
} |
| 787 |
|
| 788 |
// Don't show the pattern inserter. |
| 789 |
if ( ! apply_filters( 'cbb_has_pattern_inserter', true ) ) { |
| 790 |
return; |
| 791 |
} |
| 792 |
|
| 793 |
// The handle. |
| 794 |
$pattern_library_handle = 'boldblocks-pattern-library'; |
| 795 |
|
| 796 |
// Asset files. |
| 797 |
$pattern_library_asset = $this->the_plugin_instance->include_file( 'build/block-library.asset.php' ); |
| 798 |
|
| 799 |
// Enqueue scripts. |
| 800 |
wp_enqueue_script( |
| 801 |
$pattern_library_handle, |
| 802 |
$this->the_plugin_instance->get_file_uri( 'build/pattern-library.js' ), |
| 803 |
$pattern_library_asset['dependencies'], |
| 804 |
$this->the_plugin_instance->get_script_version( $pattern_library_asset ), |
| 805 |
[ 'in_footer' => true ] |
| 806 |
); |
| 807 |
|
| 808 |
// For debuging. |
| 809 |
$this->the_plugin_instance->enqueue_debug_information( $pattern_library_handle ); |
| 810 |
|
| 811 |
// Load pattern library url. |
| 812 |
wp_add_inline_script( $pattern_library_handle, 'var CBBPatternLibrary=' . wp_json_encode( [ 'URL' => $this->get_library_url() ] ), 'before' ); |
| 813 |
|
| 814 |
// Enqueue style. |
| 815 |
wp_enqueue_style( |
| 816 |
$pattern_library_handle, |
| 817 |
$this->the_plugin_instance->get_file_uri( 'build/pattern-library.css' ), |
| 818 |
[], |
| 819 |
$this->the_plugin_instance->get_script_version( $pattern_library_asset ) |
| 820 |
); |
| 821 |
} |
| 822 |
|
| 823 |
/** |
| 824 |
* Register an endpoint to query patterns. |
| 825 |
* |
| 826 |
* @return array |
| 827 |
*/ |
| 828 |
public function register_pattern_library_get_patterns_endpoint() { |
| 829 |
register_rest_route( |
| 830 |
'boldblocks/v1', |
| 831 |
'/getPatterns/', |
| 832 |
array( |
| 833 |
'methods' => 'GET', |
| 834 |
'callback' => [ $this, 'get_patterns_from_library' ], |
| 835 |
'permission_callback' => '__return_true', |
| 836 |
) |
| 837 |
); |
| 838 |
|
| 839 |
register_rest_route( |
| 840 |
'boldblocks/v1', |
| 841 |
'/getFullPatternData/', |
| 842 |
array( |
| 843 |
'methods' => 'GET', |
| 844 |
'callback' => [ $this, 'get_full_pattern_library_data' ], |
| 845 |
'permission_callback' => '__return_true', |
| 846 |
) |
| 847 |
); |
| 848 |
} |
| 849 |
|
| 850 |
/** |
| 851 |
* Query patterns. |
| 852 |
* |
| 853 |
* @param WP_Rest_Request $request |
| 854 |
* @return WP_Rest_Response |
| 855 |
*/ |
| 856 |
public function get_patterns_from_library( $request ) { |
| 857 |
// Get all patterns. |
| 858 |
$data = $this->get_all_patterns_from_library(); |
| 859 |
|
| 860 |
if ( is_array( $data ) ) { |
| 861 |
return wp_send_json( array_values( $data ) ); |
| 862 |
} |
| 863 |
|
| 864 |
return wp_send_json( $data ); |
| 865 |
} |
| 866 |
|
| 867 |
/** |
| 868 |
* Build patterns data |
| 869 |
* |
| 870 |
* @return array |
| 871 |
*/ |
| 872 |
private function get_all_patterns_from_library() { |
| 873 |
// Get the WP Version global. |
| 874 |
global $wp_version; |
| 875 |
$cache_key = $this->get_library_pattern_cache_key(); |
| 876 |
$data = get_transient( $cache_key ); |
| 877 |
|
| 878 |
if ( $this->is_force_refresh() || false === $data ) { |
| 879 |
$response = wp_remote_get( |
| 880 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks-patterns', |
| 881 |
[ |
| 882 |
'timeout' => 120, |
| 883 |
'sslverify' => false, |
| 884 |
'body' => [ |
| 885 |
'_load_all' => 1, |
| 886 |
'_fields' => |
| 887 |
'id,slug,meta,menu_order,order,title,description,keywords,thumbnail,boldblocks_pattern_keywords,keywordIds,is_pro,has_pro_features', |
| 888 |
'api_version' => 2, |
| 889 |
'cbb_version' => $this->the_plugin_instance->version, |
| 890 |
'wp_version' => $wp_version, |
| 891 |
], |
| 892 |
] |
| 893 |
); |
| 894 |
|
| 895 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 896 |
|
| 897 |
if ( 200 === absint( $response_code ) ) { |
| 898 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 899 |
|
| 900 |
if ( $data ) { |
| 901 |
// Re-index the data. |
| 902 |
$data = array_column( $data, null, 'id' ); |
| 903 |
|
| 904 |
// Update cache. |
| 905 |
$this->update_library_cache( $data, $cache_key ); |
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
return $data; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
/** |
| 915 |
* Get full data for patterns |
| 916 |
* |
| 917 |
* @param WP_Rest_Request $request |
| 918 |
* @return array |
| 919 |
*/ |
| 920 |
public function get_full_pattern_library_data( $request ) { |
| 921 |
// Return array. |
| 922 |
$data = []; |
| 923 |
|
| 924 |
// Get all patterns. |
| 925 |
$all_patterns = $this->get_all_patterns_from_library(); |
| 926 |
|
| 927 |
$pattern_ids = $request->get_param( 'patternIds' ); |
| 928 |
|
| 929 |
$response = wp_remote_get( |
| 930 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks-patterns', |
| 931 |
[ |
| 932 |
'timeout' => 120, |
| 933 |
'sslverify' => false, |
| 934 |
'body' => [ |
| 935 |
'_fields' => |
| 936 |
'id,slug,meta,menu_order,order,title,description,keywords,thumbnail,boldblocks_pattern_keywords,keywordIds,is_pro,has_pro_features,libraryBlocks,libraryVariations', |
| 937 |
'api_version' => 2, |
| 938 |
'include_data' => true, |
| 939 |
'include' => $pattern_ids, |
| 940 |
], |
| 941 |
] |
| 942 |
); |
| 943 |
|
| 944 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 945 |
|
| 946 |
if ( 200 === absint( $response_code ) ) { |
| 947 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 948 |
|
| 949 |
// Update cache. |
| 950 |
foreach ( $data as $post ) { |
| 951 |
$all_patterns[ $post['id'] ] = $post; |
| 952 |
} |
| 953 |
|
| 954 |
// Update cache. |
| 955 |
$this->update_library_cache( $all_patterns, $this->get_library_pattern_cache_key() ); |
| 956 |
} |
| 957 |
|
| 958 |
return $data; |
| 959 |
} |
| 960 |
|
| 961 |
/** |
| 962 |
* Register an endpoint to query pattern keywords. |
| 963 |
* |
| 964 |
* @return array |
| 965 |
*/ |
| 966 |
public function register_get_pattern_keywords_from_library_endpoint() { |
| 967 |
register_rest_route( |
| 968 |
'boldblocks/v1', |
| 969 |
'/getPatternKeywords/', |
| 970 |
array( |
| 971 |
'methods' => 'GET', |
| 972 |
'callback' => [ $this, 'get_pattern_keywords_from_library' ], |
| 973 |
'permission_callback' => '__return_true', |
| 974 |
) |
| 975 |
); |
| 976 |
} |
| 977 |
|
| 978 |
/** |
| 979 |
* Query pattern keywords. |
| 980 |
* |
| 981 |
* @param WP_Rest_Request $request |
| 982 |
* @return WP_Rest_Response |
| 983 |
*/ |
| 984 |
public function get_pattern_keywords_from_library( $request ) { |
| 985 |
$cache_key = $this->get_library_pattern_keywords_cache_key(); |
| 986 |
$data = get_transient( $cache_key ); |
| 987 |
if ( false === $data ) { |
| 988 |
$response = wp_remote_get( |
| 989 |
$this->get_library_url() . '/wp-json/wp/v2/boldblocks_pattern_keywords', |
| 990 |
[ |
| 991 |
'timeout' => 120, |
| 992 |
'sslverify' => false, |
| 993 |
'body' => [ |
| 994 |
'per_page' => 100, |
| 995 |
'orderby' => 'order', |
| 996 |
'order' => 'desc', |
| 997 |
'hide_empty' => 1, |
| 998 |
'_fields' => 'id,count,name,slug,is_design_field', |
| 999 |
], |
| 1000 |
] |
| 1001 |
); |
| 1002 |
|
| 1003 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 1004 |
|
| 1005 |
if ( 200 === absint( $response_code ) ) { |
| 1006 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 1007 |
|
| 1008 |
set_transient( $cache_key, $data, 7 * DAY_IN_SECONDS ); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
return wp_send_json( $data ); |
| 1013 |
} |
| 1014 |
|
| 1015 |
/** |
| 1016 |
* Change the rest parameters |
| 1017 |
* |
| 1018 |
* @param array $params |
| 1019 |
* @param WP_PostType $post_type |
| 1020 |
* @return array |
| 1021 |
*/ |
| 1022 |
public function rest_library_item_collection_params( $params, $post_type ) { |
| 1023 |
if ( isset( $params['per_page'] ) ) { |
| 1024 |
$max_items = $this->get_max_items( $post_type->name ); |
| 1025 |
if ( $max_items ) { |
| 1026 |
$params['per_page']['maximum'] = $max_items; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
return $params; |
| 1031 |
} |
| 1032 |
|
| 1033 |
/** |
| 1034 |
* Load all items. |
| 1035 |
* |
| 1036 |
* @param array $args |
| 1037 |
* @param WP_Request $request |
| 1038 |
* @return array |
| 1039 |
*/ |
| 1040 |
public function rest_library_item_query( $args, $request ) { |
| 1041 |
if ( $request->get_param( '_cbb_load_all' ) ) { |
| 1042 |
$max_items = $this->get_max_items( $args['post_type'] ?? '' ); |
| 1043 |
if ( $max_items ) { |
| 1044 |
$args['posts_per_page'] = $max_items; |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
return $args; |
| 1049 |
} |
| 1050 |
|
| 1051 |
/** |
| 1052 |
* Get max items by post type |
| 1053 |
* |
| 1054 |
* @param string $post_type |
| 1055 |
* @return int |
| 1056 |
*/ |
| 1057 |
private function get_max_items( $post_type ) { |
| 1058 |
$max_items = 0; |
| 1059 |
if ( 'boldblocks_block' === $post_type ) { |
| 1060 |
$max_items = $this->the_plugin_instance->get_component( CustomBlocks::class )->get_max_items(); |
| 1061 |
} elseif ( 'boldblocks_variation' === $post_type ) { |
| 1062 |
$max_items = $this->the_plugin_instance->get_component( Variations::class )->get_max_items(); |
| 1063 |
} elseif ( 'boldblocks_pattern' === $post_type ) { |
| 1064 |
$max_items = $this->the_plugin_instance->get_component( Patterns::class )->get_max_items(); |
| 1065 |
} |
| 1066 |
|
| 1067 |
return $max_items; |
| 1068 |
} |
| 1069 |
|
| 1070 |
/** |
| 1071 |
* Force reload from the library |
| 1072 |
* |
| 1073 |
* @return boolean |
| 1074 |
*/ |
| 1075 |
private function is_force_refresh() { |
| 1076 |
return defined( 'CBB_LIBRARY_DEBUG' ) && CBB_LIBRARY_DEBUG; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
endif; |
| 1080 |
|