Passed
Push — 247-fix/delete-term-meta ( 093d38...957a5a )
by Rajan
74:15 queued 71:04
created

DeletePostsByURLModule::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 21
ccs 0
cts 3
cp 0
crap 2
rs 9.9332
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 URL Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByURLModule extends PostsModule {
15 9
	protected function initialize() {
16 9
		$this->item_type     = 'posts';
17 9
		$this->field_slug    = 'specific';
18 9
		$this->meta_box_slug = 'bd_posts_by_url';
19 9
		$this->action        = 'delete_posts_by_url';
20 9
		$this->messages      = array(
21 9
			'box_label' => __( 'By URL', 'bulk-delete' ),
22
		);
23 9
	}
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 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();
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
53
		return $options;
54
	}
55
56 9
	protected function do_delete( $delete_options ) {
57 9
		$post_ids = array();
58
59 9
		foreach ( $delete_options['urls'] as $url ) {
60 9
			if ( substr( $url, 0, 1 ) === '/' ) {
61
				$url = get_site_url() . $url;
62
			}
63
64 9
			$post_id = url_to_postid( $url );
65
66 9
			if ( $post_id > 0 ) {
67 9
				$post_ids[] = $post_id;
68
			}
69
		}
70
71 9
		return $this->delete_posts_by_id( $post_ids, $delete_options['force_delete'] );
72
	}
73
74
	protected function get_success_message( $items_deleted ) {
75
		/* translators: 1 Number of pages deleted */
76
		return _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $items_deleted, 'bulk-delete' );
77
	}
78
79
	protected function build_query( $options ) {
80
		// Left empty on purpose.
81
	}
82
}
83