Skip to content

Commit

Permalink
Merge pull request #90 from valu-digital/esamattis/fix-menuitems-lang…
Browse files Browse the repository at this point in the history
…uage-filtering

Fix menuItems language filtering
  • Loading branch information
esamattis committed May 21, 2024
2 parents 3a0f6de + 1ec2fba commit f7f36cc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 45 deletions.
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

0 comments on commit f7f36cc

Please sign in to comment.