Completed
Push — 50-feature/delete-blogs-in-mul... ( ad0625...05ec87 )
by
unknown
42:17 queued 38:54
created

DeleteSitesByNameModule::do_delete()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 7
eloc 24
c 4
b 1
f 0
nc 24
nop 1
dl 0
loc 35
rs 8.6026
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
		$operator = $options['operator'];
75
		$value    = $options['value'];
76
77
		switch ( $operator ) {
78
			case 'LIKE':
79
				$value = '%' . $value . '%';
80
				break;
81
			case 'STARTS_WITH':
82
				$operator = 'LIKE';
83
				$value    = $value . '%';
84
				break;
85
			case 'ENDS_WITH':
86
				$operator = 'LIKE';
87
				$value    = '%' . $value;
88
				break;
89
		}
90
91
		if ( ! is_subdomain_install() ) {
92
			$value = get_network()->path . $value . '/';
93
		}
94
95
		$site_ids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE path {$operator} %s", $value ) );
96
97
		foreach ( $site_ids as $site_id ) {
98
			// TODO: Do we need to log errors if there is any error?
99
			$response = wp_delete_site( $site_id );
100
			if ( ! is_wp_error( $response ) ) {
101
				$count++;
102
			}
103
		}
104
105
		return $count;
106
	}
107
}
108