Completed
Push — 151-fix/delete-buddy-press-pen... ( ee62d7 )
by
unknown
11:24
created

BulkDelete   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 689
Duplicated Lines 0 %

Test Coverage

Coverage 11.88%

Importance

Changes 8
Bugs 0 Features 1
Metric Value
eloc 142
c 8
b 0
f 1
dl 0
loc 689
ccs 19
cts 160
cp 0.1188
rs 8.8
wmc 45

30 Methods

Rating   Name   Duplication   Size   Complexity  
A get_module() 0 8 2
A load_primary_pages() 0 23 1
A __clone() 0 2 1
A get_delete_posts_admin_page() 0 32 1
A setup_actions() 0 4 1
A set_loader() 0 2 1
A set_plugin_file() 0 3 1
A get_delete_metas_admin_page() 0 27 1
A get_delete_terms_admin_page() 0 25 1
A get_system_info_page() 0 4 1
A get_delete_comments_admin_page() 0 25 1
A get_primary_pages() 0 6 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 46 3
A get_secondary_pages() 0 17 2
A get_instance() 0 6 3
A register_addon_namespace() 0 2 1
A get_page() 0 8 2
A get_delete_users_admin_page() 0 26 1
A get_cron_list_admin_page() 0 4 1
A get_translations_path() 0 2 1
A load() 0 24 3
A get_plugin_file() 0 2 1
A get_delete_pages_admin_page() 0 24 1
A get_page_hook_suffix() 0 16 4
A on_init() 0 2 1
A is_admin_or_cron() 0 2 3

How to fix   Complexity   

Complex Class

Complex classes like BulkDelete often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BulkDelete, and based on these observations, apply Extract Interface, too.

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\Comments\DeleteCommentsPage;
8
use BulkWP\BulkDelete\Core\Comments\Modules\DeleteCommentsByAuthorModule;
9
use BulkWP\BulkDelete\Core\Comments\Modules\DeleteCommentsByIPModule;
10
use BulkWP\BulkDelete\Core\Cron\CronListPage;
11
use BulkWP\BulkDelete\Core\Metas\DeleteMetasPage;
12
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteCommentMetaModule;
13
use BulkWP\BulkDelete\Core\Metas\Modules\DeletePostMetaModule;
14
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteTermMetaModule;
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\DeleteBPPendingUsersModule;
34
use BulkWP\BulkDelete\Core\Users\Modules\DeleteUsersByUserMetaModule;
35
use BulkWP\BulkDelete\Core\Users\Modules\DeleteUsersByUserRoleModule;
36
37 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
38
39
/**
40
 * Main Plugin class.
41
 *
42
 * @since 5.0 Converted to Singleton
43
 * @since 6.0.0 Renamed to BulkDelete and added namespace.
44
 */
45
final class BulkDelete {
46
	/**
47
	 * The one true BulkDelete instance.
48
	 *
49
	 * @var BulkDelete
50
	 *
51
	 * @since 5.0
52
	 */
53
	private static $instance;
54
55
	/**
56
	 * Path to the main plugin file.
57
	 *
58
	 * @var string
59
	 */
60
	private $plugin_file;
61
62
	/**
63
	 * Path where translations are stored.
64
	 *
65
	 * @var string
66
	 */
67
	private $translations_path;
68
69
	/**
70
	 * Has the plugin loaded?
71
	 *
72
	 * @since 6.0.0
73
	 *
74
	 * @var bool
75
	 */
76
	private $loaded = false;
77
78
	/**
79
	 * Controller that handles all requests and nonce checks.
80
	 *
81
	 * @var \BulkWP\BulkDelete\Core\Controller
82
	 */
83
	private $controller;
84
85
	/**
86
	 * Upseller responsible for upselling add-ons.
87
	 *
88
	 * @since 6.0.0
89
	 *
90
	 * @var \BulkWP\BulkDelete\Core\Addon\Upseller
91
	 */
92
	private $upseller;
93
94
	/**
95
	 * Bulk Delete Autoloader.
96
	 *
97
	 * Will be used by add-ons to extend the namespace.
98
	 *
99
	 * @var \BulkWP\BulkDelete\BulkDeleteAutoloader
100
	 */
101
	private $loader;
102
103
	/**
104
	 * List of Primary Admin pages.
105
	 *
106
	 * @var \BulkWP\BulkDelete\Core\Base\BaseDeletePage[]
107
	 *
108
	 * @since 6.0.0
109
	 */
110
	private $primary_pages = array();
111
112
	/**
113
	 * List of Secondary Admin pages.
114
	 *
115
	 * @var BasePage[]
116
	 *
117
	 * @since 6.0.0
118
	 */
119
	private $secondary_pages = array();
120
121
	/**
122
	 * Plugin version.
123
	 */
124
	const VERSION = '6.0.2';
125
126
	/**
127
	 * Set the BulkDelete constructor as private.
128
	 *
129
	 * An instance should be created by calling the `get_instance` method.
130
	 *
131
	 * @see BulkDelete::get_instance()
132
	 */
133
	private function __construct() {}
134
135
	/**
136
	 * Main BulkDelete Instance.
137
	 *
138
	 * Insures that only one instance of BulkDelete exists in memory at any one
139
	 * time. Also prevents needing to define globals all over the place.
140
	 *
141
	 * @since     5.0
142
	 * @static
143
	 * @staticvar array $instance
144
	 *
145
	 * @return BulkDelete The one true instance of BulkDelete.
146
	 */
147 4
	public static function get_instance() {
148 4
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof BulkDelete ) ) {
149 1
			self::$instance = new BulkDelete();
150
		}
151
152 4
		return self::$instance;
153
	}
154
155
	/**
156
	 * Load the plugin if it is not loaded.
157
	 * The plugin will be loaded only it is an admin request or a cron request.
158
	 *
159
	 * This function will be invoked in the `plugins_loaded` hook.
160
	 */
161
	public function load() {
162
		if ( $this->loaded ) {
163
			return;
164
		}
165
166
		if ( ! $this->is_admin_or_cron() ) {
167
			return;
168
		}
169
170
		$this->load_dependencies();
171
		$this->setup_actions();
172
173
		$this->loaded = true;
174
175
		/**
176
		 * Bulk Delete plugin loaded.
177
		 *
178
		 * @since 6.0.0
179
		 *
180
		 * @param string Plugin main file.
181
		 */
182
		do_action( 'bd_loaded', $this->get_plugin_file() );
183
184
		$this->load_primary_pages();
185
	}
186
187
	/**
188
	 * Throw error on object clone.
189
	 *
190
	 * The whole idea of the singleton design pattern is that there is a single
191
	 * object therefore, we don't want the object to be cloned.
192
	 *
193
	 * @since  5.0
194
	 * @access protected
195
	 *
196
	 * @return void
197
	 */
198 1
	public function __clone() {
199 1
		_doing_it_wrong( __FUNCTION__, __( "This class can't be cloned. Use `get_instance()` method to get an instance.", 'bulk-delete' ), '5.0' );
200
	}
201
202
	/**
203
	 * Disable unserializing of the class.
204
	 *
205
	 * @since  5.0
206
	 * @access protected
207
	 *
208
	 * @return void
209
	 */
210 1
	public function __wakeup() {
211 1
		_doing_it_wrong( __FUNCTION__, __( "This class can't be serialized. Use `get_instance()` method to get an instance.", 'bulk-delete' ), '5.0' );
212
	}
213
214
	/**
215
	 * Load all dependencies.
216
	 *
217
	 * @since 6.0.0
218
	 */
219
	private function load_dependencies() {
220
		$this->controller = new Controller();
221
		$this->controller->load();
222
223
		$this->upseller = new Upseller();
224
		$this->upseller->load();
225
	}
226
227
	/**
228
	 * Loads the plugin's actions and hooks.
229
	 *
230
	 * @access private
231
	 *
232
	 * @since  5.0
233
	 *
234
	 * @return void
235
	 */
236
	private function setup_actions() {
237
		add_action( 'init', array( $this, 'on_init' ) );
238
239
		add_action( 'admin_menu', array( $this, 'on_admin_menu' ) );
240
	}
241
242
	/**
243
	 * Triggered when the `init` hook is fired.
244
	 *
245
	 * @since 6.0.0
246
	 */
247 1
	public function on_init() {
248 1
		$this->load_textdomain();
249
	}
250
251
	/**
252
	 * Loads the plugin language files.
253
	 *
254
	 * @since  5.0
255
	 */
256 1
	private function load_textdomain() {
257 1
		load_plugin_textdomain( 'bulk-delete', false, $this->get_translations_path() );
258
	}
259
260
	/**
261
	 * Triggered when the `admin_menu` hook is fired.
262
	 *
263
	 * Register all admin pages.
264
	 *
265
	 * @since 6.0.0
266
	 */
267
	public function on_admin_menu() {
268
		foreach ( $this->get_primary_pages() as $page ) {
269
			$page->register();
270
		}
271
272
		\Bulk_Delete_Misc::add_menu();
273
274
		/**
275
		 * Runs just after adding all *delete* menu items to Bulk WP main menu.
276
		 *
277
		 * This action is primarily for adding extra *delete* menu items to the Bulk WP main menu.
278
		 *
279
		 * @since 5.3
280
		 */
281
		do_action( 'bd_after_primary_menus' );
282
283
		/**
284
		 * Runs just before adding non-action menu items to Bulk WP main menu.
285
		 *
286
		 * This action is primarily for adding extra menu items before non-action menu items to the Bulk WP main menu.
287
		 *
288
		 * @since 5.3
289
		 */
290
		do_action( 'bd_before_secondary_menus' );
291
292
		foreach ( $this->get_secondary_pages() as $page ) {
293
			$page->register();
294
		}
295
296
		$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...
297
			\Bulk_Delete::POSTS_PAGE_SLUG,
298
			__( 'Addon Licenses', 'bulk-delete' ),
299
			__( 'Addon Licenses', 'bulk-delete' ),
300
			'activate_plugins',
301
			\Bulk_Delete::ADDON_PAGE_SLUG,
302
			array( 'BD_License', 'display_addon_page' )
303
		);
304
305
		/**
306
		 * Runs just after adding all menu items to Bulk WP main menu.
307
		 *
308
		 * This action is primarily for adding extra menu items to the Bulk WP main menu.
309
		 *
310
		 * @since 5.3
311
		 */
312
		do_action( 'bd_after_all_menus' );
313
	}
314
315
	/**
316
	 * Get the list of registered admin pages.
317
	 *
318
	 * @since 6.0.0
319
	 *
320
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] List of Primary Admin pages.
321
	 */
322
	private function get_primary_pages() {
323
		if ( empty( $this->primary_pages ) ) {
324
			$this->load_primary_pages();
325
		}
326
327
		return $this->primary_pages;
328
	}
329
330
	/**
331
	 * Load Primary admin pages.
332
	 *
333
	 * The pages need to be loaded in `init` hook, since the association between page and modules is needed in cron requests.
334
	 */
335
	private function load_primary_pages() {
336
		$posts_page    = $this->get_delete_posts_admin_page();
337
		$pages_page    = $this->get_delete_pages_admin_page();
338
		$users_page    = $this->get_delete_users_admin_page();
339
		$comments_page = $this->get_delete_comments_admin_page();
340
		$metas_page    = $this->get_delete_metas_admin_page();
341
		$terms_page    = $this->get_delete_terms_admin_page();
342
343
		$this->primary_pages[ $posts_page->get_page_slug() ]    = $posts_page;
344
		$this->primary_pages[ $pages_page->get_page_slug() ]    = $pages_page;
345
		$this->primary_pages[ $users_page->get_page_slug() ]    = $users_page;
346
		$this->primary_pages[ $comments_page->get_page_slug() ] = $comments_page;
347
		$this->primary_pages[ $metas_page->get_page_slug() ]    = $metas_page;
348
		$this->primary_pages[ $terms_page->get_page_slug() ]    = $terms_page;
349
350
		/**
351
		 * List of primary admin pages.
352
		 *
353
		 * @since 6.0.0
354
		 *
355
		 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] List of Admin pages.
356
		 */
357
		$this->primary_pages = apply_filters( 'bd_primary_pages', $this->primary_pages );
358
	}
359
360
	/**
361
	 * Get Bulk Delete Posts admin page.
362
	 *
363
	 * @return \BulkWP\BulkDelete\Core\Posts\DeletePostsPage
364
	 */
365
	private function get_delete_posts_admin_page() {
366
		$posts_page = new DeletePostsPage( $this->get_plugin_file() );
367
368
		$posts_page->add_module( new DeletePostsByStatusModule() );
369
		$posts_page->add_module( new DeletePostsByCategoryModule() );
370
		$posts_page->add_module( new DeletePostsByTagModule() );
371
		$posts_page->add_module( new DeletePostsByTaxonomyModule() );
372
		$posts_page->add_module( new DeletePostsByPostTypeModule() );
373
		$posts_page->add_module( new DeletePostsByCommentsModule() );
374
		$posts_page->add_module( new DeletePostsByURLModule() );
375
		$posts_page->add_module( new DeletePostsByRevisionModule() );
376
		$posts_page->add_module( new DeletePostsByStickyPostModule() );
377
378
		/**
379
		 * After the modules are registered in the delete posts page.
380
		 *
381
		 * @since 6.0.0
382
		 *
383
		 * @param DeletePostsPage $posts_page The page in which the modules are registered.
384
		 */
385
		do_action( "bd_after_modules_{$posts_page->get_page_slug()}", $posts_page );
386
387
		/**
388
		 * After the modules are registered in a delete page.
389
		 *
390
		 * @since 6.0.0
391
		 *
392
		 * @param BasePage $posts_page The page in which the modules are registered.
393
		 */
394
		do_action( 'bd_after_modules', $posts_page );
395
396
		return $posts_page;
397
	}
398
399
	/**
400
	 * Get Bulk Delete Pages admin page.
401
	 *
402
	 * @since 6.0.0
403
	 *
404
	 * @return \BulkWP\BulkDelete\Core\Pages\DeletePagesPage
405
	 */
406
	private function get_delete_pages_admin_page() {
407
		$pages_page = new DeletePagesPage( $this->get_plugin_file() );
408
409
		$pages_page->add_module( new DeletePagesByStatusModule() );
410
411
		/**
412
		 * After the modules are registered in the delete pages page.
413
		 *
414
		 * @since 6.0.0
415
		 *
416
		 * @param DeletePagesPage $pages_page The page in which the modules are registered.
417
		 */
418
		do_action( "bd_after_modules_{$pages_page->get_page_slug()}", $pages_page );
419
420
		/**
421
		 * After the modules are registered in a delete page.
422
		 *
423
		 * @since 6.0.0
424
		 *
425
		 * @param BasePage $pages_page The page in which the modules are registered.
426
		 */
427
		do_action( 'bd_after_modules', $pages_page );
428
429
		return $pages_page;
430
	}
431
432
	/**
433
	 * Get Bulk Delete Comments admin page.
434
	 *
435
	 * @since 6.1.0
436
	 *
437
	 * @return \BulkWP\BulkDelete\Core\Comments\DeleteCommentsPage
438
	 */
439
	private function get_delete_comments_admin_page() {
440
		$comments_page = new DeleteCommentsPage( $this->get_plugin_file() );
441
442
		$comments_page->add_module( new DeleteCommentsByAuthorModule() );
443
		$comments_page->add_module( new DeleteCommentsByIPModule() );
444
445
		/**
446
		 * After the modules are registered in the delete comments page.
447
		 *
448
		 * @since 6.0.0
449
		 *
450
		 * @param DeleteCommentsPage $comments_page The page in which the modules are registered.
451
		 */
452
		do_action( "bd_after_modules_{$comments_page->get_page_slug()}", $comments_page );
453
454
		/**
455
		 * After the modules are registered in a delete comments page.
456
		 *
457
		 * @since 6.0.0
458
		 *
459
		 * @param BasePage $comments_page The page in which the modules are registered.
460
		 */
461
		do_action( 'bd_after_modules', $comments_page );
462
463
		return $comments_page;
464
	}
465
466
	/**
467
	 * Get Bulk Delete Users admin page.
468
	 *
469
	 * @since 6.0.0
470
	 *
471
	 * @return \BulkWP\BulkDelete\Core\Users\DeleteUsersPage
472
	 */
473
	private function get_delete_users_admin_page() {
474
		$users_page = new DeleteUsersPage( $this->get_plugin_file() );
475
476
		$users_page->add_module( new DeleteUsersByUserRoleModule() );
477
		$users_page->add_module( new DeleteUsersByUserMetaModule() );
478
		$users_page->add_module( new DeleteBPPendingUsersModule() );
479
480
		/**
481
		 * After the modules are registered in the delete users page.
482
		 *
483
		 * @since 6.0.0
484
		 *
485
		 * @param DeleteUsersPage $users_page The page in which the modules are registered.
486
		 */
487
		do_action( "bd_after_modules_{$users_page->get_page_slug()}", $users_page );
488
489
		/**
490
		 * After the modules are registered in a delete page.
491
		 *
492
		 * @since 6.0.0
493
		 *
494
		 * @param BasePage $users_page The page in which the modules are registered.
495
		 */
496
		do_action( 'bd_after_modules', $users_page );
497
498
		return $users_page;
499
	}
500
501
	/**
502
	 * Get Bulk Delete Metas admin page.
503
	 *
504
	 * @since 6.0.0
505
	 *
506
	 * @return \BulkWP\BulkDelete\Core\Metas\DeleteMetasPage
507
	 */
508
	private function get_delete_metas_admin_page() {
509
		$metas_page = new DeleteMetasPage( $this->get_plugin_file() );
510
511
		$metas_page->add_module( new DeletePostMetaModule() );
512
		$metas_page->add_module( new DeleteUserMetaModule() );
513
		$metas_page->add_module( new DeleteTermMetaModule() );
514
		$metas_page->add_module( new DeleteCommentMetaModule() );
515
516
		/**
517
		 * After the modules are registered in the delete metas page.
518
		 *
519
		 * @since 6.0.0
520
		 *
521
		 * @param DeleteMetasPage $metas_page The page in which the modules are registered.
522
		 */
523
		do_action( "bd_after_modules_{$metas_page->get_page_slug()}", $metas_page );
524
525
		/**
526
		 * After the modules are registered in a delete page.
527
		 *
528
		 * @since 6.0.0
529
		 *
530
		 * @param BasePage $metas_page The page in which the modules are registered.
531
		 */
532
		do_action( 'bd_after_modules', $metas_page );
533
534
		return $metas_page;
535
	}
536
537
	/**
538
	 * Get Bulk Delete Terms admin page.
539
	 *
540
	 * @since 6.0.0
541
	 *
542
	 * @return \BulkWP\BulkDelete\Core\Terms\DeleteTermsPage
543
	 */
544
	private function get_delete_terms_admin_page() {
545
		$terms_page = new DeleteTermsPage( $this->get_plugin_file() );
546
547
		$terms_page->add_module( new DeleteTermsByNameModule() );
548
		$terms_page->add_module( new DeleteTermsByPostCountModule() );
549
550
		/**
551
		 * After the modules are registered in the delete terms page.
552
		 *
553
		 * @since 6.0.0
554
		 *
555
		 * @param DeleteTermsPage $terms_page The page in which the modules are registered.
556
		 */
557
		do_action( "bd_after_modules_{$terms_page->get_page_slug()}", $terms_page );
558
559
		/**
560
		 * After the modules are registered in a delete page.
561
		 *
562
		 * @since 6.0.0
563
		 *
564
		 * @param BasePage $terms_page The page in which the modules are registered.
565
		 */
566
		do_action( 'bd_after_modules', $terms_page );
567
568
		return $terms_page;
569
	}
570
571
	/**
572
	 * Get the Cron List admin page.
573
	 *
574
	 * @since 6.0.0
575
	 *
576
	 * @return \BulkWP\BulkDelete\Core\Cron\CronListPage
577
	 */
578
	private function get_cron_list_admin_page() {
579
		$cron_list_page = new CronListPage( $this->get_plugin_file() );
580
581
		return $cron_list_page;
582
	}
583
584
	/**
585
	 * Get the System Info page.
586
	 *
587
	 * @since 6.0.0
588
	 *
589
	 * @return \BulkWP\BulkDelete\Core\SystemInfo\SystemInfoPage
590
	 */
591
	private function get_system_info_page() {
592
		$system_info_page = new SystemInfoPage( $this->get_plugin_file() );
593
594
		return $system_info_page;
595
	}
596
597
	/**
598
	 * Get the list of secondary pages.
599
	 *
600
	 * @return BasePage[] Secondary Pages.
601
	 */
602
	private function get_secondary_pages() {
603
		if ( empty( $this->secondary_pages ) ) {
604
			$cron_list_page   = $this->get_cron_list_admin_page();
605
			$system_info_page = $this->get_system_info_page();
606
607
			$this->secondary_pages[ $cron_list_page->get_page_slug() ]   = $cron_list_page;
608
			$this->secondary_pages[ $system_info_page->get_page_slug() ] = $system_info_page;
609
		}
610
611
		/**
612
		 * List of secondary admin pages.
613
		 *
614
		 * @since 6.0.0
615
		 *
616
		 * @param BasePage[] List of Admin pages.
617
		 */
618
		return apply_filters( 'bd_secondary_pages', $this->secondary_pages );
619
	}
620
621
	/**
622
	 * Get path to main plugin file.
623
	 *
624
	 * @return string Plugin file.
625
	 */
626 1
	public function get_plugin_file() {
627 1
		return $this->plugin_file;
628
	}
629
630
	/**
631
	 * Set path to main plugin file.
632
	 *
633
	 * @param string $plugin_file Path to main plugin file.
634
	 */
635 1
	public function set_plugin_file( $plugin_file ) {
636 1
		$this->plugin_file       = $plugin_file;
637 1
		$this->translations_path = dirname( plugin_basename( $this->get_plugin_file() ) ) . '/languages/';
638
	}
639
640
	/**
641
	 * Get path to translations.
642
	 *
643
	 * @return string Translations path.
644
	 */
645 1
	public function get_translations_path() {
646 1
		return $this->translations_path;
647
	}
648
649
	/**
650
	 * Get the hook suffix of a page.
651
	 *
652
	 * @param string $page_slug Page slug.
653
	 *
654
	 * @return string|null Hook suffix if found, null otherwise.
655
	 */
656
	public function get_page_hook_suffix( $page_slug ) {
657
		$admin_page = '';
658
659
		if ( array_key_exists( $page_slug, $this->get_primary_pages() ) ) {
660
			$admin_page = $this->primary_pages[ $page_slug ];
661
		}
662
663
		if ( array_key_exists( $page_slug, $this->get_secondary_pages() ) ) {
664
			$admin_page = $this->secondary_pages[ $page_slug ];
665
		}
666
667
		if ( $admin_page instanceof BasePage ) {
668
			return $admin_page->get_hook_suffix();
669
		}
670
671
		return null;
672
	}
673
674
	/**
675
	 * Register Add-on Namespace.
676
	 *
677
	 * @param \BulkWP\BulkDelete\Core\Addon\AddonInfo $addon_info Add-on Info.
678
	 */
679
	public function register_addon_namespace( $addon_info ) {
680
		$this->loader->add_namespace( 'BulkWP\BulkDelete', $addon_info->get_addon_directory() . 'includes' );
681
	}
682
683
	/**
684
	 * Setter for Autoloader.
685
	 *
686
	 * @param \BulkWP\BulkDelete\BulkDeleteAutoloader $loader Autoloader.
687
	 */
688
	public function set_loader( $loader ) {
689
		$this->loader = $loader;
690
	}
691
692
	/**
693
	 * Get the module object instance by page slug and module class name.
694
	 *
695
	 * @param string $page_slug         Page Slug.
696
	 * @param string $module_class_name Module class name.
697
	 *
698
	 * @return \BulkWP\BulkDelete\Core\Base\BaseModule|null Module object instance or null if no match found.
699
	 */
700
	public function get_module( $page_slug, $module_class_name ) {
701
		$page = $this->get_page( $page_slug );
702
703
		if ( is_null( $page ) ) {
704
			return null;
705
		}
706
707
		return $page->get_module( $module_class_name );
708
	}
709
710
	/**
711
	 * Get the page object instance by page slug.
712
	 *
713
	 * @param string $page_slug Page slug.
714
	 *
715
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage|null Page object instance or null if no match found.
716
	 */
717
	public function get_page( $page_slug ) {
718
		$pages = $this->get_primary_pages();
719
720
		if ( ! isset( $pages[ $page_slug ] ) ) {
721
			return null;
722
		}
723
724
		return $pages[ $page_slug ];
725
	}
726
727
	/**
728
	 * Is the current request an admin or cron request?
729
	 *
730
	 * @return bool True, if yes, False otherwise.
731
	 */
732
	private function is_admin_or_cron() {
733
		return is_admin() || defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] );
734
	}
735
}
736