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

Yoast / wordpress-seo / 59517635615d055f783a2be92e52a1e2637df1be

17 Feb 2025 11:08AM UTC coverage: 53.377% (-1.3%) from 54.636%
59517635615d055f783a2be92e52a1e2637df1be

Pull #22048

github

web-flow
Merge e41dbb150 into 711656c23
Pull Request #22048: Update Dashboard page description

7808 of 13867 branches covered (56.31%)

Branch coverage included in aggregate %.

4 of 5 new or added lines in 2 files covered. (80.0%)

1554 existing lines in 42 files now uncovered.

30279 of 57488 relevant lines covered (52.67%)

40022.22 hits per line

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

16.0
/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
                $this->register_blocks();
×
73
        }
74

75
        /**
76
         * Registers the blocks.
77
         *
78
         * @return void
79
         */
80
        public function register_blocks() {
×
81
                /**
82
                 * Filter: 'wpseo_enable_structured_data_blocks' - Allows disabling Yoast's schema blocks entirely.
83
                 *
84
                 * @param bool $enable If false, our structured data blocks won't show.
85
                 */
86
                if ( ! \apply_filters( 'wpseo_enable_structured_data_blocks', true ) ) {
×
87
                        return;
×
88
                }
89

90
                \register_block_type(
×
91
                        \WPSEO_PATH . 'blocks/structured-data-blocks/faq/block.json',
×
92
                        [
93
                                'render_callback' => [ $this, 'optimize_faq_images' ],
×
94
                        ]
95
                );
96
                \register_block_type(
×
97
                        \WPSEO_PATH . 'blocks/structured-data-blocks/how-to/block.json',
×
98
                        [
99
                                'render_callback' => [ $this, 'optimize_how_to_images' ],
×
100
                        ]
101
                );
102
        }
103

104
        /**
105
         * Optimizes images in the FAQ blocks.
106
         *
107
         * @param array  $attributes The attributes.
108
         * @param string $content    The content.
109
         *
110
         * @return string The content with images optimized.
111
         */
112
        public function optimize_faq_images( $attributes, $content ) {
×
113
                if ( ! isset( $attributes['questions'] ) ) {
×
114
                        return $content;
×
115
                }
116

117
                return $this->optimize_images( $attributes['questions'], 'answer', $content );
×
118
        }
119

120
        /**
121
         * Transforms the durations into a translated string containing the count, and either singular or plural unit.
122
         * For example (in en-US): If 'days' is 1, it returns "1 day". If 'days' is 2, it returns "2 days".
123
         * If a number value is 0, we don't output the string.
124
         *
125
         * @param number $days    Number of days.
126
         * @param number $hours   Number of hours.
127
         * @param number $minutes Number of minutes.
128
         * @return array Array of pluralized durations.
129
         */
130
        private function transform_duration_to_string( $days, $hours, $minutes ) {
×
131
                $strings = [];
×
132
                if ( $days ) {
×
133
                        $strings[] = \sprintf(
×
134
                        /* translators: %d expands to the number of day/days. */
135
                                \_n( '%d day', '%d days', $days, 'wordpress-seo' ),
×
136
                                $days
×
137
                        );
138
                }
139
                if ( $hours ) {
×
140
                        $strings[] = \sprintf(
×
141
                        /* translators: %d expands to the number of hour/hours. */
142
                                \_n( '%d hour', '%d hours', $hours, 'wordpress-seo' ),
×
143
                                $hours
×
144
                        );
145
                }
146
                if ( $minutes ) {
×
147
                        $strings[] = \sprintf(
×
148
                        /* translators: %d expands to the number of minute/minutes. */
149
                                \_n( '%d minute', '%d minutes', $minutes, 'wordpress-seo' ),
×
150
                                $minutes
×
151
                        );
152
                }
153
                return $strings;
×
154
        }
155

156
        /**
157
         * Formats the durations into a translated string.
158
         *
159
         * @param array $attributes The attributes.
160
         * @return string The formatted duration.
161
         */
162
        private function build_duration_string( $attributes ) {
×
163
                $days            = ( $attributes['days'] ?? 0 );
×
164
                $hours           = ( $attributes['hours'] ?? 0 );
×
165
                $minutes         = ( $attributes['minutes'] ?? 0 );
×
166
                $elements        = $this->transform_duration_to_string( $days, $hours, $minutes );
×
167
                $elements_length = \count( $elements );
×
168

169
                switch ( $elements_length ) {
170
                        case 1:
×
171
                                return $elements[0];
×
172
                        case 2:
×
173
                                return \sprintf(
×
174
                                /* translators: %s expands to a unit of time (e.g. 1 day). */
175
                                        \__( '%1$s and %2$s', 'wordpress-seo' ),
×
176
                                        ...$elements
×
177
                                );
178
                        case 3:
×
179
                                return \sprintf(
×
180
                                /* translators: %s expands to a unit of time (e.g. 1 day). */
181
                                        \__( '%1$s, %2$s and %3$s', 'wordpress-seo' ),
×
182
                                        ...$elements
×
183
                                );
184
                        default:
185
                                return '';
×
186
                }
187
        }
188

189
        /**
190
         * Presents the duration text of the How-To block in the site language.
191
         *
192
         * @param array  $attributes The attributes.
193
         * @param string $content    The content.
194
         *
195
         * @return string The content with the duration text in the site language.
196
         */
197
        public function present_duration_text( $attributes, $content ) {
12✔
198
                $duration = $this->build_duration_string( $attributes );
12✔
199
                // 'Time needed:' is the default duration text that will be shown if a user doesn't add one.
200
                $duration_text = \__( 'Time needed:', 'wordpress-seo' );
12✔
201

202
                if ( isset( $attributes['durationText'] ) && $attributes['durationText'] !== '' ) {
12✔
203
                        $duration_text = $attributes['durationText'];
2✔
204
                }
205

206
                return \preg_replace(
12✔
207
                        '/(<p class="schema-how-to-total-time">)(<span class="schema-how-to-duration-time-text">.*<\/span>)(.[^\/p>]*)(<\/p>)/',
12✔
208
                        '<p class="schema-how-to-total-time"><span class="schema-how-to-duration-time-text">' . $duration_text . '&nbsp;</span>' . $duration . '</p>',
12✔
209
                        $content,
12✔
210
                        1
12✔
211
                );
6✔
212
        }
213

214
        /**
215
         * Optimizes images in the How-To blocks.
216
         *
217
         * @param array  $attributes The attributes.
218
         * @param string $content    The content.
219
         *
220
         * @return string The content with images optimized.
221
         */
222
        public function optimize_how_to_images( $attributes, $content ) {
2✔
223
                if ( ! isset( $attributes['steps'] ) ) {
2✔
224
                        return $content;
×
225
                }
226

227
                $content = $this->present_duration_text( $attributes, $content );
2✔
228

229
                return $this->optimize_images( $attributes['steps'], 'text', $content );
2✔
230
        }
231

232
        /**
233
         * Optimizes images in structured data blocks.
234
         *
235
         * @param array  $elements The list of elements from the block attributes.
236
         * @param string $key      The key in the data to iterate over.
237
         * @param string $content  The content.
238
         *
239
         * @return string The content with images optimized.
240
         */
241
        private function optimize_images( $elements, $key, $content ) {
×
242
                global $post;
×
243
                if ( ! $post ) {
×
244
                        return $content;
×
245
                }
246

247
                $this->add_images_from_attributes_to_used_cache( $post->ID, $elements, $key );
×
248

249
                // Then replace all images with optimized versions in the content.
250
                $content = \preg_replace_callback(
×
251
                        '/<img[^>]+>/',
×
252
                        function ( $matches ) {
253
                                \preg_match( '/src="([^"]+)"/', $matches[0], $src_matches );
×
254
                                if ( ! $src_matches || ! isset( $src_matches[1] ) ) {
×
255
                                        return $matches[0];
256
                                }
257
                                $attachment_id = $this->attachment_src_to_id( $src_matches[1] );
×
258
                                if ( $attachment_id === 0 ) {
×
259
                                        return $matches[0];
260
                                }
261
                                $image_size  = 'full';
×
262
                                $image_style = [ 'style' => 'max-width: 100%; height: auto;' ];
×
263
                                \preg_match( '/style="[^"]*width:\s*(\d+)px[^"]*"/', $matches[0], $style_matches );
×
264
                                if ( $style_matches && isset( $style_matches[1] ) ) {
×
265
                                        $width     = (int) $style_matches[1];
×
266
                                        $meta_data = \wp_get_attachment_metadata( $attachment_id );
×
267
                                        if ( isset( $meta_data['height'] ) && isset( $meta_data['width'] ) && $meta_data['height'] > 0 && $meta_data['width'] > 0 ) {
×
268
                                                $aspect_ratio = ( $meta_data['height'] / $meta_data['width'] );
×
269
                                                $height       = ( $width * $aspect_ratio );
×
270
                                                $image_size   = [ $width, $height ];
271
                                        }
272
                                        $image_style = '';
273
                                }
274

275
                                /**
276
                                 * Filter: 'wpseo_structured_data_blocks_image_size' - Allows adjusting the image size in structured data blocks.
277
                                 *
278
                                 * @since 18.2
279
                                 *
280
                                 * @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).
281
                                 * @param int          $attachment_id  The id of the attachment.
282
                                 * @param string       $attachment_src The attachment src.
283
                                 */
284
                                $image_size = \apply_filters(
×
285
                                        'wpseo_structured_data_blocks_image_size',
×
286
                                        $image_size,
×
287
                                        $attachment_id,
×
288
                                        $src_matches[1]
UNCOV
289
                                );
×
290
                                $image_html = \wp_get_attachment_image(
×
291
                                        $attachment_id,
×
292
                                        $image_size,
×
293
                                        false,
×
294
                                        $image_style
295
                                );
296

297
                                if ( empty( $image_html ) ) {
×
298
                                        return $matches[0];
299
                                }
300

301
                                return $image_html;
×
302
                        },
×
303
                        $content
304
                );
305

306
                if ( ! $this->registered_shutdown_function ) {
×
307
                        \register_shutdown_function( [ $this, 'maybe_save_used_caches' ] );
×
308
                        $this->registered_shutdown_function = true;
309
                }
310

311
                return $content;
312
        }
313

314
        /**
315
         * If the caches of structured data block images have been changed, saves them.
316
         *
317
         * @return void
318
         */
319
        public function maybe_save_used_caches() {
×
320
                foreach ( $this->used_caches as $post_id => $used_cache ) {
×
321
                        if ( isset( $this->caches[ $post_id ] ) && $used_cache === $this->caches[ $post_id ] ) {
×
322
                                continue;
323
                        }
324
                        \update_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', $used_cache );
325
                }
326
        }
327

328
        /**
329
         * Converts an attachment src to an attachment ID.
330
         *
331
         * @param string $src The attachment src.
332
         *
333
         * @return int The attachment ID. 0 if none was found.
334
         */
335
        private function attachment_src_to_id( $src ) {
×
336
                global $post;
337

338
                if ( isset( $this->used_caches[ $post->ID ][ $src ] ) ) {
×
339
                        return $this->used_caches[ $post->ID ][ $src ];
340
                }
341

342
                $cache = $this->get_cache_for_post( $post->ID );
×
343
                if ( isset( $cache[ $src ] ) ) {
×
344
                        $this->used_caches[ $post->ID ][ $src ] = $cache[ $src ];
×
345
                        return $cache[ $src ];
346
                }
347

348
                $this->used_caches[ $post->ID ][ $src ] = $this->image_helper->get_attachment_by_url( $src );
×
349
                return $this->used_caches[ $post->ID ][ $src ];
350
        }
351

352
        /**
353
         * Returns the cache from postmeta for a given post.
354
         *
355
         * @param int $post_id The post ID.
356
         *
357
         * @return array The images cache.
358
         */
359
        private function get_cache_for_post( $post_id ) {
×
360
                if ( isset( $this->caches[ $post_id ] ) ) {
×
361
                        return $this->caches[ $post_id ];
362
                }
363

364
                $cache = \get_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', true );
×
365
                if ( ! $cache ) {
×
366
                        $cache = [];
367
                }
368

369
                $this->caches[ $post_id ] = $cache;
×
370
                return $cache;
371
        }
372

373
        /**
374
         * Adds any images that have their ID in the block attributes to the cache.
375
         *
376
         * @param int    $post_id  The post ID.
377
         * @param array  $elements The elements.
378
         * @param string $key      The key in the elements we should loop over.
379
         *
380
         * @return void
381
         */
382
        private function add_images_from_attributes_to_used_cache( $post_id, $elements, $key ) {
×
383
                // First grab all image IDs from the attributes.
384
                $images = [];
×
385
                foreach ( $elements as $element ) {
×
386
                        if ( ! isset( $element[ $key ] ) ) {
×
387
                                continue;
388
                        }
389
                        if ( isset( $element[ $key ] ) && \is_array( $element[ $key ] ) ) {
×
390
                                foreach ( $element[ $key ] as $part ) {
×
391
                                        if ( ! \is_array( $part ) || ! isset( $part['type'] ) || $part['type'] !== 'img' ) {
×
392
                                                continue;
393
                                        }
394

395
                                        if ( ! isset( $part['key'] ) || ! isset( $part['props']['src'] ) ) {
×
396
                                                continue;
397
                                        }
398

399
                                        $images[ $part['props']['src'] ] = (int) $part['key'];
400
                                }
401
                        }
402
                }
403

404
                if ( isset( $this->used_caches[ $post_id ] ) ) {
×
405
                        $this->used_caches[ $post_id ] = \array_merge( $this->used_caches[ $post_id ], $images );
406
                }
407
                else {
408
                        $this->used_caches[ $post_id ] = $images;
409
                }
410
        }
411

412
        /* DEPRECATED METHODS */
413

414
        /**
415
         * Enqueue Gutenberg block assets for backend editor.
416
         *
417
         * @deprecated 22.7
418
         * @codeCoverageIgnore
419
         *
420
         * @return void
421
         */
422
        public function enqueue_block_editor_assets() {
423
                \_deprecated_function( __METHOD__, 'Yoast SEO 22.7' );
424
        }
425
}
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

© 2025 Coveralls, Inc