Passed
Push — 272-feature/delete-user-post-m... ( de90f2 )
by Rajan
07:17
created

BulkDelete   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 436
Duplicated Lines 0 %

Test Coverage

Coverage 36.1%

Importance

Changes 0
Metric Value
dl 0
loc 436
ccs 39
cts 108
cp 0.361
rs 9.6
c 0
b 0
f 0
wmc 32

22 Methods

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

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