bd_convert_old_options_for_delete_pages()   C
last analyzed

Complexity

Conditions 15
Paths 129

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 17.4683

Importance

Changes 0
Metric Value
cc 15
eloc 18
nc 129
nop 2
dl 0
loc 35
ccs 14
cts 18
cp 0.7778
crap 17.4683
rs 5.675
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Deprecated and backward compatibility code.
4
 * Don't depend on the code in this file. It would be removed in future versions of the plugin.
5
 *
6
 * @author     Sudar
7
 *
8
 * @package    BulkDelete\Util\Deprecated
9
 *
10
 * @since 5.5
11
 */
12
use BulkWP\BulkDelete\Core\Base\BaseModule;
13
14
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
15
16
/**
17
 * Backward compatibility for delete options.
18
 *
19
 * @since 5.5
20
 *
21
 * @param array                                   $options Old options.
22
 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module  Modules.
23
 *
24
 * @return array New options.
25
 */
26
function bd_delete_options_compatibility( $options, $module = null ) {
27 318
	if ( $module instanceof BaseModule && 'delete_pages_by_status' === $module->get_action() ) {
28 21
		return $options;
29
	}
30
31
	// Convert bool keys to boolean
32 297
	$bool_keys = array( 'restrict', 'force_delete', 'private' );
33 297
	foreach ( $bool_keys as $key ) {
34 297
		if ( array_key_exists( $key, $options ) ) {
35 176
			$options[ $key ] = bd_to_bool( $options[ $key ] );
36
		}
37
	}
38
39
	// convert old date comparison operators
40 297
	if ( array_key_exists( 'date_op', $options ) && array_key_exists( 'days', $options ) ) {
41 127
		if ( '<' == $options['date_op'] ) {
42
			$options['date_op'] = 'before';
43 127
		} elseif ( '>' == $options['date_op'] ) {
44
			$options['date_op'] = 'after';
45
		}
46
	}
47
48 297
	return $options;
49
}
50
add_filter( 'bd_delete_options', 'bd_delete_options_compatibility', 10, 2 );
51
52
/**
53
 * Handle backward compatibility for Delete Pages by status delete options.
54
 *
55
 * Backward compatibility code. Will eventually be removed.
56
 *
57
 * @since 6.0.0
58
 *
59
 * @param array                                   $options Delete Options.
60
 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module  Modules.
61
 *
62
 * @return array Processed delete options.
63
 */
64
function bd_convert_old_options_for_delete_pages( $options, $module = null ) {
65 318
	if ( $module instanceof BaseModule && 'delete_pages_by_status' !== $module->get_action() ) {
66 297
		return $options;
67
	}
68
69 21
	if ( array_key_exists( 'page_op', $options ) ) {
70
		$options['date_op'] = $options['page_op'];
71
		$options['days']    = $options['page_days'];
72
	}
73
74 21
	if ( ! array_key_exists( 'post_status', $options ) ) {
75 6
		$options['post_status'] = array();
76
	}
77
78 21
	if ( array_key_exists( 'publish', $options ) && 'published_pages' === $options['publish'] ) {
79 5
		$options['post_status'][] = 'publish';
80
	}
81
82 21
	if ( array_key_exists( 'drafts', $options ) && 'draft_pages' === $options['drafts'] ) {
83 5
		$options['post_status'][] = 'draft';
84
	}
85
86 21
	if ( array_key_exists( 'pending', $options ) && 'pending_pages' === $options['pending'] ) {
87
		$options['post_status'][] = 'pending';
88
	}
89
90 21
	if ( array_key_exists( 'future', $options ) && 'future_pages' === $options['future'] ) {
91
		$options['post_status'][] = 'future';
92
	}
93
94 21
	if ( array_key_exists( 'private', $options ) && 'private_pages' === $options['private'] ) {
95 1
		$options['post_status'][] = 'private';
96
	}
97
98 21
	return $options;
99
}
100
add_filter( 'bd_delete_options', 'bd_convert_old_options_for_delete_pages', 10, 2 );
101
102
/**
103
 * Handle backward compatibility for Delete Posts by category delete options.
104
 *
105
 * Backward compatibility code. Will be removed in future Bulk Delete releases.
106
 *
107
 * @since 6.0.0
108
 *
109
 * @param array $options Delete Options.
110
 *
111
 * @return array Processed delete options.
112
 */
113
function bd_convert_old_options_for_delete_posts_by_category( $options ) {
114 318
	if ( array_key_exists( 'cats_op', $options ) ) {
115
		$options['date_op'] = $options['cats_op'];
116
		$options['days']    = $options['cats_days'];
117
	}
118
119 318
	return $options;
120
}
121
add_filter( 'bd_delete_options', 'bd_convert_old_options_for_delete_posts_by_category' );
122
123
/**
124
 * Handle backward compatibility for Delete Posts by tag delete options.
125
 *
126
 * Backward compatibility code. Will be removed in future Bulk Delete releases.
127
 *
128
 * @since 6.0.0
129
 *
130
 * @param array                                   $options Delete Options.
131
 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module  Modules.
132
 *
133
 * @return array Processed delete options.
134
 */
135
function bd_convert_old_options_for_delete_posts_by_tag( $options, $module = null ) {
136 318
	if ( $module instanceof BaseModule && 'delete_posts_by_tag' !== $module->get_action() ) {
137 310
		return $options;
138
	}
139
140 8
	if ( array_key_exists( 'tags_op', $options ) ) {
141
		$options['date_op'] = $options['tags_op'];
142
		$options['days']    = $options['tags_days'];
143
	}
144
145 8
	return $options;
146
}
147
add_filter( 'bd_delete_options', 'bd_convert_old_options_for_delete_posts_by_tag', 10, 2 );
148
149
/**
150
 * Handle backward compatibility for Delete Posts by status delete options.
151
 *
152
 * Backward compatibility code. Will be removed in Bulk Delete v6.0.
153
 *
154
 * @since 5.6.0
155
 * @since 6.0.0 Added Modules parameter.
156
 *
157
 * @param array                                   $delete_options Delete Options.
158
 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module         Modules.
159
 *
160
 * @return array Processed delete options.
161
 */
162
function bd_convert_old_options_for_delete_post_by_status( $delete_options, $module = null ) {
163 318
	if ( $module instanceof BaseModule && 'delete_posts_by_status' !== $module->get_action() ) {
164 299
		return $delete_options;
165
	}
166
167
	// Format changed in 5.5.0.
168 19
	if ( array_key_exists( 'post_status_op', $delete_options ) ) {
169
		$delete_options['date_op'] = $delete_options['post_status_op'];
170
		$delete_options['days']    = $delete_options['post_status_days'];
171
	}
172
173
	// Format changed in 5.6.0.
174 19
	if ( isset( $delete_options['sticky'] ) ) {
175
		if ( 'sticky' === $delete_options['sticky'] ) {
176
			$delete_options['delete-sticky-posts'] = true;
177
		} else {
178
			$delete_options['delete-sticky-posts'] = false;
179
		}
180
	}
181
182 19
	if ( ! isset( $delete_options['post_status'] ) ) {
183
		$delete_options['post_status'] = array();
184
	}
185
186 19
	$old_statuses = array( 'publish', 'draft', 'pending', 'future', 'private' );
187
188 19
	foreach ( $old_statuses as $old_status ) {
189 19
		if ( isset( $delete_options[ $old_status ] ) ) {
190
			$delete_options['post_status'][] = $old_status;
191
		}
192
	}
193
194 19
	if ( isset( $delete_options['drafts'] ) && 'drafts' === $delete_options['drafts'] ) {
195
		$delete_options['post_status'][] = 'draft';
196
	}
197
198 19
	return $delete_options;
199
}
200
add_filter( 'bd_delete_options', 'bd_convert_old_options_for_delete_post_by_status', 10, 2 );
201
202
/**
203
 * Handle backward compatibility for Delete Posts by Taxonomy delete options.
204
 *
205
 * Backward compatibility code. Will be removed in Bulk Delete v6.0.
206
 *
207
 * @since 6.0.0
208
 *
209
 * @param array                                   $delete_options Delete Options.
210
 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module         Modules.
211
 *
212
 * @return array Processed delete options.
213
 */
214
function bd_convert_old_options_for_delete_post_by_taxonomy( $delete_options, $module = null ) {
215 318
	if ( $module instanceof BaseModule && 'bd_delete_posts_by_taxonomy' !== $module->get_action() ) {
216 222
		return $delete_options;
217
	}
218
219 96
	if ( array_key_exists( 'taxs_op', $delete_options ) ) {
220
		$delete_options['date_op'] = $delete_options['taxs_op'];
221
		$delete_options['days']    = $delete_options['taxs_days'];
222
	}
223
224 96
	return $delete_options;
225
}
226
add_filter( 'bd_delete_options', 'bd_convert_old_options_for_delete_post_by_taxonomy', 10, 2 );
227
228
/**
229
 * Handle backward compatibility for Delete Posts by Post type delete options.
230
 *
231
 * Backward compatibility code. Will be removed in Bulk Delete v6.0.
232
 *
233
 * @since 6.0.0
234
 *
235
 * @param array                                   $delete_options Delete Options.
236
 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module         Modules.
237
 *
238
 * @return array Processed delete options.
239
 */
240
function bd_convert_old_options_for_delete_post_by_post_type( $delete_options, $module = null ) {
241 318
	if ( $module instanceof BaseModule && 'delete_posts_by_post_type' !== $module->get_action() ) {
242 308
		return $delete_options;
243
	}
244
245 10
	if ( array_key_exists( 'types_op', $delete_options ) ) {
246
		$delete_options['date_op'] = $delete_options['types_op'];
247
		$delete_options['days']    = $delete_options['types_days'];
248
	}
249
250 10
	return $delete_options;
251
}
252
add_filter( 'bd_delete_options', 'bd_convert_old_options_for_delete_post_by_post_type', 10, 2 );
253
254
/**
255
 * Enable cron for old pro addons that required separate JavaScript.
256
 * This will be removed in v6.0.
257
 *
258
 * @since 5.5
259
 *
260
 * @param array $js_array JavaScript Array
261
 *
262
 * @return array Modified JavaScript Array
263
 */
264
function bd_enable_cron_for_old_addons( $js_array ) {
265
	if ( ! function_exists( 'is_plugin_active' ) ) {
266
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
267
	}
268
269
	if ( is_plugin_active( 'bulk-delete-scheduler-for-deleting-users-by-role/bulk-delete-scheduler-for-deleting-users-by-role.php' ) ) {
270
		$js_array['pro_iterators'][] = 'u_role';
271
	}
272
273
	return $js_array;
274
}
275
add_filter( 'bd_javascript_array', 'bd_enable_cron_for_old_addons' );
276
277
// Deprecated functions.
278
279
if ( ! function_exists( 'array_get' ) ) {
280
	/**
281
	 * Deprecated. Use `bd_array_get`.
282
	 *
283
	 * @param mixed      $array
284
	 * @param mixed      $key
285
	 * @param mixed|null $default
286
	 */
287
	function array_get( $array, $key, $default = null ) {
288
		return bd_array_get( $array, $key, $default );
289
	}
290
}
291
292
if ( ! function_exists( 'array_get_bool' ) ) {
293
	/**
294
	 * Deprecated. Use `bd_array_get_bool`.
295
	 *
296
	 * @param mixed $array
297
	 * @param mixed $key
298
	 * @param mixed $default
299
	 */
300
	function array_get_bool( $array, $key, $default = false ) {
301
		return bd_array_get_bool( $array, $key, $default );
302
	}
303
}
304