Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Recipe_Block
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
12
0.00% covered (danger)
0.00%
0 / 1
 render
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 render_hero
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 render_step
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jetpack Recipe Block
4 *
5 * @package automattic/jetpack
6 */
7
8namespace Automattic\Jetpack\Extensions\Recipe;
9
10use Jetpack_Gutenberg;
11
12/**
13 * Jetpack Recipe Block class.
14 *
15 * Helper class that lets us add schema attributes dynamically because they are not something that is store with the content.
16 * Due to the limitations of wp_kses.
17 *
18 * @since 11.1
19 */
20class Jetpack_Recipe_Block {
21    /**
22     * Adds recipe schema attributes.
23     *
24     * @param array  $attr    Array containing the recipe block attributes.
25     * @param string $content String containing the recipe block content.
26     *
27     * @return string
28     */
29    public static function render( $attr, $content ) {
30        Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
31
32        $find    = array(
33            '/(class="wp-block-jetpack-recipe(\s|"))/',
34            '/(class="wp-block-jetpack-recipe-title(\s|"))/',
35            '/(class="wp-block-jetpack-recipe-description(\s|"))/',
36        );
37        $replace = array(
38            'itemscope itemtype="https://schema.org/Recipe" ${1}',
39            'itemprop="name" ${1}',
40            'itemprop="description" ${1}',
41        );
42
43        return preg_replace( $find, $replace, $content );
44    }
45
46    /**
47     * Adds recipe hero schema attributes.
48     *
49     * @param array  $attr    Array containing the recipe-hero block attributes.
50     * @param string $content String containing the recipe-hero block content.
51     *
52     * @return string
53     */
54    public static function render_hero( $attr, $content ) {
55        $find    = array(
56            '<img',
57        );
58        $replace = array(
59            '<img itemprop="image" ',
60        );
61
62        return str_replace( $find, $replace, $content );
63    }
64
65    /**
66     * Adds recipe step schema attributes.
67     *
68     * @param array  $attr    Array containing the recipe-step block attributes.
69     * @param string $content String containing the recipe-step block content.
70     *
71     * @return string
72     */
73    public static function render_step( $attr, $content ) {
74        $find    = array(
75            'class="wp-block-jetpack-recipe-step-name"',
76            'class="wp-block-jetpack-recipe-step-desc"',
77            'class="wp-image',
78        );
79        $replace = array(
80            'itemprop="name" class="wp-block-jetpack-recipe-step-name"',
81            'itemprop="text" class="wp-block-jetpack-recipe-step-desc"',
82            'itemprop="image" class="wp-image',
83        );
84
85        return str_replace( $find, $replace, $content );
86    }
87}