Passed
Push — 567-fix/refactor-system-info-w... ( 1d64a1 )
by
unknown
11:45
created

BulkDelete::get_system_info_page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core;
4
5
use BulkWP\BulkDelete\Core\Addon\Upseller;
6
use BulkWP\BulkDelete\Core\Base\BasePage;
7
use BulkWP\BulkDelete\Core\Cron\CronListPage;
8
use BulkWP\BulkDelete\Core\SystemInfo\SystemInfoPage;
9
use BulkWP\BulkDelete\Core\Metas\DeleteMetasPage;
10
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteCommentMetaModule;
11
use BulkWP\BulkDelete\Core\Metas\Modules\DeletePostMetaModule;
12
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteUserMetaModule;
13
use BulkWP\BulkDelete\Core\Pages\DeletePagesPage;
14
use BulkWP\BulkDelete\Core\Pages\Modules\DeletePagesByStatusModule;
15
use BulkWP\BulkDelete\Core\Posts\DeletePostsPage;
16
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByCategoryModule;
17
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByCommentsModule;
18
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByPostTypeModule;
19
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByRevisionModule;
20
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByStatusModule;
21
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByStickyPostModule;
22
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByTagModule;
23
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByTaxonomyModule;
24
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByURLModule;
25
use BulkWP\BulkDelete\Core\Terms\DeleteTermsPage;
26
use BulkWP\BulkDelete\Core\Terms\Modules\DeleteTermsByNameModule;
27
use BulkWP\BulkDelete\Core\Terms\Modules\DeleteTermsByPostCountModule;
28
use BulkWP\BulkDelete\Core\Users\DeleteUsersPage;
29
use BulkWP\BulkDelete\Core\Users\Modules\DeleteUsersByUserMetaModule;
30
use BulkWP\BulkDelete\Core\Users\Modules\DeleteUsersByUserRoleModule;
31
32
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
33
34
/**
35
 * Main Plugin class.
36
 *
37
 * @since 5.0 Converted to Singleton
38
 * @since 6.0.0 Renamed to BulkDelete and added namespace.
39
 */
40
final class BulkDelete {
41
	/**
42
	 * The one true BulkDelete instance.
43
	 *
44
	 * @var BulkDelete
45
	 *
46
	 * @since 5.0
47
	 */
48
	private static $instance;
49
50
	/**
51
	 * Path to the main plugin file.
52
	 *
53
	 * @var string
54
	 */
55
	private $plugin_file;
56
57
	/**
58
	 * Path where translations are stored.
59
	 *
60
	 * @var string
61
	 */
62
	private $translations_path;
63
64
	/**
65
	 * Has the plugin loaded?
66
	 *
67
	 * @since 6.0.0
68
	 *
69
	 * @var bool
70
	 */
71
	private $loaded = false;
72
73
	/**
74
	 * Controller that handles all requests and nonce checks.
75
	 *
76
	 * @var \BulkWP\BulkDelete\Core\Controller
77
	 */
78
	private $controller;
79
80
	/**
81
	 * Upseller responsible for upselling add-ons.
82
	 *
83
	 * @since 6.0.0
84
	 *
85
	 * @var \BulkWP\BulkDelete\Core\Addon\Upseller
86
	 */
87
	private $upseller;
88
89
	/**
90
	 * Bulk Delete Autoloader.
91
	 *
92
	 * Will be used by add-ons to extend the namespace.
93
	 *
94
	 * @var \BulkWP\BulkDelete\BulkDeleteAutoloader
95
	 */
96
	private $loader;
97
98
	/**
99
	 * List of Primary Admin pages.
100
	 *
101
	 * @var BasePage[]
102
	 *
103
	 * @since 6.0.0
104
	 */
105
	private $primary_pages = array();
106
107
	/**
108
	 * List of Secondary Admin pages.
109
	 *
110
	 * @var BasePage[]
111
	 *
112
	 * @since 6.0.0
113
	 */
114
	private $secondary_pages = array();
115
116
	/**
117
	 * Plugin version.
118
	 */
119
	const VERSION = '6.0.0';
120
121
	/**
122
	 * Set the BulkDelete constructor as private.
123
	 *
124
	 * An instance should be created by calling the `get_instance` method.
125
	 *
126
	 * @see BulkDelete::get_instance()
127
	 */
128
	private function __construct() {}
129
130
	/**
131
	 * Main BulkDelete Instance.
132
	 *
133
	 * Insures that only one instance of BulkDelete exists in memory at any one
134
	 * time. Also prevents needing to define globals all over the place.
135
	 *
136
	 * @since     5.0
137
	 * @static
138
	 * @staticvar array $instance
139
	 *
140
	 * @return BulkDelete The one true instance of BulkDelete.
141
	 */
142
	public static function get_instance() {
143
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof BulkDelete ) ) {
144
			self::$instance = new BulkDelete();
145
		}
146
147
		return self::$instance;
148
	}
149
150
	/**
151
	 * Load the plugin if it is not loaded.
152
	 *
153
	 * This function will be invoked in the `plugins_loaded` hook.
154
	 */
155
	public function load() {
156
		if ( $this->loaded ) {
157
			return;
158
		}
159
160
		$this->load_dependencies();
161
		$this->setup_actions();
162
163
		$this->loaded = true;
164
165
		/**
166
		 * Bulk Delete plugin loaded.
167
		 *
168
		 * @since 6.0.0
169
		 *
170
		 * @param string Plugin main file.
171
		 */
172
		do_action( 'bd_loaded', $this->get_plugin_file() );
173
	}
174
175
	/**
176
	 * Throw error on object clone.
177
	 *
178
	 * The whole idea of the singleton design pattern is that there is a single
179
	 * object therefore, we don't want the object to be cloned.
180
	 *
181
	 * @since  5.0
182
	 * @access protected
183
	 *
184
	 * @return void
185
	 */
186
	public function __clone() {
187
		_doing_it_wrong( __FUNCTION__, __( "This class can't be cloned. Use `get_instance()` method to get an instance.", 'bulk-delete' ), '5.0' );
188
	}
189
190
	/**
191
	 * Disable unserializing of the class.
192
	 *
193
	 * @since  5.0
194
	 * @access protected
195
	 *
196
	 * @return void
197
	 */
198
	public function __wakeup() {
199
		_doing_it_wrong( __FUNCTION__, __( "This class can't be serialized. Use `get_instance()` method to get an instance.", 'bulk-delete' ), '5.0' );
200
	}
201
202
	/**
203
	 * Load all dependencies.
204
	 *
205
	 * @since 6.0.0
206
	 */
207
	private function load_dependencies() {
208
		$this->controller = new Controller();
209
		$this->controller->load();
210
211
		$this->upseller = new Upseller();
212
		$this->upseller->load();
213
	}
214
215
	/**
216
	 * Loads the plugin's actions and hooks.
217
	 *
218
	 * @access private
219
	 *
220
	 * @since  5.0
221
	 *
222
	 * @return void
223
	 */
224
	private function setup_actions() {
225
		add_action( 'init', array( $this, 'on_init' ) );
226
227
		add_action( 'admin_menu', array( $this, 'on_admin_menu' ) );
228
	}
229
230
	/**
231
	 * Triggered when the `init` hook is fired.
232
	 *
233
	 * @since 6.0.0
234
	 */
235
	public function on_init() {
236
		$this->load_textdomain();
237
	}
238
239
	/**
240
	 * Loads the plugin language files.
241
	 *
242
	 * @since  5.0
243
	 */
244
	private function load_textdomain() {
245
		load_plugin_textdomain( 'bulk-delete', false, $this->get_translations_path() );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $deprecated of load_plugin_textdomain(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

245
		load_plugin_textdomain( 'bulk-delete', /** @scrutinizer ignore-type */ false, $this->get_translations_path() );
Loading history...
246
	}
247
248
	/**
249
	 * Triggered when the `admin_menu` hook is fired.
250
	 *
251
	 * Register all admin pages.
252
	 *
253
	 * @since 6.0.0
254
	 */
255
	public function on_admin_menu() {
256
		foreach ( $this->get_primary_pages() as $page ) {
257
			$page->register();
258
		}
259
260
		\Bulk_Delete_Misc::add_menu();
261
262
		/**
263
		 * Runs just after adding all *delete* menu items to Bulk WP main menu.
264
		 *
265
		 * This action is primarily for adding extra *delete* menu items to the Bulk WP main menu.
266
		 *
267
		 * @since 5.3
268
		 */
269
		do_action( 'bd_after_primary_menus' );
270
271
		/**
272
		 * Runs just before adding non-action menu items to Bulk WP main menu.
273
		 *
274
		 * This action is primarily for adding extra menu items before non-action menu items to the Bulk WP main menu.
275
		 *
276
		 * @since 5.3
277
		 */
278
		do_action( 'bd_before_secondary_menus' );
279
280
		foreach ( $this->get_secondary_pages() as $page ) {
281
			$page->register();
282
		}
283
284
		$this->addon_page = add_submenu_page(
0 ignored issues
show
Bug Best Practice introduced by
The property addon_page does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
285
			\Bulk_Delete::POSTS_PAGE_SLUG,
286
			__( 'Addon Licenses', 'bulk-delete' ),
287
			__( 'Addon Licenses', 'bulk-delete' ),
288
			'activate_plugins',
289
			\Bulk_Delete::ADDON_PAGE_SLUG,
290
			array( 'BD_License', 'display_addon_page' )
291
		);
292
293
		/**
294
		 * Runs just after adding all menu items to Bulk WP main menu.
295
		 *
296
		 * This action is primarily for adding extra menu items to the Bulk WP main menu.
297
		 *
298
		 * @since 5.3
299
		 */
300
		do_action( 'bd_after_all_menus' );
301
	}
302
303
	/**
304
	 * Get the list of registered admin pages.
305
	 *
306
	 * @since 6.0.0
307
	 *
308
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] List of Primary Admin pages.
309
	 */
310
	public function get_primary_pages() {
311
		if ( empty( $this->primary_pages ) ) {
312
			$posts_page = $this->get_delete_posts_admin_page();
313
			$pages_page = $this->get_delete_pages_admin_page();
314
			$users_page = $this->get_delete_users_admin_page();
315
			$metas_page = $this->get_delete_metas_admin_page();
316
			$terms_page = $this->get_delete_terms_admin_page();
317
318
			$this->primary_pages[ $posts_page->get_page_slug() ] = $posts_page;
319
			$this->primary_pages[ $pages_page->get_page_slug() ] = $pages_page;
320
			$this->primary_pages[ $users_page->get_page_slug() ] = $users_page;
321
			$this->primary_pages[ $metas_page->get_page_slug() ] = $metas_page;
322
			$this->primary_pages[ $terms_page->get_page_slug() ] = $terms_page;
323
		}
324
325
		/**
326
		 * List of primary admin pages.
327
		 *
328
		 * @since 6.0.0
329
		 *
330
		 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] List of Admin pages.
331
		 */
332
		return apply_filters( 'bd_primary_pages', $this->primary_pages );
333
	}
334
335
	/**
336
	 * Get Bulk Delete Posts admin page.
337
	 *
338
	 * @return \BulkWP\BulkDelete\Core\Posts\DeletePostsPage
339
	 */
340
	private function get_delete_posts_admin_page() {
341
		$posts_page = new DeletePostsPage( $this->get_plugin_file() );
342
343
		$posts_page->add_module( new DeletePostsByStatusModule() );
344
		$posts_page->add_module( new DeletePostsByCategoryModule() );
345
		$posts_page->add_module( new DeletePostsByTagModule() );
346
		$posts_page->add_module( new DeletePostsByTaxonomyModule() );
347
		$posts_page->add_module( new DeletePostsByPostTypeModule() );
348
		$posts_page->add_module( new DeletePostsByCommentsModule() );
349
		$posts_page->add_module( new DeletePostsByURLModule() );
350
		$posts_page->add_module( new DeletePostsByRevisionModule() );
351
		$posts_page->add_module( new DeletePostsByStickyPostModule() );
352
353
		/**
354
		 * After the modules are registered in the delete posts page.
355
		 *
356
		 * @since 6.0.0
357
		 *
358
		 * @param DeletePostsPage $posts_page The page in which the modules are registered.
359
		 */
360
		do_action( "bd_after_{$posts_page->get_item_type()}_modules", $posts_page );
361
362
		/**
363
		 * After the modules are registered in a delete page.
364
		 *
365
		 * @since 6.0.0
366
		 *
367
		 * @param BasePage $posts_page The page in which the modules are registered.
368
		 */
369
		do_action( 'bd_after_modules', $posts_page );
370
371
		return $posts_page;
372
	}
373
374
	/**
375
	 * Get Bulk Delete Pages admin page.
376
	 *
377
	 * @since 6.0.0
378
	 *
379
	 * @return \BulkWP\BulkDelete\Core\Pages\DeletePagesPage
380
	 */
381
	private function get_delete_pages_admin_page() {
382
		$pages_page = new DeletePagesPage( $this->get_plugin_file() );
383
384
		$pages_page->add_module( new DeletePagesByStatusModule() );
385
386
		/**
387
		 * After the modules are registered in the delete pages page.
388
		 *
389
		 * @since 6.0.0
390
		 *
391
		 * @param DeletePagesPage $pages_page The page in which the modules are registered.
392
		 */
393
		do_action( "bd_after_{$pages_page->get_item_type()}_modules", $pages_page );
394
395
		/**
396
		 * After the modules are registered in a delete page.
397
		 *
398
		 * @since 6.0.0
399
		 *
400
		 * @param BasePage $pages_page The page in which the modules are registered.
401
		 */
402
		do_action( 'bd_after_modules', $pages_page );
403
404
		return $pages_page;
405
	}
406
407
	/**
408
	 * Get Bulk Delete Users admin page.
409
	 *
410
	 * @since 6.0.0
411
	 *
412
	 * @return \BulkWP\BulkDelete\Core\Users\DeleteUsersPage
413
	 */
414
	private function get_delete_users_admin_page() {
415
		$users_page = new DeleteUsersPage( $this->get_plugin_file() );
416
417
		$users_page->add_module( new DeleteUsersByUserRoleModule() );
418
		$users_page->add_module( new DeleteUsersByUserMetaModule() );
419
420
		/**
421
		 * After the modules are registered in the delete users page.
422
		 *
423
		 * @since 6.0.0
424
		 *
425
		 * @param DeleteUsersPage $users_page The page in which the modules are registered.
426
		 */
427
		do_action( "bd_after_{$users_page->get_item_type()}_modules", $users_page );
428
429
		/**
430
		 * After the modules are registered in a delete page.
431
		 *
432
		 * @since 6.0.0
433
		 *
434
		 * @param BasePage $users_page The page in which the modules are registered.
435
		 */
436
		do_action( 'bd_after_modules', $users_page );
437
438
		return $users_page;
439
	}
440
441
	/**
442
	 * Get Bulk Delete Metas admin page.
443
	 *
444
	 * @since 6.0.0
445
	 *
446
	 * @return \BulkWP\BulkDelete\Core\Metas\DeleteMetasPage
447
	 */
448
	private function get_delete_metas_admin_page() {
449
		$metas_page = new DeleteMetasPage( $this->get_plugin_file() );
450
451
		$metas_page->add_module( new DeletePostMetaModule() );
452
		$metas_page->add_module( new DeleteUserMetaModule() );
453
		$metas_page->add_module( new DeleteCommentMetaModule() );
454
455
		/**
456
		 * After the modules are registered in the delete metas page.
457
		 *
458
		 * @since 6.0.0
459
		 *
460
		 * @param DeleteMetasPage $metas_page The page in which the modules are registered.
461
		 */
462
		do_action( "bd_after_{$metas_page->get_item_type()}_modules", $metas_page );
463
464
		/**
465
		 * After the modules are registered in a delete page.
466
		 *
467
		 * @since 6.0.0
468
		 *
469
		 * @param BasePage $metas_page The page in which the modules are registered.
470
		 */
471
		do_action( 'bd_after_modules', $metas_page );
472
473
		return $metas_page;
474
	}
475
476
	/**
477
	 * Get Bulk Delete Terms admin page.
478
	 *
479
	 * @since 6.0.0
480
	 *
481
	 * @return \BulkWP\BulkDelete\Core\Terms\DeleteTermsPage
482
	 */
483
	private function get_delete_terms_admin_page() {
484
		$terms_page = new DeleteTermsPage( $this->get_plugin_file() );
485
486
		$terms_page->add_module( new DeleteTermsByNameModule() );
487
		$terms_page->add_module( new DeleteTermsByPostCountModule() );
488
489
		return $terms_page;
490
	}
491
492
	/**
493
	 * Get the Cron List admin page.
494
	 *
495
	 * @since 6.0.0
496
	 *
497
	 * @return \BulkWP\BulkDelete\Core\Cron\CronListPage
498
	 */
499
	private function get_cron_list_admin_page() {
500
		$cron_list_page = new CronListPage( $this->get_plugin_file() );
501
502
		return $cron_list_page;
503
	}
504
505
	/**
506
	 * Get the System Info page.
507
	 *
508
	 * @since 6.0.0
509
	 *
510
	 * @return \BulkWP\BulkDelete\Core\SystemInfo\SystemInfoPage
511
	 */
512
	private function get_system_info_page() {
513
		$system_info_page = new SystemInfoPage( $this->get_plugin_file() );
514
515
		return $system_info_page;
516
	}
517
518
	/**
519
	 * Get the list of secondary pages.
520
	 *
521
	 * @return BasePage[] Secondary Pages.
522
	 */
523
	private function get_secondary_pages() {
524
		if ( empty( $this->secondary_pages ) ) {
525
			$cron_list_page   = $this->get_cron_list_admin_page();
526
			$system_info_page = $this->get_system_info_page();
527
528
			$this->secondary_pages[ $cron_list_page->get_page_slug() ]   = $cron_list_page;
529
			$this->secondary_pages[ $system_info_page->get_page_slug() ] = $system_info_page;
530
		}
531
532
		/**
533
		 * List of secondary admin pages.
534
		 *
535
		 * @since 6.0.0
536
		 *
537
		 * @param BasePage[] List of Admin pages.
538
		 */
539
		return apply_filters( 'bd_secondary_pages', $this->secondary_pages );
540
	}
541
542
	/**
543
	 * Get path to main plugin file.
544
	 *
545
	 * @return string Plugin file.
546
	 */
547
	public function get_plugin_file() {
548
		return $this->plugin_file;
549
	}
550
551
	/**
552
	 * Set path to main plugin file.
553
	 *
554
	 * @param string $plugin_file Path to main plugin file.
555
	 */
556
	public function set_plugin_file( $plugin_file ) {
557
		$this->plugin_file       = $plugin_file;
558
		$this->translations_path = dirname( plugin_basename( $this->get_plugin_file() ) ) . '/languages/';
559
	}
560
561
	/**
562
	 * Get path to translations.
563
	 *
564
	 * @return string Translations path.
565
	 */
566
	public function get_translations_path() {
567
		return $this->translations_path;
568
	}
569
570
	/**
571
	 * Get the hook suffix of a page.
572
	 *
573
	 * @param string $page_slug Page slug.
574
	 *
575
	 * @return string|null Hook suffix if found, null otherwise.
576
	 */
577
	public function get_page_hook_suffix( $page_slug ) {
578
		$admin_page = '';
579
580
		if ( array_key_exists( $page_slug, $this->get_primary_pages() ) ) {
581
			$admin_page = $this->primary_pages[ $page_slug ];
582
		}
583
584
		if ( array_key_exists( $page_slug, $this->get_secondary_pages() ) ) {
585
			$admin_page = $this->secondary_pages[ $page_slug ];
586
		}
587
588
		if ( $admin_page instanceof BasePage ) {
589
			return $admin_page->get_hook_suffix();
590
		}
591
592
		return null;
593
	}
594
595
	/**
596
	 * Register Add-on Namespace.
597
	 *
598
	 * @param \BulkWP\BulkDelete\Core\Addon\AddonInfo $addon_info Add-on Info.
599
	 */
600
	public function register_addon_namespace( $addon_info ) {
601
		$this->loader->add_namespace( 'BulkWP\BulkDelete', $addon_info->get_addon_directory() . 'includes' );
602
	}
603
604
	/**
605
	 * Setter for Autoloader.
606
	 *
607
	 * @param \BulkWP\BulkDelete\BulkDeleteAutoloader $loader Autoloader.
608
	 */
609
	public function set_loader( $loader ) {
610
		$this->loader = $loader;
611
	}
612
613
	/**
614
	 * Get the module object instance by page slug and module class name.
615
	 *
616
	 * @param string $page_slug         Page Slug.
617
	 * @param string $module_class_name Module class name.
618
	 *
619
	 * @return \BulkWP\BulkDelete\Core\Base\BaseModule|null Module object instance or null if no match found.
620
	 */
621
	public function get_module( $page_slug, $module_class_name ) {
622
		$page = $this->get_page( $page_slug );
623
624
		if ( is_null( $page ) ) {
625
			return null;
626
		}
627
628
		return $page->get_module( $module_class_name );
629
	}
630
631
	/**
632
	 * Get the page object instance by page slug.
633
	 *
634
	 * @param string $page_slug Page slug.
635
	 *
636
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage|null Page object instance or null if no match found.
637
	 */
638
	public function get_page( $page_slug ) {
639
		$pages = $this->get_primary_pages();
640
641
		if ( ! isset( $pages[ $page_slug ] ) ) {
642
			return null;
643
		}
644
645
		return $pages[ $page_slug ];
646
	}
647
}
648