Completed
Pull Request — dev/6.0.0 (#332)
by Rajan
13:59 queued 10:56
created

BulkDelete::setup_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

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