| 1 |
<?php |
| 2 |
|
| 3 |
namespace BBlocks\Inc; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class RegisterBlocks |
| 10 |
{ |
| 11 |
function __construct() |
| 12 |
{ |
| 13 |
add_action( 'init', [$this, 'registerBlocks'] ); |
| 14 |
add_filter( 'block_categories_all', [$this, 'registerCategories'] ); |
| 15 |
} |
| 16 |
|
| 17 |
function registerBlocks() |
| 18 |
{ |
| 19 |
$disabledBlocks = BlocksList::disabled(); |
| 20 |
$premiumBlocks = BlocksList::premium(); |
| 21 |
|
| 22 |
foreach ( BlocksList::all() as $block ) { |
| 23 |
$dir = B_BLOCKS_DIR_PATH . "build/$block"; |
| 24 |
|
| 25 |
// The dashboard toggles and the editor bundle both key off the |
| 26 |
// block's NAME, which is not always its directory — data-table is |
| 27 |
// registered as b-blocks/table. Matching on the directory alone |
| 28 |
// leaves such a block registered server-side after it has been |
| 29 |
// disabled, so both keys are checked. |
| 30 |
$name = $this->blockName( $dir, $block ); |
| 31 |
$keys = array_unique( [ $name, "b-blocks/$block" ] ); |
| 32 |
|
| 33 |
if ( array_intersect( $keys, $disabledBlocks ) || array_intersect( $keys, $premiumBlocks ) ) { |
| 34 |
continue; |
| 35 |
} |
| 36 |
|
| 37 |
$registered = register_block_type( $dir ); |
| 38 |
|
| 39 |
// Every block shares one editor bundle, so the per-block handle is |
| 40 |
// dropped to keep it from being loaded again — leaving them all in |
| 41 |
// place runs the bundle once per block, re-registering every |
| 42 |
// inline format. |
| 43 |
$handle = ( $registered instanceof \WP_Block_Type ) ? $registered->name : $name; |
| 44 |
wp_deregister_script( generate_block_asset_handle( $handle, 'editorScript' ) ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* The name block.json registers the block under. Falls back to the |
| 50 |
* directory-derived name when the metadata cannot be read. |
| 51 |
*/ |
| 52 |
private function blockName( $dir, $block ) |
| 53 |
{ |
| 54 |
$file = "$dir/block.json"; |
| 55 |
|
| 56 |
if ( file_exists( $file ) ) { |
| 57 |
$metadata = wp_json_file_decode( $file, [ 'associative' => true ] ); |
| 58 |
|
| 59 |
if ( ! empty( $metadata['name'] ) ) { |
| 60 |
return $metadata['name']; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return "b-blocks/$block"; |
| 65 |
} |
| 66 |
|
| 67 |
function registerCategories($categories) |
| 68 |
{ |
| 69 |
return array_merge([ |
| 70 |
[ |
| 71 |
'slug' => 'bBlocks', |
| 72 |
'title' => __('bBlocks', 'b-blocks'), |
| 73 |
] |
| 74 |
], $categories); |
| 75 |
} // Register categories |
| 76 |
} |
| 77 |
|
| 78 |
new RegisterBlocks(); |
| 79 |
|
| 80 |
/** |
| 81 |
* --------------------------------------------------------------------------- |
| 82 |
* Kept from plugin.php, unchanged: wp_set_script_translations() calls for |
| 83 |
* blocks that are currently commented out of BlocksList::all(). They are |
| 84 |
* dead today; re-enable them alongside their block. Safe to delete. |
| 85 |
* --------------------------------------------------------------------------- |
| 86 |
*/ |
| 87 |
// The newsletter-optin view.js uses @wordpress/i18n – register translations |
| 88 |
// for the auto-generated view-script handle so frontend error/success strings |
| 89 |
// are translatable. The handle is derived from the block name by WordPress core. |
| 90 |
// if ( ! in_array( 'b-blocks/newsletter-optin', $disabledBlocks ) ) { |
| 91 |
// wp_set_script_translations( |
| 92 |
// 'b-blocks-newsletter-optin-view-script', |
| 93 |
// 'b-blocks', |
| 94 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 95 |
// ); |
| 96 |
// } |
| 97 |
|
| 98 |
// The popup-optin view.js imports @wordpress/i18n for frontend |
| 99 |
// validation / status strings, so its view-script handle needs |
| 100 |
// translations registered. |
| 101 |
// if ( ! in_array( 'b-blocks/popup-optin', $disabledBlocks ) ) { |
| 102 |
// wp_set_script_translations( |
| 103 |
// 'b-blocks-popup-optin-view-script', |
| 104 |
// 'b-blocks', |
| 105 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 106 |
// ); |
| 107 |
// } |
| 108 |
|
| 109 |
// The floating-cta view.js imports @wordpress/i18n (__('Button') and __('Dismiss')) |
| 110 |
// so its view-script handle also needs translations registered. |
| 111 |
// if ( ! in_array( 'b-blocks/floating-cta', $disabledBlocks ) ) { |
| 112 |
// wp_set_script_translations( |
| 113 |
// 'b-blocks-floating-cta-view-script', |
| 114 |
// 'b-blocks', |
| 115 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 116 |
// ); |
| 117 |
// } |
| 118 |
|
| 119 |
// The image-slider view.js imports @wordpress/i18n for lightbox aria-labels |
| 120 |
// so its view-script handle needs translations registered. |
| 121 |
// if ( ! in_array( 'b-blocks/image-slider', $disabledBlocks ) ) { |
| 122 |
// wp_set_script_translations( |
| 123 |
// 'b-blocks-image-slider-view-script', |
| 124 |
// 'b-blocks', |
| 125 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 126 |
// ); |
| 127 |
// } |
| 128 |
|
| 129 |
// The testimonial-slider view.js imports @wordpress/i18n for the |
| 130 |
// play/pause toggle aria-labels, so its view-script handle needs |
| 131 |
// translations registered. |
| 132 |
// if ( ! in_array( 'b-blocks/testimonial-slider', $disabledBlocks ) ) { |
| 133 |
// wp_set_script_translations( |
| 134 |
// 'b-blocks-testimonial-slider-view-script', |
| 135 |
// 'b-blocks', |
| 136 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 137 |
// ); |
| 138 |
// } |
| 139 |
|
| 140 |
// The post-filter-grid view.js imports @wordpress/i18n for the |
| 141 |
// aria-live status announcements, so its view-script handle needs |
| 142 |
// translations registered. |
| 143 |
// if ( ! in_array( 'b-blocks/post-filter-grid', $disabledBlocks ) ) { |
| 144 |
// wp_set_script_translations( |
| 145 |
// 'b-blocks-post-filter-grid-view-script', |
| 146 |
// 'b-blocks', |
| 147 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 148 |
// ); |
| 149 |
// } |
| 150 |
|
| 151 |
// The post-masonry-grid view.js imports @wordpress/i18n for the |
| 152 |
// aria-live status announcements, so its view-script handle needs |
| 153 |
// translations registered. |
| 154 |
// if ( ! in_array( 'b-blocks/post-masonry-grid', $disabledBlocks ) ) { |
| 155 |
// wp_set_script_translations( |
| 156 |
// 'b-blocks-post-masonry-grid-view-script', |
| 157 |
// 'b-blocks', |
| 158 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 159 |
// ); |
| 160 |
// } |
| 161 |
|
| 162 |
// The woo-add-to-cart view.js uses @wordpress/i18n for the aria-live |
| 163 |
// add-to-cart status announcements, so its view-script handle needs |
| 164 |
// translations registered. |
| 165 |
// if ( ! in_array( 'b-blocks/woo-add-to-cart', $disabledBlocks ) ) { |
| 166 |
// wp_set_script_translations( |
| 167 |
// 'b-blocks-woo-add-to-cart-view-script', |
| 168 |
// 'b-blocks', |
| 169 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 170 |
// ); |
| 171 |
// } |
| 172 |
|
| 173 |
// The woo-mini-cart view.js uses @wordpress/i18n for the cart |
| 174 |
// aria-label and aria-live announcements, so its view-script |
| 175 |
// handle needs translations registered. |
| 176 |
// if ( ! in_array( 'b-blocks/woo-mini-cart', $disabledBlocks ) ) { |
| 177 |
// wp_set_script_translations( |
| 178 |
// 'b-blocks-woo-mini-cart-view-script', |
| 179 |
// 'b-blocks', |
| 180 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 181 |
// ); |
| 182 |
// } |
| 183 |
|
| 184 |
// The woo-best-sellers view.js imports @wordpress/i18n for the |
| 185 |
// aria-live add-to-cart status announcements ("added to cart", |
| 186 |
// error messages), so its view-script handle needs translations |
| 187 |
// registered. |
| 188 |
// if ( ! in_array( 'b-blocks/woo-best-sellers', $disabledBlocks ) ) { |
| 189 |
// wp_set_script_translations( |
| 190 |
// 'b-blocks-woo-best-sellers-view-script', |
| 191 |
// 'b-blocks', |
| 192 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 193 |
// ); |
| 194 |
// } |
| 195 |
|
| 196 |
// The woo-featured-product view.js uses @wordpress/i18n for the |
| 197 |
// aria-live add-to-cart status announcements, so its view-script |
| 198 |
// handle needs translations registered. |
| 199 |
// if ( ! in_array( 'b-blocks/woo-featured-product', $disabledBlocks ) ) { |
| 200 |
// wp_set_script_translations( |
| 201 |
// 'b-blocks-woo-featured-product-view-script', |
| 202 |
// 'b-blocks', |
| 203 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 204 |
// ); |
| 205 |
// } |
| 206 |
|
| 207 |
// The woo-product-grid view.js uses @wordpress/i18n for the aria-live |
| 208 |
// add-to-cart status announcements, so its view-script handle needs |
| 209 |
// translations registered. |
| 210 |
// if ( ! in_array( 'b-blocks/woo-product-grid', $disabledBlocks ) ) { |
| 211 |
// wp_set_script_translations( |
| 212 |
// 'b-blocks-woo-product-grid-view-script', |
| 213 |
// 'b-blocks', |
| 214 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 215 |
// ); |
| 216 |
// } |
| 217 |
|
| 218 |
// The woo-product-carousel view.js uses @wordpress/i18n for slide |
| 219 |
// aria-live announcements and the add-to-cart status region, so its |
| 220 |
// view-script handle needs translations registered. |
| 221 |
// if ( ! in_array( 'b-blocks/woo-product-carousel', $disabledBlocks ) ) { |
| 222 |
// wp_set_script_translations( |
| 223 |
// 'b-blocks-woo-product-carousel-view-script', |
| 224 |
// 'b-blocks', |
| 225 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 226 |
// ); |
| 227 |
// } |
| 228 |
|
| 229 |
// The sticky-sidebar view.js uses @wordpress/i18n for the scrollable |
| 230 |
// sidebar aria-label, so its view-script handle needs translations registered. |
| 231 |
// if ( ! in_array( 'b-blocks/sticky-sidebar', $disabledBlocks ) ) { |
| 232 |
// wp_set_script_translations( |
| 233 |
// 'b-blocks-sticky-sidebar-view-script', |
| 234 |
// 'b-blocks', |
| 235 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 236 |
// ); |
| 237 |
// } |
| 238 |
|
| 239 |
// The search-bar view.js imports @wordpress/i18n for the live-search |
| 240 |
// no-results / aria-live result-count strings, so its view-script |
| 241 |
// handle needs translations registered. |
| 242 |
// if ( ! in_array( 'b-blocks/search-bar', $disabledBlocks ) ) { |
| 243 |
// wp_set_script_translations( |
| 244 |
// 'b-blocks-search-bar-view-script', |
| 245 |
// 'b-blocks', |
| 246 |
// B_BLOCKS_DIR_PATH . 'languages' |
| 247 |
// ); |
| 248 |
// } |
| 249 |
|