From b585453eaf3233659d35b207e27fdf9463988393 Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Fri, 6 Mar 2020 17:41:45 +0200 Subject: [PATCH] Fix issue with the Admin Language switcher Closes #18 --- src/Loader.php | 22 ++++++++++++++++++++ tests/wpunit/PostObjectQueryTest.php | 30 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Loader.php b/src/Loader.php index af30037..93c0b17 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -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()) { diff --git a/tests/wpunit/PostObjectQueryTest.php b/tests/wpunit/PostObjectQueryTest.php index 4e02dc6..8722360 100644 --- a/tests/wpunit/PostObjectQueryTest.php +++ b/tests/wpunit/PostObjectQueryTest.php @@ -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); + } }