Completed
Push — 312-fix/delete-post-meta-modul... ( fbbe37...602413 )
by Sudar
14:44 queued 11:16
created

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