Completed
Push — 233-feature/posts-by-post-stat... ( d2a551...3016f6 )
by Sudar
11:19 queued 07:05
created

BulkDelete::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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

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