-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathLanguageRootQueries.php
More file actions
110 lines (94 loc) · 3.54 KB
/
Copy pathLanguageRootQueries.php
File metadata and controls
110 lines (94 loc) · 3.54 KB
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
<?php
namespace WPGraphQL\Extensions\Polylang;
use GraphQLRelay\Relay;
class LanguageRootQueries
{
function init()
{
add_action(
'graphql_register_types',
[$this, '__action_graphql_register_types'],
10,
0
);
}
function __action_graphql_register_types()
{
register_graphql_field('RootQuery', 'languages', [
'type' => ['list_of' => 'Language'],
'description' => __(
'List available languages',
'wp-graphql-polylang'
),
'resolve' => function ($source, $args, $context, $info) {
$fields = $info->getFieldSelection();
// Oh the Polylang api is so nice here. Better ideas?
$languages = array_map(function ($code) {
return [
'id' => Relay::toGlobalId('Language', $code),
'code' => $code,
'slug' => $code,
];
}, pll_languages_list());
if (isset($fields['name'])) {
foreach (
pll_languages_list(['fields' => 'name'])
as $index => $name
) {
$languages[$index]['name'] = $name;
}
}
if (isset($fields['locale'])) {
foreach (
pll_languages_list(['fields' => 'locale'])
as $index => $locale
) {
$languages[$index]['locale'] = $locale;
}
}
if (isset($fields['active'])) {
foreach (
pll_languages_list(['fields' => 'active'])
as $index => $active
) {
$languages[$index]['active'] =
$active === false ? false : true;
}
}
if (isset($fields['homeUrl'])) {
foreach ($languages as &$language) {
$language['homeUrl'] = pll_home_url($language['slug']);
}
}
return $languages;
},
]);
register_graphql_field('RootQuery', 'defaultLanguage', [
'type' => 'Language',
'description' => __('Get language list', 'wp-graphql-polylang'),
'resolve' => function ($source, $args, $context, $info) {
$fields = $info->getFieldSelection();
$language = [];
// All these fields are build from the same data...
if (Helpers::uses_slug_based_field($fields)) {
$language['code'] = pll_default_language('slug');
$language['id'] = Relay::toGlobalId(
'Language',
$language['code']
);
$language['slug'] = $language['code'];
}
if (isset($fields['name'])) {
$language['name'] = pll_default_language('name');
}
if (isset($fields['locale'])) {
$language['locale'] = pll_default_language('locale');
}
if (isset($fields['homeUrl'])) {
$language['homeUrl'] = pll_home_url($language['slug']);
}
return $language;
},
]);
}
}