Passed
Push — 677-feature/add-wp-cli-support ( 2c69cc...edf7f9 )
by
unknown
02:58
created

DeletePostsByStickyPostModule::prepare_cli_input()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
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 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Sticky Post.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByStickyPostModule extends PostsModule {
15
	/**
16
	 * Did the user requested for unsticking posts instead of deleting them?
17
	 *
18
	 * @var bool
19
	 */
20
	protected $did_unsticky_post_instead_of_delete = false;
21
22
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
23 6
	protected function initialize() {
24 6
		$this->item_type     = 'posts';
25 6
		$this->field_slug    = 'sticky_post';
26 6
		$this->meta_box_slug = 'delete_posts_by_sticky_post';
27 6
		$this->action        = 'delete_posts_by_sticky_post';
28 6
		$this->messages      = array(
29 6
			'box_label' => __( 'By Sticky Post', 'bulk-delete' ),
30
		);
31
	}
32
33
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
34
	public function render() {
35
		if ( ! $this->are_sticky_posts_present() ) : ?>
36
			<h4>
37
				<?php _e( 'There are no sticky post present in this WordPress installation.', 'bulk-delete' ); ?>
38
			</h4>
39
			<?php return; ?>
40
		<?php endif; // phpcs:ignore?>
0 ignored issues
show
Unused Code introduced by
InlineHTMLNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
41
42
		<h4><?php _e( 'Select the sticky post that you want to delete', 'bulk-delete' ); ?></h4>
43
44
		<fieldset class="options">
45
			<table class="optiontable">
46
				<tr>
47
					<td scope="row" colspan="2">
48
						<?php $this->render_sticky_posts_dropdown(); ?>
49
					</td>
50
				</tr>
51
			</table>
52
53
			<table class="optiontable">
54
				<?php
55
				$this->render_filtering_table_header();
56
				$this->render_sticky_action_settings();
57
				$this->render_delete_settings();
58
				?>
59
			</table>
60
		</fieldset>
61
62
		<?php
63
		$this->render_submit_button();
64
	}
65
66
	public function filter_js_array( $js_array ) {
67
		$js_array['msg']['unstickyPostsWarning'] = __( 'Are you sure you want to remove the selected posts from being sticky?', 'bulk-delete' );
68
		$js_array['msg']['deletePostsWarning']   = __( 'Are you sure you want to delete all the selected posts?', 'bulk-delete' );
69
		$js_array['msg']['selectStickyPost']     = __( 'Select at least one sticky post', 'bulk-delete' );
70
71
		$js_array['validators'][ $this->action ] = 'validateStickyPost';
72
		$js_array['error_msg'][ $this->action ]  = 'selectStickyPost';
73
74
		$js_array['pre_action_msg'][ $this->action ] = 'DeletePostsByStickyPostPreAction';
75
76
		return $js_array;
77
	}
78
79
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
80
	protected function convert_user_input_to_options( $request, $options ) {
81
		$options['selected_posts'] = bd_array_get( $request, 'smbd_' . $this->field_slug );
82
		$options['sticky_action']  = bd_array_get( $request, 'smbd_' . $this->field_slug . '_sticky_action' );
83
84
		return $options;
85
	}
86
87
	/**
88
	 * Override the `do_delete` function to handle unsticking posts.
89
	 *
90
	 * @inheritdoc
91
	 *
92
	 * @param array $options Array of Delete options.
93
	 *
94
	 * @return int Number of posts deleted or unsticked.
95
	 */
96 6
	protected function do_delete( $options ) {
97 6
		if ( 'unsticky' === $options['sticky_action'] ) {
98 2
			$posts_unsticked = 0;
99
100 2
			if ( in_array( 'all', $options['selected_posts'], true ) ) {
101 1
				$options['selected_posts'] = get_option( 'sticky_posts' );
102
			}
103
104 2
			foreach ( $options['selected_posts'] as $post_id ) {
105 2
				unstick_post( $post_id );
106 2
				$posts_unsticked ++;
107
			}
108
109 2
			$this->did_unsticky_post_instead_of_delete = true;
110
111 2
			return $posts_unsticked;
112
		}
113
114 4
		if ( 'delete' === $options['sticky_action'] ) {
115 4
			$query = $this->build_query( $options );
116
117 4
			if ( empty( $query ) ) {
118
				// Short circuit deletion, if nothing needs to be deleted.
119
				return 0;
120
			}
121
122 4
			return $this->delete_posts_from_query( $query, $options );
123
		}
124
125
		return 0;
126
	}
127
128
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
129 4
	protected function build_query( $options ) {
130 4
		$query = array();
131
132 4
		if ( in_array( 'all', $options['selected_posts'], true ) ) {
133 2
			$query['post__in'] = get_option( 'sticky_posts' );
134
		} else {
135 2
			$query['post__in'] = $options['selected_posts'];
136
		}
137
138 4
		return $query;
139
	}
140
141
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
142
	protected function get_success_message( $items_deleted ) {
143
		if ( $this->did_unsticky_post_instead_of_delete ) {
144
			/* translators: 1 Number of posts unsticked */
145
			return _n( '%d sticky post was made into normal post', '%d sticky posts were made into normal posts', $items_deleted, 'bulk-delete' );
146
		}
147
148
		/* translators: 1 Number of posts deleted */
149
		return _n( 'Deleted %d sticky post', 'Deleted %d sticky posts', $items_deleted, 'bulk-delete' );
150
	}
151
152
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
153
	protected function get_non_standard_input_key_map() {
154
		$prefix = $this->get_ui_input_prefix();
155
156
		$prefix_without_underscore_at_end = substr( $prefix, 0, -1 );
157
158
		return array(
159
			$prefix_without_underscore_at_end => $prefix . 'selected_posts',
160
		);
161
	}
162
163
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
164
	protected function prepare_cli_input( $input ) {
165
		// Handle multiple sticky post selection.
166
		$input['selected_posts'] = explode( ',', $input['selected_posts'] );
167
168
		return parent::prepare_cli_input( $input );
169
	}
170
}
171