Completed
Push — dev/6.0.0 ( ee7fd2...7ea4f4 )
by Sudar
12:00
created

BulkDelete::set_plugin_file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

227
		load_plugin_textdomain( 'bulk-delete', /** @scrutinizer ignore-type */ false, $this->get_translations_path() );
Loading history...
228 1
	}
229
230
	/**
231
	 * Triggered when the `admin_menu` hook is fired.
232
	 *
233
	 * Register all admin pages.
234
	 *
235
	 * @since 6.0.0
236
	 */
237
	public function on_admin_menu() {
238
		foreach ( $this->get_primary_pages() as $page ) {
239
			$page->register();
240
		}
241
242
//		\BD_Users_Page::factory();
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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