| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* a language object is made of two terms in 'language' and 'term_language' taxonomies |
| 5 |
* manipulating only one object per language instead of two terms should make things easier |
| 6 |
* |
| 7 |
* properties: |
| 8 |
* term_id => id of term in 'language' taxonomy |
| 9 |
* name => language name. Ex: English |
| 10 |
* slug => language code used in url. Ex: en |
| 11 |
* term_group => order of the language when displayed in a list of languages |
| 12 |
* term_taxonomy_id => term taxonomy id in 'language' taxonomy |
| 13 |
* taxonomy => 'language' |
| 14 |
* description => language locale for backward compatibility |
| 15 |
* parent => 0 / not used |
| 16 |
* count => number of posts and pages in that language |
| 17 |
* tl_term_id => id of the term in 'term_language' taxonomy |
| 18 |
* tl_term_taxonomy_id => term taxonomy id in 'term_language' taxonomy |
| 19 |
* tl_count => number of terms in that language (not used by Polylang) |
| 20 |
* locale => language locale. Ex: en_US |
| 21 |
* is_rtl => 1 if the language is rtl |
| 22 |
* flag_url => url of the flag |
| 23 |
* flag => html img of the flag |
| 24 |
* custom_flag_url => url of the custom flag if exists, internal use only, moves to flag_url on frontend |
| 25 |
* custom_flag => html img of the custom flag if exists, internal use only, moves to flag on frontend |
| 26 |
* home_url => home url in this language |
| 27 |
* search_url => home url to use in search forms |
| 28 |
* host => host of this language |
| 29 |
* mo_id => id of the post storing strings translations |
| 30 |
* |
| 31 |
* @since 1.2 |
| 32 |
*/ |
| 33 |
class PLL_Language { |
| 34 |
public $term_id, $name, $slug, $term_group, $term_taxonomy_id, $taxonomy, $description, $parent, $count; |
| 35 |
public $tl_term_id, $tl_term_taxonomy_id, $tl_count; |
| 36 |
public $locale, $is_rtl; |
| 37 |
public $flag_url, $flag; |
| 38 |
public $home_url, $search_url; |
| 39 |
public $host, $mo_id; |
| 40 |
|
| 41 |
/* |
| 42 |
* constructor: builds a language object given its two corresponding terms in language and term_language taxonomies |
| 43 |
* |
| 44 |
* @since 1.2 |
| 45 |
* |
| 46 |
* @param object|array $language 'language' term or language object properties stored as an array |
| 47 |
* @param object $term_language corresponding 'term_language' term |
| 48 |
*/ |
| 49 |
public function __construct($language, $term_language = null) { |
| 50 |
// build the object from all properties stored as an array |
| 51 |
if (empty($term_language)) { |
| 52 |
foreach ($language as $prop => $value) |
| 53 |
$this->$prop = $value; |
| 54 |
} |
| 55 |
|
| 56 |
// build the object from taxonomies |
| 57 |
else { |
| 58 |
foreach ($language as $prop => $value) |
| 59 |
$this->$prop = in_array($prop, array('term_id', 'term_taxonomy_id', 'count')) ? (int) $language->$prop : $language->$prop; |
| 60 |
|
| 61 |
// although it would be convenient here, don't assume the term is shared between taxonomies as it may not be the case in future |
| 62 |
// http://make.wordpress.org/core/2013/07/28/potential-roadmap-for-taxonomy-meta-and-post-relationships/ |
| 63 |
$this->tl_term_id = (int) $term_language->term_id; |
| 64 |
$this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id; |
| 65 |
$this->tl_count = (int) $term_language->count; |
| 66 |
|
| 67 |
$description = maybe_unserialize($language->description); |
| 68 |
$this->locale = $description['locale']; |
| 69 |
$this->is_rtl = $description['rtl']; |
| 70 |
|
| 71 |
$this->description = &$this->locale; // backward compatibility with Polylang < 1.2 |
| 72 |
|
| 73 |
$this->mo_id = PLL_MO::get_id($this); |
| 74 |
$this->set_flag(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* sets flag_url and flag properties |
| 80 |
* |
| 81 |
* @since 1.2 |
| 82 |
*/ |
| 83 |
public function set_flag() { |
| 84 |
$flags['']['url'] = ''; |
| 85 |
|
| 86 |
// Polylang builtin flags |
| 87 |
if (file_exists(POLYLANG_DIR.($file = '/flags/'.$this->locale.'.png'))) { |
| 88 |
$flags['']['url'] = $flags['']['src'] = POLYLANG_URL.$file; |
| 89 |
|
| 90 |
// if base64 encoded flags are preferred |
| 91 |
if (!defined('PLL_ENCODED_FLAGS') || PLL_ENCODED_FLAGS) |
| 92 |
$flags['']['src'] = 'data:image/png;base64,' . base64_encode(file_get_contents(POLYLANG_DIR.$file)); |
| 93 |
} |
| 94 |
|
| 95 |
// custom flags ? |
| 96 |
if (file_exists(PLL_LOCAL_DIR.($file = '/'.$this->locale.'.png')) || file_exists(PLL_LOCAL_DIR.($file = '/'.$this->locale.'.jpg')) ) { |
| 97 |
$flags['custom_']['url'] = $flags['custom_']['src'] = PLL_LOCAL_URL.$file; |
| 98 |
} |
| 99 |
|
| 100 |
foreach($flags as $key => $flag) { |
| 101 |
$this->{$key . 'flag_url'} = empty($flag['url']) ? '' : esc_url($flag['url']); |
| 102 |
|
| 103 |
$this->{$key . 'flag'} = apply_filters('pll_get_flag', empty($flag['src']) ? '' : |
| 104 |
sprintf( |
| 105 |
'<img src="%s" title="%s" alt="%s" />', |
| 106 |
$flag['src'], |
| 107 |
esc_attr(apply_filters('pll_flag_title', $this->name, $this->slug, $this->locale)), |
| 108 |
esc_attr($this->name) |
| 109 |
), |
| 110 |
$this->slug |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* replace flag by custom flag |
| 117 |
* |
| 118 |
* @since 1.7 |
| 119 |
*/ |
| 120 |
public function set_custom_flag() { |
| 121 |
// overwrite with custom flags on frontend only |
| 122 |
if (!empty($this->custom_flag)) { |
| 123 |
$this->flag = $this->custom_flag; |
| 124 |
$this->flag_url = $this->custom_flag_url; |
| 125 |
unset($this->custom_flag, $this->custom_flag_url); // hide this |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* updates post and term count |
| 131 |
* |
| 132 |
* @since 1.2 |
| 133 |
*/ |
| 134 |
public function update_count() { |
| 135 |
wp_update_term_count($this->term_taxonomy_id, 'language'); // posts count |
| 136 |
wp_update_term_count($this->tl_term_taxonomy_id, 'term_language'); // terms count |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* set home_url and search_url properties |
| 141 |
* |
| 142 |
* @since 1.3 |
| 143 |
*/ |
| 144 |
public function set_home_url() { |
| 145 |
global $polylang; |
| 146 |
|
| 147 |
// home url for search form (can't use the page url if a static page is used as front page) |
| 148 |
$this->search_url = $polylang->links_model->home_url($this); |
| 149 |
|
| 150 |
// add a trailing slash as done by WP on homepage (otherwise could break the search form when the permalink structure does not include one) |
| 151 |
// only for pretty permalinks |
| 152 |
if (get_option('permalink_structure')) |
| 153 |
$this->search_url = trailingslashit($this->search_url); |
| 154 |
|
| 155 |
$options = get_option('polylang'); |
| 156 |
|
| 157 |
// a static page is used as front page |
| 158 |
if (!($options['hide_default'] && $this->slug == $options['default_lang']) && !$options['redirect_lang'] && 'page' == get_option('show_on_front') && ($page_on_front = get_option('page_on_front')) && $id = pll_get_post($page_on_front, $this)) |
| 159 |
$this->home_url = _get_page_link($id); // /!\ don't use get_page_link to avoid infinite loop |
| 160 |
|
| 161 |
else |
| 162 |
$this->home_url = $this->search_url; |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
* set home_url scheme |
| 167 |
* this can't be cached accross pages |
| 168 |
* |
| 169 |
* @since 1.6.4 |
| 170 |
*/ |
| 171 |
public function set_home_url_scheme() { |
| 172 |
if (is_ssl()) { |
| 173 |
$this->home_url = str_replace('http://', 'https://', $this->home_url); |
| 174 |
$this->search_url = str_replace('http://', 'https://', $this->search_url); |
| 175 |
} |
| 176 |
|
| 177 |
else { |
| 178 |
$this->home_url = str_replace('https://', 'http://', $this->home_url); |
| 179 |
$this->search_url = str_replace('https://', 'http://', $this->search_url); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|