Completed
Pull Request — dev/6.0.0 (#257)
by Rajan
13:39
created

DeletePostsByRevisionMetabox::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Posts\Metabox;
4
5
use BulkWP\BulkDelete\Core\Posts\PostsMetabox;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Posts by Status Metabox.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByRevisionMetabox extends PostsMetabox {
15
	protected function initialize() {
16
		$this->item_type     = 'posts';
17
		$this->field_slug    = 'revisions';
18
		$this->meta_box_slug = 'bd_posts_by_revision';
19
		$this->action        = 'delete_posts_by_revision';
20
		$this->messages      = array(
21
			'box_label' => __( 'By Post Revisions', 'bulk-delete' ),
22
		);
23
	}
24
25
	public function render() {
26
		global $wpdb;
27
		$revisions = $wpdb->get_var( "select count(*) from $wpdb->posts where post_type = 'revision'" );
28
?>
29
        <!-- Post Revisions start-->
30
        <h4><?php _e( 'Select the posts which you want to delete', 'bulk-delete' ); ?></h4>
31
32
        <fieldset class="options">
33
        <table class="optiontable">
34
            <tr>
35
                <td>
36
                    <input name="smbd_revisions" id ="smbd_revisions" value="revisions" type="checkbox">
37
                    <label for="smbd_revisions"><?php _e( 'All Revisions', 'bulk-delete' ); ?> (<?php echo $revisions . ' '; _e( 'Revisions', 'bulk-delete' ); ?>)</label>
38
                </td>
39
            </tr>
40
41
        </table>
42
        </fieldset>
43
<?php
44
		$this->render_submit_button( 'delete_posts_by_revision' );
0 ignored issues
show
Unused Code introduced by
The call to BulkWP\BulkDelete\Core\B...:render_submit_button() has too many arguments starting with 'delete_posts_by_revision'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
		$this->/** @scrutinizer ignore-call */ 
45
         render_submit_button( 'delete_posts_by_revision' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
45
	}
46
47
	protected function convert_user_input_to_options( $request, $options ) {
48
		$options = array( 'revisions' => bd_array_get( $request, 'smbd_revisions' ) );
49
50
		return $options;
51
	}
52
53
	public function delete( $delete_options ) {
54
		$deleted_count = $this->delete_posts_by_revision( $delete_options );
55
56
		return $deleted_count;
57
	}
58
59
	/**
60
	 * Delete all post revisions.
61
	 *
62
	 * @since 5.0
63
	 * @static
64
	 *
65
	 * @param array $delete_options
66
	 *
67
	 * @return int The number of posts that were deleted
68
	 */
69
	public static function delete_posts_by_revision( $delete_options ) {
70
		global $wpdb;
71
72
		// Revisions
73
		if ( 'revisions' == $delete_options['revisions'] ) {
74
			$revisions = $wpdb->get_results( "select ID from $wpdb->posts where post_type = 'revision'" );
75
76
			foreach ( $revisions as $revision ) {
77
				wp_delete_post( $revision->ID );
78
			}
79
80
			return count( $revisions );
81
		}
82
83
		return 0;
84
	}
85
86
	protected function get_success_message( $items_deleted ) {
87
		/* translators: 1 Number of pages deleted */
88
		return _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $items_deleted, 'bulk-delete' );
89
	}
90
}
91