Completed
Pull Request — dev/6.0.0 (#224)
by Rajan
18:05 queued 11:10
created

bulk-delete.php (1 issue)

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
23
/**
24
 * Copyright 2009  Sudar Muthu  (email : [email protected])
25
 * This program is free software; you can redistribute it and/or modify
26
 * it under the terms of the GNU General Public License, version 2, as
27
 * published by the Free Software Foundation.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
 * GNU General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU General Public License
35
 * along with this program; if not, write to the Free Software
36
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
37
 */
38
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
39
40
/**
41
 * Main Bulk_Delete class.
42
 *
43
 * Singleton @since 5.0
44
 */
45
final class Bulk_Delete {
46
	/**
47
	 * The one true Bulk_Delete instance.
48
	 *
49
	 * @var Bulk_Delete
50
	 *
51
	 * @since 5.0
52
	 */
53
	private static $instance;
54
55
	/**
56
	 * Path to the main plugin file.
57
	 *
58
	 * @var string
59
	 */
60
	private $plugin_file;
61
62
	/**
63
	 * Path where translations are stored.
64
	 *
65
	 * @var string
66
	 */
67
	private $translations_path;
68
69
	/**
70
	 * Is the plugin is loaded?
71
	 *
72
	 * @since 6.0.0
73
	 *
74
	 * @var bool
75
	 */
76
	private $loaded = false;
77
78
	/**
79
	 * Controller that handles all requests and nonce checks.
80
	 *
81
	 * @var \BulkWP\BulkDelete\Core\Controller
82
	 */
83
	private $controller;
84
85
	/**
86
	 * List of Admin pages.
87
	 *
88
	 * @var BasePage[]
89
	 *
90
	 * @since 6.0.0
91
	 */
92
	private $admin_pages = array();
93
94
	// version
95
	const VERSION                   = '5.6.1';
96
97
	// page slugs
98
	const POSTS_PAGE_SLUG           = 'bulk-delete-posts';
99
	const PAGES_PAGE_SLUG           = 'bulk-delete-pages';
100
	const CRON_PAGE_SLUG            = 'bulk-delete-cron';
101
	const ADDON_PAGE_SLUG           = 'bulk-delete-addon';
102
103
	// JS constants
104
	const JS_HANDLE                 = 'bulk-delete';
105
	const CSS_HANDLE                = 'bulk-delete';
106
107
	// Cron hooks
108
	const CRON_HOOK_CATEGORY        = 'do-bulk-delete-cat';
109
	const CRON_HOOK_POST_STATUS     = 'do-bulk-delete-post-status';
110
	const CRON_HOOK_TAG             = 'do-bulk-delete-tag';
111
	const CRON_HOOK_TAXONOMY        = 'do-bulk-delete-taxonomy';
112
	const CRON_HOOK_POST_TYPE       = 'do-bulk-delete-post-type';
113
	const CRON_HOOK_CUSTOM_FIELD    = 'do-bulk-delete-custom-field';
114
	const CRON_HOOK_TITLE           = 'do-bulk-delete-by-title';
115
	const CRON_HOOK_DUPLICATE_TITLE = 'do-bulk-delete-by-duplicate-title';
116
	const CRON_HOOK_POST_BY_ROLE    = 'do-bulk-delete-posts-by-role';
117
118
	const CRON_HOOK_PAGES_STATUS    = 'do-bulk-delete-pages-by-status';
119
120
	// meta boxes for delete posts
121
	const BOX_POST_STATUS           = 'bd_by_post_status';
122
	const BOX_CATEGORY              = 'bd_by_category';
123
	const BOX_TAG                   = 'bd_by_tag';
124
	const BOX_TAX                   = 'bd_by_tax';
125
	const BOX_POST_TYPE             = 'bd_by_post_type';
126
	const BOX_URL                   = 'bd_by_url';
127
	const BOX_POST_REVISION         = 'bd_by_post_revision';
128
	const BOX_CUSTOM_FIELD          = 'bd_by_custom_field';
129
	const BOX_TITLE                 = 'bd_by_title';
130
	const BOX_DUPLICATE_TITLE       = 'bd_by_duplicate_title';
131
	const BOX_POST_FROM_TRASH       = 'bd_posts_from_trash';
132
	const BOX_POST_BY_ROLE          = 'bd_post_by_user_role';
133
134
	// meta boxes for delete pages
135
	const BOX_PAGE_STATUS           = 'bd_by_page_status';
136
	const BOX_PAGE_FROM_TRASH       = 'bd_pages_from_trash';
137
138
	// Settings constants
139
	const SETTING_OPTION_GROUP      = 'bd_settings';
140
	const SETTING_OPTION_NAME       = 'bd_licenses';
141
	const SETTING_SECTION_ID        = 'bd_license_section';
142
143
	// Transient keys
144
	const LICENSE_CACHE_KEY_PREFIX  = 'bd-license_';
145
146
	const MAX_SELECT2_LIMIT  = 50;
147
148
	// path variables
149
	// Ideally these should be constants, but because of PHP's limitations, these are static variables
150
	public static $PLUGIN_DIR;
151
	public static $PLUGIN_URL;
152
	public static $PLUGIN_FILE;
153
154
	// Instance variables
155
	public $translations;
156
	public $posts_page;
157
	public $pages_page;
158
	public $cron_page;
159
	public $addon_page;
160
	public $settings_page;
161
	public $meta_page;
162
	public $misc_page;
163
	public $display_activate_license_form = false;
164
165
	// Deprecated.
166
	// Will be removed in v6.0
167
	const CRON_HOOK_USER_ROLE = 'do-bulk-delete-users-by-role';
168
	public $users_page;
169
170
	/**
171
	 * Main Bulk_Delete Instance.
172
	 *
173
	 * Insures that only one instance of Bulk_Delete exists in memory at any one
174
	 * time. Also prevents needing to define globals all over the place.
175
	 *
176
	 * @since 5.0
177
	 * @static
178
	 * @staticvar array $instance
179
	 *
180
	 * @see BULK_DELETE()
181
	 *
182
	 * @uses Bulk_Delete::setup_paths() Setup the plugin paths
183
	 * @uses Bulk_Delete::includes() Include the required files
184
	 * @uses Bulk_Delete::load_textdomain() Load text domain for translation
185
	 * @uses Bulk_Delete::setup_actions() Setup the hooks and actions
186
	 *
187
	 * @return Bulk_Delete The one true instance of Bulk_Delete
188
	 */
189 5
	public static function get_instance() {
190 5
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Bulk_Delete ) ) {
191 1
			self::$instance = new Bulk_Delete();
192
193 1
			self::$instance->setup_paths();
194 1
			self::$instance->includes();
195
		}
196
197 5
		return self::$instance;
198
	}
199
200
	/**
201
	 * Load the plugin if it is not loaded.
202
	 *
203
	 * This function will be invoked in the `plugins_loaded` hook.
204
	 */
205 1
	public function load() {
206 1
		if ( $this->loaded ) {
207
			return;
208
		}
209
210 1
		add_action( 'init', array( $this, 'on_init' ) );
211
212 1
		$this->load_dependencies();
213 1
		$this->setup_actions();
214
215 1
		$this->loaded = true;
216
217
		/**
218
		 * Bulk Delete plugin loaded.
219
		 *
220
		 * @since 6.0.0
221
		 */
222 1
		do_action( 'bd_loaded' );
223 1
	}
224
225
	/**
226
	 * Throw error on object clone.
227
	 *
228
	 * The whole idea of the singleton design pattern is that there is a single
229
	 * object therefore, we don't want the object to be cloned.
230
	 *
231
	 * @since  5.0
232
	 * @access protected
233
	 *
234
	 * @return void
235
	 */
236 1
	public function __clone() {
237
		// Cloning instances of the class is forbidden
238 1
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bulk-delete' ), '5.0' );
239 1
	}
240
241
	/**
242
	 * Disable unserializing of the class.
243
	 *
244
	 * @since  5.0
245
	 * @access protected
246
	 *
247
	 * @return void
248
	 */
249 1
	public function __wakeup() {
250
		// Unserializing instances of the class is forbidden
251 1
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bulk-delete' ), '5.0' );
252 1
	}
253
254
	/**
255
	 * Setup plugin constants.
256
	 *
257
	 * @access private
258
	 *
259
	 * @since  5.0
260
	 *
261
	 * @return void
262
	 */
263 1
	private function setup_paths() {
264
		// Plugin Folder Path
265 1
		self::$PLUGIN_DIR = plugin_dir_path( __FILE__ );
266
267
		// Plugin Folder URL
268 1
		self::$PLUGIN_URL = plugin_dir_url( __FILE__ );
269
270
		// Plugin Root File
271 1
		self::$PLUGIN_FILE = __FILE__;
272 1
	}
273
274
	/**
275
	 * Include required files.
276
	 *
277
	 * // TODO: Replace includes with autoloader.
278
	 *
279
	 * @access private
280
	 *
281
	 * @since  5.0
282
	 *
283
	 * @return void
284
	 */
285 1
	private function includes() {
286 1
		require_once self::$PLUGIN_DIR . '/include/Core/Base/BasePage.php';
287 1
		require_once self::$PLUGIN_DIR . '/include/Core/Base/MetaboxPage.php';
288
289 1
		require_once self::$PLUGIN_DIR . '/include/Core/Pages/DeletePagesPage.php';
290 1
		require_once self::$PLUGIN_DIR . '/include/Core/Posts/DeletePostsPage.php';
291
292 1
		require_once self::$PLUGIN_DIR . '/include/Core/Base/BaseMetabox.php';
293 1
		require_once self::$PLUGIN_DIR . '/include/Core/Pages/PagesMetabox.php';
294 1
		require_once self::$PLUGIN_DIR . '/include/Core/Pages/Metabox/DeletePagesByStatusMetabox.php';
295
296 1
		require_once self::$PLUGIN_DIR . '/include/base/class-bd-meta-box-module.php';
297 1
		require_once self::$PLUGIN_DIR . '/include/base/users/class-bd-user-meta-box-module.php';
298 1
		require_once self::$PLUGIN_DIR . '/include/base/class-bd-base-page.php';
299 1
		require_once self::$PLUGIN_DIR . '/include/base/class-bd-page.php';
300
301 1
		require_once self::$PLUGIN_DIR . '/include/Core/Controller.php';
302
303 1
		require_once self::$PLUGIN_DIR . '/include/ui/form.php';
304
305 1
		require_once self::$PLUGIN_DIR . '/include/posts/class-bulk-delete-posts.php';
306
//		require_once self::$PLUGIN_DIR . '/include/pages/class-bulk-delete-pages.php';
307
308 1
		require_once self::$PLUGIN_DIR . '/include/users/class-bd-users-page.php';
309 1
		require_once self::$PLUGIN_DIR . '/include/users/modules/class-bulk-delete-users-by-user-role.php';
310 1
		require_once self::$PLUGIN_DIR . '/include/users/modules/class-bulk-delete-users-by-user-meta.php';
311
312 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-meta.php';
313 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-post-meta.php';
314 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-comment-meta.php';
315 1
		require_once self::$PLUGIN_DIR . '/include/meta/class-bulk-delete-user-meta.php';
316
317 1
		require_once self::$PLUGIN_DIR . '/include/misc/class-bulk-delete-misc.php';
318 1
		require_once self::$PLUGIN_DIR . '/include/misc/class-bulk-delete-jetpack-contact-form-messages.php';
319
320 1
		require_once self::$PLUGIN_DIR . '/include/settings/class-bd-settings-page.php';
321 1
		require_once self::$PLUGIN_DIR . '/include/settings/setting-helpers.php';
322 1
		require_once self::$PLUGIN_DIR . '/include/settings/class-bd-settings.php';
323
324 1
		require_once self::$PLUGIN_DIR . '/include/system-info/class-bd-system-info-page.php';
325
326 1
		require_once self::$PLUGIN_DIR . '/include/util/class-bd-util.php';
327 1
		require_once self::$PLUGIN_DIR . '/include/util/query.php';
328
329 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/simple-login-log.php';
330 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/the-event-calendar.php';
331 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/woocommerce.php';
332 1
		require_once self::$PLUGIN_DIR . '/include/compatibility/advanced-custom-fields-pro.php';
333
334 1
		require_once self::$PLUGIN_DIR . '/include/deprecated/class-bulk-delete-users.php';
335 1
		require_once self::$PLUGIN_DIR . '/include/deprecated/deprecated.php';
336
337 1
		require_once self::$PLUGIN_DIR . '/include/addons/base/class-bd-addon.php';
338 1
		require_once self::$PLUGIN_DIR . '/include/addons/base/class-bd-base-addon.php';
339 1
		require_once self::$PLUGIN_DIR . '/include/addons/base/class-bd-scheduler-addon.php';
340
341 1
		require_once self::$PLUGIN_DIR . '/include/addons/addon-list.php';
342 1
		require_once self::$PLUGIN_DIR . '/include/addons/posts.php';
343 1
		require_once self::$PLUGIN_DIR . '/include/addons/pages.php';
344 1
		require_once self::$PLUGIN_DIR . '/include/addons/util.php';
345
346 1
		require_once self::$PLUGIN_DIR . '/include/license/class-bd-license.php';
347 1
		require_once self::$PLUGIN_DIR . '/include/license/class-bd-license-handler.php';
348 1
		require_once self::$PLUGIN_DIR . '/include/license/class-bd-edd-api-wrapper.php';
349
350 1
		require_once self::$PLUGIN_DIR . '/include/ui/admin-ui.php';
351 1
		require_once self::$PLUGIN_DIR . '/include/ui/class-bulk-delete-help-screen.php';
352 1
	}
353
354
	/**
355
	 * Triggered when the `init` hook is fired.
356
	 *
357
	 * @since 6.0.0
358
	 */
359 1
	public function on_init() {
360 1
		$this->load_textdomain();
361 1
	}
362
363
	/**
364
	 * Loads the plugin language files.
365
	 *
366
	 * @since  5.0
367
	 */
368 1
	private function load_textdomain() {
369 1
		load_plugin_textdomain( 'bulk-delete', false, $this->get_translations_path() );
370 1
	}
371
372
	/**
373
	 * Load all dependencies.
374
	 *
375
	 * @since 6.0.0
376
	 */
377 1
	private function load_dependencies() {
378 1
		$this->controller = new Controller();
379 1
	}
380
381
	/**
382
	 * Loads the plugin's actions and hooks.
383
	 *
384
	 * @access private
385
	 *
386
	 * @since  5.0
387
	 *
388
	 * @return void
389
	 */
390 1
	private function setup_actions() {
391 1
		add_action( 'admin_menu', array( $this, 'on_admin_menu' ) );
392
393
		/**
394
		 * This is Ajax hook, It's runs when user search categories or tags on bulk-delete-posts page.
395
		 *
396
		 * @since 6.0.0
397
		 */
398 1
		add_action( 'wp_ajax_bd_load_taxonomy_term', array( $this, 'load_taxonomy_term' ) );
399
400 1
		add_filter( 'bd_help_tooltip', 'bd_generate_help_tooltip', 10, 2 );
401
402 1
		add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 );
403
404 1
		if ( defined( 'BD_DEBUG' ) && BD_DEBUG ) {
405
			add_action( 'bd_after_query', array( $this, 'log_sql_query' ) );
406
		}
407 1
	}
408
409
	/**
410
	 * Adds the settings link in the Plugin page.
411
	 *
412
	 * Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/.
413
	 *
414
	 * @staticvar string $this_plugin
415
	 *
416
	 * @param array  $action_links Action Links.
417
	 * @param string $file         Plugin file name.
418
	 *
419
	 * @return array Modified links.
420
	 */
421
	public function filter_plugin_action_links( $action_links, $file ) {
422
		static $this_plugin;
423
424
		if ( ! $this_plugin ) {
425
			$this_plugin = plugin_basename( $this->get_plugin_file() );
426
		}
427
428
		if ( $file == $this_plugin ) {
429
			/**
430
			 * Filter plugin action links added by Bulk Move.
431
			 *
432
			 * @since 6.0.0
433
			 *
434
			 * @param array Plugin Links.
435
			 */
436
			$bm_action_links = apply_filters( 'bd_plugin_action_links', array() );
437
438
			if ( ! empty( $bm_action_links ) ) {
439
				$action_links = array_merge( $bm_action_links, $action_links );
440
			}
441
		}
442
443
		return $action_links;
444
	}
445
446
	/**
447
	 * Log SQL query used by Bulk Delete.
448
	 *
449
	 * Query is logged only when `BD_DEBUG` is set.
450
	 *
451
	 * @since 5.6
452
	 *
453
	 * @param \WP_Query $wp_query WP Query object.
454
	 */
455
	public function log_sql_query( $wp_query ) {
456
		$query = $wp_query->request;
457
458
		/**
459
		 * Bulk Delete query is getting logged.
460
		 *
461
		 * @since 5.6
462
		 *
463
		 * @param string $query Bulk Delete SQL Query.
464
		 */
465
		do_action( 'bd_log_sql_query', $query );
466
467
		error_log( 'Bulk Delete Query: ' . $query );
468
	}
469
470
	/**
471
	 * Triggered when the `admin_menu` hook is fired.
472
	 *
473
	 * Register all admin pages.
474
	 *
475
	 * @since 6.0.0
476
	 */
477
	public function on_admin_menu() {
478
		foreach ( $this->get_admin_pages() as $page ) {
479
			$page->register();
480
		}
481
482
		$this->load_legacy_menu();
483
	}
484
485
	/**
486
	 * Get the list of registered admin pages.
487
	 *
488
	 * @since 6.0.0
489
	 *
490
	 * @return BasePage[] List of Admin pages.
491
	 */
492
	private function get_admin_pages() {
493
		if ( empty( $this->admin_pages ) ) {
494
			$posts_page = $this->get_delete_posts_admin_page();
495
			$pages_page = $this->get_delete_pages_admin_page();
496
497
			$this->admin_pages[ $posts_page->get_page_slug() ] = $posts_page;
498
			$this->admin_pages[ $pages_page->get_page_slug() ] = $pages_page;
499
		}
500
501
		/**
502
		 * List of admin pages.
503
		 *
504
		 * @since 6.0.0
505
		 *
506
		 * @param BasePage[] List of Admin pages.
507
		 */
508
		return apply_filters( 'bd_admin_pages', $this->admin_pages );
509
	}
510
511
	/**
512
	 * Get Bulk Delete Posts admin page.
513
	 *
514
	 * @return \BulkWP\BulkDelete\Core\Posts\DeletePostsPage
515
	 */
516
	private function get_delete_posts_admin_page() {
517
		$posts_page = new DeletePostsPage( $this->get_plugin_file() );
518
519
//		$posts_page->add_metabox( new DeletePagesByStatusMetabox() );
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...
520
521
		return $posts_page;
522
	}
523
524
	/**
525
	 * Get Bulk Delete Pages admin page.
526
	 *
527
	 * @since 6.0.0
528
	 *
529
	 * @return DeletePagesPage Bulk Move Post admin page.
530
	 */
531
	private function get_delete_pages_admin_page() {
532
		$pages_page = new DeletePagesPage( $this->get_plugin_file() );
533
534
		$pages_page->add_metabox( new DeletePagesByStatusMetabox() );
535
536
		return $pages_page;
537
	}
538
539
	/**
540
	 * Add navigation menu.
541
	 */
542
	public function load_legacy_menu() {
543
		$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' ) );
544
545
		/**
546
		 * Runs just after adding all *delete* menu items to Bulk WP main menu.
547
		 *
548
		 * This action is primarily for adding extra *delete* menu items to the Bulk WP main menu.
549
		 *
550
		 * @since 5.3
551
		 */
552
		do_action( 'bd_after_primary_menus' );
553
554
		/**
555
		 * Runs just before adding non-action menu items to Bulk WP main menu.
556
		 *
557
		 * This action is primarily for adding extra menu items before non-action menu items to the Bulk WP main menu.
558
		 *
559
		 * @since 5.3
560
		 */
561
		do_action( 'bd_before_secondary_menus' );
562
563
		$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' ) );
564
		$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' ) );
565
566
		/**
567
		 * Runs just after adding all menu items to Bulk WP main menu.
568
		 *
569
		 * This action is primarily for adding extra menu items to the Bulk WP main menu.
570
		 *
571
		 * @since 5.3
572
		 */
573
		do_action( 'bd_after_all_menus' );
574
575
		$admin_pages = $this->get_admin_pages();
576
		$pages_page  = $admin_pages['bulk-delete-pages'];
577
578
		// enqueue JavaScript
579
		add_action( 'admin_print_scripts-' . $this->posts_page, array( $pages_page, 'enqueue_assets' ) );
580
581
		// delete posts page
582
		add_action( "load-{$this->posts_page}", array( $this, 'add_delete_posts_settings_panel' ) );
583
		add_action( "add_meta_boxes_{$this->posts_page}", array( $this, 'add_delete_posts_meta_boxes' ) );
584
	}
585
586
	/**
587
	 * Add settings Panel for delete posts page.
588
	 */
589
	public function add_delete_posts_settings_panel() {
590
		/**
591
		 * Add contextual help for admin screens.
592
		 *
593
		 * @since 5.1
594
		 */
595
		do_action( 'bd_add_contextual_help', $this->posts_page );
596
597
		/* Trigger the add_meta_boxes hooks to allow meta boxes to be added */
598
		do_action( 'add_meta_boxes_' . $this->posts_page, null );
599
600
		/* Enqueue WordPress' script for handling the meta boxes */
601
		wp_enqueue_script( 'postbox' );
602
	}
603
604
	/**
605
	 * Ajax call back function for getting taxonomies to load select2 options.
606
	 *
607
	 * @since 6.0.0
608
	 */
609
	public function load_taxonomy_term(){
610
		$response = array();
611
612
		$taxonomy = sanitize_text_field( $_GET['taxonomy'] );
613
614
		$terms = get_terms( array(
615
			'taxonomy'   => $taxonomy,
616
			'hide_empty' => false,
617
			'search'     => sanitize_text_field($_GET['q']),
618
		) );
619
620
		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
621
			foreach ( $terms as $term ) {
622
				$response[] = array( absint($term->term_id), $term->name . ' (' . $term->count . __( ' Posts', 'bulk-delete' ) . ')' );
623
			}
624
		}
625
626
		echo json_encode( $response );
627
		die;
628
	}
629
630
	/**
631
	 * Register meta boxes for delete posts page.
632
	 */
633
	public function add_delete_posts_meta_boxes() {
634
		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' );
635
		add_meta_box( self::BOX_CATEGORY      , __( 'By Category'          , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_category_box'  , $this->posts_page , 'advanced' );
636
		add_meta_box( self::BOX_TAG           , __( 'By Tag'               , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_tag_box'       , $this->posts_page , 'advanced' );
637
		add_meta_box( self::BOX_TAX           , __( 'By Custom Taxonomy'   , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_taxonomy_box'  , $this->posts_page , 'advanced' );
638
		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' );
639
		add_meta_box( self::BOX_URL           , __( 'By URL'               , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_delete_posts_by_url_box'       , $this->posts_page , 'advanced' );
640
		add_meta_box( self::BOX_POST_REVISION , __( 'By Post Revision'     , 'bulk-delete' ) , 'Bulk_Delete_Posts::render_posts_by_revision_box'         , $this->posts_page , 'advanced' );
641
642
		/**
643
		 * Add meta box in delete posts page
644
		 * This hook can be used for adding additional meta boxes in delete posts page.
645
		 *
646
		 * @since 5.3
647
		 */
648
		do_action( 'bd_add_meta_box_for_posts' );
649
	}
650
651
	/**
652
	 * Enqueue Scripts and Styles.
653
	 */
654
	public function add_script() {
655
		// TODO: Remove this function.
656
657
		$admin_pages = $this->get_admin_pages();
658
		$pages_page  = $admin_pages['bulk-delete-pages'];
659
		$pages_page->enqueue_assets();
660
	}
661
662
	/**
663
	 * Show the delete posts page.
664
	 *
665
	 * @Todo Move this function to Bulk_Delete_Posts class
666
	 */
667
	public function display_posts_page() {
668
?>
669
<div class="wrap">
670
    <h2><?php _e( 'Bulk Delete Posts', 'bulk-delete' );?></h2>
671
    <?php settings_errors(); ?>
672
673
    <form method = "post">
674
<?php
675
		// nonce for bulk delete
676
		wp_nonce_field( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' );
677
678
		/* Used to save closed meta boxes and their order */
679
		wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
680
		wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
681
?>
682
    <div id = "poststuff">
683
        <div id="post-body" class="metabox-holder columns-1">
684
685
            <div class="notice notice-warning">
686
                <p><strong><?php _e( 'WARNING: Posts deleted once cannot be retrieved back. Use with caution.', 'bulk-delete' ); ?></strong></p>
687
            </div>
688
689
            <div id="postbox-container-2" class="postbox-container">
690
                <?php do_meta_boxes( '', 'advanced', null ); ?>
691
            </div> <!-- #postbox-container-2 -->
692
693
        </div> <!-- #post-body -->
694
    </div><!-- #poststuff -->
695
    </form>
696
</div><!-- .wrap -->
697
698
<?php
699
		/**
700
		 * Runs just before displaying the footer text in the "Bulk Delete Posts" admin page.
701
		 *
702
		 * This action is primarily for adding extra content in the footer of "Bulk Delete Posts" admin page.
703
		 *
704
		 * @since 5.0
705
		 */
706
		do_action( 'bd_admin_footer_posts_page' );
707
	}
708
709
	/**
710
	 * Display the schedule page.
711
	 */
712
	public function display_cron_page() {
713
		if ( ! class_exists( 'WP_List_Table' ) ) {
714
			require_once ABSPATH . WPINC . '/class-wp-list-table.php';
715
		}
716
717
		if ( ! class_exists( 'Cron_List_Table' ) ) {
718
			require_once self::$PLUGIN_DIR . '/include/cron/class-cron-list-table.php';
719
		}
720
721
		// Prepare Table of elements
722
		$cron_list_table = new Cron_List_Table();
723
		$cron_list_table->prepare_items();
724
?>
725
    <div class="wrap">
726
        <h2><?php _e( 'Bulk Delete Schedules', 'bulk-delete' );?></h2>
727
        <?php settings_errors(); ?>
728
<?php
729
		// Table of elements
730
		$cron_list_table->display();
731
		bd_display_available_addon_list();
732
?>
733
    </div>
734
<?php
735
		/**
736
		 * Runs just before displaying the footer text in the "Schedules" admin page.
737
		 *
738
		 * This action is primarily for adding extra content in the footer of "Schedules" admin page.
739
		 *
740
		 * @since 5.0
741
		 */
742
		do_action( 'bd_admin_footer_cron_page' );
743
	}
744
745
	/**
746
	 * Get path to main plugin file.
747
	 *
748
	 * @return string Plugin file.
749
	 */
750 1
	public function get_plugin_file() {
751 1
		return $this->plugin_file;
752
	}
753
754
	/**
755
	 * Set path to main plugin file.
756
	 *
757
	 * @param string $plugin_file Path to main plugin file.
758
	 */
759 1
	public function set_plugin_file( $plugin_file ) {
760 1
		$this->plugin_file       = $plugin_file;
761 1
		$this->translations_path = dirname( plugin_basename( $this->get_plugin_file() ) ) . '/languages/';
762 1
	}
763
764
	/**
765
	 * Get path to translations.
766
	 *
767
	 * @return string Translations path.
768
	 */
769 1
	public function get_translations_path() {
770 1
		return $this->translations_path;
771
	}
772
}
773
774
/**
775
 * The main function responsible for returning the one true Bulk_Delete
776
 * Instance to functions everywhere.
777
 *
778
 * Use this function like you would a global variable, except without needing
779
 * to declare the global.
780
 *
781
 * Example: `<?php $bulk_delete = BULK_DELETE(); ?>`
782
 *
783
 * @since 5.0
784
 *
785
 * @return Bulk_Delete The one true Bulk_Delete Instance
786
 */
787
function BULK_DELETE() {
788 1
	return Bulk_Delete::get_instance();
789
}
790
791
/**
792
 * Load Bulk Delete plugin.
793
 *
794
 * @since 6.0.0
795
 */
796
function load_bulk_delete() {
797 1
	$bulk_delete = BULK_DELETE();
798 1
	$bulk_delete->set_plugin_file( __FILE__ );
799
800 1
	add_action( 'plugins_loaded', array( $bulk_delete, 'load' ), 101 );
801 1
}
802
803
load_bulk_delete();
804