PluginProbe
WPIDE – File Manager & Code Editor / 3.5.7
WPIDE – File Manager & Code Editor v3.5.7
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / App.php

App.php in WPIDE – File Manager & Code Editor 3.5.7, at App/App.php

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