| 1 |
<?php |
| 2 |
|
| 3 |
// setups basic functions |
| 4 |
abstract class Polylang_Base { |
| 5 |
|
| 6 |
// returns the query variables of the referer |
| 7 |
function get_referer_vars() { |
| 8 |
$qvars = array(); |
| 9 |
$referer = wp_get_referer(); |
| 10 |
if ($referer) { |
| 11 |
$urlparts = parse_url($referer); |
| 12 |
if (isset($urlparts['query'])) { |
| 13 |
parse_str($urlparts['query'], $qvars); |
| 14 |
} |
| 15 |
} |
| 16 |
return $qvars; |
| 17 |
} |
| 18 |
|
| 19 |
// returns the list of available languages |
| 20 |
function get_languages_list($hide_empty = false) { |
| 21 |
return get_terms('language', array('hide_empty'=>$hide_empty)); |
| 22 |
} |
| 23 |
|
| 24 |
// returns the language by its id or its slug |
| 25 |
// Note: it seems that the first option is better for performance (3.2.1) |
| 26 |
function get_language($value) { |
| 27 |
if (is_numeric($value)) |
| 28 |
return get_term($value, 'language'); |
| 29 |
elseif (is_string($value)) |
| 30 |
return get_term_by('slug', $value , 'language'); // seems it is not cached in 3.2.1 |
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
// deletes a translation of a post or term |
| 35 |
function delete_translation($type, $id) { |
| 36 |
$translations = unserialize(get_metadata($type, $id, '_translations', true)); |
| 37 |
if (is_array($translations)) { |
| 38 |
$slug = array_search($id, $translations); |
| 39 |
unset($translations[$slug]); |
| 40 |
$tr = serialize($translations); |
| 41 |
foreach($translations as $key=>$p) |
| 42 |
update_metadata($type, $p, '_translations', $tr); |
| 43 |
delete_metadata($type, $id, '_translations'); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// returns the id of the translation of a post or term |
| 48 |
function get_translation($type, $id, $lang) { |
| 49 |
$translations = unserialize(get_metadata($type, $id, '_translations', true)); |
| 50 |
$slug = is_string($lang) ? $lang : $lang->slug; |
| 51 |
return isset($translations[$slug]) ? $translations[$slug] : ''; |
| 52 |
} |
| 53 |
|
| 54 |
// returns the language of a post |
| 55 |
function get_post_language($post_id) { |
| 56 |
$lang = get_the_terms($post_id, 'language' ); |
| 57 |
return ($lang) ? reset($lang) : null; // there's only one language per post : first element of the array returned |
| 58 |
} |
| 59 |
|
| 60 |
// among the post and its translations, returns the id of the post which is in $lang |
| 61 |
function get_post($post_id, $lang) { |
| 62 |
$slug = is_string($lang) ? $lang : $lang->slug; |
| 63 |
return $this->get_post_language($post_id)->slug == $slug ? $post_id : $this->get_translation('post', $post_id, $lang); |
| 64 |
} |
| 65 |
|
| 66 |
function update_term_language($term_id, $lang) { |
| 67 |
if (is_numeric($lang)) |
| 68 |
$lang_id = $lang; |
| 69 |
elseif(is_string($lang)) |
| 70 |
$lang_id = $this->get_language($lang)->term_id; |
| 71 |
else |
| 72 |
$lang_id = $lang->term_id; |
| 73 |
|
| 74 |
update_metadata('term', $term_id, '_language', $lang_id ); |
| 75 |
} |
| 76 |
|
| 77 |
function delete_term_language($term_id) { |
| 78 |
delete_metadata('term', $term_id, '_language'); |
| 79 |
} |
| 80 |
|
| 81 |
// returns the language of a term |
| 82 |
function get_term_language($value, $taxonomy = '') { |
| 83 |
if (is_numeric($value)) |
| 84 |
$term_id = $value; |
| 85 |
elseif (is_string($value) && $taxonomy) |
| 86 |
$term_id = get_term_by('slug', $value , $taxonomy)->term_id; |
| 87 |
return $term_id ? $this->get_language(get_metadata('term', $term_id, '_language', true)) : null; |
| 88 |
} |
| 89 |
|
| 90 |
// among the term and its translations, returns the id of the term which is in $lang |
| 91 |
function get_term($term_id, $lang) { |
| 92 |
$slug = is_string($lang) ? $lang : $lang->slug; |
| 93 |
return $this->get_term_language($term_id)->slug == $slug ? $term_id : $this->get_translation('term', $term_id, $lang); |
| 94 |
} |
| 95 |
|
| 96 |
// returns the html link to the flag if exists |
| 97 |
function get_flag($lang) { |
| 98 |
return ( !is_admin() && ( // never use local flags on admin side |
| 99 |
file_exists(POLYLANG_DIR.($file = '/local_flags/'.$lang->description.'.png')) || |
| 100 |
file_exists(POLYLANG_DIR.($file = '/local_flags/'.$lang->description.'.jpg')) )) || |
| 101 |
file_exists(POLYLANG_DIR.($file = '/flags/'.$lang->description.'.png')) ? |
| 102 |
'<img src="'.esc_url(WP_PLUGIN_URL.'/polylang'.$file).'" alt="'.esc_attr($lang->name).'" />' : ''; |
| 103 |
} |
| 104 |
|
| 105 |
// adds terms clauses to get_terms - used in both frontend and admin |
| 106 |
function _terms_clauses($clauses, $lang, $display_all = false) { |
| 107 |
global $wpdb; |
| 108 |
if (isset($lang) && !is_wp_error($lang)) { |
| 109 |
$clauses['join'] .= " LEFT JOIN $wpdb->termmeta AS tm ON t.term_id = tm.term_id"; |
| 110 |
$where_lang = $wpdb->prepare("tm.meta_key = '_language' AND tm.meta_value = %d", $lang->term_id); // add terms in the right language |
| 111 |
$where_all = "t.term_id NOT IN (SELECT term_id FROM $wpdb->termmeta WHERE meta_key IN ('_language'))"; // add terms with no language set |
| 112 |
$clauses['where'] .= $display_all ? " AND (($where_lang) OR ($where_all))" : " AND $where_lang"; |
| 113 |
} |
| 114 |
return $clauses; |
| 115 |
} |
| 116 |
|
| 117 |
} //class Polylang_Base |
| 118 |
?> |
| 119 |
|