Completed
Push — dev/6.0.0 ( 1147b4...030ea1 )
by Sudar
09:12
created

bd_delete_options_compatibility()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.2964

Importance

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