*/ namespace RankMath\Schema; use WP_Block_Type_Registry; use RankMath\Traits\Hooker; use RankMath\Paper\Paper; use RankMath\Helpers\Attachment; use RankMath\Helpers\Str; defined( 'ABSPATH' ) || exit; /** * HowTo Block class. */ class Block_HowTo extends Block { use Hooker; /** * Block type name. * * @var string */ private $block_type = 'rank-math/howto-block'; /** * The single instance of the class. * * @var Block_HowTo */ protected static $instance = null; /** * Retrieve main Block_HowTo instance. * * Ensure only one instance is loaded or can be loaded. * * @return Block_HowTo */ public static function get() { if ( is_null( self::$instance ) && ! ( self::$instance instanceof Block_HowTo ) ) { self::$instance = new Block_HowTo(); } return self::$instance; } /** * The Constructor. */ public function __construct() { if ( WP_Block_Type_Registry::get_instance()->is_registered( $this->block_type ) ) { return; } register_block_type( RANK_MATH_PATH . 'includes/modules/schema/blocks/howto/block.json', [ 'render_callback' => [ $this, 'render' ], ] ); add_filter( 'rank_math/schema/block/howto-block', [ $this, 'add_graph' ], 10, 2 ); } /** * Add HowTO schema data in JSON-LD array.. * * @param array $data Array of JSON-LD data. * @param array $block JsonLD Instance. * * @return array */ public function add_graph( $data, $block ) { // Early bail. if ( ! $this->has_steps( $block['attrs'] ) ) { return $data; } $attrs = $block['attrs']; if ( ! isset( $data['howto'] ) ) { $data['howto'] = [ '@type' => 'HowTo', 'name' => Paper::get()->get_title(), 'description' => isset( $attrs['description'] ) ? $this->clean_text( do_shortcode( $attrs['description'] ) ) : '', 'totalTime' => '', 'step' => [], ]; } $this->add_step_image( $data['howto'], $attrs ); $this->add_duration( $data['howto'], $attrs ); $permalink = get_permalink() . '#'; foreach ( $attrs['steps'] as $index => $step ) { if ( empty( $step['visible'] ) ) { continue; } $schema_step = $this->add_step( $step, $permalink . $step['id'] ); if ( $schema_step ) { $data['howto']['step'][] = $schema_step; } } return $data; } /** * Render block content. * * @param array $attributes Array of atributes. * @return string */ public static function markup( $attributes = [] ) { $list_style = isset( $attributes['listStyle'] ) ? esc_attr( $attributes['listStyle'] ) : ''; $list_css_classes = isset( $attributes['listCssClasses'] ) ? esc_attr( $attributes['listCssClasses'] ) : ''; $title_wrapper = isset( $attributes['titleWrapper'] ) ? esc_attr( $attributes['titleWrapper'] ) : 'h3'; $title_css_classes = isset( $attributes['titleCssClasses'] ) ? esc_attr( $attributes['titleCssClasses'] ) : ''; $content_css_classes = isset( $attributes['contentCssClasses'] ) ? esc_attr( $attributes['contentCssClasses'] ) : ''; $size_slug = isset( $attributes['sizeSlug'] ) ? esc_attr( $attributes['sizeSlug'] ) : ''; $list_tag = self::get()->get_list_style( $list_style ); $item_tag = self::get()->get_list_item_style( $list_style ); $class = 'rank-math-block'; if ( ! empty( $attributes['className'] ) ) { $class .= ' ' . esc_attr( $attributes['className'] ); } // HTML. $out = []; $out[] = 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; } }