PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Plugin / Core.php

Core.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at classes/Plugin/Core.php

490 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main plugin.
4 *
5 * @copyright (c) 2021, Code Atlantic LLC.
6 *
7 * @package ContentControl
8 */
9
10 namespace ContentControl\Plugin;
11
12 use ContentControl\Base\Container;
13 use ContentControl\Plugin\Options;
14 use ContentControl\Interfaces\Controller;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Class Plugin
20 *
21 * @package ContentControl\Plugin
22 *
23 * @version 2.0.0
24 */
25 class Core {
26
27 /**
28 * Exposed container.
29 *
30 * @var Container
31 */
32 public $container;
33
34 /**
35 * Array of controllers.
36 *
37 * Useful to unhook actions/filters from global space.
38 *
39 * @var Container
40 */
41 public $controllers;
42
43 /**
44 * Initiate the plugin.
45 *
46 * @param array<string,string|bool> $config Configuration variables passed from main plugin file.
47 */
48 public function __construct( $config ) {
49 $this->container = new Container( $config );
50 $this->controllers = new Container();
51
52 $this->register_services();
53 $this->define_paths();
54 $this->initiate_controllers();
55
56 $this->check_version();
57
58 add_action( 'init', [ $this, 'load_textdomain' ] );
59 }
60
61 /**
62 * Update & track version info.
63 *
64 * @return void
65 */
66 protected function check_version() {
67 $version = $this->get( 'version' );
68 $option_key = "{$this->get( 'option_prefix' )}_version";
69
70 $current_data = \get_option( $option_key, false );
71
72 $data = wp_parse_args(
73 false !== $current_data ? $current_data : [],
74 [
75 'version' => $version,
76 'upgraded_from' => null,
77 'initial_version' => $version,
78 'installed_on' => gmdate( 'Y-m-d H:i:s' ),
79 ]
80 );
81
82 // Process old version data storage.
83 if ( false === $current_data ) {
84 $data = $this->process_version_data_migration( $data );
85 }
86
87 if ( version_compare( $data['version'], (string) $version, '<' ) ) {
88 // Allow processing of small core upgrades.
89
90 /**
91 * Fires when the plugin version is updated.
92 *
93 * Note: Old version is still available in options.
94 *
95 * @param string $old_version The old version.
96 * @param string $new_version The new version.
97 */
98 do_action( "{$this->get( 'option_prefix' )}/update_version", $data['version'], $version );
99
100 // Save Upgraded From option.
101 $data['upgraded_from'] = $data['version'];
102 $data['version'] = $version;
103 }
104
105 if ( $current_data !== $data ) {
106 \update_option( $option_key, $data );
107 }
108 }
109
110 /**
111 * Process old version data.
112 *
113 * @param array<string,string|null> $data Array of data.
114 * @return array<string,string|null>
115 */
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 );
123
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 }
143 }
144
145 if ( empty( $data['initial_version'] ) ) {
146 $oldest_known = $data['version'];
147
148 if ( $data['upgraded_from'] && version_compare( $data['upgraded_from'], $oldest_known, '<' ) ) {
149 $oldest_known = $data['upgraded_from'];
150 }
151
152 $data['initial_version'] = $oldest_known;
153 }
154
155 return $data;
156 }
157
158 /**
159 * Internationalization.
160 *
161 * @return void
162 */
163 public function load_textdomain() {
164 load_plugin_textdomain( $this->container['text_domain'], false, $this->get_path( 'languages' ) );
165 }
166
167 /**
168 * Add default services to our Container.
169 *
170 * @return void
171 */
172 public function register_services() {
173 /**
174 * Self reference for deep DI lookup.
175 */
176 $this->container['plugin'] = $this;
177
178 /**
179 * Attach our container to the global.
180 */
181 $GLOBALS[ $this->get( 'option_prefix' ) ] = $this->container;
182
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 };
193
194 if ( $this->is_legacy_pro_active() ) {
195 $this->container['license'] =
196 /**
197 * Get the temporary old-Pro license bridge.
198 *
199 * @return License
200 * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns licensing.
201 */
202 function () {
203 return new \ContentControl\Plugin\License();
204 };
205 }
206
207 $this->container['logging'] =
208 /**
209 * Get plugin logging.
210 *
211 * @return Logging
212 */
213 function () {
214 return new \ContentControl\Plugin\Logging();
215 };
216
217 $this->container['rules'] =
218 /**
219 * Get plugin rules.
220 *
221 * @return \ContentControl\RuleEngine\Rules
222 */
223 function () {
224 return new \ContentControl\RuleEngine\Rules();
225 };
226
227 $this->container['restrictions'] =
228 /**
229 * Get plugin restrictions.
230 *
231 * @return \ContentControl\Services\Restrictions
232 */
233 function () {
234 return new \ContentControl\Services\Restrictions();
235 };
236
237 $this->container['globals'] =
238 /**
239 * Get plugin global manager.
240 *
241 * @return \ContentControl\Services\Globals
242 */
243 function () {
244 return new \ContentControl\Services\Globals();
245 };
246 }
247
248 apply_filters( "{$this->get( 'option_prefix' )}/register_services", $this->container, $this );
249 }
250
251 /**
252 * Update & track version info.
253 *
254 * @return array<string,\ContentControl\Base\Controller>
255 */
256 protected function registered_controllers() {
257 return [
258 'PostTypes' => new \ContentControl\Controllers\PostTypes( $this ),
259 'Assets' => new \ContentControl\Controllers\Assets( $this ),
260 'Admin' => new \ContentControl\Controllers\Admin( $this ),
261 'Compatibility' => new \ContentControl\Controllers\Compatibility( $this ),
262 'RestAPI' => new \ContentControl\Controllers\RestAPI( $this ),
263 'BlockEditor' => new \ContentControl\Controllers\BlockEditor( $this ),
264 'Frontend' => new \ContentControl\Controllers\Frontend( $this ),
265 'Shortcodes' => new \ContentControl\Controllers\Shortcodes( $this ),
266 'TrustedLoginController' => new \ContentControl\Controllers\TrustedLogin( $this ),
267 ];
268 }
269
270 /**
271 * Initiate internal components.
272 *
273 * @return void
274 */
275 protected function initiate_controllers() {
276 $this->register_controllers( $this->registered_controllers() );
277 }
278
279 /**
280 * Register controllers.
281 *
282 * @param array<string,Controller> $controllers Array of controllers.
283 * @return void
284 */
285 public function register_controllers( $controllers = [] ) {
286 foreach ( $controllers as $name => $controller ) {
287 // Sanity check to ensure controller implements Controller interface.
288 // @phpstan-ignore-next-line .
289 if ( $controller instanceof Controller ) {
290 if ( $controller->controller_enabled() ) {
291 $controller->init();
292 }
293 $this->controllers->set( $name, $controller );
294 }
295 }
296 }
297
298 /**
299 * Get a controller.
300 *
301 * @param string $name Controller name.
302 *
303 * @return Controller|null
304 */
305 public function get_controller( $name ) {
306 $controller = $this->controllers->get( $name );
307
308 if ( $controller instanceof Controller ) {
309 return $controller;
310 }
311
312 return null;
313 }
314
315 /**
316 * Initiate internal paths.
317 *
318 * @return void
319 */
320 protected function define_paths() {
321 /**
322 * Attach utility functions.
323 */
324 $this->container['get_path'] = [ $this, 'get_path' ];
325 $this->container['get_url'] = [ $this, 'get_url' ];
326
327 // Define paths.
328 $this->container['dist_path'] = $this->get_path( 'dist' ) . '/';
329 }
330
331 /**
332 * Utility method to get a path.
333 *
334 * @param string $path Subpath to return.
335 * @return string
336 */
337 public function get_path( $path ) {
338 return $this->container['path'] . $path;
339 }
340
341 /**
342 * Utility method to get a url.
343 *
344 * @param string $path Sub url to return.
345 * @return string
346 */
347 public function get_url( $path = '' ) {
348 return $this->container['url'] . $path;
349 }
350
351 /**
352 * Get item from container
353 *
354 * @param string $id Key for the item.
355 *
356 * @return mixed Current value of the item.
357 */
358 public function get( $id ) {
359 // 1. Check if the item exists in the container.
360 if ( $this->container->offsetExists( $id ) ) {
361 return $this->container->get( $id );
362 }
363
364 // 2. Check if the item exists in the controllers container.
365 if ( $this->controllers->offsetExists( $id ) ) {
366 return $this->controllers->get( $id );
367 }
368
369 // 3. Check if the item exists in the global space.
370 if ( get_called_class() !== __CLASS__ ) {
371 // If this is an addon, check if the service exists in the core plugin.
372 // Get core plugin container and see if the service exists there.
373 $plugin_service = \ContentControl\plugin( $id );
374
375 if ( $plugin_service ) {
376 return $plugin_service;
377 }
378 }
379
380 // 5. Return null, item not found.
381 return null;
382 }
383
384 /**
385 * Set item in container
386 *
387 * @param string $id Key for the item.
388 * @param mixed $value Value to set.
389 *
390 * @return void
391 */
392 public function set( $id, $value ) {
393 $this->container->set( $id, $value );
394 }
395
396 /**
397 * Get plugin option.
398 *
399 * @param string $key Option key.
400 * @param boolean|mixed $default_value Default value.
401 * @return mixed
402 */
403 public function get_option( $key, $default_value = false ) {
404 return $this->get( 'options' )->get( $key, $default_value );
405 }
406
407 /**
408 * Get plugin permissions.
409 *
410 * @return array<string,string> Array of permissions.
411 */
412 public function get_permissions() {
413 $permissions = \ContentControl\get_default_permissions();
414
415 $user_permisions = $this->get( 'options' )->get( 'permissions', [] );
416
417 if ( ! empty( $user_permisions ) ) {
418 foreach ( $user_permisions as $cap => $user_permission ) {
419 if ( ! empty( $user_permission ) ) {
420 $permissions[ $cap ] = $user_permission;
421 }
422 }
423 }
424
425 return $permissions;
426 }
427
428 /**
429 * Get plugin permission for capability.
430 *
431 * @param string $cap Permission key.
432 *
433 * @return string User role or cap required.
434 */
435 public function get_permission( $cap ) {
436 $permissions = $this->get_permissions();
437
438 return isset( $permissions[ $cap ] ) ? $permissions[ $cap ] : 'manage_options';
439 }
440
441 /**
442 * Check if pro version is installed.
443 *
444 * @return boolean
445 */
446 public function is_pro_installed() {
447 return file_exists( WP_PLUGIN_DIR . '/content-control-pro/content-control-pro.php' );
448 }
449
450 /**
451 * Check if pro version is active.
452 *
453 * @return boolean
454 */
455 public function is_pro_active() {
456 return $this->is_pro_installed() && function_exists( '\ContentControl\Pro\plugin' );
457 }
458
459 /**
460 * Whether an active Pro release still depends on Core's license provider.
461 *
462 * This version gate is the only reason Core retains its deprecated license
463 * bridge. Pro 1.3.0 and later register their own provider before use.
464 *
465 * @return boolean
466 */
467 public function is_legacy_pro_active() {
468 if ( ! $this->is_pro_active() || ! function_exists( '\ContentControl\Pro\config' ) ) {
469 return false;
470 }
471
472 $version = \ContentControl\Pro\config( 'version' );
473
474 return is_string( $version ) && '' !== $version && version_compare( $version, '1.3.0', '<' );
475 }
476
477 /**
478 * Check if license is active.
479 *
480 * @return boolean
481 */
482 public function is_license_active() {
483 $license = $this->get( 'license' );
484
485 return is_object( $license ) && method_exists( $license, 'is_license_active' )
486 ? $license->is_license_active()
487 : false;
488 }
489 }
490