Passed
Push — 677-feature/add-wp-cli-support ( 8a68ba...d28ac0 )
by Sudar
05:15
created

BulkDelete::is_plugin_needed()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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