PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / schema-blocks.php

schema-blocks.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at src/integrations/schema-blocks.php

165 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations;
4
5 use WPSEO_Admin_Asset_Manager;
6 use WPSEO_Admin_Asset_Yoast_Components_L10n;
7 use Yoast\WP\SEO\Conditionals\Schema_Blocks_Conditional;
8 use Yoast\WP\SEO\Helpers\Short_Link_Helper;
9
10 /**
11 * Loads schema block templates into Gutenberg.
12 */
13 class Schema_Blocks implements Integration_Interface {
14
15 /**
16 * The registered templates.
17 *
18 * @var string[]
19 */
20 protected $templates = [];
21
22 /**
23 * Contains the asset manager.
24 *
25 * @var WPSEO_Admin_Asset_Manager
26 */
27 protected $asset_manager;
28
29 /**
30 * Represents the schema blocks conditional.
31 *
32 * @var Schema_Blocks_Conditional
33 */
34 protected $blocks_conditional;
35
36 /**
37 * Represents the short link helper.
38 *
39 * @var Short_Link_Helper
40 */
41 protected $short_link_helper;
42
43 /**
44 * Returns the conditionals based on which this loadable should be active.
45 *
46 * @return array
47 */
48 public static function get_conditionals() {
49 return [
50 Schema_Blocks_Conditional::class,
51 ];
52 }
53
54 /**
55 * Schema_Blocks constructor.
56 *
57 * @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
58 * @param Schema_Blocks_Conditional $blocks_conditional The schema blocks conditional.
59 * @param Short_Link_Helper $short_link_helper The short link helper.
60 */
61 public function __construct(
62 WPSEO_Admin_Asset_Manager $asset_manager,
63 Schema_Blocks_Conditional $blocks_conditional,
64 Short_Link_Helper $short_link_helper
65 ) {
66 $this->asset_manager = $asset_manager;
67 $this->blocks_conditional = $blocks_conditional;
68 $this->short_link_helper = $short_link_helper;
69 }
70
71 /**
72 * Initializes the integration.
73 *
74 * This is the place to register hooks and filters.
75 *
76 * @return void
77 */
78 public function register_hooks() {
79 \add_action( 'enqueue_block_editor_assets', [ $this, 'load' ] );
80 \add_action( 'enqueue_block_editor_assets', [ $this, 'load_translations' ] );
81 \add_action( 'admin_enqueue_scripts', [ $this, 'output' ] );
82 }
83
84 /**
85 * Registers a schema template.
86 *
87 * @param string $template The template to be registered.
88 * If starting with a / is assumed to be an absolute path.
89 * If not starting with a / is assumed to be relative to WPSEO_PATH.
90 *
91 * @return void
92 */
93 public function register_template( $template ) {
94 if ( \substr( $template, 0, 1 ) !== '/' ) {
95 $template = \WPSEO_PATH . '/' . $template;
96 }
97
98 $this->templates[] = $template;
99 }
100
101 /**
102 * Loads all schema block templates and the required JS library for them.
103 *
104 * @return void
105 */
106 public function load() {
107 $this->asset_manager->enqueue_script( 'schema-blocks' );
108 $this->asset_manager->enqueue_style( 'schema-blocks' );
109
110 $this->asset_manager->localize_script(
111 'schema-blocks',
112 'yoastSchemaBlocks',
113 [
114 'requiredLink' => $this->short_link_helper->build( 'https://yoa.st/required-fields' ),
115 'recommendedLink' => $this->short_link_helper->build( 'https://yoa.st/recommended-fields' ),
116 ]
117 );
118 }
119
120 /**
121 * Outputs the set templates.
122 */
123 public function output() {
124 if ( ! $this->asset_manager->is_script_enqueued( 'schema-blocks' ) ) {
125 return;
126 }
127
128 $templates = [];
129
130 // When the schema blocks feature flag is enabled, use the registered templates.
131 if ( $this->blocks_conditional->is_met() ) {
132 $templates = $this->templates;
133 }
134
135 /**
136 * Filter: 'wpseo_load_schema_templates' - Allow adding additional schema templates.
137 *
138 * @param array $templates The templates to filter.
139 */
140 $templates = \apply_filters( 'wpseo_load_schema_templates', $templates );
141 if ( ! \is_array( $templates ) || empty( $templates ) ) {
142 return;
143 }
144
145 foreach ( $templates as $template ) {
146 if ( ! \file_exists( $template ) ) {
147 continue;
148 }
149 // `.schema` and other suffixes become Schema (root) templates.
150 $type = ( \substr( $template, -10 ) === '.block.php' ) ? 'block' : 'schema';
151 echo '<script type="text/' . \esc_html( $type ) . '-template">';
152 include $template;
153 echo '</script>';
154 }
155 }
156
157 /**
158 * Loads the translations and localizes the schema-blocks script file.
159 */
160 public function load_translations() {
161 $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
162 $yoast_components_l10n->localize_script( 'schema-blocks' );
163 }
164 }
165