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
|
|
|
* Meta belongs to which table. It can be post,comment or user. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $whose_meta; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Setup hooks and load. |
38
|
|
|
* |
39
|
|
|
* @since 1.0 |
40
|
|
|
*/ |
41
|
1 |
|
public function load() { |
42
|
1 |
|
if ( 'comment' === $this->whose_meta ) { |
43
|
1 |
|
add_action( 'parse_comment_query', array( $this, 'parse_query' ) ); |
44
|
|
|
} elseif ( 'post' === $this->whose_meta ) { |
45
|
|
|
add_action( 'parse_query', array( $this, 'parse_query' ) ); |
46
|
|
|
} elseif ( 'user' === $this->whose_meta ) { |
47
|
|
|
add_action( 'pre_user_query', array( $this, 'parse_query' ) ); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates query object after processing date with specified date format. |
53
|
|
|
* |
54
|
|
|
* @param array $delete_options Delete Options. |
55
|
|
|
* |
56
|
|
|
* @return \WP_Query $query Query object. |
57
|
|
|
*/ |
58
|
7 |
|
public function get_query( $delete_options ) { |
59
|
7 |
|
$query = $this->process_date_fields( $delete_options ); |
60
|
|
|
|
61
|
7 |
|
return $query; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Process date fields and returns query built. |
66
|
|
|
* |
67
|
|
|
* @param array $delete_options Delete Options. |
68
|
|
|
* |
69
|
|
|
* @return array $options Query. |
70
|
|
|
*/ |
71
|
7 |
|
public function process_date_fields( $delete_options ) { |
72
|
7 |
|
if ( ! empty( $delete_options['relative_date'] ) && 'custom' !== $delete_options['relative_date'] ) { |
73
|
1 |
|
$delete_options['meta_value'] = date( 'c', strtotime( $delete_options['relative_date'] ) ); |
74
|
|
|
} |
75
|
|
|
|
76
|
7 |
|
if ( ! empty( $delete_options['date_unit'] ) && ! empty( $delete_options['date_type'] ) ) { |
77
|
2 |
|
$interval_unit = $delete_options['date_unit']; |
78
|
2 |
|
$interval_type = $delete_options['date_type']; |
79
|
|
|
|
80
|
2 |
|
switch ( $delete_options['meta_op'] ) { |
81
|
|
|
case '<': |
82
|
|
|
case '<=': |
83
|
|
|
$delete_options['meta_value'] = date( 'Y-m-d', strtotime( '-' . $interval_unit . ' ' . $interval_type ) ); |
84
|
|
|
break; |
85
|
|
|
default: |
86
|
2 |
|
$delete_options['meta_value'] = date( 'Y-m-d', strtotime( $interval_unit . ' ' . $interval_type ) ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$meta_query = array( |
91
|
7 |
|
'key' => $delete_options['meta_key'], |
92
|
7 |
|
'value' => $delete_options['meta_value'], |
93
|
7 |
|
'compare' => $delete_options['meta_op'], |
94
|
7 |
|
'type' => $delete_options['meta_type'], |
95
|
|
|
); |
96
|
|
|
|
97
|
7 |
|
$options = array( $meta_query ); |
98
|
|
|
|
99
|
7 |
|
if ( 'DATE' === $meta_query['type'] && ! empty( $delete_options['meta_value_date_format'] ) ) { |
100
|
1 |
|
$options['bd_meta_value_date_format'] = $delete_options['meta_value_date_format']; |
101
|
1 |
|
$this->whose_meta = $delete_options['whose_meta']; |
102
|
|
|
|
103
|
1 |
|
$this->load(); |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
return $options; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Parse the query object. |
111
|
|
|
* |
112
|
|
|
* @param \WP_Query $query Query object. |
113
|
|
|
* |
114
|
|
|
* @since 0.3 |
115
|
|
|
*/ |
116
|
1 |
|
public function parse_query( $query ) { |
117
|
1 |
|
if ( isset( $query->query_vars['meta_query']['bd_meta_value_date_format'] ) ) { |
118
|
1 |
|
$this->meta_value_date_format = $query->query_vars['meta_query']['bd_meta_value_date_format']; |
119
|
|
|
|
120
|
1 |
|
add_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ), 10, 6 ); |
121
|
1 |
|
add_action( 'bd_after_meta_query', array( $this, 'remove_filter' ) ); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Process date format in sql query. |
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
|
|
|
* @since 0.3 |
138
|
|
|
*/ |
139
|
1 |
|
public function process_sql_date_format( $query, $input, $type, $primary_table, $primary_column, $context ) { |
|
|
|
|
140
|
|
|
global $wpdb; |
141
|
1 |
|
if ( 'DATE' === $input[0]['type'] && $this->whose_meta === $type && 'comment_ID' === $primary_column ) { |
142
|
1 |
|
$meta_table = _get_meta_table( $type ); |
143
|
1 |
|
if ( 'unixtimestamp' === strtolower( str_replace( ' ', '', $this->meta_value_date_format ) ) ) { |
144
|
|
|
$query['where'] = $wpdb->prepare( |
145
|
|
|
" AND ( $meta_table.meta_key = %s AND FROM_UNIXTIME($meta_table.meta_value, %s) {$input[0]['compare']} STR_TO_DATE(%s, %s) ) ", |
146
|
|
|
$input[0]['key'], |
147
|
|
|
'%Y-%m-%d', // Meta value format. |
148
|
|
|
$input[0]['value'], |
149
|
|
|
'%Y-%m-%d' |
150
|
|
|
); |
151
|
|
|
} else { |
152
|
1 |
|
$query['where'] = $wpdb->prepare( |
153
|
1 |
|
" AND ( $meta_table.meta_key = %s AND STR_TO_DATE($meta_table.meta_value, %s) {$input[0]['compare']} STR_TO_DATE(%s, %s) ) ", |
154
|
1 |
|
$input[0]['key'], |
155
|
1 |
|
$this->meta_value_date_format, |
156
|
1 |
|
$input[0]['value'], |
157
|
1 |
|
'%Y-%m-%d' |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $query; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Remove meta sql filter. |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
1 |
|
public function remove_filter() { |
171
|
1 |
|
remove_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ) ); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.