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

Bulk_Delete::__get()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 20
nc 5
nop 1
dl 0
loc 29
ccs 0
cts 15
cp 0
crap 42
rs 8.439
c 1
b 1
f 0
1
<?php
2
/**
3
 * Old version of Bulk_Delete.
4
 *
5
 * This class is deprecated since 6.0.0. But included here for backward compatibility.
6
 * Don't depend on functionality from this class.
7
 */
8
use BulkWP\BulkDelete\Core\BulkDelete;
9
10
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
11
12
/**
13
 * Main Bulk_Delete class.
14
 *
15
 * @property string|null translations
16
 * @property string|null posts_page
17
 * @property string|null pages_page
18
 * @property string|null users_page
19
 * @property string|null metas_page
20
 *
21
 * @since 5.0 Singleton
22
 * @since 6.0.0 Deprecated.
23
 */
24
final class Bulk_Delete {
25
	/**
26
	 * The one true Bulk_Delete instance.
27
	 *
28
	 * @var Bulk_Delete
29
	 *
30
	 * @since 5.0
31
	 */
32
	private static $instance;
33
34
	/**
35
	 * Path to the main plugin file.
36
	 *
37
	 * @var string
38
	 */
39
	private $plugin_file;
40
41
	// Deprecated constants. They are defined here for backward compatibility.
42
	const VERSION = '5.6.1';
43
44
	const JS_HANDLE = 'bulk-delete';
45
46
	// Cron hooks.
47
	const CRON_HOOK_PAGES_STATUS = 'do-bulk-delete-pages-by-status'; // used in Scheduler For Deleting Pages by Post status add-on v0.6.
48
49
	const CRON_HOOK_POST_STATUS = 'do-bulk-delete-post-status';      // used in Scheduler For Deleting Posts by Post status add-on v0.6.
50
	const CRON_HOOK_CATEGORY    = 'do-bulk-delete-cat';              // used in Scheduler For Deleting Posts by Category add-on v0.6.
51
	const CRON_HOOK_TAG         = 'do-bulk-delete-tag';              // used in Scheduler For Deleting Posts by Tag add-on v0.6.
52
	const CRON_HOOK_TAXONOMY    = 'do-bulk-delete-taxonomy';         // used in Scheduler For Deleting Posts by Taxonomy add-on v0.6.
53
	const CRON_HOOK_POST_TYPE   = 'do-bulk-delete-post-type';        // used in Scheduler For Deleting Posts by Post Type add-on v0.6.
54
	const CRON_HOOK_USER_ROLE   = 'do-bulk-delete-users-by-role';    // used in Scheduler for Deleting Users by User Role add-on v0.6.
55
56
	// page slugs
57
	const POSTS_PAGE_SLUG           = 'bulk-delete-posts';
58
	const PAGES_PAGE_SLUG           = 'bulk-delete-pages';
59
	const CRON_PAGE_SLUG            = 'bulk-delete-cron';
60
	const ADDON_PAGE_SLUG           = 'bulk-delete-addon';
61
62
	// Cron hooks
63
	const CRON_HOOK_CUSTOM_FIELD    = 'do-bulk-delete-custom-field';
64
	const CRON_HOOK_TITLE           = 'do-bulk-delete-by-title';
65
	const CRON_HOOK_DUPLICATE_TITLE = 'do-bulk-delete-by-duplicate-title';
66
	const CRON_HOOK_POST_BY_ROLE    = 'do-bulk-delete-posts-by-role';
67
68
	// meta boxes for delete posts
69
	const BOX_CUSTOM_FIELD          = 'bd_by_custom_field';
70
	const BOX_TITLE                 = 'bd_by_title';
71
	const BOX_DUPLICATE_TITLE       = 'bd_by_duplicate_title';
72
	const BOX_POST_FROM_TRASH       = 'bd_posts_from_trash';
73
	const BOX_POST_BY_ROLE          = 'bd_post_by_user_role';
74
75
	// meta boxes for delete pages
76
	const BOX_PAGE_FROM_TRASH       = 'bd_pages_from_trash';
77
78
	// Settings constants
79
	const SETTING_OPTION_GROUP      = 'bd_settings';
80
	const SETTING_OPTION_NAME       = 'bd_licenses';
81
	const SETTING_SECTION_ID        = 'bd_license_section';
82
83
	// Transient keys
84
	const LICENSE_CACHE_KEY_PREFIX  = 'bd-license_';
85
86
	// path variables
87
	// Ideally these should be constants, but because of PHP's limitations, these are static variables
88
	public static $PLUGIN_DIR;
89
	public static $PLUGIN_FILE;
90
91
	// Instance variables
92
	public $settings_page;
93
	public $misc_page;
94
	public $display_activate_license_form = false;
95
96
	/**
97
	 * Main Bulk_Delete Instance.
98
	 *
99
	 * Insures that only one instance of Bulk_Delete exists in memory at any one
100
	 * time. Also prevents needing to define globals all over the place.
101
	 *
102
	 * @since 5.0
103
	 * @static
104
	 * @staticvar array $instance
105
	 *
106
	 * @see BULK_DELETE()
107
	 *
108
	 * @return Bulk_Delete The one true instance of Bulk_Delete
109
	 */
110
	public static function get_instance() {
111
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Bulk_Delete ) ) {
112
			self::$instance = new Bulk_Delete();
113
		}
114
115
		return self::$instance;
116
	}
117
118
	/**
119
	 * Throw error on object clone.
120
	 *
121
	 * The whole idea of the singleton design pattern is that there is a single
122
	 * object therefore, we don't want the object to be cloned.
123
	 *
124
	 * @since  5.0
125
	 * @access protected
126
	 *
127
	 * @return void
128
	 */
129
	public function __clone() {
130
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bulk-delete' ), '5.0' );
131
	}
132
133
	/**
134
	 * Disable unserializing of the class.
135
	 *
136
	 * @since  5.0
137
	 * @access protected
138
	 *
139
	 * @return void
140
	 */
141
	public function __wakeup() {
142
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bulk-delete' ), '5.0' );
143
	}
144
145
	/**
146
	 * Set path to main plugin file.
147
	 *
148
	 * @param string $plugin_file Path to main plugin file.
149
	 */
150
	public function set_plugin_file( $plugin_file ) {
151
		$this->plugin_file = $plugin_file;
152
153
		self::$PLUGIN_DIR  = plugin_dir_path( $plugin_file );
154
		self::$PLUGIN_FILE = $plugin_file;
155
	}
156
157
	/**
158
	 * Get path to main plugin file.
159
	 *
160
	 * @return string Plugin file.
161
	 */
162
	public function get_plugin_file() {
163
		return $this->plugin_file;
164
	}
165
166
	/**
167
	 * Monkey patch the old `add_script` method.
168
	 *
169
	 * @since 6.0.0
170
	 */
171
	public function add_script() {
172
		$bd = BulkDelete::get_instance();
173
174
		$primary_pages = $bd->get_primary_pages();
175
		$post_page     = $primary_pages[ self::POSTS_PAGE_SLUG ];
176
177
		$post_page->enqueue_assets();
178
	}
179
180
	/**
181
	 * Provide access to old public fields through Magic method.
182
	 *
183
	 * This function is added to provide backward compatibility and will be eventually removed from future versions.
184
	 *
185
	 * @since 6.0.0
186
	 *
187
	 * @param string $name Field.
188
	 *
189
	 * @return string|null
190
	 */
191
	public function __get( $name ) {
192
		$new_bd = BulkDelete::get_instance();
193
194
		switch ( $name ) {
195
			case 'translations':
196
				return $new_bd->get_translations_path();
197
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
198
199
			case 'posts_page':
200
				return $new_bd->get_page_hook_suffix( 'bulk-delete-posts' );
201
				break;
202
203
			case 'pages_page':
204
				return $new_bd->get_page_hook_suffix( 'bulk-delete-pages' );
205
				break;
206
207
			case 'users_page':
208
				return $new_bd->get_page_hook_suffix( 'bulk-delete-users' );
209
				break;
210
211
			case 'meta_page':
212
				return $new_bd->get_page_hook_suffix( 'bulk-delete-metas' );
213
				break;
214
		}
215
216
		$trace = debug_backtrace();
217
		trigger_error( 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE );
218
219
		return null;
220
	}
221
}
222
223
/**
224
 * The main function responsible for returning the one true Bulk_Delete
225
 * Instance to functions everywhere.
226
 *
227
 * Use this function like you would a global variable, except without needing
228
 * to declare the global.
229
 *
230
 * Example: `<?php $bulk_delete = BULK_DELETE(); ?>`
231
 *
232
 * @since 5.0
233
 *
234
 * @return Bulk_Delete The one true Bulk_Delete Instance
235
 */
236
function BULK_DELETE() {
237
	return Bulk_Delete::get_instance();
238
}
239
240
/**
241
 * Setup old Bulk_Delete class for backward compatibility reasons.
242
 *
243
 * Eventually this will be removed.
244
 *
245
 * @since 6.0.0
246
 *
247
 * @param string $plugin_file Main plugin file.
248
 */
249
function bd_setup_backward_compatibility( $plugin_file ) {
250
	$bd = BULK_DELETE();
251
	$bd->set_plugin_file( $plugin_file );
252
}
253
add_action( 'bd_loaded', 'bd_setup_backward_compatibility' );
254