PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5.1
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 / loader.php

loader.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5.1, at src/loader.php

238 lines 4.9 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;
4
5 use WP_CLI;
6 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
7
8 /**
9 * Class that manages loading integrations if and only if all their conditionals are met.
10 */
11 class Loader {
12
13 /**
14 * The registered integrations.
15 *
16 * @var string[]
17 */
18 protected $integrations = [];
19
20 /**
21 * The registered integrations.
22 *
23 * @var string[]
24 */
25 protected $initializers = [];
26
27 /**
28 * The registered routes.
29 *
30 * @var string[]
31 */
32 protected $routes = [];
33
34 /**
35 * The registered commands.
36 *
37 * @var string[]
38 */
39 protected $commands = [];
40
41 /**
42 * The registered migrations.
43 *
44 * @var string[]
45 */
46 protected $migrations = [];
47
48 /**
49 * The dependency injection container.
50 *
51 * @var ContainerInterface
52 */
53 protected $container;
54
55 /**
56 * Loader constructor.
57 *
58 * @param ContainerInterface $container The dependency injection container.
59 */
60 public function __construct( ContainerInterface $container ) {
61 $this->container = $container;
62 }
63
64 /**
65 * Registers an integration.
66 *
67 * @param string $integration_class The class name of the integration to be loaded.
68 *
69 * @return void
70 */
71 public function register_integration( $integration_class ) {
72 $this->integrations[] = $integration_class;
73 }
74
75 /**
76 * Registers an initializer.
77 *
78 * @param string $initializer_class The class name of the initializer to be loaded.
79 *
80 * @return void
81 */
82 public function register_initializer( $initializer_class ) {
83 $this->initializers[] = $initializer_class;
84 }
85
86 /**
87 * Registers a route.
88 *
89 * @param string $route_class The class name of the route to be loaded.
90 *
91 * @return void
92 */
93 public function register_route( $route_class ) {
94 $this->routes[] = $route_class;
95 }
96
97 /**
98 * Registers a command.
99 *
100 * @param string $command_class The class name of the command to be loaded.
101 *
102 * @return void
103 */
104 public function register_command( $command_class ) {
105 $this->commands[] = $command_class;
106 }
107
108 /**
109 * Registers a migration.
110 *
111 * @param string $plugin The plugin the migration belongs to.
112 * @param string $version The version of the migration.
113 * @param string $migration_class The class name of the migration to be loaded.
114 *
115 * @return void
116 */
117 public function register_migration( $plugin, $version, $migration_class ) {
118 if ( ! \array_key_exists( $plugin, $this->migrations ) ) {
119 $this->migrations[ $plugin ] = [];
120 }
121
122 $this->migrations[ $plugin ][ $version ] = $migration_class;
123 }
124
125 /**
126 * Loads all registered classes if their conditionals are met.
127 *
128 * @return void
129 */
130 public function load() {
131 $this->load_initializers();
132
133 if ( ! \did_action( 'init' ) ) {
134 \add_action( 'init', [ $this, 'load_integrations' ] );
135 }
136 else {
137 $this->load_integrations();
138 }
139
140 \add_action( 'rest_api_init', [ $this, 'load_routes' ] );
141
142 if ( \defined( 'WP_CLI' ) && \WP_CLI ) {
143 $this->load_commands();
144 }
145 }
146
147 /**
148 * Returns all registered migrations.
149 *
150 * @param string $plugin The plugin to get the migrations for.
151 *
152 * @return string[]|false The registered migrations. False if no migrations were registered.
153 */
154 public function get_migrations( $plugin ) {
155 if ( ! \array_key_exists( $plugin, $this->migrations ) ) {
156 return false;
157 }
158
159 return $this->migrations[ $plugin ];
160 }
161
162 /**
163 * Loads all registered commands.
164 *
165 * @return void
166 */
167 protected function load_commands() {
168 foreach ( $this->commands as $class ) {
169 $command = $this->container->get( $class );
170
171 WP_CLI::add_command( $class::get_namespace(), $command );
172 }
173 }
174
175 /**
176 * Loads all registered initializers if their conditionals are met.
177 *
178 * @return void
179 */
180 protected function load_initializers() {
181 foreach ( $this->initializers as $class ) {
182 if ( ! $this->conditionals_are_met( $class ) ) {
183 continue;
184 }
185
186 $this->container->get( $class )->initialize();
187 }
188 }
189
190 /**
191 * Loads all registered integrations if their conditionals are met.
192 *
193 * @return void
194 */
195 public function load_integrations() {
196 foreach ( $this->integrations as $class ) {
197 if ( ! $this->conditionals_are_met( $class ) ) {
198 continue;
199 }
200
201 $this->container->get( $class )->register_hooks();
202 }
203 }
204
205 /**
206 * Loads all registered routes if their conditionals are met.
207 *
208 * @return void
209 */
210 public function load_routes() {
211 foreach ( $this->routes as $class ) {
212 if ( ! $this->conditionals_are_met( $class ) ) {
213 continue;
214 }
215
216 $this->container->get( $class )->register_routes();
217 }
218 }
219
220 /**
221 * Checks if all conditionals of a given integration are met.
222 *
223 * @param Loadable_Interface $integration_class The class name of the integration.
224 *
225 * @return bool Whether or not all conditionals of the integration are met.
226 */
227 protected function conditionals_are_met( $integration_class ) {
228 $conditionals = $integration_class::get_conditionals();
229 foreach ( $conditionals as $conditional ) {
230 if ( ! $this->container->get( $conditional )->is_met() ) {
231 return false;
232 }
233 }
234
235 return true;
236 }
237 }
238