Completed
Pull Request — dev/6.0.0 (#225)
by
unknown
15:12 queued 08:01
created

Bulk_Delete::get_admin_pages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 17
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
1
<?php
2
/**
3
 * Plugin Name: Bulk Delete
4
 * Plugin Script: bulk-delete.php
5
 * Plugin URI: https://bulkwp.com
6
 * Description: Bulk delete users and posts from selected categories, tags, post types, custom taxonomies or by post status like drafts, scheduled posts, revisions etc.
7
 * Version: 5.6.1
8
 * License: GPLv2 or later
9
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 * Author: Sudar
11
 * Author URI: https://sudarmuthu.com/
12
 * Text Domain: bulk-delete
13
 * Domain Path: languages/
14
 * === RELEASE NOTES ===
15
 * Check readme file for full release notes.
16
 */
17
use BulkWP\BulkDelete\Core\Base\BasePage;
18
use BulkWP\BulkDelete\Core\Controller;
19
use BulkWP\BulkDelete\Core\Pages\DeletePagesPage;
20
use BulkWP\BulkDelete\Core\Pages\Metabox\DeletePagesByStatusMetabox;
21
use BulkWP\BulkDelete\Core\Posts\DeletePostsPage;
22
use BulkWP\BulkDelete\Core\Posts\Metabox\DeletePostsByStatusMetabox;
23
use BulkWP\BulkDelete\Core\Posts\Metabox\DeletePostsByTagMetabox;
24
25
/**
26
 * Copyright 2009  Sudar Muthu  (email : [email protected])
27
 * This program is free software; you can redistribute it and/or modify
28
 * it under the terms of the GNU General Public License, version 2, as
29
 * published by the Free Software Foundation.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program; if not, write to the Free Software
38
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
39
 */
40
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
41
42
/**
43
 * Main Bulk_Delete class.
44
 *
45
 * Singleton @since 5.0
46
 */
47
final class Bulk_Delete {
48
	/**
49
	 * The one true Bulk_Delete instance.
50
	 *
51
	 * @var Bulk_Delete
52
	 *
53
	 * @since 5.0
54
	 */
55
	private static $instance;
56
57
	/**
58
	 * Path to the main plugin file.
59
	 *
60
	 * @var string
61
	 */
62
	private $plugin_file;
63
64
	/**
65
	 * Path where translations are stored.
66
	 *
67
	 * @var string
68
	 */
69
	private $translations_path;
70
71
	/**
72
	 * Is the plugin is loaded?
73
	 *
74
	 * @since 6.0.0
75
	 *
76
	 * @var bool
77
	 */
78
	private $loaded = false;
79
80
	/**
81
	 * Controller that handles all requests and nonce checks.
82
	 *
83
	 * @var \BulkWP\BulkDelete\Core\Controller
84
	 */
85
	private $controller;
86
87
	/**
88
	 * List of Admin pages.
89
	 *
90
	 * @var BasePage[]
91
	 *
92
	 * @since 6.0.0
93
	 */
94
	private $admin_pages = array();
95
96
	// version
97
	const VERSION                   = '5.6.1';
98
99
	// page slugs
100
	const POSTS_PAGE_SLUG           = 'bulk-delete-posts';
101
	const PAGES_PAGE_SLUG           = 'bulk-delete-pages';
102
	const CRON_PAGE_SLUG            = 'bulk-delete-cron';
103
	const ADDON_PAGE_SLUG           = 'bulk-delete-addon';
104
105
	// JS constants
106
	const JS_HANDLE                 = 'bulk-delete';
107
	const CSS_HANDLE                = 'bulk-delete';
108
109
	// Cron hooks
110
	const CRON_HOOK_CATEGORY        = 'do-bulk-delete-cat';
111
	const CRON_HOOK_POST_STATUS     = 'do-bulk-delete-post-status';
112
	const CRON_HOOK_TAG             = 'do-bulk-delete-tag';
113
	const CRON_HOOK_TAXONOMY        = 'do-bulk-delete-taxonomy';
114
	const CRON_HOOK_POST_TYPE       = 'do-bulk-delete-post-type';
115
	const CRON_HOOK_CUSTOM_FIELD    = 'do-bulk-delete-custom-field';
116
	const CRON_HOOK_TITLE           = 'do-bulk-delete-by-title';
117
	const CRON_HOOK_DUPLICATE_TITLE = 'do-bulk-delete-by-duplicate-title';
118
	const CRON_HOOK_POST_BY_ROLE    = 'do-bulk-delete-posts-by-role';
119
120
	const CRON_HOOK_PAGES_STATUS    = 'do-bulk-delete-pages-by-status';
121
122
	// meta boxes for delete posts
123
	const BOX_POST_STATUS           = 'bd_by_post_status';
124
	const BOX_CATEGORY              = 'bd_by_category';
125
	const BOX_TAG                   = 'bd_by_tag';
126
	const BOX_TAX                   = 'bd_by_tax';
127
	const BOX_POST_TYPE             = 'bd_by_post_type';
128
	const BOX_URL                   = 'bd_by_url';
129
	const BOX_POST_REVISION         = 'bd_by_post_revision';
130
	const BOX_CUSTOM_FIELD          = 'bd_by_custom_field';
131
	const BOX_TITLE                 = 'bd_by_title';
132
	const BOX_DUPLICATE_TITLE       = 'bd_by_duplicate_title';
133
	const BOX_POST_FROM_TRASH       = 'bd_posts_from_trash';
134
	const BOX_POST_BY_ROLE          = 'bd_post_by_user_role';
135
136
	// meta boxes for delete pages
137
	const BOX_PAGE_STATUS           = 'bd_by_page_status';
138
	const BOX_PAGE_FROM_TRASH       = 'bd_pages_from_trash';
139
140
	// Settings constants
141
	const SETTING_OPTION_GROUP      = 'bd_settings';
142
	const SETTING_OPTION_NAME       = 'bd_licenses';
143
	const SETTING_SECTION_ID        = 'bd_license_section';
144
145
	// Transient keys
146
	const LICENSE_CACHE_KEY_PREFIX  = 'bd-license_';
147
148
	const MAX_SELECT2_LIMIT  = 50;
149
150
	// path variables
151
	// Ideally these should be constants, but because of PHP's limitations, these are static variables
152
	public static $PLUGIN_DIR;
153
	public static $PLUGIN_URL;
154
	public static $PLUGIN_FILE;
155
156
	// Instance variables
157
	public $translations;
158
	public $posts_page;
159
	public $pages_page;
160
	public $cron_page;
161
	public $addon_page;
162
	public $settings_page;
163
	public $meta_page;
164
	public $misc_page;
165
	public $display_activate_license_form = false;
166
167
	// Deprecated.
168
	// Will be removed in v6.0
169
	const CRON_HOOK_USER_ROLE = 'do-bulk-delete-users-by-role';
170
	public $users_page;
171
172
	/**
173
	 * Main Bulk_Delete Instance.
174
	 *
175
	 * Insures that only one instance of Bulk_Delete exists in memory at any one
176
	 * time. Also prevents needing to define globals all over the place.
177
	 *
178
	 * @since 5.0
179
	 * @static
180
	 * @staticvar array $instance
181
	 *
182
	 * @see BULK_DELETE()
183
	 *
184
	 * @uses Bulk_Delete::setup_paths() Setup the plugin paths
185
	 * @uses Bulk_Delete::includes() Include the required files
186
	 * @uses Bulk_Delete::load_textdomain() Load text domain for translation
187
	 * @uses Bulk_Delete::setup_actions() Setup the hooks and actions
188
	 *
189
	 * @return Bulk_Delete The one true instance of Bulk_Delete
190
	 */
191 5
	public static function get_instance() {
192 5
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Bulk_Delete ) ) {
193 1
			self::$instance = new Bulk_Delete();
194
195 1
			self::$instance->setup_paths();
196 1
			self::$instance->includes();
197
		}
198
199 5
		return self::$instance;
200
	}
201
202
	/**
203
	 * Load the plugin if it is not loaded.
204
	 *
205
	 * This function will be invoked in the `plugins_loaded` hook.
206
	 */
207 1
	public function load() {
208 1
		if ( $this->loaded ) {
209
			return;
210
		}
211
212 1
		add_action( 'init', array( $this, 'on_init' ) );
213
214 1
		$this->load_dependencies();
215 1
		$this->setup_actions();
216
217 1
		$this->loaded = true;
218
219
		/**
220
		 * Bulk Delete plugin loaded.
221
		 *
222
		 * @since 6.0.0
223
		 */
224 1
		do_action( 'bd_loaded' );
225 1
	}
226
227
	/**
228
	 * Throw error on object clone.
229
	 *
230
	 * The whole idea of the singleton design pattern is that there is a single
231
	 * object therefore, we don't want the object to be cloned.
232
	 *
233
	 * @since  5.0
234
	 * @access protected
235
	 *
236
	 * @return void
237
	 */
238 1
	public function __clone() {
239
		// Cloning instances of the class is forbidden
240 1
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bulk-delete' ), '5.0' );
241 1
	}
242
243
	/**
244
	 * Disable unserializing of the class.
245
	 *
246
	 * @since  5.0
247
	 * @access protected
248
	 *
249
	 * @return void
250
	 */
251 1
	public function __wakeup() {
252
		// Unserializing instances of the class is forbidden
253 1
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bulk-delete' ), '5.0' );
254 1
	}
255
256
	/**
257
	 * Setup plugin constants.
258
	 *
259
	 * @access private
260
	 *
261
	 * @since  5.0
262
	 *
263
	 * @return void
264
	 */
265 1
	private function setup_paths() {
266
		// Plugin Folder Path
267 1
		self::$PLUGIN_DIR = plugin_dir_path( __FILE__ );
268
269
		// Plugin Folder URL
270 1
		self::$PLUGIN_URL = plugin_dir_url( __FILE__ );
271
272
		// Plugin Root File
273 1
		self::$PLUGIN_FILE = __FILE__;
274 1
	}
275
276
	/**
277
	 * Include required files.
278
	 *
279
	 * // TODO: Replace includes with autoloader.
280
	 *
281
	 * @access private
282
	 *
283
	 * @since  5.0
284
	 *
285
	 * @return void
286
	 */
287 1
	private function includes() {
288 1
		require_once self::$PLUGIN_DIR . '/include/Core/Base/BasePage.php';
289 1
		require_once self::$PLUGIN_DIR . '/include/Core/Base/MetaboxPage.php';
290
291 1
		require_once self::$PLUGIN_DIR . '/include/Core/Pages/DeletePagesPage.php';
292 1
		require_once self::$PLUGIN_DIR . '/include/Core/Posts/DeletePostsPage.php';
293
294 1
		require_once self::$PLUGIN_DIR . '/include/Core/Base/BaseMetabox.php';
295 1
		require_once self::$PLUGIN_DIR . '/include/Core/Pages/PagesMetabox.php';
296 1
		require_once self::$PLUGIN_DIR . '/include/Core/Pages/Metabox/DeletePagesByStatusMetabox.php';
297
298 1
		require_once self::$PLUGIN_DIR . '/include/Core/Posts/PostsMetabox.php';
299 1
		require_once self::$PLUGIN_DIR . '/include/Core/Posts/Metabox/DeletePostsByStatusMetabox.php';
300
301 1
		require_once self::$PLUGIN_DIR . '/include/base/class-bd-meta-box-module.php';
302 1
		require_once self::$PLUGIN_DIR . '/include/base/users/class-bd-user-meta-box-module.php';
303 1
		require_once self::$PLUGIN_DIR . '/include/base/class-bd-base-page.php';
304 1
		require_once self::$PLUGIN_DIR . '/include/base/class-bd-page.php';
305
306 1
		require_once self::$PLUGIN_DIR . '/include/Core/Controller.php';
307
308 1
		require_once self::$PLUGIN_DIR . '/include/ui/form.php';
309
310 1
		require_once self::$PLUGIN_DIR . '/include/posts/class-bulk-delete-posts.php';
311
//		require_once self::$PLUGIN_DIR . '/include/pages/class-bulk-delete-pages.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
312
313 1
		require_once self::$PLUGIN_DIR . '/include/users/class-bd-users-page.php';
314 1
		require_once self::$PLUGIN_DIR . '/include/users/modules/class-bulk-delete-users-by-user-role.php';
315 1
		require_once self::$PLUGIN_DIR . '/include/users/modules/class-bulk-delete-users-by-user-meta.php';
316
317 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-meta.php';
318 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-post-meta.php';
319 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-comment-meta.php';
320 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-user-meta.php';
321
322 1
		require_once self::$PLUGIN_DIR . '/include/misc/class-bulk-delete-misc.php';
323 1
		require_once self::$PLUGIN_DIR . '/include/misc/class-bulk-delete-jetpack-contact-form-messages.php';
324
325 1
		require_once self::$PLUGIN_DIR . '/include/settings/class-bd-settings-page.php';
326 1
		require_once self::$PLUGIN_DIR . '/include/settings/setting-helpers.php';
327 1
		require_once self::$PLUGIN_DIR . '/include/settings/class-bd-settings.php';
328
329 1
		require_once self::$PLUGIN_DIR . '/include/system-info/class-bd-system-info-page.php';
330
331 1
		require_once self::$PLUGIN_DIR . '/include/util/class-bd-util.php';
332 1
		require_once self::$PLUGIN_DIR . '/include/util/query.php';
333
334 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/simple-login-log.php';
335 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/the-event-calendar.php';
336 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/woocommerce.php';
337 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/advanced-custom-fields-pro.php';
338
339 1
		require_once self::$PLUGIN_DIR . '/include/deprecated/class-bulk-delete-users.php';
340 1
		require_once self::$PLUGIN_DIR . '/include/deprecated/deprecated.php';
341
342 1
		require_once self::$PLUGIN_DIR . '/include/addons/base/class-bd-addon.php';
343 1
		require_once self::$PLUGIN_DIR . '/include/addons/base/class-bd-base-addon.php';
344 1
		require_once self::$PLUGIN_DIR . '/include/addons/base/class-bd-scheduler-addon.php';
345
346 1
		require_once self::$PLUGIN_DIR . '/include/addons/addon-list.php';
347 1
		require_once self::$PLUGIN_DIR . '/include/addons/posts.php';
348 1
		require_once self::$PLUGIN_DIR . '/include/addons/pages.php';
349 1
		require_once self::$PLUGIN_DIR . '/include/addons/util.php';
350
351 1
		require_once self::$PLUGIN_DIR . '/include/license/class-bd-license.php';
352 1
		require_once self::$PLUGIN_DIR . '/include/license/class-bd-license-handler.php';
353 1
		require_once self::$PLUGIN_DIR . '/include/license/class-bd-edd-api-wrapper.php';
354
355 1
		require_once self::$PLUGIN_DIR . '/include/ui/admin-ui.php';
356 1
		require_once self::$PLUGIN_DIR . '/include/ui/class-bulk-delete-help-screen.php';
357 1
	}
358
359
	/**
360
	 * Triggered when the `init` hook is fired.
361
	 *
362
	 * @since 6.0.0
363
	 */
364 1
	public function on_init() {
365 1
		$this->load_textdomain();
366 1
	}
367
368
	/**
369
	 * Loads the plugin language files.
370
	 *
371
	 * @since  5.0
372
	 */
373 1
	private function load_textdomain() {
374 1
		load_plugin_textdomain( 'bulk-delete', false, $this->get_translations_path() );
375 1
	}
376
377
	/**
378
	 * Load all dependencies.
379
	 *
380
	 * @since 6.0.0
381
	 */
382 1
	private function load_dependencies() {
383 1
		$this->controller = new Controller();
384 1
	}
385
386
	/**
387
	 * Loads the plugin's actions and hooks.
388
	 *
389
	 * @access private
390
	 *
391
	 * @since  5.0
392
	 *
393
	 * @return void
394
	 */
395 1
	private function setup_actions() {
396 1
		add_action( 'admin_menu', array( $this, 'on_admin_menu' ) );
397
398
		/**
399
		 * This is Ajax hook, It's runs when user search categories or tags on bulk-delete-posts page.
400
		 *
401
		 * @since 6.0.0
402
		 */
403 1
		add_action( 'wp_ajax_bd_load_taxonomy_term', array( $this, 'load_taxonomy_term' ) );
404
405 1
		add_filter( 'bd_help_tooltip', 'bd_generate_help_tooltip', 10, 2 );
406
407 1
		add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 );
408
409 1
		if ( defined( 'BD_DEBUG' ) && BD_DEBUG ) {
410
			add_action( 'bd_after_query', array( $this, 'log_sql_query' ) );
411
		}
412 1
	}
413
414
	/**
415
	 * Adds the settings link in the Plugin page.
416
	 *
417
	 * Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/.
418
	 *
419
	 * @staticvar string $this_plugin
420
	 *
421
	 * @param array  $action_links Action Links.
422
	 * @param string $file         Plugin file name.
423
	 *
424
	 * @return array Modified links.
425
	 */
426
	public function filter_plugin_action_links( $action_links, $file ) {
427
		static $this_plugin;
428
429
		if ( ! $this_plugin ) {
430
			$this_plugin = plugin_basename( $this->get_plugin_file() );
431
		}
432
433
		if ( $file == $this_plugin ) {
434
			/**
435
			 * Filter plugin action links added by Bulk Move.
436
			 *
437
			 * @since 6.0.0
438
			 *
439
			 * @param array Plugin Links.
440
			 */
441
			$bm_action_links = apply_filters( 'bd_plugin_action_links', array() );
442
443
			if ( ! empty( $bm_action_links ) ) {
444
				$action_links = array_merge( $bm_action_links, $action_links );
445
			}
446
		}
447
448
		return $action_links;
449
	}
450
451
	/**
452
	 * Log SQL query used by Bulk Delete.
453
	 *
454
	 * Query is logged only when `BD_DEBUG` is set.
455
	 *
456
	 * @since 5.6
457
	 *
458
	 * @param \WP_Query $wp_query WP Query object.
459
	 */
460
	public function log_sql_query( $wp_query ) {
461
		$query = $wp_query->request;
462
463
		/**
464
		 * Bulk Delete query is getting logged.
465
		 *
466
		 * @since 5.6
467
		 *
468
		 * @param string $query Bulk Delete SQL Query.
469
		 */
470
		do_action( 'bd_log_sql_query', $query );
471
472
		error_log( 'Bulk Delete Query: ' . $query );
473
	}
474
475
	/**
476
	 * Triggered when the `admin_menu` hook is fired.
477
	 *
478
	 * Register all admin pages.
479
	 *
480
	 * @since 6.0.0
481
	 */
482
	public function on_admin_menu() {
483
		foreach ( $this->get_admin_pages() as $page ) {
484
			$page->register();
485
		}
486
487
		$this->load_legacy_menu();
488
	}
489
490
	/**
491
	 * Get the list of registered admin pages.
492
	 *
493
	 * @since 6.0.0
494
	 *
495
	 * @return BasePage[] List of Admin pages.
496
	 */
497
	private function get_admin_pages() {
498
		if ( empty( $this->admin_pages ) ) {
499
			$posts_page = $this->get_delete_posts_admin_page();
500
			$pages_page = $this->get_delete_pages_admin_page();
501
502
			$this->admin_pages[ $posts_page->get_page_slug() ] = $posts_page;
503
			$this->admin_pages[ $pages_page->get_page_slug() ] = $pages_page;
504
		}
505
506
		/**
507
		 * List of admin pages.
508
		 *
509
		 * @since 6.0.0
510
		 *
511
		 * @param BasePage[] List of Admin pages.
512
		 */
513
		return apply_filters( 'bd_admin_pages', $this->admin_pages );
514
	}
515
516
	/**
517
	 * Get Bulk Delete Posts admin page.
518
	 *
519
	 * @return \BulkWP\BulkDelete\Core\Posts\DeletePostsPage
520
	 */
521
	private function get_delete_posts_admin_page() {
522
		$posts_page = new DeletePostsPage( $this->get_plugin_file() );
523
524
		$posts_page->add_metabox( new DeletePostsByStatusMetabox() );
525
526
		return $posts_page;
527
	}
528
529
	/**
530
	 * Get Bulk Delete Pages admin page.
531
	 *
532
	 * @since 6.0.0
533
	 *
534
	 * @return DeletePagesPage Bulk Move Post admin page.
535
	 */
536
	private function get_delete_pages_admin_page() {
537
		$pages_page = new DeletePagesPage( $this->get_plugin_file() );
538
539
		$pages_page->add_metabox( new DeletePagesByStatusMetabox() );
540
541
		return $pages_page;
542
	}
543
544
	/**
545
	 * Add navigation menu.
546
	 */
547
	public function load_legacy_menu() {
548
		$this->posts_page = add_submenu_page( self::POSTS_PAGE_SLUG, __( 'Bulk Delete Posts - Old', 'bulk-delete' ), __( 'Bulk Delete Posts - Old', 'bulk-delete' ), 'delete_posts', 'bulk-delete-posts-old', array( $this, 'display_posts_page' ) );
549
550
		/**
551
		 * Runs just after adding all *delete* menu items to Bulk WP main menu.
552
		 *
553
		 * This action is primarily for adding extra *delete* menu items to the Bulk WP main menu.
554
		 *
555
		 * @since 5.3
556
		 */
557
		do_action( 'bd_after_primary_menus' );
558
559
		/**
560
		 * Runs just before adding non-action menu items to Bulk WP main menu.
561
		 *
562
		 * This action is primarily for adding extra menu items before non-action menu items to the Bulk WP main menu.
563
		 *
564
		 * @since 5.3
565
		 */
566
		do_action( 'bd_before_secondary_menus' );
567
568
		$this->cron_page  = add_submenu_page( self::POSTS_PAGE_SLUG, __( 'Bulk Delete Schedules', 'bulk-delete' ), __( 'Scheduled Jobs', 'bulk-delete' ), 'delete_posts'    , self::CRON_PAGE_SLUG , array( $this, 'display_cron_page' ) );
569
		$this->addon_page = add_submenu_page( self::POSTS_PAGE_SLUG, __( 'Addon Licenses'       , 'bulk-delete' ), __( 'Addon Licenses', 'bulk-delete' ), 'activate_plugins', self::ADDON_PAGE_SLUG, array( 'BD_License', 'display_addon_page' ) );
570
571
		/**
572
		 * Runs just after adding all menu items to Bulk WP main menu.
573
		 *
574
		 * This action is primarily for adding extra menu items to the Bulk WP main menu.
575
		 *
576
		 * @since 5.3
577
		 */
578
		do_action( 'bd_after_all_menus' );
579
580
		$admin_pages = $this->get_admin_pages();
581
		$pages_page  = $admin_pages['bulk-delete-pages'];
582
583
		// enqueue JavaScript
584
		add_action( 'admin_print_scripts-' . $this->posts_page, array( $pages_page, 'enqueue_assets' ) );
585
586
		// delete posts page
587
		add_action( "load-{$this->posts_page}", array( $this, 'add_delete_posts_settings_panel' ) );
588
		add_action( "add_meta_boxes_{$this->posts_page}", array( $this, 'add_delete_posts_meta_boxes' ) );
589
	}
590
591
	/**
592
	 * Add settings Panel for delete posts page.
593
	 */
594
	public function add_delete_posts_settings_panel() {
595
		/**
596
		 * Add contextual help for admin screens.
597
		 *
598
		 * @since 5.1
599
		 */
600
		do_action( 'bd_add_contextual_help', $this->posts_page );
601
602
		/* Trigger the add_meta_boxes hooks to allow meta boxes to be added */
603
		do_action( 'add_meta_boxes_' . $this->posts_page, null );
604
605
		/* Enqueue WordPress' script for handling the meta boxes */
606
		wp_enqueue_script( 'postbox' );
607
	}
608
609
	/**
610
	 * Ajax call back function for getting taxonomies to load select2 options.
611
	 *
612
	 * @since 6.0.0
613
	 */
614
	public function load_taxonomy_term(){
615
		$response = array();
616
617
		$taxonomy = sanitize_text_field( $_GET['taxonomy'] );
618
619
		$terms = get_terms( array(
620
			'taxonomy'   => $taxonomy,
621
			'hide_empty' => false,
622
			'search'     => sanitize_text_field($_GET['q']),
623
		) );
624
625
		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
626
			foreach ( $terms as $term ) {
627
				$response[] = array( absint($term->term_id), $term->name . ' (' . $term->count . __( ' Posts', 'bulk-delete' ) . ')' );
628
			}
629
		}
630
631
		echo json_encode( $response );
632
		die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
633
	}
634
635
	/**
636
	 * Register meta boxes for delete posts page.
637
	 */
638
	public function add_delete_posts_meta_boxes() {
639
		add_meta_box( self::BOX_POST_STATUS   , __( 'By Post Status'       , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_status_box'    , $this->posts_page , 'advanced' );
640
		add_meta_box( self::BOX_CATEGORY      , __( 'By Category'          , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_category_box'  , $this->posts_page , 'advanced' );
641
		add_meta_box( self::BOX_TAG           , __( 'By Tag'               , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_tag_box'       , $this->posts_page , 'advanced' );
642
		add_meta_box( self::BOX_TAX           , __( 'By Custom Taxonomy'   , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_taxonomy_box'  , $this->posts_page , 'advanced' );
643
		add_meta_box( self::BOX_POST_TYPE     , __( 'By Custom Post Type'  , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_post_type_box' , $this->posts_page , 'advanced' );
644
		add_meta_box( self::BOX_URL           , __( 'By URL'               , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_url_box'       , $this->posts_page , 'advanced' );
645
		add_meta_box( self::BOX_POST_REVISION , __( 'By Post Revision'     , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_posts_by_revision_box'         , $this->posts_page , 'advanced' );
646
647
		/**
648
		 * Add meta box in delete posts page
649
		 * This hook can be used for adding additional meta boxes in delete posts page.
650
		 *
651
		 * @since 5.3
652
		 */
653
		do_action( 'bd_add_meta_box_for_posts' );
654
	}
655
656
	/**
657
	 * Enqueue Scripts and Styles.
658
	 */
659
	public function add_script() {
660
		// TODO: Remove this function.
661
662
		$admin_pages = $this->get_admin_pages();
663
		$pages_page  = $admin_pages['bulk-delete-pages'];
664
		$pages_page->enqueue_assets();
665
	}
666
667
	/**
668
	 * Show the delete posts page.
669
	 *
670
	 * @Todo Move this function to Bulk_Delete_Posts class
671
	 */
672
	public function display_posts_page() {
673
?>
674
<div class="wrap">
675
    <h2><?php _e( 'Bulk Delete Posts', 'bulk-delete' );?></h2>
676
    <?php settings_errors(); ?>
677
678
    <form method = "post">
679
<?php
680
		// nonce for bulk delete
681
		wp_nonce_field( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' );
682
683
		/* Used to save closed meta boxes and their order */
684
		wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
685
		wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
686
?>
687
    <div id = "poststuff">
688
        <div id="post-body" class="metabox-holder columns-1">
689
690
            <div class="notice notice-warning">
691
                <p><strong><?php _e( 'WARNING: Posts deleted once cannot be retrieved back. Use with caution.', 'bulk-delete' ); ?></strong></p>
692
            </div>
693
694
            <div id="postbox-container-2" class="postbox-container">
695
                <?php do_meta_boxes( '', 'advanced', null ); ?>
696
            </div> <!-- #postbox-container-2 -->
697
698
        </div> <!-- #post-body -->
699
    </div><!-- #poststuff -->
700
    </form>
701
</div><!-- .wrap -->
702
703
<?php
704
		/**
705
		 * Runs just before displaying the footer text in the "Bulk Delete Posts" admin page.
706
		 *
707
		 * This action is primarily for adding extra content in the footer of "Bulk Delete Posts" admin page.
708
		 *
709
		 * @since 5.0
710
		 */
711
		do_action( 'bd_admin_footer_posts_page' );
712
	}
713
714
	/**
715
	 * Display the schedule page.
716
	 */
717
	public function display_cron_page() {
718
		if ( ! class_exists( 'WP_List_Table' ) ) {
719
			require_once ABSPATH . WPINC . '/class-wp-list-table.php';
720
		}
721
722
		if ( ! class_exists( 'Cron_List_Table' ) ) {
723
			require_once self::$PLUGIN_DIR . '/include/cron/class-cron-list-table.php';
724
		}
725
726
		// Prepare Table of elements
727
		$cron_list_table = new Cron_List_Table();
728
		$cron_list_table->prepare_items();
729
?>
730
    <div class="wrap">
731
        <h2><?php _e( 'Bulk Delete Schedules', 'bulk-delete' );?></h2>
732
        <?php settings_errors(); ?>
733
<?php
734
		// Table of elements
735
		$cron_list_table->display();
736
		bd_display_available_addon_list();
737
?>
738
    </div>
739
<?php
740
		/**
741
		 * Runs just before displaying the footer text in the "Schedules" admin page.
742
		 *
743
		 * This action is primarily for adding extra content in the footer of "Schedules" admin page.
744
		 *
745
		 * @since 5.0
746
		 */
747
		do_action( 'bd_admin_footer_cron_page' );
748
	}
749
750
	/**
751
	 * Get path to main plugin file.
752
	 *
753
	 * @return string Plugin file.
754
	 */
755 1
	public function get_plugin_file() {
756 1
		return $this->plugin_file;
757
	}
758
759
	/**
760
	 * Set path to main plugin file.
761
	 *
762
	 * @param string $plugin_file Path to main plugin file.
763
	 */
764 1
	public function set_plugin_file( $plugin_file ) {
765 1
		$this->plugin_file       = $plugin_file;
766 1
		$this->translations_path = dirname( plugin_basename( $this->get_plugin_file() ) ) . '/languages/';
767 1
	}
768
769
	/**
770
	 * Get path to translations.
771
	 *
772
	 * @return string Translations path.
773
	 */
774 1
	public function get_translations_path() {
775 1
		return $this->translations_path;
776
	}
777
}
778
779
/**
780
 * The main function responsible for returning the one true Bulk_Delete
781
 * Instance to functions everywhere.
782
 *
783
 * Use this function like you would a global variable, except without needing
784
 * to declare the global.
785
 *
786
 * Example: `<?php $bulk_delete = BULK_DELETE(); ?>`
787
 *
788
 * @since 5.0
789
 *
790
 * @return Bulk_Delete The one true Bulk_Delete Instance
791
 */
792
function BULK_DELETE() {
793 1
	return Bulk_Delete::get_instance();
794
}
795
796
/**
797
 * Load Bulk Delete plugin.
798
 *
799
 * @since 6.0.0
800
 */
801
function load_bulk_delete() {
802 1
	$bulk_delete = BULK_DELETE();
803 1
	$bulk_delete->set_plugin_file( __FILE__ );
804
805 1
	add_action( 'plugins_loaded', array( $bulk_delete, 'load' ), 101 );
806 1
}
807
808
load_bulk_delete();
809