From 16498a92181b90d6cde23258fddb569502a9c3ff Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Fri, 6 Mar 2020 16:50:09 +0200 Subject: [PATCH] Use __filter_ naming and run prettier --- src/LanguageRootQueries.php | 9 ++++- src/Loader.php | 56 ++++++++++++++-------------- src/MenuItem.php | 14 ++++--- src/PolylangTypes.php | 9 ++++- src/PostObject.php | 23 ++++++++---- src/StringsTranslations.php | 9 ++++- src/TermObject.php | 13 +++++-- tests/functional/BasicCest.php | 53 +++++++++++++------------- tests/wpunit/PostObjectQueryTest.php | 4 +- tests/wpunit/TermObjectQueryTest.php | 1 - wp-graphql-polylang.php | 7 ++-- 11 files changed, 113 insertions(+), 85 deletions(-) diff --git a/src/LanguageRootQueries.php b/src/LanguageRootQueries.php index f7df248..cd384b9 100644 --- a/src/LanguageRootQueries.php +++ b/src/LanguageRootQueries.php @@ -8,10 +8,15 @@ class LanguageRootQueries { function init() { - add_action('graphql_register_types', [$this, 'register'], 10, 0); + add_action( + 'graphql_register_types', + [$this, '__action_graphql_register_types'], + 10, + 0 + ); } - function register() + function __action_graphql_register_types() { register_graphql_field('RootQuery', 'languages', [ 'type' => ['list_of' => 'Language'], diff --git a/src/Loader.php b/src/Loader.php index ae3232a..af30037 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -2,25 +2,25 @@ namespace WPGraphQL\Extensions\Polylang; - class Loader { private $pll_context_called = false; - static function init() { + static function init() + { define('WPGRAPHQL_POLYLANG', true); (new Loader())->bind_hooks(); } function bind_hooks() { - add_filter('pll_model', [$this, 'get_pll_model'], 10, 1); - add_filter('pll_context', [$this, 'get_pll_context'], 10, 1); - add_action('graphql_init', [$this, 'graphql_polylang_init']); - add_action('admin_notices', [$this, 'admin_notices']); + add_filter('pll_model', [$this, '__filter_pll_model'], 10, 1); + add_filter('pll_context', [$this, '__filter_pll_context'], 10, 1); + add_action('graphql_init', [$this, '__action_graphql_init']); + add_action('admin_notices', [$this, '__action_admin_notices']); } - function graphql_polylang_init() + function __action_graphql_init() { if (!$this->is_graphql_request()) { return; @@ -42,7 +42,7 @@ function graphql_polylang_init() (new StringsTranslations())->init(); } - function get_pll_model($class) + function __filter_pll_model($class) { if ($this->is_graphql_request()) { return 'PLL_Admin_Model'; @@ -51,7 +51,7 @@ function get_pll_model($class) return $class; } - function get_pll_context($class) + function __filter_pll_context($class) { $this->pll_context_called = true; @@ -62,7 +62,7 @@ function get_pll_context($class) return $class; } - function admin_notices() + function __action_admin_notices() { if (!is_super_admin()) { return; @@ -92,7 +92,7 @@ function endsWith($haystack, $needle) return true; } - return (substr($haystack, -$length) === $needle); + return substr($haystack, -$length) === $needle; } function is_graphql_request() @@ -100,7 +100,7 @@ function is_graphql_request() // Detect WPGraphQL activation by checking if the main class is defined if (!class_exists('WPGraphQL')) { return false; - }; + } if (!defined('POLYLANG_VERSION')) { return false; @@ -112,23 +112,23 @@ function is_graphql_request() // Copied from https://github.com/wp-graphql/wp-graphql/pull/1067 // For now as the existing version is buggy. - if ( isset( $_GET[ \WPGraphQL\Router::$route ] ) ) { - return true; - } - - // If before 'init' check $_SERVER. - if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { - $haystack = wp_unslash( $_SERVER['HTTP_HOST'] ) - . wp_unslash( $_SERVER['REQUEST_URI'] ); - $needle = site_url( \WPGraphQL\Router::$route ); - // Strip protocol. - $haystack = preg_replace( '#^(http(s)?://)#', '', $haystack ); - $needle = preg_replace( '#^(http(s)?://)#', '', $needle ); - $len = strlen( $needle ); - return ( substr( $haystack, 0, $len ) === $needle ); + if (isset($_GET[\WPGraphQL\Router::$route])) { + return true; } - return false; + // If before 'init' check $_SERVER. + if (isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) { + $haystack = + wp_unslash($_SERVER['HTTP_HOST']) . + wp_unslash($_SERVER['REQUEST_URI']); + $needle = site_url(\WPGraphQL\Router::$route); + // Strip protocol. + $haystack = preg_replace('#^(http(s)?://)#', '', $haystack); + $needle = preg_replace('#^(http(s)?://)#', '', $needle); + $len = strlen($needle); + return substr($haystack, 0, $len) === $needle; + } + return false; } -} \ No newline at end of file +} diff --git a/src/MenuItem.php b/src/MenuItem.php index 8a92162..45a4112 100644 --- a/src/MenuItem.php +++ b/src/MenuItem.php @@ -21,18 +21,22 @@ function init() { $this->create_nav_menu_locations(); - add_action('graphql_register_types', [$this, 'register_fields'], 10, 0); + add_action( + 'graphql_register_types', + [$this, '__action_graphql_register_types'], + 10, + 0 + ); - // XXX This is not supported by WPGraphQL yet add_filter( 'graphql_menu_item_connection_args', - [$this, 'map_input_language_to_location'], + [$this, '__filter_graphql_menu_item_connection_args'], 10, 1 ); } - function map_input_language_to_location(array $args) + function __filter_graphql_menu_item_connection_args(array $args) { if (!isset($args['where']['language'])) { return $args; @@ -79,7 +83,7 @@ function () { ); } - function register_fields() + function __action_graphql_register_types() { register_graphql_fields('RootQueryToMenuItemConnectionWhereArgs', [ 'language' => [ diff --git a/src/PolylangTypes.php b/src/PolylangTypes.php index a0e2c96..e2c5695 100644 --- a/src/PolylangTypes.php +++ b/src/PolylangTypes.php @@ -6,10 +6,15 @@ class PolylangTypes { function init() { - add_action('graphql_register_types', [$this, 'register_types'], 9, 0); + add_action( + 'graphql_register_types', + [$this, '__action_graphql_register_types'], + 9, + 0 + ); } - function register_types() + function __action_graphql_register_types() { $language_codes = []; diff --git a/src/PostObject.php b/src/PostObject.php index 66bf083..d234a72 100644 --- a/src/PostObject.php +++ b/src/PostObject.php @@ -8,11 +8,19 @@ class PostObject { function init() { - add_action('graphql_register_types', [$this, 'register_fields'], 10, 0); + add_action( + 'graphql_register_types', + [$this, '__action_graphql_register_types'], + 10, + 0 + ); add_action( 'graphql_post_object_mutation_update_additional_data', - [$this, 'mutate_language'], + [ + $this, + '__action_graphql_post_object_mutation_update_additional_data', + ], 10, 4 ); @@ -28,28 +36,29 @@ function init() /** * Handle 'language' in post object create&language mutations */ - function mutate_language( + function __action_graphql_post_object_mutation_update_additional_data( $post_id, array $input, \WP_Post_Type $post_type_object, $mutation_name ) { - $is_create = substr( $mutation_name, 0, 6 ) === 'create'; + $is_create = substr($mutation_name, 0, 6) === 'create'; if (isset($input['language'])) { pll_set_post_language($post_id, $input['language']); - } else if ($is_create) { + } elseif ($is_create) { $default_lang = pll_default_language(); pll_set_post_language($post_id, $default_lang); } } - function register_fields() + function __action_graphql_register_types() { register_graphql_fields('RootQueryToContentNodeConnectionWhereArgs', [ 'language' => [ 'type' => 'LanguageCodeFilterEnum', - 'description' => "Filter content nodes by language code (Polylang)", + 'description' => + 'Filter content nodes by language code (Polylang)', ], ]); diff --git a/src/StringsTranslations.php b/src/StringsTranslations.php index c75868c..b60764c 100644 --- a/src/StringsTranslations.php +++ b/src/StringsTranslations.php @@ -6,10 +6,15 @@ class StringsTranslations { function init() { - add_action('graphql_register_types', [$this, 'register'], 10, 0); + add_action( + 'graphql_register_types', + [$this, '__action_graphql_register_types'], + 10, + 0 + ); } - function register() + function __action_graphql_register_types() { register_graphql_field('RootQuery', 'translateString', [ 'type' => 'String', diff --git a/src/TermObject.php b/src/TermObject.php index ef25c84..c13f91b 100644 --- a/src/TermObject.php +++ b/src/TermObject.php @@ -8,7 +8,12 @@ class TermObject { function init() { - add_action('graphql_register_types', [$this, 'register'], 10, 0); + add_action( + 'graphql_register_types', + [$this, '__action_graphql_register_types'], + 10, + 0 + ); add_filter( 'graphql_map_input_fields_to_get_terms', @@ -19,13 +24,13 @@ function init() add_filter( 'graphql_term_object_insert_term_args', - [$this, 'map_language_input_to_args'], + [$this, '__filter_graphql_term_object_insert_term_args'], 10, 2 ); } - function map_language_input_to_args($insert_args, $input) + function __filter_graphql_term_object_insert_term_args($insert_args, $input) { if (isset($input['language'])) { $insert_args['language'] = $input['language']; @@ -34,7 +39,7 @@ function map_language_input_to_args($insert_args, $input) return $insert_args; } - function register() + function __action_graphql_register_types() { foreach (\WPGraphQL::get_allowed_taxonomies() as $taxonomy) { $this->add_taxonomy_fields(get_taxonomy($taxonomy)); diff --git a/tests/functional/BasicCest.php b/tests/functional/BasicCest.php index 68c3f69..0aefd3f 100644 --- a/tests/functional/BasicCest.php +++ b/tests/functional/BasicCest.php @@ -1,9 +1,9 @@ $query, - ] ); - $I->haveHttpHeader( 'Content-Type', 'application/json' ); - $I->sendGET( "/graphql?{$query_vars}" ); - $I->seeResponseCodeIs( 200 ); + ]); + $I->haveHttpHeader('Content-Type', 'application/json'); + $I->sendGET("/graphql?{$query_vars}"); + $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->seeResponseContainsJson([ 'data' => [ @@ -26,15 +26,14 @@ public function testCanSendGET( FunctionalTester $I ) { ], [ 'code' => 'FI', - ] + ], ], ], ]); - } - public function testCanSendPOST( FunctionalTester $I ) { - + public function testCanSendPOST(FunctionalTester $I) + { $query = ' { languages { @@ -42,11 +41,11 @@ public function testCanSendPOST( FunctionalTester $I ) { } }'; - $I->haveHttpHeader( 'Content-Type', 'application/json' ); - $I->sendPOST( '/graphql', [ + $I->haveHttpHeader('Content-Type', 'application/json'); + $I->sendPOST('/graphql', [ 'query' => $query, - ] ); - $I->seeResponseCodeIs( 200 ); + ]); + $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->seeResponseContainsJson([ 'data' => [ @@ -56,18 +55,17 @@ public function testCanSendPOST( FunctionalTester $I ) { ], [ 'code' => 'FI', - ] + ], ], ], ]); - } /** * wp-graphql sends graphql requests to index.php?graphql */ - public function testCanSendPOSTToGraphiql( FunctionalTester $I ) { - + public function testCanSendPOSTToGraphiql(FunctionalTester $I) + { $query = ' { languages { @@ -75,11 +73,11 @@ public function testCanSendPOSTToGraphiql( FunctionalTester $I ) { } }'; - $I->haveHttpHeader( 'Content-Type', 'application/json' ); - $I->sendPOST( '/index.php?graphql', [ + $I->haveHttpHeader('Content-Type', 'application/json'); + $I->sendPOST('/index.php?graphql', [ 'query' => $query, - ] ); - $I->seeResponseCodeIs( 200 ); + ]); + $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->seeResponseContainsJson([ 'data' => [ @@ -89,10 +87,9 @@ public function testCanSendPOSTToGraphiql( FunctionalTester $I ) { ], [ 'code' => 'FI', - ] + ], ], ], ]); - } -} \ No newline at end of file +} diff --git a/tests/wpunit/PostObjectQueryTest.php b/tests/wpunit/PostObjectQueryTest.php index a2df2f9..4e02dc6 100644 --- a/tests/wpunit/PostObjectQueryTest.php +++ b/tests/wpunit/PostObjectQueryTest.php @@ -235,8 +235,8 @@ public function testContentNodesFiltering() $nodes = $data['data']['contentNodes']['nodes'] ?? []; $expected = [ [ - 'title' => 'Finnish post' - ] + 'title' => 'Finnish post', + ], ]; $this->assertEquals($nodes, $expected); diff --git a/tests/wpunit/TermObjectQueryTest.php b/tests/wpunit/TermObjectQueryTest.php index e605bf4..f232c77 100644 --- a/tests/wpunit/TermObjectQueryTest.php +++ b/tests/wpunit/TermObjectQueryTest.php @@ -182,7 +182,6 @@ public function testCanFetchTranslatedTermVersionsWithIds() $data = do_graphql_request($query); $this->assertArrayNotHasKey('errors', $data, print_r($data, true)); - } public function testCanFetchSpecificTranslatedVersion() diff --git a/wp-graphql-polylang.php b/wp-graphql-polylang.php index 2271719..89dfa49 100644 --- a/wp-graphql-polylang.php +++ b/wp-graphql-polylang.php @@ -10,10 +10,9 @@ * @package wp-graphql-polylang */ - - // Use the local autoload if not using project wide autoload - if (!\class_exists('\WPGraphQL\Extensions\Polylang\Loader')) { +// Use the local autoload if not using project wide autoload +if (!\class_exists('\WPGraphQL\Extensions\Polylang\Loader')) { require_once __DIR__ . '/vendor/autoload.php'; - } +} \WPGraphQL\Extensions\Polylang\Loader::init();