| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Frontend\Schema |
| 6 |
*/ |
| 7 |
|
| 8 |
use Yoast\WP\SEO\Generators\Schema\HowTo; |
| 9 |
|
| 10 |
/** |
| 11 |
* Returns schema FAQ data. |
| 12 |
* |
| 13 |
* @since 11.5 |
| 14 |
* @deprecated 14.0 |
| 15 |
*/ |
| 16 |
class WPSEO_Schema_HowTo extends WPSEO_Deprecated_Graph_Piece { |
| 17 |
|
| 18 |
/** |
| 19 |
* The HowTo blocks count on the current page. |
| 20 |
* |
| 21 |
* @var int |
| 22 |
*/ |
| 23 |
private $counter = 0; |
| 24 |
|
| 25 |
/** |
| 26 |
* WPSEO_Schema_FAQ constructor. |
| 27 |
* |
| 28 |
* @deprecated 14.0 |
| 29 |
* @codeCoverageIgnore |
| 30 |
* |
| 31 |
* @param null $context The context. No longer used but present for BC. |
| 32 |
*/ |
| 33 |
public function __construct( $context = null ) { |
| 34 |
parent::__construct( HowTo::class ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Renders the How-To block into our graph. |
| 39 |
* |
| 40 |
* @deprecated 14.0 |
| 41 |
* @codeCoverageIgnore |
| 42 |
* |
| 43 |
* @param array $graph Our Schema data. |
| 44 |
* @param array $block The How-To block content. |
| 45 |
* |
| 46 |
* @return mixed |
| 47 |
*/ |
| 48 |
public function render( $graph, $block ) { |
| 49 |
_deprecated_function( __METHOD__, 'WPSEO 14.0', 'Yoast\WP\SEO\Generators\Schema\HowTo::add_how_to' ); |
| 50 |
|
| 51 |
++$this->counter; |
| 52 |
$this->add_how_to( $graph, $block, $this->counter ); |
| 53 |
|
| 54 |
return $graph; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Generates the HowTo schema for a block. |
| 59 |
* |
| 60 |
* @param array $graph Our Schema data. |
| 61 |
* @param array $block The How-To block content. |
| 62 |
* @param int $index The index of the current block. |
| 63 |
*/ |
| 64 |
protected function add_how_to( &$graph, $block, $index ) { |
| 65 |
$data = [ |
| 66 |
'@type' => 'HowTo', |
| 67 |
'@id' => $this->stable->context->canonical . '#howto-' . ( $index + 1 ), |
| 68 |
'name' => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->stable->context->id ) ), |
| 69 |
'mainEntityOfPage' => [ '@id' => $this->stable->context->main_schema_id ], |
| 70 |
'description' => '', |
| 71 |
]; |
| 72 |
|
| 73 |
if ( isset( $block['attrs']['jsonDescription'] ) ) { |
| 74 |
$data['description'] = $this->helpers->schema->html->sanitize( $block['attrs']['jsonDescription'] ); |
| 75 |
} |
| 76 |
|
| 77 |
$this->add_duration( $data, $block['attrs'] ); |
| 78 |
$this->add_steps( $data, $block['attrs']['steps'] ); |
| 79 |
|
| 80 |
$data = $this->helpers->schema->language->add_piece_language( $data ); |
| 81 |
|
| 82 |
$graph[] = $data; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Adds the steps to our How-To output. |
| 87 |
* |
| 88 |
* @param array $data Our How-To schema data. |
| 89 |
* @param array $steps Our How-To block's steps. |
| 90 |
*/ |
| 91 |
private function add_steps( &$data, $steps ) { |
| 92 |
foreach ( $steps as $step ) { |
| 93 |
$schema_id = $this->stable->context->canonical . '#' . esc_attr( $step['id'] ); |
| 94 |
$schema_step = [ |
| 95 |
'@type' => 'HowToStep', |
| 96 |
'url' => $schema_id, |
| 97 |
]; |
| 98 |
|
| 99 |
if ( isset( $step['jsonText'] ) ) { |
| 100 |
$json_text = $this->helpers->schema->html->sanitize( $step['jsonText'] ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( isset( $step['jsonName'] ) ) { |
| 104 |
$json_name = $this->helpers->schema->html->smart_strip_tags( $step['jsonName'] ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( empty( $json_name ) ) { |
| 108 |
if ( empty( $step['text'] ) ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
$schema_step['text'] = ''; |
| 113 |
|
| 114 |
$this->add_step_image( $schema_step, $step ); |
| 115 |
|
| 116 |
// If there is no text and no image, don't output the step. |
| 117 |
if ( empty( $json_text ) && empty( $schema_step['image'] ) ) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
if ( ! empty( $json_text ) ) { |
| 122 |
$schema_step['text'] = $json_text; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
elseif ( empty( $json_text ) ) { |
| 127 |
$schema_step['text'] = $json_name; |
| 128 |
} |
| 129 |
else { |
| 130 |
$schema_step['name'] = $json_name; |
| 131 |
|
| 132 |
$schema_step['itemListElement'] = [ |
| 133 |
[ |
| 134 |
'@type' => 'HowToDirection', |
| 135 |
'text' => $json_text, |
| 136 |
], |
| 137 |
]; |
| 138 |
|
| 139 |
$this->add_step_description( $schema_step, $json_text ); |
| 140 |
$this->add_step_image( $schema_step, $step ); |
| 141 |
} |
| 142 |
|
| 143 |
$data['step'][] = $schema_step; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Adds the duration of the task to the Schema. |
| 149 |
* |
| 150 |
* @param array $data Our How-To schema data. |
| 151 |
* @param array $attributes The block data attributes. |
| 152 |
*/ |
| 153 |
private function add_duration( &$data, $attributes ) { |
| 154 |
if ( empty( $attributes['hasDuration'] ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
$days = empty( $attributes['days'] ) ? 0 : $attributes['days']; |
| 159 |
$hours = empty( $attributes['hours'] ) ? 0 : $attributes['hours']; |
| 160 |
$minutes = empty( $attributes['minutes'] ) ? 0 : $attributes['minutes']; |
| 161 |
|
| 162 |
if ( ( $days + $hours + $minutes ) > 0 ) { |
| 163 |
$data['totalTime'] = esc_attr( 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M' ); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Determines whether we're part of an article or a webpage. |
| 169 |
* |
| 170 |
* @deprecated 14.0 |
| 171 |
* @codeCoverageIgnore |
| 172 |
* |
| 173 |
* @return string A reference URL. |
| 174 |
*/ |
| 175 |
protected function get_main_schema_id() { |
| 176 |
_deprecated_function( __METHOD__, 'WPSEO 14.0' ); |
| 177 |
|
| 178 |
return $this->stable->context->main_schema_id; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Checks if we have a step description, if we do, add it. |
| 183 |
* |
| 184 |
* @param array $schema_step Our Schema output for the Step. |
| 185 |
* @param string $json_text The step text. |
| 186 |
*/ |
| 187 |
private function add_step_description( &$schema_step, $json_text ) { |
| 188 |
$schema_step['itemListElement'] = [ |
| 189 |
[ |
| 190 |
'@type' => 'HowToDirection', |
| 191 |
'text' => $json_text, |
| 192 |
], |
| 193 |
]; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Checks if we have a step image, if we do, add it. |
| 198 |
* |
| 199 |
* @param array $schema_step Our Schema output for the Step. |
| 200 |
* @param array $step The step block data. |
| 201 |
*/ |
| 202 |
private function add_step_image( &$schema_step, $step ) { |
| 203 |
foreach ( $step['text'] as $line ) { |
| 204 |
if ( is_array( $line ) && isset( $line['type'] ) && $line['type'] === 'img' ) { |
| 205 |
$schema_step['image'] = $this->get_image_schema( esc_url( $line['props']['src'] ) ); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Generates the image schema from the attachment $url. |
| 212 |
* |
| 213 |
* @deprecated 14.0 |
| 214 |
* @codeCoverageIgnore |
| 215 |
* |
| 216 |
* @param string $url Attachment url. |
| 217 |
* |
| 218 |
* @return array Image schema. |
| 219 |
*/ |
| 220 |
protected function get_image_schema( $url ) { |
| 221 |
_deprecated_function( __METHOD__, 'WPSEO 14.0', 'Yoast\WP\SEO\Generators\Schema\HowTo::get_image_schema' ); |
| 222 |
|
| 223 |
$schema_id = $this->stable->context->canonical . '#schema-image-' . md5( $url ); |
| 224 |
|
| 225 |
return $this->helpers->schema->image->generate_from_url( $schema_id, $url ); |
| 226 |
} |
| 227 |
} |
| 228 |
|