Completed
Push — 272-feature/delete-user-post-m... ( f8ddd2...118da4 )
by Sudar
09:39 queued 05:57
created

BulkDelete::get_page_hook_suffix()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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

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