Skip to content
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

[fix] Fatal error #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions wp-rest-polylang.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -32,7 +32,7 @@ public static function getInstance() {
return self::$instance;
}

public static function init() {
public function init() {
global $polylang;

if (isset($_GET['lang'])) {
Expand All @@ -42,12 +42,20 @@ public static 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) {
Expand All @@ -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']);

Expand Down