Completed
Push — 233-feature/posts-by-post-stat... ( d2a551...3016f6 )
by Sudar
11:19 queued 07:05
created

DeleteCommentMetaModule::delete()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 24
nop 1
dl 0
loc 37
ccs 0
cts 30
cp 0
crap 42
rs 8.439
c 0
b 0
f 0
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
		);
23
	}
24
25
	/**
26
	 * Render the Modules.
27
	 *
28
	 * @return void
29
	 */
30
	public function render() {
31
		?>
32
		<!-- Comment Meta box start-->
33
		<fieldset class="options">
34
			<h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4>
35
			<table class="optiontable">
36
				<?php $this->render_post_type_as_radios(); ?>
37
			</table>
38
39
			<h4><?php _e( 'Choose your comment meta field settings', 'bulk-delete' ); ?></h4>
40
			<table class="optiontable">
41
				<tr>
42
					<td>
43
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked>
44
						<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>
45
					</td>
46
				</tr>
47
48
				<tr>
49
					<td>
50
						<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>
51
						<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>
52
						<span class="bd-cm-pro" style="color:red; vertical-align: middle;">
53
							<?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>
54
						</span>
55
					</td>
56
				</tr>
57
58
				<tr>
59
					<td>
60
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label>
61
						<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' ); ?>">
62
					</td>
63
				</tr>
64
			</table>
65
66
			<?php
67
			/**
68
			 * Add more fields to the delete comment meta field form.
69
			 * This hook can be used to add more fields to the delete comment meta field form.
70
			 *
71
			 * @since 5.4
72
			 */
73
			do_action( 'bd_delete_comment_meta_form' );
74
			?>
75
			<table class="optiontable">
76
				<tr>
77
					<td colspan="2">
78
						<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4>
79
					</td>
80
				</tr>
81
82
				<?php $this->render_restrict_settings(); ?>
83
				<?php $this->render_limit_settings(); ?>
84
				<?php $this->render_cron_settings(); ?>
85
86
			</table>
87
		</fieldset>
88
89
		<?php $this->render_submit_button(); ?>
90
91
		<!-- Comment Meta box end-->
92
		<?php
93
	}
94
95
	protected function convert_user_input_to_options( $request, $options ) {
96
		$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) );
97
98
		$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false );
99
		$options['meta_key']  = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) );
100
101
		return $options;
102
	}
103
104
	public function delete( $options ) {
105
		$args = array(
106
			'post_type' => $options['post_type'],
107
		);
108
109
		if ( $options['limit_to'] > 0 ) {
110
			$args['number'] = $options['limit_to'];
111
		}
112
113
		$op   = $options['date_op'];
114
		$days = $options['days'];
115
116
		if ( $options['restrict'] ) {
117
			$args['date_query'] = array(
118
				array(
119
					'column' => 'comment_date',
120
					$op      => "{$days} day ago",
121
				),
122
			);
123
		}
124
125
		if ( $options['use_value'] ) {
126
			$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options );
127
		} else {
128
			$args['meta_key'] = $options['meta_key'];
129
		}
130
131
		$meta_deleted = 0;
132
		$comments     = get_comments( $args );
133
134
		foreach ( $comments as $comment ) {
135
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
136
				$meta_deleted ++;
137
			}
138
		}
139
140
		return $meta_deleted;
141
	}
142
143
	public function filter_js_array( $js_array ) {
144
		$js_array['dt_iterators'][]              = '_' . $this->field_slug;
145
		$js_array['validators'][ $this->action ] = 'noValidation';
146
147
		$js_array['pre_action_msg'][ $this->action ] = 'deleteCMWarning';
148
		$js_array['msg']['deleteCMWarning']          = __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' );
149
150
		return $js_array;
151
	}
152
153
	protected function get_success_message( $items_deleted ) {
154
		/* translators: 1 Number of posts deleted */
155
		return _n( 'Deleted comment meta field from %d comment', 'Deleted comment meta field from %d comments', $items_deleted, 'bulk-delete' );
156
	}
157
}
158