Completed
Pull Request — dev/6.1.0 (#564)
by Sudar
11:39 queued 07:59
created

DateQueryOverrider::parse_query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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