Completed
Push — 50-feature/delete-blogs-in-mul... ( a42973 )
by
unknown
05:00
created

SitesModule::get_date_query()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 9.6111
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Sites;
4
5
use BulkWP\BulkDelete\Core\Base\BaseModule;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Module for deleting sites.
11
 *
12
 * @since 6.2.0
13
 */
14
abstract class SitesModule extends BaseModule {
15
	protected $item_type = 'sites';
16
17
	/**
18
	 * Build query params for WP_Site_Query by using delete options.
19
	 *
20
	 * Return an empty query array to short-circuit deletion.
21
	 *
22
	 * @param array $options Delete options.
23
	 *
24
	 * @return array Query.
25
	 */
26
	abstract protected function build_query( $options );
27
28
	/**
29
	 * Handle common filters.
30
	 *
31
	 * @param array $request Request array.
32
	 *
33
	 * @return array User options.
34
	 */
35
	protected function parse_common_filters( $request ) {
36
		$options = array();
37
38
		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false );
39
		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) );
40
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
41
42
		if ( $options['restrict'] ) {
43
			$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
44
			$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
45
		}
46
47
		return $options;
48
	}
49
50
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
51
	protected function do_delete( $options ) {
52
		$query = $this->build_query( $options );
53
54
		if ( empty( $query ) ) {
55
			// Short circuit deletion, if nothing needs to be deleted.
56
			return 0;
57
		}
58
59
		return $this->delete_sites_from_query( $query, $options );
60
	}
61
62
	/**
63
	 * Query and Delete sites.
64
	 *
65
	 * @access protected
66
	 *
67
	 * @param array $query   Options to query sites.
68
	 * @param array $options Delete options.
69
	 *
70
	 * @return int Number of sites deleted.
71
	 */
72
	protected function delete_sites_from_query( $query, $options ) {
73
		$count    = 0;
74
		$comments = $this->query_sites( $query );
0 ignored issues
show
Bug introduced by
The method query_sites() does not exist on BulkWP\BulkDelete\Core\Sites\SitesModule. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
		/** @scrutinizer ignore-call */ 
75
  $comments = $this->query_sites( $query );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
76
		foreach ( $comments as $comment ) {
77
			$deleted = wp_delete_site( $comment, $options['force_delete'] );
0 ignored issues
show
Unused Code introduced by
The call to wp_delete_site() has too many arguments starting with $options['force_delete']. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
			$deleted = /** @scrutinizer ignore-call */ wp_delete_site( $comment, $options['force_delete'] );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
79
			if ( $deleted ) {
80
				$count ++;
81
			}
82
		}
83
84
		return $count;
85
	}
86
87
	/**
88
	 * Query sites using options.
89
	 *
90
	 * @param array $options Query options.
91
	 *
92
	 * @return array List of comment IDs.
93
	 */
94
	protected function sites( $options ) {
95
		$defaults = array(
96
			'update_site_meta_cache' => false,
97
			'fields'                 => 'ids',
98
		);
99
100
		$options = wp_parse_args( $options, $defaults );
101
102
		$wp_site_query = new \WP_Site_Query();
103
104
		/**
105
		 * This action before the query happens.
106
		 *
107
		 * @since 6.2.0
108
		 *
109
		 * @param \WP_Site_Query $wp_site_query Query object.
110
		 */
111
		do_action( 'bd_before_query', $wp_site_query );
112
113
		$sites = (array) $wp_comment_query->query( $options );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $wp_comment_query seems to be never defined.
Loading history...
114
115
		/**
116
		 * This action runs after the query happens.
117
		 *
118
		 * @since 6.2.0
119
		 *
120
		 * @param \WP_Site_Query $wp_site_query Query object.
121
		 */
122
		do_action( 'bd_after_query', $wp_site_query );
123
124
		return $sites;
125
	}
126
127
	/**
128
	 * Get the date query part for WP_Comment_Query.
129
	 *
130
	 * Date query corresponds to comment date.
131
	 *
132
	 * @param array $options Delete options.
133
	 *
134
	 * @return array Date Query.
135
	 */
136
	protected function get_date_query( $options ) {
137
		if ( ! $options['restrict'] ) {
138
			return array();
139
		}
140
141
		if ( $options['days'] <= 0 ) {
142
			return array();
143
		}
144
145
		if ( 'before' === $options['date_op'] || 'after' === $options['date_op'] ) {
146
			return array(
147
				$options['date_op'] => $options['days'] . ' days ago',
148
			);
149
		}
150
151
		return array();
152
	}
153
}
154