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