Completed
Push — 50-feature/delete-blogs-in-mul... ( a42973...c0491a )
by
unknown
04:15
created

DeleteSitesByNameModule::do_delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Sites\Modules;
4
5
use BulkWP\BulkDelete\Core\Sites\SitesModule;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Sites by Name.
11
 *
12
 * @since 6.2.0
13
 */
14
class DeleteSitesByNameModule extends SitesModule {
15
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
16
	protected function initialize() {
17
		$this->item_type     = 'sites';
18
		$this->field_slug    = 'sites_by_name';
19
		$this->meta_box_slug = 'bd_delete_sites_by_name';
20
		$this->action        = 'delete_sites_by_name';
21
		$this->messages      = array(
22
			'box_label'        => __( 'Delete Sites by Name', 'bulk-delete' ),
23
			'confirm_deletion' => __( 'Are you sure you want to delete all the sites based on the selected option?', 'bulk-delete' ),
24
			'validation_error' => __( 'Please enter the site name that should be deleted', 'bulk-delete' ),
25
			/* translators: 1 Number of sites deleted */
26
			'deleted_one'      => __( 'Deleted %d site with the selected options', 'bulk-delete' ),
27
			/* translators: 1 Number of sites deleted */
28
			'deleted_multiple' => __( 'Deleted %d sites with the selected options', 'bulk-delete' ),
29
		);
30
	}
31
32
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
33
	public function render() {
34
		?>
35
		<h4><?php _e( 'Enter the site name that you want to delete', 'bulk-delete' ); ?></h4>
36
		<fieldset class="options">
37
			<table class="optiontable">
38
				<tr>
39
					<td><?php _e( 'Delete Sites if the name ', 'bulk-delete' ); ?></td>
40
					<td><?php $this->render_string_operators_dropdown( 'stringy', array( '=', 'LIKE', 'STARTS_WITH', 'ENDS_WITH' ) ); ?></td>
41
					<td><input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" placeholder="<?php _e( 'Site Name', 'bulk-delete' ); ?>" class="validate"></td>
42
				</tr>
43
			</table>
44
		</fieldset>
45
46
		<?php
47
		$this->render_submit_button();
48
	}
49
50
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
51
	protected function append_to_js_array( $js_array ) {
52
		$js_array['validators'][ $this->action ] = 'validateTextbox';
53
54
		return $js_array;
55
	}
56
57
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
58
	protected function convert_user_input_to_options( $request, $options ) {
59
		$options['operator'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' );
60
		$options['value']    = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value' ) );
61
62
		return $options;
63
	}
64
65
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
66
	protected function build_query( $options ) {
67
		// Left empty on purpose.
68
	}
69
70
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
71
	protected function do_delete( $options ) {
72
		global $wpdb;
73
		$count = 0;
74
75
		$site_ids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE path = %s", $options['value'] ) );
76
77
		foreach ( $site_ids as $site_id ) {
78
			$response = wp_delete_site( $site_id );
79
			if ( ! is_wp_error( $response ) ) {
80
				$count++;
81
			}
82
		}
83
		return $count;
84
	}
85
}
86