1 | <?php |
||
11 | class Bulk_Delete_User_Meta { |
||
12 | /** |
||
13 | * Box slug. |
||
14 | * |
||
15 | * @since 5.4 |
||
16 | */ |
||
17 | const BOX_USER_META = 'bd-user-meta'; |
||
18 | |||
19 | /** |
||
20 | * Cron Hook. |
||
21 | * |
||
22 | * @since 5.4 |
||
23 | */ |
||
24 | const CRON_HOOK = 'do-bulk-delete-user-meta'; |
||
25 | |||
26 | /** |
||
27 | * Register user-meta meta box for delete meta page. |
||
28 | * |
||
29 | * @static |
||
30 | * |
||
31 | * @since 5.4 |
||
32 | */ |
||
33 | public static function add_delete_user_meta_box() { |
||
34 | $bd = BULK_DELETE(); |
||
35 | |||
36 | add_meta_box( |
||
37 | self::BOX_USER_META, |
||
38 | __( 'Bulk Delete User Meta', 'bulk-delete' ), |
||
39 | array( __CLASS__, 'render_delete_user_meta_box' ), |
||
40 | $bd->meta_page, |
||
41 | 'advanced' |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Render delete user-meta meta box for delete meta page. |
||
47 | * |
||
48 | * @static |
||
49 | * |
||
50 | * @since 5.4 |
||
51 | */ |
||
52 | public static function render_delete_user_meta_box() { |
||
53 | if ( Bulk_Delete_Meta::is_meta_box_hidden( self::BOX_USER_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 | <!-- User Meta box start--> |
||
60 | <fieldset class="options"> |
||
61 | <h4><?php _e( 'Select the user role whose user meta fields you want to delete', 'bulk-delete' ); ?></h4> |
||
62 | <table class="optiontable"> |
||
63 | <?php |
||
64 | $users_count = count_users(); |
||
65 | foreach ( $users_count['avail_roles'] as $role => $count ) { |
||
66 | ?> |
||
67 | <tr> |
||
68 | <td> |
||
69 | <input name="smbd_um_role" value = "<?php echo $role; ?>" type = "radio" <?php checked( $role, 'administrator' ); ?>> |
||
70 | <label for="smbd_um_role"><?php echo $role; ?> (<?php echo $count . ' '; _e( 'Users', 'bulk-delete' ); ?>)</label> |
||
71 | </td> |
||
72 | </tr> |
||
73 | <?php |
||
74 | } |
||
75 | ?> |
||
76 | </table> |
||
77 | |||
78 | <h4><?php _e( 'Choose your user meta field settings', 'bulk-delete' ); ?></h4> |
||
79 | <table class="optiontable"> |
||
80 | <tr> |
||
81 | <td> |
||
82 | <input name="smbd_um_use_value" value="false" type="radio" checked> |
||
83 | <label for="smbd_um_use_value"><?php echo __( 'Delete based on user meta key name only', 'bulk-delete' ); ?></label> |
||
84 | </td> |
||
85 | </tr> |
||
86 | |||
87 | <tr> |
||
88 | <td> |
||
89 | <input name="smbd_um_use_value" id="smbd_um_use_value" value="true" type="radio" disabled> |
||
90 | <label for="smbd_um_use_value"><?php echo __( 'Delete based on user meta key name and value', 'bulk-delete' ); ?></label> |
||
91 | <span class="bd-um-pro" style="color:red; vertical-align: middle;"> |
||
92 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-user-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-u" target="_blank">Buy now</a> |
||
93 | </span> |
||
94 | </td> |
||
95 | </tr> |
||
96 | |||
97 | <tr> |
||
98 | <td> |
||
99 | <label for="smbd_um_key"><?php _e( 'User Meta Key ', 'bulk-delete' ); ?></label> |
||
100 | <input name="smbd_um_key" id="smbd_um_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>"> |
||
101 | </td> |
||
102 | </tr> |
||
103 | </table> |
||
104 | <?php |
||
105 | /** |
||
106 | * Add more fields to the delete user meta field form. |
||
107 | * This hook can be used to add more fields to the delete user meta field form. |
||
108 | * |
||
109 | * @since 5.4 |
||
110 | */ |
||
111 | do_action( 'bd_delete_user_meta_form' ); |
||
112 | ?> |
||
113 | <table class="optiontable"> |
||
114 | <tr> |
||
115 | <td> |
||
116 | <h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
||
117 | </td> |
||
118 | </tr> |
||
119 | |||
120 | <tr> |
||
121 | <td> |
||
122 | <input name="smbd_um_limit" id="smbd_um_limit" value = "true" type = "checkbox"> |
||
123 | <?php _e( 'Only delete user meta field from first ', 'bulk-delete' );?> |
||
124 | <input type ="textbox" name="smbd_um_limit_to" id="smbd_um_limit_to" disabled value ="0" maxlength="4" size="4"><?php _e( 'users.', 'bulk-delete' );?> |
||
125 | <?php _e( 'Use this option if there are more than 1000 users and the script timesout.', 'bulk-delete' ) ?> |
||
126 | </td> |
||
127 | </tr> |
||
128 | |||
129 | <tr> |
||
130 | <td> |
||
131 | <input name="smbd_um_cron" value = "false" type = "radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
||
132 | <input name="smbd_um_cron" value = "true" type = "radio" id = "smbd_um_cron" disabled > <?php _e( 'Schedule', 'bulk-delete' ); ?> |
||
133 | <input name="smbd_um_cron_start" id = "smbd_um_cron_start" value = "now" type = "text" disabled><?php _e( 'repeat ', 'bulk-delete' );?> |
||
134 | <select name="smbd_um_cron_freq" id = "smbd_um_cron_freq" disabled> |
||
135 | <option value = "-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
||
136 | <?php |
||
137 | $schedules = wp_get_schedules(); |
||
138 | foreach ( $schedules as $key => $value ) { |
||
139 | ?> |
||
140 | <option value = "<?php echo $key; ?>"><?php echo $value['display']; ?></option> |
||
141 | <?php |
||
142 | } |
||
143 | ?> |
||
144 | </select> |
||
145 | <span class="bd-um-pro" style="color:red"> |
||
146 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-user-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-u">Buy now</a> |
||
147 | </span> |
||
148 | </td> |
||
149 | </tr> |
||
150 | |||
151 | <tr> |
||
152 | <td> |
||
153 | <?php _e( 'Enter time in Y-m-d H:i:s format or enter now to use current time', 'bulk-delete' );?> |
||
154 | </td> |
||
155 | </tr> |
||
156 | |||
157 | </table> |
||
158 | </fieldset> |
||
159 | |||
160 | <p> |
||
161 | <button type="submit" name="bd_action" value="delete_meta_user" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ) ?>»</button> |
||
162 | </p> |
||
163 | <!-- User Meta box end--> |
||
164 | <?php |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Filter JS Array and add validation hooks. |
||
169 | * |
||
170 | * @since 5.4 |
||
171 | * @static |
||
172 | * |
||
173 | * @param array $js_array JavaScript Array |
||
174 | * |
||
175 | * @return array Modified JavaScript Array |
||
176 | */ |
||
177 | public static function filter_js_array( $js_array ) { |
||
178 | $js_array['dt_iterators'][] = '_um'; |
||
179 | $js_array['validators']['delete_meta_user'] = 'noValidation'; |
||
180 | |||
181 | $js_array['pre_action_msg']['delete_meta_user'] = 'deleteUMWarning'; |
||
182 | $js_array['msg']['deleteUMWarning'] = __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ); |
||
183 | |||
184 | return $js_array; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Controller for deleting user meta fields. |
||
189 | * |
||
190 | * @static |
||
191 | * |
||
192 | * @since 5.4 |
||
193 | */ |
||
194 | public static function do_delete_user_meta() { |
||
195 | $delete_options = array(); |
||
196 | $delete_options['user_role'] = array_get( $_POST, 'smbd_um_role', 'administrator' ); |
||
197 | |||
198 | $delete_options['use_value'] = array_get_bool( $_POST, 'smbd_um_use_value', false ); |
||
199 | $delete_options['meta_key'] = esc_sql( array_get( $_POST, 'smbd_um_key', '' ) ); |
||
200 | |||
201 | $delete_options['limit_to'] = absint( array_get( $_POST, 'smbd_um_limit_to', 0 ) ); |
||
202 | |||
203 | /** |
||
204 | * Delete user-meta delete options filter. |
||
205 | * This filter is for processing filtering options for deleting user meta. |
||
206 | * |
||
207 | * @since 5.4 |
||
208 | */ |
||
209 | $delete_options = apply_filters( 'bd_delete_user_meta_options', $delete_options, $_POST ); |
||
210 | |||
211 | if ( 'true' == array_get( $_POST, 'smbd_um_cron', 'false' ) ) { |
||
212 | $freq = $_POST['smbd_um_cron_freq']; |
||
213 | $time = strtotime( $_POST['smbd_um_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||
214 | |||
215 | if ( $freq == -1 ) { |
||
216 | wp_schedule_single_event( $time, self::CRON_HOOK, array( $delete_options ) ); |
||
217 | } else { |
||
218 | wp_schedule_event( $time, $freq, self::CRON_HOOK, array( $delete_options ) ); |
||
219 | } |
||
220 | $msg = __( 'User meta fields from the users with the selected criteria are scheduled for deletion.', 'bulk-delete' ) . ' ' . |
||
221 | sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG ); |
||
222 | } else { |
||
223 | $deleted_count = self::delete_user_meta( $delete_options ); |
||
224 | $msg = sprintf( _n( 'Deleted user meta field from %d user', 'Deleted user meta field from %d users' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||
225 | } |
||
226 | |||
227 | add_settings_error( |
||
228 | Bulk_Delete_Meta::META_PAGE_SLUG, |
||
229 | 'deleted-users', |
||
230 | $msg, |
||
231 | 'updated' |
||
232 | ); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Delete User Meta. |
||
237 | * |
||
238 | * @static |
||
239 | * |
||
240 | * @since 5.4 |
||
241 | * |
||
242 | * @param array $delete_options Options for deleting |
||
243 | * |
||
244 | * @return int Number of users that were deleted |
||
245 | */ |
||
246 | public static function delete_user_meta( $delete_options ) { |
||
276 | } |
||
277 | } |
||
278 | |||
279 | // hooks |
||
280 | 1 | add_action( 'bd_add_meta_box_for_meta', array( 'Bulk_Delete_User_Meta', 'add_delete_user_meta_box' ) ); |
|
285 |