Update: Filter by polylang language on REST Query

This commit is contained in:
Siroco 2023-02-06 22:29:37 +00:00
parent ad8d0b610c
commit cdfe2f63d7

View File

@ -399,6 +399,27 @@ if (!class_exists('Soundmap')){
/* REST API Options */
add_action('rest_api_init', function() {
/* REST API Polylang Expand */
if ( isset($_GET['lang']) && function_exists('pll_default_language') ) {
global $polylang;
$default = pll_default_language();
$langs = pll_languages_list();
$cur_lang = $_GET['lang'];
if (!in_array($cur_lang, $langs)) {
$cur_lang = $default;
}
$polylang->curlang = $polylang->model->get_language($cur_lang);
$GLOBALS['text_direction'] = $polylang->curlang->is_rtl ? 'rtl' : 'ltr';
}
});
add_action('rest_api_init', function() {
/* REST API expand fields */
@ -438,7 +459,6 @@ if (!class_exists('Soundmap')){
));
});
@ -785,6 +805,37 @@ if (!class_exists('Soundmap')){
function register_filters(){
//add_filter( 'pre_get_posts', array($this, 'pre_get_posts'));
add_filter('rest_prepare_marker', array($this,'soinumapa_rest_prepare_marker'));
add_filter('rest_prepare_page', array($this,'soinumapa_rest_prepare_marker'));
add_filter('rest_prepare_post', array($this,'soinumapa_rest_prepare_marker'));
add_filter('rest_marker_query',array($this,'soinumapa_rest_marker_by_language'));
add_filter('rest_page_query',array($this,'soinumapa_rest_marker_by_language'));
add_filter('rest_post_query',array($this,'soinumapa_rest_marker_by_language'));
}
function soinumapa_rest_prepare_marker($data) {
$_data = $data->data;
$post = get_post($data->data['id']);
if ( function_exists('pll_get_post') ) {
$_data['postname_eu'] = get_post(pll_get_post($post->ID,'eu'))->post_name;
$_data['postname_en'] = get_post(pll_get_post($post->ID,'en'))->post_name;
$_data['postname_es'] = get_post(pll_get_post($post->ID,'es'))->post_name;
}
if ( function_exists('pll_get_post_language') ) {
$_data['postname_language'] = pll_get_post_language($post->ID);
}
$data->data = $_data;
return $data;
}
function soinumapa_rest_marker_by_language($args) {
global $polylang;
$language = $polylang->curlang->slug;
if (isset($language)) { $args['lang'] = $language; }
return $args;
}
function set_query($query){