|
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 ) { |
|
|
|
|
|
|
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
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.