Files
cultureat-bak/wp-content/themes/ccat/includes/vendors/acf.php

125 lines
3.9 KiB
PHP

<?php
// Override template file path for ACF previews
add_filter( 'acfe/flexible/render/template', 'ccat_acfe_flexible_render_template', 10, 4 );
function ccat_acfe_flexible_render_template( $file, $field, $layout, $is_preview ) {
if ( ! $is_preview ) {
return $file;
}
// Return relative path - ACF Extended Pro will use acfe_locate_file_path() to find it
return 'layouts/preview.php';
}
// Disable caching for preview requests
add_action( 'acfe/flexible/render/before_template', 'ccat_disable_cache_for_preview', 1, 3 );
function ccat_disable_cache_for_preview( $field, $layout, $is_preview ) {
if ( ! $is_preview ) {
return;
}
// Disable WordPress object cache for this request
wp_cache_flush();
// Send no-cache headers
nocache_headers();
// Disable ACF cache if function exists
if ( function_exists( 'acf_get_store' ) ) {
acf_get_store( 'fields' )->reset();
acf_get_store( 'field-groups' )->reset();
}
}
// Disable ACF / ACFE modules
add_action( 'acf/init', 'ccat_acf_init' );
function ccat_acf_init() {
acf_update_setting( 'acfe/modules/block_types', false );
acf_update_setting( 'acfe/modules/categories', false );
acf_update_setting( 'acfe/modules/forms', false );
acf_update_setting( 'acfe/modules/options_pages', false );
acf_update_setting( 'acfe/modules/post_types', false );
acf_update_setting( 'acfe/modules/taxonomies', false );
acf_update_setting( 'acfe/modules/templates', false );
}
// Helper: Convert field name to GraphQL format
function ccat_get_graphql_field_name( $field_config ) {
return ! empty( $field_config['graphql_field_name'] )
? $field_config['graphql_field_name']
: \WPGraphQL\Utils\Utils::format_field_name( $field_config['name'], false );
}
// Helper: Convert field data to GraphQL format recursively
function ccat_get_graphql_fields_value( $fields, $data ) {
$result = array();
foreach ( $fields as $field ) {
$key = ccat_get_graphql_field_name( $field );
$value = $data[ $field['name'] ] ?? null;
if ( $value && isset( $field['sub_fields'] ) ) {
$value = is_array( $value[0] ?? null )
? array_map( fn( $row ) => ccat_get_graphql_fields_value( $field['sub_fields'], $row ), $value )
: ccat_get_graphql_fields_value( $field['sub_fields'], $value );
}
$result[ $key ] = $value;
}
return $result;
}
// Helper: Get current row data in GraphQL format
function ccat_get_graphql_row_value() {
global $acf_loop;
if ( empty( $layout_name = get_row_layout() ) ) {
return array();
}
// Get layout config
$layout_config = null;
if ( ! empty( $acf_loop['active']['field']['layouts'] ) ) {
foreach ( $acf_loop['active']['field']['layouts'] as $layout ) {
if ( $layout['name'] === $layout_name ) {
$layout_config = $layout;
break;
}
}
}
// Fallback to raw data if no config
if ( ! $layout_config || ! isset( $layout_config['sub_fields'] ) ) {
$raw_data = get_row( true );
$result = array();
foreach ( $raw_data as $key => $value ) {
if ( strpos( $key, 'acf_fc_layout' ) !== 0 && strpos( $key, '_' ) !== 0 ) {
$result[ ccat_get_graphql_field_name( array( 'name' => $key ) ) ] = $value;
}
}
return $result;
}
// Process fields with proper ACF access
$result = array();
foreach ( $layout_config['sub_fields'] as $field ) {
$key = ccat_get_graphql_field_name( $field );
if ( $field['type'] === 'repeater' && $field['name'] === 'layout_settings' ) {
if ( have_rows( 'layout_settings' ) ) {
the_row();
$result[ $key ] = ccat_get_graphql_fields_value( $field['sub_fields'], get_row( true ) );
wp_reset_postdata();
}
} else {
$value = get_sub_field( $field['name'] );
$result[ $key ] = isset( $field['sub_fields'] ) && $value
? ccat_get_graphql_fields_value( $field['sub_fields'], $value )
: $value;
}
}
return $result;
}
// Google Maps API key
add_filter( 'acf/fields/google_map/api', 'ccat_acf_google_map_api' );
function ccat_acf_google_map_api( $api ) {
$api['key'] = 'AIzaSyCoB9_Um059jyenVcFfpXTBq-zZAxlBPqk';
return $api;
}