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