-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
322 lines (299 loc) · 9.97 KB
/
functions.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
<?php
/**
* General ACF functions and helpers.
*
* @package ALM_ACF
*/
/**
* Access ACF fields for Repeater and Flexible Content field types.
*
* @param string $type The type of data to return (query/total).
* @param string $parent_field_name The ACF parent field name.
* @param string $field_name The ACF field name.
* @param string $id The post ID.
* @param mixed $options Various config options.
* @param int $row_index The row index to access data.
* @return mixed Total posts count or an array of data containing repeater rows.
* @since 1.3.0
*/
function alm_acf_loop_repeater_rows( $type = 'query', $parent_field_name = '', $field_name = '', $id = '', $options = '', $row_index = 0 ) {
if ( ! $field_name || ! $id ) {
return ''; // Bail early if empty.
}
$content = '';
$total = 0;
if ( empty( $parent_field_name ) ) {
// Standard Field.
$total = count( get_field( $field_name, $id ) );
if ( $type === 'query' ) {
$content = alm_acf_get_repeater_fields( $field_name, $id, $options, $total );
}
} elseif ( $parent_field_name && $row_index > 0 ) {
// Row index: Access sub fields by row index.
if ( have_rows( $parent_field_name, $id ) ) :
while ( have_rows( $parent_field_name, $id ) ) :
the_row();
if ( get_row_index() === $row_index && get_sub_field( $field_name, $id ) ) {
$total = count( get_sub_field( $field_name, $id ) );
if ( $type === 'query' ) {
$content = alm_acf_get_repeater_fields( $field_name, $id, $options, $total, $parent_field_name, $row_index );
}
}
endwhile;
endif;
} else {
// Parent field: Access sub field data by parent field name.
$parent = explode( ':', $parent_field_name ); // Split into array.
$parent_count = count( $parent );
// Loop sub fields to get at the field.
if ( $parent_count == 1 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
if ( get_sub_field( $field_name, $id ) ) {
$total = count( get_sub_field( $field_name, $id ) );
if ( $type === 'query' ) {
$content = alm_acf_get_repeater_fields( $field_name, $id, $options, $total );
}
}
endwhile;
}
if ( $parent_count == 2 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
while ( have_rows( $parent[1], $id ) ) :
the_row();
$total = count( get_sub_field( $field_name, $id ) );
if ( $type === 'query' ) {
$content = alm_acf_get_repeater_fields( $field_name, $id, $options, $total );
}
endwhile;
endwhile;
}
if ( $parent_count == 3 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
while ( have_rows( $parent[1], $id ) ) :
the_row();
while ( have_rows( $parent[2], $id ) ) :
the_row();
$total = count( get_sub_field( $field_name, $id ) );
if ( $type === 'query' ) {
$content = alm_acf_get_repeater_fields( $field_name, $id, $options, $total );
}
endwhile;
endwhile;
endwhile;
}
}
// If count, return the count.
return $type === 'count' ? $total : $content;
}
/**
* Get the fields for Repeater and Flexible Content fields
*
* @param string $field_name The ACF field name.
* @param string $id The post ID.
* @param mixed $options Optional options to pass.
* @param number $total The total posts.
* @param string $parent_field_name The ACF parent field name.
* @param int $row_index The row index to access data.
* @since 1.3.0
*/
function alm_acf_get_repeater_fields( $field_name, $id, $options, $total = 0, $parent_field_name = '', $row_index = 0 ) {
if ( ! $field_name || ! $id ) {
return ''; // Bail early if empty.
}
// Set initial variables.
$content = '';
$data = '';
$postcount = 0;
$row_count = 0;
$preloaded = isset( $options['is_preloaded'] ) && $options['is_preloaded'];
if ( ! have_rows( $field_name, $id ) ) {
return '';
}
if ( $preloaded ) {
ob_start();
while ( have_rows( $field_name, $id ) ) :
the_row();
// Start displaying rows after the offset.
if ( $row_count >= $options['offset'] ) {
if ( $row_count >= $options['max_pages'] ) {
// Exit when rows exceeds max pages.
break;
}
// Set ALM Variables.
$alm_found_posts = $total;
$alm_page = 1;
$alm_item = $row_count + 1;
$alm_current = $alm_item;
if ( $options['theme_repeater'] !== 'null' && has_action( 'alm_get_theme_repeater' ) ) {
// Theme Repeater.
do_action( 'alm_get_theme_repeater', $options['theme_repeater'], $alm_found_posts, $alm_page, $alm_item, $alm_current );
} else {
// Repeater.
$type = alm_get_repeater_type( $options['repeater'] );
include alm_get_current_repeater( $options['repeater'], $type );
}
}
$row_count++;
endwhile;
return ob_get_clean();
} else {
// Standard.
$per_page = ( $options['posts_per_page'] * $options['page'] ) + 1;
$start = ( $options['posts_per_page'] * $options['page'] ) + $options['offset'];
$end = $start + $options['posts_per_page'];
$page = isset( $options['page'] ) ? $options['page'] : 1;
$preloaded = isset( $options['preloaded'] ) ? $options['preloaded'] : 'false';
$page = $preloaded === 'true' ? $page + 1 : $page; // If preloaded, add 1 to $page.
$count = 0;
$row_counter = 0;
ob_start();
while ( have_rows( $field_name, $id ) ) :
the_row();
// Only display rows between the values.
if ( $row_counter < $options['posts_per_page'] && $count >= $start ) {
// Increase row counter.
$row_counter++;
// Set ALM Variables.
$alm_found_posts = $total;
$alm_page = $page + 1;
$alm_item = $count + 1;
$alm_current = $row_counter + 1;
if ( $options['theme_repeater'] !== 'null' && has_action( 'alm_get_theme_repeater' ) ) {
do_action( 'alm_get_theme_repeater', $options['theme_repeater'], $alm_found_posts, $alm_page, $alm_item, $alm_current );
} else {
$type = alm_get_repeater_type( $options['repeater'] );
include alm_get_current_repeater( $options['repeater'], $type );
}
}
$count++;
if ( $count >= $end ) {
break; // exit loop.
}
endwhile;
$acf_data = ob_get_clean();
return [
'content' => $acf_data,
'postcount' => $row_counter,
'totalposts' => $total,
];
}
}
/**
* Access ACF fields for the Gallery field type.
*
* @param string $type The type of data to return (query/total).
* @param string $parent_field_name The ACF parent field name.
* @param string $field_name The ACF field name.
* @param string $id The post ID.
* @param int $row_index The row index to access data.
* @return mixed Total posts count or an content as data.
* @since 1.3.0
*/
function alm_acf_loop_gallery_rows( $type = 'query', $parent_field_name = '', $field_name = '', $id = '', $row_index = 0 ) {
if ( ! $field_name || ! $id ) {
return ''; // Bail early if empty.
}
// Set initial variables.
$content = '';
if ( empty( $parent_field_name ) ) {
// Standard Field.
while ( have_rows( $field_name, $id ) ) :
the_row();
$content = get_field( $field_name, $id );
endwhile;
} elseif ( $parent_field_name && $row_index > 0 ) {
// Row index: Access sub fields by row index.
if ( have_rows( $parent_field_name, $id ) ) :
while ( have_rows( $parent_field_name, $id ) ) :
the_row();
if ( get_row_index() === $row_index ) {
$content = get_sub_field( $field_name, $id );
}
endwhile;
endif;
} else {
// Parent field: Access sub field data by parent field name.
$parent = explode( ':', $parent_field_name ); // Split into array.
$parent_count = count( $parent );
// Loop sub fields to access the field data.
if ( $parent_count == 1 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
$content = get_sub_field( $field_name, $id );
endwhile;
}
if ( $parent_count == 2 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
while ( have_rows( $parent[1], $id ) ) :
the_row();
$content = get_sub_field( $field_name, $id );
endwhile;
endwhile;
}
if ( $parent_count == 3 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
while ( have_rows( $parent[1], $id ) ) :
the_row();
while ( have_rows( $parent[2], $id ) ) :
the_row();
$content = get_sub_field( $field_name, $id );
endwhile;
endwhile;
endwhile;
}
}
// If count, return the count.
return $type === 'count' ? count( $content ) : $content;
}
/**
* Access nested ACF fields for the Relationship field type.
*
* @param string $parent_field_name The ACF parent field name.
* @param string $field_name The ACF field name.
* @param string $id The post ID.
* @return string The content as raw HTML.
* @since 1.3.0
*/
function alm_acf_loop_relationship_rows( $parent_field_name = '', $field_name = '', $id = '' ) {
if ( ! $field_name || ! $id ) {
return ''; // Bail early if empty.
}
// Initial variables.
$parent = explode( ':', $parent_field_name ); // Split into array.
$parent_count = count( $parent );
$content = '';
// Loop sub fields to get at the field.
if ( $parent_count == 1 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
$content = get_sub_field( $field_name, $id );
endwhile;
}
if ( $parent_count == 2 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
while ( have_rows( $parent[1], $id ) ) :
the_row();
$content = get_sub_field( $field_name, $id );
endwhile;
endwhile;
}
if ( $parent_count == 3 ) { // phpcs:ignore
while ( have_rows( $parent[0], $id ) ) :
the_row();
while ( have_rows( $parent[1], $id ) ) :
the_row();
while ( have_rows( $parent[2], $id ) ) :
the_row();
$content = get_sub_field( $field_name, $id );
endwhile;
endwhile;
endwhile;
}
return $content;
}