class-schema-faq-question-list.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns a question object for each question in an FAQ block.
  9. *
  10. * @since 11.1
  11. *
  12. * @property array $data The Schema array.
  13. * @property WP_Block_Parser_Block[] $blocks The block we're taking the questions out of.
  14. * @property WPSEO_Schema_Context context A value object with context variables.
  15. * @property array ids
  16. * @property int count
  17. */
  18. class WPSEO_Schema_FAQ_Question_List {
  19. /**
  20. * The Schema array.
  21. *
  22. * @var array
  23. */
  24. private $data = [];
  25. /**
  26. * All the blocks of this block-type.
  27. *
  28. * @var WP_Block_Parser_Block
  29. */
  30. private $blocks;
  31. /**
  32. * Number of questions on the page.
  33. *
  34. * @var int
  35. */
  36. private $count;
  37. /**
  38. * IDs of the questions on the page.
  39. *
  40. * @var array
  41. */
  42. private $ids;
  43. /**
  44. * WPSEO_Schema_FAQ_Question_List constructor.
  45. *
  46. * @param WP_Block_Parser_Block[] $blocks An array of the FAQ blocks on this page.
  47. * @param WPSEO_Schema_Context $context A value object with context variables.
  48. */
  49. public function __construct( $blocks, $context ) {
  50. $this->blocks = $blocks;
  51. $this->context = $context;
  52. $this->count = 1;
  53. }
  54. /**
  55. * Find an image based on its URL and generate a Schema object for it.
  56. *
  57. * @return array The Schema with a question list added.
  58. */
  59. public function generate() {
  60. $this->prepare_blocks();
  61. $this->data[] = [
  62. '@type' => 'ItemList',
  63. 'mainEntityOfPage' => [ '@id' => $this->get_schema_id() ],
  64. 'numberOfItems' => $this->count,
  65. 'itemListElement' => $this->ids,
  66. ];
  67. return $this->data;
  68. }
  69. /**
  70. * Determine whether we're part of an article or a webpage.
  71. *
  72. * @return string A reference URL.
  73. */
  74. private function get_schema_id() {
  75. if ( $this->context->site_represents !== false && WPSEO_Schema_Article::is_article_post_type() ) {
  76. return $this->context->canonical . WPSEO_Schema_IDs::ARTICLE_HASH;
  77. }
  78. return $this->context->canonical . WPSEO_Schema_IDs::WEBPAGE_HASH;
  79. }
  80. /**
  81. * Loop through the blocks of our type.
  82. */
  83. private function prepare_blocks() {
  84. foreach ( $this->blocks as $block ) {
  85. $this->prepare_questions( $block );
  86. }
  87. }
  88. /**
  89. * Prepare our data.
  90. *
  91. * @param WP_Block_Parser_Block[] $block The block to prepare the questions for.
  92. */
  93. private function prepare_questions( $block ) {
  94. foreach ( $block['attrs']['questions'] as $question ) {
  95. if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
  96. continue;
  97. }
  98. $this->count ++;
  99. $this->ids[] = [ '@id' => $this->context->canonical . '#' . esc_attr( $question['id'] ) ];
  100. }
  101. }
  102. }