*/ private $services = array(); /** * Booted optional modules, keyed by class name. * * @var array */ private $modules = array(); /** * Importer registry instance, or false when unavailable. * * @var object|false|null */ private $importers = null; /** * Whether `boot()` ran. * * @var bool */ private $booted = false; /** * Whether the spaces were registered. * * @var bool */ private $spaces_registered = false; /** * Whether the REST routes were registered. * * @var bool */ private $rest_registered = false; /** * Returns the singleton, creating and wiring it on first call. * * @return Plugin */ public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); self::$instance->setup(); } return self::$instance; } /** * Private constructor: use `Plugin::instance()` or `foldivo()`. */ private function __construct() {} /** * Schedules the boot, or boots now when `plugins_loaded` already fired (late load). * * @return void */ private function setup() { if ( did_action( 'plugins_loaded' ) ) { $this->boot(); return; } add_action( 'plugins_loaded', array( $this, 'boot' ), 10 ); } /** * Boots core services and optional modules, then fires `foldivo_loaded`. * * @return void */ public function boot() { if ( $this->booted ) { return; } $this->booted = true; $this->maybe_upgrade(); $this->taxonomies()->register(); $this->capabilities()->register(); $this->counts()->register(); $this->assignments()->register(); $this->query_filter()->register(); $this->uploads()->register(); if ( did_action( 'init' ) ) { $this->register_spaces(); } else { add_action( 'init', array( $this, 'register_spaces' ), 5 ); } if ( did_action( 'rest_api_init' ) ) { $this->register_rest_routes(); } else { add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); } foreach ( self::MODULES as $class_name ) { $this->boot_module( $class_name ); } if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'Foldivo\\Cli\\Cli_Command' ) ) { \WP_CLI::add_command( 'foldivo', 'Foldivo\\Cli\\Cli_Command' ); } /** * Fires once Foldivo is booted. Extensions (Foldivo Pro) hook their features here. * * @param Plugin $plugin Plugin instance. */ do_action( 'foldivo_loaded', $this ); } /** * Instantiates and registers an optional module when its class exists. * * @param string $class_name Fully qualified class name. * @return void */ private function boot_module( $class_name ) { if ( isset( $this->modules[ $class_name ] ) || ! class_exists( $class_name ) ) { return; } $module = 'Foldivo\\Importers\\Importer_Registry' === $class_name ? $this->importers() : new $class_name(); if ( ! is_object( $module ) ) { return; } $this->modules[ $class_name ] = $module; if ( method_exists( $module, 'register' ) ) { $module->register(); } } /** * Registers the spaces (init, priority 5): attachment always, post/page per settings, * then `foldivo_register_spaces`, then the taxonomies and per-space query hooks. * * @return void */ public function register_spaces() { if ( $this->spaces_registered ) { return; } $this->spaces_registered = true; $spaces = $this->spaces(); $spaces->register( 'attachment' ); $enabled = Settings::get( 'spaces' ); $enabled = is_array( $enabled ) ? $enabled : array(); foreach ( array( 'post', 'page' ) as $post_type ) { if ( ! empty( $enabled[ $post_type ] ) && post_type_exists( $post_type ) ) { $spaces->register( $post_type ); } } /** * Lets extensions register more spaces with `$registry->register( $post_type, $args )`. * * @param Spaces $registry Space registry. */ do_action( 'foldivo_register_spaces', $spaces ); foreach ( $spaces->all() as $space ) { $this->setup_space( $space ); } // Spaces registered later (after init) are wired immediately. add_action( 'foldivo_space_registered', array( $this, 'setup_space' ) ); } /** * Registers the taxonomy and the query hooks of a space. * * @param Space $space Space. * @return void */ public function setup_space( $space ) { if ( ! $space instanceof Space ) { return; } $this->taxonomies()->register_taxonomy( $space ); $this->query_filter()->register_space( $space ); } /** * Registers the REST controllers, then fires `foldivo_register_rest_routes`. * * @return void */ public function register_rest_routes() { if ( $this->rest_registered ) { return; } $this->rest_registered = true; foreach ( self::REST_CONTROLLERS as $class_name ) { if ( ! class_exists( $class_name ) ) { continue; } $controller = new $class_name(); if ( method_exists( $controller, 'register_routes' ) ) { $controller->register_routes(); } } /** * Fires after the Foldivo REST routes are registered. * * @param string $namespace REST namespace (`foldivo/v1`). */ do_action( 'foldivo_register_rest_routes', self::REST_NAMESPACE ); } /** * Activation: stores the default settings and the version. Nothing else. * * @return void */ public static function activate() { add_option( Settings::OPTION, Settings::defaults(), '', 'yes' ); add_option( 'foldivo_version', FOLDIVO_VERSION, '', 'yes' ); } /** * Runs idempotent migrations when the stored version differs from the code version. * * @return void */ private function maybe_upgrade() { $stored = get_option( 'foldivo_version', '' ); if ( FOLDIVO_VERSION === $stored ) { return; } // 1.0.0: nothing to migrate yet. Future migrations go here and must stay idempotent. update_option( 'foldivo_version', FOLDIVO_VERSION, true ); /** * Fires after Foldivo migrated its data to a new version. * * @param string $from Previous version ('' on a fresh install). * @param string $to Current version. */ do_action( 'foldivo_upgraded', (string) $stored, FOLDIVO_VERSION ); } /** * Returns a lazily created core service. * * @param string $key Service key. * @param string $class_name Class to instantiate. * @return object */ private function service( $key, $class_name ) { if ( ! isset( $this->services[ $key ] ) ) { $this->services[ $key ] = new $class_name(); } return $this->services[ $key ]; } /** * Space registry. * * @return Spaces */ public function spaces() { return $this->service( 'spaces', Spaces::class ); } /** * Folder repository. * * @return Folder_Repository */ public function folders() { return $this->service( 'folders', Folder_Repository::class ); } /** * Assignment service. * * @return Assignments */ public function assignments() { return $this->service( 'assignments', Assignments::class ); } /** * Counts service. * * @return Counts */ public function counts() { return $this->service( 'counts', Counts::class ); } /** * Media usage service (where a media file is used). * * @return Media_Usage */ public function media_usage() { return $this->service( 'media_usage', Media_Usage::class ); } /** * Settings service (its methods are static and may be called either way). * * @return Settings */ public function settings() { return $this->service( 'settings', Settings::class ); } /** * User preferences service (its methods are static and may be called either way). * * @return Preferences */ public function preferences() { return $this->service( 'preferences', Preferences::class ); } /** * Query filter service. * * @return Query_Filter */ public function query_filter() { return $this->service( 'query_filter', Query_Filter::class ); } /** * Upload assignment service. * * @return Uploads */ public function uploads() { return $this->service( 'uploads', Uploads::class ); } /** * Capabilities service. * * @return Capabilities */ public function capabilities() { return $this->service( 'capabilities', Capabilities::class ); } /** * Taxonomies service. * * @return Taxonomies */ public function taxonomies() { return $this->service( 'taxonomies', Taxonomies::class ); } /** * Importer registry singleton. * * @return \Foldivo\Importers\Importer_Registry|null Null when the importers are not available. */ public function importers() { if ( null === $this->importers ) { $this->importers = class_exists( 'Foldivo\\Importers\\Importer_Registry' ) ? new \Foldivo\Importers\Importer_Registry() : false; } return $this->importers ? $this->importers : null; } /** * Returns a booted optional module. * * @param string $class_name Fully qualified class name, e.g. `Foldivo\Admin\Assets`. * @return object|null */ public function get_module( $class_name ) { $class_name = ltrim( (string) $class_name, '\\' ); return isset( $this->modules[ $class_name ] ) ? $this->modules[ $class_name ] : null; } /** * Prevents cloning. */ private function __clone() {} /** * Prevents unserialization. * * @throws \LogicException Always. */ public function __wakeup() { throw new \LogicException( 'Foldivo\Plugin cannot be unserialized.' ); } }