Passed
Push — 247-fix/delete-term-meta ( 093d38...957a5a )
by Rajan
74:15 queued 71:04
created

BulkDelete::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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

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