Passed
Push — 192-feature/delete-posts-by-UR... ( 1476b6 )
by Rajan
06:39
created

DeletePostsByURLMetabox::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
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 URL Metabox.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByURLMetabox extends PostsMetabox {
15
	protected function initialize() {
16
		$this->item_type     = 'posts';
17
		$this->field_slug    = 'specific';
18
		$this->meta_box_slug = 'bd_posts_by_url';
19
		$this->action        = 'delete_posts_by_url';
20
		$this->messages      = array(
21
			'box_label' => __( 'By URL', 'bulk-delete' )
22
		);
23
	}
24
25
	public function render() { ?>
26
		<!-- URLs start-->
27
        <h4><?php _e( 'Delete posts and pages that have the following Permalink', 'bulk-delete' ); ?></h4>
28
29
        <fieldset class="options">
30
        <table class="optiontable">
31
            <tr>
32
                <td scope="row" colspan="2">
33
                    <label for="smdb_specific_pages"><?php _e( 'Enter one post url (not post ids) per line', 'bulk-delete' ); ?></label>
34
                    <br>
35
                    <textarea style="width: 450px; height: 80px;" id="smdb_specific_pages_urls" name="smdb_specific_pages_urls" rows="5" columns="80"></textarea>
36
                </td>
37
            </tr>
38
39
			<?php $this->render_filtering_table_header(); ?>
40
			<?php $this->render_delete_settings(); ?>
41
42
        </table>
43
        </fieldset>
44
<?php
45
		$this->render_submit_button( 'delete_posts_by_url' );
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_url'. ( Ignorable by Annotation )

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

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

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...
46
	}
47
48
	protected function convert_user_input_to_options( $request, $options ) {
49
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_specific_force_delete', false );
50
51
		$options['urls'] = preg_split( '/\r\n|\r|\n/', bd_array_get( $request, 'smdb_specific_pages_urls' ) );
52
		return $options;
53
	}
54
55
	public function delete( $delete_options ) {
56
57
		foreach ( $delete_options['urls'] as $url ) {
58
			$checkedurl = $url;
59
			if ( substr( $checkedurl , 0, 1 ) == '/' ) {
60
				$checkedurl = get_site_url() . $checkedurl ;
61
			}
62
			$postid = url_to_postid( $checkedurl );
63
			wp_delete_post( $postid, $delete_options['force_delete'] );
64
		}
65
66
		$deleted_count = count( $delete_options['urls'] );
67
68
		return $deleted_count;
69
	}
70
71
	protected function get_success_message( $items_deleted ) {
72
		/* translators: 1 Number of pages deleted */
73
		return _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $items_deleted, 'bulk-delete' );
74
	}
75
}
76