*/ namespace RankMath\Tools; defined( 'ABSPATH' ) || exit; /** * Yoast_HowTo_Converter class. */ class Yoast_HowTo_Converter { /** * Convert HowTo blocks to Rank Math. * * @param array $block Block to convert. * * @return array */ public function convert( $block ) { $attrs = $block['attrs']; $new_block = [ 'blockName' => 'rank-math/howto-block', 'attrs' => [ 'hasDuration' => isset( $attrs['hasDuration'] ) ? $attrs['hasDuration'] : '', 'days' => isset( $attrs['days'] ) ? $attrs['days'] : '', 'hours' => isset( $attrs['hours'] ) ? $attrs['hours'] : '', 'minutes' => isset( $attrs['minutes'] ) ? $attrs['minutes'] : '', 'description' => isset( $attrs['jsonDescription'] ) ? $attrs['jsonDescription'] : '', 'listStyle' => isset( $attrs['unorderedList'] ) && $attrs['unorderedList'] ? 'ol' : '', 'timeLabel' => isset( $attrs['durationText'] ) ? $attrs['durationText'] : '', 'textAlign' => 'left', 'titleWrapper' => 'h3', 'listCssClasses' => '', 'titleCssClasses' => '', 'contentCssClasses' => '', 'steps' => array_map( [ $this, 'get_step' ], $attrs['steps'] ), 'className' => isset( $attrs['className'] ) ? $attrs['className'] : '', ], ]; $new_block['innerContent'][] = $this->get_html( $new_block['attrs'] ); return $new_block; } /** * Replace block in content. * * @param string $post_content Post content. * @param array $blocks Blocks. * * @return string */ public function replace( $post_content, $blocks ) { preg_match_all( '/.*/iUs', $post_content, $matches ); foreach ( $matches[0] as $index => $match ) { $post_content = \str_replace( $match, $blocks[ $index ], $post_content ); } return $post_content; } /** * Format steps. * * @param array $step Steps. * * @return array */ public function get_step( $step ) { return [ 'id' => uniqid( 'howto-step-' ), 'visible' => true, 'title' => $step['jsonName'], 'content' => $step['jsonText'], ]; } /** * Generate HTML. * * @param array $attributes Block attributes. * * @return string */ private function get_html( $attributes ) { // HTML. $out = [ '
' ]; // Steps. foreach ( $attributes['steps'] as $step ) { if ( empty( $step['visible'] ) ) { continue; } $out[] = '
'; if ( ! empty( $step['title'] ) ) { $out[] = sprintf( '<%1$s class="rank-math-howto-title">%2$s', $attributes['titleWrapper'], $step['title'] ); } if ( ! empty( $step['content'] ) ) { $out[] = sprintf( '
%1$s
', $step['content'] ); } $out[] = '
'; } $out[] = '
'; return join( '', $out ); } }