7
7
8
8
class MenuItem
9
9
{
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
-
22
10
function init ()
23
11
{
24
12
$ this ->create_nav_menu_locations ();
@@ -30,52 +18,39 @@ function init()
30
18
0
31
19
);
32
20
21
+ // https://github.com/wp-graphql/wp-graphql/blob/release/v1.26.0/src/Data/Connection/MenuItemConnectionResolver.php#L107
33
22
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 ' ],
36
25
10 ,
37
26
2
38
27
);
39
28
}
40
29
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
44
33
) {
45
- if (!( $ resolver instanceof MenuItemConnectionResolver )) {
46
- return $ query_args ;
34
+ if (!isset ( $ args [ ' where ' ][ ' location ' ] )) {
35
+ return $ args ;
47
36
}
48
37
49
- $ args = $ resolver -> getArgs () ;
38
+ $ lang = $ args [ ' where ' ][ ' language ' ] ?? null ;
50
39
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 ;
57
42
}
58
43
59
44
// Required only when using other than the default language because the
60
45
// 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 ;
63
48
}
64
49
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 ;
77
52
78
- return $ resolver -> get_query_args () ;
53
+ return $ args ;
79
54
}
80
55
81
56
/**
0 commit comments