Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix menuItems language filtering #90

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: root
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
MARIADB_ROOT_PASSWORD: root
options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand Down
1 change: 1 addition & 0 deletions .gitremote
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
origin [email protected]:valu-digital/wp-graphql-polylang.git
2 changes: 1 addition & 1 deletion composer.wp-install.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"require": {
"composer/installers": "^1.0",
"wp-graphql/wp-graphql": "1.6.5",
"wp-graphql/wp-graphql": "1.26.0",
"wpackagist-plugin/polylang": "3.1.1"
},
"extra": {
Expand Down
57 changes: 16 additions & 41 deletions src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@

class MenuItem
{
/**
* Convert menu location to match the one generated by Polylang
*
* Ex. TOP_MENU -> TOP_MENU___fi
*/
static function translate_menu_location(
string $location,
string $language
): string {
return "${location}___${language}";
}

function init()
{
$this->create_nav_menu_locations();
Expand All @@ -30,52 +18,39 @@ function init()
0
);

// https://github.com/wp-graphql/wp-graphql/blob/release/v1.26.0/src/Data/Connection/MenuItemConnectionResolver.php#L107
add_filter(
'graphql_connection_query_args',
[$this, '__filter_graphql_connection_query_args'],
'graphql_menu_item_connection_args',
[$this, '__filter_graphql_menu_item_connection_args'],
10,
2
);
}

function __filter_graphql_connection_query_args(
array $query_args,
AbstractConnectionResolver $resolver
function __filter_graphql_menu_item_connection_args(
array $args,
$unfiltered
) {
if (!($resolver instanceof MenuItemConnectionResolver)) {
return $query_args;
if (!isset($args['where']['location'])) {
return $args;
}

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

if (!isset($args['where']['language'])) {
return $query_args;
}

if (!isset($args['where']['location'])) {
return $query_args;
if (!$lang) {
return $args;
}

// Required only when using other than the default language because the
// menu location for the default language is the original location
if (pll_default_language('slug') === $args['where']['language']) {
return $query_args;
if (pll_default_language('slug') === $lang) {
return $args;
}

// Update the 'location' arg to use translated location
$args['where']['location'] = self::translate_menu_location(
$args['where']['location'],
$args['where']['language']
);

// XXX. This is a hack. Modify the protected "args" so we can re-execute
// the get_query_args method with the new "location" arg
$ref = new \ReflectionObject($resolver);
$args_prop = $ref->getProperty('args');
$args_prop->setAccessible(true);
$args_prop->setValue($resolver, $args);
// Ex. TOP_MENU -> TOP_MENU___fi
$args['where']['location'] .= '___' . $lang;

return $resolver->get_query_args();
return $args;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/wpunit/SanityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public function testCanUsePolylang()
$this->assertTrue(defined('POLYLANG_VERSION'));

$langs = pll_languages_list(['fields' => 'slug']);
$this->assertEquals($langs, ['en', 'fr', 'fi', 'de', 'es']);
asort($langs);

$this->assertEquals($langs, ['es', 'en', 'fr', 'fi', 'de']);
}

public function testPluginIsActivated()
Expand Down
Loading