Completed
Pull Request — 330-fix/delete-user-meta-add-a... (#524)
by Sudar
26:21 queued 11:22
created

get_unique_meta_keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A DeleteUsersByUserMetaModule::get_success_message() 0 3 1
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Users\Modules;
4
5
use BulkWP\BulkDelete\Core\Users\UsersModule;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Bulk Delete Users by User Meta.
11
 *
12
 * @since 5.5
13
 * @since 6.0.0 Renamed to DeleteUsersByUserMetaModule.
14
 */
15
class DeleteUsersByUserMetaModule extends UsersModule {
16
	/**
17
	 * Initialize and setup variables.
18
	 *
19
	 * @since 5.5
20
	 */
21 40
	protected function initialize() {
22 40
		$this->item_type     = 'users';
23 40
		$this->field_slug    = 'u_meta';
24 40
		$this->meta_box_slug = 'bd_users_by_meta';
25 40
		$this->action        = 'delete_users_by_meta';
26 40
		$this->cron_hook     = 'do-bulk-delete-users-by-meta';
27 40
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-users-by-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ma';
28 40
		$this->messages      = array(
29 40
			'box_label'  => __( 'By User Meta', 'bulk-delete' ),
30 40
			'scheduled'  => __( 'Users from with the selected user meta are scheduled for deletion.', 'bulk-delete' ),
31 40
			'cron_label' => __( 'Delete Users by User Meta', 'bulk-delete' ),
32
		);
33 40
	}
34
35
	/**
36
	 * Render delete users box.
37
	 *
38
	 * @since 5.5
39
	 */
40
	public function render() {
41
?>
42
		<!-- Users Start-->
43
		<h4><?php _e( 'Select the user meta from which you want to delete users', 'bulk-delete' ); ?></h4>
44
45
		<fieldset class="options">
46
			<table class="optiontable">
47
				<select name="smbd_u_meta_key" class="enhanced-dropdown">
48
					<?php
49
					$meta_keys = $this->get_unique_user_meta_keys();
50
					foreach ( $meta_keys as $meta_key ) {
51
						printf( '<option value="%s">%s</option>', esc_attr( $meta_key ), esc_html( $meta_key ) );
52
					}
53
					?>
54
				</select>
55
56
				<select name="smbd_u_meta_compare">
57
					<option value="=">Equals to</option>
58
					<option value="!=">Not Equals to</option>
59
					<option value=">">Greater than</option>
60
					<option value=">=">Greater than or equals to</option>
61
					<option value="<">Less than</option>
62
					<option value="<=">Less than or equals to</option>
63
					<option value="LIKE">Contains</option>
64
					<option value="NOT LIKE">Not Contains</option>
65
					<option value="STARTS WITH">Starts with</option>
66
					<option value="ENDS WITH">Ends with</option>
67
				</select>
68
				<input type="text" name="smbd_u_meta_value" id="smbd_u_meta_value" placeholder="<?php _e( 'Meta Value', 'bulk-delete' ); ?>">
69
70
			</table>
71
72
			<p>
73
				<?php _e( 'If you want to check for null values, then leave the value column blank', 'bulk-delete' ); ?>
74
			</p>
75
76
			<table class="optiontable">
77
				<?php
78
				$this->render_filtering_table_header();
79
				$this->render_user_login_restrict_settings();
80
				$this->render_user_with_no_posts_settings();
81
				$this->render_limit_settings();
82
				$this->render_cron_settings();
83
				?>
84
			</table>
85
		</fieldset>
86
		<!-- Users end-->
87
88
		<?php
89
		$this->render_submit_button();
90
	}
91
92
	/**
93
	 * Process user input and create metabox options.
94
	 *
95
	 * @param array $request Request array.
96
	 * @param array $options User options.
97
	 *
98
	 * @return array User options.
99
	 */
100
	protected function convert_user_input_to_options( $request, $options ) {
101
		$options['meta_key']     = bd_array_get( $request, 'smbd_u_meta_key' );
102
		$options['meta_compare'] = bd_array_get( $request, 'smbd_u_meta_compare', '=' );
103
		$options['meta_value']   = bd_array_get( $request, 'smbd_u_meta_value' );
104
105
		switch ( strtolower( trim( $options['meta_compare'] ) ) ) {
106
			case 'starts with':
107
				$options['meta_compare'] = 'REGEXP';
108
				$options['meta_value']   = '^' . $options['meta_value'];
109
				break;
110
			case 'ends with':
111
				$options['meta_compare'] = 'REGEXP';
112
				$options['meta_value']   = $options['meta_value'] . '$';
113
				break;
114
		}
115
116
		return $options;
117 40
	}
118
119
	protected function build_query( $options ) {
120
		$query = array(
121 40
			'meta_query' => array(
122 40
				array(
123 40
					'key'     => $options['meta_key'],
124
					'value'   => $options['meta_value'],
125
					'compare' => $options['meta_compare'],
126
				),
127
			),
128 40
		);
129 10
130
		if ( $options['limit_to'] > 0 ) {
131
			$query['number'] = $options['limit_to'];
132 40
		}
133
134
		$date_query = $this->get_date_query( $options );
135
136
		if ( ! empty( $date_query ) ) {
137
			$query['date_query'] = $date_query;
138
		}
139
140
		return $query;
141
	}
142
143
	/**
144
	 * Filter JS Array and add validation hooks.
145
	 *
146
	 * @since 5.5
147
	 *
148
	 * @param array $js_array JavaScript Array.
149
	 *
150
	 * @return array Modified JavaScript Array
151
	 */
152
	public function filter_js_array( $js_array ) {
153
		$js_array['dt_iterators'][]              = '_' . $this->field_slug;
154
		$js_array['validators'][ $this->action ] = 'validateUserMeta';
155
156
		$js_array['pre_action_msg'][ $this->action ] = 'deleteUsersByMetaWarning';
157
		$js_array['msg']['deleteUsersByMetaWarning'] = __( 'Are you sure you want to delete all the users from the selected user meta?', 'bulk-delete' );
158
159
		$js_array['error_msg'][ $this->action ] = 'enterUserMetaValue';
160
		$js_array['msg']['enterUserMetaValue']  = __( 'Please enter the value for the user meta field based on which you want to delete users', 'bulk-delete' );
161
162
		return $js_array;
163
	}
164
165
	protected function get_success_message( $items_deleted ) {
166
		/* translators: 1 Number of users deleted */
167
		return _n( 'Deleted %d user with the selected user meta', 'Deleted %d users with the selected user meta', $items_deleted, 'bulk-delete' );
168
	}
169
}
170