Passed
Pull Request — dev/6.0.0 (#277)
by Rajan
14:36
created

DeleteCommentMetaModule   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 153
ccs 0
cts 88
cp 0
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A filter_js_array() 0 8 1
A initialize() 0 9 1
A get_success_message() 0 3 1
A convert_user_input_to_options() 0 7 1
A get_cron_name() 0 2 1
B delete() 0 37 6
A render() 0 61 1
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 Comment Meta.
10
 *
11
 * @since 6.0.0
12
 */
13
class DeleteCommentMetaModule extends MetasModule {
14
	protected function initialize() {
15
		$this->field_slug    = 'meta_comment';
16
		$this->meta_box_slug = 'bd-meta-comment';
17
		$this->action        = 'delete_meta_comment';
18
		$this->cron_hook     = 'do-bulk-delete-comment-meta';
19
		$this->messages      = array(
20
			'box_label' => __( 'Bulk Delete Comment Meta', 'bulk-delete' ),
21
			'scheduled' => __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
22
			'cron_name' => __( 'Delete Comment Meta', 'bulk-delete' ),
23
		);
24
	}
25
26
	/**
27
	 * Render the Modules.
28
	 *
29
	 * @return void
30
	 */
31
	public function render() {
32
		?>
33
		<!-- Comment Meta box start-->
34
		<fieldset class="options">
35
			<h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4>
36
			<table class="optiontable">
37
				<?php $this->render_post_type_as_radios(); ?>
38
			</table>
39
40
			<h4><?php _e( 'Choose your comment 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 __( 'Delete based on comment 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" value="true" type="radio" disabled>
52
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on comment meta key name and value', 'bulk-delete' ); ?></label>
53
						<span class="bd-cm-pro" style="color:red; vertical-align: middle;">
54
							<?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-comment-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-c" target="_blank">Buy now</a>
55
						</span>
56
					</td>
57
				</tr>
58
59
				<tr>
60
					<td>
61
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label>
62
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>">
63
					</td>
64
				</tr>
65
			</table>
66
67
			<?php
68
			/**
69
			 * Add more fields to the delete comment meta field form.
70
			 * This hook can be used to add more fields to the delete comment meta field form.
71
			 *
72
			 * @since 5.4
73
			 */
74
			do_action( 'bd_delete_comment_meta_form' );
75
			?>
76
			<table class="optiontable">
77
				<tr>
78
					<td colspan="2">
79
						<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4>
80
					</td>
81
				</tr>
82
83
				<?php $this->render_restrict_settings(); ?>
84
				<?php $this->render_limit_settings(); ?>
85
				<?php $this->render_cron_settings(); ?>
86
87
			</table>
88
		</fieldset>
89
90
		<?php $this->render_submit_button(); ?>
91
92
		<!-- Comment Meta box end-->
93
		<?php
94
	}
95
96
	protected function convert_user_input_to_options( $request, $options ) {
97
		$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) );
98
99
		$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false );
100
		$options['meta_key']  = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) );
101
102
		return $options;
103
	}
104
105
	public function delete( $options ) {
106
		$args = array(
107
			'post_type' => $options['post_type'],
108
		);
109
110
		if ( $options['limit_to'] > 0 ) {
111
			$args['number'] = $options['limit_to'];
112
		}
113
114
		$op   = $options['date_op'];
115
		$days = $options['days'];
116
117
		if ( $options['restrict'] ) {
118
			$args['date_query'] = array(
119
				array(
120
					'column' => 'comment_date',
121
					$op      => "{$days} day ago",
122
				),
123
			);
124
		}
125
126
		if ( $options['use_value'] ) {
127
			$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options );
128
		} else {
129
			$args['meta_key'] = $options['meta_key'];
130
		}
131
132
		$meta_deleted = 0;
133
		$comments     = get_comments( $args );
134
135
		foreach ( $comments as $comment ) {
136
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
137
				$meta_deleted ++;
138
			}
139
		}
140
141
		return $meta_deleted;
142
	}
143
144
	public function filter_js_array( $js_array ) {
145
		$js_array['dt_iterators'][]              = '_' . $this->field_slug;
146
		$js_array['validators'][ $this->action ] = 'noValidation';
147
148
		$js_array['pre_action_msg'][ $this->action ] = 'deleteCMWarning';
149
		$js_array['msg']['deleteCMWarning']          = __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' );
150
151
		return $js_array;
152
	}
153
154
	protected function get_success_message( $items_deleted ) {
155
		/* translators: 1 Number of posts deleted */
156
		return _n( 'Deleted comment meta field from %d comment', 'Deleted comment meta field from %d comments', $items_deleted, 'bulk-delete' );
157
	}
158
159
	/**
160
	 * Schedule job title.
161
	 *
162
	 * @return string humane readable title
163
	 */
164
	protected function get_cron_name(){
165
		return $this->messages['cron_name'];
166
	}
167
}
168