| 1 |
<?php |
| 2 |
/** |
| 3 |
* Snippet block for Gutenberg editor |
| 4 |
* |
| 5 |
* @package Woody_Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* WINP_Gutenberg_Snippet Class |
| 15 |
*/ |
| 16 |
class WINP_Gutenberg_Snippet { |
| 17 |
|
| 18 |
/** |
| 19 |
* Shortcode data. |
| 20 |
* |
| 21 |
* @var array<int, array<string, mixed>> |
| 22 |
*/ |
| 23 |
protected $shortcode_data; |
| 24 |
|
| 25 |
/** |
| 26 |
* WINP_Gutenberg_Snippet constructor. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
$this->register_hooks(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Register WordPress hooks. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
private function register_hooks() { |
| 38 |
add_action( 'init', [ $this, 'init' ] ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize the Gutenberg block. |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function init() { |
| 47 |
$this->shortcode_data = WINP_Helper::get_shortcode_data(); |
| 48 |
|
| 49 |
if ( empty( $this->shortcode_data ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Register the block type and get the WP_Block_Type instance. |
| 54 |
$block_type = register_block_type( WINP_PLUGIN_DIR . '/admin/assets/gutenberg/build/block.json' ); |
| 55 |
|
| 56 |
/** |
| 57 |
* Register snippets object, so it can be accessible within Gutenberg. |
| 58 |
* The script handle is automatically generated by WordPress from the block name. |
| 59 |
*/ |
| 60 |
if ( $block_type && ! empty( $block_type->editor_script_handles ) ) { |
| 61 |
wp_localize_script( |
| 62 |
$block_type->editor_script_handles[0], |
| 63 |
'winp_snippets', |
| 64 |
[ |
| 65 |
'data' => $this->prepared_snippets_data(), |
| 66 |
] |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Prepare snippets data for the block editor. |
| 73 |
* |
| 74 |
* @return array<int, array<string, mixed>> |
| 75 |
*/ |
| 76 |
public function prepared_snippets_data() { |
| 77 |
return self::get_prepared_snippets_data(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get prepared snippets data statically for use in render.php. |
| 82 |
* |
| 83 |
* @return array<int, array<string, mixed>> |
| 84 |
*/ |
| 85 |
public static function get_prepared_snippets_data() { |
| 86 |
$shortcode_data = WINP_Helper::get_shortcode_data(); |
| 87 |
$prepared_object = []; |
| 88 |
|
| 89 |
foreach ( (array) $shortcode_data as $item ) { |
| 90 |
|
| 91 |
if ( ! isset( $item['id'] ) ) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
$tags = isset( $item['snippet_tags'] ) ? $item['snippet_tags'] : []; |
| 96 |
$tags = array_values( |
| 97 |
array_filter( |
| 98 |
$tags, |
| 99 |
function ( $tag ) { |
| 100 |
return 'id' !== $tag; |
| 101 |
} |
| 102 |
) |
| 103 |
); |
| 104 |
|
| 105 |
$prepared_object[ $item['id'] ] = [ |
| 106 |
'id' => $item['id'], |
| 107 |
'title' => $item['title'], |
| 108 |
'type' => isset( $item['type'] ) ? $item['type'] : '', |
| 109 |
'tags' => $tags, |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
return $prepared_object; |
| 114 |
} |
| 115 |
} |
| 116 |
|