-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPostObject.php
More file actions
313 lines (268 loc) · 9.58 KB
/
Copy pathPostObject.php
File metadata and controls
313 lines (268 loc) · 9.58 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
namespace WPGraphQL\Extensions\Polylang;
use GraphQLRelay\Relay;
class PostObject
{
function init()
{
add_action(
'graphql_register_types',
[$this, '__action_graphql_register_types'],
10,
0
);
add_action(
'graphql_post_object_mutation_set_object_terms',
[
$this,
'__action_graphql_post_object_mutation_update_additional_data',
],
10,
4
);
add_filter(
'graphql_map_input_fields_to_wp_query',
[__NAMESPACE__ . '\\Helpers', 'map_language_to_query_args'],
10,
2
);
/**
* Check translated front page
*/
add_action(
'graphql_resolve_field',
[$this, '__action_is_translated_front_page'],
10,
8
);
}
/**
* Handle 'language' in post object create&language mutations
* Fires before setting object terms during a GraphQL Post Object Mutation.
* This way the language of the post is set before trying to update the taxonomies.
*/
function __action_graphql_post_object_mutation_update_additional_data(
$post_id,
array $input,
\WP_Post_Type $post_type_object,
$mutation_name
) {
$is_create = substr($mutation_name, 0, 6) === 'create';
if (isset($input['language'])) {
pll_set_post_language($post_id, $input['language']);
} elseif ($is_create) {
$default_lang = pll_default_language();
pll_set_post_language($post_id, $default_lang);
}
}
function __action_graphql_register_types()
{
register_graphql_fields('RootQueryToContentNodeConnectionWhereArgs', [
'language' => [
'type' => 'LanguageCodeFilterEnum',
'description' =>
'Filter content nodes by language code (Polylang)',
],
'languages' => [
'type' => [
'list_of' => [
'non_null' => 'LanguageCodeEnum',
],
],
'description' =>
'Filter content nodes by one or more languages (Polylang)',
],
]);
foreach (\WPGraphQL::get_allowed_post_types() as $post_type) {
$this->add_post_type_fields(get_post_type_object($post_type));
}
}
function add_post_type_fields(\WP_Post_Type $post_type_object)
{
if (!pll_is_translated_post_type($post_type_object->name)) {
return;
}
$type = ucfirst($post_type_object->graphql_single_name);
register_graphql_fields("RootQueryTo${type}ConnectionWhereArgs", [
'language' => [
'type' => 'LanguageCodeFilterEnum',
'description' => "Filter by ${type}s by language code (Polylang)",
],
'languages' => [
'type' => [
'list_of' => [
'non_null' => 'LanguageCodeEnum',
],
],
'description' => "Filter ${type}s by one or more languages (Polylang)",
],
]);
register_graphql_fields("Create${type}Input", [
'language' => [
'type' => 'LanguageCodeEnum',
],
]);
register_graphql_fields("Update${type}Input", [
'language' => [
'type' => 'LanguageCodeEnum',
],
]);
register_graphql_field(
$post_type_object->graphql_single_name,
'language',
[
'type' => 'Language',
'description' => __('Polylang language', 'wpnext'),
'resolve' => function (
\WPGraphQL\Model\Post $post,
$args,
$context,
$info
) {
$fields = $info->getFieldSelection();
$language = [
'name' => null,
'slug' => null,
'code' => null,
];
$post_id = $post->ID;
// The language of the preview post is not set at all so we
// must get the language using the original post id
if ($post->isPreview) {
$post_id = wp_get_post_parent_id($post->ID);
}
$slug = pll_get_post_language($post_id, 'slug');
if (!$slug) {
return null;
}
$language['code'] = $slug;
$language['slug'] = $slug;
$language['id'] = Relay::toGlobalId('Language', $slug);
if (isset($fields['name'])) {
$language['name'] = pll_get_post_language(
$post_id,
'name'
);
}
if (isset($fields['locale'])) {
$language['locale'] = pll_get_post_language(
$post_id,
'locale'
);
}
return $language;
},
]
);
register_graphql_field(
$post_type_object->graphql_single_name,
'translation',
[
'type' => $type,
'description' => __(
'Get specific translation version of this object',
'wp-graphql-polylang'
),
'args' => [
'language' => [
'type' => [
'non_null' => 'LanguageCodeEnum',
],
],
],
'resolve' => function (
\WPGraphQL\Model\Post $post,
array $args
) {
$translations = pll_get_post_translations($post->ID);
$post_id = $translations[$args['language']] ?? null;
if (!$post_id) {
return null;
}
return new \WPGraphQL\Model\Post(
\WP_Post::get_instance($post_id)
);
},
]
);
register_graphql_field(
$post_type_object->graphql_single_name,
'translations',
[
'type' => [
'list_of' => $type,
],
'description' => __(
'List all translated versions of this post',
'wp-graphql-polylang'
),
'resolve' => function (\WPGraphQL\Model\Post $post) {
$posts = [];
if ($post->isPreview) {
$parent = wp_get_post_parent_id($post->ID);
$translations = pll_get_post_translations($parent);
} else {
$translations = pll_get_post_translations($post->ID);
}
foreach ($translations as $lang => $post_id) {
$translation = \WP_Post::get_instance($post_id);
if (!$translation) {
continue;
}
if (is_wp_error($translation)) {
continue;
}
if ($post->ID === $translation->ID) {
continue;
}
// If fetching preview do not add the original as a translation
if ($post->isPreview && $parent === $translation->ID) {
continue;
}
$model = new \WPGraphQL\Model\Post($translation);
// If we do not filter out privates here wp-graphql will
// crash with 'Cannot return null for non-nullable field
// Post.id.'. This might be a wp-graphql bug.
// Interestingly only fetching the id of the translated
// post caused the crash. For example title is ok even
// without this check
if ($model->is_private()) {
continue;
}
$posts[] = $model;
}
return $posts;
},
]
);
}
function __action_is_translated_front_page(
$result,
$source,
$args,
$context,
$info,
$type_name,
$field_key
) {
if ('isFrontPage' !== $field_key) {
return $result;
}
if (!($source instanceof \WPGraphQL\Model\Post)) {
return $result;
}
if ('page' !== get_option('show_on_front', 'posts')) {
return $result;
}
if (empty((int) get_option('page_on_front', 0))) {
return $result;
}
$translated_front_page = pll_get_post_translations(
get_option('page_on_front', 0)
);
if (empty($translated_front_page)) {
return false;
}
return in_array($source->ID, $translated_front_page);
}
}