Skip to content

Commit

Permalink
Die with error message when using too old Polylang version
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed May 7, 2019
1 parent 18ad67d commit 83907ec
Showing 1 changed file with 62 additions and 43 deletions.
105 changes: 62 additions & 43 deletions wp-graphql-polylang.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace WPGraphQL\Extensions\Polylang;

use GraphQL\Error\UserError;

define('WPGRAPHQL_POLYLANG', true);

require_once __DIR__ . '/src/Helpers.php';
Expand All @@ -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();

0 comments on commit 83907ec

Please sign in to comment.