| 1 |
<?php |
| 2 |
/** |
| 3 |
* The main Bookit plugin service provider: it bootstraps the plugin code. |
| 4 |
* |
| 5 |
* @since 2.5.0 |
| 6 |
* |
| 7 |
* @package Bookit |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Bookit; |
| 11 |
|
| 12 |
use Bookit\Contracts\Container; |
| 13 |
use Bookit\REST\Provider as REST_Provider; |
| 14 |
use Bookit\Gateways\StripeConnect\Provider as Stripe_Provider; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Plugin |
| 18 |
* |
| 19 |
* @since 2.5.0 |
| 20 |
* |
| 21 |
* @package Bookit |
| 22 |
*/ |
| 23 |
class Plugin { |
| 24 |
|
| 25 |
/** |
| 26 |
* Stores the version for the plugin. |
| 27 |
* |
| 28 |
* @since 2.5.0 |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
public const VERSION = '2.5.1'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Stores the base slug for the plugin. |
| 36 |
* |
| 37 |
* @since 2.5.0 |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
const SLUG = 'bookit'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Stores the base slug for the plugin |
| 45 |
* |
| 46 |
* @since 2.5.0 |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
const FILE = BOOKIT_FILE; |
| 51 |
|
| 52 |
/** |
| 53 |
* @var bool Prevent autoload initialization |
| 54 |
*/ |
| 55 |
private $should_prevent_autoload_init = false; |
| 56 |
|
| 57 |
/** |
| 58 |
* @since 2.5.0 |
| 59 |
* |
| 60 |
* @var string Plugin Directory. |
| 61 |
*/ |
| 62 |
public $plugin_dir; |
| 63 |
|
| 64 |
/** |
| 65 |
* @since 2.5.0 |
| 66 |
* |
| 67 |
* @var string Plugin path. |
| 68 |
*/ |
| 69 |
public $plugin_path; |
| 70 |
|
| 71 |
/** |
| 72 |
* @since 2.5.0 |
| 73 |
* |
| 74 |
* @var string Plugin basename. |
| 75 |
*/ |
| 76 |
public $plugin_basename; |
| 77 |
|
| 78 |
/** |
| 79 |
* @since 2.5.0 |
| 80 |
* |
| 81 |
* @var string Plugin URL. |
| 82 |
*/ |
| 83 |
public $plugin_url; |
| 84 |
|
| 85 |
/** |
| 86 |
* @since 2.5.0 |
| 87 |
* |
| 88 |
* @var string Plugin Base Path. |
| 89 |
*/ |
| 90 |
public static $plugin_base_path; |
| 91 |
|
| 92 |
/** |
| 93 |
* @since 2.5.0 |
| 94 |
* |
| 95 |
* @var string Plugin Base URL. |
| 96 |
*/ |
| 97 |
public static $plugin_base_url; |
| 98 |
|
| 99 |
/** |
| 100 |
* Allows this class to be used as a singleton. |
| 101 |
* |
| 102 |
* Note this specifically doesn't have a typing, just a type hinting via Docblocks, it helps |
| 103 |
* avoid problems with deprecation since this is loaded so early. |
| 104 |
* |
| 105 |
* @since 2.5.0 |
| 106 |
* |
| 107 |
* @var \Bookit__Container |
| 108 |
*/ |
| 109 |
protected $container; |
| 110 |
|
| 111 |
/** |
| 112 |
* Sets the container for the class. |
| 113 |
* |
| 114 |
* Note this specifically doesn't have a typing for the container, just a type hinting via Docblocks, it helps |
| 115 |
* avoid problems with deprecation since this is loaded so early. |
| 116 |
* |
| 117 |
* @since 2.5.0 |
| 118 |
* |
| 119 |
* @param ?\Bookit__Container $container The container to use, if any. If not provided, the global container will be used. |
| 120 |
*/ |
| 121 |
public function set_container( $container = null ): void { |
| 122 |
$this->container = $container ?: new Container(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Boots the plugin class and registers it as a singleton. |
| 127 |
* |
| 128 |
* Note this specifically doesn't have a typing for the container, just a type hinting via Docblocks, it helps |
| 129 |
* avoid problems with deprecation since this is loaded so early. |
| 130 |
* |
| 131 |
* @since 2.5.0 |
| 132 |
* |
| 133 |
* @param ?\Bookit__Container $container The container to use, if any. If not provided, the global container will be used. |
| 134 |
*/ |
| 135 |
public function boot( $container = null ): void { |
| 136 |
$this->plugin_path = trailingslashit( dirname( static::FILE ) ); |
| 137 |
self::$plugin_base_path = $this->plugin_path; |
| 138 |
$this->plugin_basename = basename( static::FILE ); |
| 139 |
$this->plugin_dir = trailingslashit( basename( $this->plugin_path ) ); |
| 140 |
$this->plugin_url = plugins_url( $this->plugin_dir, $this->plugin_path ); |
| 141 |
self::$plugin_base_url = $this->plugin_url; |
| 142 |
|
| 143 |
add_action( 'plugins_loaded', [ $this, 'bootstrap' ], 1 ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Plugins shouldn't include their functions before `plugins_loaded` because this will allow |
| 148 |
* better compatibility with the autoloader methods. |
| 149 |
* |
| 150 |
* @since 2.5.0 |
| 151 |
*/ |
| 152 |
public function bootstrap() { |
| 153 |
if ( $this->should_prevent_autoload_init ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
$plugin = new static(); |
| 157 |
$plugin->register_autoloader(); |
| 158 |
$plugin->set_container(); |
| 159 |
$plugin->container->singleton( static::class, $plugin ); |
| 160 |
$plugin->register(); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Setup the Extension's properties. |
| 165 |
* |
| 166 |
* This always executes even if the required plugins are not present. |
| 167 |
* |
| 168 |
* @since 2.5.0 |
| 169 |
*/ |
| 170 |
public function register() { |
| 171 |
$this->register_autoloader(); |
| 172 |
|
| 173 |
// Register this provider as the main one and use a bunch of aliases. |
| 174 |
$this->container->singleton( static::class, $this ); |
| 175 |
$this->container->singleton( 'bookit', $this ); |
| 176 |
$this->container->singleton( 'bookit.plugin', $this ); |
| 177 |
|
| 178 |
$this->container->register( REST_Provider::class ); |
| 179 |
$this->container->register( Stripe_Provider::class ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Register the Autoloader for Bookit. |
| 184 |
* |
| 185 |
* @since 2.5.0 |
| 186 |
*/ |
| 187 |
protected function register_autoloader() { |
| 188 |
// Load Composer autoload and strauss autoloader. |
| 189 |
require_once dirname( BOOKIT_FILE ) . '/vendor/vendor-prefixed/autoload.php'; |
| 190 |
require_once dirname( BOOKIT_FILE ) . '/vendor/autoload.php'; |
| 191 |
|
| 192 |
// The DI container class. |
| 193 |
require_once dirname( __FILE__ ) . '/Container.php'; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Get Vendor URL. |
| 198 |
* |
| 199 |
* @since 2.5.0 |
| 200 |
*/ |
| 201 |
public static function get_vendor_url() { |
| 202 |
return self::$plugin_base_url . 'vendor/'; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get Assets Path. |
| 207 |
* |
| 208 |
* @since 2.5.0 |
| 209 |
*/ |
| 210 |
public static function get_asset_path() { |
| 211 |
return self::$plugin_base_path . 'src/resources/'; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/** |
| 216 |
* Get Assets Path. |
| 217 |
* |
| 218 |
* @since 2.5.0 |
| 219 |
*/ |
| 220 |
public static function get_asset_url() { |
| 221 |
return self::$plugin_base_url . 'src/resources/'; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Plugin activation callback. |
| 226 |
* @see register_activation_hook() |
| 227 |
* |
| 228 |
* @since 2.5.0 |
| 229 |
*/ |
| 230 |
public static function activate() {} |
| 231 |
|
| 232 |
/** |
| 233 |
* Plugin deactivation callback. |
| 234 |
* @see register_deactivation_hook() |
| 235 |
* |
| 236 |
* @since 2.5.0 |
| 237 |
* |
| 238 |
* @param bool $network_deactivating |
| 239 |
*/ |
| 240 |
public static function deactivate( $network_deactivating ) { |
| 241 |
flush_rewrite_rules(); |
| 242 |
} |
| 243 |
} |
| 244 |
|