generated from pascalmartineau/wp-skeleton
134 lines
3.7 KiB
PHP
134 lines
3.7 KiB
PHP
<?php
|
|
|
|
// Register Address GraphQL type
|
|
add_action( 'graphql_register_types', 'ccat_graphql_address_register' );
|
|
function ccat_graphql_address_register() {
|
|
register_graphql_object_type(
|
|
'Address',
|
|
array(
|
|
'description' => 'ACF Extended Pro Address field with optional sub-fields',
|
|
'fields' => array(
|
|
'address' => array(
|
|
'type' => 'String',
|
|
'description' => 'The full address',
|
|
),
|
|
'name' => array(
|
|
'type' => 'String',
|
|
'description' => 'Name of the location (e.g., street address)',
|
|
),
|
|
'streetNumber' => array(
|
|
'type' => 'String',
|
|
'description' => 'Street number',
|
|
),
|
|
'streetName' => array(
|
|
'type' => 'String',
|
|
'description' => 'Street name',
|
|
),
|
|
'city' => array(
|
|
'type' => 'String',
|
|
'description' => 'City name',
|
|
),
|
|
'state' => array(
|
|
'type' => 'String',
|
|
'description' => 'Full state or province name',
|
|
),
|
|
'stateShort' => array(
|
|
'type' => 'String',
|
|
'description' => 'State or province abbreviation',
|
|
),
|
|
'country' => array(
|
|
'type' => 'String',
|
|
'description' => 'Full country name',
|
|
),
|
|
'countryShort' => array(
|
|
'type' => 'String',
|
|
'description' => 'Country abbreviation (ISO code)',
|
|
),
|
|
'postalCode' => array(
|
|
'type' => 'String',
|
|
'description' => 'Postal code or ZIP code',
|
|
),
|
|
'placeId' => array(
|
|
'type' => 'String',
|
|
'description' => 'Google Places API place ID',
|
|
),
|
|
'lat' => array(
|
|
'type' => 'Float',
|
|
'description' => 'Latitude coordinate',
|
|
),
|
|
'lng' => array(
|
|
'type' => 'Float',
|
|
'description' => 'Longitude coordinate',
|
|
),
|
|
),
|
|
)
|
|
);
|
|
}
|
|
|
|
// Register the Address field type with WPGraphQL ACF
|
|
add_action( 'wpgraphql/acf/registry_init', 'ccat_graphql_address_register_field_type' );
|
|
function ccat_graphql_address_register_field_type() {
|
|
register_graphql_acf_field_type(
|
|
'acfe_address',
|
|
array(
|
|
'graphql_type' => static function () {
|
|
return 'Address';
|
|
},
|
|
'resolve' => static function ( $root, $args, $context, $info, $field_type, $field_config ) {
|
|
$value = get_field( $field_config->get_acf_field()['key'], $root['node']->data->ID, false );
|
|
if ( empty( $value ) || ! is_array( $value ) ) {
|
|
return null;
|
|
}
|
|
$address_data = array();
|
|
foreach ( $value as $key => $field_value ) {
|
|
if ( empty( $field_value ) ) {
|
|
continue;
|
|
}
|
|
switch ( $key ) {
|
|
case 'address':
|
|
$address_data['address'] = $field_value;
|
|
break;
|
|
case 'name':
|
|
$address_data['name'] = $field_value;
|
|
break;
|
|
case 'street_number':
|
|
$address_data['streetNumber'] = $field_value;
|
|
break;
|
|
case 'street_name':
|
|
$address_data['streetName'] = $field_value;
|
|
break;
|
|
case 'city':
|
|
$address_data['city'] = $field_value;
|
|
break;
|
|
case 'state':
|
|
$address_data['state'] = $field_value;
|
|
break;
|
|
case 'state_short':
|
|
$address_data['stateShort'] = $field_value;
|
|
break;
|
|
case 'country':
|
|
$address_data['country'] = $field_value;
|
|
break;
|
|
case 'country_short':
|
|
$address_data['countryShort'] = $field_value;
|
|
break;
|
|
case 'post_code':
|
|
$address_data['postalCode'] = $field_value;
|
|
break;
|
|
case 'place_id':
|
|
$address_data['placeId'] = $field_value;
|
|
break;
|
|
case 'lat':
|
|
$address_data['lat'] = floatval( $field_value );
|
|
break;
|
|
case 'lng':
|
|
$address_data['lng'] = floatval( $field_value );
|
|
break;
|
|
}
|
|
}
|
|
return empty( $address_data ) ? null : $address_data;
|
|
},
|
|
)
|
|
);
|
|
}
|