Passed
Push — analysis-8AOO2l ( 192129 )
by Sudar
30:45 queued 15:47
created

DeletePostsByCommentsModule::append_to_js_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
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
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Comments Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByCommentsModule extends PostsModule {
15
	/**
16
	 * Base parameters setup.
17
	 */
18
	protected function initialize() {
19
		$this->item_type     = 'posts';
20
		$this->field_slug    = 'comments';
21
		$this->meta_box_slug = 'bd_by_comments';
22
		$this->action        = 'delete_posts_by_comments';
23
		$this->cron_hook     = 'do-bulk-delete-comments';
24
		$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
		$this->messages      = array(
26
			'box_label'  => __( 'By Comment count', 'bulk-delete' ),
27
			'scheduled'  => __( 'The selected posts are scheduled for deletion', 'bulk-delete' ),
28
			'cron_label' => __( 'Delete Post By Comments', 'bulk-delete' ),
29
		);
30
	}
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
						<?php $this->render_number_comparison_operators(); ?>
48
					</td>
49
					<td>
50
						<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value"
51
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_count_value" placeholder="Comments Count" min="0" class="comments_count_num">
52
					</td>
53
				</tr>
54
			</table>
55
56
			<table class="optiontable">
57
				<?php
58
				$this->render_filtering_table_header();
59
				$this->render_restrict_settings();
60
				$this->render_delete_settings();
61
				$this->render_private_post_settings();
62
				$this->render_limit_settings();
63
				$this->render_cron_settings();
64
				?>
65
			</table>
66
		</fieldset>
67
		<?php
68
		$this->render_submit_button();
69
	}
70
71
	/**
72
	 * Process delete posts, user inputs by comments count.
73
	 *
74
	 * @param array $request Request array.
75
	 * @param array $options Options for deleting posts.
76
	 *
77
	 * @return array $options Inputs from user for posts that were need to be deleted.
78
	 */
79
	protected function convert_user_input_to_options( $request, $options ) {
80
		$options['operator']      = bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' );
81
		$options['comment_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_count_value' ) );
82
83
		return $options;
84
	}
85
86
	/**
87
	 * Build the Query from user input.
88
	 *
89
	 * @param array $options User Input.
90
	 *
91
	 * @return array $query Query Params.
92
	 */
93
	protected function build_query( $options ) {
94
		$query = array();
95
96
		$query['comment_count'] = array(
97
			'compare' => $options['operator'],
98
			'value'   => $options['comment_count'],
99
		);
100
101
		return $query;
102
	}
103
104
	protected function append_to_js_array( $js_array ) {
105
		$js_array['validators'][ $this->action ] = 'validateCommentsCount';
106
		$js_array['error_msg'][ $this->action ]  = 'validCommentsCount';
107
		$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' );
108
109
		$js_array['pre_action_msg'][ $this->action ] = 'deletePostsWarning';
110
		$js_array['msg']['deletePostsWarning']       = __( 'Are you sure you want to delete all the posts based on the selected comment count?', 'bulk-delete' );
111
112
		return $js_array;
113
	}
114
115
	/**
116
	 * Response message for deleting posts.
117
	 *
118
	 * @param int $items_deleted count of items deleted.
119
	 *
120
	 * @return string Response message
121
	 */
122
	protected function get_success_message( $items_deleted ) {
123
		/* translators: 1 Number of posts deleted */
124
		return _n( 'Deleted %d post with the selected comments count', 'Deleted %d posts with the selected comments count', $items_deleted, 'bulk-delete' );
125
	}
126
}
127