class-schema-faq.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns schema FAQ data.
  9. *
  10. * @since 11.3
  11. */
  12. class WPSEO_Schema_FAQ implements WPSEO_Graph_Piece {
  13. /**
  14. * Determine whether this graph piece is needed or not.
  15. *
  16. * @var bool
  17. */
  18. private $is_needed = false;
  19. /**
  20. * The FAQ blocks on the current page.
  21. *
  22. * @var array
  23. */
  24. private $blocks;
  25. /**
  26. * A value object with context variables.
  27. *
  28. * @var WPSEO_Schema_Context
  29. */
  30. private $context;
  31. /**
  32. * WPSEO_Schema_FAQ constructor.
  33. *
  34. * @param WPSEO_Schema_Context $context A value object with context variables.
  35. */
  36. public function __construct( WPSEO_Schema_Context $context ) {
  37. $this->context = $context;
  38. add_action( 'wpseo_pre_schema_block_type_yoast/faq-block', [ $this, 'prepare_schema' ], 10, 1 );
  39. add_filter( 'wpseo_schema_block_yoast/faq-block', [ $this, 'render_schema_questions' ], 10, 3 );
  40. }
  41. /**
  42. * If this fires, we know there's an FAQ block ont he page, so filter the page type.
  43. *
  44. * @param array $blocks The blocks of this type on the current page.
  45. */
  46. public function prepare_schema( $blocks ) {
  47. $this->blocks = $blocks;
  48. $this->is_needed = true;
  49. add_filter( 'wpseo_schema_webpage_type', [ $this, 'change_schema_page_type' ] );
  50. }
  51. /**
  52. * Change the page type to an array if it isn't one, include FAQPage.
  53. *
  54. * @param array|string $page_type The page type.
  55. *
  56. * @return array $page_type The page type that's now an array.
  57. */
  58. public function change_schema_page_type( $page_type ) {
  59. if ( ! is_array( $page_type ) ) {
  60. $page_type = [ $page_type ];
  61. }
  62. $page_type[] = 'FAQPage';
  63. return $page_type;
  64. }
  65. /**
  66. * Render a list of questions, referencing them by ID.
  67. *
  68. * @return array $data Our Schema graph.
  69. */
  70. public function generate() {
  71. $question_list = new WPSEO_Schema_FAQ_Question_List( $this->blocks, $this->context );
  72. $graph = $question_list->generate();
  73. return $graph;
  74. }
  75. /**
  76. * Add the Questions in our FAQ blocks as separate pieces to the graph.
  77. *
  78. * @param array $graph Schema data for the current page.
  79. * @param WP_Block_Parser_Block $block The block data array.
  80. * @param WPSEO_Schema_Context $context A value object with context variables.
  81. *
  82. * @return array $data Our Schema graph.
  83. */
  84. public function render_schema_questions( $graph, $block, $context ) {
  85. $questions = new WPSEO_Schema_FAQ_Questions( $graph, $block, $context );
  86. $graph = $questions->generate();
  87. return $graph;
  88. }
  89. /**
  90. * Determines whether or not a piece should be added to the graph.
  91. *
  92. * @return bool
  93. */
  94. public function is_needed() {
  95. return $this->is_needed;
  96. }
  97. }