'GET', 'callback' => 'alm_acf_query', ) ); }); /** * alm_acf_query * Query ACF fields and return data to ALM * * @param $req $_GET * @since 1.0 * @return JSON */ function alm_acf_query( WP_REST_Request $req ) { if(!isset($req)){ return false; } error_reporting(E_ALL|E_STRICT); $data = (isset($req['acf'])) ? $req['acf'] : ''; // $acf object array $repeater = (isset($req['repeater'])) ? $req['repeater'] : 'default'; $type = alm_get_repeater_type($repeater); $theme_repeater = (isset($req['theme_repeater'])) ? $req['theme_repeater'] : 'null'; $posts_per_page = (isset($req['posts_per_page'])) ? $req['posts_per_page'] : 5; $page = (isset($req['page'])) ? $req['page'] : 1; $offset = (isset($req['offset'])) ? $req['offset'] : 0; $canonical_url = (isset($req['canonical_url'])) ? $req['canonical_url'] : $_SERVER['HTTP_REFERER']; // Cache Add-on $cache_id = (isset($req['cache_id'])) ? $req['cache_id'] : ''; // Preload Add-on $preloaded = (isset($req['preloaded'])) ? $req['preloaded'] : false; $preloaded_amount = (isset($req['preloaded_amount'])) ? $req['preloaded_amount'] : '5'; if(has_action('alm_preload_installed') && $preloaded === 'true'){ // If preload - offset the posts_per_page + preload_amount $old_offset = $preloaded_amount; $offset = $offset + $preloaded_amount; } // SEO Add-on $seo_start_page = (isset($req['seo_start_page'])) ? $req['seo_start_page'] : 1; /* * alm_cache_create_dir * * Cache Add-on hook * Create cache directory + meta .txt file * * @return null */ if(!empty($cache_id) && has_action('alm_cache_create_dir')){ apply_filters('alm_cache_create_dir', $cache_id, $canonical_url); $page_cache = ''; // set our page cache variable } 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 Type if(empty($field_name) || empty($post_id)){ $acf = false; // If field_name and post_id are not set, exit } if($acf && $post_id){ // Repeater -OR- Flexible Content if($field_type === 'repeater' || $field_type === 'flexible'){ if( have_rows($field_name, $post_id)){ $total = count(get_field($field_name, $post_id)); $per_page = ($posts_per_page * $page) + 1; $start = ($posts_per_page * $page) + $offset; $end = $start + $posts_per_page; $count = 0; $row_counter = 0; ob_start(); while (have_rows($field_name, $post_id)) : the_row(); // Only display rows between the values if ($row_counter < $posts_per_page && $count >= $start) { $row_counter++; // Set ALM Variables $alm_found_posts = $total; $alm_page = $page + 1; $alm_item = $count + 1; $alm_current = $row_counter + 1; if($theme_repeater != 'null' && has_action('alm_get_theme_repeater')){ do_action('alm_get_theme_repeater', $theme_repeater, $alm_found_posts, $alm_page, $alm_item, $alm_current); // Theme Repeater }else{ include(alm_get_current_repeater( $repeater, $type )); // Repeater } } $count++; if($count >= $end){ break; // exit } endwhile; $acf_data = ob_get_clean(); /* * alm_cache_file * * Cache Add-on hook * If Cache is enabled, check the cache file * * @return null */ if(!empty($cache_id) && has_action('alm_cache_installed') && !empty($acf_data)){ apply_filters('alm_cache_file', $cache_id, $page, $seo_start_page, $acf_data, $preloaded); } } } // Gallery if($field_type === 'gallery'){ $images = get_field($field_name, $post_id); if( $images ) { $total = count(get_field($field_name, $post_id)); $per_page = ($posts_per_page * $page) + 1; $start = ($posts_per_page * $page) + $offset; $end = $start + $posts_per_page; $count = 0; $row_counter = 0; ob_start(); foreach( $images as $image ) : // Only display rows between the values if ($row_counter < $posts_per_page && $count >= $start) { $row_counter++; // Set ALM Variables $alm_found_posts = $total; $alm_page = $page + 1; $alm_item = $count + 1; $alm_current = $row_counter + 1; if($theme_repeater != 'null' && has_action('alm_get_acf_gallery_theme_repeater')){ do_action('alm_get_acf_gallery_theme_repeater', $theme_repeater, $alm_found_posts, $alm_page, $alm_item, $alm_current, $image); // Theme Repeater }else{ include(alm_get_current_repeater( $repeater, $type )); // Repeater } } $count++; if($count >= $end){ break; // exit } endforeach; $acf_data = ob_get_clean(); /* * alm_cache_file * * Cache Add-on hook * If Cache is enabled, check the cache file * * @return null */ if(!empty($cache_id) && has_action('alm_cache_installed') && !empty($acf_data)){ apply_filters('alm_cache_file', $cache_id, $page, $seo_start_page, $acf_data, $preloaded); } } } if($acf_data){ $return = array( 'html' => $acf_data, 'meta' => array( 'postcount' => $row_counter, 'totalposts' => $total ) ); } else{ $return = array( 'html' => '', 'meta' => array( 'postcount' => null, 'totalposts' => null ) ); } wp_send_json($return); } } wp_die(); }