-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime_since.php
193 lines (162 loc) · 5.82 KB
/
time_since.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
class FacetWP_Facet_Time_Since
{
function __construct() {
$this->label = __( 'Time Since', 'fwp' );
}
/**
* Load the available choices
*/
function load_values( $params ) {
global $wpdb;
$facet = $params['facet'];
$where_clause = $params['where_clause'];
$sql = "
SELECT f.facet_display_value
FROM {$wpdb->prefix}facetwp_index f
WHERE f.facet_name = '{$facet['name']}' $where_clause";
$results = $wpdb->get_results( $sql );
// Parse the "choices" setting
$choices = explode( "\n", $facet['choices'] );
foreach ( $choices as $key => $choice ) {
$temp = array_map( 'trim', explode( '|', $choice ) );
$choices[ $key ] = array(
'label' => $temp[0],
'format' => $temp[1],
'seconds' => strtotime( $temp[1] ),
'counter' => 0,
);
}
// Loop through the results
foreach ( $results as $result ) {
$post_time = (int) strtotime( $result->facet_display_value );
foreach ( $choices as $key => $choice ) {
$choice_time = $choice['seconds'];
// last week, etc.
if ( $choice_time < time() && $post_time >= $choice_time ) {
$choices[ $key ]['counter']++;
}
// next week, etc.
elseif ( $choice_time > time() && $post_time <= $choice_time ) {
$choices[ $key ]['counter']++;
}
}
}
// We need to return an array of objects
// Each row having "facet_value", "facet_display_value", and "counter"
$output = array();
foreach ( $choices as $choice ) {
if ( 0 < $choice['counter'] ) {
$output[] = (object) array(
'facet_value' => $choice['seconds'],
'facet_display_value' => $choice['label'],
'counter' => $choice['counter'],
);
}
}
return $output;
}
/**
* Generate the facet HTML
*/
function render( $params ) {
$output = '';
$facet = $params['facet'];
$values = (array) $params['values'];
$selected_values = (array) $params['selected_values'];
$is_empty = empty( $selected_values ) ? ' selected' : '';
$output .= '<div class="facetwp-radio' . $is_empty . '" data-value="">' . __( 'Any', 'fwp' ) . '</div>';
foreach ( $values as $result ) {
$selected = in_array( $result->facet_value, $selected_values ) ? ' selected' : '';
// Determine whether to show counts
$display_value = $result->facet_display_value;
$display_value .= " <span class='counts'>($result->counter)</span>";
$output .= '<div class="facetwp-radio' . $selected . '" data-value="' . $result->facet_value . '">' . $display_value . '</div>';
}
return $output;
}
/**
* Filter the query based on selected values
*/
function filter_posts( $params ) {
global $wpdb;
$facet = $params['facet'];
$selected_values = $params['selected_values'];
$selected_values = is_array( $selected_values ) ? $selected_values[0] : $selected_values;
$selected_values = date( 'Y-m-d H:i:s', (int) $selected_values );
$sql = "
SELECT DISTINCT post_id FROM {$wpdb->prefix}facetwp_index
WHERE facet_name = '{$facet['name']}' AND facet_value >= '$selected_values'";
return $wpdb->get_col( $sql );
}
/**
* Output any admin scripts
*/
function admin_scripts() {
?>
<script>
(function($) {
wp.hooks.addAction('facetwp/load/time_since', function($this, obj) {
$this.find('.facet-source').val(obj.source);
$this.find('.type-time_since .facet-choices').val(obj.choices);
});
wp.hooks.addFilter('facetwp/save/time_since', function($this, obj) {
obj['source'] = $this.find('.facet-source').val();
obj['choices'] = $this.find('.type-time_since .facet-choices').val();
return obj;
});
})(jQuery);
</script>
<?php
}
/**
* Output any front-end scripts
*/
function front_scripts() {
?>
<link href="<?php echo WP_CONTENT_URL; ?>/plugins/facetwp-time-since/assets/css/front.css" rel="stylesheet">
<script>
(function($) {
wp.hooks.addAction('facetwp/refresh/time_since', function($this, facet_name) {
var selected_values = [];
$this.find('.facetwp-radio.selected').each(function() {
var val = $(this).attr('data-value');
if ('' != val) {
selected_values.push(val);
}
});
FWP.facets[facet_name] = selected_values;
});
wp.hooks.addAction('facetwp/ready', function() {
$(document).on('click', '.facetwp-radio', function() {
var $facet = $(this).closest('.facetwp-facet');
$facet.find('.facetwp-radio').removeClass('selected');
$(this).addClass('selected');
if ('' != $(this).attr('data-value')) {
FWP.static_facet = $facet.attr('data-name');
}
FWP.autoload();
});
});
})(jQuery);
</script>
<?php
}
/**
* Output admin settings HTML
*/
function settings_html() {
?>
<tr class="facetwp-conditional type-time_since">
<td>
<?php _e('Choices', 'fwp'); ?>:
<div class="facetwp-tooltip">
<span class="icon-question">?</span>
<div class="facetwp-tooltip-content"><?php _e( 'Enter the available choices (one per line)', 'fwp' ); ?></div>
</div>
</td>
<td><textarea class="facet-choices"></textarea></td>
</tr>
<?php
}
}