class-structured-data-blocks.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class to load assets required for structured data blocks.
  9. */
  10. class WPSEO_Structured_Data_Blocks implements WPSEO_WordPress_Integration {
  11. /**
  12. * An instance of the WPSEO_Admin_Asset_Manager class.
  13. *
  14. * @var WPSEO_Admin_Asset_Manager
  15. */
  16. protected $asset_manager;
  17. /**
  18. * Registers hooks for Structured Data Blocks with WordPress.
  19. */
  20. public function register_hooks() {
  21. add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
  22. add_filter( 'block_categories', [ $this, 'add_block_category' ] );
  23. }
  24. /**
  25. * Checks whether the Structured Data Blocks are disabled.
  26. *
  27. * @return boolean
  28. */
  29. private function check_enabled() {
  30. /**
  31. * Filter: 'wpseo_enable_structured_data_blocks' - Allows disabling Yoast's schema blocks entirely.
  32. *
  33. * @api bool If false, our structured data blocks won't show.
  34. */
  35. $enabled = apply_filters( 'wpseo_enable_structured_data_blocks', true );
  36. return $enabled;
  37. }
  38. /**
  39. * Enqueue Gutenberg block assets for backend editor.
  40. */
  41. public function enqueue_block_editor_assets() {
  42. if ( ! $this->check_enabled() ) {
  43. return;
  44. }
  45. if ( ! $this->asset_manager ) {
  46. $this->asset_manager = new WPSEO_Admin_Asset_Manager();
  47. }
  48. $this->asset_manager->enqueue_script( 'structured-data-blocks' );
  49. $this->asset_manager->enqueue_style( 'structured-data-blocks' );
  50. }
  51. /**
  52. * Adds the structured data blocks category to the Gutenberg categories.
  53. *
  54. * @param array $categories The current categories.
  55. *
  56. * @return array The updated categories.
  57. */
  58. public function add_block_category( $categories ) {
  59. if ( $this->check_enabled() ) {
  60. $categories[] = [
  61. 'slug' => 'yoast-structured-data-blocks',
  62. 'title' => sprintf(
  63. /* translators: %1$s expands to Yoast. */
  64. __( '%1$s Structured Data Blocks', 'wordpress-seo' ),
  65. 'Yoast'
  66. ),
  67. ];
  68. }
  69. return $categories;
  70. }
  71. }