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( 'c', strtotime( '-' . $interval_unit . ' ' . $interval_type ) ); |
58
|
|
|
break; |
59
|
|
|
default: |
60
|
|
|
$delete_options['meta_value'] = date( 'c', strtotime( $interval_unit . ' ' . $interval_type ) ); |
61
|
|
|
break; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// In v1.0 `date_format` was changed to `meta_value_date_format`. |
66
|
|
|
// Needed? |
67
|
|
|
if ( isset( $delete_options['date_format'] ) ) { |
68
|
|
|
$delete_options['meta_value_date_format'] = $delete_options['date_format']; |
69
|
|
|
} |
70
|
|
|
$meta_query = array( |
71
|
|
|
'key' => $delete_options['meta_key'], |
72
|
|
|
'value' => $delete_options['meta_value'], |
73
|
|
|
'compare' => $delete_options['meta_op'], |
74
|
|
|
'type' => $delete_options['meta_type'], |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$options = array( |
78
|
|
|
'meta_query' => array( $meta_query ), |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
if ( 'DATE' === $meta_query['type'] && ! empty( $delete_options['meta_value_date_format'] ) ) { |
82
|
|
|
$options['cf_meta_value_date_format'] = $delete_options['meta_value_date_format']; |
83
|
|
|
|
84
|
|
|
if ( ! empty( $delete_options['input_value_date_format'] ) ) { |
85
|
|
|
$options['cf_input_value_date_format'] = $delete_options['input_value_date_format']; |
86
|
|
|
} else { |
87
|
|
|
$options['cf_input_value_date_format'] = '%Y-%m-%d'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->load(); |
91
|
|
|
} |
92
|
|
|
return $options; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Setup hooks and load. |
97
|
|
|
* |
98
|
|
|
* @since 1.0 |
99
|
|
|
*/ |
100
|
|
|
public function load() { |
101
|
|
|
add_action( 'parse_query', array( $this, 'parse_query' ) ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Parse the query object |
106
|
|
|
* |
107
|
|
|
* @since 0.3 |
108
|
|
|
* |
109
|
|
|
* @param \WP_Query $query Query object. |
110
|
|
|
*/ |
111
|
|
|
public function parse_query( $query ) { |
112
|
|
|
if ( isset( $query->query_vars['cf_meta_value_date_format'] ) ) { |
113
|
|
|
$this->meta_value_date_format = $query->query_vars['cf_meta_value_date_format']; |
114
|
|
|
$this->input_value_date_format = $query->query_vars['cf_input_value_date_format']; |
115
|
|
|
|
116
|
|
|
add_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ), 10, 6 ); |
117
|
|
|
add_filter( 'posts_selection', array( $this, 'remove_filter' ) ); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Process date format in sql query. |
123
|
|
|
* |
124
|
|
|
* @since 0.3 |
125
|
|
|
* |
126
|
|
|
* @param array $query Array containing the query's JOIN and WHERE clauses. |
127
|
|
|
* @param array $input Array of meta queries. |
128
|
|
|
* @param string $type Type of meta. |
129
|
|
|
* @param string $primary_table Primary table. |
130
|
|
|
* @param string $primary_column Primary column ID. |
131
|
|
|
* @param object $context The main query object. |
132
|
|
|
* |
133
|
|
|
* @return array $query Processed query. |
134
|
|
|
*/ |
135
|
|
|
public function process_sql_date_format( $query, $input, $type, $primary_table, $primary_column, $context ) { |
|
|
|
|
136
|
|
|
global $wpdb; |
137
|
|
|
|
138
|
|
|
if ( 'DATE' === $input[0]['type'] && 'post' === $type && 'wp_posts' === $primary_table && 'ID' === $primary_column ) { |
139
|
|
|
$meta_table = _get_meta_table( $type ); |
140
|
|
|
|
141
|
|
|
$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) ) ", |
142
|
|
|
$input[0]['key'], |
143
|
|
|
$this->meta_value_date_format, |
144
|
|
|
$input[0]['value'], |
145
|
|
|
$this->input_value_date_format |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $query; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Remove the filter |
154
|
|
|
* |
155
|
|
|
* @since 0.3 |
156
|
|
|
* @access public |
157
|
|
|
*/ |
158
|
|
|
public function remove_filter() { |
159
|
|
|
remove_filter( 'get_meta_sql', array( $this, 'process_sql_date_format' ) ); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.