PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.8.0
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.8.0
1.8.0 1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / src / Blocks / Block.php
hostinger-reach / src / Blocks Last commit date
Block.php 1 week ago SubscriptionFormBlock.php 1 week ago
Block.php
114 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( 'enqueue_block_assets', array( $this, 'enqueue_block_style' ) );
21 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
22 }
23
24
25 public function get_block_name(): string {
26 return "hostinger-reach-$this->name-block";
27 }
28
29 public function enqueue_scripts(): void {
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 wp_localize_script(
59 $this->get_block_name() . '-editor',
60 'hostinger_reach_block_editor_data',
61 $this->get_block_editor_data()
62 );
63
64 $this->autoloader();
65 }
66
67 protected function get_block_editor_data(): array {
68 return array(
69 'rest_url' => esc_url_raw( rest_url() ),
70 'embed_script_url' => HOSTINGER_REACH_EMBED_SCRIPT_URL,
71 'nonce' => wp_create_nonce( 'wp_rest' ),
72 );
73 }
74
75 public function enqueue_block_style(): void {
76 if ( $this->functions->block_file_exists( "$this->name.css" ) === false ) {
77 return;
78 }
79
80 wp_enqueue_style(
81 $this->get_block_name(),
82 $this->functions->get_blocks_url() . "$this->name.css",
83 array(),
84 filemtime( $this->functions->get_block_file_name( "$this->name.css" ) )
85 );
86 }
87
88 public function enqueue_block_script(): void {
89 if ( $this->functions->block_file_exists( "$this->name-view.js" ) === false ) {
90 return;
91 }
92
93 $handler = $this->get_block_name() . '-view';
94
95 wp_enqueue_script(
96 $handler,
97 $this->functions->get_blocks_url() . "$this->name-view.js",
98 array(),
99 filemtime( $this->functions->get_block_file_name( "$this->name-view.js" ) ),
100 true
101 );
102
103 wp_localize_script(
104 $handler,
105 "hostinger_reach_{$this->name}_block_data",
106 $this->data()
107 );
108 }
109
110 abstract public function autoloader(): void;
111 abstract public function data(): array;
112 abstract public function render( array $attributes ): bool|string;
113 }
114