image( $schema_step, $step ) ) { $this->add_step_image_from_content( $schema_step, $step ); } return $schema_step; } /** * Checks if we have an inline image and add it. * * @param array $schema_step Our Schema output for the Step. * @param array $step The step block data. */ private function add_step_image_from_content( &$schema_step, $step ) { // Early Bail. if ( empty( $step['content'] ) || ! Str::contains( '/i', $step['content'], $matches ); if ( ! isset( $matches[1][0] ) || empty( $matches[1][0] ) ) { return; } $schema_image = [ '@type' => 'ImageObject', 'url' => $matches[1][0], ]; $image_id = Attachment::get_by_url( $schema_image['url'] ); if ( $image_id > 0 ) { $this->add_caption( $schema_image, $image_id ); $this->add_image_size( $schema_image, $image_id ); } $schema_step['image'] = $schema_image; } /** * Checks if we have a step image and add it. * * @copyright Copyright (C) 2008-2019, Yoast BV * The following code is a derivative work of the code from the Yoast(https://github.com/Yoast/wordpress-seo/), which is licensed under GPL v3. * * @param array $schema_step Our Schema output for the Step. * @param array $step The step block data. */ private function add_step_image( &$schema_step, $step ) { if ( empty( $step['imageID'] ) ) { return false; } $image_id = absint( $step['imageID'] ); if ( ! ( $image_id > 0 ) ) { return false; } $image_url = wp_get_attachment_image_url( $image_id, 'full' ); if ( ! $image_url ) { return false; } $schema_image = [ '@type' => 'ImageObject', 'url' => $image_url, ]; $this->add_caption( $schema_image, $image_id ); $this->add_image_size( $schema_image, $image_id ); $schema_step['image'] = $schema_image; return true; } /** * Add caption to schema. * * @param array $schema_image Our Schema output for the Image. * @param int $image_id The image ID. */ private function add_caption( &$schema_image, $image_id ) { $caption = wp_get_attachment_caption( $image_id ); if ( ! empty( $caption ) ) { $schema_image['caption'] = $caption; return; } $caption = Attachment::get_alt_tag( $image_id ); if ( ! empty( $caption ) ) { $schema_image['caption'] = $caption; } } /** * Add image size to schema. * * @param array $schema_image Our Schema output for the Image. * @param int $image_id The image ID. */ private function add_image_size( &$schema_image, $image_id ) { $image_meta = wp_get_attachment_metadata( $image_id ); if ( empty( $image_meta['width'] ) || empty( $image_meta['height'] ) ) { return; } $schema_image['width'] = $image_meta['width']; $schema_image['height'] = $image_meta['height']; } /** * Add duration to schema. * * @param array $data Our Schema output. * @param array $attrs The block attributes. */ private function add_duration( &$data, $attrs ) { if ( ! empty( $attrs['hasDuration'] ) ) { $days = absint( $attrs['days'] ?? 0 ); $hours = absint( $attrs['hours'] ?? 0 ); $minutes = absint( $attrs['minutes'] ?? 0 ); if ( ( $days + $hours + $minutes ) > 0 ) { $data['totalTime'] = esc_attr( 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M' ); } } } /** * Generate HowTo duration property. * * @param array $attrs The block attributes. * * @return string */ private function build_duration( $attrs ) { if ( empty( $attrs['hasDuration'] ) ) { return ''; } $days = isset( $attrs['days'] ) ? absint( $attrs['days'] ) : 0; $hours = isset( $attrs['hours'] ) ? absint( $attrs['hours'] ) : 0; $minutes = isset( $attrs['minutes'] ) ? absint( $attrs['minutes'] ) : 0; $elements = []; if ( $days > 0 ) { /* translators: %d is the number of days. */ $elements[] = sprintf( _n( '%d day', '%d days', $days, 'rank-math' ), $days ); } if ( $hours > 0 ) { /* translators: %d is the number of hours. */ $elements[] = sprintf( _n( '%d hour', '%d hours', $hours, 'rank-math' ), $hours ); } if ( $minutes > 0 ) { /* translators: %d is the number of minutes. */ $elements[] = sprintf( _n( '%d minute', '%d minutes', $minutes, 'rank-math' ), $minutes ); } $count = count( $elements ); $formats = [ 1 => '%1$s', /* translators: placeholders are units of time, e.g. '1 hour and 30 minutes' */ 2 => __( '%1$s and %2$s', 'rank-math' ), /* translators: placeholders are units of time, e.g. '1 day, 8 hours and 30 minutes' */ 3 => __( '%1$s, %2$s and %3$s', 'rank-math' ), ]; return sprintf( '
%2$s %1$s
', isset( $formats[ $count ] ) ? vsprintf( $formats[ $count ], $elements ) : '', empty( $attrs['timeLabel'] ) ? __( 'Total Time:', 'rank-math' ) : esc_html( $attrs['timeLabel'] ) ); } /** * Function to check the HowTo block have steps data. * * @param array $attributes Array of attributes. * * @return boolean */ private function has_steps( $attributes ) { return ! isset( $attributes['steps'] ) || empty( $attributes['steps'] ) ? false : true; } }