Passed
Push — 701-fix/make-query-overrider-g... ( 81ac3b )
by
unknown
05:04
created

DateQueryOverrider::process_date_fields()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9.4867

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
nc 16
nop 1
dl 0
loc 36
ccs 18
cts 22
cp 0.8182
crap 9.4867
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Metas\QueryOverriders;
4
5
use BulkWP\BulkDelete\Core\Base\BaseQueryOverrider;
6
7
/**
8
 * Class that encapsulates the logic for handling date format specified by the user.
9
 *
10
 * @package Bulk Delete
11
 *
12
 * @author  Sudar
13
 *
14
 * @since   6.0
15
 */
16
class DateQueryOverrider extends BaseQueryOverrider {
17
	/**
18
	 * Delete Options.
19
	 *
20
	 * @var array
21
	 */
22
	protected $delete_options;
23
	/**
24
	 * Date format of meta value that is stored in `wp_post_meta` table..
25
	 *
26
	 * @var string
27
	 */
28
	protected $meta_value_date_format;
29
	/**
30
	 * Meta belongs to which table.  It can be post,comment or user.
31
	 *
32
	 * @var string
33
	 */
34
	protected $whose_meta;
35
36
	/**
37
	 * Setup hooks and load.
38
	 *
39
	 * @since 1.0
40
	 */
41 1
	public function load() {
42 1
		if ( 'comment' === $this->whose_meta ) {
43 1
			add_action( 'parse_comment_query', array( $this, 'parse_query' ) );
44
		} elseif ( 'post' === $this->whose_meta ) {
45
			add_action( 'parse_query', array( $this, 'parse_query' ) );
46
		} elseif ( 'user' === $this->whose_meta ) {
47
			add_action( 'pre_user_query', array( $this, 'parse_query' ) );
48
		}
49
	}
50
51
	/**
52
	 * Creates query object after processing date with specified date format.
53
	 *
54
	 * @param array $delete_options Delete Options.
55
	 *
56
	 * @return \WP_Query $query Query object.
57
	 */
58 7
	public function get_query( $delete_options ) {
59 7
		$query = $this->process_date_fields( $delete_options );
60
61 7
		return $query;
62
	}
63
64
	/**
65
	 * Process date fields and returns query built.
66
	 *
67
	 * @param array $delete_options Delete Options.
68
	 *
69
	 * @return array $options Query.
70
	 */
71 7
	public function process_date_fields( $delete_options ) {
72 7
		if ( ! empty( $delete_options['relative_date'] ) && 'custom' !== $delete_options['relative_date'] ) {
73 1
			$delete_options['meta_value'] = date( 'c', strtotime( $delete_options['relative_date'] ) );
74
		}
75
76 7
		if ( ! empty( $delete_options['date_unit'] ) && ! empty( $delete_options['date_type'] ) ) {
77 2
			$interval_unit = $delete_options['date_unit'];
78 2
			$interval_type = $delete_options['date_type'];
79
80 2
			switch ( $delete_options['meta_op'] ) {
81
				case '<':
82
				case '<=':
83
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( '-' . $interval_unit . ' ' . $interval_type ) );
84
					break;
85
				default:
86 2
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( $interval_unit . ' ' . $interval_type ) );
87
			}
88
		}
89
90
		$meta_query = array(
91 7
			'key'     => $delete_options['meta_key'],
92 7
			'value'   => $delete_options['meta_value'],
93 7
			'compare' => $delete_options['meta_op'],
94 7
			'type'    => $delete_options['meta_type'],
95
		);
96
97 7
		$options = array( $meta_query );
98
99 7
		if ( 'DATE' === $meta_query['type'] && ! empty( $delete_options['meta_value_date_format'] ) ) {
100 1
			$options['bd_meta_value_date_format'] = $delete_options['meta_value_date_format'];
101 1
			$this->whose_meta                     = $delete_options['whose_meta'];
102
103 1
			$this->load();
104
		}
105
106 7
		return $options;
107
	}
108
109
	/**
110
	 * Parse the query object.
111
	 *
112
	 * @param \WP_Query $query Query object.
113
	 *
114
	 * @since  0.3
115
	 */
116 1
	public function parse_query( $query ) {
117 1
		if ( isset( $query->query_vars['meta_query']['bd_meta_value_date_format'] ) ) {
118 1
			$this->meta_value_date_format = $query->query_vars['meta_query']['bd_meta_value_date_format'];
119
120 1
			add_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ), 10, 6 );
121 1
			add_action( 'bd_after_meta_query', array( $this, 'remove_filter' ) );
122
		}
123
	}
124
125
	/**
126
	 * Process date format in sql query.
127
	 *
128
	 * @param array  $query          Array containing the query's JOIN and WHERE clauses.
129
	 * @param array  $input          Array of meta queries.
130
	 * @param string $type           Type of meta.
131
	 * @param string $primary_table  Primary table.
132
	 * @param string $primary_column Primary column ID.
133
	 * @param object $context        The main query object.
134
	 *
135
	 * @return array $query Processed query.
136
	 *
137
	 * @since 0.3
138
	 */
139 1
	public function process_sql_date_format( $query, $input, $type, $primary_table, $primary_column, $context ) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

139
	public function process_sql_date_format( $query, $input, $type, $primary_table, $primary_column, /** @scrutinizer ignore-unused */ $context ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
		global $wpdb;
141 1
		if ( 'DATE' === $input[0]['type'] && $this->whose_meta === $type && 'comment_ID' === $primary_column ) {
142 1
			$meta_table = _get_meta_table( $type );
143
144 1
			$query['where'] = $wpdb->prepare(
145 1
				" AND ( $meta_table.meta_key = %s AND STR_TO_DATE($meta_table.meta_value, %s) {$input[0]['compare']} STR_TO_DATE(%s, %s) ) ",
146 1
				$input[0]['key'],
147 1
				$this->meta_value_date_format,
148 1
				$input[0]['value'],
149 1
				'%Y-%m-%d'
150
			);
151
		}
152
153 1
		return $query;
154
	}
155
156
	/**
157
	 * Remove meta sql filter.
158
	 *
159
	 * @return void
160
	 */
161 1
	public function remove_filter() {
162 1
		remove_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ) );
163
	}
164
}
165