BulkDelete::set_plugin_file()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
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\Metas\DeleteMetasPage;
9
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteCommentMetaModule;
10
use BulkWP\BulkDelete\Core\Metas\Modules\DeletePostMetaModule;
11
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteUserMetaModule;
12
use BulkWP\BulkDelete\Core\Pages\DeletePagesPage;
13
use BulkWP\BulkDelete\Core\Pages\Modules\DeletePagesByStatusModule;
14
use BulkWP\BulkDelete\Core\Posts\DeletePostsPage;
15
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByCategoryModule;
16
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByCommentsModule;
17
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByPostTypeModule;
18
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByRevisionModule;
19
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByStatusModule;
20
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByStickyPostModule;
21
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByTagModule;
22
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByTaxonomyModule;
23
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByURLModule;
24
use BulkWP\BulkDelete\Core\SystemInfo\SystemInfoPage;
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 1
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 \BulkWP\BulkDelete\Core\Base\BaseDeletePage[]
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.2';
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 4
	public static function get_instance() {
143 4
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof BulkDelete ) ) {
144 1
			self::$instance = new BulkDelete();
145
		}
146
147 4
		return self::$instance;
148
	}
149
150
	/**
151
	 * Load the plugin if it is not loaded.
152
	 * The plugin will be loaded only it is an admin request or a cron request.
153
	 *
154
	 * This function will be invoked in the `plugins_loaded` hook.
155
	 */
156
	public function load() {
157
		if ( $this->loaded ) {
158
			return;
159
		}
160
161
		if ( ! $this->is_admin_or_cron() ) {
162
			return;
163
		}
164
165
		$this->load_dependencies();
166
		$this->setup_actions();
167
168
		$this->loaded = true;
169
170
		/**
171
		 * Bulk Delete plugin loaded.
172
		 *
173
		 * @since 6.0.0
174
		 *
175
		 * @param string Plugin main file.
176
		 */
177
		do_action( 'bd_loaded', $this->get_plugin_file() );
178
179
		$this->load_primary_pages();
180
	}
181
182
	/**
183
	 * Throw error on object clone.
184
	 *
185
	 * The whole idea of the singleton design pattern is that there is a single
186
	 * object therefore, we don't want the object to be cloned.
187
	 *
188
	 * @since  5.0
189
	 * @access protected
190
	 *
191
	 * @return void
192
	 */
193 1
	public function __clone() {
194 1
		_doing_it_wrong( __FUNCTION__, __( "This class can't be cloned. Use `get_instance()` method to get an instance.", 'bulk-delete' ), '5.0' );
195 1
	}
196
197
	/**
198
	 * Disable unserializing of the class.
199
	 *
200
	 * @since  5.0
201
	 * @access protected
202
	 *
203
	 * @return void
204
	 */
205 1
	public function __wakeup() {
206 1
		_doing_it_wrong( __FUNCTION__, __( "This class can't be serialized. Use `get_instance()` method to get an instance.", 'bulk-delete' ), '5.0' );
207 1
	}
208
209
	/**
210
	 * Load all dependencies.
211
	 *
212
	 * @since 6.0.0
213
	 */
214
	private function load_dependencies() {
215
		$this->controller = new Controller();
216
		$this->controller->load();
217
218
		$this->upseller = new Upseller();
219
		$this->upseller->load();
220
	}
221
222
	/**
223
	 * Loads the plugin's actions and hooks.
224
	 *
225
	 * @access private
226
	 *
227
	 * @since  5.0
228
	 *
229
	 * @return void
230
	 */
231
	private function setup_actions() {
232
		add_action( 'init', array( $this, 'on_init' ) );
233
234
		add_action( 'admin_menu', array( $this, 'on_admin_menu' ) );
235
	}
236
237
	/**
238
	 * Triggered when the `init` hook is fired.
239
	 *
240
	 * @since 6.0.0
241
	 */
242 1
	public function on_init() {
243 1
		$this->load_textdomain();
244 1
	}
245
246
	/**
247
	 * Loads the plugin language files.
248
	 *
249
	 * @since  5.0
250
	 */
251 1
	private function load_textdomain() {
252 1
		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

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