PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.4
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.4
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 2.6.4, at classes/Plugin/Core.php

485 lines 11.3 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 $this->container['connect'] =
195 /**
196 * Get plugin connect.
197 *
198 * @return Connect
199 */
200 function ( $c ) {
201 return new \ContentControl\Plugin\Connect( $c );
202 };
203
204 $this->container['license'] =
205 /**
206 * Get plugin license.
207 *
208 * @return License
209 */
210 function () {
211 return new \ContentControl\Plugin\License();
212 };
213
214 $this->container['logging'] =
215 /**
216 * Get plugin logging.
217 *
218 * @return Logging
219 */
220 function () {
221 return new \ContentControl\Plugin\Logging();
222 };
223
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 };
233
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 };
243
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 };
253
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 );
266 }
267
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 /**
288 * Initiate internal components.
289 *
290 * @return void
291 */
292 protected function initiate_controllers() {
293 $this->register_controllers( $this->registered_controllers() );
294 }
295
296 /**
297 * Register controllers.
298 *
299 * @param array<string,Controller> $controllers Array of controllers.
300 * @return void
301 */
302 public function register_controllers( $controllers = [] ) {
303 foreach ( $controllers as $name => $controller ) {
304 // Sanity check to ensure controller implements Controller interface.
305 // @phpstan-ignore-next-line .
306 if ( $controller instanceof Controller ) {
307 if ( $controller->controller_enabled() ) {
308 $controller->init();
309 }
310 $this->controllers->set( $name, $controller );
311 }
312 }
313 }
314
315 /**
316 * Get a controller.
317 *
318 * @param string $name Controller name.
319 *
320 * @return Controller|null
321 */
322 public function get_controller( $name ) {
323 $controller = $this->controllers->get( $name );
324
325 if ( $controller instanceof Controller ) {
326 return $controller;
327 }
328
329 return null;
330 }
331
332 /**
333 * Initiate internal paths.
334 *
335 * @return void
336 */
337 protected function define_paths() {
338 /**
339 * Attach utility functions.
340 */
341 $this->container['get_path'] = [ $this, 'get_path' ];
342 $this->container['get_url'] = [ $this, 'get_url' ];
343
344 // Define paths.
345 $this->container['dist_path'] = $this->get_path( 'dist' ) . '/';
346 }
347
348 /**
349 * Utility method to get a path.
350 *
351 * @param string $path Subpath to return.
352 * @return string
353 */
354 public function get_path( $path ) {
355 return $this->container['path'] . $path;
356 }
357
358 /**
359 * Utility method to get a url.
360 *
361 * @param string $path Sub url to return.
362 * @return string
363 */
364 public function get_url( $path = '' ) {
365 return $this->container['url'] . $path;
366 }
367
368 /**
369 * Get item from container
370 *
371 * @param string $id Key for the item.
372 *
373 * @return mixed Current value of the item.
374 */
375 public function get( $id ) {
376 // 1. Check if the item exists in the container.
377 if ( $this->container->offsetExists( $id ) ) {
378 return $this->container->get( $id );
379 }
380
381 // 2. Check if the item exists in the controllers container.
382 if ( $this->controllers->offsetExists( $id ) ) {
383 return $this->controllers->get( $id );
384 }
385
386 // 3. Check if the item exists in the global space.
387 if ( get_called_class() !== __CLASS__ ) {
388 // If this is an addon, check if the service exists in the core plugin.
389 // Get core plugin container and see if the service exists there.
390 $plugin_service = \ContentControl\plugin( $id );
391
392 if ( $plugin_service ) {
393 return $plugin_service;
394 }
395 }
396
397 // 5. Return null, item not found.
398 return null;
399 }
400
401 /**
402 * Set item in container
403 *
404 * @param string $id Key for the item.
405 * @param mixed $value Value to set.
406 *
407 * @return void
408 */
409 public function set( $id, $value ) {
410 $this->container->set( $id, $value );
411 }
412
413 /**
414 * Get plugin option.
415 *
416 * @param string $key Option key.
417 * @param boolean|mixed $default_value Default value.
418 * @return mixed
419 */
420 public function get_option( $key, $default_value = false ) {
421 return $this->get( 'options' )->get( $key, $default_value );
422 }
423
424 /**
425 * Get plugin permissions.
426 *
427 * @return array<string,string> Array of permissions.
428 */
429 public function get_permissions() {
430 $permissions = \ContentControl\get_default_permissions();
431
432 $user_permisions = $this->get( 'options' )->get( 'permissions', [] );
433
434 if ( ! empty( $user_permisions ) ) {
435 foreach ( $user_permisions as $cap => $user_permission ) {
436 if ( ! empty( $user_permission ) ) {
437 $permissions[ $cap ] = $user_permission;
438 }
439 }
440 }
441
442 return $permissions;
443 }
444
445 /**
446 * Get plugin permission for capability.
447 *
448 * @param string $cap Permission key.
449 *
450 * @return string User role or cap required.
451 */
452 public function get_permission( $cap ) {
453 $permissions = $this->get_permissions();
454
455 return isset( $permissions[ $cap ] ) ? $permissions[ $cap ] : 'manage_options';
456 }
457
458 /**
459 * Check if pro version is installed.
460 *
461 * @return boolean
462 */
463 public function is_pro_installed() {
464 return file_exists( WP_PLUGIN_DIR . '/content-control-pro/content-control-pro.php' );
465 }
466
467 /**
468 * Check if pro version is active.
469 *
470 * @return boolean
471 */
472 public function is_pro_active() {
473 return $this->is_pro_installed() && function_exists( '\ContentControl\Pro\plugin' );
474 }
475
476 /**
477 * Check if license is active.
478 *
479 * @return boolean
480 */
481 public function is_license_active() {
482 return $this->get( 'license' )->is_license_active();
483 }
484 }
485