| @@ -1,11 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Register custom variation | |
| 3 | + * Register custom variations | |
| 4 | 4 | * |
| 5 | 5 | * @package BoldBlocks |
| 6 | 6 | * @author Phi Phan <mrphipv@gmail.com> |
| 7 | - * @copyright Copyright (c) 2021, Phi Phan | |
| 7 | + * @copyright Copyright (c) 2022, Phi Phan | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace BoldBlocks; |
| 11 | 11 | |
| @@ -11,19 +11,58 @@ | ||
| 11 | 11 | |
| 12 | 12 | // Exit if accessed directly. |
| 13 | 13 | defined( 'ABSPATH' ) || exit; |
| 14 | 14 | |
| 15 | -if ( ! class_exists( 'Variations' ) ) : | |
| 15 | +if ( ! class_exists( Variations::class ) ) : | |
| 16 | 16 | /** |
| 17 | 17 | * Manage custom block variations |
| 18 | 18 | */ |
| 19 | - class Variations { | |
| 19 | + class Variations extends CoreComponent { | |
| 20 | 20 | /** |
| 21 | - * A dummy constructor | |
| 21 | + * Post type | |
| 22 | + * | |
| 23 | + * @var string | |
| 22 | 24 | */ |
| 23 | - public function __construct() {} | |
| 25 | + protected $post_type = 'boldblocks_variation'; | |
| 24 | 26 | |
| 25 | 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 | + /** | |
| 26 | 65 | * Run main hooks |
| 27 | 66 | * |
| 28 | 67 | * @return void |
| 29 | 68 | */ |
| @@ -31,17 +70,20 @@ | ||
| 31 | 70 | // Register custom post type. |
| 32 | 71 | add_action( 'init', [ $this, 'register_custom_post_type' ], 5 ); |
| 33 | 72 | |
| 34 | 73 | // 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' ] ); | |
| 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' ] ); | |
| 38 | 77 | add_action( 'pre_get_posts', [ $this, 'pre_get_posts_order_variation_custom_columns' ], 1 ); |
| 39 | 78 | |
| 79 | + // Load custom block variation styles. | |
| 80 | + add_action( 'init', [ $this, 'load_custom_block_variation_styles' ] ); | |
| 81 | + | |
| 40 | 82 | // Only do customization on 'edit.php' page for variation. |
| 41 | 83 | add_action( 'load-edit.php', [ $this, 'admin_listing_variation_page' ] ); |
| 42 | 84 | |
| 43 | - // Load data for js. | |
| 85 | + // Enqueue scripts for variations. | |
| 44 | 86 | add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] ); |
| 45 | 87 | |
| 46 | 88 | // Update template on the fly for variation post type. |
| 47 | 89 | add_action( 'admin_init', [ $this, 'update_template_for_variation' ] ); |
| @@ -47,8 +89,26 @@ | ||
| 47 | 89 | add_action( 'admin_init', [ $this, 'update_template_for_variation' ] ); |
| 48 | 90 | |
| 49 | 91 | // Add rest api endpoint to create variations. |
| 50 | 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' ] ); | |
| 51 | 111 | } |
| 52 | 112 | |
| 53 | 113 | /** |
| 54 | 114 | * Register custom post type |
| @@ -55,116 +115,182 @@ | ||
| 55 | 115 | * |
| 56 | 116 | * @return void |
| 57 | 117 | */ |
| 58 | 118 | 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' ), | |
| 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 | + ] | |
| 75 | 139 | ); |
| 76 | 140 | |
| 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', | |
| 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 | + ] | |
| 99 | 156 | ); |
| 100 | 157 | |
| 101 | - register_post_type( 'boldblocks_variation', $variation_args ); | |
| 158 | + $this->register_meta( | |
| 159 | + 'boldblocks_variation_is_transformable', | |
| 160 | + [ | |
| 161 | + 'type' => 'boolean', | |
| 162 | + 'default' => false, | |
| 163 | + ] | |
| 164 | + ); | |
| 102 | 165 | |
| 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 | - ) | |
| 166 | + $this->register_meta( | |
| 167 | + 'boldblocks_variation_hide_from_inserter', | |
| 168 | + [ | |
| 169 | + 'type' => 'boolean', | |
| 170 | + 'default' => false, | |
| 171 | + ] | |
| 114 | 172 | ); |
| 115 | 173 | |
| 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 | - ) | |
| 174 | + $this->register_meta( | |
| 175 | + 'boldblocks_variation_data', | |
| 176 | + [ | |
| 177 | + 'default' => '{}', | |
| 178 | + ] | |
| 126 | 179 | ); |
| 127 | 180 | |
| 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 | - ) | |
| 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 | + ] | |
| 138 | 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 | + ); | |
| 139 | 206 | } |
| 140 | 207 | |
| 141 | 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 | + /** | |
| 142 | 274 | * Enqueue editor assets |
| 143 | 275 | * |
| 144 | 276 | * @return void |
| 145 | 277 | */ |
| 146 | 278 | public function enqueue_block_editor_assets() { |
| 147 | - global $boldblocks_cbb; | |
| 279 | + // Variations access file. | |
| 280 | + $variations_asset = $this->the_plugin_instance->include_file( 'build/variations.asset.php' ); | |
| 148 | 281 | |
| 149 | - // Custom blocks access file. | |
| 150 | - $variations_asset = $boldblocks_cbb->include_file( 'build/variations.asset.php' ); | |
| 151 | - | |
| 152 | 282 | // Scripts. |
| 153 | 283 | wp_enqueue_script( |
| 154 | 284 | 'boldblocks-variations', |
| 155 | - BOLDBLOCKS_CBB_URL . '/build/variations.js', | |
| 156 | - $variations_asset['dependencies'], | |
| 157 | - $variations_asset['version'], | |
| 158 | - true | |
| 285 | + $this->the_plugin_instance->get_file_uri( '/build/variations.js' ), | |
| 286 | + $variations_asset['dependencies'] ?? [], | |
| 287 | + $this->the_plugin_instance->get_script_version( $variations_asset ), | |
| 288 | + [ 'in_footer' => true ] | |
| 159 | 289 | ); |
| 160 | 290 | |
| 161 | - // Load all variations for registration. | |
| 162 | - wp_localize_script( | |
| 163 | - 'boldblocks-variations', | |
| 164 | - 'BoldblocksVariations', | |
| 165 | - $this->get_all_variations() | |
| 166 | - ); | |
| 291 | + // Add translation. | |
| 292 | + wp_set_script_translations( 'boldblocks-variations', 'content-blocks-builder' ); | |
| 167 | 293 | } |
| 168 | 294 | |
| 169 | 295 | /** |
| 170 | 296 | * Update template for variation |
| @@ -171,24 +297,24 @@ | ||
| 171 | 297 | * |
| 172 | 298 | * @return void |
| 173 | 299 | */ |
| 174 | 300 | public function update_template_for_variation() { |
| 301 | + global $pagenow; | |
| 302 | + | |
| 175 | 303 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 176 | - if ( isset( $_GET['post'] ) ) { | |
| 304 | + if ( 'post.php' === $pagenow && isset( $_GET['post'] ) ) { | |
| 177 | 305 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 178 | 306 | $post_id = absint( $_GET['post'] ); |
| 179 | 307 | $current_post = get_post( $post_id ); |
| 180 | 308 | |
| 181 | - if ( 'boldblocks_variation' !== $current_post->post_type ) { | |
| 309 | + if ( ! $current_post instanceof \WP_Post || $this->post_type !== $current_post->post_type ) { | |
| 182 | 310 | return; |
| 183 | 311 | } |
| 184 | 312 | |
| 185 | 313 | $block_name = get_post_meta( $post_id, 'boldblocks_variation_block_name', true ); |
| 186 | 314 | if ( $block_name ) { |
| 187 | - $post_type_object = get_post_type_object( 'boldblocks_variation' ); | |
| 188 | - $post_type_object->template = array( | |
| 189 | - array( $block_name ), | |
| 190 | - ); | |
| 315 | + $post_type_object = get_post_type_object( $this->post_type ); | |
| 316 | + $post_type_object->template = [ [ $block_name ] ]; | |
| 191 | 317 | } |
| 192 | 318 | } |
| 193 | 319 | } |
| 194 | 320 | |
| @@ -197,33 +323,126 @@ | ||
| 197 | 323 | * |
| 198 | 324 | * @return array |
| 199 | 325 | */ |
| 200 | 326 | public function get_all_variations() { |
| 201 | - $variation_posts = get_posts( | |
| 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( | |
| 202 | 368 | [ |
| 203 | - 'post_type' => 'boldblocks_variation', | |
| 204 | - 'posts_per_page' => -1, | |
| 205 | - 'post_status' => 'publish', | |
| 369 | + 'post_type' => $this->post_type, | |
| 370 | + 'posts_per_page' => $this->get_max_items(), | |
| 371 | + 'orderby' => [ | |
| 372 | + 'menu_order' => 'DESC', | |
| 373 | + 'date' => 'DESC', | |
| 374 | + ], | |
| 206 | 375 | ] |
| 207 | 376 | ); |
| 208 | 377 | |
| 209 | - return array_map( | |
| 210 | - function( $item ) { | |
| 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 | + | |
| 211 | 390 | 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 ), | |
| 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 ), | |
| 218 | 406 | ]; |
| 219 | 407 | }, |
| 220 | - $variation_posts | |
| 408 | + $raw_posts | |
| 221 | 409 | ); |
| 410 | + | |
| 411 | + return $variation_posts; | |
| 222 | 412 | } |
| 223 | 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 | + } | |
| 224 | 426 | |
| 427 | + // Ignore autosave. | |
| 428 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
| 429 | + return; | |
| 430 | + } | |
| 431 | + | |
| 432 | + $this->clear_transient_cache(); | |
| 433 | + } | |
| 434 | + | |
| 225 | 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 | + /** | |
| 226 | 445 | * Build a custom endpoint to create variation |
| 227 | 446 | * |
| 228 | 447 | * @return void |
| 229 | 448 | */ |
| @@ -234,9 +453,9 @@ | ||
| 234 | 453 | array( |
| 235 | 454 | 'methods' => 'POST', |
| 236 | 455 | 'callback' => [ $this, 'create_variation' ], |
| 237 | 456 | 'permission_callback' => function () { |
| 238 | - return current_user_can( 'publish_posts' ); | |
| 457 | + return current_user_can( 'manage_options' ); | |
| 239 | 458 | }, |
| 240 | 459 | ) |
| 241 | 460 | ); |
| 242 | 461 | } |
| @@ -249,43 +468,40 @@ | ||
| 249 | 468 | */ |
| 250 | 469 | public function create_variation( $request ) { |
| 251 | 470 | $args = $request->get_params(); |
| 252 | 471 | |
| 253 | - if ( empty( $args['blockName'] ) || empty( $args['variationName'] ) || empty( $args['content'] ) || empty( $args['data'] ) ) { | |
| 254 | - wp_send_json_error( __( 'Invalid request!', 'boldblocks' ), 400 ); | |
| 472 | + if ( empty( $args['title'] ) || empty( $args['content'] ) || empty( $args['meta'] ) ) { | |
| 473 | + wp_send_json_error( __( 'Invalid request!', 'content-blocks-builder' ), 400 ); | |
| 255 | 474 | } |
| 256 | 475 | |
| 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 | - ]; | |
| 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 | + } | |
| 269 | 480 | |
| 270 | - // Parse JSON object from JSON.stringify. | |
| 271 | - // $data = json_decode(html_entity_decode( stripslashes ($args['data'] ) ), 1);. | |
| 481 | + // Using Posts controller to create a custom variation. | |
| 482 | + $controller = new \WP_REST_Posts_Controller( $this->post_type ); | |
| 483 | + $response = $controller->create_item( $request ); | |
| 272 | 484 | |
| 273 | - // Insert new boldblocks_block post item. | |
| 274 | - $post_id = wp_insert_post( $post_array ); | |
| 485 | + // Create a hook for new variation created. | |
| 486 | + do_action( 'boldblocks_variation_created', $response->data ); | |
| 275 | 487 | |
| 276 | - // Send the error if any. | |
| 277 | - if ( is_wp_error( $post_id ) ) { | |
| 278 | - wp_send_json_error( $post_id, 500 ); | |
| 488 | + if ( is_wp_error( $response ) ) { | |
| 489 | + wp_send_json_error( $response ); | |
| 279 | 490 | } |
| 280 | 491 | |
| 281 | - wp_send_json( | |
| 282 | - [ | |
| 283 | - 'data' => __( 'The new variation has been created!', 'boldblocks' ), | |
| 284 | - 'success' => true, | |
| 285 | - 'post' => [ 'id' => $post_id ], | |
| 286 | - ] | |
| 287 | - ); | |
| 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 ); | |
| 288 | 504 | } |
| 289 | 505 | |
| 290 | 506 | /** |
| 291 | 507 | * Add custom columns to custom post type |
| @@ -294,10 +510,11 @@ | ||
| 294 | 510 | * @return array |
| 295 | 511 | */ |
| 296 | 512 | public function add_variation_custom_columns( $columns ) { |
| 297 | 513 | $custom_columns = array( |
| 298 | - 'block_name' => esc_html__( 'Block name', 'boldblocks' ), | |
| 299 | - 'variation_name' => esc_html__( 'Variation name', 'boldblocks' ), | |
| 514 | + 'block_name' => esc_html__( 'Block name', 'content-blocks-builder' ), | |
| 515 | + 'variation_name' => esc_html__( 'Variation name', 'content-blocks-builder' ), | |
| 516 | + 'is_default' => esc_html__( 'Is default?', 'content-blocks-builder' ), | |
| 300 | 517 | ); |
| 301 | 518 | |
| 302 | 519 | // Add after title column. |
| 303 | 520 | $return = array_slice( $columns, 0, 2, true ) + $custom_columns + array_slice( $columns, 2, count( $columns ) - 2, true ); |
| @@ -320,8 +537,12 @@ | ||
| 320 | 537 | case 'variation_name': |
| 321 | 538 | echo esc_html( get_post_meta( $post_id, 'boldblocks_variation_name', true ) ); |
| 322 | 539 | break; |
| 323 | 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 | + | |
| 324 | 545 | // Just break out of the switch statement for everything else. |
| 325 | 546 | default: |
| 326 | 547 | break; |
| 327 | 548 | } |
| @@ -352,9 +573,15 @@ | ||
| 352 | 573 | return; |
| 353 | 574 | } |
| 354 | 575 | |
| 355 | 576 | // Only for main query with orderby parameter. |
| 356 | - if ( $query->is_main_query() && 'boldblocks_variation' === $query->post_type && $query->get( 'orderby' ) ) { | |
| 577 | + if ( $query->is_main_query() && $this->post_type === $query->get( 'post_type' ) && $query->get( 'orderby' ) ) { | |
| 578 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 579 | + if ( isset( $_GET['boldblocks_variation_filter_block'] ) ) { | |
| 580 | + // Ignore if we're filtering. | |
| 581 | + return; | |
| 582 | + } | |
| 583 | + | |
| 357 | 584 | $orderby = $query->get( 'orderby' ); |
| 358 | 585 | |
| 359 | 586 | switch ( $orderby ) { |
| 360 | 587 | case 'block_name': |
| @@ -359,13 +586,15 @@ | ||
| 359 | 586 | switch ( $orderby ) { |
| 360 | 587 | case 'block_name': |
| 361 | 588 | // set our query's meta_key, which is used for custom fields. |
| 362 | 589 | $query->set( 'meta_key', 'boldblocks_variation_block_name' ); |
| 590 | + $query->set( 'orderby', 'meta_value' ); | |
| 363 | 591 | break; |
| 364 | 592 | |
| 365 | 593 | case 'variation_name': |
| 366 | 594 | // set our query's meta_key, which is used for custom fields. |
| 367 | 595 | $query->set( 'meta_key', 'boldblocks_variation_name' ); |
| 596 | + $query->set( 'orderby', 'meta_value' ); | |
| 368 | 597 | break; |
| 369 | 598 | |
| 370 | 599 | default: |
| 371 | 600 | break; |
| @@ -370,9 +599,8 @@ | ||
| 370 | 599 | default: |
| 371 | 600 | break; |
| 372 | 601 | } |
| 373 | 602 | } |
| 374 | - | |
| 375 | 603 | } |
| 376 | 604 | |
| 377 | 605 | /** |
| 378 | 606 | * Add filters for custom variation columns |
| @@ -380,9 +608,9 @@ | ||
| 380 | 608 | * @return void |
| 381 | 609 | */ |
| 382 | 610 | public function admin_listing_variation_page() { |
| 383 | 611 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 384 | - if ( isset( $_GET['post_type'] ) && 'boldblocks_variation' === $_GET['post_type'] ) { | |
| 612 | + if ( isset( $_GET['post_type'] ) && $this->post_type === $_GET['post_type'] ) { | |
| 385 | 613 | add_filter( 'request', [ $this, 'add_custom_columns_to_query_vars' ] ); |
| 386 | 614 | add_filter( 'restrict_manage_posts', [ $this, 'add_filter_select_control' ] ); |
| 387 | 615 | } |
| 388 | 616 | } |
| @@ -422,13 +650,133 @@ | ||
| 422 | 650 | $boldblocks_variation_filter_block = sanitize_text_field( wp_unslash( $_GET['boldblocks_variation_filter_block'] ) ); |
| 423 | 651 | } |
| 424 | 652 | ?> |
| 425 | 653 | <select name="boldblocks_variation_filter_block" id="boldblocks_variation_filter_block"> |
| 426 | - <option value=""><?php esc_html_e( 'All Blocks', 'boldblocks' ); ?></option> | |
| 654 | + <option value=""><?php esc_html_e( 'All Blocks', 'content-blocks-builder' ); ?></option> | |
| 427 | 655 | <?php foreach ( $block_names as $block_name ) { ?> |
| 428 | 656 | <option value="<?php echo esc_attr( $block_name ); ?>"<?php selected( $boldblocks_variation_filter_block, $block_name ); ?>><?php echo esc_attr( $block_name ); ?></option> |
| 429 | 657 | <?php } ?> |
| 430 | 658 | </select> |
| 431 | 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 ); | |
| 432 | 780 | } |
| 433 | 781 | } |
| 434 | 782 | endif; |