<?php
declare(strict_types=1);
namespace MPBC;
use MPBC\Action\Actions;
use MPBC\Admin\{ Admin_Notices, Admin_Side };
use MPBC\Email\Emails_Dispatcher;
use MPBC\Post_Type\Post_Types;
use MPBC\REST\REST_API;
use MPBC\Shortcode\Shortcodes;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class Plugin {
private static self $instance;
private string $root_file;
private string $plugin_slug;
private string $plugin_dir;
private string $plugin_url;
private Actions $actions;
private Admin_Side $admin;
private Assets $assets;
private Blocks $blocks;
private Database $db;
private Emails_Dispatcher $emails_dispatcher;
private Post_Types $post_types;
private REST_API $rest_api;
private Shortcodes $shortcodes;
private function __construct( string $root_file ) {
$this->root_file = $root_file;
$this->plugin_slug = plugin_basename( $root_file );
$this->plugin_dir = plugin_dir_path( $root_file );
$this->plugin_url = plugin_dir_url( $root_file );
new Autoloader( __NAMESPACE__, $this->plugin_dir );
$this->actions = new Actions();
$this->admin = new Admin_Side();
$this->assets = new Assets();
$this->blocks = new Blocks();
$this->db = new Database();
$this->emails_dispatcher = new Emails_Dispatcher();
$this->post_types = new Post_Types();
$this->rest_api = new REST_API();
$this->shortcodes = new Shortcodes();
Admin_Notices::load();
}
public function is_lite(): bool {
return true;
}
public function get_plugin_name(): string {
return __( 'Booking Calendar', 'motopress-booking-calendar' );
}
public function get_plugin_slug(): string {
return $this->plugin_slug;
}
public function get_plugin_url(): string {
return 'https://motopress.com/products/booking-calendar/';
}
public function get_root_file(): string {
return $this->root_file;
}
public function get_file_path( string $plugin_file ): string {
return $this->plugin_dir . $plugin_file;
}
public function get_file_url( string $plugin_file ): string {
return $this->plugin_url . $plugin_file;
}
public function get_version(): string {
// See motopress-booking-calendar.php
return VERSION;
}
public function get_licensing_data(): array {
return array(
'edd_api_url' => $this->get_plugin_url(),
'edd_product_id' => 1770219,
'plugin_author' => 'MotoPress',
'plugin_prefix' => 'mpbc',
'plugin_root_file' => $this->get_root_file(),
'plugin_version' => $this->get_version(),
);
}
public function actions(): Actions {
return $this->actions;
}
public function admin(): Admin_Side {
return $this->admin;
}
public function assets(): Assets {
return $this->assets;
}
public function blocks(): Blocks {
return $this->blocks;
}
public function database(): Database {
return $this->db;
}
public function emails_dispatcher(): Emails_Dispatcher {
return $this->emails_dispatcher;
}
public function post_types(): Post_Types {
return $this->post_types;
}
public function shortcodes(): Shortcodes {
return $this->shortcodes;
}
public static function run( string $root_file ): void {
if ( ! isset( static::$instance ) ) {
static::create_instance( $root_file );
// See register_uninstall_hook() in activate_on_site()
register_activation_hook( $root_file, array( __CLASS__, 'activate' ) );
register_deactivation_hook( $root_file, array( __CLASS__, 'deactivate' ) );
// Add installation for a new site in multisite WordPress
add_action(
'wp_initialize_site',
fn( \WP_Site $new_site ) => static::activate( false, (int) $new_site->blog_id )
);
}
}
/**
* When the method is called:
* - all active plugins ARE loaded;
* - current plugin in NOT loaded.
*
* There is an immediate redirection after the plugin's activation.
*
* @access private
*/
public static function activate( bool $network_wide = false, int $site_id = 0 ): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( is_multisite() && ( $network_wide || $site_id !== 0 ) ) {
$site_ids = array();
if ( $network_wide ) {
$site_ids = get_sites( array( 'fields' => 'ids' ) );
} elseif ( is_plugin_active_for_network( static::get_instance()->get_plugin_slug() ) ) {
$site_ids = array( $site_id );
}
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
static::activate_on_site();
restore_current_blog();
}
} else {
static::activate_on_site();
}
}
/**
* When the method is called:
* - all active plugins are loaded;
* - current plugin is fully loaded and inited.
*
* @access private
*/
public static function deactivate( bool $network_wide = false ): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( is_multisite() && $network_wide ) {
$site_ids = get_sites( array( 'fields' => 'ids' ) );
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
static::deactivate_on_site();
restore_current_blog();
}
} else {
static::deactivate_on_site();
}
}
/**
* If multisite enabled, uninstallation of the plugin is invoked for all
* sub-sites at once.
*
* When the method is called:
* - all must-use plugins are active.
*
* @access private
*/
public static function uninstall(): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( is_multisite() ) {
$site_ids = get_sites( array( 'fields' => 'ids' ) );
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
static::uninstall_on_site();
restore_current_blog();
}
} else {
static::uninstall_on_site();
}
}
private static function activate_on_site(): void {
// Register uninstall hook only once to avoid extra DB query for each
// website visit
register_uninstall_hook(
static::get_instance()->get_root_file(),
array( __CLASS__, 'uninstall' )
);
Roles_And_Capabilities::create_capabilities();
static::get_instance()->post_types()->flush_rewrite_rules();
}
private static function deactivate_on_site(): void {
}
private static function uninstall_on_site(): void {
Roles_And_Capabilities::delete_capabilities();
}
private static function create_instance( string $root_file ): void {
static::$instance = new self( $root_file );
}
public static function get_instance(): self {
return static::$instance;
}
}