forked from nlemoine/acf-country
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacf-country.php
138 lines (117 loc) · 3.27 KB
/
acf-country.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
Plugin Name: Advanced Custom Fields: ACF Country
Plugin URI: https://github.com/nlemoine/acf-country
Description: Display a select field of all countries, in any language.
Version: 2.0.2
Author: Nicolas Lemoine
Author URI: https://helloni.co/
GitHub Plugin URI: https://github.com/nlemoine/acf-country
License: MIT License
License URI: http://opensource.org/licenses/MIT
*/
namespace HelloNico\ACF;
/**
* Return if Field Loader already exists.
*/
if ( class_exists( 'FieldLoader' ) ) {
return;
}
/**
* Field Loader
*/
class FieldLoader {
/**
* Constructor
*/
public function __construct() {
$this->settings = array(
'version' => '2.0.2',
'url' => plugin_dir_url( __FILE__ ),
'path' => plugin_dir_path( __FILE__ ),
);
load_plugin_textdomain( 'acf-country', false, plugin_basename( dirname( __FILE__ ) ) . '/lang' );
add_action( 'acf/include_field_types', array( $this, 'fields' ) );
add_action( 'acf/register_fields', array( $this, 'fields' ) );
add_filter( 'wpgraphql_acf_register_graphql_field', array( $this, 'register_graphql_field' ), 10, 4 );
}
/**
* Register WPGraphQL field
*
* @see https://github.com/wp-graphql/wp-graphql/issues/214#issuecomment-653141685
*
* @param array $field_config
* @param string $type_name
* @param string $field_name
* @param array $config
* @return mixed
*/
public function register_graphql_field( $field_config, $type_name, $field_name, $config ) {
$acf_field = isset( $config['acf_field'] ) ? $config['acf_field'] : null;
$acf_type = isset( $acf_field['type'] ) ? $acf_field['type'] : null;
if ( $acf_type !== 'country' ) {
return $field_config;
}
$resolve = $field_config['resolve'];
switch ( $acf_field['return_format'] ) {
case 'array':
$field_config = array(
'type' => array( 'list_of' => 'String' ),
'resolve' => function ( $root, $args, $context, $info ) use ( $resolve, $acf_field ) {
$value = $resolve( $root, $args, $context, $info );
if ( ! empty( $value ) ) {
return array(
'value' => $value,
'label' => $acf_field['choices'][ $value ],
);
}
return array();
},
);
break;
case 'value':
$field_config = array(
'type' => 'String',
'resolve' => function ( $root, $args, $context, $info ) use ( $resolve ) {
$value = $resolve( $root, $args, $context, $info );
return ! empty( $value ) ? $value : null;
},
);
break;
case 'label':
$field_config = array(
'type' => 'String',
'resolve' => function ( $root, $args, $context, $info ) use ( $resolve, $acf_field ) {
$value = $resolve( $root, $args, $context, $info );
if ( ! empty( $value ) ) {
return $acf_field['choices'][ $value ];
}
return null;
},
);
break;
}
return $field_config;
}
/**
* Add ACF Country to WPGraphQL supported fields
*
* @param array $supported_fields
* @return array
*/
public function add_graphql_field_support( $supported_fields ) {
array_push( $supported_fields, 'country' );
return $supported_fields;
}
/**
* Include our ACF Field Types
*
* @param integer $version
*
* @return void
*/
public function fields( $version = 5 ) {
include_once 'fields/acf-country.php';
}
}
new FieldLoader();