Skip to content

Commit

Permalink
Fix issue with the Admin Language switcher
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
esamattis committed Mar 6, 2020
1 parent 4dddc21 commit b585453
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,32 @@ function bind_hooks()
{
add_filter('pll_model', [$this, '__filter_pll_model'], 10, 1);
add_filter('pll_context', [$this, '__filter_pll_context'], 10, 1);
add_filter(
'get_user_metadata',
[$this, '__filter_get_user_metadata'],
10,
4
);
add_action('graphql_init', [$this, '__action_graphql_init']);
add_action('admin_notices', [$this, '__action_admin_notices']);
}

/**
* Disable wp-admin language switcher on graphql context
*/
function __filter_get_user_metadata($data, $object_id, $meta_key, $single)
{
if ($meta_key !== 'pll_filter_content') {
return $data;
}

if (!$this->is_graphql_request()) {
return $data;
}

return 'all';
}

function __action_graphql_init()
{
if (!$this->is_graphql_request()) {
Expand Down
30 changes: 30 additions & 0 deletions tests/wpunit/PostObjectQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,34 @@ public function testContentNodesFiltering()

$this->assertEquals($nodes, $expected);
}

public function testListsPostsFromAllLanguagesWhenLanguageIsSelectedInWPAdmin()
{
wp_set_current_user(1);
update_user_meta(1, 'pll_filter_content', 'en');

// XXX This is not enough to initialize the admin mode for Polylang
// set_current_screen( 'edit-post' );
set_current_screen('edit.php');

$query = '
query Posts {
posts {
nodes {
title
}
}
}
';

$data = do_graphql_request($query);
$nodes = $data['data']['posts']['nodes'];
$expected = [
//
['title' => 'English post'],
['title' => 'Finnish post'],
];

$this->assertEquals($nodes, $expected);
}
}

0 comments on commit b585453

Please sign in to comment.