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

382 lines 8.6 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 class Core {
24
25 /**
26 * Exposed container.
27 *
28 * @var Container
29 */
30 public $container;
31
32 /**
33 * Array of controllers.
34 *
35 * Useful to unhook actions/filters from global space.
36 *
37 * @var Container
38 */
39 public $controllers;
40
41 /**
42 * Initiate the plugin.
43 *
44 * @param array<string,string|bool> $config Configuration variables passed from main plugin file.
45 */
46 public function __construct( $config ) {
47 $this->container = new Container( $config );
48 $this->controllers = new Container();
49
50 $this->register_services();
51 $this->initiate_controllers();
52
53 $this->check_version();
54
55 add_action( 'init', [ $this, 'load_textdomain' ] );
56 }
57
58 /**
59 * Update & track version info.
60 *
61 * @return void
62 */
63 private function check_version() {
64 $version = $this->get( 'version' );
65 $option_key = 'content_control_version';
66
67 $current_data = \get_option( $option_key, false );
68
69 $data = wp_parse_args(
70 false !== $current_data ? $current_data : [],
71 [
72 'version' => $version,
73 'upgraded_from' => null,
74 'initial_version' => $version,
75 'installed_on' => gmdate( 'Y-m-d H:i:s' ),
76 ]
77 );
78
79 // Process old version data storage.
80 if ( false === $current_data ) {
81 $data = $this->process_version_data_migration( $data );
82 }
83
84 if ( version_compare( $data['version'], (string) $version, '<' ) ) {
85 // Allow processing of small core upgrades.
86
87 /**
88 * Fires when the plugin version is updated.
89 *
90 * Note: Old version is still available in options.
91 *
92 * @param string $version The new version.
93 */
94 do_action( 'content_control/update_version', $data['version'] );
95
96 // Save Upgraded From option.
97 $data['upgraded_from'] = $data['version'];
98 $data['version'] = $version;
99 }
100
101 if ( $current_data !== $data ) {
102 \update_option( $option_key, $data );
103 }
104 }
105
106 /**
107 * Process old version data.
108 *
109 * @param array<string,string|null> $data Array of data.
110 * @return array<string,string|null>
111 */
112 private function process_version_data_migration( $data ) {
113 $has_old_settings_data = \get_option( 'jp_cc_settings', false );
114 $has_old_install_date = \get_option( 'jp_cc_reviews_installed_on', false );
115
116 // Check if old settings exist.
117 if ( false !== $has_old_settings_data || false !== $has_old_install_date ) {
118 $data = [
119 'version' => '1.1.9',
120 'upgraded_from' => null,
121 'initial_version' => '1.1.9',
122 ];
123 }
124
125 if ( empty( $data['initial_version'] ) ) {
126 $oldest_known = $data['version'];
127
128 if ( $data['upgraded_from'] && version_compare( $data['upgraded_from'], $oldest_known, '<' ) ) {
129 $oldest_known = $data['upgraded_from'];
130 }
131
132 $data['initial_version'] = $oldest_known;
133 }
134
135 return $data;
136 }
137
138 /**
139 * Internationalization.
140 *
141 * @return void
142 */
143 public function load_textdomain() {
144 load_plugin_textdomain( $this->container['text_domain'], false, $this->get_path( 'languages' ) );
145 }
146
147 /**
148 * Add default services to our Container.
149 *
150 * @return void
151 */
152 public function register_services() {
153 /**
154 * Self reference for deep DI lookup.
155 */
156 $this->container['plugin'] = $this;
157
158 /**
159 * Attach our container to the global.
160 */
161 $GLOBALS['content_control'] = $this->container;
162
163 $this->container['options'] = function ( $c ) {
164 return new Options( $c->get( 'option_prefix' ) );
165 };
166
167 $this->container['connect'] = function ( $c ) {
168 return new \ContentControl\Plugin\Connect( $c );
169 };
170
171 $this->container['license'] = function () {
172 return new \ContentControl\Plugin\License();
173 };
174
175 $this->container['logging'] = function () {
176 return new \ContentControl\Plugin\Logging();
177 };
178
179 $this->container['upgrader'] = function ( $c ) {
180 return new \ContentControl\Plugin\Upgrader( $c );
181 };
182
183 $this->container['rules'] = function () {
184 return new \ContentControl\RuleEngine\Rules();
185 };
186
187 $this->container['restrictions'] = function () {
188 return new \ContentControl\Services\Restrictions();
189 };
190
191 apply_filters( 'content_control/register_services', $this->container, $this );
192 }
193
194 /**
195 * Initiate internal components.
196 *
197 * @return void
198 */
199 private function initiate_controllers() {
200 $this->define_paths();
201
202 $this->register_controllers( [
203 'PostTypes' => new \ContentControl\Controllers\PostTypes( $this ),
204 'Assets' => new \ContentControl\Controllers\Assets( $this ),
205 'Admin' => new \ContentControl\Controllers\Admin( $this ),
206 'Compatibility' => new \ContentControl\Controllers\Compatibility( $this ),
207 'RestAPI' => new \ContentControl\Controllers\RestAPI( $this ),
208 'BlockEditor' => new \ContentControl\Controllers\BlockEditor( $this ),
209 'Frontend' => new \ContentControl\Controllers\Frontend( $this ),
210 'Shortcodes' => new \ContentControl\Controllers\Shortcodes( $this ),
211 'TrustedLogin' => new \ContentControl\Controllers\TrustedLogin( $this ),
212 ] );
213 }
214
215 /**
216 * Register controllers.
217 *
218 * @param array<string,Controller> $controllers Array of controllers.
219 * @return void
220 */
221 public function register_controllers( $controllers = [] ) {
222 foreach ( $controllers as $name => $controller ) {
223 if ( $controller instanceof Controller ) {
224 $controller->init();
225 $this->controllers->set( $name, $controller );
226 }
227 }
228 }
229
230 /**
231 * Get a controller.
232 *
233 * @param string $name Controller name.
234 *
235 * @return Controller|null
236 */
237 public function get_controller( $name ) {
238 $controller = $this->controllers->get( $name );
239
240 if ( $controller instanceof Controller ) {
241 return $controller;
242 }
243
244 return null;
245 }
246
247 /**
248 * Initiate internal paths.
249 *
250 * @return void
251 */
252 private function define_paths() {
253 /**
254 * Attach utility functions.
255 */
256 $this->container['get_path'] = [ $this, 'get_path' ];
257 $this->container['get_url'] = [ $this, 'get_url' ];
258
259 // Define paths.
260 $this->container['dist_path'] = $this->get_path( 'dist' ) . '/';
261 }
262
263 /**
264 * Utility method to get a path.
265 *
266 * @param string $path Subpath to return.
267 * @return string
268 */
269 public function get_path( $path ) {
270 return $this->container['path'] . $path;
271 }
272
273 /**
274 * Utility method to get a url.
275 *
276 * @param string $path Sub url to return.
277 * @return string
278 */
279 public function get_url( $path = '' ) {
280 return $this->container['url'] . $path;
281 }
282
283 /**
284 * Get item from container
285 *
286 * @param string $id Key for the item.
287 *
288 * @return mixed Current value of the item.
289 */
290 public function get( $id ) {
291 if ( ! $this->container->offsetExists( $id ) ) {
292 return $this->controllers->get( $id );
293 }
294
295 return $this->container->get( $id );
296 }
297
298 /**
299 * Set item in container
300 *
301 * @param string $id Key for the item.
302 * @param mixed $value Value to set.
303 *
304 * @return void
305 */
306 public function set( $id, $value ) {
307 $this->container->set( $id, $value );
308 }
309
310 /**
311 * Get plugin option.
312 *
313 * @param string $key Option key.
314 * @param boolean|mixed $default_value Default value.
315 * @return mixed
316 */
317 public function get_option( $key, $default_value = false ) {
318 return $this->get( 'options' )->get( $key, $default_value );
319 }
320
321 /**
322 * Get plugin permissions.
323 *
324 * @return array<string,string> Array of permissions.
325 */
326 public function get_permissions() {
327 $permissions = \ContentControl\get_default_permissions();
328
329 $user_permisions = $this->get( 'options' )->get( 'permissions', [] );
330
331 if ( ! empty( $user_permisions ) ) {
332 foreach ( $user_permisions as $cap => $user_permission ) {
333 if ( ! empty( $user_permission ) ) {
334 $permissions[ $cap ] = $user_permission;
335 }
336 }
337 }
338
339 return $permissions;
340 }
341
342 /**
343 * Get plugin permission for capability.
344 *
345 * @param string $cap Permission key.
346 *
347 * @return string User role or cap required.
348 */
349 public function get_permission( $cap ) {
350 $permissions = $this->get_permissions();
351
352 return isset( $permissions[ $cap ] ) ? $permissions[ $cap ] : 'manage_options';
353 }
354
355 /**
356 * Check if pro version is installed.
357 *
358 * @return boolean
359 */
360 public function is_pro_installed() {
361 return file_exists( WP_PLUGIN_DIR . '/content-control-pro/content-control-pro.php' );
362 }
363
364 /**
365 * Check if pro version is active.
366 *
367 * @return boolean
368 */
369 public function is_pro_active() {
370 return $this->is_pro_installed() && class_exists( '\ContentControlPro\Plugin\Core' );
371 }
372
373 /**
374 * Check if license is active.
375 *
376 * @return boolean
377 */
378 public function is_license_active() {
379 return $this->get( 'license' )->is_license_active();
380 }
381 }
382