feat: backend preview logic

This commit is contained in:
2025-09-24 12:02:36 -04:00
parent cd8c98be20
commit cd4cf2e3e3
2 changed files with 49 additions and 164 deletions

View File

@@ -42,85 +42,40 @@ function ccat_acf_init() {
acf_update_setting( 'acfe/modules/templates', false );
}
// Helper: Get GraphQL field name using WPGraphQL ACF's field name resolution
// Helper: Convert field name to GraphQL format
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 );
return ! empty( $field_config['graphql_field_name'] )
? $field_config['graphql_field_name']
: \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 );
// 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 );
}
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 );
$result[ $key ] = $value;
}
}
$sub_field_value = $resolved_repeater;
}
break;
}
}
$resolved_group[ $sub_field_graphql_name ] = $sub_field_value;
}
return $resolved_group;
return $result;
}
// 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 ) {
// 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 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
// Get layout config
$layout_config = null;
if ( isset( $field_config['layouts'] ) ) {
foreach ( $field_config['layouts'] as $layout ) {
if ( ! empty( $acf_loop['active']['field']['layouts'] ) ) {
foreach ( $acf_loop['active']['field']['layouts'] as $layout ) {
if ( $layout['name'] === $layout_name ) {
$layout_config = $layout;
break;
@@ -128,107 +83,37 @@ function ccat_get_row_graphql_value() {
}
}
// Fallback to raw data if no config
if ( ! $layout_config || ! isset( $layout_config['sub_fields'] ) ) {
// Fallback if layout config not found
$raw_data = get_row( true );
$formatted_data = array();
$result = array();
foreach ( $raw_data as $key => $value ) {
if ( strpos( $key, 'acf_fc_layout' ) === 0 || strpos( $key, '_' ) === 0 ) {
continue;
if ( strpos( $key, 'acf_fc_layout' ) !== 0 && strpos( $key, '_' ) !== 0 ) {
$result[ ccat_get_graphql_field_name( array( 'name' => $key ) ) ] = $value;
}
$graphql_key = ccat_get_graphql_field_name( array( 'name' => $key ) );
$formatted_data[ $graphql_key ] = $value;
}
return $formatted_data;
return $result;
}
// 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
// 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();
$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'] );
}
$result[ $key ] = ccat_get_graphql_fields_value( $field['sub_fields'], get_row( true ) );
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;
$value = get_sub_field( $field['name'] );
$result[ $key ] = isset( $field['sub_fields'] ) && $value
? ccat_get_graphql_fields_value( $field['sub_fields'], $value )
: $value;
}
}
$resolved_settings[ $sub_field_graphql_name ] = $sub_field_value;
}
return $resolved_settings;
return $result;
}
// Google Maps API key

View File

@@ -2,7 +2,7 @@
$preview_url = ( defined( 'NUXT_PUBLIC_SITE_URL' ) ? NUXT_PUBLIC_SITE_URL : 'https://cultureat.ca' ) . '/api/acf-preview';
$data = array(
'fieldGroupName' => get_row_layout(),
'value' => ccat_get_row_graphql_value(),
'value' => ccat_get_graphql_row_value(),
);
$result = wp_remote_post( $preview_url, array( 'body' => $data ) );
if ( is_wp_error( $result ) ) {