PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.3
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.3, at src/integrations/schema-blocks.php

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