Passed
Pull Request — dev/6.0.0 (#564)
by
unknown
49:57 queued 45:16
created

Bulk_Delete_Date_Handler::get_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class that encapsulates the logic for handling date format specified by the user.
4
 *
5
 * @package Bulk Delete
6
 *
7
 * @author Sudar
8
 *
9
 * @since 6.0
10
 */
11
class Bulk_Delete_Date_Handler {
12
	/**
13
	 * Delete Options.
14
	 *
15
	 * @var array
16
	 */
17
	protected $delete_options;
18
	/**
19
	 * Date format of meta value that is stored in `wp_post_meta` table..
20
	 *
21
	 * @var string
22
	 */
23
	protected $meta_value_date_format;
24
25
	/**
26
	 * Date format of input value that will be compared with meta value.
27
	 *
28
	 * @since 1.0
29
	 *
30
	 * @var string
31
	 */
32
	protected $input_value_date_format;
33
34
	/**
35
	 * Creates query object after processing date with specified date format.
36
	 *
37
	 * @param array $delete_options Delete Options.
38
	 *
39
	 * @return \WP_Query $query Query object.
40
	 */
41
	public function get_query( $delete_options ) {
42
		$query = $this->process_date_fields( $delete_options );
43
44
		return $query;
45
	}
46
47
	public function process_date_fields( $delete_options ) {
48
		if ( ! empty( $delete_options['relative_date'] ) && 'custom' !== $delete_options['relative_date'] ) {
49
			$delete_options['meta_value'] = date( 'c', strtotime( $delete_options['relative_date'] ) );
50
		}
51
52
		if ( ! empty( $delete_options['date_unit'] ) && ! empty( $delete_options['date_type'] ) ) {
53
			$interval_unit = $delete_options['date_unit'];
54
			$interval_type = $delete_options['date_type'];
55
56
			switch ( $delete_options['meta_op'] ) {
57
				case '<':
58
				case '<=':
59
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( '-' . $interval_unit . ' ' . $interval_type ) );
60
					break;
61
				default:
62
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( $interval_unit . ' ' . $interval_type ) );
63
			}
64
		}
65
66
		// In v1.0 `date_format` was changed to `meta_value_date_format`.
67
		// Needed?
68
		if ( isset( $delete_options['date_format'] ) ) {
69
			$delete_options['meta_value_date_format'] = $delete_options['date_format'];
70
		}
71
		$meta_query = array(
72
			'key'     => $delete_options['meta_key'],
73
			'value'   => $delete_options['meta_value'],
74
			'compare' => $delete_options['meta_op'],
75
			'type'    => $delete_options['meta_type'],
76
		);
77
78
		$options = array(
79
			'meta_query' => array( $meta_query ),
80
		);
81
82
		if ( 'DATE' === $meta_query['type'] && ! empty( $delete_options['meta_value_date_format'] ) ) {
83
			$options['cf_meta_value_date_format'] = $delete_options['meta_value_date_format'];
84
85
			if ( ! empty( $delete_options['input_value_date_format'] ) ) {
86
				$options['cf_input_value_date_format'] = $delete_options['input_value_date_format'];
87
			} else {
88
				$options['cf_input_value_date_format'] = '%Y-%m-%d';
89
			}
90
91
			$this->load();
92
		}
93
94
		return $options;
95
	}
96
97
	/**
98
	 * Setup hooks and load.
99
	 *
100
	 * @since 1.0
101
	 */
102
	public function load() {
103
		add_action( 'parse_query', array( $this, 'parse_query' ) );
104
	}
105
106
	/**
107
	 * Parse the query object.
108
	 *
109
	 * @since  0.3
110
	 *
111
	 * @param \WP_Query $query Query object.
112
	 */
113
	public function parse_query( $query ) {
114
		if ( isset( $query->query_vars['cf_meta_value_date_format'] ) ) {
115
			$this->meta_value_date_format  = $query->query_vars['cf_meta_value_date_format'];
116
			$this->input_value_date_format = $query->query_vars['cf_input_value_date_format'];
117
118
			add_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ), 10, 6 );
119
			add_filter( 'posts_selection', array( $this, 'remove_filter' ) );
120
		}
121
	}
122
123
	/**
124
	 * Process date format in sql query.
125
	 *
126
	 * @since 0.3
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
	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

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