• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 2d66c210b3cdb04e0cabd3a27852287fa31a581c

14 May 2024 08:05AM UTC coverage: 53.131% (+0.9%) from 52.242%
2d66c210b3cdb04e0cabd3a27852287fa31a581c

push

github

YoastBot
Bump version to 22.7 on free

7464 of 13622 branches covered (54.79%)

Branch coverage included in aggregate %.

28518 of 54101 relevant lines covered (52.71%)

41520.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.46
/src/integrations/blocks/structured-data-blocks.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Blocks;
4

5
use WPSEO_Admin_Asset_Manager;
6
use Yoast\WP\SEO\Conditionals\No_Conditionals;
7
use Yoast\WP\SEO\Helpers\Image_Helper;
8
use Yoast\WP\SEO\Integrations\Integration_Interface;
9

10
/**
11
 * Class to load assets required for structured data blocks.
12
 */
13
class Structured_Data_Blocks implements Integration_Interface {
14

15
        use No_Conditionals;
16

17
        /**
18
         * An instance of the WPSEO_Admin_Asset_Manager class.
19
         *
20
         * @var WPSEO_Admin_Asset_Manager
21
         */
22
        protected $asset_manager;
23

24
        /**
25
         * An instance of the image helper class.
26
         *
27
         * @var Image_Helper
28
         */
29
        protected $image_helper;
30

31
        /**
32
         * The image caches per post.
33
         *
34
         * @var array
35
         */
36
        protected $caches = [];
37

38
        /**
39
         * The used cache keys per post.
40
         *
41
         * @var array
42
         */
43
        protected $used_caches = [];
44

45
        /**
46
         * Whether or not we've registered our shutdown function.
47
         *
48
         * @var bool
49
         */
50
        protected $registered_shutdown_function = false;
51

52
        /**
53
         * Structured_Data_Blocks constructor.
54
         *
55
         * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
56
         * @param Image_Helper              $image_helper  The image helper.
57
         */
58
        public function __construct(
2✔
59
                WPSEO_Admin_Asset_Manager $asset_manager,
60
                Image_Helper $image_helper
61
        ) {
1✔
62
                $this->asset_manager = $asset_manager;
2✔
63
                $this->image_helper  = $image_helper;
2✔
64
        }
1✔
65

66
        /**
67
         * Registers hooks for Structured Data Blocks with WordPress.
68
         *
69
         * @return void
70
         */
71
        public function register_hooks() {
×
72
                \add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
×
73
                $this->register_blocks();
74
        }
75

76
        /**
77
         * Registers the blocks.
78
         *
79
         * @return void
80
         */
81
        public function register_blocks() {
82
                \register_block_type(
83
                        'yoast/faq-block',
84
                        [
85
                                'render_callback' => [ $this, 'optimize_faq_images' ],
86
                                'attributes'      => [
87
                                        'className' => [
88
                                                'default' => '',
89
                                                'type'    => 'string',
90
                                        ],
91
                                        'questions' => [
92
                                                'type' => 'array',
93
                                        ],
94
                                        'additionalListCssClasses' => [
95
                                                'type' => 'string',
96
                                        ],
97
                                ],
98
                        ]
99
                );
100
                \register_block_type(
101
                        'yoast/how-to-block',
102
                        [
103
                                'render_callback' => [ $this, 'optimize_how_to_images' ],
104
                                'attributes'      => [
105
                                        'hasDuration' => [
106
                                                'type' => 'boolean',
107
                                        ],
108
                                        'days' => [
109
                                                'type' => 'string',
110
                                        ],
111
                                        'hours' => [
112
                                                'type' => 'string',
113
                                        ],
114
                                        'minutes' => [
115
                                                'type' => 'string',
116
                                        ],
117
                                        'description' => [
118
                                                'type'     => 'array',
119
                                                'source'   => 'children',
120
                                                'selector' => '.schema-how-to-description',
121
                                        ],
122
                                        'jsonDescription' => [
123
                                                'type' => 'string',
124
                                        ],
125
                                        'steps' => [
126
                                                'type' => 'array',
127
                                        ],
128
                                        'additionalListCssClasses' => [
129
                                                'type' => 'string',
130
                                        ],
131
                                        'unorderedList' => [
132
                                                'type' => 'boolean',
133
                                        ],
134
                                        'durationText' => [
135
                                                'type' => 'string',
136
                                        ],
137
                                        'defaultDurationText' => [
138
                                                'type' => 'string',
139
                                        ],
140
                                ],
141
                        ]
142
                );
143
        }
×
144

145
        /**
146
         * Enqueue Gutenberg block assets for backend editor.
147
         *
148
         * @return void
149
         */
150
        public function enqueue_block_editor_assets() {
×
151
                /**
152
                 * Filter: 'wpseo_enable_structured_data_blocks' - Allows disabling Yoast's schema blocks entirely.
153
                 *
154
                 * @param bool $enable If false, our structured data blocks won't show.
155
                 */
156
                if ( ! \apply_filters( 'wpseo_enable_structured_data_blocks', true ) ) {
157
                        return;
158
                }
159

160
                $this->asset_manager->enqueue_script( 'structured-data-blocks' );
161
                $this->asset_manager->enqueue_style( 'structured-data-blocks' );
162
        }
×
163

164
        /**
165
         * Optimizes images in the FAQ blocks.
166
         *
167
         * @param array  $attributes The attributes.
168
         * @param string $content    The content.
169
         *
170
         * @return string The content with images optimized.
171
         */
172
        public function optimize_faq_images( $attributes, $content ) {
×
173
                if ( ! isset( $attributes['questions'] ) ) {
×
174
                        return $content;
175
                }
176

177
                return $this->optimize_images( $attributes['questions'], 'answer', $content );
178
        }
179

180
        /**
181
         * Transforms the durations into a translated string containing the count, and either singular or plural unit.
182
         * For example (in en-US): If 'days' is 1, it returns "1 day". If 'days' is 2, it returns "2 days".
183
         * If a number value is 0, we don't output the string.
184
         *
185
         * @param number $days    Number of days.
186
         * @param number $hours   Number of hours.
187
         * @param number $minutes Number of minutes.
188
         * @return array Array of pluralized durations.
189
         */
190
        private function transform_duration_to_string( $days, $hours, $minutes ) {
191
                $strings = [];
192
                if ( $days ) {
193
                        $strings[] = \sprintf(
194
                        /* translators: %d expands to the number of day/days. */
195
                                \_n( '%d day', '%d days', $days, 'wordpress-seo' ),
196
                                $days
197
                        );
12✔
198
                }
12✔
199
                if ( $hours ) {
200
                        $strings[] = \sprintf(
12✔
201
                        /* translators: %d expands to the number of hour/hours. */
202
                                \_n( '%d hour', '%d hours', $hours, 'wordpress-seo' ),
12✔
203
                                $hours
2✔
204
                        );
205
                }
206
                if ( $minutes ) {
12✔
207
                        $strings[] = \sprintf(
12✔
208
                        /* translators: %d expands to the number of minute/minutes. */
12✔
209
                                \_n( '%d minute', '%d minutes', $minutes, 'wordpress-seo' ),
12✔
210
                                $minutes
12✔
211
                        );
6✔
212
                }
213
                return $strings;
214
        }
215

216
        /**
217
         * Formats the durations into a translated string.
218
         *
219
         * @param array $attributes The attributes.
220
         * @return string The formatted duration.
221
         */
222
        private function build_duration_string( $attributes ) {
2✔
223
                $days            = ( $attributes['days'] ?? 0 );
2✔
224
                $hours           = ( $attributes['hours'] ?? 0 );
×
225
                $minutes         = ( $attributes['minutes'] ?? 0 );
226
                $elements        = $this->transform_duration_to_string( $days, $hours, $minutes );
227
                $elements_length = \count( $elements );
2✔
228

229
                switch ( $elements_length ) {
2✔
230
                        case 1:
231
                                return $elements[0];
232
                        case 2:
233
                                return \sprintf(
234
                                /* translators: %s expands to a unit of time (e.g. 1 day). */
235
                                        \__( '%1$s and %2$s', 'wordpress-seo' ),
236
                                        ...$elements
237
                                );
238
                        case 3:
239
                                return \sprintf(
240
                                /* translators: %s expands to a unit of time (e.g. 1 day). */
241
                                        \__( '%1$s, %2$s and %3$s', 'wordpress-seo' ),
×
242
                                        ...$elements
×
243
                                );
244
                        default:
245
                                return '';
246
                }
247
        }
248

249
        /**
250
         * Presents the duration text of the How-To block in the site language.
251
         *
252
         * @param array  $attributes The attributes.
253
         * @param string $content    The content.
254
         *
255
         * @return string The content with the duration text in the site language.
256
         */
257
        public function present_duration_text( $attributes, $content ) {
×
258
                $duration = $this->build_duration_string( $attributes );
×
259
                // 'Time needed:' is the default duration text that will be shown if a user doesn't add one.
260
                $duration_text = \__( 'Time needed:', 'wordpress-seo' );
261

262
                if ( isset( $attributes['durationText'] ) && $attributes['durationText'] !== '' ) {
×
263
                        $duration_text = $attributes['durationText'];
×
264
                }
265

266
                return \preg_replace(
×
267
                        '/(<p class="schema-how-to-total-time">)(<span class="schema-how-to-duration-time-text">.*<\/span>)(.[^\/p>]*)(<\/p>)/',
×
268
                        '<p class="schema-how-to-total-time"><span class="schema-how-to-duration-time-text">' . $duration_text . '&nbsp;</span>' . $duration . '</p>',
×
269
                        $content,
×
270
                        1
271
                );
272
        }
273

274
        /**
275
         * Optimizes images in the How-To blocks.
276
         *
277
         * @param array  $attributes The attributes.
278
         * @param string $content    The content.
279
         *
280
         * @return string The content with images optimized.
281
         */
282
        public function optimize_how_to_images( $attributes, $content ) {
283
                if ( ! isset( $attributes['steps'] ) ) {
284
                        return $content;
×
285
                }
286

287
                $content = $this->present_duration_text( $attributes, $content );
×
288

289
                return $this->optimize_images( $attributes['steps'], 'text', $content );
290
        }
291

292
        /**
293
         * Optimizes images in structured data blocks.
294
         *
295
         * @param array  $elements The list of elements from the block attributes.
296
         * @param string $key      The key in the data to iterate over.
297
         * @param string $content  The content.
298
         *
299
         * @return string The content with images optimized.
300
         */
301
        private function optimize_images( $elements, $key, $content ) {
×
302
                global $post;
×
303
                if ( ! $post ) {
304
                        return $content;
305
                }
306

307
                $this->add_images_from_attributes_to_used_cache( $post->ID, $elements, $key );
×
308

309
                // Then replace all images with optimized versions in the content.
310
                $content = \preg_replace_callback(
311
                        '/<img[^>]+>/',
312
                        function ( $matches ) {
313
                                \preg_match( '/src="([^"]+)"/', $matches[0], $src_matches );
314
                                if ( ! $src_matches || ! isset( $src_matches[1] ) ) {
315
                                        return $matches[0];
316
                                }
317
                                $attachment_id = $this->attachment_src_to_id( $src_matches[1] );
318
                                if ( $attachment_id === 0 ) {
319
                                        return $matches[0];
×
320
                                }
321
                                $image_size  = 'full';
×
322
                                $image_style = [ 'style' => 'max-width: 100%; height: auto;' ];
323
                                \preg_match( '/style="[^"]*width:\s*(\d+)px[^"]*"/', $matches[0], $style_matches );
324
                                if ( $style_matches && isset( $style_matches[1] ) ) {
325
                                        $width     = (int) $style_matches[1];
326
                                        $meta_data = \wp_get_attachment_metadata( $attachment_id );
327
                                        if ( isset( $meta_data['height'] ) && isset( $meta_data['width'] ) && $meta_data['height'] > 0 && $meta_data['width'] > 0 ) {
328
                                                $aspect_ratio = ( $meta_data['height'] / $meta_data['width'] );
329
                                                $height       = ( $width * $aspect_ratio );
330
                                                $image_size   = [ $width, $height ];
331
                                        }
332
                                        $image_style = '';
333
                                }
334

335
                                /**
336
                                 * Filter: 'wpseo_structured_data_blocks_image_size' - Allows adjusting the image size in structured data blocks.
337
                                 *
338
                                 * @since 18.2
339
                                 *
340
                                 * @param string|int[] $image_size     The image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
341
                                 * @param int          $attachment_id  The id of the attachment.
342
                                 * @param string       $attachment_src The attachment src.
343
                                 */
344
                                $image_size = \apply_filters(
×
345
                                        'wpseo_structured_data_blocks_image_size',
346
                                        $image_size,
347
                                        $attachment_id,
348
                                        $src_matches[1]
×
349
                                );
350
                                $image_html = \wp_get_attachment_image(
351
                                        $attachment_id,
352
                                        $image_size,
353
                                        false,
354
                                        $image_style
355
                                );
356

357
                                if ( empty( $image_html ) ) {
358
                                        return $matches[0];
359
                                }
360

361
                                return $image_html;
362
                        },
363
                        $content
364
                );
365

366
                if ( ! $this->registered_shutdown_function ) {
367
                        \register_shutdown_function( [ $this, 'maybe_save_used_caches' ] );
368
                        $this->registered_shutdown_function = true;
369
                }
370

371
                return $content;
372
        }
373

374
        /**
375
         * If the caches of structured data block images have been changed, saves them.
376
         *
377
         * @return void
378
         */
379
        public function maybe_save_used_caches() {
380
                foreach ( $this->used_caches as $post_id => $used_cache ) {
381
                        if ( isset( $this->caches[ $post_id ] ) && $used_cache === $this->caches[ $post_id ] ) {
382
                                continue;
×
383
                        }
384
                        \update_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', $used_cache );
×
385
                }
386
        }
×
387

388
        /**
389
         * Converts an attachment src to an attachment ID.
390
         *
391
         * @param string $src The attachment src.
392
         *
393
         * @return int The attachment ID. 0 if none was found.
394
         */
395
        private function attachment_src_to_id( $src ) {
×
396
                global $post;
397

398
                if ( isset( $this->used_caches[ $post->ID ][ $src ] ) ) {
399
                        return $this->used_caches[ $post->ID ][ $src ];
400
                }
401

402
                $cache = $this->get_cache_for_post( $post->ID );
403
                if ( isset( $cache[ $src ] ) ) {
404
                        $this->used_caches[ $post->ID ][ $src ] = $cache[ $src ];
×
405
                        return $cache[ $src ];
406
                }
407

408
                $this->used_caches[ $post->ID ][ $src ] = $this->image_helper->get_attachment_by_url( $src );
409
                return $this->used_caches[ $post->ID ][ $src ];
410
        }
411

412
        /**
413
         * Returns the cache from postmeta for a given post.
414
         *
415
         * @param int $post_id The post ID.
416
         *
417
         * @return array The images cache.
418
         */
419
        private function get_cache_for_post( $post_id ) {
420
                if ( isset( $this->caches[ $post_id ] ) ) {
421
                        return $this->caches[ $post_id ];
422
                }
423

424
                $cache = \get_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', true );
425
                if ( ! $cache ) {
426
                        $cache = [];
427
                }
428

429
                $this->caches[ $post_id ] = $cache;
430
                return $cache;
431
        }
432

433
        /**
434
         * Adds any images that have their ID in the block attributes to the cache.
435
         *
436
         * @param int    $post_id  The post ID.
437
         * @param array  $elements The elements.
438
         * @param string $key      The key in the elements we should loop over.
439
         *
440
         * @return void
441
         */
442
        private function add_images_from_attributes_to_used_cache( $post_id, $elements, $key ) {
443
                // First grab all image IDs from the attributes.
444
                $images = [];
445
                foreach ( $elements as $element ) {
446
                        if ( ! isset( $element[ $key ] ) ) {
447
                                continue;
448
                        }
449
                        if ( isset( $element[ $key ] ) && \is_array( $element[ $key ] ) ) {
450
                                foreach ( $element[ $key ] as $part ) {
451
                                        if ( ! \is_array( $part ) || ! isset( $part['type'] ) || $part['type'] !== 'img' ) {
452
                                                continue;
453
                                        }
454

455
                                        if ( ! isset( $part['key'] ) || ! isset( $part['props']['src'] ) ) {
456
                                                continue;
457
                                        }
458

459
                                        $images[ $part['props']['src'] ] = (int) $part['key'];
460
                                }
461
                        }
462
                }
463

464
                if ( isset( $this->used_caches[ $post_id ] ) ) {
465
                        $this->used_caches[ $post_id ] = \array_merge( $this->used_caches[ $post_id ], $images );
466
                }
467
                else {
468
                        $this->used_caches[ $post_id ] = $images;
469
                }
470
        }
471
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc