PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.4.0
GenerateBlocks v2.4.0
2.4.0 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 month ago dynamic-tags 1 week ago pattern-library 1 month ago utils 2 years ago class-do-css.php 3 years ago class-dynamic-content.php 1 week ago class-dynamic-tag-security.php 1 week ago class-enqueue-css.php 1 week ago class-legacy-attributes.php 4 years ago class-map-deprecated-attributes.php 3 years ago class-meta-handler.php 1 week ago class-plugin-update.php 1 year ago class-query-loop.php 2 years ago class-query-utils.php 1 week ago class-render-blocks.php 1 week ago class-rest.php 1 year ago class-save-gate.php 1 week ago class-settings.php 1 year ago dashboard.php 1 week ago defaults.php 1 year ago deprecated.php 1 year ago functions.php 1 week ago general.php 1 week ago
class-render-blocks.php
294 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 //
223 // Intentionally NOT gated on useDynamicData: this check has always run whenever
224 // dynamicLinkType + dynamicLinkRemoveIfEmpty are set, so existing sites rely on it
225 // (blocks with these attributes are removed when the URL resolves empty, even with
226 // dynamic data toggled off). Because this is the one render path that reads the
227 // linked object without useDynamicData, the save-time validator mirrors this exact
228 // condition — see GenerateBlocks_Dynamic_Content::get_attribute_referenced_post_ids().
229 // Keep the two in sync: any attribute read added here must also be validated there.
230 if (
231 isset( $attributes ) &&
232 ! empty( $attributes['dynamicLinkType'] ) &&
233 ! empty( $attributes['dynamicLinkRemoveIfEmpty'] )
234 ) {
235 $dynamic_link = GenerateBlocks_Dynamic_Content::get_dynamic_url( $attributes, $instance );
236
237 if ( ! $dynamic_link ) {
238 return '';
239 }
240 }
241
242 return $block_content;
243 }
244
245 /**
246 * Filter existing rendered blocks. Fires later than `filter_rendered_blocks`.
247 *
248 * @since 2.0.0
249 * @param string $block_content The block content.
250 * @param array $block The block data.
251 * @param WP_Block $instance Block instance.
252 */
253 public function late_filter_rendered_blocks( $block_content, $block, $instance ) {
254 $block_name = $block['blockName'] ?? '';
255 $attributes = $block['attrs'] ?? [];
256
257 if ( 'generateblocks/media' === $block_name ) {
258 $attachment_id = $attributes['mediaId'] ?? 0;
259
260 if ( ! $attachment_id ) {
261 $p = new WP_HTML_Tag_Processor( $block_content );
262
263 if ( $p->next_tag(
264 [
265 'tag_name' => 'img',
266 ]
267 ) ) {
268 $attachment_id = (int) $p->get_attribute( 'data-media-id' ) ?? 0;
269 }
270 }
271
272 if ( $attachment_id > 0 && 'img' === $attributes['tagName'] ) {
273 $context = current_filter();
274
275 if ( ! generateblocks_str_contains( $block_content, ' width=' ) && ! generateblocks_str_contains( $block_content, ' height=' ) ) {
276 $block_content = wp_img_tag_add_width_and_height_attr( $block_content, $context, $attachment_id );
277 }
278
279 // Add 'srcset' and 'sizes' attributes if applicable.
280 if ( ! generateblocks_str_contains( $block_content, ' srcset=' ) ) {
281 $block_content = wp_img_tag_add_srcset_and_sizes_attr( $block_content, $context, $attachment_id );
282 }
283
284 // Add loading optimization attributes if applicable.
285 $block_content = wp_img_tag_add_loading_optimization_attrs( $block_content, $context );
286 }
287 }
288
289 return $block_content;
290 }
291 }
292
293 GenerateBlocks_Render_Block::get_instance();
294