PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / classes / block-base-abstract.php

block-base-abstract.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at includes/classes/block-base-abstract.php

212 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Classes;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use \ABlocks\Helper;
9 use \ABlocks\Assets;
10
11 abstract class BlockBaseAbstract {
12
13 protected $namespace = 'ablocks';
14
15 protected $assets_url = ABLOCKS_ASSETS_URL;
16
17 protected $assets_path = ABLOCKS_ASSETS_PATH;
18
19 protected $blocks_dir_path = ABLOCKS_BLOCKS_DIR_PATH;
20
21 protected $parent_block_name = '';
22
23 protected $block_name = '';
24
25 protected $style_depends = [];
26 protected $script_depends = [];
27
28 public function __construct( $keep_silent = false ) {
29 if ( $this->is_enabled_block() && ! $keep_silent ) {
30 add_action( 'init', array( $this, 'register_block' ), 20 );
31 add_action( 'ablocks/before_render_' . $this->block_name . '_block_content', array( $this, 'enqueue_block_specific_static_assets' ) );
32 }
33 }
34
35 public function is_enabled_block() {
36 global $ablocks_blocks;
37 $block_name = ! empty( $this->parent_block_name ) ? $this->parent_block_name : $this->block_name;
38 if ( isset( $ablocks_blocks->{$block_name} ) ) {
39 return (bool) $ablocks_blocks->{$block_name};
40 }
41 return false;
42 }
43
44 public function register_block() {
45 $block_path = $this->assets_path . 'build/blocks/' . $this->block_name . '/block.json';
46 // Register the block with the merged attributes and render callback
47 register_block_type( $block_path, array(
48 'render_callback' => array( $this, 'render_callback' ),
49 ) );
50 }
51
52 public function get_attributes() {
53 $block_attributes = include $this->blocks_dir_path . $this->block_name . '/attributes.php';
54 return apply_filters( 'ablocks/register_block_attributes', $block_attributes, $this->block_name, $this->parent_block_name );
55 }
56
57 private function get_block_class( $css_class ) {
58 $classes = array();
59 if ( $css_class ) {
60 if ( ! is_array( $css_class ) ) {
61 $css_class = preg_split( '#\s+#', $css_class );
62 }
63 $classes = array_map( 'esc_attr', $css_class );
64 } else {
65 // Ensure that we always coerce class to being an array.
66 $css_class = array();
67 }
68
69 return 'class="' . esc_attr( implode( ' ', $classes ) ) . '"';
70 }
71 private function get_block_data_settings_attributes( $settings ) {
72 if ( ! is_array( $settings ) ) {
73 return '';
74 }
75 // Sanitize each setting in the array
76 $sanitized_settings = array_map( 'esc_attr', $settings );
77 // Encode sanitized settings to JSON and escape it for safe output
78 return sprintf( 'data-settings="%s"', esc_attr( wp_json_encode( $sanitized_settings ) ) );
79 }
80 private function get_dynamic_block_wrap( $attributes, $content, $block_instance ) {
81 $block_id = ( isset( $attributes['block_id'] ) ? $attributes['block_id'] : '' );
82 $animation = ( isset( $attributes['_animation'] ) ? $attributes['_animation'] : [] );
83 $block_class_args = array( 'ablocks-block', 'ablocks-block-' . $block_id, 'ablocks-block--' . $this->block_name );
84 if (
85 count( $animation ) &&
86 ( $animation['animationType'] && $animation['animationType'] !== 'none' ) ||
87 ( $animation['animationTypeTablet'] && $animation['animationTypeTablet'] !== 'none' ) ||
88 ( $animation['animationTypeMobile'] && $animation['animationTypeMobile'] !== 'none' )
89 ) {
90 array_push( $block_class_args, 'ablocks-invisible' );
91 }
92 ob_start();
93 ?>
94 <div <?php echo $this->get_block_class( $block_class_args ) . $this->get_block_data_settings_attributes( $animation ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
95 <div class="ablocks-block-container">
96 <?php
97 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
98 echo $this->render_block_content( $attributes, $content, $block_instance );
99 ?>
100 </div>
101 </div>
102 <?php
103 return ob_get_clean();
104 }
105
106
107 public function render_block_content( $attributes, $content, $block_instance ) {
108 return $content;
109 }
110
111 public function render_callback( $attributes, $content, $block_instance ) {
112 $block_name = $block_instance->name;
113 do_action( 'ablocks/before_render_' . explode( '/', $block_name )[1] . '_block_content', $block_name );
114
115 // Dynamic block
116 if ( ! $content ) {
117 $content = $this->get_dynamic_block_wrap( $attributes, $content, $block_instance );
118 }
119
120 // Static Block but control render from php
121 $content = apply_filters( 'ablocks/get_render_block_content', $content, $attributes, $block_instance );
122 if ( ! $content ) {
123 return $content;
124 }
125
126 // Pure Static block
127 if ( apply_filters( 'ablocks/is_allow_block_inline_assets', ! is_admin() && ! Helper::is_enabled_assets_generation() ) ) {
128 $build_css = '<style>' . $this->build_css( $attributes ) . '</style>';
129 return $build_css . $content;
130 }
131
132 return $content;
133 }
134
135 private function enqueue_static_assets( $block_name ) {
136 // Library
137 if ( count( $this->get_style_depends() ) ) {
138 foreach ( $this->get_style_depends() as $style_handler ) {
139 wp_enqueue_style( $style_handler );
140 }
141 }
142
143 // block static css
144 if ( file_exists( $this->assets_path . 'build/blocks/' . $block_name . '/style.css' ) ) {
145 wp_enqueue_style( 'ablocks-' . $block_name . '-block-static-style', $this->assets_url . 'build/blocks/' . $block_name . '/style.css', array(), filemtime( $this->assets_path . 'build/blocks/' . $block_name . '/style.css' ), 'all' );
146 }
147
148 // Library
149 if ( count( $this->get_script_depends() ) ) {
150 foreach ( $this->get_script_depends() as $script_handler ) {
151 wp_enqueue_script( $script_handler );
152 }
153 }
154
155 if ( file_exists( $this->assets_path . 'build/blocks/' . $block_name . '/view.js' ) ) {
156 $dependencies = include $this->assets_path . 'build/blocks/' . $block_name . '/view.asset.php';
157 wp_enqueue_script(
158 'ablocks-' . $block_name . '-block-static-script',
159 $this->assets_url . 'build/blocks/' . $block_name . '/view.js',
160 $dependencies['dependencies'],
161 $dependencies['version'],
162 true
163 );
164 $Assets = new Assets();
165 wp_localize_script(
166 'ablocks-' . $block_name . '-block-static-script',
167 'ABlocksGlobal',
168 $Assets->get_localize_script_data()
169 );
170 }
171 }
172
173 public function enqueue_block_static_assets() {
174 if ( ! is_admin() && ! Helper::is_enabled_assets_generation() ) {
175 $this->enqueue_static_assets( $this->block_name );
176 }//end if
177 }
178 public function enqueue_block_specific_static_assets( $block_name ) {
179 if ( ! is_admin() && ! Helper::is_enabled_assets_generation() ) {
180 $this->enqueue_static_assets( explode( '/', $block_name )[1] );
181 }//end if
182 }
183
184 public function get_static_css() {
185 if ( file_exists( $this->assets_path . 'build/blocks/' . $this->block_name . '/style.css' ) ) {
186 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
187 $has_static_css = file_get_contents( $this->assets_path . 'build/blocks/' . $this->block_name . '/style.css' );
188 if ( $has_static_css ) {
189 return $has_static_css;
190 }
191 }
192 return '';
193 }
194 public function get_static_js() {
195 if ( file_exists( $this->assets_path . 'build/blocks/' . $this->block_name . '/view.js' ) ) {
196 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
197 $has_static_js = file_get_contents( $this->assets_path . 'build/blocks/' . $this->block_name . '/view.js' );
198 if ( $has_static_js ) {
199 return $has_static_js;
200 }
201 }
202 return '';
203 }
204 public function get_style_depends() {
205 return apply_filters( 'ablocks/block_style_depends', array_merge( $this->style_depends, array( 'ablocks-frontend-google-fonts', 'ablocks-animate-style', 'ablocks-common-style' ) ) );
206 }
207 public function get_script_depends() {
208 return apply_filters( 'ablocks/block_script_depends', array_merge( $this->script_depends, array( 'ablocks-common-script' ) ) );
209 }
210 abstract public function build_css( $attributes);
211 }
212