-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-load-more-for-terms.php
324 lines (291 loc) · 11.6 KB
/
ajax-load-more-for-terms.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* Plugin Name: Ajax Load More for Terms
* Plugin URI: http://connekthq.com/plugins/ajax-load-more/extensions/terms/
* Description: An Ajax Load More extension that adds compatibility for loading taxonomy terms via Ajax.
* Text Domain: ajax-load-more-for-terms
* Author: Darren Cooney
* Author URI: https://connekthq.com
* Version: 1.1.1
* License: GPL
* Copyright: Connekt Media & Darren Cooney
*
* @package ajax-load-more-for-terms
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Installation hook.
*/
function alm_terms_install() {
if ( ! is_plugin_active( 'ajax-load-more/ajax-load-more.php' ) ) {
set_transient( 'alm_terms_admin_notice', true, 5 );
}
}
register_activation_hook( __FILE__, 'alm_terms_install' );
/**
* Display admin notice if plugin does not meet the requirements.
*/
function alm_terms_admin_notice() {
$slug = 'ajax-load-more';
// Ajax Load More Notice.
if ( get_transient( 'alm_terms_admin_notice' ) ) {
$install_url = get_admin_url() . '/update.php?action=install-plugin&plugin=' . $slug . '&_wpnonce=' . wp_create_nonce( 'install-plugin_' . $slug );
$message = '<div class="error">';
$message .= '<p>You must install and activate the core Ajax Load More plugin before using the Ajax Load More Terms extension.</p>';
$message .= '<p>' . sprintf( '<a href="%s" class="button-primary">%s</a>', $install_url, 'Install Ajax Load More Now' ) . '</p>';
$message .= '</div>';
echo wp_kses_post( $message );
delete_transient( 'alm_terms_admin_notice' );
}
}
add_action( 'admin_notices', 'alm_terms_admin_notice' );
define( 'ALM_TERMS_PATH', plugin_dir_path( __FILE__ ) );
define( 'ALM_TERMS_URL', plugins_url( '', __FILE__ ) );
if ( ! class_exists( 'ALM_TERMS' ) ) :
/**
* Initiate the class.
*/
class ALM_TERMS {
/**
* Construct class.
*
* @author ConnektMedia <[email protected]>
* @since 1.0
*/
public function __construct() {
add_action( 'alm_terms_installed', [ &$this, 'alm_terms_installed' ] );
add_filter( 'alm_terms_shortcode', [ &$this, 'alm_terms_shortcode' ], 10, 6 );
add_filter( 'alm_terms_preloaded', [ &$this, 'alm_terms_preloaded_query' ], 10, 4 );
add_action( 'wp_ajax_alm_get_terms', [ &$this, 'alm_get_terms_query' ] );
add_action( 'wp_ajax_nopriv_alm_get_terms', [ &$this, 'alm_get_terms_query' ] );
}
/**
* Preloaded Query for Terms.
*
* @param array $args The query args.
* @param string $preloaded_amount The amount to preload.
* @param string $repeater The current Repeater Template name.
* @param string $theme_repeater The current Theme Repeater name.
* @since 1.0
*/
public function alm_terms_preloaded_query( $args, $preloaded_amount, $repeater, $theme_repeater ) {
$id = isset( $args['id'] ) ? sanitize_text_field( $args['id'] ) : '';
$offset = isset( $args['offset'] ) ? sanitize_text_field( $args['offset'] ) : 0;
$preloaded_amount = isset( $preloaded_amount ) ? $preloaded_amount : $args['term_query_number'];
$term_query = isset( $args['term_query'] ) ? $args['term_query'] : false;
$term_query_taxonomy = isset( $term_query['taxonomy'] ) ? sanitize_text_field( trim( $term_query['taxonomy'] ) ) : '';
$term_query_hide_empty = isset( $term_query['hide_empty'] ) ? sanitize_text_field( $term_query['hide_empty'] ) : true;
$term_query_hide_empty = $term_query_hide_empty === 'false' ? false : true;
$term_query_number = isset( $term_query['number'] ) ? sanitize_text_field( $term_query['number'] ) : '5';
$term_query = empty( $term_query_taxonomy ) ? false : $term_query;
if ( $term_query ) {
$args = [
'taxonomy' => explode( ',', $term_query_taxonomy ),
'number' => $term_query_number,
'hide_empty' => $term_query_hide_empty,
'offset' => $offset,
];
/**
* ALM Term Query Filter Hook
*
* @return array
*/
$args = apply_filters( 'alm_term_query_args_' . $id, $args );
// WP_Term_Query.
$alm_term_query = new WP_Term_Query( $args );
// Set ALM Variables.
$alm_found_posts = $this->alm_terms_count( $args, $offset );
$alm_page = 0;
$alm_item = 0;
$alm_current = 0;
$data = '';
if ( $alm_term_query->terms ) {
ob_start();
foreach ( $alm_term_query->terms as $term ) {
++$alm_item;
++$alm_current;
if ( $theme_repeater !== 'null' && has_action( 'alm_get_term_query_theme_repeater' ) ) {
// Theme Repeater.
do_action( 'alm_get_term_query_theme_repeater', $theme_repeater, $alm_found_posts, $alm_page, $alm_item, $alm_current, $term );
} else {
// Repeater.
include alm_get_current_repeater( $repeater, $type );
}
}
$data = ob_get_clean();
}
return [
'data' => $data,
'total' => $alm_found_posts,
];
}
}
/**
* Ajax Query terms and return data to ALM
*
* @since 1.0
*/
public function alm_get_terms_query() {
$form_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_STRING );
$data = isset( $form_data['term_query'] ) ? $form_data['term_query'] : '';
$id = isset( $form_data['id'] ) ? sanitize_text_field( $form_data['id'] ) : '';
$repeater = isset( $form_data['repeater'] ) ? sanitize_text_field( $form_data['repeater'] ) : 'default';
$type = alm_get_repeater_type( $repeater );
$theme_repeater = isset( $form_data['theme_repeater'] ) ? sanitize_text_field( $form_data['theme_repeater'] ) : 'null';
$page = isset( $form_data['page'] ) ? (int) $form_data['page'] : 1;
$offset = isset( $form_data['offset'] ) ? (int) $form_data['offset'] : 0;
$original_offset = $offset;
$canonical_url = isset( $form_data['canonical_url'] ) ? esc_url( $form_data['canonical_url'] ) : $_SERVER['HTTP_REFERER'];
$query_type = isset( $form_data['query_type'] ) ? sanitize_text_field( $form_data['query_type'] ) : 'standard';
// Cache Add-on.
$cache_id = isset( $form_data['cache_id'] ) ? sanitize_text_field( $form_data['cache_id'] ) : '';
$cache_slug = isset( $form_data['cache_slug'] ) && $form_data['cache_slug'] ? sanitize_text_field( $form_data['cache_slug'] ) : '';
$cache_logged_in = isset( $form_data['cache_logged_in'] ) ? sanitize_text_field( $form_data['cache_logged_in'] ) : false;
$do_create_cache = $cache_logged_in === 'true' && is_user_logged_in() ? false : true;
// Preloaded Add-on.
$preloaded = isset( $form_data['preloaded'] ) ? sanitize_text_field( $form_data['preloaded'] ) : false;
$preloaded_amount = 0;
if ( has_action( 'alm_preload_installed' ) && $preloaded === 'true' ) {
$preloaded_amount = isset( $form_data['preloaded_amount'] ) ? sanitize_text_field( $form_data['preloaded_amount'] ) : '5';
$offset = $offset + $preloaded_amount;
}
// Get Term Data.
if ( $data ) {
$term_query = isset( $data['term_query'] ) ? sanitize_text_field( $data['term_query'] ) : false;
$term_query_taxonomy = isset( $data['taxonomy'] ) ? sanitize_text_field( trim( $data['taxonomy'] ) ) : '';
$term_query_hide_empty = isset( $data['hide_empty'] ) ? sanitize_text_field( $data['hide_empty'] ) : true;
$term_query_hide_empty = $term_query_hide_empty === 'false' ? false : true;
$term_query_number = isset( $data['number'] ) ? sanitize_text_field( $data['number'] ) : '5';
$term_query = empty( $term_query_taxonomy ) ? false : $term_query;
$offset = $offset + ( $term_query_number * $page );
if ( $term_query ) {
$args = [
'taxonomy' => explode( ',', $term_query_taxonomy ),
'hide_empty' => $term_query_hide_empty,
'number' => $term_query_number,
'offset' => $offset,
];
/**
* ALM Term Query Filter Hook
*
* @return $args;
*/
$args = apply_filters( 'alm_term_query_args_' . $id, $args );
// WP_Term_Query.
$alm_term_query = new WP_Term_Query( $args );
if ( $query_type === 'totalposts' ) {
// Paging add-on.
$return = [
'totalposts' => $this->alm_terms_count( $args, $original_offset ),
];
} else {
// Standard ALM.
if ( $alm_term_query->terms ) {
// Set ALM Variables.
$alm_found_posts = $this->alm_terms_count( $args, $original_offset );
$alm_post_count = count( $alm_term_query->terms );
$alm_current = 0;
ob_start();
foreach ( $alm_term_query->terms as $term ) {
++$alm_current; // Current item in loop.
$alm_page = $page + 1; // Get page number.
$alm_item = ( $alm_page * $term_query_number ) - $term_query_number + $alm_current + $preloaded_amount;
if ( $theme_repeater !== 'null' && has_action( 'alm_get_term_query_theme_repeater' ) ) {
// Theme Repeater.
do_action( 'alm_get_term_query_theme_repeater', $theme_repeater, $alm_found_posts, $alm_page, $alm_item, $alm_current, $term );
} else {
// Repeater.
include alm_get_current_repeater( $repeater, $type );
}
}
$data = ob_get_clean();
$return = [
'html' => $data,
'meta' => [
'postcount' => $alm_post_count,
'totalposts' => $alm_found_posts,
],
];
/**
* Cache Add-on hook
* If Cache is enabled, check the cache file
*
* @return void
*/
if ( $cache_id && method_exists( 'ALMCache', 'create_cache_file' ) && $do_create_cache ) {
ALMCache::create_cache_file( $cache_id, $cache_slug, $canonical_url, $data, $alm_post_count, $alm_found_posts );
}
} else {
// No Results.
$return = [
'html' => null,
'meta' => [
'postcount' => 0,
'totalposts' => 0,
],
];
}
}
}
}
wp_send_json( $return );
}
/**
* Get a full count of the available terms.
*
* @param array $args The query args.
* @param string $offset The offset count.
* @return int The total terms count.
* @since 1.0
*/
public function alm_terms_count( $args, $offset ) {
$count_args = $args;
$count_args['number'] = 99999;
$count_args['offset'] = $offset;
$count_args['fields'] = 'tt_ids';
$term_query = new WP_Term_Query( $count_args );
return count( $term_query->terms );
}
/**
* Build Term Query shortcode params and send back to core ALM
*
* @param string $term_query The value for terms (true/false).
* @param string $term_query_taxonomy The term taxonomy.
* @param string $term_query_hide_empty Query parameter.
* @param string $term_query_number Query parameter.
* @return string The generated HTML data attributes.
* @since 1.0
*/
public function alm_terms_shortcode( $term_query, $term_query_taxonomy, $term_query_hide_empty, $term_query_number ) {
$return = ' data-term-query="true"';
$return .= ' data-term-query-taxonomy="' . $term_query_taxonomy . '"';
$return .= ' data-term-query-hide-empty="' . $term_query_hide_empty . '"';
$return .= ' data-term-query-number="' . $term_query_number . '"';
return $return;
}
/**
* An empty function to determine if Terms is activated.
*
* @since 1.0
*/
public function alm_terms_installed() {
// phpcs:ignore
// Empty return.
}
}
/**
* The main function responsible for returning the one true ALM_TERMS Instance.
*
* @since 1.0
*/
function alm_terms() {
global $alm_terms;
if ( ! isset( $alm_terms ) ) {
$alm_terms = new ALM_TERMS();
}
return $alm_terms;
}
alm_terms();
endif;