Completed
Pull Request — dev/6.0.1 (#616)
by
unknown
97:02 queued 71:20
created

DeletePostMetaModule::do_delete()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 18.744

Importance

Changes 0
Metric Value
cc 11
eloc 34
c 0
b 0
f 0
nc 32
nop 1
dl 0
loc 53
ccs 18
cts 30
cp 0.6
crap 18.744
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace BulkWP\BulkDelete\Core\Metas\Modules;
3
4
use BulkWP\BulkDelete\Core\Metas\MetasModule;
5
6 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Delete Post Meta.
10
 *
11
 * @since 6.0.0
12
 */
13
class DeletePostMetaModule extends MetasModule {
14 2
	protected function initialize() {
15 2
		$this->field_slug    = 'pm'; // Ideally it should be `meta_post`. But we are keeping it as pm for backward compatibility.
16 2
		$this->meta_box_slug = 'bd-post-meta';
17 2
		$this->action        = 'delete_post_meta';
18 2
		$this->cron_hook     = 'do-bulk-delete-post-meta';
19 2
		$this->scheduler_url = 'http://bulkwp.com/addons/bulk-delete-post-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-p';
20 2
		$this->messages      = array(
21 2
			'box_label'  => __( 'Bulk Delete Post Meta', 'bulk-delete' ),
22 2
			'scheduled'  => __( 'Post meta fields from the posts with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
23 2
			'cron_label' => __( 'Delete Post Meta', 'bulk-delete' ),
24
		);
25 2
	}
26
27
	/**
28
	 * Render the Modules.
29
	 *
30
	 * @return void
31
	 */
32
	public function render() {
33
		?>
34
		<!-- Post Meta box start-->
35
		<fieldset class="options">
36
			<h4><?php _e( 'Select the post type whose post meta fields you want to delete', 'bulk-delete' ); ?></h4>
37
38
			<table class="optiontable">
39
				<?php $this->render_post_type_with_status( false ); ?>
40
			</table>
41
42
			<h4><?php _e( 'Choose your post meta field settings', 'bulk-delete' ); ?></h4>
43
			<table class="optiontable">
44
				<tr>
45
					<td>
46
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="use_key" type="radio" checked>
47
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on post meta key name only', 'bulk-delete' ); ?></label>
48
					</td>
49
				</tr>
50
51
				<tr>
52
					<td>
53
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smdb_<?php echo esc_attr( $this->field_slug ); ?>_use_key_compare" value="use_key_compare" type="radio" disabled>
54
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on post meta key name prefix or postfix', 'bulk-delete' ); ?></label>
55
						<span class="bd-pm-pro" style="color:red; vertical-align: middle;">
56
							<?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>
57
						</span>
58
					</td>
59
				</tr>
60
61
				<tr>
62
					<td>
63
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="use_value" type="radio" disabled>
64
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on post meta key name and value', 'bulk-delete' ); ?></label>
65
						<span class="bd-pm-pro" style="color:red; vertical-align: middle;">
66
							<?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>
67
						</span>
68
					</td>
69
				</tr>
70
71
				<tr>
72
					<td>
73
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key"><?php _e( 'Post Meta Key ', 'bulk-delete' ); ?></label>
74
						<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key_prefix_postfix" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key_prefix_postfix" style="display: none;">
75
							<option value="starts_with">starts with</option>
76
							<option value="contains">contains</option>
77
							<option value="ends_with">ends with</option>
78
						</select>
79
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>">
80
					</td>
81
				</tr>
82
			</table>
83
84
		<?php
85
			/**
86
			 * Add more fields to the delete post meta field form.
87
			 * This hook can be used to add more fields to the delete post meta field form.
88
			 *
89
			 * @since 5.4
90
			 */
91
			do_action( 'bd_delete_post_meta_form' );
92
		?>
93
94
			<table class="optiontable">
95
				<tr>
96
					<td colspan="2">
97
						<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4>
98
					</td>
99
				</tr>
100
101
				<?php $this->render_restrict_settings(); ?>
102
				<?php $this->render_limit_settings(); ?>
103
				<?php $this->render_cron_settings(); ?>
104
105
			</table>
106
		</fieldset>
107
108
		<?php $this->render_submit_button(); ?>
109
110
		<!-- Post Meta box end-->
111
		<?php
112
	}
113
114
	protected function convert_user_input_to_options( $request, $options ) {
115
		$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug ) );
116
117
		$options['use_value'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_use_value', 'use_key' );
118
		$options['meta_key']  = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) );
119
120
		/**
121
		 * Delete post-meta delete options filter.
122
		 *
123
		 * This filter is for processing filtering options for deleting post meta.
124
		 *
125
		 * @since 5.4
126
		 */
127
		return apply_filters( 'bd_delete_post_meta_options', $options, $request );
128
	}
129
130 2
	protected function do_delete( $options ) {
131 2
		$count       = 0;
132 2
		$type_status = $this->split_post_type_and_status( $options['post_type'] );
133
		$args        = array(
134 2
			'post_type'   => $type_status['type'],
135 2
			'post_status' => $type_status['status'],
136
		);
137
138 2
		if ( $options['limit_to'] > 0 ) {
139
			$args['number'] = $options['limit_to'];
140
		} else {
141 2
			$args['nopaging'] = 'true';
142
		}
143
144 2
		$op   = $options['date_op'];
145 2
		$days = $options['days'];
146
147 2
		if ( $options['restrict'] ) {
148
			$args['date_query'] = array(
149
				array(
150
					'column' => 'post_date',
151
					$op      => "{$days} day ago",
152
				),
153
			);
154
		}
155
156 2
		if ( 'use_key' === $options['use_value'] ) {
157 2
			$options['meta_key'] = $options['meta_key'];
158
		} else {
159
			$options['meta_query'] = apply_filters( 'bd_delete_post_meta_query', array(), $options );
160
		}
161
162 2
		$post_ids = bd_query( $args );
163 2
		foreach ( $post_ids as $post_id ) {
164 2
			if ( isset( $options['meta_key'] ) && is_array( $options['meta_key'] ) ) {
165
				$is_post_id_counted = false;
166
				foreach ( $options['meta_key'] as $meta_key ) {
167
					if ( delete_post_meta( $post_id, $meta_key ) ) {
168
						if ( $is_post_id_counted ) {
169
							continue;
170
						}
171
						$count++;
172
						$is_post_id_counted = true;
173
					}
174
				}
175
			} else {
176 2
				if ( delete_post_meta( $post_id, $options['meta_key'] ) ) {
177 2
					$count++;
178
				}
179
			}
180
		}
181
182 2
		return $count;
183
	}
184
185
	protected function append_to_js_array( $js_array ) {
186
		$js_array['validators'][ $this->action ] = 'noValidation';
187
188
		$js_array['pre_action_msg'][ $this->action ] = 'deletePMWarning';
189
		$js_array['msg']['deletePMWarning']          = __( 'Are you sure you want to delete all the post meta fields that match the selected filters?', 'bulk-delete' );
190
191
		return $js_array;
192
	}
193
194
	protected function get_success_message( $items_deleted ) {
195
		/* translators: 1 Number of posts deleted */
196
		return _n( 'Deleted post meta field from %d post', 'Deleted post meta field from %d posts', $items_deleted, 'bulk-delete' );
197
	}
198
}
199