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

483 lines 11.2 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 if ( $controller instanceof Controller ) {
305 if ( $controller->controller_enabled() ) {
306 $controller->init();
307 }
308 $this->controllers->set( $name, $controller );
309 }
310 }
311 }
312
313 /**
314 * Get a controller.
315 *
316 * @param string $name Controller name.
317 *
318 * @return Controller|null
319 */
320 public function get_controller( $name ) {
321 $controller = $this->controllers->get( $name );
322
323 if ( $controller instanceof Controller ) {
324 return $controller;
325 }
326
327 return null;
328 }
329
330 /**
331 * Initiate internal paths.
332 *
333 * @return void
334 */
335 protected function define_paths() {
336 /**
337 * Attach utility functions.
338 */
339 $this->container['get_path'] = [ $this, 'get_path' ];
340 $this->container['get_url'] = [ $this, 'get_url' ];
341
342 // Define paths.
343 $this->container['dist_path'] = $this->get_path( 'dist' ) . '/';
344 }
345
346 /**
347 * Utility method to get a path.
348 *
349 * @param string $path Subpath to return.
350 * @return string
351 */
352 public function get_path( $path ) {
353 return $this->container['path'] . $path;
354 }
355
356 /**
357 * Utility method to get a url.
358 *
359 * @param string $path Sub url to return.
360 * @return string
361 */
362 public function get_url( $path = '' ) {
363 return $this->container['url'] . $path;
364 }
365
366 /**
367 * Get item from container
368 *
369 * @param string $id Key for the item.
370 *
371 * @return mixed Current value of the item.
372 */
373 public function get( $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 ) ) {
381 return $this->controllers->get( $id );
382 }
383
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;
397 }
398
399 /**
400 * Set item in container
401 *
402 * @param string $id Key for the item.
403 * @param mixed $value Value to set.
404 *
405 * @return void
406 */
407 public function set( $id, $value ) {
408 $this->container->set( $id, $value );
409 }
410
411 /**
412 * Get plugin option.
413 *
414 * @param string $key Option key.
415 * @param boolean|mixed $default_value Default value.
416 * @return mixed
417 */
418 public function get_option( $key, $default_value = false ) {
419 return $this->get( 'options' )->get( $key, $default_value );
420 }
421
422 /**
423 * Get plugin permissions.
424 *
425 * @return array<string,string> Array of permissions.
426 */
427 public function get_permissions() {
428 $permissions = \ContentControl\get_default_permissions();
429
430 $user_permisions = $this->get( 'options' )->get( 'permissions', [] );
431
432 if ( ! empty( $user_permisions ) ) {
433 foreach ( $user_permisions as $cap => $user_permission ) {
434 if ( ! empty( $user_permission ) ) {
435 $permissions[ $cap ] = $user_permission;
436 }
437 }
438 }
439
440 return $permissions;
441 }
442
443 /**
444 * Get plugin permission for capability.
445 *
446 * @param string $cap Permission key.
447 *
448 * @return string User role or cap required.
449 */
450 public function get_permission( $cap ) {
451 $permissions = $this->get_permissions();
452
453 return isset( $permissions[ $cap ] ) ? $permissions[ $cap ] : 'manage_options';
454 }
455
456 /**
457 * Check if pro version is installed.
458 *
459 * @return boolean
460 */
461 public function is_pro_installed() {
462 return file_exists( WP_PLUGIN_DIR . '/content-control-pro/content-control-pro.php' );
463 }
464
465 /**
466 * Check if pro version is active.
467 *
468 * @return boolean
469 */
470 public function is_pro_active() {
471 return $this->is_pro_installed() && function_exists( '\ContentControl\Pro\plugin' );
472 }
473
474 /**
475 * Check if license is active.
476 *
477 * @return boolean
478 */
479 public function is_license_active() {
480 return $this->get( 'license' )->is_license_active();
481 }
482 }
483