use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
* Class that manages loading integrations if and only if all their conditionals are met.
* The registered integrations.
protected $integrations = [];
* The registered integrations.
protected $initializers = [];
* The registered commands.
protected $commands = [];
* The registered migrations.
protected $migrations = [];
* The dependency injection container.
* @var ContainerInterface
* @param ContainerInterface $container The dependency injection container.
public function __construct( ContainerInterface $container ) {
$this->container = $container;
* Registers an integration.
* @param string $class The class name of the integration to be loaded.
public function register_integration( $class ) {
$this->integrations[] = $class;
* Registers an initializer.
* @param string $class The class name of the initializer to be loaded.
public function register_initializer( $class ) {
$this->initializers[] = $class;
* @param string $class The class name of the route to be loaded.
public function register_route( $class ) {
$this->routes[] = $class;
* @param string $class The class name of the command to be loaded.
public function register_command( $class ) {
$this->commands[] = $class;
* @param string $plugin The plugin the migration belongs to.
* @param string $version The version of the migration.
* @param string $class The class name of the migration to be loaded.
public function register_migration( $plugin, $version, $class ) {
if ( ! \array_key_exists( $plugin, $this->migrations ) ) {
$this->migrations[ $plugin ] = [];
$this->migrations[ $plugin ][ $version ] = $class;
* Loads all registered classes if their conditionals are met.
$this->load_initializers();
if ( ! \did_action( 'init' ) ) {
\add_action( 'init', [ $this, 'load_integrations' ] );
$this->load_integrations();
\add_action( 'rest_api_init', [ $this, 'load_routes' ] );
if ( \defined( 'WP_CLI' ) && \WP_CLI ) {
* Returns all registered migrations.
* @param string $plugin The plugin to get the migrations for.
* @return string[]|false The registered migrations. False if no migrations were registered.
public function get_migrations( $plugin ) {
if ( ! \array_key_exists( $plugin, $this->migrations ) ) {
return $this->migrations[ $plugin ];
* Loads all registered commands.
protected function load_commands() {
foreach ( $this->commands as $class ) {
$command = $this->container->get( $class );
WP_CLI::add_command( $class::get_namespace(), $command );
* Loads all registered initializers if their conditionals are met.
protected function load_initializers() {
foreach ( $this->initializers as $class ) {
if ( ! $this->conditionals_are_met( $class ) ) {
$this->container->get( $class )->initialize();
* Loads all registered integrations if their conditionals are met.
public function load_integrations() {
foreach ( $this->integrations as $class ) {
if ( ! $this->conditionals_are_met( $class ) ) {
$this->container->get( $class )->register_hooks();
* Loads all registered routes if their conditionals are met.
public function load_routes() {
foreach ( $this->routes as $class ) {
if ( ! $this->conditionals_are_met( $class ) ) {
$this->container->get( $class )->register_routes();
* Checks if all conditionals of a given integration are met.
* @param Loadable_Interface $class The class name of the integration.
* @return bool Whether or not all conditionals of the integration are met.
protected function conditionals_are_met( $class ) {
$conditionals = $class::get_conditionals();
foreach ( $conditionals as $conditional ) {
if ( ! $this->container->get( $conditional )->is_met() ) {