forked from rburgst/wp-graphql-wpml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-graphql-wpml.php
479 lines (424 loc) · 14.7 KB
/
wp-graphql-wpml.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
/**
* Plugin Name: WPGraphQL WPML
* Plugin URI: https://github.com/rburgst/wp-graphql-wpml
* Description: Adds WPML functionality to WPGraphQL schema.
* Version: 0.0.1
* Author: rburgst
* Author URI: https://github.com/rburgst/
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* WC requires at least: 3.0.0
* WC tested up to: 4.0.0
* WPGraphQL requires at least: 0.8.0+
*
* @package WPGraphQL\WPML
* @author rburgst
* @license GPL-3
*/
function wpgraphqlwpml_is_graphql_request()
{
// Detect WPGraphQL activation by checking if the main class is defined
if (!class_exists('WPGraphQL')) {
return false;
}
return is_graphql_http_request();
}
function wpgraphqlwpml_disable_wpml($query_args, $source, $args, $context, $info)
{
$query_args['suppress_wpml_where_and_join_filter'] = true;
return $query_args;
}
add_filter('graphql_post_object_connection_query_args', 'wpgraphqlwpml_disable_wpml', 100, 5);
function wpgraphqlwpml_add_post_type_fields(\WP_Post_Type $post_type_object)
{
$type = ucfirst($post_type_object->graphql_single_name);
register_graphql_field(
$post_type_object->graphql_single_name,
'locale',
[
'type' => 'Locale',
'description' => __('WPML translation link', 'wp-graphql-wpml'),
'resolve' => function (
\WPGraphQL\Model\Post $post,
$args,
$context,
$info
) {
$fields = $info->getFieldSelection();
$language = [
'id' => null,
'locale' => null,
];
$langInfo = wpml_get_language_information($post->ID);
$locale = $langInfo['locale'];
if (!$locale) {
return null;
}
$language['id'] = $locale;
if (isset($fields['locale'])) {
$language['locale'] = $locale;
}
return $language;
},
]
);
register_graphql_field(
$post_type_object->graphql_single_name,
'localizedWpmlUrl',
[
'type' => 'String',
'description' => __('WPML localized url of the page/post', 'wp-graphql-wpml'),
'resolve' => function (
\WPGraphQL\Model\Post $post,
$args,
$context,
$info
) {
global $sitepress;
$post_id = $post->ID;
$langInfo = wpml_get_language_information($post_id);
$languages = $sitepress->get_active_languages();
$post_language = [];
foreach ($languages as $language) {
if ($language['code'] === $langInfo['language_code']) {
$post_language = $language;
break;
}
}
list($thisPost, $translationUrl) = graphql_wpml_get_translation_url($post_id, $post_language);
return $translationUrl;
},
]
);
register_graphql_field(
$post_type_object->graphql_single_name,
'translations',
[
'type' => ['list_of' => 'Translation'],
'description' => __('WPML translations', 'wpnext'),
'resolve' => function (
\WPGraphQL\Model\Post $post,
$args,
$context,
$info
) {
global $sitepress;
$translations = [];
$languages = $sitepress->get_active_languages();
$orig_post_id = $post->ID;
foreach ($languages as $language) {
$lang_code = array_key_exists('language_code', $languages) ? $language['language_code'] : $language['code'];
$post_id = wpml_object_id_filter($orig_post_id, 'post', false, $lang_code);
if ($post_id === null || $post_id == $orig_post_id) continue;
list($thisPost, $translationUrl) = graphql_wpml_get_translation_url($post_id, $language);
$translations[] = array('locale' => $language['default_locale'], 'id' => $post_id, 'post_title' => $thisPost->post_title, 'href' => $translationUrl);
}
return $translations;
},
]
);
register_graphql_field(
$post_type_object->graphql_single_name,
'translated',
[
'type' => ['list_of' => $type],
'description' => __('WPML translated versions of the same post', 'wpnext'),
'resolve' => function (
\WPGraphQL\Model\Post $post,
$args,
$context,
$info
) {
global $sitepress;
$fields = $info->getFieldSelection();
$translations = [];
$languages = $sitepress->get_active_languages();
foreach ($languages as $language) {
$orig_post_id = $post->ID;
$lang_code = array_key_exists('language_code', $languages) ? $language['language_code'] : $language['code'];
$post_id = wpml_object_id_filter($orig_post_id, 'post', false, $lang_code);
if ($post_id === null || $post_id == $orig_post_id) continue;
$translation = new \WPGraphQL\Model\Post(
\WP_Post::get_instance($post_id)
);
array_push($translations, $translation);
}
return $translations;
},
]
);
}
/**
* @param int $post_id
* @param $language
* @return array
*/
function graphql_wpml_get_translation_url(int $post_id, $language): array
{
$thisPost = get_post($post_id);
// $translationUrl = apply_filters('wpml_permalink', $orig_url, $language['language_code'], true);
$baseUrl = apply_filters('WPML_filter_link', $language['url'], $language);
// // for posts it can be that the $language['url'] already contains the translated url of the post
if (isset($baseUrl) && strpos($baseUrl, $thisPost->post_name) > 0) {
$translationUrl = $baseUrl;
} else {
$href = get_permalink($thisPost->ID);
global $sitepress;
$root_url = $sitepress->language_url($language['code']);
$siteUrl = get_site_url();
// special handling for root urls (homepage)
if ($href !== $siteUrl && $href !== ($siteUrl . "/")) {
$hrefPath = calculate_rel_path($href, $thisPost);
} else {
$hrefPath = "/";
}
$relativePath = str_replace($siteUrl, "", $hrefPath);
$translationUrl = $root_url;
if (strlen($translationUrl) > 0 && substr($translationUrl, -1) !== "/") {
$translationUrl .= "/";
}
// now avoid adding the relative path with a preceding slash
if (substr($relativePath, 0, 1) === "/") {
$translationUrl .= substr($relativePath, 1);
} else {
$translationUrl .= $relativePath;
}
if (strlen($relativePath) > 0 && substr($relativePath, -1) !== "/") {
$translationUrl .= "/";
}
$translationUrl .= $thisPost->post_name . "/";
}
return array($thisPost, $translationUrl);
}
/**
* @param string $href
* @param WP_Post $thisPost
* @return string
*/
function calculate_rel_path(string $href, WP_Post $thisPost): string
{
$hrefPath = dirname($href);
if ($thisPost->post_parent > 0) {
$cur_post = get_post($thisPost->post_parent);
$rel_path = calculate_rel_path($hrefPath, $cur_post);
return $rel_path . "/" . $cur_post->post_name;
}
return $hrefPath;
}
function wpgraphqlwpml_action_graphql_register_types()
{
register_graphql_object_type('Locale', [
'description' => __('Locale (WPML)', 'wp-graphql-wpml'),
'fields' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __(
'Language ID (WPML)',
'wp-graphql-wpml'
),
],
'locale' => [
'type' => 'String',
'description' => __(
'Language locale (WPML)',
'wp-graphql-wpml'
),
],
],
]);
register_graphql_object_type('Translation', [
'description' => __('Translation (WPML)', 'wp-graphql-wpml'),
'fields' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __(
'the id of the referenced translation (WPML)',
'wp-graphql-wpml'
),
],
'href' => [
'type' => 'String',
'description' => __(
'the relative link to the translated content (WPML)',
'wp-graphql-wpml'
),
],
'locale' => [
'type' => 'String',
'description' => __(
'Language code (WPML)',
'wp-graphql-wpml'
),
],
'post_title' => [
'type' => 'String',
'description' => __(
'the title of the translated page (WPML)',
'wp-graphql-wpml'
),
],
],
]);
register_graphql_fields('RootQueryToMenuItemConnectionWhereArgs', [
'language' => [
'type' => 'String',
'description' => 'filters the menu items by language',
],
]);
register_graphql_fields('RootQueryToMenuConnectionWhereArgs', [
'language' => [
'type' => 'String',
'description' => 'filters the menus by language',
],
]);
foreach (\WPGraphQL::get_allowed_post_types() as $post_type) {
wpgraphqlwpml_add_post_type_fields(get_post_type_object($post_type));
}
}
function wpgraphqlwpml__translate_menu_location(
string $location,
string $language
): string
{
global $sitepress;
$language = strtolower($language);
return "${location}___${language}";
}
function wpgraphqlwpml__theme_mod_nav_menu_locations(array $args)
{
foreach ($args as $menu_name => $menu_term_id) {
$translated = get_term($menu_term_id);
$args[$menu_name] = $translated->term_id;
}
return $args;
}
function wpgraphqlwpml__filter_graphql_connection_query_args(array $args)
{
global $sitepress;
if (!isset($args['taxonomy'])) {
return $args;
}
if (!isset($args['language'])) {
return $args;
}
if ($args['taxonomy'] !== 'nav_menu') {
return $args;
}
$target_lang = $args['language'];
$curLang = $sitepress->get_current_language();
// Required only when using other than the default language because the
// menu location for the default language is the original location
if ($curLang !== $target_lang) {
$sitepress->switch_lang($target_lang);
if (!has_filter('theme_mod_nav_menu_locations', 'wpgraphqlwpml__theme_mod_nav_menu_locations')) {
add_filter('theme_mod_nav_menu_locations', 'wpgraphqlwpml__theme_mod_nav_menu_locations');
}
// $args['where']['location'] = wpgraphqlwpml__translate_menu_location(
// $args['where']['location'],
// $target_lang
// );
}
unset($target_lang);
return $args;
}
$wpgraphqlwpml_prev_language = null;
function wpgraphqlwpml__filter_graphql_connection_should_execute(bool $should_execute, AbstractConnectionResolver $resolver)
{
global $sitepress;
global $wpgraphqlwpml_prev_language;
$fieldName = $resolver->getInfo()->fieldName;
if ($fieldName === 'menuItems') {
$args = $resolver->getArgs();
if (!isset($args['where']) || !isset($args['where']['language'])) {
return $should_execute;
}
$new_lang = $args['where']['language'];
$current_language = $sitepress->get_current_language();
if ($new_lang !== $current_language) {
$sitepress->switch_lang($new_lang);
$wpgraphqlwpml_prev_language = $current_language;
}
unset($args['where']['language']);
} else if ($fieldName === 'menus') {
$args = $resolver->getArgs();
if (!isset($args['where']) || !isset($args['where']['language'])) {
return $should_execute;
}
$new_lang = $args['where']['language'];
$current_language = $sitepress->get_current_language();
if ($new_lang !== $current_language) {
$sitepress->switch_lang($new_lang);
$wpgraphqlwpml_prev_language = $current_language;
}
unset($args['where']['language']);
}
return $should_execute;
}
function wpgraphqlwpml__filter_graphql_connection_query(mixed $query, AbstractConnectionResolver $resolver)
{
return $query;
}
function wpgraphqlwpml__filter_graphql_connection_ids(array $ids, AbstractConnectionResolver $resolver)
{
global $sitepress;
global $wpgraphqlwpml_prev_language;
if ($resolver->getInfo()->fieldName === 'menus') {
$args = $resolver->getArgs();
if (!isset($args['where']) && !isset($wpgraphqlwpml_prev_language)) {
return $ids;
}
$result = array();
foreach ($ids as $orig_id) {
$translated = get_term($orig_id);
array_push($result, $translated->term_id);
}
// $sitepress->switch_lang($wpgraphqlwpml_prev_language);
// unset($wpgraphqlwpml_prev_language);
return $result;
}
return $ids;
}
function wpgraphqlwpml_action_init()
{
if (!wpgraphqlwpml_is_graphql_request()) {
return;
}
add_action(
'graphql_register_types',
'wpgraphqlwpml_action_graphql_register_types',
10,
0
);
add_filter(
'graphql_connection_query_args',
'wpgraphqlwpml__filter_graphql_connection_query_args',
10,
2
);
add_filter(
'graphql_connection_should_execute',
'wpgraphqlwpml__filter_graphql_connection_should_execute',
10,
2
);
// add_filter(
// 'graphql_connection_query',
// 'wpgraphqlwpml__filter_graphql_connection_query',
// 10,
// 2
// );
add_filter(
'graphql_connection_ids',
'wpgraphqlwpml__filter_graphql_connection_ids',
10,
2
);
}
add_action('graphql_init', 'wpgraphqlwpml_action_init');
// END RBA 20200828