Passed
Pull Request — dev/6.0.0 (#332)
by Rajan
03:26
created

BulkDelete::load_textdomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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