Completed
Push — 330-fix/delete-user-meta-add-a... ( d70968...8c3efe )
by Sudar
45:44 queued 42:41
created

BulkDelete   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 557
Duplicated Lines 0 %

Test Coverage

Coverage 30.83%

Importance

Changes 0
Metric Value
eloc 112
dl 0
loc 557
ccs 41
cts 133
cp 0.3083
rs 9.68
c 0
b 0
f 0
wmc 34

24 Methods

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

243
		load_plugin_textdomain( 'bulk-delete', /** @scrutinizer ignore-type */ false, $this->get_translations_path() );
Loading history...
244 1
	}
245
246
	/**
247
	 * Triggered when the `admin_menu` hook is fired.
248
	 *
249
	 * Register all admin pages.
250
	 *
251
	 * @since 6.0.0
252
	 */
253
	public function on_admin_menu() {
254
		foreach ( $this->get_primary_pages() as $page ) {
255
			$page->register();
256
		}
257
258
		\Bulk_Delete_Misc::add_menu();
259
260
		/**
261
		 * Runs just after adding all *delete* menu items to Bulk WP main menu.
262
		 *
263
		 * This action is primarily for adding extra *delete* menu items to the Bulk WP main menu.
264
		 *
265
		 * @since 5.3
266
		 */
267
		do_action( 'bd_after_primary_menus' );
268
269
		/**
270
		 * Runs just before adding non-action menu items to Bulk WP main menu.
271
		 *
272
		 * This action is primarily for adding extra menu items before non-action menu items to the Bulk WP main menu.
273
		 *
274
		 * @since 5.3
275
		 */
276
		do_action( 'bd_before_secondary_menus' );
277
278
		foreach ( $this->get_secondary_pages() as $page ) {
279
			$page->register();
280
		}
281
282
		$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...
283
			\Bulk_Delete::POSTS_PAGE_SLUG,
284
			__( 'Addon Licenses', 'bulk-delete' ),
285
			__( 'Addon Licenses', 'bulk-delete' ),
286
			'activate_plugins',
287
			\Bulk_Delete::ADDON_PAGE_SLUG,
288
			array( 'BD_License', 'display_addon_page' )
289
		);
290
291
		\BD_System_Info_page::factory();
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 DeletePostsByURLModule() );
349
		$posts_page->add_module( new DeletePostsByRevisionModule() );
350
		$posts_page->add_module( new DeletePostsByStickyPostModule() );
351
352
		/**
353
		 * After the modules are registered in the delete posts page.
354
		 *
355
		 * @since 6.0.0
356
		 *
357
		 * @param DeletePostsPage $posts_page The page in which the modules are registered.
358
		 */
359
		do_action( "bd_after_{$posts_page->get_item_type()}_modules", $posts_page );
360
361
		/**
362
		 * After the modules are registered in a delete page.
363
		 *
364
		 * @since 6.0.0
365
		 *
366
		 * @param BasePage $posts_page The page in which the modules are registered.
367
		 */
368
		do_action( 'bd_after_modules', $posts_page );
369
370
		return $posts_page;
371
	}
372
373
	/**
374
	 * Get Bulk Delete Pages admin page.
375
	 *
376
	 * @since 6.0.0
377
	 *
378
	 * @return \BulkWP\BulkDelete\Core\Pages\DeletePagesPage
379
	 */
380
	private function get_delete_pages_admin_page() {
381
		$pages_page = new DeletePagesPage( $this->get_plugin_file() );
382
383
		$pages_page->add_module( new DeletePagesByStatusModule() );
384
385
		/**
386
		 * After the modules are registered in the delete pages page.
387
		 *
388
		 * @since 6.0.0
389
		 *
390
		 * @param DeletePagesPage $pages_page The page in which the modules are registered.
391
		 */
392
		do_action( "bd_after_{$pages_page->get_item_type()}_modules", $pages_page );
393
394
		/**
395
		 * After the modules are registered in a delete page.
396
		 *
397
		 * @since 6.0.0
398
		 *
399
		 * @param BasePage $pages_page The page in which the modules are registered.
400
		 */
401
		do_action( 'bd_after_modules', $pages_page );
402
403
		return $pages_page;
404
	}
405
406
	/**
407
	 * Get Bulk Delete Users admin page.
408
	 *
409
	 * @since 6.0.0
410
	 *
411
	 * @return \BulkWP\BulkDelete\Core\Users\DeleteUsersPage
412
	 */
413
	private function get_delete_users_admin_page() {
414
		$users_page = new DeleteUsersPage( $this->get_plugin_file() );
415
416
		$users_page->add_module( new DeleteUsersByUserRoleModule() );
417
		$users_page->add_module( new DeleteUsersByUserMetaModule() );
418
419
		/**
420
		 * After the modules are registered in the delete users page.
421
		 *
422
		 * @since 6.0.0
423
		 *
424
		 * @param DeleteUsersPage $users_page The page in which the modules are registered.
425
		 */
426
		do_action( "bd_after_{$users_page->get_item_type()}_modules", $users_page );
427
428
		/**
429
		 * After the modules are registered in a delete page.
430
		 *
431
		 * @since 6.0.0
432
		 *
433
		 * @param BasePage $users_page The page in which the modules are registered.
434
		 */
435
		do_action( 'bd_after_modules', $users_page );
436
437
		return $users_page;
438
	}
439
440
	/**
441
	 * Get Bulk Delete Metas admin page.
442
	 *
443
	 * @since 6.0.0
444
	 *
445
	 * @return \BulkWP\BulkDelete\Core\Metas\DeleteMetasPage
446
	 */
447
	private function get_delete_metas_admin_page() {
448
		$metas_page = new DeleteMetasPage( $this->get_plugin_file() );
449
450
		$metas_page->add_module( new DeletePostMetaModule() );
451
		$metas_page->add_module( new DeleteUserMetaModule() );
452
		$metas_page->add_module( new DeleteCommentMetaModule() );
453
454
		/**
455
		 * After the modules are registered in the delete metas page.
456
		 *
457
		 * @since 6.0.0
458
		 *
459
		 * @param DeleteMetasPage $metas_page The page in which the modules are registered.
460
		 */
461
		do_action( "bd_after_{$metas_page->get_item_type()}_modules", $metas_page );
462
463
		/**
464
		 * After the modules are registered in a delete page.
465
		 *
466
		 * @since 6.0.0
467
		 *
468
		 * @param BasePage $metas_page The page in which the modules are registered.
469
		 */
470
		do_action( 'bd_after_modules', $metas_page );
471
472
		return $metas_page;
473
	}
474
475
	/**
476
	 * Get Bulk Delete Terms admin page.
477
	 *
478
	 * @since 6.0.0
479
	 *
480
	 * @return \BulkWP\BulkDelete\Core\Terms\DeleteTermsPage
481
	 */
482
	private function get_delete_terms_admin_page() {
483
		$terms_page = new DeleteTermsPage( $this->get_plugin_file() );
484
485
		$terms_page->add_module( new DeleteTermsByNameModule() );
486
		$terms_page->add_module( new DeleteTermsByPostCountModule() );
487
488
		return $terms_page;
489
	}
490
491
	/**
492
	 * Get the Cron List admin page.
493
	 *
494
	 * @since 6.0.0
495
	 *
496
	 * @return \BulkWP\BulkDelete\Core\Cron\CronListPage
497
	 */
498
	private function get_cron_list_admin_page() {
499
		$cron_list_page = new CronListPage( $this->get_plugin_file() );
500
501
		return $cron_list_page;
502
	}
503
504
	/**
505
	 * Get the list of secondary pages.
506
	 *
507
	 * @return BasePage[] Secondary Pages.
508
	 */
509
	private function get_secondary_pages() {
510
		if ( empty( $this->secondary_pages ) ) {
511
			$cron_list_page = $this->get_cron_list_admin_page();
512
513
			$this->secondary_pages[ $cron_list_page->get_page_slug() ] = $cron_list_page;
514
		}
515
516
		/**
517
		 * List of secondary admin pages.
518
		 *
519
		 * @since 6.0.0
520
		 *
521
		 * @param BasePage[] List of Admin pages.
522
		 */
523
		return apply_filters( 'bd_secondary_pages', $this->secondary_pages );
524
	}
525
526
	/**
527
	 * Get path to main plugin file.
528
	 *
529
	 * @return string Plugin file.
530
	 */
531 2
	public function get_plugin_file() {
532 2
		return $this->plugin_file;
533
	}
534
535
	/**
536
	 * Set path to main plugin file.
537
	 *
538
	 * @param string $plugin_file Path to main plugin file.
539
	 */
540 2
	public function set_plugin_file( $plugin_file ) {
541 2
		$this->plugin_file       = $plugin_file;
542 2
		$this->translations_path = dirname( plugin_basename( $this->get_plugin_file() ) ) . '/languages/';
543 2
	}
544
545
	/**
546
	 * Get path to translations.
547
	 *
548
	 * @return string Translations path.
549
	 */
550 1
	public function get_translations_path() {
551 1
		return $this->translations_path;
552
	}
553
554
	/**
555
	 * Get the hook suffix of a page.
556
	 *
557
	 * @param string $page_slug Page slug.
558
	 *
559
	 * @return string|null Hook suffix if found, null otherwise.
560
	 */
561
	public function get_page_hook_suffix( $page_slug ) {
562
		$admin_page = '';
563
564
		if ( array_key_exists( $page_slug, $this->get_primary_pages() ) ) {
565
			$admin_page = $this->primary_pages[ $page_slug ];
566
		}
567
568
		if ( array_key_exists( $page_slug, $this->get_secondary_pages() ) ) {
569
			$admin_page = $this->secondary_pages[ $page_slug ];
570
		}
571
572
		if ( $admin_page instanceof BasePage ) {
573
			return $admin_page->get_hook_suffix();
574
		}
575
576
		return null;
577
	}
578
579
	/**
580
	 * Getter for Autoloader.
581
	 *
582
	 * @return \BulkWP\BulkDelete\BulkDeleteAutoloader
583
	 */
584
	public function get_loader() {
585
		return $this->loader;
586
	}
587
588
	/**
589
	 * Setter for Autoloader.
590
	 *
591
	 * @param \BulkWP\BulkDelete\BulkDeleteAutoloader $loader Autoloader.
592
	 */
593
	public function set_loader( $loader ) {
594
		$this->loader = $loader;
595
	}
596
}
597