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

240 lines
7.6 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: Get GraphQL field name using WPGraphQL ACF's field name resolution
function ccat_get_graphql_field_name( $field_config ) {
// Check if custom graphql_field_name is set
if ( ! empty( $field_config['graphql_field_name'] ) ) {
return $field_config['graphql_field_name'];
}
// Use WPGraphQL's format_field_name function
return \WPGraphQL\Utils\Utils::format_field_name( $field_config['name'], false );
}
// Helper: Resolve group/nested fields recursively
function ccat_resolve_group_fields( $sub_fields, $field_data ) {
$resolved_group = array();
foreach ( $sub_fields as $sub_field ) {
$sub_field_graphql_name = ccat_get_graphql_field_name( $sub_field );
$sub_field_value = isset( $field_data[ $sub_field['name'] ] ) ? $field_data[ $sub_field['name'] ] : null;
// Recursively handle nested structures within the group
if ( isset( $sub_field['type'] ) && $sub_field_value !== null ) {
switch ( $sub_field['type'] ) {
case 'group':
if ( isset( $sub_field['sub_fields'] ) && is_array( $sub_field_value ) ) {
$sub_field_value = ccat_resolve_group_fields( $sub_field['sub_fields'], $sub_field_value );
}
break;
case 'repeater':
if ( isset( $sub_field['sub_fields'] ) && is_array( $sub_field_value ) ) {
$resolved_repeater = array();
foreach ( $sub_field_value as $row_data ) {
if ( is_array( $row_data ) ) {
$resolved_repeater[] = ccat_resolve_group_fields( $sub_field['sub_fields'], $row_data );
}
}
$sub_field_value = $resolved_repeater;
}
break;
}
}
$resolved_group[ $sub_field_graphql_name ] = $sub_field_value;
}
return $resolved_group;
}
// Helper: Get current flexible content row data formatted as GraphQL would return it
function ccat_get_row_graphql_value() {
$layout_name = get_row_layout();
if ( ! $layout_name ) {
return array();
}
// Get the current flexible content field configuration
global $acf_loop;
if ( empty( $acf_loop['active'] ) ) {
// Fallback to basic transformation if no active loop
$raw_data = get_row( true );
$formatted_data = array();
foreach ( $raw_data as $key => $value ) {
if ( strpos( $key, 'acf_fc_layout' ) === 0 || strpos( $key, '_' ) === 0 ) {
continue;
}
$graphql_key = ccat_get_graphql_field_name( array( 'name' => $key ) );
$formatted_data[ $graphql_key ] = $value;
}
return $formatted_data;
}
$active_loop = $acf_loop['active'];
$field_config = $active_loop['field'];
// Find the current layout configuration
$layout_config = null;
if ( isset( $field_config['layouts'] ) ) {
foreach ( $field_config['layouts'] as $layout ) {
if ( $layout['name'] === $layout_name ) {
$layout_config = $layout;
break;
}
}
}
if ( ! $layout_config || ! isset( $layout_config['sub_fields'] ) ) {
// Fallback if layout config not found
$raw_data = get_row( true );
$formatted_data = array();
foreach ( $raw_data as $key => $value ) {
if ( strpos( $key, 'acf_fc_layout' ) === 0 || strpos( $key, '_' ) === 0 ) {
continue;
}
$graphql_key = ccat_get_graphql_field_name( array( 'name' => $key ) );
$formatted_data[ $graphql_key ] = $value;
}
return $formatted_data;
}
// Resolve fields using the actual layout configuration with proper field access
return ccat_resolve_layout_fields( $layout_config['sub_fields'] );
}
// Helper: Resolve fields within a flexible content layout context
function ccat_resolve_layout_fields( $field_configs ) {
$resolved_data = array();
foreach ( $field_configs as $field_config ) {
$graphql_field_name = ccat_get_graphql_field_name( $field_config );
$field_value = null;
// Handle different field types with their proper access methods
switch ( $field_config['type'] ) {
case 'repeater':
// For layout_settings repeater (which acts like settings)
if ( $field_config['name'] === 'layout_settings' ) {
// layout_settings is a single-row repeater, get the first (and usually only) row
if ( have_rows( 'layout_settings' ) ) {
the_row();
$field_value = ccat_resolve_settings_fields( $field_config['sub_fields'] );
wp_reset_postdata(); // Reset after the_row()
}
} else {
// Regular repeater field
$repeater_data = array();
if ( have_rows( $field_config['name'] ) ) {
while ( have_rows( $field_config['name'] ) ) {
the_row();
$repeater_data[] = ccat_resolve_settings_fields( $field_config['sub_fields'] );
}
wp_reset_postdata();
}
$field_value = $repeater_data;
}
break;
case 'group':
// Regular group field
$group_value = get_sub_field( $field_config['name'] );
if ( is_array( $group_value ) && isset( $field_config['sub_fields'] ) ) {
$field_value = ccat_resolve_group_fields( $field_config['sub_fields'], $group_value );
} else {
$field_value = $group_value;
}
break;
default:
// Regular field
$field_value = get_sub_field( $field_config['name'] );
break;
}
$resolved_data[ $graphql_field_name ] = $field_value;
}
return $resolved_data;
}
// Helper: Resolve fields within repeater/settings context (using get_sub_field)
function ccat_resolve_settings_fields( $sub_fields ) {
$resolved_settings = array();
foreach ( $sub_fields as $sub_field ) {
$sub_field_graphql_name = ccat_get_graphql_field_name( $sub_field );
$sub_field_value = get_sub_field( $sub_field['name'] );
// Handle nested structures within settings
if ( isset( $sub_field['type'] ) && $sub_field_value !== null ) {
switch ( $sub_field['type'] ) {
case 'group':
if ( isset( $sub_field['sub_fields'] ) && is_array( $sub_field_value ) ) {
$sub_field_value = ccat_resolve_group_fields( $sub_field['sub_fields'], $sub_field_value );
}
break;
case 'repeater':
// Nested repeater within settings - this would be complex, handle as needed
// For now, leave as-is
break;
}
}
$resolved_settings[ $sub_field_graphql_name ] = $sub_field_value;
}
return $resolved_settings;
}
// 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;
}