Skip to content

Commit

Permalink
Use __filter_ naming and run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Mar 6, 2020
1 parent f4623e3 commit 16498a9
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 85 deletions.
9 changes: 7 additions & 2 deletions src/LanguageRootQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
56 changes: 28 additions & 28 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand All @@ -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;

Expand All @@ -62,7 +62,7 @@ function get_pll_context($class)
return $class;
}

function admin_notices()
function __action_admin_notices()
{
if (!is_super_admin()) {
return;
Expand Down Expand Up @@ -92,15 +92,15 @@ function endsWith($haystack, $needle)
return true;
}

return (substr($haystack, -$length) === $needle);
return substr($haystack, -$length) === $needle;
}

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;
Expand All @@ -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;
}
}
}
14 changes: 9 additions & 5 deletions src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +83,7 @@ function () {
);
}

function register_fields()
function __action_graphql_register_types()
{
register_graphql_fields('RootQueryToMenuItemConnectionWhereArgs', [
'language' => [
Expand Down
9 changes: 7 additions & 2 deletions src/PolylangTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
23 changes: 16 additions & 7 deletions src/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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)',
],
]);

Expand Down
9 changes: 7 additions & 2 deletions src/StringsTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 9 additions & 4 deletions src/TermObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'];
Expand All @@ -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));
Expand Down
Loading

0 comments on commit 16498a9

Please sign in to comment.