Passed
Push — 193-feature/delete-post-revisi... ( 2fa2e8...6fe7c2 )
by Rajan
13:44 queued 10:01
created

BulkDelete   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 409
Duplicated Lines 0 %

Test Coverage

Coverage 40.21%

Importance

Changes 0
Metric Value
dl 0
loc 409
ccs 39
cts 97
cp 0.4021
rs 10
c 0
b 0
f 0
wmc 28

21 Methods

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

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