Passed
Push — analysis-D2GvQJ ( 9b001d )
by Sudar
71:50 queued 38s
created

DateQueryOverrider   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 43
dl 0
loc 134
rs 10
c 4
b 2
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_query() 0 4 1
A load() 0 2 1
A process_sql_date_format() 0 15 4
B process_date_fields() 0 35 9
A parse_query() 0 6 2
A remove_filter() 0 2 1
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
	 * Setup hooks and load.
32
	 *
33
	 * @since 1.0
34
	 */
35
	public function load() {
36
		add_action( 'parse_comment_query', array( $this, 'parse_query' ) );
37
	}
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
	/**
53
	 * Process date fields and returns query built.
54
	 *
55
	 * @param array $delete_options Delete Options.
56
	 *
57
	 * @return array $options Query.
58
	 */
59
	public function process_date_fields( $delete_options ) {
60
		if ( ! empty( $delete_options['relative_date'] ) && 'custom' !== $delete_options['relative_date'] ) {
61
			$delete_options['meta_value'] = date( 'c', strtotime( $delete_options['relative_date'] ) );
62
		}
63
64
		if ( ! empty( $delete_options['date_unit'] ) && ! empty( $delete_options['date_type'] ) ) {
65
			$interval_unit = $delete_options['date_unit'];
66
			$interval_type = $delete_options['date_type'];
67
68
			switch ( $delete_options['meta_op'] ) {
69
				case '<':
70
				case '<=':
71
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( '-' . $interval_unit . ' ' . $interval_type ) );
72
					break;
73
				default:
74
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( $interval_unit . ' ' . $interval_type ) );
75
			}
76
		}
77
78
		$meta_query = array(
79
			'key'     => $delete_options['meta_key'],
80
			'value'   => $delete_options['meta_value'],
81
			'compare' => $delete_options['meta_op'],
82
			'type'    => $delete_options['meta_type'],
83
		);
84
85
		$options = array( $meta_query );
86
87
		if ( 'DATE' === $meta_query['type'] && ! empty( $delete_options['meta_value_date_format'] ) ) {
88
			$options['bd_meta_value_date_format'] = $delete_options['meta_value_date_format'];
89
90
			$this->load();
91
		}
92
93
		return $options;
94
	}
95
96
	/**
97
	 * Parse the query object.
98
	 *
99
	 * @param \WP_Query $query Query object.
100
	 *
101
	 * @since  0.3
102
	 */
103
	public function parse_query( $query ) {
104
		if ( isset( $query->query_vars['meta_query']['bd_meta_value_date_format'] ) ) {
105
			$this->meta_value_date_format = $query->query_vars['meta_query']['bd_meta_value_date_format'];
106
107
			add_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ), 10, 6 );
108
			add_action( 'bd_after_meta_query', array( $this, 'remove_filter' ) );
109
		}
110
	}
111
112
	/**
113
	 * Process date format in sql query.
114
	 *
115
	 * @param array  $query          Array containing the query's JOIN and WHERE clauses.
116
	 * @param array  $input          Array of meta queries.
117
	 * @param string $type           Type of meta.
118
	 * @param string $primary_table  Primary table.
119
	 * @param string $primary_column Primary column ID.
120
	 * @param object $context        The main query object.
121
	 *
122
	 * @return array $query Processed query.
123
	 *
124
	 * @since 0.3
125
	 */
126
	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

126
	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...
127
		global $wpdb;
128
		if ( 'DATE' === $input[0]['type'] && 'comment' === $type && 'comment_ID' === $primary_column ) {
129
			$meta_table = _get_meta_table( $type );
130
131
			$query['where'] = $wpdb->prepare(
132
				" AND ( $meta_table.meta_key = %s AND STR_TO_DATE($meta_table.meta_value, %s) {$input[0]['compare']} STR_TO_DATE(%s, %s) ) ",
133
				$input[0]['key'],
134
				$this->meta_value_date_format,
135
				$input[0]['value'],
136
				'%Y-%m-%d'
137
			);
138
		}
139
140
		return $query;
141
	}
142
143
	/**
144
	 * Remove meta sql filter.
145
	 *
146
	 * @return void
147
	 */
148
	public function remove_filter() {
149
		remove_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ) );
150
	}
151
}
152