Completed
Push — 233-feature/posts-by-post-stat... ( e33426...261c88 )
by Sudar
132:57 queued 129:48
created

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

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