1 | <?php |
||
11 | class Bulk_Delete_Post_Meta { |
||
12 | /** |
||
13 | * Box slug. |
||
14 | * |
||
15 | * @since 5.4 |
||
16 | */ |
||
17 | const BOX_POST_META = 'bd-post-meta'; |
||
18 | |||
19 | /** |
||
20 | * Cron Hook. |
||
21 | * |
||
22 | * @since 5.4 |
||
23 | */ |
||
24 | const CRON_HOOK = 'do-bulk-delete-post-meta'; |
||
25 | |||
26 | /** |
||
27 | * Register post-meta meta box for delete meta page. |
||
28 | * |
||
29 | * @static |
||
30 | * |
||
31 | * @since 5.4 |
||
32 | */ |
||
33 | public static function add_delete_post_meta_box() { |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Render delete post-meta meta box for delete meta page. |
||
47 | * |
||
48 | * @static |
||
49 | * |
||
50 | * @since 5.4 |
||
51 | */ |
||
52 | public static function render_delete_post_meta_box() { |
||
53 | if ( Bulk_Delete_Meta::is_meta_box_hidden( self::BOX_POST_META ) ) { |
||
54 | printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete_meta::META_PAGE_SLUG ); |
||
55 | |||
56 | return; |
||
57 | } |
||
58 | ?> |
||
59 | <!-- Post Meta box start--> |
||
60 | <fieldset class="options"> |
||
61 | <?php |
||
62 | $types = get_post_types( array( |
||
63 | 'public' => true, |
||
64 | '_builtin' => false, |
||
65 | ), 'names' |
||
66 | ); |
||
67 | |||
68 | array_unshift( $types, 'post' ); |
||
69 | ?> |
||
70 | <h4><?php _e( 'Select the post type whose post meta fields you want to delete', 'bulk-delete' ); ?></h4> |
||
71 | <table class="optiontable"> |
||
72 | <?php |
||
73 | foreach ( $types as $type ) { |
||
74 | ?> |
||
75 | <tr> |
||
76 | <td> |
||
77 | <input name="smbd_pm_post_type" value = "<?php echo $type; ?>" type = "radio" class = "smbd_pm_post_type" <?php checked( $type, 'post' ); ?>> |
||
78 | <label for="smbd_pm_post_type"><?php echo $type; ?> </label> |
||
79 | </td> |
||
80 | </tr> |
||
81 | <?php |
||
82 | } |
||
83 | ?> |
||
84 | </table> |
||
85 | |||
86 | <h4><?php _e( 'Choose your post meta field settings', 'bulk-delete' ); ?></h4> |
||
87 | <table class="optiontable"> |
||
88 | <tr> |
||
89 | <td> |
||
90 | <input name="smbd_pm_use_value" value="use_key" type="radio" checked> |
||
91 | <label for="smbd_pm_use_value"><?php echo __( 'Delete based on post meta key name only', 'bulk-delete' ); ?></label> |
||
92 | </td> |
||
93 | </tr> |
||
94 | |||
95 | <tr> |
||
96 | <td> |
||
97 | <input name="smbd_pm_use_value" id="smdb_pm_use_key_compare" value="use_key_compare" type="radio" disabled> |
||
98 | <label for="smbd_pm_use_value"><?php echo __( 'Delete based on post meta key name prefix or postfix', 'bulk-delete' ); ?></label> |
||
99 | <span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
||
100 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p" target="_blank">Buy now</a> |
||
101 | </span> |
||
102 | </td> |
||
103 | </tr> |
||
104 | |||
105 | <tr> |
||
106 | <td> |
||
107 | <input name="smbd_pm_use_value" id="smbd_pm_use_value" value="use_value" type="radio" disabled> |
||
108 | <label for="smbd_pm_use_value"><?php echo __( 'Delete based on post meta key name and value', 'bulk-delete' ); ?></label> |
||
109 | <span class="bd-pm-pro" style="color:red; vertical-align: middle;"> |
||
110 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p" target="_blank">Buy now</a> |
||
111 | </span> |
||
112 | </td> |
||
113 | </tr> |
||
114 | |||
115 | <tr> |
||
116 | <td> |
||
117 | <label for="smbd_pm_key"><?php _e( 'Post Meta Key ', 'bulk-delete' ); ?></label> |
||
118 | <select name="smbd_pm_key_prefix_postfix" id="smbd_pm_key_prefix_postfix" style="display: none;"> |
||
119 | <option value="starts_with">starts with</option> |
||
120 | <option value="contains">contains</option> |
||
121 | <option value="ends_with">ends with</option> |
||
122 | </select> |
||
123 | <input name="smbd_pm_key" id="smbd_pm_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>"> |
||
124 | </td> |
||
125 | </tr> |
||
126 | </table> |
||
127 | <?php |
||
128 | /** |
||
129 | * Add more fields to the delete post meta field form. |
||
130 | * This hook can be used to add more fields to the delete post meta field form. |
||
131 | * |
||
132 | * @since 5.4 |
||
133 | */ |
||
134 | do_action( 'bd_delete_post_meta_form' ); |
||
135 | ?> |
||
136 | <table class="optiontable"> |
||
137 | <tr> |
||
138 | <td> |
||
139 | <h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
||
140 | </td> |
||
141 | </tr> |
||
142 | |||
143 | <tr> |
||
144 | <td> |
||
145 | <input name="smbd_pm_restrict" id="smbd_pm_restrict" value = "true" type = "checkbox" > |
||
146 | <?php _e( 'Only restrict to posts which are ', 'bulk-delete' );?> |
||
147 | <select name="smbd_pm_op" id="smbd_pm_op" disabled> |
||
148 | <option value ="before"><?php _e( 'older than', 'bulk-delete' );?></option> |
||
149 | <option value ="after"><?php _e( 'posted within last', 'bulk-delete' );?></option> |
||
150 | </select> |
||
151 | <input type ="textbox" name="smbd_pm_days" id="smbd_pm_days" disabled value ="0" maxlength="4" size="4"><?php _e( 'days', 'bulk-delete' );?> |
||
152 | </td> |
||
153 | </tr> |
||
154 | |||
155 | <tr> |
||
156 | <td> |
||
157 | <input name="smbd_pm_limit" id="smbd_pm_limit" value = "true" type = "checkbox"> |
||
158 | <?php _e( 'Only delete post meta field from first ', 'bulk-delete' );?> |
||
159 | <input type ="textbox" name="smbd_pm_limit_to" id="smbd_pm_limit_to" disabled value ="0" maxlength="4" size="4"><?php _e( 'posts.', 'bulk-delete' );?> |
||
160 | <?php _e( 'Use this option if there are more than 1000 posts and the script times out.', 'bulk-delete' ) ?> |
||
161 | </td> |
||
162 | </tr> |
||
163 | |||
164 | <tr> |
||
165 | <td> |
||
166 | <input name="smbd_pm_cron" value = "false" type = "radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
||
167 | <input name="smbd_pm_cron" value = "true" type = "radio" id = "smbd_pm_cron" disabled > <?php _e( 'Schedule', 'bulk-delete' ); ?> |
||
168 | <input name="smbd_pm_cron_start" id = "smbd_pm_cron_start" value = "now" type = "text" disabled><?php _e( 'repeat ', 'bulk-delete' );?> |
||
169 | <select name="smbd_pm_cron_freq" id = "smbd_pm_cron_freq" disabled> |
||
170 | <option value = "-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
||
171 | <?php |
||
172 | $schedules = wp_get_schedules(); |
||
173 | foreach ( $schedules as $key => $value ) { |
||
174 | ?> |
||
175 | <option value = "<?php echo $key; ?>"><?php echo $value['display']; ?></option> |
||
176 | <?php |
||
177 | } |
||
178 | ?> |
||
179 | </select> |
||
180 | <span class="bd-pm-pro" style="color:red"> |
||
181 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p">Buy now</a> |
||
182 | </span> |
||
183 | </td> |
||
184 | </tr> |
||
185 | |||
186 | <tr> |
||
187 | <td> |
||
188 | <?php _e( 'Enter time in Y-m-d H:i:s format or enter now to use current time', 'bulk-delete' );?> |
||
189 | </td> |
||
190 | </tr> |
||
191 | |||
192 | </table> |
||
193 | </fieldset> |
||
194 | |||
195 | <p> |
||
196 | <button type="submit" name="bd_action" value="delete_meta_post" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ) ?>»</button> |
||
197 | </p> |
||
198 | <!-- Post Meta box end--> |
||
199 | <?php |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Filter JS Array and add validation hooks. |
||
204 | * |
||
205 | * @since 5.4 |
||
206 | * @static |
||
207 | * |
||
208 | * @param array $js_array JavaScript Array |
||
209 | * |
||
210 | * @return array Modified JavaScript Array |
||
211 | */ |
||
212 | public static function filter_js_array( $js_array ) { |
||
213 | $js_array['dt_iterators'][] = '_pm'; |
||
214 | $js_array['validators']['delete_meta_post'] = 'noValidation'; |
||
215 | |||
216 | $js_array['pre_action_msg']['delete_meta_post'] = 'deletePMWarning'; |
||
217 | $js_array['msg']['deletePMWarning'] = __( 'Are you sure you want to delete all the post meta fields that match the selected filters?', 'bulk-delete' ); |
||
218 | |||
219 | return $js_array; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Controller for deleting post meta fields. |
||
224 | * |
||
225 | * @static |
||
226 | * |
||
227 | * @since 5.4 |
||
228 | */ |
||
229 | public static function do_delete_post_meta() { |
||
271 | ); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Delete Post Meta. |
||
276 | * |
||
277 | * @static |
||
278 | * |
||
279 | * @since 5.4 |
||
280 | * |
||
281 | * @param array $delete_options Options for deleting |
||
282 | * |
||
283 | * @return int Number of posts that were deleted |
||
284 | */ |
||
285 | public static function delete_post_meta( $delete_options ) { |
||
286 | $count = 0; |
||
287 | $post_type = $delete_options['post_type']; |
||
288 | $limit_to = $delete_options['limit_to']; |
||
289 | $meta_key = $delete_options['meta_key']; |
||
290 | $use_value = $delete_options['use_value']; |
||
291 | $restrict = $delete_options['restrict']; |
||
292 | $days = $delete_options['days']; |
||
293 | $op = $delete_options['op']; |
||
294 | |||
295 | $options = array( |
||
296 | 'post_status' => 'publish', |
||
297 | 'post_type' => $post_type, |
||
298 | ); |
||
299 | |||
300 | if ( $limit_to > 0 ) { |
||
301 | $options['showposts'] = $limit_to; |
||
302 | } else { |
||
303 | $options['nopaging'] = 'true'; |
||
304 | } |
||
305 | |||
306 | if ( $restrict ) { |
||
307 | $options['date_query'] = array( |
||
308 | array( |
||
309 | 'column' => 'post_date', |
||
310 | $op => "{$days} day ago", |
||
311 | ), |
||
312 | ); |
||
313 | } |
||
314 | |||
315 | if ( 'use_key' === $use_value ) { |
||
316 | $options['meta_key'] = $meta_key; |
||
351 |