Completed
Push — 312-fix/delete-post-meta-modul... ( fbbe37...602413 )
by Sudar
14:44 queued 11:16
created

DeletePostsByURLModule::do_delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
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 URL Module.
11
 *
12
 * @since 6.0.0
13
 */
14
class DeletePostsByURLModule extends PostsModule {
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 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
	protected function do_delete( $delete_options ) {
57
		$post_ids = array();
58
59
		foreach ( $delete_options['urls'] as $url ) {
60
			if ( substr( $url, 0, 1 ) == '/' ) {
61
				$post_ids[] = url_to_postid( get_site_url() . $url );
62
			}
63
		}
64
65
		return $this->delete_posts_by_id( $post_ids, $delete_options['force_delete'] );
66
	}
67
68
	protected function get_success_message( $items_deleted ) {
69
		/* translators: 1 Number of pages deleted */
70
		return _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $items_deleted, 'bulk-delete' );
71
	}
72
73
	protected function build_query( $options ) {
74
		// Left empty on purpose.
75
	}
76
}
77