Block.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Blocks; |
| 4 | |
| 5 | use Hostinger\Reach\Functions; |
| 6 | use Hostinger\Reach\Setup\Assets; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | abstract class Block { |
| 13 | public Assets $assets; |
| 14 | public Functions $functions; |
| 15 | public string $name; |
| 16 | |
| 17 | public function __construct( Assets $assets, Functions $functions ) { |
| 18 | $this->assets = $assets; |
| 19 | $this->functions = $functions; |
| 20 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 21 | } |
| 22 | |
| 23 | |
| 24 | public function get_block_name(): string { |
| 25 | return "hostinger-reach-$this->name-block"; |
| 26 | } |
| 27 | |
| 28 | public function enqueue_scripts(): void { |
| 29 | $this->enqueue_block_style(); |
| 30 | $this->enqueue_block_script(); |
| 31 | } |
| 32 | |
| 33 | public function register(): void { |
| 34 | register_block_type( |
| 35 | HOSTINGER_REACH_PLUGIN_DIR . "frontend/blocks/$this->name-block/block.json", |
| 36 | array( |
| 37 | 'render_callback' => array( $this, 'render' ), |
| 38 | ) |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | public function enqueue_block(): void { |
| 43 | if ( $this->functions->block_file_exists( "$this->name.js" ) === false ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | wp_enqueue_script( |
| 48 | $this->get_block_name() . '-editor', |
| 49 | $this->functions->get_blocks_url() . "$this->name.js", |
| 50 | array( 'react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n' ), |
| 51 | filemtime( $this->functions->get_block_file_name( "$this->name.js" ) ), |
| 52 | true |
| 53 | ); |
| 54 | |
| 55 | $this->enqueue_block_style(); |
| 56 | wp_set_script_translations( $this->get_block_name(), 'hostinger-reach', HOSTINGER_REACH_PLUGIN_DIR . 'languages' ); |
| 57 | |
| 58 | $this->autoloader(); |
| 59 | } |
| 60 | |
| 61 | public function enqueue_block_style(): void { |
| 62 | if ( $this->functions->block_file_exists( "$this->name.css" ) === false ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | wp_enqueue_style( |
| 67 | $this->get_block_name(), |
| 68 | $this->functions->get_blocks_url() . "$this->name.css", |
| 69 | array(), |
| 70 | filemtime( $this->functions->get_block_file_name( "$this->name.css" ) ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | public function enqueue_block_script(): void { |
| 75 | if ( $this->functions->block_file_exists( "$this->name-view.js" ) === false ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | $handler = $this->get_block_name() . '-view'; |
| 80 | |
| 81 | wp_enqueue_script( |
| 82 | $handler, |
| 83 | $this->functions->get_blocks_url() . "$this->name-view.js", |
| 84 | array(), |
| 85 | filemtime( $this->functions->get_block_file_name( "$this->name-view.js" ) ), |
| 86 | true |
| 87 | ); |
| 88 | |
| 89 | wp_localize_script( |
| 90 | $handler, |
| 91 | "hostinger_reach_{$this->name}_block_data", |
| 92 | $this->data() |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | abstract public function autoloader(): void; |
| 97 | abstract public function data(): array; |
| 98 | abstract public function render( array $attributes ): bool|string; |
| 99 | } |
| 100 |