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

@ -397,7 +397,28 @@ if (!class_exists('Soundmap')){
add_action('wp_ajax_nopriv_soundmap-get-markers',array($this,'soundmap_get_markers'));
add_action('wp_ajax_soundmap-get-content',array($this, 'soundmap_get_content'));
/* REST API Options */
/* 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 */
@ -435,11 +456,10 @@ if (!class_exists('Soundmap')){
'methods' => 'GET',
'callback' => 'soundmap_save_term_translation',
'permission_callback' => '__return_true'
));
));
});
});
@ -784,8 +804,39 @@ if (!class_exists('Soundmap')){
}
function register_filters(){
//add_filter( 'pre_get_posts', array($this, 'pre_get_posts'));
}
//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){
if (!is_array($query)){