Completed
Push — dev/6.0.0 ( 06207e...bb8154 )
by Sudar
15:08
created

DeletePostsByStickyPostModule::do_delete()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 25
ccs 0
cts 3
cp 0
crap 30
rs 9.5222
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 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
	protected function initialize() {
24
		$this->item_type     = 'posts';
25
		$this->field_slug    = 'sticky_post';
26
		$this->meta_box_slug = 'delete_posts_by_sticky_post';
27
		$this->action        = 'delete_posts_by_sticky_post';
28
		$this->messages      = array(
29
			'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
	protected function do_delete( $options ) {
97
		if ( 'unsticky' === $options['sticky_action'] ) {
98
			$posts_unsticked = 0;
99
100
			foreach ( $options['selected_posts'] as $post ) {
101
				unstick_post( $post );
102
				$posts_unsticked ++;
103
			}
104
105
			$this->did_unsticky_post_instead_of_delete = true;
106
			return $posts_unsticked;
107
		}
108
109
		if ( 'delete' === $options['sticky_action'] ) {
110
			$query = $this->build_query( $options );
111
112
			if ( empty( $query ) ) {
113
				// Short circuit deletion, if nothing needs to be deleted.
114
				return 0;
115
			}
116
117
			return $this->delete_posts_from_query( $query, $options );
118
		}
119
120
		return 0;
121
	}
122
123
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
124
	protected function build_query( $options ) {
125
		$query = array();
126
127
		if ( in_array( 'all', $options['selected_posts'], true ) ) {
128
			$query['post__in'] = get_option( 'sticky_posts' );
129
		} else {
130
			$query['post__in'] = $options['selected_posts'];
131
		}
132
133
		return $query;
134
	}
135
136
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
137
	protected function get_success_message( $items_deleted ) {
138
		if ( $this->did_unsticky_post_instead_of_delete ) {
139
			/* translators: 1 Number of posts unsticked */
140
			return _n( '%d sticky post was made into normal post', '%d sticky posts were made into normal posts', $items_deleted, 'bulk-delete' );
141
		}
142
143
		/* translators: 1 Number of posts deleted */
144
		return _n( 'Deleted %d sticky post', 'Deleted %d sticky posts', $items_deleted, 'bulk-delete' );
145
	}
146
}
147