Passed
Pull Request — dev/6.1.0 (#564)
by
unknown
10:07
created

DateQueryOverrider::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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
	/**
31
	 * Date format of input value that will be compared with meta value.
32
	 *
33
	 * @since 1.0
34
	 *
35
	 * @var string
36
	 */
37
	protected $input_value_date_format;
38
39
	/**
40
	 * Setup hooks and load.
41
	 *
42
	 * @since 1.0
43
	 */
44
	public function load() {
45
		add_action( 'parse_query', array( $this, 'parse_query' ) );
46
	}
47
48
	/**
49
	 * Creates query object after processing date with specified date format.
50
	 *
51
	 * @param array $delete_options Delete Options.
52
	 *
53
	 * @return \WP_Query $query Query object.
54
	 */
55
	public function get_query( $delete_options ) {
56
		$query = $this->process_date_fields( $delete_options );
57
58
		return $query;
59
	}
60
61
	public function process_date_fields( $delete_options ) {
62
		if ( ! empty( $delete_options['relative_date'] ) && 'custom' !== $delete_options['relative_date'] ) {
63
			$delete_options['meta_value'] = date( 'c', strtotime( $delete_options['relative_date'] ) );
64
		}
65
66
		if ( ! empty( $delete_options['date_unit'] ) && ! empty( $delete_options['date_type'] ) ) {
67
			$interval_unit = $delete_options['date_unit'];
68
			$interval_type = $delete_options['date_type'];
69
70
			switch ( $delete_options['meta_op'] ) {
71
				case '<':
72
				case '<=':
73
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( '-' . $interval_unit . ' ' . $interval_type ) );
74
					break;
75
				default:
76
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( $interval_unit . ' ' . $interval_type ) );
77
			}
78
		}
79
80
		// In v1.0 `date_format` was changed to `meta_value_date_format`.
81
		// Needed?
82
		if ( isset( $delete_options['date_format'] ) ) {
83
			$delete_options['meta_value_date_format'] = $delete_options['date_format'];
84
		}
85
		$meta_query = array(
86
			'key'     => $delete_options['meta_key'],
87
			'value'   => $delete_options['meta_value'],
88
			'compare' => $delete_options['meta_op'],
89
			'type'    => $delete_options['meta_type'],
90
		);
91
92
		$options = array(
93
			'meta_query' => array( $meta_query ),
94
		);
95
96
		if ( 'DATE' === $meta_query['type'] && ! empty( $delete_options['meta_value_date_format'] ) ) {
97
			$options['cf_meta_value_date_format'] = $delete_options['meta_value_date_format'];
98
99
			if ( ! empty( $delete_options['input_value_date_format'] ) ) {
100
				$options['cf_input_value_date_format'] = $delete_options['input_value_date_format'];
101
			} else {
102
				$options['cf_input_value_date_format'] = '%Y-%m-%d';
103
			}
104
105
			$this->load();
106
		}
107
108
		return $options;
109
	}
110
111
	/**
112
	 * Parse the query object.
113
	 *
114
	 * @param \WP_Query $query Query object.
115
	 *
116
	 * @since  0.3
117
	 *
118
	 */
119
	public function parse_query( $query ) {
120
		if ( isset( $query->query_vars['cf_meta_value_date_format'] ) ) {
121
			$this->meta_value_date_format  = $query->query_vars['cf_meta_value_date_format'];
122
			$this->input_value_date_format = $query->query_vars['cf_input_value_date_format'];
123
124
			add_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ), 10, 6 );
125
			add_filter( 'posts_selection', array( $this, 'remove_filter' ) );
126
		}
127
	}
128
129
	/**
130
	 * Process date format in sql query.
131
	 *
132
	 * @param array  $query          Array containing the query's JOIN and WHERE clauses.
133
	 * @param array  $input          Array of meta queries.
134
	 * @param string $type           Type of meta.
135
	 * @param string $primary_table  Primary table.
136
	 * @param string $primary_column Primary column ID.
137
	 * @param object $context        The main query object.
138
	 *
139
	 * @return array $query Processed query.
140
	 *
141
	 * @since 0.3
142
	 *
143
	 */
144
	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

144
	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...
145
		global $wpdb;
146
147
		if ( 'DATE' === $input[0]['type'] && 'post' === $type && 'wp_posts' === $primary_table && 'ID' === $primary_column ) {
148
			$meta_table = _get_meta_table( $type );
149
150
			$query['where'] = $wpdb->prepare(
151
				" AND ( $meta_table.meta_key = %s AND STR_TO_DATE($meta_table.meta_value, %s) {$input[0]['compare']} STR_TO_DATE(%s, %s) ) ",
152
				$input[0]['key'],
153
				$this->meta_value_date_format,
154
				$input[0]['value'],
155
				$this->input_value_date_format
156
			);
157
		}
158
159
		return $query;
160
	}
161
162
	/**
163
	 * Remove the filter.
164
	 *
165
	 * @since  0.3
166
	 * @access public
167
	 */
168
	public function remove_filter() {
169
		remove_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ) );
170
	}
171
}
172