Passed
Pull Request — dev/6.0.0 (#445)
by Rajan
20:39 queued 11:49
created

BulkDelete::get_translations_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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