| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenberg Blocks |
| 4 |
* |
| 5 |
* @package CTC |
| 6 |
* @since 5.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace CTC\Gutenberg; |
| 10 |
|
| 11 |
/** |
| 12 |
* Blocks |
| 13 |
* |
| 14 |
* @since 5.0.0 |
| 15 |
*/ |
| 16 |
class Blocks { |
| 17 |
|
| 18 |
/** |
| 19 |
* Instance |
| 20 |
* |
| 21 |
* @var Blocks|null |
| 22 |
*/ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Get instance. |
| 27 |
* |
| 28 |
* @return Blocks |
| 29 |
*/ |
| 30 |
public static function get() { |
| 31 |
if ( null === self::$instance ) { |
| 32 |
self::$instance = new self(); |
| 33 |
} |
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
private function __construct() { |
| 41 |
add_filter( 'block_categories_all', [ $this, 'register_category' ], 10, 2 ); |
| 42 |
|
| 43 |
// Initialize blocks. |
| 44 |
$this->init_blocks(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get block definitions. |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
private function get_block_definitions() { |
| 53 |
return [ |
| 54 |
'term-title' => [ |
| 55 |
'name' => __( 'Term Title', 'ctc' ), |
| 56 |
'description' => __( 'Display the current term title.', 'ctc' ), |
| 57 |
'class' => Blocks\Term_Title::class, |
| 58 |
], |
| 59 |
'copy-button' => [ |
| 60 |
'name' => __( 'Copy Button', 'ctc' ), |
| 61 |
'description' => __( 'Add a copy button with custom content.', 'ctc' ), |
| 62 |
'class' => Blocks\Copy_Button::class, |
| 63 |
], |
| 64 |
'copy-icon' => [ |
| 65 |
'name' => __( 'Copy Icon', 'ctc' ), |
| 66 |
'description' => __( 'Add a copy icon with custom content.', 'ctc' ), |
| 67 |
'class' => Blocks\Copy_Icon::class, |
| 68 |
], |
| 69 |
'icon' => [ |
| 70 |
'name' => __( 'Icon', 'ctc' ), |
| 71 |
'description' => __( 'Add a standalone copy icon.', 'ctc' ), |
| 72 |
'class' => Blocks\Icon::class, |
| 73 |
], |
| 74 |
'social-share' => [ |
| 75 |
'name' => __( 'Social Share', 'ctc' ), |
| 76 |
'description' => __( 'Add social share buttons.', 'ctc' ), |
| 77 |
'class' => Blocks\Social_Share::class, |
| 78 |
], |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Initialize blocks. |
| 84 |
*/ |
| 85 |
private function init_blocks() { |
| 86 |
$definitions = $this->get_block_definitions(); |
| 87 |
|
| 88 |
foreach ( $definitions as $id => $block ) { |
| 89 |
$class = $block['class']; |
| 90 |
|
| 91 |
if ( class_exists( $class ) && method_exists( $class, 'get' ) ) { |
| 92 |
$class::get(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Register category |
| 99 |
* |
| 100 |
* @param array $categories Block categories. |
| 101 |
* @param \WP_Block_Editor_Context $context Block editor context. |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function register_category( $categories, $context ) { |
| 105 |
$categories[] = [ |
| 106 |
'slug' => 'ctc-blocks', |
| 107 |
'title' => esc_html__( 'Copy Anything to Clipboard', 'ctc' ), |
| 108 |
'icon' => 'wordpress', |
| 109 |
]; |
| 110 |
return $categories; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get blocks for external use (e.g., admin UI). |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public static function get_blocks() { |
| 119 |
$instance = self::get(); |
| 120 |
$definitions = $instance->get_block_definitions(); |
| 121 |
$blocks = []; |
| 122 |
|
| 123 |
foreach ( $definitions as $id => $block ) { |
| 124 |
$blocks[] = [ |
| 125 |
'id' => $id, |
| 126 |
'name' => $block['name'], |
| 127 |
'description' => $block['description'], |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
return $blocks; |
| 132 |
} |
| 133 |
} |
| 134 |
|