diff --git a/wp-content/themes/ccat/includes/vendors/acf.php b/wp-content/themes/ccat/includes/vendors/acf.php index 7584128..243fc1e 100644 --- a/wp-content/themes/ccat/includes/vendors/acf.php +++ b/wp-content/themes/ccat/includes/vendors/acf.php @@ -42,6 +42,195 @@ function ccat_acf_init() { 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 ) { diff --git a/wp-content/themes/ccat/layouts/preview.php b/wp-content/themes/ccat/layouts/preview.php index ad4fabc..9cf10d9 100644 --- a/wp-content/themes/ccat/layouts/preview.php +++ b/wp-content/themes/ccat/layouts/preview.php @@ -1,270 +1,8 @@ $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( 'fieldGroupName' => get_row_layout(), - 'data' => get_row_graphql_formatted(), + 'value' => ccat_get_row_graphql_value(), ); $result = wp_remote_post( $preview_url, array( 'body' => $data ) ); if ( is_wp_error( $result ) ) {