-
Notifications
You must be signed in to change notification settings - Fork 17
Enable taxonomy term translations #17
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
elskwid
wants to merge
5
commits into
rburgst:master
Choose a base branch
from
mutations:dm/enable-taxonomy-term-translations
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
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<?php | ||
|
||
use GraphQLRelay\Relay; | ||
use WPGraphQL\Data\Connection\AbstractConnectionResolver; | ||
|
||
/** | ||
|
@@ -178,6 +179,135 @@ function wpgraphqlwpml_add_post_type_fields(\WP_Post_Type $post_type_object) | |
); | ||
} | ||
|
||
function wpgraphqlwpml_add_taxonomy_type_fields(\WP_Taxonomy $taxonomy) | ||
{ | ||
// Locale information for the current taxonomy | ||
register_graphql_field( | ||
$taxonomy->graphql_single_name, | ||
'locale', | ||
[ | ||
'type' => 'Locale', | ||
'description' => __('WPML translation link', 'wp-graphql-wpml'), | ||
'resolve' => function ( | ||
\WPGraphQL\Model\Term $term, | ||
$args, | ||
$context, | ||
$info | ||
) use ($taxonomy) { | ||
global $sitepress; | ||
|
||
$fields = $info->getFieldSelection(); | ||
|
||
$language = [ | ||
'id' => null, | ||
'locale' => null, | ||
]; | ||
|
||
$wpml_element_type = 'tax_' . $taxonomy->name; | ||
$language_code = $sitepress->get_language_for_element( $term->term_id, $wpml_element_type); | ||
$locale = $sitepress->get_locale( $language_code ); | ||
|
||
if (!$locale) { | ||
return null; | ||
} | ||
|
||
$language['id'] = $locale; | ||
|
||
if (isset($fields['locale'])) { | ||
$language['locale'] = $locale; | ||
} | ||
|
||
return $language; | ||
}, | ||
] | ||
); | ||
|
||
// Collection of available translations for this taxonomy | ||
register_graphql_field( | ||
$taxonomy->graphql_single_name, | ||
'translations', | ||
[ | ||
'type' => ['list_of' => 'TermTranslation'], | ||
'description' => __('WPML translations', 'wpnext'), | ||
'resolve' => function ( | ||
\WPGraphQL\Model\Term $term, | ||
$args, | ||
$context, | ||
$info | ||
) use ($taxonomy) { | ||
global $sitepress; | ||
|
||
$translations = []; | ||
$languages = $sitepress->get_active_languages(); | ||
$original_language_code = $sitepress->get_current_language(); | ||
$original_term_id = $term->term_id; | ||
|
||
foreach ($languages as $language) { | ||
$language_code = array_key_exists('language_code', $languages) ? $language['language_code'] : $language['code']; | ||
$term_id = wpml_object_id_filter($original_term_id, $taxonomy->name, false, $language_code); | ||
|
||
if ($term_id === null || $term_id == $original_term_id) continue; | ||
|
||
// Relies on adjust ids feature being 'off' | ||
$translated_term = get_term($term_id, $taxonomy->name); | ||
$translated_url = get_term_link($translated_term); | ||
|
||
$translations[] = array( | ||
'databaseId' => $translated_term->term_id, | ||
'href' => $translated_url, | ||
'id' => Relay::toGlobalId( 'term', (string) $translated_term->term_id ), | ||
'locale' => $language['default_locale'], | ||
'name' => $translated_term->name, | ||
'slug' => $translated_term->slug, | ||
); | ||
} | ||
|
||
return $translations; | ||
}, | ||
] | ||
); | ||
|
||
// Collection of translated versions of the taxonomy | ||
register_graphql_field( | ||
$taxonomy->graphql_single_name, | ||
'translated', | ||
[ | ||
'type' => ['list_of' => 'TermNode'], | ||
'description' => __('WPML translated versions of the term', 'wpnext'), | ||
'resolve' => function ( | ||
\WPGraphQL\Model\Term $term, | ||
$args, | ||
$context, | ||
$info | ||
) use ($taxonomy) { | ||
global $sitepress; | ||
|
||
$fields = $info->getFieldSelection(); | ||
|
||
$translations = []; | ||
$languages = $sitepress->get_active_languages(); | ||
$original_language_code = $sitepress->get_current_language(); | ||
$original_term_id = $term->term_id; | ||
|
||
foreach ($languages as $language) { | ||
$language_code = array_key_exists('language_code', $languages) ? $language['language_code'] : $language['code']; | ||
$term_id = wpml_object_id_filter($original_term_id, $taxonomy->name, false, $language_code); | ||
|
||
if ($term_id === null || $term_id == $original_term_id) continue; | ||
|
||
$translation = new \WPGraphQL\Model\Term( | ||
\WP_Term::get_instance($term_id, $taxonomy->name) | ||
); | ||
|
||
array_push($translations, $translation); | ||
} | ||
|
||
return $translations; | ||
}, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param int $post_id | ||
* @param $language | ||
|
@@ -299,6 +429,58 @@ function wpgraphqlwpml_action_graphql_register_types() | |
], | ||
]); | ||
|
||
register_graphql_object_type('TermTranslation', [ | ||
'description' => __('Term Translation (WPML)', 'wp-graphql-wpml'), | ||
'fields' => [ | ||
'id' => [ | ||
'type' => [ | ||
'non_null' => 'ID', | ||
], | ||
'description' => __( | ||
'the id of the referenced translation (WPML)', | ||
'wp-graphql-wpml' | ||
), | ||
], | ||
'databaseId' => [ | ||
'type' => [ | ||
'non_null' => 'Int', | ||
], | ||
'description' => __( | ||
'the primary key from the database for the referenced translation (WPML)', | ||
'wp-graphql-wpml' | ||
), | ||
], | ||
'href' => [ | ||
'type' => 'String', | ||
'description' => __( | ||
'the relative link to the translated content (WPML)', | ||
'wp-graphql-wpml' | ||
), | ||
], | ||
'locale' => [ | ||
'type' => 'String', | ||
'description' => __( | ||
'Language code (WPML)', | ||
'wp-graphql-wpml' | ||
), | ||
], | ||
'name' => [ | ||
'type' => 'String', | ||
'description' => __( | ||
'the name of the translated taxonomy (WPML)', | ||
'wp-graphql-wpml' | ||
), | ||
], | ||
'slug' => [ | ||
'type' => 'String', | ||
'description' => __( | ||
'the slug of the translated taxonomy (WPML)', | ||
'wp-graphql-wpml' | ||
), | ||
], | ||
], | ||
]); | ||
|
||
register_graphql_fields('RootQueryToMenuItemConnectionWhereArgs', [ | ||
'language' => [ | ||
'type' => 'String', | ||
|
@@ -374,9 +556,14 @@ function wpgraphqlwpml_action_graphql_register_types() | |
} | ||
], | ||
]); | ||
|
||
foreach (\WPGraphQL::get_allowed_post_types() as $post_type) { | ||
wpgraphqlwpml_add_post_type_fields(get_post_type_object($post_type)); | ||
} | ||
|
||
foreach (\WPGraphQL::get_allowed_taxonomies() as $taxonomy_type) { | ||
wpgraphqlwpml_add_taxonomy_type_fields(get_taxonomy($taxonomy_type)); | ||
} | ||
} | ||
|
||
function wpgraphqlwpml__theme_mod_nav_menu_locations(array $args) | ||
|
@@ -582,6 +769,32 @@ function wpgraphqlwpml__switch_language_to_all_for_query(array $args) | |
return $args; | ||
} | ||
|
||
/** | ||
* Remove the `get_term` filter added by WPML during WPGraphQL requests. | ||
* This filter forces WPML to adjust term ids *before* other queries are | ||
* run. There is a WPML setting named `auto_adjust_ids` that will turn | ||
* off this feature, but it should never be on for GraphQL queries. | ||
*/ | ||
function wpgraphqlwpml__remove_term_adjust_id_filter() { | ||
global $sitepress; | ||
|
||
remove_filter('get_term', array($sitepress, 'get_term_adjust_id'), 1); | ||
} | ||
|
||
/** | ||
* Set the `icl_adjust_id_url_filter_off` global used in the | ||
* `WPML_Term_Adjust_Id->filter()` function to indicate if term ids | ||
* should be adjusted. Setting this global to `true` ensures that | ||
* term ids are not adjusted during GraphQL queries. | ||
*/ | ||
function wpgraphqlwpml__set_global_adjust_id_filter_off() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am confused here, doesnt do this the same as line 773? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// Set global for adjust ids in case the filter is added | ||
// again at some point in the request | ||
global $icl_adjust_id_url_filter_off; | ||
|
||
$icl_adjust_id_url_filter_off = true; | ||
} | ||
|
||
function wpgraphqlwpml_action_init() | ||
{ | ||
if (!wpgraphqlwpml_is_graphql_request()) { | ||
|
@@ -639,9 +852,23 @@ function wpgraphqlwpml_action_init() | |
10, | ||
2 | ||
); | ||
} | ||
|
||
// Remove the adjust id filter during WPGraphQL requests | ||
add_action( | ||
'init_graphql_request', | ||
'wpgraphqlwpml__remove_term_adjust_id_filter', | ||
10, | ||
0 | ||
); | ||
|
||
add_action('graphql_init', 'wpgraphqlwpml_action_init'); | ||
// Set the global adjust id filter to allow for multi-language term queries | ||
add_action( | ||
'init_graphql_request', | ||
'wpgraphqlwpml__set_global_adjust_id_filter_off', | ||
10, | ||
0 | ||
); | ||
} | ||
|
||
|
||
add_action('graphql_init', 'wpgraphqlwpml_action_init'); |
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.
I need to double check this, I am currently working on filtering menus and menu items and had a case where I actually need to get the adjusted ids.
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.
@rburgst no problem! We could probably set it conditionally if needed. We were lucky enough to not need any menu data for our project but the adjusted ids caused some serious havoc with Gatsby pulling translated terms.
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.
In fact, we could use this or the constant below and set them around the resolver functions if that would work better for you.