Completed
Pull Request — dev/6.0.0 (#564)
by
unknown
27:27 queued 12:25
created

Bulk_Delete_Date_Handler::get_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
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
 * @author Sudar
7
 *
8
 * @since 6.0
9
 */
10
class Bulk_Delete_Date_Handler {
11
	/**
12
	 * Delete Options.
13
	 *
14
	 * @var array
15
	 */
16
	protected $delete_options;
17
	/**
18
	 * Date format of meta value that is stored in `wp_post_meta` table..
19
	 *
20
	 * @var string
21
	 */
22
	protected $meta_value_date_format;
23
24
	/**
25
	 * Date format of input value that will be compared with meta value.
26
	 *
27
	 * @since 1.0
28
	 *
29
	 * @var string
30
	 */
31
	protected $input_value_date_format;
32
33
34
	/**
35
	 * Creates query object after processing date with specified date format.
36
	 *
37
	 * @param array $delete_options Delete Options.
38
	 * @return \WP_Query $query Query object.
39
	 */
40
	public function get_query( $delete_options ) {
41
		$query = $this->process_date_fields( $delete_options );
42
		return $query;
43
	}
44
45
	public function process_date_fields( $delete_options ) {
46
		if ( ! empty( $delete_options['relative_date'] ) && 'custom' !== $delete_options['relative_date'] ) {
47
			$delete_options['meta_value'] = date( 'c', strtotime( $delete_options['relative_date'] ) );
48
		}
49
50
		if ( ! empty( $delete_options['date_unit'] ) && ! empty( $delete_options['date_type'] ) ) {
51
			$interval_unit = $delete_options['date_unit'];
52
			$interval_type = $delete_options['date_type'];
53
54
			switch ( $delete_options['meta_op'] ) {
55
				case '<':
56
				case '<=':
57
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( '-' . $interval_unit . ' ' . $interval_type ) );
58
					break;
59
				default:
60
					$delete_options['meta_value'] = date( 'Y-m-d', strtotime( $interval_unit . ' ' . $interval_type ) );
61
			}
62
		}
63
64
		// In v1.0 `date_format` was changed to `meta_value_date_format`.
65
		// Needed?
66
		if ( isset( $delete_options['date_format'] ) ) {
67
			$delete_options['meta_value_date_format'] = $delete_options['date_format'];
68
		}
69
		$meta_query = array(
70
			'key'     => $delete_options['meta_key'],
71
			'value'   => $delete_options['meta_value'],
72
			'compare' => $delete_options['meta_op'],
73
			'type'    => $delete_options['meta_type'],
74
		);
75
76
		$options = array(
77
			'meta_query' => array( $meta_query ),
78
		);
79
80
		if ( 'DATE' === $meta_query['type'] && ! empty( $delete_options['meta_value_date_format'] ) ) {
81
			$options['cf_meta_value_date_format'] = $delete_options['meta_value_date_format'];
82
83
			if ( ! empty( $delete_options['input_value_date_format'] ) ) {
84
				$options['cf_input_value_date_format'] = $delete_options['input_value_date_format'];
85
			} else {
86
				$options['cf_input_value_date_format'] = '%Y-%m-%d';
87
			}
88
89
			$this->load();
90
		}
91
		return $options;
92
	}
93
94
	/**
95
	 * Setup hooks and load.
96
	 *
97
	 * @since 1.0
98
	 */
99
	public function load() {
100
		add_action( 'parse_query', array( $this, 'parse_query' ) );
101
	}
102
103
	/**
104
	 * Parse the query object
105
	 *
106
	 * @since  0.3
107
	 *
108
	 * @param \WP_Query $query Query object.
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
	 * @since 0.3
124
	 *
125
	 * @param array  $query          Array containing the query's JOIN and WHERE clauses.
126
	 * @param array  $input          Array of meta queries.
127
	 * @param string $type           Type of meta.
128
	 * @param string $primary_table  Primary table.
129
	 * @param string $primary_column Primary column ID.
130
	 * @param object $context        The main query object.
131
	 *
132
	 * @return array $query Processed query.
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( " AND ( $meta_table.meta_key = %s AND STR_TO_DATE($meta_table.meta_value, %s) {$input[0]['compare']} STR_TO_DATE(%s, %s) ) ",
141
				$input[0]['key'],
142
				$this->meta_value_date_format,
143
				$input[0]['value'],
144
				$this->input_value_date_format
145
			);
146
		}
147
		return $query;
148
	}
149
150
	/**
151
	 * Remove the filter
152
	 *
153
	 * @since 0.3
154
	 * @access public
155
	 */
156
	public function remove_filter() {
157
		remove_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ) );
158
	}
159
}
160