blocks = $blocks; $this->context = $context; $this->count = 1; } /** * Find an image based on its URL and generate a Schema object for it. * * @return array The Schema with a question list added. */ public function generate() { $this->prepare_blocks(); $this->data[] = [ '@type' => 'ItemList', 'mainEntityOfPage' => [ '@id' => $this->get_schema_id() ], 'numberOfItems' => $this->count, 'itemListElement' => $this->ids, ]; return $this->data; } /** * Determine whether we're part of an article or a webpage. * * @return string A reference URL. */ private function get_schema_id() { if ( $this->context->site_represents !== false && WPSEO_Schema_Article::is_article_post_type() ) { return $this->context->canonical . WPSEO_Schema_IDs::ARTICLE_HASH; } return $this->context->canonical . WPSEO_Schema_IDs::WEBPAGE_HASH; } /** * Loop through the blocks of our type. */ private function prepare_blocks() { foreach ( $this->blocks as $block ) { $this->prepare_questions( $block ); } } /** * Prepare our data. * * @param WP_Block_Parser_Block[] $block The block to prepare the questions for. */ private function prepare_questions( $block ) { foreach ( $block['attrs']['questions'] as $question ) { if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) { continue; } $this->count ++; $this->ids[] = [ '@id' => $this->context->canonical . '#' . esc_attr( $question['id'] ) ]; } } }