generated from pascalmartineau/wp-skeleton
wip: preview using GraphQL value
This commit is contained in:
@@ -1,7 +1,270 @@
|
||||
<?php
|
||||
// Get GraphQL field name using WPGraphQL ACF's actual field name resolution
|
||||
function 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 if available
|
||||
if ( class_exists( '\WPGraphQL\Utils\Utils' ) && method_exists( '\WPGraphQL\Utils\Utils', 'format_field_name' ) ) {
|
||||
return \WPGraphQL\Utils\Utils::format_field_name( $field_config['name'], false );
|
||||
}
|
||||
|
||||
// Fallback to manual conversion matching WPGraphQL's logic
|
||||
$name = $field_config['name'];
|
||||
|
||||
// Remove non-alphanumeric characters except underscores and spaces
|
||||
$name = preg_replace( '/[^a-zA-Z0-9_\s]/', '', $name );
|
||||
|
||||
// Convert snake_case to camelCase (matching WPGraphQL logic)
|
||||
$name = lcfirst( str_replace( '_', ' ', ucwords( $name, '_' ) ) );
|
||||
|
||||
// Convert kebab-case to camelCase
|
||||
$name = lcfirst( str_replace( '-', ' ', ucwords( $name, '-' ) ) );
|
||||
|
||||
// Convert spaces to camelCase
|
||||
$name = lcfirst( str_replace( ' ', '', ucwords( $name, ' ' ) ) );
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
// Helper function to resolve group/nested fields recursively
|
||||
function resolve_group_fields( $sub_fields, $field_data ) {
|
||||
$resolved_group = array();
|
||||
|
||||
foreach ( $sub_fields as $sub_field ) {
|
||||
$sub_field_graphql_name = 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 = 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[] = 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;
|
||||
}
|
||||
|
||||
// Recursively resolve field data using WPGraphQL ACF field resolution
|
||||
function resolve_acf_fields_for_graphql( $field_configs, $use_sub_field_context = false ) {
|
||||
$resolved_data = array();
|
||||
|
||||
foreach ( $field_configs as $field_config ) {
|
||||
// Get the GraphQL field name using WPGraphQL ACF's method
|
||||
$graphql_field_name = get_graphql_field_name( $field_config );
|
||||
|
||||
// Get the field value - use get_sub_field when in flexible content context
|
||||
if ( $use_sub_field_context ) {
|
||||
$field_value = get_sub_field( $field_config['name'] );
|
||||
} else {
|
||||
$field_value = get_field( $field_config['name'] );
|
||||
}
|
||||
|
||||
// Apply WPGraphQL ACF formatting if available
|
||||
if ( function_exists( 'graphql' ) && class_exists( '\WPGraphQL\Acf\Utils' ) ) {
|
||||
try {
|
||||
$field_value = apply_filters( 'graphql_acf_field_value', $field_value, $field_config, get_the_ID() );
|
||||
} catch ( Exception $e ) { // @phpcs:ignore
|
||||
// Exception caught, fallback to raw value
|
||||
}
|
||||
}
|
||||
|
||||
// Handle nested structures (groups, repeaters, flexible content)
|
||||
if ( isset( $field_config['type'] ) ) {
|
||||
switch ( $field_config['type'] ) {
|
||||
case 'group':
|
||||
if ( isset( $field_config['sub_fields'] ) && is_array( $field_value ) ) {
|
||||
// Recursively resolve the group's sub-fields
|
||||
$field_value = resolve_group_fields( $field_config['sub_fields'], $field_value );
|
||||
}
|
||||
break;
|
||||
|
||||
case 'repeater':
|
||||
if ( isset( $field_config['sub_fields'] ) && is_array( $field_value ) ) {
|
||||
$resolved_repeater = array();
|
||||
foreach ( $field_value as $row_data ) {
|
||||
if ( is_array( $row_data ) ) {
|
||||
$resolved_repeater[] = resolve_group_fields( $field_config['sub_fields'], $row_data );
|
||||
}
|
||||
}
|
||||
$field_value = $resolved_repeater;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$resolved_data[ $graphql_field_name ] = $field_value;
|
||||
}
|
||||
|
||||
return $resolved_data;
|
||||
}
|
||||
|
||||
// Get current flexible content row data formatted as GraphQL would return it
|
||||
function get_row_graphql_formatted() {
|
||||
$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 = 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 = 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 resolve_layout_fields( $layout_config['sub_fields'] );
|
||||
}
|
||||
|
||||
// Resolve fields within a flexible content layout context
|
||||
function resolve_layout_fields( $field_configs ) {
|
||||
$resolved_data = array();
|
||||
|
||||
foreach ( $field_configs as $field_config ) {
|
||||
$graphql_field_name = 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 = 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[] = 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 = 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;
|
||||
}
|
||||
|
||||
// Resolve fields within repeater/settings context (using get_sub_field)
|
||||
function resolve_settings_fields( $sub_fields ) {
|
||||
$resolved_settings = array();
|
||||
|
||||
foreach ( $sub_fields as $sub_field ) {
|
||||
$sub_field_graphql_name = 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 = 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;
|
||||
}
|
||||
|
||||
$preview_url = ( defined( 'NUXT_PUBLIC_SITE_URL' ) ? NUXT_PUBLIC_SITE_URL : 'https://cultureat.ca' ) . '/api/acf-preview';
|
||||
$data = array(
|
||||
'layout' => get_row_layout(),
|
||||
'fieldGroupName' => get_row_layout(),
|
||||
'data' => get_row_graphql_formatted(),
|
||||
);
|
||||
$result = wp_remote_post( $preview_url, array( 'body' => $data ) );
|
||||
if ( is_wp_error( $result ) ) {
|
||||
|
||||
Reference in New Issue
Block a user