From 113fbd77cb164b6c8f2edaab14ce10e46f30ce56 Mon Sep 17 00:00:00 2001 From: Fabrice Date: Tue, 19 Oct 2021 18:46:05 +0200 Subject: [PATCH 1/2] [fix] Fatal error Fatal error: Uncaught Error: Using $this when not in object context in wp-rest-polylang.php on line 58 --- wp-rest-polylang.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-rest-polylang.php b/wp-rest-polylang.php index c09b2a9..3d8cc81 100644 --- a/wp-rest-polylang.php +++ b/wp-rest-polylang.php @@ -32,7 +32,7 @@ public static function getInstance() { return self::$instance; } - public static function init() { + public function init() { global $polylang; if (isset($_GET['lang'])) { From dc7606e2c4083f8dd204dcb73b531b28207a3a2e Mon Sep 17 00:00:00 2001 From: Fabrice Date: Wed, 9 Feb 2022 00:24:46 +0100 Subject: [PATCH 2/2] maj pour ajouter la lang dans la taxo --- wp-rest-polylang.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/wp-rest-polylang.php b/wp-rest-polylang.php index 3d8cc81..f275911 100644 --- a/wp-rest-polylang.php +++ b/wp-rest-polylang.php @@ -4,7 +4,7 @@ * Description: Polylang integration for the WP REST API * Author: Marc-Antoine Ruel * Author URI: https://www.marcantoineruel.com - * Version: 1.0.0 + * Version: 1.1.0 * Plugin URI: https://github.com/maru3l/wp-rest-polylang * License: gpl-3.0 */ @@ -42,12 +42,20 @@ public function init() { } $post_types = get_post_types( array( 'public' => true ), 'names' ); - + foreach( $post_types as $post_type ) { if (pll_is_translated_post_type( $post_type )) { self::register_api_field($post_type); } } + + $taxonomies = get_taxonomies(['show_in_rest' => true], 'names'); + + foreach ($taxonomies as $taxonomy) { + if (pll_is_translated_taxonomy($taxonomy)) { + self::register_api_taxonomy($taxonomy); + } + } } public function register_api_field($post_type) { @@ -70,10 +78,27 @@ public function register_api_field($post_type) { ); } + public function register_api_taxonomy($taxonomy) + { + register_rest_field( + $taxonomy, + "polylang_current_lang", + array( + "get_callback" => array($this, "get_current_taxonomy_lang"), + "schema" => null, + ) + ); + } + public function get_current_lang( $object ) { return pll_get_post_language($object['id'], 'locale'); } + public function get_current_taxonomy_lang($object) + { + return pll_get_term_language($object['id'], 'locale'); + } + public function get_translations( $object ) { $translations = pll_get_post_translations($object['id']);