class-how-to-block.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Structured_Data_Blocks
  6. */
  7. /**
  8. * Class WPSEO_How_To_Block
  9. */
  10. class WPSEO_How_To_Block implements WPSEO_WordPress_Integration {
  11. /**
  12. * Registers the how-to block as a server-side rendered block.
  13. *
  14. * @return void
  15. */
  16. public function register_hooks() {
  17. if ( ! function_exists( 'register_block_type' ) ) {
  18. return;
  19. }
  20. register_block_type( 'yoast/how-to-block', array(
  21. 'render_callback' => array( $this, 'render' ),
  22. ) );
  23. }
  24. /**
  25. * Renders the block.
  26. *
  27. * Because we can't save script tags in Gutenberg without sufficient user permissions, we render these server-side.
  28. *
  29. * @param array $attributes The attributes of the block.
  30. * @param string $content The HTML content of the block.
  31. *
  32. * @return string The block preceded by its JSON-LD script.
  33. */
  34. public function render( $attributes, $content ) {
  35. if ( ! is_array( $attributes ) || ! is_singular() ) {
  36. return $content;
  37. }
  38. $json_ld = $this->get_json_ld( $attributes );
  39. return '<script type="application/ld+json">' . wp_json_encode( $json_ld ) . '</script>' . $content;
  40. }
  41. /**
  42. * Returns the JSON-LD for a how-to block.
  43. *
  44. * @param array $attributes The attributes of the how-to block.
  45. *
  46. * @return array The JSON-LD representation of the how-to block.
  47. */
  48. protected function get_json_ld( array $attributes ) {
  49. $json_ld = array(
  50. '@context' => 'https://schema.org',
  51. '@type' => 'HowTo',
  52. );
  53. $post_title = get_the_title();
  54. if ( ! empty( $post_title ) ) {
  55. $json_ld['name'] = $post_title;
  56. }
  57. if ( ! empty( $attributes['hasDuration'] ) && $attributes['hasDuration'] === true ) {
  58. $days = empty( $attributes['days'] ) ? 0 : $attributes['days'];
  59. $hours = empty( $attributes['hours'] ) ? 0 : $attributes['hours'];
  60. $minutes = empty( $attributes['minutes'] ) ? 0 : $attributes['minutes'];
  61. if ( ( $days + $hours + $minutes ) > 0 ) {
  62. $json_ld['totalTime'] = 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M';
  63. }
  64. }
  65. if ( ! empty( $attributes['jsonDescription'] ) ) {
  66. $json_ld['description'] = $attributes['jsonDescription'];
  67. }
  68. if ( ! empty( $attributes['steps'] ) && is_array( $attributes['steps'] ) ) {
  69. $json_ld['step'] = array();
  70. $steps = array_filter( $attributes['steps'], 'is_array' );
  71. foreach ( $steps as $step ) {
  72. $json_ld['step'][] = $this->get_section_json_ld( $step );
  73. }
  74. }
  75. return $json_ld;
  76. }
  77. /**
  78. * Returns the JSON-LD for a step-section in a how-to block.
  79. *
  80. * @param array $step The attributes of a step-section in the how-to block.
  81. *
  82. * @return array The JSON-LD representation of the step-section in a how-to block.
  83. */
  84. protected function get_section_json_ld( array $step ) {
  85. $section_json_ld = array(
  86. '@type' => 'HowToSection',
  87. 'itemListElement' => $this->get_step_json_ld( $step ),
  88. );
  89. if ( ! empty( $step['jsonName'] ) ) {
  90. $section_json_ld['name'] = $step['jsonName'];
  91. }
  92. if ( ! empty( $step['jsonImageSrc'] ) ) {
  93. $section_json_ld['image'] = array(
  94. '@type' => 'ImageObject',
  95. 'contentUrl' => $step['jsonImageSrc'],
  96. );
  97. }
  98. return $section_json_ld;
  99. }
  100. /**
  101. * Returns the JSON-LD for a step's description in a how-to block.
  102. *
  103. * @param array $step The attributes of a step(-section) in the how-to block.
  104. *
  105. * @return array The JSON-LD representation of the step's description in a how-to block.
  106. */
  107. protected function get_step_json_ld( array $step ) {
  108. $step_json_ld = array(
  109. '@type' => 'HowToStep',
  110. );
  111. if ( ! empty( $step['jsonText'] ) ) {
  112. $step_json_ld['text'] = $step['jsonText'];
  113. }
  114. return $step_json_ld;
  115. }
  116. }