| 1 |
<?php |
| 2 |
namespace BulkWP\BulkDelete\Core\Metas\Modules; |
| 3 |
|
| 4 |
use BulkWP\BulkDelete\Core\Metas\MetasModule; |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 7 |
|
| 8 |
/** |
| 9 |
* Delete User Meta. |
| 10 |
* |
| 11 |
* @since 6.0.0 |
| 12 |
*/ |
| 13 |
class DeleteUserMetaModule extends MetasModule { |
| 14 |
protected function initialize() { |
| 15 |
$this->field_slug = 'um'; // Ideally it should be `meta_user`. But we are keeping it as um for backward compatibility. |
| 16 |
$this->meta_box_slug = 'bd-user-meta'; |
| 17 |
$this->action = 'delete_user_meta'; |
| 18 |
$this->cron_hook = 'do-bulk-delete-user-meta'; |
| 19 |
$this->scheduler_url = 1; |
| 20 |
$this->messages = array( |
| 21 |
'box_label' => __( 'Bulk Delete User Meta', 'bulk-delete' ), |
| 22 |
'scheduled' => __( 'User meta fields from the users with the selected criteria are scheduled for deletion.', 'bulk-delete' ), |
| 23 |
'cron_label' => __( 'Delete User Meta`', 'bulk-delete' ), |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Render the Modules. |
| 29 |
*/ |
| 30 |
public function render() { |
| 31 |
?> |
| 32 |
<!-- User Meta box start--> |
| 33 |
<fieldset class="options"> |
| 34 |
<h4><?php esc_html_e( 'Select the user role whose user meta fields you want to delete', 'bulk-delete' ); ?></h4> |
| 35 |
|
| 36 |
<table class="optiontable"> |
| 37 |
<?php $this->render_user_role_dropdown(); ?> |
| 38 |
</table> |
| 39 |
|
| 40 |
<h4><?php esc_html_e( 'Choose your user meta field settings', 'bulk-delete' ); ?></h4> |
| 41 |
<table class="optiontable"> |
| 42 |
<tr> |
| 43 |
<td> |
| 44 |
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked> |
| 45 |
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo esc_html__( 'Delete based on user meta key name only', 'bulk-delete' ); ?></label> |
| 46 |
</td> |
| 47 |
</tr> |
| 48 |
|
| 49 |
<tr> |
| 50 |
<td> |
| 51 |
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" |
| 52 |
value="true" type="radio" disabled> |
| 53 |
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo esc_html__( 'Delete based on user meta key name and value', 'bulk-delete' ); ?></label> |
| 54 |
<span class="bd-um-pro" style="color:red; vertical-align: middle;"> |
| 55 |
<span class="open-upsell pro-feature-inline" data-pro-feature="delete-post-meta">Available in PRO</span> |
| 56 |
</span> |
| 57 |
</td> |
| 58 |
</tr> |
| 59 |
|
| 60 |
<tr> |
| 61 |
<td> |
| 62 |
<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php esc_html_e( 'User Meta Key ', 'bulk-delete' ); ?></label> |
| 63 |
<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" placeholder="<?php esc_html_e( 'Meta Key', 'bulk-delete' ); ?>"> |
| 64 |
</td> |
| 65 |
</tr> |
| 66 |
</table> |
| 67 |
<?php |
| 68 |
/** |
| 69 |
* Add more fields to the delete user meta field form. |
| 70 |
* This hook can be used to add more fields to the delete user meta field form. |
| 71 |
* |
| 72 |
* @since 5.4 |
| 73 |
*/ |
| 74 |
do_action( 'bd_delete_user_meta_form' ); //phpcs:ignore |
| 75 |
?> |
| 76 |
<table class="optiontable"> |
| 77 |
<tr> |
| 78 |
<td colspan="2"> |
| 79 |
<h4><?php esc_html_e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
| 80 |
</td> |
| 81 |
</tr> |
| 82 |
|
| 83 |
<?php $this->render_limit_settings(); ?> |
| 84 |
<?php $this->render_cron_settings(); ?> |
| 85 |
</table> |
| 86 |
</fieldset> |
| 87 |
|
| 88 |
<?php $this->render_submit_button(); ?> |
| 89 |
<!-- User Meta box end--> |
| 90 |
<?php |
| 91 |
} |
| 92 |
|
| 93 |
protected function convert_user_input_to_options( $request, $options ) { |
| 94 |
$options['selected_roles'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_roles' ) ); |
| 95 |
$options['use_value'] = sanitize_text_field( bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false ) ); |
| 96 |
$options['meta_key'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) ); |
| 97 |
$options['meta_value'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value', '' ) ); |
| 98 |
|
| 99 |
/** |
| 100 |
* Delete user-meta delete options filter. |
| 101 |
* |
| 102 |
* This filter is for processing filtering options for deleting user meta. |
| 103 |
* |
| 104 |
* @since 5.4 |
| 105 |
*/ |
| 106 |
return apply_filters( 'bd_delete_user_meta_options', $options, $request ); //phpcs:ignore |
| 107 |
} |
| 108 |
|
| 109 |
protected function do_delete( $options ) { |
| 110 |
$count = 0; |
| 111 |
$meta_key = $options['meta_key']; |
| 112 |
$use_value = $options['use_value']; |
| 113 |
$limit_to = $options['limit_to']; |
| 114 |
|
| 115 |
$args = array( |
| 116 |
'role__in' => $options['selected_roles'], |
| 117 |
); |
| 118 |
|
| 119 |
if ( $limit_to > 0 ) { |
| 120 |
$args['number'] = $limit_to; |
| 121 |
} |
| 122 |
|
| 123 |
if ( $use_value ) { |
| 124 |
$args['meta_query'] = apply_filters( 'bd_delete_user_meta_query', array(), $options ); //phpcs:ignore |
| 125 |
} else { |
| 126 |
$args['meta_key'] = $meta_key; |
| 127 |
} |
| 128 |
|
| 129 |
$users = get_users( $args ); |
| 130 |
|
| 131 |
foreach ( $users as $user ) { |
| 132 |
if ( $use_value ) { |
| 133 |
if ( delete_user_meta( $user->ID, $meta_key, $options['meta_value'] ) ) { |
| 134 |
$count++; |
| 135 |
} |
| 136 |
} else { |
| 137 |
if ( delete_user_meta( $user->ID, $meta_key ) ) { |
| 138 |
$count++; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return $count; |
| 144 |
} |
| 145 |
|
| 146 |
protected function append_to_js_array( $js_array ) { |
| 147 |
$js_array['validators']['delete_user_meta'] = 'noValidation'; |
| 148 |
|
| 149 |
$js_array['pre_action_msg']['delete_user_meta'] = 'deleteUMWarning'; |
| 150 |
$js_array['msg']['deleteUMWarning'] = __( 'Are you sure you want to delete all the user meta fields that match the selected filters?', 'bulk-delete' ); |
| 151 |
|
| 152 |
return $js_array; |
| 153 |
} |
| 154 |
|
| 155 |
protected function get_success_message( $items_deleted ) { |
| 156 |
/* translators: 1 Number of posts deleted */ |
| 157 |
return _n( 'Deleted user meta field from %d user', 'Deleted user meta field from %d users', $items_deleted, 'bulk-delete' ); |
| 158 |
} |
| 159 |
} |
| 160 |
|