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