From 83907ec02b8fb242c33bf33bbd0c0d0bc5cd3496 Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Tue, 7 May 2019 17:17:41 +0300 Subject: [PATCH] Die with error message when using too old Polylang version --- wp-graphql-polylang.php | 105 ++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/wp-graphql-polylang.php b/wp-graphql-polylang.php index 34a5dcd..53b1b8a 100644 --- a/wp-graphql-polylang.php +++ b/wp-graphql-polylang.php @@ -12,6 +12,8 @@ namespace WPGraphQL\Extensions\Polylang; +use GraphQL\Error\UserError; + define('WPGRAPHQL_POLYLANG', true); require_once __DIR__ . '/src/Helpers.php'; @@ -22,63 +24,80 @@ require_once __DIR__ . '/src/TermObject.php'; require_once __DIR__ . '/src/MenuItem.php'; -function isGraphqlContext() +class Loader { - if (!defined('POLYLANG_VERSION')) { - return false; - } + private $pll_context_called = false; - if (defined('GRAPHQL_POLYLANG_TESTS')) { - return true; + function init() + { + 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']); } - if (defined('GRAPHQL_HTTP_REQUEST')) { - return GRAPHQL_HTTP_REQUEST; - } + function graphql_polylang_init() + { + if (!$this->is_graphql_request()) { + return; + } - // XXX GRAPHQL_HTTP_REQUEST is not defined early enough! - if ('/graphql' == $_SERVER['REQUEST_URI']) { - return true; - } + if (!$this->pll_context_called) { + \http_response_code(500); + $msg = + 'wp-graphql-polylang: You are using too old Polylang version. You must use one that implements pll_context filter https://github.com/polylang/polylang/commit/2203b9e16532797fa530f9b73024b53885d728ef'; + error_log($msg); + die($msg); + } - return false; -} + (new PolylangTypes())->init(); + (new PostObject())->init(); + (new TermObject())->init(); + (new LanguageRootQueries())->init(); + (new MenuItem())->init(); + (new StringsTranslations())->init(); + } -add_filter( - 'pll_model', - function ($class) { - if (isGraphqlContext()) { + function get_pll_model($class) + { + if ($this->is_graphql_request()) { return 'PLL_Admin_Model'; } return $class; - }, - 10, - 1 -); - -add_filter( - 'pll_context', - function ($class) { - if (isGraphqlContext()) { + } + + function get_pll_context($class) + { + $this->pll_context_called = true; + + if ($this->is_graphql_request()) { return 'PLL_Admin'; } return $class; - }, - 10, - 1 -); - -add_action('graphql_init', function () { - if (!isGraphqlContext()) { - return; } - (new PolylangTypes())->init(); - (new PostObject())->init(); - (new TermObject())->init(); - (new LanguageRootQueries())->init(); - (new MenuItem())->init(); - (new StringsTranslations())->init(); -}); + function is_graphql_request() + { + if (!defined('POLYLANG_VERSION')) { + return false; + } + + if (defined('GRAPHQL_POLYLANG_TESTS')) { + return true; + } + + if (defined('GRAPHQL_HTTP_REQUEST')) { + return GRAPHQL_HTTP_REQUEST; + } + + // XXX GRAPHQL_HTTP_REQUEST is not defined early enough! + if ('/graphql' == $_SERVER['REQUEST_URI']) { + return true; + } + + return false; + } +} + +(new Loader())->init();