| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* links model for use when the language code is added in url as a directory |
| 5 |
* for example mysite.com/en/something |
| 6 |
* implements the "links_model interface" |
| 7 |
* |
| 8 |
* @since 1.2 |
| 9 |
*/ |
| 10 |
class PLL_Links_Directory extends PLL_Links_Permalinks { |
| 11 |
protected $index = 'index.php'; // need this before $wp_rewrite is created, also harcoded in wp-includes/rewrite.php |
| 12 |
protected $root; |
| 13 |
|
| 14 |
/* |
| 15 |
* constructor |
| 16 |
* |
| 17 |
* @since 1.2 |
| 18 |
* |
| 19 |
* @param object $model PLL_Model instance |
| 20 |
*/ |
| 21 |
public function __construct(&$model) { |
| 22 |
parent::__construct($model); |
| 23 |
|
| 24 |
// inspired by wp-includes/rewrite.php |
| 25 |
$this->root = preg_match('#^/*' . $this->index . '#', get_option('permalink_structure')) ? $this->index . '/' : ''; |
| 26 |
|
| 27 |
add_action('pll_init', array(&$this, 'init')); |
| 28 |
} |
| 29 |
|
| 30 |
/* |
| 31 |
* called only at first object creation to avoid duplicating filters when switching blog |
| 32 |
* |
| 33 |
* @since 1.6 |
| 34 |
*/ |
| 35 |
public function init() { |
| 36 |
add_action('setup_theme', array(&$this, 'add_permastruct'), 2); |
| 37 |
|
| 38 |
// make sure to prepare rewrite rules when flushing |
| 39 |
add_action('pre_option_rewrite_rules', array(&$this, 'prepare_rewrite_rules')); |
| 40 |
|
| 41 |
// refresh rewrite rules if the 'page_on_front' option is modified |
| 42 |
add_action('update_option_page_on_front', 'flush_rewrite_rules'); |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* adds the language code in url |
| 47 |
* links_model interface |
| 48 |
* |
| 49 |
* @since 1.2 |
| 50 |
* |
| 51 |
* @param string $url url to modify |
| 52 |
* @param object $lang language |
| 53 |
* @return string modified url |
| 54 |
*/ |
| 55 |
public function add_language_to_link($url, $lang) { |
| 56 |
if (!empty($lang)) { |
| 57 |
$base = $this->options['rewrite'] ? '' : 'language/'; |
| 58 |
$slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : $base . $lang->slug . '/'; |
| 59 |
return str_replace($this->home . '/' . $this->root, $this->home . '/' . $this->root . $slug, $url); |
| 60 |
} |
| 61 |
return $url; |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* returns the url without language code |
| 66 |
* links_model interface |
| 67 |
* |
| 68 |
* @since 1.2 |
| 69 |
* |
| 70 |
* @param string $url url to modify |
| 71 |
* @return string modified url |
| 72 |
*/ |
| 73 |
function remove_language_from_link($url) { |
| 74 |
foreach ($this->model->get_languages_list() as $language) |
| 75 |
if (!$this->options['hide_default'] || $this->options['default_lang'] != $language->slug) |
| 76 |
$languages[] = $language->slug; |
| 77 |
|
| 78 |
if (!empty($languages)) { |
| 79 |
$pattern = str_replace('/', '\/', $this->home . '/' . $this->root); |
| 80 |
$pattern = '#' . $pattern . ($this->options['rewrite'] ? '' : 'language\/') . '('.implode('|', $languages).')(\/|$)#'; |
| 81 |
$url = preg_replace($pattern, $this->home . '/' . $this->root, $url); |
| 82 |
} |
| 83 |
return $url; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* returns the language based on language code in url |
| 88 |
* links_model interface |
| 89 |
* |
| 90 |
* @since 1.2 |
| 91 |
* |
| 92 |
* @return string language slug |
| 93 |
*/ |
| 94 |
public function get_language_from_url() { |
| 95 |
$requested_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 96 |
$pattern = str_replace('/', '\/', $this->home . '/' . $this->root . ($this->options['rewrite'] ? '' : 'language/')); |
| 97 |
$pattern = '#' . $pattern . '('. implode('|', $this->model->get_languages_list(array('fields' => 'slug'))) . ')(\/|$)#'; |
| 98 |
return preg_match($pattern, trailingslashit($requested_url), $matches) ? $matches[1] : ''; // $matches[1] is the slug of the requested language |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* returns the home url |
| 103 |
* links_model interface |
| 104 |
* |
| 105 |
* @since 1.3.1 |
| 106 |
* |
| 107 |
* @param object $lang PLL_Language object |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public function home_url($lang) { |
| 111 |
return $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ? $this->home : get_term_link($lang, 'language'); |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* optionaly removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/ |
| 116 |
* |
| 117 |
* @since 1.2 |
| 118 |
*/ |
| 119 |
function add_permastruct() { |
| 120 |
// language information always in front of the uri ('with_front' => false) |
| 121 |
// the 3rd parameter structure has been modified in WP 3.4 |
| 122 |
add_permastruct('language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array('with_front' => false)); |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
* prepares rewrite rules filters |
| 127 |
* |
| 128 |
* @since 0.8.1 |
| 129 |
* |
| 130 |
* @param array $pre not used |
| 131 |
* @return unmodified $pre |
| 132 |
*/ |
| 133 |
public function prepare_rewrite_rules($pre) { |
| 134 |
static $done = false; // make sure to add filters only one time |
| 135 |
|
| 136 |
// don't modify the rules if there is no languages created yet |
| 137 |
if ($this->model->get_languages_list() && !$done) { |
| 138 |
// suppress the rules created by WordPress for our taxonomy |
| 139 |
add_filter('language_rewrite_rules', create_function('$rules', 'return array();')); |
| 140 |
|
| 141 |
foreach ($this->get_rewrite_rules_filters() as $type) |
| 142 |
add_filter($type . '_rewrite_rules', array(&$this, 'rewrite_rules')); |
| 143 |
|
| 144 |
add_filter('rewrite_rules_array', array(&$this, 'rewrite_rules')); // needed for post type archives |
| 145 |
|
| 146 |
$done = true; |
| 147 |
} |
| 148 |
return $pre; |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* the rewrite rules ! |
| 153 |
* always make sure the default language is at the end in case the language information is hidden for default language |
| 154 |
* thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct |
| 155 |
* |
| 156 |
* @since 0.8.1 |
| 157 |
* |
| 158 |
* @param array $rules rewrite rules |
| 159 |
* @return array modified rewrite rules |
| 160 |
*/ |
| 161 |
public function rewrite_rules($rules) { |
| 162 |
$filter = str_replace('_rewrite_rules', '', current_filter()); |
| 163 |
|
| 164 |
global $wp_rewrite; |
| 165 |
$newrules = array(); |
| 166 |
|
| 167 |
$languages = $this->model->get_languages_list(array('fields' => 'slug')); |
| 168 |
if ($this->options['hide_default']) |
| 169 |
$languages = array_diff($languages, array($this->options['default_lang'])); |
| 170 |
|
| 171 |
if (!empty($languages)) |
| 172 |
$slug = $wp_rewrite->root . ($this->options['rewrite'] ? '' : 'language/') . '('.implode('|', $languages).')/'; |
| 173 |
|
| 174 |
// for custom post type archives |
| 175 |
$cpts = array_intersect($this->model->get_translated_post_types(), get_post_types(array('_builtin' => false))); |
| 176 |
$cpts = $cpts ? '#post_type=('.implode('|', $cpts).')#' : ''; |
| 177 |
|
| 178 |
foreach ($rules as $key => $rule) { |
| 179 |
// we don't need the lang parameter for post types and taxonomies |
| 180 |
// moreover adding it would create issues for pages and taxonomies |
| 181 |
if ($this->options['force_lang'] && in_array($filter, array_merge($this->model->get_translated_post_types(), $this->model->get_translated_taxonomies()))) { |
| 182 |
if (isset($slug)) |
| 183 |
$newrules[$slug.str_replace($wp_rewrite->root, '', $key)] = str_replace( |
| 184 |
array('[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]'), |
| 185 |
array('[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]'), |
| 186 |
$rule |
| 187 |
); // should be enough! |
| 188 |
|
| 189 |
if ($this->options['hide_default']) { |
| 190 |
$newrules[$key] = $rules[$key]; |
| 191 |
// unset only if we hide the code for the default language as check_language_code_in_url will do its job in other cases |
| 192 |
unset($rules[$key]); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// rewrite rules filtered by language |
| 197 |
elseif (in_array($filter, $this->always_rewrite) || in_array($filter, $this->model->get_filtered_taxonomies()) || ($cpts && preg_match($cpts, $rule) && !strpos($rule, 'name=')) || ($filter != 'rewrite_rules_array' && $this->options['force_lang'])) { |
| 198 |
if (isset($slug)) |
| 199 |
$newrules[$slug.str_replace($wp_rewrite->root, '', $key)] = str_replace( |
| 200 |
array('[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?'), |
| 201 |
array('[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&'), |
| 202 |
$rule |
| 203 |
); // should be enough! |
| 204 |
|
| 205 |
if ($this->options['hide_default']) |
| 206 |
$newrules[$key] = str_replace('?', '?lang='.$this->options['default_lang'].'&', $rule); |
| 207 |
|
| 208 |
unset($rules[$key]); // now useless |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// the home rewrite rule |
| 213 |
if ($filter == 'root' && isset($slug)) |
| 214 |
$newrules[$slug.'?$'] = $wp_rewrite->index.'?lang=$matches[1]'; |
| 215 |
|
| 216 |
return $newrules + $rules; |
| 217 |
} |
| 218 |
} |
| 219 |
|