Skip to content

Commit 9dba3e9

Browse files
committed
Fix menuItems language filtering
For wp-graphql v1.26 and later
1 parent 86d0303 commit 9dba3e9

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

src/MenuItem.php

+16-41
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@
77

88
class MenuItem
99
{
10-
/**
11-
* Convert menu location to match the one generated by Polylang
12-
*
13-
* Ex. TOP_MENU -> TOP_MENU___fi
14-
*/
15-
static function translate_menu_location(
16-
string $location,
17-
string $language
18-
): string {
19-
return "${location}___${language}";
20-
}
21-
2210
function init()
2311
{
2412
$this->create_nav_menu_locations();
@@ -30,52 +18,39 @@ function init()
3018
0
3119
);
3220

21+
// https://github.com/wp-graphql/wp-graphql/blob/release/v1.26.0/src/Data/Connection/MenuItemConnectionResolver.php#L107
3322
add_filter(
34-
'graphql_connection_query_args',
35-
[$this, '__filter_graphql_connection_query_args'],
23+
'graphql_menu_item_connection_args',
24+
[$this, '__filter_graphql_menu_item_connection_args'],
3625
10,
3726
2
3827
);
3928
}
4029

41-
function __filter_graphql_connection_query_args(
42-
array $query_args,
43-
AbstractConnectionResolver $resolver
30+
function __filter_graphql_menu_item_connection_args(
31+
array $args,
32+
$unfiltered
4433
) {
45-
if (!($resolver instanceof MenuItemConnectionResolver)) {
46-
return $query_args;
34+
if (!isset($args['where']['location'])) {
35+
return $args;
4736
}
4837

49-
$args = $resolver->getArgs();
38+
$lang = $args['where']['language'] ?? null;
5039

51-
if (!isset($args['where']['language'])) {
52-
return $query_args;
53-
}
54-
55-
if (!isset($args['where']['location'])) {
56-
return $query_args;
40+
if (!$lang) {
41+
return $args;
5742
}
5843

5944
// Required only when using other than the default language because the
6045
// menu location for the default language is the original location
61-
if (pll_default_language('slug') === $args['where']['language']) {
62-
return $query_args;
46+
if (pll_default_language('slug') === $lang) {
47+
return $args;
6348
}
6449

65-
// Update the 'location' arg to use translated location
66-
$args['where']['location'] = self::translate_menu_location(
67-
$args['where']['location'],
68-
$args['where']['language']
69-
);
70-
71-
// XXX. This is a hack. Modify the protected "args" so we can re-execute
72-
// the get_query_args method with the new "location" arg
73-
$ref = new \ReflectionObject($resolver);
74-
$args_prop = $ref->getProperty('args');
75-
$args_prop->setAccessible(true);
76-
$args_prop->setValue($resolver, $args);
50+
// Ex. TOP_MENU -> TOP_MENU___fi
51+
$args['where']['location'] .= '___' . $lang;
7752

78-
return $resolver->get_query_args();
53+
return $args;
7954
}
8055

8156
/**

0 commit comments

Comments
 (0)