Completed
Pull Request — dev/6.0.0 (#546)
by Sudar
57:11 queued 48:06
created

DeletePostsByCommentsModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 11
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Posts\Modules;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsModule;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Comments Module.
11
 *
12
 * @since 6.1.0
13
 */
14
class DeletePostsByCommentsModule extends PostsModule {
15
	/**
16
	 * Base parameters setup.
17
	 */
18 8
	protected function initialize() {
19 8
		$this->item_type     = 'posts';
20 8
		$this->field_slug    = 'comments';
21 8
		$this->meta_box_slug = 'bd_by_comments';
22 8
		$this->action        = 'delete_posts_by_comments';
23 8
		$this->cron_hook     = 'do-bulk-delete-comments';
24 8
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-comments/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-st';
25 8
		$this->messages      = array(
26 8
			'box_label'  => __( 'By Post Comments', 'bulk-delete' ),
27 8
			'scheduled'  => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
28 8
			'cron_label' => __( 'Delete Post By Comments', 'bulk-delete' ),
29
		);
30 8
	}
31
32
	/**
33
	 * Render Delete posts by comments box.
34
	 */
35
	public function render() {
36
		?>
37
		<h4><?php _e( 'Delete Posts based on the number of comments', 'bulk-delete' ); ?></h4>
38
39
		<!-- Comments start-->
40
		<fieldset class="options">
41
			<table class="optiontable">
42
				<tr>
43
					<td scope="row" colspan="2">
44
						<?php _e( 'Delete posts that have comments', 'bulk-delete' ); ?>
45
					</td>
46
					<td>
47
						<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_op">
48
							<option value="="><?php _e( 'equal to', 'bulk-delete' ); ?></option>
49
							<option value="!="><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
50
							<option value="<"><?php _e( 'less than', 'bulk-delete' ); ?></option>
51
							<option value=">"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
52
						</select>
53
					</td>
54
					<td>
55
						<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value"
56
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value" placeholder="Comments Count" min="0" class="comments_count_num">
57
					</td>
58
				</tr>
59
			</table>
60
61
			<table class="optiontable">
62
				<?php
63
				$this->render_filtering_table_header();
64
				$this->render_restrict_settings();
65
				$this->render_delete_settings();
66
				$this->render_private_post_settings();
67
				$this->render_limit_settings();
68
				$this->render_cron_settings();
69
				?>
70
			</table>
71
		</fieldset>
72
		<?php
73
		$this->render_submit_button();
74
	}
75
76
	/**
77
	 * Process delete posts, user inputs by comments count.
78
	 *
79
	 * @param array $request Request array.
80
	 * @param array $options Options for deleting posts.
81
	 *
82
	 * @return array $options Inputs from user for posts that were need to be deleted.
83
	 */
84
	protected function convert_user_input_to_options( $request, $options ) {
85
		$options['operator']      = bd_array_get( $request, 'smbd_' . $this->field_slug . '_count_op' );
86
		$options['comment_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_count_value' ) );
87
88
		return $options;
89
	}
90
91
	/**
92
	 * Build the Query from user input.
93
	 *
94
	 * @param array $options User Input.
95
	 *
96
	 * @return array $query Query Params.
97
	 */
98 8
	protected function build_query( $options ) {
99 8
		$query = array();
100
101 8
		$query['comment_count'] = array(
102 8
			'compare' => $options['operator'],
103 8
			'value'   => $options['comment_count'],
104
		);
105
106 8
		return $query;
107
	}
108
109
	/**
110
	 * Filter JS Array and add validation hooks.
111
	 *
112
	 * @param array $js_array JavaScript Array.
113
	 *
114
	 * @return array Modified JavaScript Array.
115
	 */
116
	public function filter_js_array( $js_array ) {
117
		$js_array['validators'][ $this->action ] = 'validateCommentsCount';
118
		$js_array['error_msg'][ $this->action ]  = 'validCommentsCount';
119
		$js_array['msg']['validCommentsCount']   = __( 'Please enter the comments count based on which posts should be deleted. A valid comment count will be greater than or equal to zero', 'bulk-delete' );
120
121
		$js_array['pre_action_msg'][ $this->action ] = 'deletePostsWarning';
122
		$js_array['msg']['deletePostsWarning']       = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
123
124
		return $js_array;
125
	}
126
127
	/**
128
	 * Response message for deleting posts.
129
	 *
130
	 * @param int $items_deleted count of items deleted.
131
	 *
132
	 * @return string Response message
133
	 */
134
	protected function get_success_message( $items_deleted ) {
135
		/* translators: 1 Number of posts deleted */
136
		return _n( 'Deleted %d post with the selected comments count', 'Deleted %d posts with the selected comments count', $items_deleted, 'bulk-delete' );
137
	}
138
}
139