| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO; |
| 4 | 4 | |
| 5 | +use Throwable; | |
| 5 | 6 | use WP_CLI; |
| 6 | 7 | use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface; |
| 7 | 8 | |
| 8 | 9 | /** |
| @@ -63,64 +64,64 @@ | ||
| 63 | 64 | |
| 64 | 65 | /** |
| 65 | 66 | * Registers an integration. |
| 66 | 67 | * |
| 67 | - * @param string $class The class name of the integration to be loaded. | |
| 68 | + * @param string $integration_class The class name of the integration to be loaded. | |
| 68 | 69 | * |
| 69 | 70 | * @return void |
| 70 | 71 | */ |
| 71 | - public function register_integration( $class ) { | |
| 72 | - $this->integrations[] = $class; | |
| 72 | + public function register_integration( $integration_class ) { | |
| 73 | + $this->integrations[] = $integration_class; | |
| 73 | 74 | } |
| 74 | 75 | |
| 75 | 76 | /** |
| 76 | 77 | * Registers an initializer. |
| 77 | 78 | * |
| 78 | - * @param string $class The class name of the initializer to be loaded. | |
| 79 | + * @param string $initializer_class The class name of the initializer to be loaded. | |
| 79 | 80 | * |
| 80 | 81 | * @return void |
| 81 | 82 | */ |
| 82 | - public function register_initializer( $class ) { | |
| 83 | - $this->initializers[] = $class; | |
| 83 | + public function register_initializer( $initializer_class ) { | |
| 84 | + $this->initializers[] = $initializer_class; | |
| 84 | 85 | } |
| 85 | 86 | |
| 86 | 87 | /** |
| 87 | 88 | * Registers a route. |
| 88 | 89 | * |
| 89 | - * @param string $class The class name of the route to be loaded. | |
| 90 | + * @param string $route_class The class name of the route to be loaded. | |
| 90 | 91 | * |
| 91 | 92 | * @return void |
| 92 | 93 | */ |
| 93 | - public function register_route( $class ) { | |
| 94 | - $this->routes[] = $class; | |
| 94 | + public function register_route( $route_class ) { | |
| 95 | + $this->routes[] = $route_class; | |
| 95 | 96 | } |
| 96 | 97 | |
| 97 | 98 | /** |
| 98 | 99 | * Registers a command. |
| 99 | 100 | * |
| 100 | - * @param string $class The class name of the command to be loaded. | |
| 101 | + * @param string $command_class The class name of the command to be loaded. | |
| 101 | 102 | * |
| 102 | 103 | * @return void |
| 103 | 104 | */ |
| 104 | - public function register_command( $class ) { | |
| 105 | - $this->commands[] = $class; | |
| 105 | + public function register_command( $command_class ) { | |
| 106 | + $this->commands[] = $command_class; | |
| 106 | 107 | } |
| 107 | 108 | |
| 108 | 109 | /** |
| 109 | 110 | * Registers a migration. |
| 110 | 111 | * |
| 111 | - * @param string $plugin The plugin the migration belongs to. | |
| 112 | - * @param string $version The version of the migration. | |
| 113 | - * @param string $class The class name of the migration to be loaded. | |
| 112 | + * @param string $plugin The plugin the migration belongs to. | |
| 113 | + * @param string $version The version of the migration. | |
| 114 | + * @param string $migration_class The class name of the migration to be loaded. | |
| 114 | 115 | * |
| 115 | 116 | * @return void |
| 116 | 117 | */ |
| 117 | - public function register_migration( $plugin, $version, $class ) { | |
| 118 | + public function register_migration( $plugin, $version, $migration_class ) { | |
| 118 | 119 | if ( ! \array_key_exists( $plugin, $this->migrations ) ) { |
| 119 | 120 | $this->migrations[ $plugin ] = []; |
| 120 | 121 | } |
| 121 | 122 | |
| 122 | - $this->migrations[ $plugin ][ $version ] = $class; | |
| 123 | + $this->migrations[ $plugin ][ $version ] = $migration_class; | |
| 123 | 124 | } |
| 124 | 125 | |
| 125 | 126 | /** |
| 126 | 127 | * Loads all registered classes if their conditionals are met. |
| @@ -161,14 +162,27 @@ | ||
| 161 | 162 | |
| 162 | 163 | /** |
| 163 | 164 | * Loads all registered commands. |
| 164 | 165 | * |
| 166 | + * Commands that opt in to conditional loading by implementing | |
| 167 | + * Loadable_Interface are skipped when their conditionals are not met. | |
| 168 | + * | |
| 165 | 169 | * @return void |
| 166 | 170 | */ |
| 167 | 171 | protected function load_commands() { |
| 168 | 172 | foreach ( $this->commands as $class ) { |
| 169 | - $command = $this->container->get( $class ); | |
| 173 | + if ( \is_subclass_of( $class, Loadable_Interface::class ) | |
| 174 | + && ! $this->conditionals_are_met( $class ) | |
| 175 | + ) { | |
| 176 | + continue; | |
| 177 | + } | |
| 170 | 178 | |
| 179 | + $command = $this->get_class( $class ); | |
| 180 | + | |
| 181 | + if ( $command === null ) { | |
| 182 | + continue; | |
| 183 | + } | |
| 184 | + | |
| 171 | 185 | WP_CLI::add_command( $class::get_namespace(), $command ); |
| 172 | 186 | } |
| 173 | 187 | } |
| 174 | 188 | |
| @@ -182,9 +196,15 @@ | ||
| 182 | 196 | if ( ! $this->conditionals_are_met( $class ) ) { |
| 183 | 197 | continue; |
| 184 | 198 | } |
| 185 | 199 | |
| 186 | - $this->container->get( $class )->initialize(); | |
| 200 | + $initializer = $this->get_class( $class ); | |
| 201 | + | |
| 202 | + if ( $initializer === null ) { | |
| 203 | + continue; | |
| 204 | + } | |
| 205 | + | |
| 206 | + $initializer->initialize(); | |
| 187 | 207 | } |
| 188 | 208 | } |
| 189 | 209 | |
| 190 | 210 | /** |
| @@ -197,9 +217,15 @@ | ||
| 197 | 217 | if ( ! $this->conditionals_are_met( $class ) ) { |
| 198 | 218 | continue; |
| 199 | 219 | } |
| 200 | 220 | |
| 201 | - $this->container->get( $class )->register_hooks(); | |
| 221 | + $integration = $this->get_class( $class ); | |
| 222 | + | |
| 223 | + if ( $integration === null ) { | |
| 224 | + continue; | |
| 225 | + } | |
| 226 | + | |
| 227 | + $integration->register_hooks(); | |
| 202 | 228 | } |
| 203 | 229 | } |
| 204 | 230 | |
| 205 | 231 | /** |
| @@ -212,26 +238,74 @@ | ||
| 212 | 238 | if ( ! $this->conditionals_are_met( $class ) ) { |
| 213 | 239 | continue; |
| 214 | 240 | } |
| 215 | 241 | |
| 216 | - $this->container->get( $class )->register_routes(); | |
| 242 | + $route = $this->get_class( $class ); | |
| 243 | + | |
| 244 | + if ( $route === null ) { | |
| 245 | + continue; | |
| 246 | + } | |
| 247 | + | |
| 248 | + $route->register_routes(); | |
| 217 | 249 | } |
| 218 | 250 | } |
| 219 | 251 | |
| 220 | 252 | /** |
| 221 | - * Checks if all conditionals of a given integration are met. | |
| 253 | + * Checks if all conditionals of a given loadable are met. | |
| 222 | 254 | * |
| 223 | - * @param Loadable_Interface $class The class name of the integration. | |
| 255 | + * @param string $loadable_class The class name of the loadable. | |
| 224 | 256 | * |
| 225 | - * @return bool Whether or not all conditionals of the integration are met. | |
| 257 | + * @return bool Whether all conditionals of the loadable are met. | |
| 226 | 258 | */ |
| 227 | - protected function conditionals_are_met( $class ) { | |
| 228 | - $conditionals = $class::get_conditionals(); | |
| 229 | - foreach ( $conditionals as $conditional ) { | |
| 230 | - if ( ! $this->container->get( $conditional )->is_met() ) { | |
| 259 | + protected function conditionals_are_met( $loadable_class ) { | |
| 260 | + // In production environments do not fatal if the class does not exist but log and fail gracefully. | |
| 261 | + if ( \YOAST_ENVIRONMENT === 'production' && ! \class_exists( $loadable_class ) ) { | |
| 262 | + if ( \defined( 'WP_DEBUG' ) && \WP_DEBUG ) { | |
| 263 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 264 | + \error_log( | |
| 265 | + \sprintf( | |
| 266 | + /* translators: %1$s expands to Yoast SEO, %2$s expands to the name of the class that could not be found. */ | |
| 267 | + \__( '%1$s attempted to load the class %2$s but it could not be found.', 'wordpress-seo' ), | |
| 268 | + 'Yoast SEO', | |
| 269 | + $loadable_class, | |
| 270 | + ), | |
| 271 | + ); | |
| 272 | + } | |
| 273 | + return false; | |
| 274 | + } | |
| 275 | + | |
| 276 | + $conditionals = $loadable_class::get_conditionals(); | |
| 277 | + foreach ( $conditionals as $class ) { | |
| 278 | + $conditional = $this->get_class( $class ); | |
| 279 | + if ( $conditional === null || ! $conditional->is_met() ) { | |
| 231 | 280 | return false; |
| 232 | 281 | } |
| 233 | 282 | } |
| 234 | 283 | |
| 235 | 284 | return true; |
| 285 | + } | |
| 286 | + | |
| 287 | + /** | |
| 288 | + * Gets a class from the container. | |
| 289 | + * | |
| 290 | + * @param string $class_name The class name. | |
| 291 | + * | |
| 292 | + * @return object|null The class or, in production environments, null if it does not exist. | |
| 293 | + * | |
| 294 | + * @throws Throwable If the class does not exist in development environments. | |
| 295 | + */ | |
| 296 | + protected function get_class( $class_name ) { | |
| 297 | + try { | |
| 298 | + return $this->container->get( $class_name ); | |
| 299 | + } catch ( Throwable $e ) { | |
| 300 | + // In production environments do not fatal if the class could not be constructed but log and fail gracefully. | |
| 301 | + if ( \YOAST_ENVIRONMENT === 'production' ) { | |
| 302 | + if ( \defined( 'WP_DEBUG' ) && \WP_DEBUG ) { | |
| 303 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 304 | + \error_log( $e->getMessage() ); | |
| 305 | + } | |
| 306 | + return null; | |
| 307 | + } | |
| 308 | + throw $e; | |
| 309 | + } | |
| 236 | 310 | } |
| 237 | 311 | } |