| 1 |
<?php |
| 2 |
/** |
| 3 |
* Term Thumbnail Block |
| 4 |
* |
| 5 |
* @package gutenify |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace gutenify\Blocks\Term_Thumbnail; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
class Term_Thumbnail { |
| 13 |
|
| 14 |
/** |
| 15 |
* Init function. |
| 16 |
*/ |
| 17 |
public static function init() { |
| 18 |
add_action( 'init', array( __CLASS__, 'register_block' ) ); |
| 19 |
add_action( 'init', array( __CLASS__, 'register_term_meta' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Exposes the `thumbnail_id` term meta convention this block already |
| 24 |
* reads (see render.php) over the REST API, for any taxonomy. |
| 25 |
* |
| 26 |
* Without this, `GET /wp/v2/<taxonomy>/<id>` returns an empty `meta` |
| 27 |
* object regardless of whether term meta exists — WordPress never |
| 28 |
* exposes a meta key over REST unless it's explicitly registered with |
| 29 |
* `show_in_rest`, confirmed live against this site's own |
| 30 |
* `product_cat` terms. That silently blocked the block editor's own |
| 31 |
* Focal Point picker (edit.js) from ever finding a real image to show |
| 32 |
* a preview against, since it has no other way to resolve "this |
| 33 |
* term's thumbnail" client-side. |
| 34 |
*/ |
| 35 |
public static function register_term_meta() { |
| 36 |
register_term_meta( |
| 37 |
'', |
| 38 |
'thumbnail_id', |
| 39 |
array( |
| 40 |
'type' => 'integer', |
| 41 |
'single' => true, |
| 42 |
'show_in_rest' => true, |
| 43 |
) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Register the block. |
| 49 |
* |
| 50 |
* block.json's `render` field wires render.php as the render callback |
| 51 |
* automatically. Unlike category-list/single-product-gallery, this |
| 52 |
* block is registered unconditionally (not gated behind WooCommerce or |
| 53 |
* WP_DEBUG) — it works for any taxonomy that happens to store a |
| 54 |
* thumbnail under the `thumbnail_id` term meta key, and render.php |
| 55 |
* itself only reaches into WooCommerce for its own placeholder-image |
| 56 |
* fallback when the taxonomy is `product_cat`. |
| 57 |
*/ |
| 58 |
public static function register_block() { |
| 59 |
register_block_type( __DIR__ ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
Term_Thumbnail::init(); |
| 64 |
|