Completed
Pull Request — dev/6.0.0 (#307)
by Rajan
12:39 queued 08:52
created

BulkDelete::get_secondary_pages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

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