Skip to content

Scoped Bootstrapping

Performance is a key consideration in Xe Plugin. The Xe_Plugin\Bootstrap class ensures that services, hooks, and views are only loaded in the execution context where they are needed.


Service Scopes

Services are separated into three arrays:

namespace Xe_Plugin;

class Bootstrap {

  /**
   * Loaded on EVERY request (Global scope)
   */
  protected array $global = [
    Setup::class,
    Endpoints::class,
    PageTemplates::class,
    PostTypes::class,
    Taxonomies::class,
    Ajax::class,
    Elementor::class
  ];

  /**
   * Loaded ONLY when in WordPress Admin (is_admin() === true)
   */
  protected array $admin = [
    AdminAssets::class,
    AdminViews::class,
    MenuPages::class
  ];

  /**
   * Loaded on Frontend, AJAX, or REST requests
   */
  protected array $frontend = [
    FrontendAssets::class,
    FrontendViews::class,
    Shortcodes::class
  ];

}

How Registration Works

When WordPress triggers plugins_loaded, _xe_plugin_bootstrap() calls $bootstrap->register():

public function register(): void {

  // 1. Always load global services
  $this->load_services( $this->global );

  // 2. Load admin services only in WP Admin
  if ( is_admin() ) {
    $this->load_services( $this->admin );
  }

  // 3. Load frontend services on public requests, AJAX, or REST
  if ( $this->is_render_request() ) {
    $this->load_services( $this->frontend );
  }

}

The load_services() method iterates over each class, creates a new instance, and automatically invokes its register() method if defined:

protected function load_services( array $services ): void {
  foreach ( $services as $service ) {
    $instance = new $service();
    if ( method_exists( $instance, 'register' ) ) {
      $instance->register();
    }
  }
}

Adding Your Own Services

To add a new service:

  1. Create your class in the appropriate folder (e.g. src/MyFeature.php or src/Admin/MyAdminFeature.php).
  2. Add a register() method containing your WordPress action and filter hooks:
    namespace Xe_Plugin;
    
    class MyFeature {
      public function register(): void {
        add_action( 'init', [ $this, 'init_feature' ] );
      }
    
      public function init_feature(): void {
        // Your feature initialization...
      }
    }
    
  3. Register the class name in src/Bootstrap.php within $global, $admin, or $frontend.