Passed
Push — 640-fix/refactor-messages-in-u... ( 8f1d4f )
by
unknown
05:07
created

DeletePostsByURLModule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 63.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 79
ccs 19
cts 30
cp 0.6333
rs 10
c 1
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A convert_user_input_to_options() 0 6 1
A append_to_js_array() 0 4 1
A initialize() 0 13 1
A render() 0 21 1
A do_delete() 0 16 4
A build_query() 0 1 1
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
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
16 9
	protected function initialize() {
17 9
		$this->item_type     = 'posts';
18 9
		$this->field_slug    = 'specific';
19 9
		$this->meta_box_slug = 'bd_posts_by_url';
20 9
		$this->action        = 'delete_posts_by_url';
21 9
		$this->messages      = array(
22 9
			'box_label'        => __( 'By URL', 'bulk-delete' ),
23 9
			'confirm_deletion' => __( 'Are you sure you want to delete all the posts based on the entered url?', 'bulk-delete' ),
24 9
			'validation_error' => __( 'Please enter at least one post url', 'bulk-delete' ),
25
			/* translators: 1 Number of posts deleted */
26 9
			'deleted_one'      => __( 'Deleted %d post that had the entered URL(s)', 'bulk-delete' ),
27
			/* translators: 1 Number of posts deleted */
28 9
			'deleted_multiple' => __( 'Deleted %d posts that had the entered URL(s)', 'bulk-delete' ),
29
		);
30
	}
31
32
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
33
	public function render() { ?>
34
		<!-- URLs start-->
35
		<h4><?php _e( 'Delete posts and pages that have the following Permalink', 'bulk-delete' ); ?></h4>
36
37
		<fieldset class="options">
38
			<table class="optiontable">
39
				<tr>
40
					<td scope="row" colspan="2">
41
						<label for="smdb_specific_pages"><?php _e( 'Enter one post url (not post ids) per line', 'bulk-delete' ); ?></label>
42
						<br>
43
						<textarea id="smdb_specific_pages_urls" name="smdb_specific_pages_urls" rows="5" columns="80" class="validate"></textarea>
44
					</td>
45
				</tr>
46
47
				<?php $this->render_filtering_table_header(); ?>
48
				<?php $this->render_delete_settings(); ?>
49
50
			</table>
51
		</fieldset>
52
		<?php
53
		$this->render_submit_button();
54
	}
55
56
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
57
	public function append_to_js_array( $js_array ) {
58
		$js_array['validators'][ $this->action ] = 'validateTextbox';
59
60
		return $js_array;
61
	}
62
63
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
64
	protected function convert_user_input_to_options( $request, $options ) {
65
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_specific_force_delete', false );
66
67
		$options['urls'] = preg_split( '/\r\n|\r|\n/', bd_array_get( $request, 'smdb_specific_pages_urls' ) );
68
69
		return $options;
70
	}
71
72
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
73 9
	protected function do_delete( $delete_options ) {
74 9
		$post_ids = array();
75
76 9
		foreach ( $delete_options['urls'] as $url ) {
77 9
			if ( substr( $url, 0, 1 ) === '/' ) {
78
				$url = get_site_url() . $url;
79
			}
80
81 9
			$post_id = url_to_postid( $url );
82
83 9
			if ( $post_id > 0 ) {
84 7
				$post_ids[] = $post_id;
85
			}
86
		}
87
88 9
		return $this->delete_posts_by_id( $post_ids, $delete_options['force_delete'] );
89
	}
90
91
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
92
	protected function build_query( $options ) {
93
		// Left empty on purpose.
94
	}
95
}
96