Completed
Push — 247-fix/delete-term-meta ( 63325a )
by Rajan
08:28
created

BulkDelete   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 439
Duplicated Lines 0 %

Test Coverage

Coverage 35.14%

Importance

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

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