Completed
Push — 137-feature/refactor-post-modu... ( 580e59 )
by Sudar
07:52
created

DeletePagesByStatusMetabox::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 61
ccs 0
cts 15
cp 0
crap 2
rs 9.5147
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace BulkWP\BulkDelete\Core\Pages\Metabox;
3
4
use BulkWP\BulkDelete\Core\Pages\PagesMetabox;
5
6 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Delete Pages by Status Metabox.
10
 *
11
 * @since 5.7.0
12
 */
13
class DeletePagesByStatusMetabox extends PagesMetabox {
14
15
	protected function initialize() {
16
		$this->item_type     = 'pages';
17
//		$this->field_slug    = 'u_meta';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
18
		$this->meta_box_slug = 'bd_pages_by_status';
19
		$this->action        = 'delete_pages_by_status';
20
		$this->cron_hook     = 'do-bulk-delete-pages-by-status';
21
		$this->scheduler_url = 'http://bulkwp.com/addons/scheduler-for-deleting-pages-by-status/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-u-ma';
22
		$this->messages      = array(
23
			'box_label'      => __( 'By Page Status', 'bulk-delete' ),
24
			'scheduled'      => __( 'The selected pages are scheduled for deletion', 'bulk-delete' ),
25
		);
26
	}
27
28
	public function render() {
29
		$pages_count  = wp_count_posts( 'page' );
30
		$pages        = $pages_count->publish;
31
		$page_drafts  = $pages_count->draft;
32
		$page_future  = $pages_count->future;
33
		$page_pending = $pages_count->pending;
34
		$page_private = $pages_count->private;
35
		?>
36
		<!-- Pages start-->
37
		<h4><?php _e( 'Select the status from which you want to delete pages', 'bulk-delete' ); ?></h4>
38
39
		<fieldset class="options">
40
			<table class="optiontable">
41
				<tr>
42
					<td>
43
						<input name="smbd_published_pages" value="published_pages" type="checkbox">
44
						<label for="smbd_published_pages"><?php _e( 'All Published Pages', 'bulk-delete' ); ?> (<?php echo $pages . ' '; _e( 'Pages', 'bulk-delete' ); ?>)</label>
45
					</td>
46
				</tr>
47
48
				<tr>
49
					<td>
50
						<input name="smbd_draft_pages" value="draft_pages" type="checkbox">
51
						<label for="smbd_draft_pages"><?php _e( 'All Draft Pages', 'bulk-delete' ); ?> (<?php echo $page_drafts . ' '; _e( 'Pages', 'bulk-delete' ); ?>)</label>
52
					</td>
53
				</tr>
54
55
				<tr>
56
					<td>
57
						<input name="smbd_future_pages" value="scheduled_pages" type="checkbox">
58
						<label for="smbd_future_pages"><?php _e( 'All Scheduled Pages', 'bulk-delete' ); ?> (<?php echo $page_future . ' '; _e( 'Pages', 'bulk-delete' ); ?>)</label>
59
					</td>
60
				</tr>
61
62
				<tr>
63
					<td>
64
						<input name="smbd_pending_pages" value="pending_pages" type="checkbox">
65
						<label for="smbd_pending_pages"><?php _e( 'All Pending Pages', 'bulk-delete' ); ?> (<?php echo $page_pending . ' '; _e( 'Pages', 'bulk-delete' ); ?>)</label>
66
					</td>
67
				</tr>
68
69
				<tr>
70
					<td>
71
						<input name="smbd_private_pages" value="private_pages" type="checkbox">
72
						<label for="smbd_private_pages"><?php _e( 'All Private Pages', 'bulk-delete' ); ?> (<?php echo $page_private . ' '; _e( 'Pages', 'bulk-delete' ); ?>)</label>
73
					</td>
74
				</tr>
75
			</table>
76
77
			<table class="optiontable">
78
				<?php
79
				bd_render_filtering_table_header();
80
				bd_render_restrict_settings( 'pages', 'pages' );
81
				bd_render_delete_settings( 'pages' );
82
				bd_render_limit_settings( 'pages' );
83
				bd_render_cron_settings( 'pages','http://bulkwp.com/addons/scheduler-for-deleting-pages-by-status/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sp' );
84
				?>
85
			</table>
86
		</fieldset>
87
		<?php
88
		$this->render_submit_button();
89
	}
90
91
	protected function convert_user_input_to_options( $request ) {
92
		$delete_options = array();
93
94
		$delete_options['restrict']     = bd_array_get_bool( $request, 'smbd_pages_restrict', false );
95
		$delete_options['limit_to']     = absint( bd_array_get( $request, 'smbd_pages_limit_to', 0 ) );
96
		$delete_options['force_delete'] = bd_array_get_bool( $request, 'smbd_pages_force_delete', false );
97
98
		$delete_options['date_op'] = bd_array_get( $request, 'smbd_pages_op' );
99
		$delete_options['days']    = absint( bd_array_get( $request, 'smbd_pages_days' ) );
100
101
		$delete_options['publish'] = bd_array_get( $request, 'smbd_published_pages' );
102
		$delete_options['drafts']  = bd_array_get( $request, 'smbd_draft_pages' );
103
		$delete_options['pending'] = bd_array_get( $request, 'smbd_pending_pages' );
104
		$delete_options['future']  = bd_array_get( $request, 'smbd_future_pages' );
105
		$delete_options['private'] = bd_array_get( $request, 'smbd_private_pages' );
106
107
		return $delete_options;
108
	}
109
110
	public function delete( $delete_options ) {
111
		global $wp_query;
112
113
		// Backward compatibility code. Will be removed in Bulk Delete v6.0
114
		if ( array_key_exists( 'page_op', $delete_options ) ) {
115
			$delete_options['date_op'] = $delete_options['page_op'];
116
			$delete_options['days']    = $delete_options['page_days'];
117
		}
118
		$delete_options = apply_filters( 'bd_delete_options', $delete_options );
119
120
		$post_status = array();
121
122
		// published pages
123
		if ( 'published_pages' == $delete_options['publish'] ) {
124
			$post_status[] = 'publish';
125
		}
126
127
		// Drafts
128
		if ( 'draft_pages' == $delete_options['drafts'] ) {
129
			$post_status[] = 'draft';
130
		}
131
132
		// Pending pages
133
		if ( 'pending_pages' == $delete_options['pending'] ) {
134
			$post_status[] = 'pending';
135
		}
136
137
		// Future pages
138
		if ( 'future_pages' == $delete_options['future'] ) {
139
			$post_status[] = 'future';
140
		}
141
142
		// Private pages
143
		if ( 'private_pages' == $delete_options['private'] ) {
144
			$post_status[] = 'private';
145
		}
146
147
		$options = array(
148
			'post_type'   => 'page',
149
			'post_status' => $post_status,
150
		);
151
152
		$options = bd_build_query_options( $delete_options, $options );
153
		$pages   = $wp_query->query( $options );
154
		foreach ( $pages as $page ) {
155
			wp_delete_post( $page->ID, $delete_options['force_delete'] );
156
		}
157
158
		return count( $pages );
159
	}
160
161
	protected function get_success_message( $items_deleted ) {
162
		/* translators: 1 Number of pages deleted */
163
		return _n( 'Deleted %d page with the selected page status', 'Deleted %d pages with the selected page status', $items_deleted, 'bulk-delete' );
164
	}
165
}
166