PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.2.1
GenerateBlocks v2.2.1
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / class-render-blocks.php
generateblocks / includes Last commit date
blocks 1 year ago dynamic-tags 3 months ago pattern-library 1 year ago utils 2 years ago class-do-css.php 2 years ago class-dynamic-content.php 6 months ago class-dynamic-tag-security.php 6 months ago class-enqueue-css.php 1 year ago class-legacy-attributes.php 4 years ago class-map-deprecated-attributes.php 2 years ago class-meta-handler.php 3 months ago class-plugin-update.php 1 year ago class-query-loop.php 2 years ago class-query-utils.php 3 months ago class-render-blocks.php 1 year ago class-rest.php 1 year ago class-settings.php 1 year ago dashboard.php 1 year ago defaults.php 1 year ago deprecated.php 1 year ago functions.php 6 months ago general.php 8 months ago
class-render-blocks.php
282 lines
1 <?php
2 /**
3 * This file handles the dynamic parts of our blocks.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Render the dynamic aspects of our blocks.
14 *
15 * @since 1.2.0
16 */
17 class GenerateBlocks_Render_Block {
18 /**
19 * Instance.
20 *
21 * @access private
22 * @var object Instance
23 * @since 1.2.0
24 */
25 private static $instance;
26
27 /**
28 * Initiator.
29 *
30 * @since 1.2.0
31 * @return object initialized object of class.
32 */
33 public static function get_instance() {
34 if ( ! isset( self::$instance ) ) {
35 self::$instance = new self();
36 }
37
38 return self::$instance;
39 }
40
41 /**
42 * Constructor.
43 */
44 public function __construct() {
45 add_action( 'init', array( $this, 'register_blocks' ) );
46
47 if ( version_compare( $GLOBALS['wp_version'], '5.9', '>' ) ) {
48 add_filter( 'render_block', array( $this, 'filter_rendered_blocks' ), 10, 3 );
49 add_filter( 'render_block', array( $this, 'late_filter_rendered_blocks' ), 20, 3 );
50 }
51 }
52
53 /**
54 * Register our dynamic blocks.
55 *
56 * @since 1.2.0
57 */
58 public function register_blocks() {
59 $container_args = [
60 'title' => esc_html__( 'Container', 'generateblocks' ),
61 'render_callback' => [ 'GenerateBlocks_Block_Container', 'render_block' ],
62 ];
63
64 if ( version_compare( $GLOBALS['wp_version'], '6.1.0', '<' ) ) {
65 $container_args['editor_script'] = 'generateblocks';
66 $container_args['editor_style'] = 'generateblocks';
67 } else {
68 $container_args['editor_script_handles'] = [ 'generateblocks' ];
69 $container_args['editor_style_handles'] = [ 'generateblocks' ];
70 }
71
72 register_block_type(
73 'generateblocks/container',
74 $container_args
75 );
76
77 register_block_type(
78 'generateblocks/grid',
79 array(
80 'title' => esc_html__( 'Grid', 'generateblocks' ),
81 'render_callback' => [ 'GenerateBlocks_Block_Grid', 'render_block' ],
82 'uses_context' => array(
83 'generateblocks/query',
84 'generateblocks/queryId',
85 'generateblocks/inheritQuery',
86 ),
87 )
88 );
89
90 register_block_type(
91 'generateblocks/query-loop',
92 array(
93 'title' => esc_html__( 'Query loop', 'generateblocks' ),
94 'render_callback' => [ 'GenerateBlocks_Block_Grid', 'render_block' ],
95 'provides_context' => array(
96 'generateblocks/query' => 'query',
97 'generateblocks/queryId' => 'uniqueId',
98 'generateblocks/inheritQuery' => 'inheritQuery',
99 ),
100 )
101 );
102
103 register_block_type(
104 'generateblocks/button-container',
105 array(
106 'title' => esc_html__( 'Buttons', 'generateblocks' ),
107 'render_callback' => [ 'GenerateBlocks_Block_Button_Container', 'render_block' ],
108 )
109 );
110
111 register_block_type(
112 'generateblocks/headline',
113 array(
114 'title' => esc_html__( 'Headline', 'generateblocks' ),
115 'render_callback' => [ 'GenerateBlocks_Block_Headline', 'render_block' ],
116 )
117 );
118
119 register_block_type(
120 'generateblocks/button',
121 array(
122 'title' => esc_html__( 'Button', 'generateblocks' ),
123 'render_callback' => [ 'GenerateBlocks_Block_Button', 'render_block' ],
124 'uses_context' => array(
125 'generateblocks/query',
126 'generateblocks/queryId',
127 'generateblocks/inheritQuery',
128 ),
129 )
130 );
131
132 register_block_type(
133 'generateblocks/image',
134 array(
135 'title' => esc_html__( 'Image', 'generateblocks' ),
136 'render_callback' => [ 'GenerateBlocks_Block_Image', 'render_block' ],
137 'uses_context' => array(
138 'generateblocks/query',
139 'generateblocks/queryId',
140 'postType',
141 'postId',
142 ),
143 )
144 );
145
146 register_block_type_from_metadata(
147 GENERATEBLOCKS_DIR . '/dist/blocks/text',
148 [
149 'render_callback' => [ 'GenerateBlocks_Block_Text', 'render_block' ],
150 ]
151 );
152
153 register_block_type_from_metadata(
154 GENERATEBLOCKS_DIR . '/dist/blocks/element',
155 [
156 'render_callback' => [ 'GenerateBlocks_Block_Element', 'render_block' ],
157 ]
158 );
159
160 register_block_type_from_metadata(
161 GENERATEBLOCKS_DIR . '/dist/blocks/media',
162 [
163 'render_callback' => [ 'GenerateBlocks_Block_Media', 'render_block' ],
164 ]
165 );
166
167 register_block_type_from_metadata(
168 GENERATEBLOCKS_DIR . '/dist/blocks/shape',
169 [
170 'render_callback' => [ 'GenerateBlocks_Block_Shape', 'render_block' ],
171 ]
172 );
173
174 register_block_type_from_metadata(
175 GENERATEBLOCKS_DIR . '/dist/blocks/query',
176 [
177 'render_callback' => [ 'GenerateBlocks_Block_Query', 'render_block' ],
178 ]
179 );
180
181 register_block_type_from_metadata(
182 GENERATEBLOCKS_DIR . '/dist/blocks/looper',
183 [
184 'render_callback' => [ 'GenerateBlocks_Block_Looper', 'render_block' ],
185 ]
186 );
187
188 register_block_type_from_metadata(
189 GENERATEBLOCKS_DIR . '/dist/blocks/query-no-results',
190 [
191 'render_callback' => [ 'GenerateBlocks_Block_Query_No_Results', 'render_block' ],
192 ]
193 );
194
195 register_block_type_from_metadata(
196 GENERATEBLOCKS_DIR . '/dist/blocks/query-page-numbers',
197 [
198 'render_callback' => [ 'GenerateBlocks_Block_Query_Page_Numbers', 'render_block' ],
199 ]
200 );
201
202 register_block_type_from_metadata(
203 GENERATEBLOCKS_DIR . '/dist/blocks/loop-item',
204 [
205 'render_callback' => [ 'GenerateBlocks_Block_Loop_Item', 'render_block' ],
206 ]
207 );
208 }
209
210 /**
211 * Filter existing rendered blocks.
212 *
213 * @since 1.5.0
214 * @param string $block_content The block content.
215 * @param array $block The block data.
216 * @param WP_Block $instance Block instance.
217 */
218 public function filter_rendered_blocks( $block_content, $block, $instance ) {
219 $attributes = isset( $block['attrs'] ) ? $block['attrs'] : null;
220
221 // Don't output if no dynamic link exists.
222 if ( isset( $attributes ) && ! empty( $attributes['dynamicLinkType'] ) && ! empty( $attributes['dynamicLinkRemoveIfEmpty'] ) ) {
223 $dynamic_link = GenerateBlocks_Dynamic_Content::get_dynamic_url( $attributes, $instance );
224
225 if ( ! $dynamic_link ) {
226 return '';
227 }
228 }
229
230 return $block_content;
231 }
232
233 /**
234 * Filter existing rendered blocks. Fires later than `filter_rendered_blocks`.
235 *
236 * @since 2.0.0
237 * @param string $block_content The block content.
238 * @param array $block The block data.
239 * @param WP_Block $instance Block instance.
240 */
241 public function late_filter_rendered_blocks( $block_content, $block, $instance ) {
242 $block_name = $block['blockName'] ?? '';
243 $attributes = $block['attrs'] ?? [];
244
245 if ( 'generateblocks/media' === $block_name ) {
246 $attachment_id = $attributes['mediaId'] ?? 0;
247
248 if ( ! $attachment_id ) {
249 $p = new WP_HTML_Tag_Processor( $block_content );
250
251 if ( $p->next_tag(
252 [
253 'tag_name' => 'img',
254 ]
255 ) ) {
256 $attachment_id = (int) $p->get_attribute( 'data-media-id' ) ?? 0;
257 }
258 }
259
260 if ( $attachment_id > 0 && 'img' === $attributes['tagName'] ) {
261 $context = current_filter();
262
263 if ( ! generateblocks_str_contains( $block_content, ' width=' ) && ! generateblocks_str_contains( $block_content, ' height=' ) ) {
264 $block_content = wp_img_tag_add_width_and_height_attr( $block_content, $context, $attachment_id );
265 }
266
267 // Add 'srcset' and 'sizes' attributes if applicable.
268 if ( ! generateblocks_str_contains( $block_content, ' srcset=' ) ) {
269 $block_content = wp_img_tag_add_srcset_and_sizes_attr( $block_content, $context, $attachment_id );
270 }
271
272 // Add loading optimization attributes if applicable.
273 $block_content = wp_img_tag_add_loading_optimization_attrs( $block_content, $context );
274 }
275 }
276
277 return $block_content;
278 }
279 }
280
281 GenerateBlocks_Render_Block::get_instance();
282