Passed
Push — 249-fix/delete-posts-by-custom... ( e6cf34...f27197 )
by Rajan
12:28
created

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

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