| @@ -18,8 +18,10 @@ | ||
| 18 | 18 | /** |
| 19 | 19 | * Class Plugin |
| 20 | 20 | * |
| 21 | 21 | * @package ContentControl\Plugin |
| 22 | + * | |
| 23 | + * @version 2.0.0 | |
| 22 | 24 | */ |
| 23 | 25 | class Core { |
| 24 | 26 | |
| 25 | 27 | /** |
| @@ -35,14 +37,14 @@ | ||
| 35 | 37 | * Useful to unhook actions/filters from global space. |
| 36 | 38 | * |
| 37 | 39 | * @var Container |
| 38 | 40 | */ |
| 39 | - public $controllers = []; | |
| 41 | + public $controllers; | |
| 40 | 42 | |
| 41 | 43 | /** |
| 42 | 44 | * Initiate the plugin. |
| 43 | 45 | * |
| 44 | - * @param array $config Configuration variables passed from main plugin file. | |
| 46 | + * @param array<string,string|bool> $config Configuration variables passed from main plugin file. | |
| 45 | 47 | */ |
| 46 | 48 | public function __construct( $config ) { |
| 47 | 49 | $this->container = new Container( $config ); |
| 48 | 50 | $this->controllers = new Container(); |
| @@ -47,8 +49,9 @@ | ||
| 47 | 49 | $this->container = new Container( $config ); |
| 48 | 50 | $this->controllers = new Container(); |
| 49 | 51 | |
| 50 | 52 | $this->register_services(); |
| 53 | + $this->define_paths(); | |
| 51 | 54 | $this->initiate_controllers(); |
| 52 | 55 | |
| 53 | 56 | $this->check_version(); |
| 54 | 57 | |
| @@ -56,12 +59,14 @@ | ||
| 56 | 59 | } |
| 57 | 60 | |
| 58 | 61 | /** |
| 59 | 62 | * Update & track version info. |
| 63 | + * | |
| 64 | + * @return void | |
| 60 | 65 | */ |
| 61 | - private function check_version() { | |
| 66 | + protected function check_version() { | |
| 62 | 67 | $version = $this->get( 'version' ); |
| 63 | - $option_key = 'content_control_version'; | |
| 68 | + $option_key = "{$this->get( 'option_prefix' )}_version"; | |
| 64 | 69 | |
| 65 | 70 | $current_data = \get_option( $option_key, false ); |
| 66 | 71 | |
| 67 | 72 | $data = wp_parse_args( |
| @@ -86,11 +91,12 @@ | ||
| 86 | 91 | * Fires when the plugin version is updated. |
| 87 | 92 | * |
| 88 | 93 | * Note: Old version is still available in options. |
| 89 | 94 | * |
| 90 | - * @param string $version The new version. | |
| 95 | + * @param string $old_version The old version. | |
| 96 | + * @param string $new_version The new version. | |
| 91 | 97 | */ |
| 92 | - do_action( 'content_control/update_version', $data['version'] ); | |
| 98 | + do_action( "{$this->get( 'option_prefix' )}/update_version", $data['version'], $version ); | |
| 93 | 99 | |
| 94 | 100 | // Save Upgraded From option. |
| 95 | 101 | $data['upgraded_from'] = $data['version']; |
| 96 | 102 | $data['version'] = $version; |
| @@ -103,22 +109,38 @@ | ||
| 103 | 109 | |
| 104 | 110 | /** |
| 105 | 111 | * Process old version data. |
| 106 | 112 | * |
| 107 | - * @param array $data Array of data. | |
| 108 | - * @return array | |
| 113 | + * @param array<string,string|null> $data Array of data. | |
| 114 | + * @return array<string,string|null> | |
| 109 | 115 | */ |
| 110 | - private function process_version_data_migration( $data ) { | |
| 111 | - $has_old_settings_data = \get_option( 'jp_cc_settings', false ); | |
| 112 | - $has_old_install_date = \get_option( 'jp_cc_reviews_installed_on', false ); | |
| 116 | + protected function process_version_data_migration( $data ) { | |
| 117 | + // This class can be extended for addons, only do the following if this is core and not an extended class. | |
| 118 | + // If the current instance is not an extended class, check if old settings exist. | |
| 119 | + if ( get_called_class() === __CLASS__ ) { | |
| 120 | + // Check if old settings exist. | |
| 121 | + $has_old_settings_data = \get_option( 'content_control_settings', false ); | |
| 122 | + $has_old_install_date = \get_option( 'content_control_installed_on', false ); | |
| 113 | 123 | |
| 114 | - // Check if old settings exist. | |
| 115 | - if ( false !== $has_old_settings_data || false !== $has_old_install_date ) { | |
| 116 | - $data = [ | |
| 117 | - 'version' => '1.1.9', | |
| 118 | - 'upgraded_from' => null, | |
| 119 | - 'initial_version' => '1.1.9', | |
| 120 | - ]; | |
| 124 | + if ( false !== $has_old_settings_data || false !== $has_old_install_date ) { | |
| 125 | + $data = [ | |
| 126 | + 'version' => '1.1.9', | |
| 127 | + 'upgraded_from' => null, | |
| 128 | + 'initial_version' => '1.1.9', | |
| 129 | + ]; | |
| 130 | + } | |
| 131 | + | |
| 132 | + $has_old_settings_data = \get_option( 'jp_cc_settings', false ); | |
| 133 | + $has_old_install_date = \get_option( 'jp_cc_reviews_installed_on', false ); | |
| 134 | + | |
| 135 | + // Check if old settings exist. | |
| 136 | + if ( false !== $has_old_settings_data || false !== $has_old_install_date ) { | |
| 137 | + $data = [ | |
| 138 | + 'version' => '1.1.9', | |
| 139 | + 'upgraded_from' => null, | |
| 140 | + 'initial_version' => '1.1.9', | |
| 141 | + ]; | |
| 142 | + } | |
| 121 | 143 | } |
| 122 | 144 | |
| 123 | 145 | if ( empty( $data['initial_version'] ) ) { |
| 124 | 146 | $oldest_known = $data['version']; |
| @@ -133,9 +155,11 @@ | ||
| 133 | 155 | return $data; |
| 134 | 156 | } |
| 135 | 157 | |
| 136 | 158 | /** |
| 137 | - * Internationalization | |
| 159 | + * Internationalization. | |
| 160 | + * | |
| 161 | + * @return void | |
| 138 | 162 | */ |
| 139 | 163 | public function load_textdomain() { |
| 140 | 164 | load_plugin_textdomain( $this->container['text_domain'], false, $this->get_path( 'languages' ) ); |
| 141 | 165 | } |
| @@ -140,9 +164,11 @@ | ||
| 140 | 164 | load_plugin_textdomain( $this->container['text_domain'], false, $this->get_path( 'languages' ) ); |
| 141 | 165 | } |
| 142 | 166 | |
| 143 | 167 | /** |
| 144 | - * Add default services to our Container | |
| 168 | + * Add default services to our Container. | |
| 169 | + * | |
| 170 | + * @return void | |
| 145 | 171 | */ |
| 146 | 172 | public function register_services() { |
| 147 | 173 | /** |
| 148 | 174 | * Self reference for deep DI lookup. |
| @@ -151,70 +177,135 @@ | ||
| 151 | 177 | |
| 152 | 178 | /** |
| 153 | 179 | * Attach our container to the global. |
| 154 | 180 | */ |
| 155 | - $GLOBALS['content_control'] = $this->container; | |
| 181 | + $GLOBALS[ $this->get( 'option_prefix' ) ] = $this->container; | |
| 156 | 182 | |
| 157 | - $this->container['options'] = function ( $c ) { | |
| 158 | - return new Options( $c->get( 'option_prefix' ) ); | |
| 159 | - }; | |
| 183 | + if ( get_called_class() === __CLASS__ ) { | |
| 184 | + $this->container['options'] = | |
| 185 | + /** | |
| 186 | + * Get plugin options. | |
| 187 | + * | |
| 188 | + * @return Options | |
| 189 | + */ | |
| 190 | + function ( $c ) { | |
| 191 | + return new Options( $c->get( 'option_prefix' ) ); | |
| 192 | + }; | |
| 160 | 193 | |
| 161 | - $this->container['connect'] = function ( $c ) { | |
| 162 | - return new \ContentControl\Plugin\Connect( $c ); | |
| 163 | - }; | |
| 194 | + $this->container['connect'] = | |
| 195 | + /** | |
| 196 | + * Get plugin connect. | |
| 197 | + * | |
| 198 | + * @return Connect | |
| 199 | + */ | |
| 200 | + function ( $c ) { | |
| 201 | + return new \ContentControl\Plugin\Connect( $c ); | |
| 202 | + }; | |
| 164 | 203 | |
| 165 | - $this->container['license'] = function ( $c ) { | |
| 166 | - return new \ContentControl\Plugin\License( $c ); | |
| 167 | - }; | |
| 204 | + $this->container['license'] = | |
| 205 | + /** | |
| 206 | + * Get plugin license. | |
| 207 | + * | |
| 208 | + * @return License | |
| 209 | + */ | |
| 210 | + function () { | |
| 211 | + return new \ContentControl\Plugin\License(); | |
| 212 | + }; | |
| 168 | 213 | |
| 169 | - $this->container['logging'] = function ( $c ) { | |
| 170 | - return new \ContentControl\Plugin\Logging( $c ); | |
| 171 | - }; | |
| 214 | + $this->container['logging'] = | |
| 215 | + /** | |
| 216 | + * Get plugin logging. | |
| 217 | + * | |
| 218 | + * @return Logging | |
| 219 | + */ | |
| 220 | + function () { | |
| 221 | + return new \ContentControl\Plugin\Logging(); | |
| 222 | + }; | |
| 172 | 223 | |
| 173 | - $this->container['upgrader'] = function ( $c ) { | |
| 174 | - return new \ContentControl\Plugin\Upgrader( $c ); | |
| 175 | - }; | |
| 224 | + $this->container['upgrader'] = | |
| 225 | + /** | |
| 226 | + * Get plugin upgrader. | |
| 227 | + * | |
| 228 | + * @return Upgrader | |
| 229 | + */ | |
| 230 | + function ( $c ) { | |
| 231 | + return new \ContentControl\Plugin\Upgrader( $c ); | |
| 232 | + }; | |
| 176 | 233 | |
| 177 | - $this->container['rules'] = function () { | |
| 178 | - return new \ContentControl\RuleEngine\Rules(); | |
| 179 | - }; | |
| 234 | + $this->container['rules'] = | |
| 235 | + /** | |
| 236 | + * Get plugin rules. | |
| 237 | + * | |
| 238 | + * @return \ContentControl\RuleEngine\Rules | |
| 239 | + */ | |
| 240 | + function () { | |
| 241 | + return new \ContentControl\RuleEngine\Rules(); | |
| 242 | + }; | |
| 180 | 243 | |
| 181 | - $this->container['restrictions'] = function () { | |
| 182 | - return new \ContentControl\Services\Restrictions(); | |
| 183 | - }; | |
| 244 | + $this->container['restrictions'] = | |
| 245 | + /** | |
| 246 | + * Get plugin restrictions. | |
| 247 | + * | |
| 248 | + * @return \ContentControl\Services\Restrictions | |
| 249 | + */ | |
| 250 | + function () { | |
| 251 | + return new \ContentControl\Services\Restrictions(); | |
| 252 | + }; | |
| 184 | 253 | |
| 185 | - apply_filters( 'content_control/register_services', $this->container, $this ); | |
| 254 | + $this->container['globals'] = | |
| 255 | + /** | |
| 256 | + * Get plugin global manager. | |
| 257 | + * | |
| 258 | + * @return \ContentControl\Services\Globals | |
| 259 | + */ | |
| 260 | + function () { | |
| 261 | + return new \ContentControl\Services\Globals(); | |
| 262 | + }; | |
| 263 | + } | |
| 264 | + | |
| 265 | + apply_filters( "{$this->get( 'option_prefix' )}/register_services", $this->container, $this ); | |
| 186 | 266 | } |
| 187 | 267 | |
| 188 | 268 | /** |
| 269 | + * Update & track version info. | |
| 270 | + * | |
| 271 | + * @return array<string,\ContentControl\Base\Controller> | |
| 272 | + */ | |
| 273 | + protected function registered_controllers() { | |
| 274 | + return [ | |
| 275 | + 'PostTypes' => new \ContentControl\Controllers\PostTypes( $this ), | |
| 276 | + 'Assets' => new \ContentControl\Controllers\Assets( $this ), | |
| 277 | + 'Admin' => new \ContentControl\Controllers\Admin( $this ), | |
| 278 | + 'Compatibility' => new \ContentControl\Controllers\Compatibility( $this ), | |
| 279 | + 'RestAPI' => new \ContentControl\Controllers\RestAPI( $this ), | |
| 280 | + 'BlockEditor' => new \ContentControl\Controllers\BlockEditor( $this ), | |
| 281 | + 'Frontend' => new \ContentControl\Controllers\Frontend( $this ), | |
| 282 | + 'Shortcodes' => new \ContentControl\Controllers\Shortcodes( $this ), | |
| 283 | + 'TrustedLoginController' => new \ContentControl\Controllers\TrustedLogin( $this ), | |
| 284 | + ]; | |
| 285 | + } | |
| 286 | + | |
| 287 | + /** | |
| 189 | 288 | * Initiate internal components. |
| 289 | + * | |
| 290 | + * @return void | |
| 190 | 291 | */ |
| 191 | - private function initiate_controllers() { | |
| 192 | - $this->define_paths(); | |
| 193 | - | |
| 194 | - $this->register_controllers( [ | |
| 195 | - 'PostTypes' => new \ContentControl\Controllers\PostTypes( $this ), | |
| 196 | - 'Assets' => new \ContentControl\Controllers\Assets( $this ), | |
| 197 | - 'Admin' => new \ContentControl\Controllers\Admin( $this ), | |
| 198 | - 'Compatibility' => new \ContentControl\Controllers\Compatibility( $this ), | |
| 199 | - 'RestAPI' => new \ContentControl\Controllers\RestAPI( $this ), | |
| 200 | - 'BlockEditor' => new \ContentControl\Controllers\BlockEditor( $this ), | |
| 201 | - 'Frontend' => new \ContentControl\Controllers\Frontend( $this ), | |
| 202 | - 'Shortcodes' => new \ContentControl\Controllers\Shortcodes( $this ), | |
| 203 | - 'TrustedLogin' => new \ContentControl\Controllers\TrustedLogin( $this ), | |
| 204 | - ] ); | |
| 292 | + protected function initiate_controllers() { | |
| 293 | + $this->register_controllers( $this->registered_controllers() ); | |
| 205 | 294 | } |
| 206 | 295 | |
| 207 | 296 | /** |
| 208 | 297 | * Register controllers. |
| 209 | 298 | * |
| 210 | - * @param array $controllers Array of controllers. | |
| 299 | + * @param array<string,Controller> $controllers Array of controllers. | |
| 211 | 300 | * @return void |
| 212 | 301 | */ |
| 213 | 302 | public function register_controllers( $controllers = [] ) { |
| 214 | 303 | foreach ( $controllers as $name => $controller ) { |
| 215 | 304 | if ( $controller instanceof Controller ) { |
| 216 | - $controller->init(); | |
| 305 | + if ( $controller->controller_enabled() ) { | |
| 306 | + $controller->init(); | |
| 307 | + } | |
| 217 | 308 | $this->controllers->set( $name, $controller ); |
| 218 | 309 | } |
| 219 | 310 | } |
| 220 | 311 | } |
| @@ -226,15 +317,23 @@ | ||
| 226 | 317 | * |
| 227 | 318 | * @return Controller|null |
| 228 | 319 | */ |
| 229 | 320 | public function get_controller( $name ) { |
| 230 | - return $this->controllers->get( $name, null ); | |
| 321 | + $controller = $this->controllers->get( $name ); | |
| 322 | + | |
| 323 | + if ( $controller instanceof Controller ) { | |
| 324 | + return $controller; | |
| 325 | + } | |
| 326 | + | |
| 327 | + return null; | |
| 231 | 328 | } |
| 232 | 329 | |
| 233 | 330 | /** |
| 234 | 331 | * Initiate internal paths. |
| 332 | + * | |
| 333 | + * @return void | |
| 235 | 334 | */ |
| 236 | - private function define_paths() { | |
| 335 | + protected function define_paths() { | |
| 237 | 336 | /** |
| 238 | 337 | * Attach utility functions. |
| 239 | 338 | */ |
| 240 | 339 | $this->container['get_path'] = [ $this, 'get_path' ]; |
| @@ -271,13 +370,31 @@ | ||
| 271 | 370 | * |
| 272 | 371 | * @return mixed Current value of the item. |
| 273 | 372 | */ |
| 274 | 373 | public function get( $id ) { |
| 275 | - if ( ! $this->container->offsetExists( $id ) ) { | |
| 374 | + // 1. Check if the item exists in the container. | |
| 375 | + if ( $this->container->offsetExists( $id ) ) { | |
| 376 | + return $this->container->get( $id ); | |
| 377 | + } | |
| 378 | + | |
| 379 | + // 2. Check if the item exists in the controllers container. | |
| 380 | + if ( $this->controllers->offsetExists( $id ) ) { | |
| 276 | 381 | return $this->controllers->get( $id ); |
| 277 | 382 | } |
| 278 | 383 | |
| 279 | - return $this->container->get( $id ); | |
| 384 | + // 3. Check if the item exists in the global space. | |
| 385 | + if ( get_called_class() !== __CLASS__ ) { | |
| 386 | + // If this is an addon, check if the service exists in the core plugin. | |
| 387 | + // Get core plugin container and see if the service exists there. | |
| 388 | + $plugin_service = \ContentControl\plugin( $id ); | |
| 389 | + | |
| 390 | + if ( $plugin_service ) { | |
| 391 | + return $plugin_service; | |
| 392 | + } | |
| 393 | + } | |
| 394 | + | |
| 395 | + // 5. Return null, item not found. | |
| 396 | + return null; | |
| 280 | 397 | } |
| 281 | 398 | |
| 282 | 399 | /** |
| 283 | 400 | * Set item in container |
| @@ -293,10 +410,10 @@ | ||
| 293 | 410 | |
| 294 | 411 | /** |
| 295 | 412 | * Get plugin option. |
| 296 | 413 | * |
| 297 | - * @param string $key Option key. | |
| 298 | - * @param boolean $default_value Default value. | |
| 414 | + * @param string $key Option key. | |
| 415 | + * @param boolean|mixed $default_value Default value. | |
| 299 | 416 | * @return mixed |
| 300 | 417 | */ |
| 301 | 418 | public function get_option( $key, $default_value = false ) { |
| 302 | 419 | return $this->get( 'options' )->get( $key, $default_value ); |
| @@ -304,9 +421,9 @@ | ||
| 304 | 421 | |
| 305 | 422 | /** |
| 306 | 423 | * Get plugin permissions. |
| 307 | 424 | * |
| 308 | - * @return array Array of permissions. | |
| 425 | + * @return array<string,string> Array of permissions. | |
| 309 | 426 | */ |
| 310 | 427 | public function get_permissions() { |
| 311 | 428 | $permissions = \ContentControl\get_default_permissions(); |
| 312 | 429 | |
| @@ -350,9 +467,9 @@ | ||
| 350 | 467 | * |
| 351 | 468 | * @return boolean |
| 352 | 469 | */ |
| 353 | 470 | public function is_pro_active() { |
| 354 | - return $this->is_pro_installed() && class_exists( '\ContentControlPro\Plugin\Core' ); | |
| 471 | + return $this->is_pro_installed() && function_exists( '\ContentControl\Pro\plugin' ); | |
| 355 | 472 | } |
| 356 | 473 | |
| 357 | 474 | /** |
| 358 | 475 | * Check if license is active. |