Skip to content

Commit

Permalink
comments++
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Apr 15, 2020
1 parent 0f97ee7 commit 1f1a30a
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/OptionsPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@

class OptionsPages
{
static $language_root_queries = [];

/**
* Mapping of used options page root queries to their selected languages
*/
static $root_query_locale_mapping = [];


/**
* Language of the currently resolving options page field
*/
static $current_language = null;

static function init()
Expand Down Expand Up @@ -46,6 +55,9 @@ static function init()
);
}

/**
* Add language arg to all options page root queries
*/
static function __action_graphql_RootQuery_fields($fields)
{
foreach (self::get_options_page_root_queries() as $root_query) {
Expand Down Expand Up @@ -74,22 +86,25 @@ static function __action_graphql_before_resolve_field(
$field_key,
$field
) {
/**
* Record what languages are used by the options page root queries
*/
if (self::is_options_page_root_query($info)) {
$model = \PLL()->model;
$lang = $model->get_language($args['language'])->locale ?? null;

if ($lang) {
self::$language_root_queries[$info->path[0]] = $lang;
self::$root_query_locale_mapping[$info->path[0]] = $lang;
}
}

/**
* If resolving field under options page root query set the current language
*/
if (self::is_options_page($source)) {
$root_query = $info->path[0];
$lang = self::$language_root_queries[$root_query] ?? null;
$lang = self::$root_query_locale_mapping[$root_query] ?? null;
if ($lang) {
error_log(
"Setting lang $field_key ::" . self::$current_language
);
self::$current_language = $lang;
}
}
Expand All @@ -105,6 +120,9 @@ static function __action_graphql_after_resolve_field(
$field_key,
$field
) {
/**
* Clear the current language after the field has been resolved
*/
if (self::is_options_page($source)) {
self::$current_language = null;
}
Expand Down Expand Up @@ -161,6 +179,11 @@ static function is_options_page_root_query(ResolveInfo $info)
return in_array($info->fieldName, $root_queries);
}

/**
* Return array of the options page root query names.
*
* This requires that 'graphql_field_name' is passed to acf_add_options_page()
*/
static function get_options_page_root_queries()
{
$graphql_options_pages = acf_get_options_pages();
Expand Down

0 comments on commit 1f1a30a

Please sign in to comment.