-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-load-more-for-acf.php
422 lines (372 loc) · 14.7 KB
/
ajax-load-more-for-acf.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
/**
* Plugin Name: Ajax Load More for ACF
* Plugin URI: http://connekthq.com/plugins/ajax-load-more/extensions/advanced-custom-fields/
* Description: An Ajax Load More extension that adds compatibility for ACF field types.
* Text Domain: ajax-load-more-for-acf
* Author: Darren Cooney
* Author URI: https://connekthq.com
* Version: 1.3.3
* License: GPL
* Copyright: Connekt Media & Darren Cooney
*
* @package ALM_ACF
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'ALM_ACF_PATH', plugin_dir_path( __FILE__ ) );
define( 'ALM_ACF_URL', plugins_url( '', __FILE__ ) );
/**
* Installation hook.
*/
function alm_acf_install() {
if ( ! is_plugin_active( 'ajax-load-more/ajax-load-more.php' ) ) {
set_transient( 'alm_acf_admin_notice', true, 5 );
}
}
register_activation_hook( __FILE__, 'alm_acf_install' );
/**
* Display admin notice if plugin does not meet the requirements.
*/
function alm_acf_admin_notice() {
$slug = 'ajax-load-more';
$plugin = $slug . '-for-acf';
// Ajax Load More Notice.
if ( get_transient( 'alm_acf_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 ACF 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_acf_admin_notice' );
}
}
add_action( 'admin_notices', 'alm_acf_admin_notice' );
if ( ! class_exists( 'ALM_ACF' ) ) :
/**
* Initiate the class.
*/
class ALM_ACF {
/**
* Set up contructors.
*/
public function __construct() {
add_action( 'alm_acf_installed', [ &$this, 'alm_acf_installed' ] );
add_filter( 'alm_acf_shortcode', [ &$this, 'alm_acf_shortcode' ], 10, 7 );
add_filter( 'alm_acf_preloaded', [ &$this, 'alm_acf_preloaded_query' ], 10, 3 );
add_action( 'wp_ajax_alm_acf', [ &$this, 'alm_acf_query' ] );
add_action( 'wp_ajax_nopriv_alm_acf', [ &$this, 'alm_acf_query' ] );
add_filter( 'alm_acf_total_rows', [ &$this, 'alm_acf_total_rows' ], 10, 1 );
$this->alm_acf_includes();
}
/**
* Load these files before the theme loads
*
* @since 1.3.0
*/
public function alm_acf_includes() {
include_once ALM_ACF_PATH . 'functions.php';
}
/**
* Count total rows for Repeater field.
*
* @see /ajax-load-more/classes/class.alm-shortcode
*
* @param array $args The query arguments.
* @return mixed The total rows.
* @since 1.0
*/
public function alm_acf_total_rows( $args ) {
$acf = isset( $args['acf'] ) ? $args['acf'] : false; // true / false.
$post_id = isset( $args['acf_post_id'] ) ? $args['acf_post_id'] : ''; // Post ID.
$field_type = isset( $args['acf_field_type'] ) ? $args['acf_field_type'] : 'repeater'; // ACF Field Type.
$field_name = isset( $args['acf_field_name'] ) ? $args['acf_field_name'] : ''; // ACF Field Name.
$parent_field_name = isset( $args['acf_parent_field_name'] ) ? $args['acf_parent_field_name'] : ''; // ACF Parent Field Name.
$row_index = isset( $args['acf_row_index'] ) ? (int) $args['acf_row_index'] : 0; // ACF Row Index.
$total = 0;
if ( empty( $post_id ) ) {
$post_id = isset( $args['post_id'] ) ? $args['post_id'] : ''; // Backup Post ID.
}
if ( $acf && $post_id && ( $field_type !== 'relationship' ) ) {
if ( $parent_field_name ) {
switch ( $field_type ) {
case 'repeater':
case 'flexible':
$total = alm_acf_loop_repeater_rows( 'count', $parent_field_name, $field_name, $post_id, $row_index );
break;
case 'gallery':
$total = alm_acf_loop_gallery_rows( 'count', $parent_field_name, $field_name, $post_id, $row_index );
break;
}
} else {
$total = count( get_field( $field_name, $post_id ) );
}
}
return $total;
}
/**
* Preloaded Query for Advanced Custom Fields.
*
* @param array $args The query arguments.
* @param string $repeater The current Repeater Template name.
* @param string $theme_repeater The current Theme Repeater name.
* @since 1.0
*/
public function alm_acf_preloaded_query( $args, $repeater, $theme_repeater ) {
$acf_data = '';
$acf = isset( $args['acf'] ) ? $args['acf'] : false; // true / false.
$acf_post_id = isset( $args['acf_post_id'] ) ? $args['acf_post_id'] : ''; // Post ID.
$field_type = isset( $args['acf_field_type'] ) ? $args['acf_field_type'] : 'repeater'; // ACF Field Type.
$field_name = isset( $args['acf_field_name'] ) ? $args['acf_field_name'] : ''; // ACF Field Name.
$parent_field_name = isset( $args['acf_parent_field_name'] ) ? $args['acf_parent_field_name'] : ''; // ACF Parent Field Name.
$row_index = isset( $args['acf_row_index'] ) ? (int) $args['acf_row_index'] : 0; // ACF Row Index.
$posts_per_page = isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : 5;
$offset = isset( $args['offset'] ) ? (int) $args['offset'] : 0;
$max_pages = $posts_per_page + $offset;
// Check for empty ACF post ID.
$post_id = empty( $acf_post_id ) ? $args['post_id'] : $acf_post_id;
switch ( $field_type ) {
case 'repeater':
case 'flexible':
$options = [
'posts_per_page' => $posts_per_page,
'offset' => $offset,
'max_pages' => $max_pages,
'repeater' => $repeater,
'theme_repeater' => $theme_repeater,
'is_preloaded' => true,
'preloaded' => 'true',
];
$acf_data = alm_acf_loop_repeater_rows( 'query', $parent_field_name, $field_name, $post_id, $options, $row_index );
break;
case 'gallery':
$images = alm_acf_loop_gallery_rows( 'query', $parent_field_name, $field_name, $post_id, $row_index );
if ( $images ) {
$total = count( $images );
$max_pages = $posts_per_page + $offset;
$row_count = 0;
ob_start();
foreach ( $images as $key => $image ) :
// Start displaying rows after the offset.
if ( $key >= $offset ) {
// Exit when rows exceeds max pages.
if ( $key >= $max_pages ) {
break; // exit early.
}
$row_count++;
// Set ALM Variables.
$alm_found_posts = $total;
$alm_page = 1;
$alm_item = $row_count;
$alm_current = $alm_item;
if ( $theme_repeater !== 'null' && has_action( 'alm_get_acf_gallery_theme_repeater' ) ) {
// Theme Repeater.
do_action( 'alm_get_acf_gallery_theme_repeater', $theme_repeater, $alm_found_posts, $alm_page, $alm_item, $alm_current, $image );
} else {
// Repeater.
$type = alm_get_repeater_type( $repeater );
include alm_get_current_repeater( $repeater, $type );
}
}
endforeach;
$acf_data = ob_get_clean();
}
break;
}
return $acf_data;
}
/**
* Ajax Query ACF fields and return data to ALM.
*
* @return mixed Returns the contents of the output buffer and end output buffering. If output buffering isn't active then false is returned.
* @since 1.0
*/
public function alm_acf_query() {
$form_data = filter_input_array( INPUT_GET );
$data = isset( $form_data['acf'] ) ? $form_data['acf'] : ''; // Get $acf object array.
$repeater = isset( $form_data['repeater'] ) ? $form_data['repeater'] : 'default';
$type = alm_get_repeater_type( $repeater );
$theme_repeater = isset( $form_data['theme_repeater'] ) ? $form_data['theme_repeater'] : 'null';
$posts_per_page = isset( $form_data['posts_per_page'] ) ? $form_data['posts_per_page'] : 5;
$page = isset( $form_data['page'] ) ? $form_data['page'] : 1;
$offset = isset( $form_data['offset'] ) ? $form_data['offset'] : 0;
$canonical_url = isset( $form_data['canonical_url'] ) ? $form_data['canonical_url'] : $_SERVER['HTTP_REFERER'];
$query_type = isset( $form_data['query_type'] ) ? $form_data['query_type'] : 'standard'; // 'standard' or 'totalposts'. // phpcs:ignore
// Cache Add-on.
$cache_id = isset( $form_data['cache_id'] ) ? $form_data['cache_id'] : '';
$cache_slug = isset( $form_data['cache_slug'] ) && $form_data['cache_slug'] ? $form_data['cache_slug'] : '';
$cache_logged_in = isset( $form_data['cache_logged_in'] ) ? $form_data['cache_logged_in'] : false;
$do_create_cache = $cache_logged_in === 'true' && is_user_logged_in() ? false : true;
// Preload Add-on.
$preloaded = isset( $form_data['preloaded'] ) ? $form_data['preloaded'] : false;
$preloaded_amount = isset( $form_data['preloaded_amount'] ) ? (int) $form_data['preloaded_amount'] : 5;
if ( has_action( 'alm_preload_installed' ) && $preloaded === 'true' ) {
// Preloaded: Offset by posts_per_page + preload_amount.
$offset = $offset + $preloaded_amount;
}
// SEO Add-on.
$seo_start_page = isset( $form_data['seo_start_page'] ) ? $form_data['seo_start_page'] : 1;
// Default variables.
$postcount = 0;
$totalposts = 0;
$no_results_data = [
'html' => '',
'meta' => [
'postcount' => 0,
'totalposts' => 0,
],
];
if ( $data ) {
$acf_data = '';
$acf = isset( $data['acf'] ) ? $data['acf'] : false; // true / false.
$post_id = isset( $data['post_id'] ) ? $data['post_id'] : ''; // Post ID.
$field_type = isset( $data['field_type'] ) ? $data['field_type'] : 'repeater'; // ACF Field Type.
$field_name = isset( $data['field_name'] ) ? $data['field_name'] : ''; // ACF Field Name.
$parent_field_name = isset( $data['parent_field_name'] ) ? $data['parent_field_name'] : ''; // ACF Parent Field Name.
$row_index = isset( $data['row_index'] ) ? (int) $data['row_index'] : false; // ACF Row Index.
$max_pages = 999;
if ( empty( $field_name ) || empty( $post_id ) ) {
$acf = false; // If field_name and post_id are not set, exit.
}
if ( $acf && $post_id ) {
switch ( $field_type ) {
case 'repeater':
case 'flexible':
$options = [
'page' => $page,
'posts_per_page' => $posts_per_page,
'offset' => $offset,
'max_pages' => $max_pages,
'repeater' => $repeater,
'theme_repeater' => $theme_repeater,
'is_preloaded' => false,
'preloaded' => $preloaded,
];
$data = alm_acf_loop_repeater_rows( 'query', $parent_field_name, $field_name, $post_id, $options, $row_index );
// Parse return data.
$acf_data = $data && $data['content'] ? $data['content'] : '';
$postcount = $data && $data['postcount'] ? $data['postcount'] : '';
$total = $data && $data['totalposts'] ? $data['totalposts'] : '';
break;
case 'gallery':
$images = alm_acf_loop_gallery_rows( 'query', $parent_field_name, $field_name, $post_id, $row_index ); // Get Images.
if ( $images ) {
$total = count( $images );
$per_page = ( $posts_per_page * $page ) + 1;
$start = ( $posts_per_page * $page ) + $offset;
$end = $start + $posts_per_page;
$count = 0;
ob_start();
foreach ( $images as $image ) :
// Only display rows between the values.
if ( $postcount < $posts_per_page && $count >= $start ) {
$postcount++;
// Set ALM Variables.
$alm_found_posts = $total;
$alm_page = $page + 1;
$alm_item = $count + 1;
$alm_current = $postcount + 1;
if ( $theme_repeater !== 'null' && has_action( 'alm_get_acf_gallery_theme_repeater' ) ) {
// Theme Repeater.
do_action( 'alm_get_acf_gallery_theme_repeater', $theme_repeater, $alm_found_posts, $alm_page, $alm_item, $alm_current, $image );
} else {
// Repeater.
include alm_get_current_repeater( $repeater, $type );
}
}
$count++;
if ( $count >= $end ) {
break; // exit.
}
endforeach;
$acf_data = ob_get_clean();
}
break;
}
if ( $query_type === 'totalposts' ) {
wp_send_json(
[
'totalposts' => $total,
]
);
}
if ( $acf_data ) {
$return = [
'html' => $acf_data,
'meta' => [
'postcount' => $postcount,
'totalposts' => $total,
],
];
/**
* Cache Add-on.
* Create the cache file.
*/
if ( $cache_id && method_exists( 'ALMCache', 'create_cache_file' ) && $do_create_cache ) {
ALMCache::create_cache_file( $cache_id, $cache_slug, $canonical_url, $acf_data, $postcount, $total );
}
} else {
$return = $no_results_data;
}
wp_send_json( $return );
}
} else {
// No results.
if ( $query_type === 'totalposts' ) {
wp_send_json(
[
'totalposts' => 0,
]
);
} else {
wp_send_json( $no_results_data );
}
wp_die();
}
}
/**
* Build ACF shortcode params and send back to core ALM as data attributes.
*
* @param string $acf The value for ACF (true/false).
* @param string $field_type The ACF field type.
* @param string $field_name The ACF field name.
* @param string $acf_post_id The ACF post ID passed via params.
* @param string $post_id The current post ID.
* @param string $parent_field_name The parent field name.
* @param string $row_index The row index.
* @return string The generated HTML data attributes.
* @since 1.0
*/
public function alm_acf_shortcode( $acf, $field_type, $field_name, $acf_post_id, $post_id, $parent_field_name, $row_index = 0 ) {
$data = ' data-acf="' . $acf . '"';
$data .= ' data-acf-field-type="' . $field_type . '"';
$data .= ' data-acf-field-name="' . $field_name . '"';
if ( $parent_field_name ) {
$data .= ' data-acf-parent-field-name="' . $parent_field_name . '"';
if ( $row_index ) {
$data .= ' data-acf-row-index="' . $row_index . '"';
}
}
if ( empty( $acf_post_id ) ) {
$acf_post_id = $post_id;
}
$data .= ' data-acf-post-id="' . $acf_post_id . '"';
return $data;
}
}
/**
* The main function responsible for returning the one true ALM_ACF Instance.
*
* @since 1.0
*/
function alm_acf() {
global $alm_acf;
if ( ! isset( $alm_acf ) ) {
$alm_acf = new ALM_ACF();
}
return $alm_acf;
}
alm_acf();
endif;