| 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 |
* home_url => home url in this language |
| 25 |
* search_url => home url to use in search forms |
| 26 |
* |
| 27 |
* @since 1.2 |
| 28 |
*/ |
| 29 |
class PLL_Language { |
| 30 |
|
| 31 |
/* |
| 32 |
* constructor: builds a language object given its two corresponding terms in language and term_language taxonomies |
| 33 |
* |
| 34 |
* @since 1.2 |
| 35 |
* |
| 36 |
* @param object $language 'language' term |
| 37 |
* @param object $term_language corresponding 'term_language' term |
| 38 |
*/ |
| 39 |
public function __construct($language, $term_language) { |
| 40 |
foreach ($language as $prop => $value) |
| 41 |
$this->$prop = in_array($prop, array('term_id', 'term_taxonomy_id', 'count')) ? (int) $language->$prop : $language->$prop; |
| 42 |
|
| 43 |
// although it would be convenient here, don't assume the term is shared between taxonomies as it may not be the case in future |
| 44 |
// http://make.wordpress.org/core/2013/07/28/potential-roadmap-for-taxonomy-meta-and-post-relationships/ |
| 45 |
$this->tl_term_id = (int) $term_language->term_id; |
| 46 |
$this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id; |
| 47 |
$this->tl_count = (int) $term_language->count; |
| 48 |
|
| 49 |
$description = maybe_unserialize($language->description); |
| 50 |
$this->locale = $description['locale']; |
| 51 |
$this->is_rtl = $description['rtl']; |
| 52 |
|
| 53 |
$this->description = &$this->locale; // backward compatibility with Polylang < 1.2 |
| 54 |
} |
| 55 |
|
| 56 |
/* |
| 57 |
* sets flag_url and flag properties |
| 58 |
* |
| 59 |
* @since 1.2 |
| 60 |
*/ |
| 61 |
public function set_flag() { |
| 62 |
if (file_exists(POLYLANG_DIR.($file = '/flags/'.$this->locale.'.png'))) |
| 63 |
$url = POLYLANG_URL.$file; |
| 64 |
|
| 65 |
// overwrite with custom flags |
| 66 |
// never use custom flags on admin side |
| 67 |
if (!PLL_ADMIN && ( file_exists(PLL_LOCAL_DIR.($file = '/'.$this->locale.'.png')) || file_exists(PLL_LOCAL_DIR.($file = '/'.$this->locale.'.jpg')) )) |
| 68 |
$url = PLL_LOCAL_URL.$file; |
| 69 |
|
| 70 |
$this->flag_url = empty($url) ? '' : esc_url($url); |
| 71 |
|
| 72 |
$this->flag = apply_filters('pll_get_flag', empty($this->flag_url) ? '' : |
| 73 |
sprintf( |
| 74 |
'<img src="%s" title="%s" alt="%s" />', |
| 75 |
esc_url($this->flag_url), |
| 76 |
esc_attr(apply_filters('pll_flag_title', $this->name, $this->slug, $this->locale)), |
| 77 |
esc_attr($this->name) |
| 78 |
)); |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* updates post and term count |
| 83 |
* |
| 84 |
* @since 1.2 |
| 85 |
*/ |
| 86 |
public function update_count() { |
| 87 |
wp_update_term_count($this->term_taxonomy_id, 'language'); // posts count |
| 88 |
wp_update_term_count($this->tl_term_taxonomy_id, 'term_language'); // terms count |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* set home_url and search_url properties |
| 93 |
* |
| 94 |
* @since 1.3 |
| 95 |
* |
| 96 |
*/ |
| 97 |
public function set_home_url() { |
| 98 |
$options = get_option('polylang'); |
| 99 |
|
| 100 |
if ($options['default_lang'] == $this->slug && $options['hide_default']) |
| 101 |
return $this->home_url = $this->search_url = trailingslashit(get_option('home')); |
| 102 |
|
| 103 |
// a static page is used as front page : /!\ don't use get_page_link to avoid infinite loop |
| 104 |
// don't use this for search form |
| 105 |
if (!$options['redirect_lang'] && ($page_on_front = get_option('page_on_front')) && $id = pll_get_post($page_on_front, $this)) |
| 106 |
$this->home_url = _get_page_link($id); |
| 107 |
|
| 108 |
$link = get_term_link($this, 'language'); |
| 109 |
|
| 110 |
// add a trailing slash as done by WP on homepage (otherwise could break the search form when the permalink structure does not include one) |
| 111 |
// only for pretty permalinks |
| 112 |
$this->search_url = get_option('using_permalinks') ? trailingslashit($link) : $link; |
| 113 |
|
| 114 |
$this->home_url = empty($this->home_url) ? $this->search_url : $this->home_url; |
| 115 |
} |
| 116 |
} |
| 117 |
|