-
Notifications
You must be signed in to change notification settings - Fork 2
feat(rest): POST /cdcf/v1/link-term-translations + Python client method #205
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
Merged
Merged
Changes from all commits
Commits
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
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
81 changes: 81 additions & 0 deletions
81
wordpress/themes/cdcf-headless/includes/handlers/link-term-translations.php
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,81 @@ | ||
| <?php | ||
| /** | ||
| * REST route handler for /cdcf/v1/link-term-translations. | ||
| * | ||
| * Term equivalent of /link-translations. Given a {lang => term_id} map | ||
| * of two or more already-existing terms in a single taxonomy, sets each | ||
| * term's Polylang language and links them into a single translation | ||
| * group in one atomic pll_save_term_translations() call. | ||
| * | ||
| * Use case: repairing corrupted Polylang term groups (e.g. after a | ||
| * propagation bug scrambled sibling links) or seeding language metadata | ||
| * on terms created outside the normal flow. Polylang's term-side | ||
| * language and translation-group helpers are PHP-only — there's no | ||
| * native REST surface for them, hence this thin wrapper. | ||
| * | ||
| * Extracted from functions.php so the body can be unit-tested with | ||
| * Brain Monkey + Mockery. | ||
| */ | ||
|
|
||
| if (defined('ABSPATH') === false) { | ||
| return; | ||
| } | ||
|
|
||
| function cdcf_rest_link_term_translations(WP_REST_Request $request) { | ||
| if ( | ||
| !function_exists('pll_set_term_language') | ||
| || !function_exists('pll_save_term_translations') | ||
| ) { | ||
| return new WP_Error('polylang_missing', 'Polylang is not active.', ['status' => 500]); | ||
| } | ||
|
|
||
| $taxonomy = $request['taxonomy']; | ||
| if (!is_string($taxonomy) || $taxonomy === '' || !taxonomy_exists($taxonomy)) { | ||
| return new WP_Error( | ||
| 'invalid_taxonomy', | ||
| "Taxonomy '{$taxonomy}' does not exist.", | ||
| ['status' => 400] | ||
| ); | ||
| } | ||
|
|
||
| $translations = $request['translations']; | ||
| if (!is_array($translations) || count($translations) < 2) { | ||
| return new WP_Error( | ||
| 'invalid_translations', | ||
| 'Provide at least 2 language => term_id pairs.', | ||
| ['status' => 400] | ||
| ); | ||
| } | ||
|
|
||
| // Validate all terms exist in the named taxonomy. | ||
| foreach ($translations as $lang => $term_id) { | ||
| $term_id = (int) $term_id; | ||
| $term = get_term($term_id, $taxonomy); | ||
| if (!$term || is_wp_error($term)) { | ||
| return new WP_Error( | ||
| 'invalid_term', | ||
| "Term {$term_id} does not exist in taxonomy '{$taxonomy}'.", | ||
| ['status' => 400] | ||
| ); | ||
| } | ||
| $translations[$lang] = $term_id; | ||
| } | ||
|
|
||
| // Set language on each term, then atomically link the group. | ||
| foreach ($translations as $lang => $term_id) { | ||
| pll_set_term_language($term_id, $lang); | ||
| } | ||
| if (pll_save_term_translations($translations) === false) { | ||
| return new WP_Error( | ||
| 'link_failed', | ||
| 'Polylang refused to save the term translation group.', | ||
| ['status' => 500] | ||
| ); | ||
| } | ||
|
|
||
| return rest_ensure_response([ | ||
| 'success' => true, | ||
| 'taxonomy' => $taxonomy, | ||
| 'translations' => $translations, | ||
| ]); | ||
| } |
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,48 @@ | ||
| <?php | ||
| /** | ||
| * Shared sanitize_callback helpers for REST route arg declarations. | ||
| * | ||
| * Per the convention documented in CLAUDE.md (settled in #111): | ||
| * - Sanitization happens ONCE at register_rest_route() args time. | ||
| * - Handlers trust the sanitized input and do NOT re-sanitize. | ||
| * - Structural validation + contextual WP_Error returns stay in the | ||
| * handler body (e.g. count-checks, existence-checks). | ||
| * | ||
| * Functions are top-level (callable by name from sanitize_callback). | ||
| */ | ||
|
|
||
| defined('ABSPATH') || exit; | ||
|
|
||
| /** | ||
| * Sanitize the {lang => id} translations map used by both | ||
| * /cdcf/v1/link-translations (posts) and /cdcf/v1/link-term-translations | ||
| * (terms). Coerces to an associative array of language-code keys to | ||
| * positive integer IDs; silently drops entries whose keys are not a | ||
| * recognized language-code shape (`/^[a-z]{2}(-[A-Z]{2})?$/`, covering | ||
| * ISO 639-1 alone plus optional ISO 3166-1 region — matches the | ||
| * Polylang slug shape the rest of the site uses), and entries whose | ||
| * IDs are zero or non-numeric after absint(). The handler is then | ||
| * responsible for the count >= 2 structural check and the per-id | ||
| * existence check, both of which return contextual WP_Error. | ||
| * | ||
| * @param mixed $value Whatever the client sent (usually a | ||
| * JSON-decoded associative array; WP REST | ||
| * framework leaves objects as arrays). | ||
| * @return array<string, int> Empty array on no valid entries. | ||
| */ | ||
| function cdcf_sanitize_translations_map($value): array { | ||
| if (!is_array($value)) { | ||
| return []; | ||
| } | ||
| $sanitized = []; | ||
| foreach ($value as $lang => $id) { | ||
| if (!is_string($lang) || !preg_match('/^[a-z]{2}(-[A-Z]{2})?$/', $lang)) { | ||
| continue; | ||
| } | ||
| $id = absint($id); | ||
| if ($id > 0) { | ||
| $sanitized[$lang] = $id; | ||
| } | ||
| } | ||
| return $sanitized; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.