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