Completed
Pull Request — dev/5.7.0 (#182)
by Maria Daniel Deepak
02:23 queued 47s
created

Bulk_Delete_Users_By_User_Meta::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 47
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 23
nc 2
nop 0
dl 0
loc 47
rs 9.0303
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bulk Delete Users by User Meta.
4
 *
5
 * @since   5.5
6
 *
7
 * @author  Sudar
8
 *
9
 * @package BulkDelete\Users\Modules
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
/**
14
 * Bulk Delete Users by User Meta.
15
 *
16
 * @since 5.5
17
 */
18
class Bulk_Delete_Users_By_User_Meta extends BD_User_Meta_Box_Module {
19
	/**
20
	 * Make this class a "hybrid Singleton".
21
	 *
22
	 * @static
23
	 *
24
	 * @since 5.5
25
	 */
26
	public static function factory() {
27
		static $instance = false;
28
29
		if ( ! $instance ) {
30
			$instance = new self;
31
		}
32
33
		return $instance;
34
	}
35
36
	/**
37
	 * Initialize and setup variables.
38
	 *
39
	 * @since 5.5
40
	 */
41
	protected function initialize() {
42
		$this->item_type     = 'users';
43
		$this->field_slug    = 'u_meta';
44
		$this->meta_box_slug = 'bd_users_by_meta';
45
		$this->meta_box_hook = "bd_add_meta_box_for_{$this->item_type}";
46
		$this->delete_action = 'delete_users_by_meta';
47
		$this->cron_hook     = 'do-bulk-delete-users-by-meta';
48
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-users-by-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ma';
49
		$this->messages      = array(
50
			'box_label'      => __( 'By User Meta', 'bulk-delete' ),
51
			'scheduled'      => __( 'Users from with the selected user meta are scheduled for deletion.', 'bulk-delete' ),
52
			'deleted_single' => __( 'Deleted %d user with the selected user meta', 'bulk-delete' ),
53
			'deleted_plural' => __( 'Deleted %d users with the selected user meta', 'bulk-delete' ),
54
		);
55
	}
56
57
	/**
58
	 * Render delete users box.
59
	 *
60
	 * @since 5.5
61
	 */
62
	public function render() {
63
?>
64
        <!-- Users Start-->
65
        <h4><?php _e( 'Select the user meta from which you want to delete users', 'bulk-delete' ); ?></h4>
66
67
        <fieldset class="options">
68
        <table class="optiontable">
69
		<select name="smbd_u_meta_key" class="select2">
70
<?php
71
		$meta_keys = $this->get_unique_meta_keys();
72
		foreach ( $meta_keys as $meta_key ) {
73
			printf( '<option value="%s">%s</option>', $meta_key, $meta_key );
74
		}
75
?>
76
		</select>
77
		<select name="smbd_u_meta_compare">
78
			<option value="=">Equals to</option>
79
			<option value="!=">Not Equals to</option>
80
			<option value=">">Greater than</option>
81
			<option value=">=">Greater than or equals to</option>
82
			<option value="<">Less than</option>
83
			<option value="<=">Less than or equals to</option>
84
			<option value="LIKE">Like</option>
85
			<option value="NOT LIKE">Not Like</option>
86
		</select>
87
		<input type="text" name="smbd_u_meta_value" id="smbd_u_meta_value" placeholder="<?php _e( 'Meta Value', 'bulk-delete' );?>">
88
89
		</table>
90
91
		<p>
92
			<?php _e( 'If you want to check for null values, then leave the value column blank', 'bulk-delete' ); ?>
93
		</p>
94
95
        <table class="optiontable">
96
<?php
97
		$this->render_filtering_table_header();
98
		$this->render_user_login_restrict_settings();
99
		$this->render_user_with_no_posts_settings();
100
		$this->render_limit_settings();
101
		$this->render_cron_settings();
102
?>
103
        </table>
104
        </fieldset>
105
        <!-- Users end-->
106
<?php
107
		$this->render_submit_button();
108
	}
109
110
	/**
111
	 * Process the request for deleting users by meta.
112
	 *
113
	 * @since 5.5
114
	 */
115
	public function process() {
116
		$delete_options                   = array();
117
		$delete_options['meta_key']       = array_get( $_POST, 'smbd_u_meta_key' );
118
		$delete_options['meta_compare']   = array_get( $_POST, 'smbd_u_meta_compare', '=' );
119
		$delete_options['meta_value']     = array_get( $_POST, 'smbd_u_meta_value' );
120
121
		$this->process_user_delete( $delete_options );
122
	}
123
124
	/**
125
	 * Delete users by user meta.
126
	 *
127
	 * @since 5.5
128
	 *
129
	 * @param array $delete_options Delete Options
130
	 *
131
	 * @return int Number of users deleted
132
	 */
133
	public function delete( $delete_options ) {
134
		if ( ! function_exists( 'wp_delete_user' ) ) {
135
			require_once ABSPATH . 'wp-admin/includes/user.php';
136
		}
137
138
		$options = array(
139
			'meta_query' => array(
140
				array(
141
					'key'     => $delete_options['meta_key'],
142
					'value'   => $delete_options['meta_value'],
143
					'compare' => $delete_options['meta_compare'],
144
				),
145
			),
146
		);
147
148
		if ( $delete_options['limit_to'] > 0 ) {
149
			$options['number'] = $delete_options['limit_to'];
150
		}
151
152
		return $this->delete_users( $options, $delete_options );
153
	}
154
155
	/**
156
	 * Filter JS Array and add validation hooks.
157
	 *
158
	 * @since 5.5
159
	 *
160
	 * @param array $js_array JavaScript Array
161
	 *
162
	 * @return array Modified JavaScript Array
163
	 */
164
	public function filter_js_array( $js_array ) {
165
		$js_array['dt_iterators'][]                     = '_' . $this->field_slug;
166
		$js_array['validators'][ $this->delete_action ] = 'noValidation';
167
168
		$js_array['pre_action_msg'][ $this->delete_action ] = 'deleteUsersByMetaWarning';
169
		$js_array['msg']['deleteUsersByMetaWarning']        = __( 'Are you sure you want to delete all the users from the selected user meta?', 'bulk-delete' );
170
171
        $js_array['error_msg'][ $this->delete_action ] = 'enterUserMetaValue';
172
        $js_array['msg']['enterUserMetaValue']         = __( 'Please enter the value for the user meta field based on which you want to delete users', 'bulk-delete' );
173
174
		return $js_array;
175
	}
176
177
	/**
178
	 * Get unique user meta keys.
179
	 *
180
	 * @since 5.5
181
	 *
182
	 * @return array List of unique meta keys.
183
	 */
184
	private function get_unique_meta_keys() {
185
		global $wpdb;
186
187
		return $wpdb->get_col( "SELECT DISTINCT(meta_key) FROM {$wpdb->prefix}usermeta ORDER BY meta_key" );
188
	}
189
}
190
191
Bulk_Delete_Users_By_User_Meta::factory();
192
?>
193