| 1 |
<?php |
| 2 |
namespace WPIDE\App; |
| 3 |
|
| 4 |
use WPIDE\App\Classes\Migrations; |
| 5 |
use WPIDE\App\Classes\RecommendedPlugins; |
| 6 |
use WPIDE\App\Classes\ReviewNotice; |
| 7 |
use WPIDE\App\Classes\Notices; |
| 8 |
use WPIDE\App\Config\Config; |
| 9 |
use WPIDE\App\Container\Container; |
| 10 |
use WPIDE\App\Helpers\EmptyDir; |
| 11 |
use WPIDE\App\Helpers\FileBackup; |
| 12 |
use WPIDE\App\Helpers\ImageStateData; |
| 13 |
use WPIDE\App\Kernel\Request; |
| 14 |
use WPIDE\App\Kernel\Response; |
| 15 |
use WPIDE\App\Kernel\StreamedResponse; |
| 16 |
use WPIDE\App\Services\Services; |
| 17 |
use WPIDE\App\Services\Storage\LocalFileSystem; |
| 18 |
use const WPIDE\Constants\AUTHOR; |
| 19 |
use const WPIDE\Constants\CONTENT_DIR; |
| 20 |
use const WPIDE\Constants\DIR; |
| 21 |
use const WPIDE\Constants\TMP_DIR; |
| 22 |
use const WPIDE\Constants\URL; |
| 23 |
use const WPIDE\Constants\ASSETS_URL; |
| 24 |
use const WPIDE\Constants\FATAL_ERROR_DROPIN; |
| 25 |
use const WPIDE\Constants\FATAL_ERROR_DROPIN_VERSION; |
| 26 |
use const WPIDE\Constants\FATAL_ERROR_DROPIN_VERSION_OPT; |
| 27 |
use const WPIDE\Constants\NAME; |
| 28 |
use const WPIDE\Constants\SLUG; |
| 29 |
use const WPIDE\Constants\PLUGIN_URL; |
| 30 |
use const WPIDE\Constants\VERSION; |
| 31 |
|
| 32 |
class App |
| 33 |
{ |
| 34 |
public static $_instance; |
| 35 |
|
| 36 |
private $container; |
| 37 |
private $dependencyNotice; |
| 38 |
|
| 39 |
protected $notices = []; |
| 40 |
public $services = []; |
| 41 |
|
| 42 |
public function __construct() |
| 43 |
{ |
| 44 |
|
| 45 |
register_activation_hook(WPIDE_FILE, [$this, 'activate']); |
| 46 |
register_deactivation_hook(WPIDE_FILE, [$this, 'deactivate']); |
| 47 |
|
| 48 |
add_action( 'plugins_loaded', [$this, 'load']); |
| 49 |
|
| 50 |
$this->container = new Container(); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
public function activate() { |
| 55 |
$this->installDropIn(); |
| 56 |
} |
| 57 |
|
| 58 |
public function deactivate() { |
| 59 |
$this->unInstallDropIn(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @throws \DI\DependencyException |
| 64 |
* @throws \DI\NotFoundException |
| 65 |
*/ |
| 66 |
public function resolve($name) |
| 67 |
{ |
| 68 |
return $this->container->get($name); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @throws \DI\DependencyException |
| 73 |
* @throws \DI\NotFoundException |
| 74 |
*/ |
| 75 |
public function cache() { |
| 76 |
|
| 77 |
return $this->resolve('WPIDE\App\Services\Cache\Cache'); |
| 78 |
} |
| 79 |
|
| 80 |
public function call($callable, array $parameters) |
| 81 |
{ |
| 82 |
return $this->container->call($callable, $parameters); |
| 83 |
} |
| 84 |
|
| 85 |
public function prefix($suffix = null): string |
| 86 |
{ |
| 87 |
return SLUG . (!empty($suffix) ? '_' . $suffix : ''); |
| 88 |
} |
| 89 |
|
| 90 |
public function isLatestDropIn(): bool |
| 91 |
{ |
| 92 |
|
| 93 |
$currentVersion = get_option(FATAL_ERROR_DROPIN_VERSION_OPT, null); |
| 94 |
|
| 95 |
return $currentVersion === FATAL_ERROR_DROPIN_VERSION; |
| 96 |
} |
| 97 |
|
| 98 |
public function dropInExists() { |
| 99 |
|
| 100 |
$fs = LocalFileSystem::load(CONTENT_DIR); |
| 101 |
|
| 102 |
return $fs->fileExists(FATAL_ERROR_DROPIN); |
| 103 |
} |
| 104 |
|
| 105 |
public function installDropIn() { |
| 106 |
|
| 107 |
$fs = LocalFileSystem::load(CONTENT_DIR); |
| 108 |
|
| 109 |
if($fs->fileExists(FATAL_ERROR_DROPIN)) { |
| 110 |
$this->unInstallDropIn(); |
| 111 |
} |
| 112 |
|
| 113 |
$fs->copyFile('plugins/'.basename(DIR).'/wp-content/'.FATAL_ERROR_DROPIN, './'); |
| 114 |
|
| 115 |
update_option(FATAL_ERROR_DROPIN_VERSION_OPT, FATAL_ERROR_DROPIN_VERSION); |
| 116 |
} |
| 117 |
|
| 118 |
public function unInstallDropIn() { |
| 119 |
|
| 120 |
$fs = LocalFileSystem::load(CONTENT_DIR); |
| 121 |
|
| 122 |
if($fs->fileExists(FATAL_ERROR_DROPIN)) { |
| 123 |
$fs->deleteFile(FATAL_ERROR_DROPIN); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public function load() |
| 128 |
{ |
| 129 |
|
| 130 |
if(!$this->dependencyCheck() || !is_admin()) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
if(!$this->dropInExists() || !$this->isLatestDropIn()) { |
| 135 |
$this->installDropIn(); |
| 136 |
} |
| 137 |
|
| 138 |
$this->loadTextDomain(); |
| 139 |
$this->loadHooks(); |
| 140 |
|
| 141 |
Migrations::init(); |
| 142 |
ReviewNotice::init(); |
| 143 |
RecommendedPlugins::init(); |
| 144 |
} |
| 145 |
|
| 146 |
protected function loadTextDomain() { |
| 147 |
|
| 148 |
load_plugin_textdomain( |
| 149 |
SLUG, |
| 150 |
false, |
| 151 |
basename( dirname( WPIDE_FILE ) ) . '/languages/' |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
protected function dependencyCheck(): bool |
| 156 |
{ |
| 157 |
|
| 158 |
if(!empty($this->dependencyNotice)) { |
| 159 |
echo $this->dependencyNotice; |
| 160 |
} |
| 161 |
|
| 162 |
// Perform checks here |
| 163 |
// $class = 'notice notice-error'; |
| 164 |
// $message = ''; |
| 165 |
// $this->dependencyNotice = sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), $message); |
| 166 |
|
| 167 |
if(!empty($this->dependencyNotice)) { |
| 168 |
add_action('admin_notices', [$this, 'dependencyCheck']); |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
protected function loadHooks() |
| 176 |
{ |
| 177 |
|
| 178 |
add_action('admin_menu', [$this, 'adminMenu' ]); |
| 179 |
add_action("wp_ajax_wpide_request", [$this, "ajaxRequest"]); |
| 180 |
add_action("admin_init", [$this, "registerTasks"]); |
| 181 |
add_action('admin_enqueue_scripts', [ $this, 'enqueueCommonAssets' ]); |
| 182 |
|
| 183 |
if($this->isPluginScreen()) { |
| 184 |
|
| 185 |
add_filter('admin_title', [$this, 'setAdminTitle']); |
| 186 |
add_action('admin_head', [$this, 'addAdminFavicon']); |
| 187 |
add_action("admin_menu", [$this, "setCurrentScreen"], -1); |
| 188 |
add_action('admin_menu', [Notices::class, 'init']); |
| 189 |
add_filter('screen_options_show_screen', '__return_false'); |
| 190 |
add_filter('admin_body_class', [$this, 'bodyClasses'], 10, 1); |
| 191 |
add_action('admin_enqueue_scripts', [ $this, 'deregisterWpStyles' ]); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
public function registerTasks() { |
| 196 |
|
| 197 |
//TestTask::register(); |
| 198 |
} |
| 199 |
|
| 200 |
public function setCurrentScreen() { |
| 201 |
|
| 202 |
set_current_screen(SLUG); |
| 203 |
} |
| 204 |
|
| 205 |
public function canManage(): bool |
| 206 |
{ |
| 207 |
return current_user_can('manage_options'); |
| 208 |
} |
| 209 |
|
| 210 |
public function adminMenu() |
| 211 |
{ |
| 212 |
|
| 213 |
add_menu_page( |
| 214 |
NAME, |
| 215 |
NAME, |
| 216 |
'manage_options', |
| 217 |
SLUG, |
| 218 |
[$this, 'bootstrap'], |
| 219 |
'dashicons-editor-code', |
| 220 |
3 |
| 221 |
); |
| 222 |
|
| 223 |
add_submenu_page( |
| 224 |
SLUG, |
| 225 |
__( 'File Manager', 'wpide' ), |
| 226 |
__( 'File Manager', 'wpide' ), |
| 227 |
'manage_options', |
| 228 |
SLUG.'#/file-manager', |
| 229 |
[$this, 'bootstrap'], |
| 230 |
3 |
| 231 |
); |
| 232 |
|
| 233 |
add_submenu_page( |
| 234 |
SLUG, |
| 235 |
__( 'File Editor', 'wpide' ), |
| 236 |
__( 'File Editor', 'wpide' ), |
| 237 |
'manage_options', |
| 238 |
SLUG.'#/file-editor', |
| 239 |
[$this, 'bootstrap'], |
| 240 |
3 |
| 241 |
); |
| 242 |
|
| 243 |
add_submenu_page( |
| 244 |
SLUG, |
| 245 |
__( 'DB Manager', 'wpide' ), |
| 246 |
__( 'DB Manager', 'wpide' ), |
| 247 |
'manage_options', |
| 248 |
SLUG.'#/db-manager', |
| 249 |
[$this, 'bootstrap'], |
| 250 |
3 |
| 251 |
); |
| 252 |
|
| 253 |
remove_submenu_page(SLUG, SLUG); |
| 254 |
} |
| 255 |
|
| 256 |
public function bootstrap() { |
| 257 |
|
| 258 |
FileBackup::init(); |
| 259 |
ImageStateData::init(); |
| 260 |
EmptyDir::create(TMP_DIR); |
| 261 |
|
| 262 |
$config = AppConfig::load(); |
| 263 |
$services = AppServices::load(); |
| 264 |
$request = Request::createFromGlobals(); |
| 265 |
$response = new Response(); |
| 266 |
$sresponse = new StreamedResponse(); |
| 267 |
|
| 268 |
$this->container->set(Config::class, $config); |
| 269 |
$this->container->set(Services::class, $services); |
| 270 |
$this->container->set(Container::class, $this->container); |
| 271 |
$this->container->set(Request::class, $request); |
| 272 |
$this->container->set(Response::class, $response); |
| 273 |
$this->container->set(StreamedResponse::class, $sresponse); |
| 274 |
|
| 275 |
$this->services = []; |
| 276 |
foreach ($services->get() as $key => $service) { |
| 277 |
if(empty($service['handler'])) { |
| 278 |
continue; |
| 279 |
} |
| 280 |
$this->container->set($key, $this->container->get($service['handler'])); |
| 281 |
$this->container->get($key)->init(isset($service['config']) ? $service['config'] : []); |
| 282 |
$this->services[$key] = $this->container->get($key); |
| 283 |
} |
| 284 |
|
| 285 |
if(!defined('WPIDE_DOING_TASK')) { |
| 286 |
if (wp_doing_ajax()) { |
| 287 |
$response->send(); |
| 288 |
} else { |
| 289 |
$response->sendContent(); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
public function bodyClasses($classes): string |
| 295 |
{ |
| 296 |
|
| 297 |
if(!$this->isFreemiusScreen()) { |
| 298 |
$classes = $classes . " folded"; |
| 299 |
} |
| 300 |
|
| 301 |
return $classes; |
| 302 |
} |
| 303 |
|
| 304 |
public function deregisterWpStyles() |
| 305 |
{ |
| 306 |
|
| 307 |
wp_deregister_style('color'); |
| 308 |
wp_deregister_style('forms'); |
| 309 |
wp_deregister_style('dashboard'); |
| 310 |
wp_deregister_style('list-tables'); |
| 311 |
wp_deregister_style('edit'); |
| 312 |
wp_deregister_style('revisions'); |
| 313 |
wp_deregister_style('media'); |
| 314 |
wp_deregister_style('themes'); |
| 315 |
wp_deregister_style('about'); |
| 316 |
wp_deregister_style('nav-menus'); |
| 317 |
wp_deregister_style('widgets'); |
| 318 |
wp_deregister_style('l10n'); |
| 319 |
wp_deregister_style('site-icon'); |
| 320 |
|
| 321 |
wp_register_style('color', null); |
| 322 |
wp_register_style('forms', null); |
| 323 |
wp_register_style('dashboard', null); |
| 324 |
wp_register_style('list-tables', null); |
| 325 |
wp_register_style('edit', null); |
| 326 |
wp_register_style('revisions', null); |
| 327 |
wp_register_style('media', null); |
| 328 |
wp_register_style('themes', null); |
| 329 |
wp_register_style('about', null); |
| 330 |
wp_register_style('nav-menus', null); |
| 331 |
wp_register_style('widgets', null); |
| 332 |
wp_register_style('l10n', null); |
| 333 |
wp_register_style('site-icon', null); |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
public function enqueueCommonAssets() |
| 338 |
{ |
| 339 |
|
| 340 |
wp_enqueue_style(SLUG.'-fs-notices', ASSETS_URL.'global/css/fs-notices.css', [], VERSION); |
| 341 |
} |
| 342 |
|
| 343 |
public function ajaxRequest() |
| 344 |
{ |
| 345 |
if(!$this->canManage()) { |
| 346 |
die(esc_html__('Restricted Access', 'wpide')); |
| 347 |
} |
| 348 |
|
| 349 |
$this->bootstrap(); |
| 350 |
die(); |
| 351 |
} |
| 352 |
|
| 353 |
public function isPluginScreen($section = null): bool |
| 354 |
{ |
| 355 |
|
| 356 |
$screen = function_exists('get_current_screen') ? get_current_screen() : null; |
| 357 |
|
| 358 |
$slug = SLUG; |
| 359 |
|
| 360 |
if(!empty($section)) { |
| 361 |
$slug .= '-'. $section; |
| 362 |
} |
| 363 |
|
| 364 |
if(!empty($screen)) { |
| 365 |
return strpos($screen->base, 'page_' . $slug) !== false; |
| 366 |
} |
| 367 |
|
| 368 |
if(!empty($_GET['page'])) { |
| 369 |
$page = sanitize_text_field($_GET['page']); |
| 370 |
return strpos($page, $slug) !== false; |
| 371 |
} |
| 372 |
|
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
public function isFreemiusScreen($section = null): bool |
| 377 |
{ |
| 378 |
|
| 379 |
if($section) { |
| 380 |
return $this->isPluginScreen($section); |
| 381 |
} |
| 382 |
|
| 383 |
if( |
| 384 |
$this->isPluginScreen('account') || |
| 385 |
$this->isPluginScreen('pricing') || |
| 386 |
$this->isPluginScreen('contact') || |
| 387 |
$this->isPluginScreen('affiliates') |
| 388 |
) { |
| 389 |
return true; |
| 390 |
} |
| 391 |
|
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
public function getAdminUrl(): string { |
| 396 |
|
| 397 |
return admin_url( 'admin.php?page='.SLUG ); |
| 398 |
} |
| 399 |
|
| 400 |
public function getAjaxUrl($req = ''): string |
| 401 |
{ |
| 402 |
return admin_url('admin-ajax.php').'?action=wpide_request&req='.$req; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Get external url with utm campaign / content |
| 407 |
* |
| 408 |
* @param string|null $utm_content |
| 409 |
* @param string|null $url |
| 410 |
* |
| 411 |
* @return string $link; |
| 412 |
* @since 1.0.0 |
| 413 |
*/ |
| 414 |
public function getExternalUrl(string $utm_content = null, string $url = null): string |
| 415 |
{ |
| 416 |
|
| 417 |
$url = !empty($url) ? $url : PLUGIN_URL; |
| 418 |
|
| 419 |
if(!empty($utm_content)) { |
| 420 |
$url .= '?utm_source='.AUTHOR.'&utm_medium=Plugin&utm_campaign='.SLUG.'&utm_content='.$utm_content; |
| 421 |
} |
| 422 |
|
| 423 |
return $url; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Filter admin page title |
| 428 |
*/ |
| 429 |
public function setAdminTitle(): string |
| 430 |
{ |
| 431 |
return NAME; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* render backend favicon |
| 436 |
*/ |
| 437 |
public function addAdminFavicon () { |
| 438 |
|
| 439 |
echo '<link rel="shortcut icon" type="image/svg+xml" href="' . ASSETS_URL. '/img/favicon.svg">',"\n"; |
| 440 |
echo '<link rel="shortcut icon" type="image/x-icon" href="' . ASSETS_URL. '/img/favicon.ico">',"\n"; |
| 441 |
echo '<link rel="shortcut icon" type="image/png" href="' . ASSETS_URL. '/img/favicon.png">',"\n"; |
| 442 |
echo '<meta name="theme-color" content="#ffffff">',"\n"; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
/** |
| 447 |
* @return App |
| 448 |
*/ |
| 449 |
public static function instance(): App |
| 450 |
{ |
| 451 |
if ( is_null( self::$_instance ) ) { |
| 452 |
self::$_instance = new self(); |
| 453 |
} |
| 454 |
|
| 455 |
return self::$_instance; |
| 456 |
} |
| 457 |
|
| 458 |
} |
| 459 |
|