-
Notifications
You must be signed in to change notification settings - Fork 17
Add a language filter for ACF options pages #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ririshi
wants to merge
6
commits into
rburgst:master
Choose a base branch
from
Ririshi:feat/acf-options-pages-language-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d8f910
Add gitignore to exclude JetBrains files
9e94b19
Reformat indentation/braces/spaces/wrapping, add use statements, remo…
e5de655
Replace deprecated getArgs calls, add @throws tags, remove duplicate/…
74e0c6b
(WIP) attempt to add wpml language filter to ACF options page type
615c489
Move ACF functionality to own file, add options page language argumen…
fb6a3a2
Add ACF class exists check
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # JetBrains IDE files | ||
| .idea/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| use WPGraphQL\Registry\TypeRegistry; | ||
| use WPGraphQL\Utils\Utils; | ||
|
|
||
| /** | ||
| * Registers a new "wpmlLanguage" input field on ACF options pages queries, and | ||
| * uses translated options page content based on the language code supplied | ||
| * @throws Exception | ||
| */ | ||
| function wpgraphqlwpml_action_add_options_pages_language_filter(TypeRegistry $type_registry) | ||
| { | ||
| foreach (acf_get_options_pages() as $options_page) { | ||
| // Check if the option page should be shown in GraphQL schema | ||
| if (!isset($options_page['show_in_graphql']) || false === (bool)$options_page['show_in_graphql']) { | ||
| continue; | ||
| } | ||
| $type_name = Utils::format_type_name($options_page['graphql_field_name'] ?? $options_page['menu_slug']); | ||
| // Register new options page field with the wpmlLanguage argument | ||
| $options_page['type'] = 'options_page'; | ||
| $type_registry->register_field( | ||
| 'RootQuery', | ||
| Utils::format_field_name($type_name), | ||
| [ | ||
| 'type' => $type_name, | ||
| 'args' => [ | ||
| 'wpmlLanguage' => [ | ||
| 'type' => 'String', | ||
| 'description' => 'Filter by WPML language code', | ||
| ], | ||
| ], | ||
| 'description' => sprintf(__('%s options.', 'wp-graphql-acf'), $options_page['page_title']), | ||
| 'resolve' => function ($unused, $args) use ($options_page) { | ||
| // If the wpmlLanguage argument exists in the arguments | ||
| if (isset($args['wpmlLanguage'])) { | ||
| $lang = $args['wpmlLanguage']; | ||
| global $sitepress; | ||
| // If WPML is installed | ||
| if ($sitepress) { | ||
| // Switch the current locale WPML | ||
| $sitepress->switch_lang($lang); | ||
| // Override ACF language explicitly, otherwise the output language doesn't change | ||
| acf_update_setting('current_language', $lang); | ||
| } | ||
| } | ||
| return !empty($options_page) ? $options_page : null; | ||
| } | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function wpgraphqlwpml_init_acf() | ||
| { | ||
| add_action( | ||
| 'graphql_register_types', | ||
| 'wpgraphqlwpml_action_add_options_pages_language_filter', | ||
| 10, | ||
| 1 | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldnt we switch the locale back to what it was before after this resolver? Otherwise this will cause resolvers further down in the call chain to behave different, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something that seems to be happening, yes. I'm using it to my advantage by following it up with a query that has no language filter, but it would be better to reset the language to the default. Or maybe even better, save what it was before and set that back.