| @@ -3,23 +3,27 @@ | ||
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * Links model for use when the language code is added in url as a directory | |
| 8 | - * for example mysite.com/en/something | |
| 9 | - * implements the "links_model interface" | |
| 7 | + * Links model for use when the language code is added in the url as a directory | |
| 8 | + * for example mysite.com/en/something. | |
| 10 | 9 | * |
| 11 | 10 | * @since 1.2 |
| 12 | 11 | */ |
| 13 | 12 | class PLL_Links_Directory extends PLL_Links_Permalinks { |
| 13 | + /** | |
| 14 | + * Relative path to the home url. | |
| 15 | + * | |
| 16 | + * @var string | |
| 17 | + */ | |
| 14 | 18 | protected $home_relative; |
| 15 | 19 | |
| 16 | 20 | /** |
| 17 | - * Constructor | |
| 21 | + * Constructor. | |
| 18 | 22 | * |
| 19 | 23 | * @since 1.2 |
| 20 | 24 | * |
| 21 | - * @param object $model PLL_Model instance | |
| 25 | + * @param PLL_Model $model PLL_Model instance. | |
| 22 | 26 | */ |
| 23 | 27 | public function __construct( &$model ) { |
| 24 | 28 | parent::__construct( $model ); |
| 25 | 29 | |
| @@ -32,11 +36,13 @@ | ||
| 32 | 36 | } |
| 33 | 37 | } |
| 34 | 38 | |
| 35 | 39 | /** |
| 36 | - * Called only at first object creation to avoid duplicating filters when switching blog | |
| 40 | + * Called only at first object creation to avoid duplicating filters when switching blog. | |
| 37 | 41 | * |
| 38 | 42 | * @since 1.6 |
| 43 | + * | |
| 44 | + * @return void | |
| 39 | 45 | */ |
| 40 | 46 | public function init() { |
| 41 | 47 | if ( did_action( 'setup_theme' ) ) { |
| 42 | 48 | $this->add_permastruct(); |
| @@ -43,21 +49,20 @@ | ||
| 43 | 49 | } else { |
| 44 | 50 | add_action( 'setup_theme', array( $this, 'add_permastruct' ), 2 ); |
| 45 | 51 | } |
| 46 | 52 | |
| 47 | - // Make sure to prepare rewrite rules when flushing | |
| 53 | + // Make sure to prepare rewrite rules when flushing. | |
| 48 | 54 | add_action( 'pre_option_rewrite_rules', array( $this, 'prepare_rewrite_rules' ) ); |
| 49 | 55 | } |
| 50 | 56 | |
| 51 | 57 | /** |
| 52 | - * Adds the language code in url | |
| 53 | - * links_model interface | |
| 58 | + * Adds the language code in a url. | |
| 54 | 59 | * |
| 55 | 60 | * @since 1.2 |
| 56 | 61 | * |
| 57 | - * @param string $url url to modify | |
| 58 | - * @param object $lang language | |
| 59 | - * @return string modified url | |
| 62 | + * @param string $url The url to modify. | |
| 63 | + * @param PLL_Language|false $lang The language object. | |
| 64 | + * @return string The modified url. | |
| 60 | 65 | */ |
| 61 | 66 | public function add_language_to_link( $url, $lang ) { |
| 62 | 67 | if ( ! empty( $lang ) ) { |
| 63 | 68 | $base = $this->options['rewrite'] ? '' : 'language/'; |
| @@ -66,9 +71,9 @@ | ||
| 66 | 71 | |
| 67 | 72 | if ( false === strpos( $url, $new = $root . $slug ) ) { |
| 68 | 73 | $pattern = preg_quote( $root, '#' ); |
| 69 | 74 | $pattern = '#' . $pattern . '#'; |
| 70 | - return preg_replace( $pattern, $new, $url, 1 ); // Only once | |
| 75 | + return preg_replace( $pattern, $new, $url, 1 ); // Only once. | |
| 71 | 76 | } |
| 72 | 77 | } |
| 73 | 78 | return $url; |
| 74 | 79 | } |
| @@ -73,15 +78,14 @@ | ||
| 73 | 78 | return $url; |
| 74 | 79 | } |
| 75 | 80 | |
| 76 | 81 | /** |
| 77 | - * Returns the url without language code | |
| 78 | - * links_model interface | |
| 82 | + * Returns the url without the language code. | |
| 79 | 83 | * |
| 80 | 84 | * @since 1.2 |
| 81 | 85 | * |
| 82 | - * @param string $url url to modify | |
| 83 | - * @return string modified url | |
| 86 | + * @param string $url The url to modify. | |
| 87 | + * @return string The modified url. | |
| 84 | 88 | */ |
| 85 | 89 | public function remove_language_from_link( $url ) { |
| 86 | 90 | $languages = array(); |
| 87 | 91 | |
| @@ -93,24 +97,23 @@ | ||
| 93 | 97 | |
| 94 | 98 | if ( ! empty( $languages ) ) { |
| 95 | 99 | $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root ); |
| 96 | 100 | |
| 97 | - $pattern = preg_quote( $root, '#' ); | |
| 98 | - $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(/|$)#'; | |
| 99 | - $url = preg_replace( $pattern, $root, $url ); | |
| 101 | + $pattern = preg_quote( $root, '@' ); | |
| 102 | + $pattern = '@' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(([?#])|(/|$))@'; | |
| 103 | + $url = preg_replace( $pattern, $root . '$3', $url ); | |
| 100 | 104 | } |
| 101 | 105 | return $url; |
| 102 | 106 | } |
| 103 | 107 | |
| 104 | 108 | /** |
| 105 | - * Returns the language based on language code in url | |
| 106 | - * links_model interface | |
| 109 | + * Returns the language based on the language code in the url. | |
| 107 | 110 | * |
| 108 | 111 | * @since 1.2 |
| 109 | - * @since 2.0 add $url argument | |
| 112 | + * @since 2.0 Add the $url argument. | |
| 110 | 113 | * |
| 111 | - * @param string $url optional, defaults to current url | |
| 112 | - * @return string language slug | |
| 114 | + * @param string $url Optional, defaults to the current url. | |
| 115 | + * @return string The language slug. | |
| 113 | 116 | */ |
| 114 | 117 | public function get_language_from_url( $url = '' ) { |
| 115 | 118 | if ( empty( $url ) ) { |
| 116 | 119 | $url = pll_get_requested_url(); |
| @@ -115,24 +118,23 @@ | ||
| 115 | 118 | if ( empty( $url ) ) { |
| 116 | 119 | $url = pll_get_requested_url(); |
| 117 | 120 | } |
| 118 | 121 | |
| 119 | - $path = wp_parse_url( $url, PHP_URL_PATH ); | |
| 122 | + $path = (string) wp_parse_url( $url, PHP_URL_PATH ); | |
| 120 | 123 | $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root; |
| 121 | 124 | |
| 122 | - $pattern = wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH ); | |
| 125 | + $pattern = (string) wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH ); | |
| 123 | 126 | $pattern = preg_quote( $pattern, '#' ); |
| 124 | 127 | $pattern = '#^' . $pattern . '(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(/|$)#'; |
| 125 | - return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language | |
| 128 | + return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language. | |
| 126 | 129 | } |
| 127 | 130 | |
| 128 | 131 | /** |
| 129 | - * Returns the home url | |
| 130 | - * links_model interface | |
| 132 | + * Returns the home url in a given language. | |
| 131 | 133 | * |
| 132 | 134 | * @since 1.3.1 |
| 133 | 135 | * |
| 134 | - * @param object $lang PLL_Language object | |
| 136 | + * @param PLL_Language $lang The language object. | |
| 135 | 137 | * @return string |
| 136 | 138 | */ |
| 137 | 139 | public function home_url( $lang ) { |
| 138 | 140 | $base = $this->options['rewrite'] ? '' : 'language/'; |
| @@ -140,16 +142,16 @@ | ||
| 140 | 142 | return trailingslashit( $this->home . $slug ); |
| 141 | 143 | } |
| 142 | 144 | |
| 143 | 145 | /** |
| 144 | - * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/ | |
| 146 | + * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/. | |
| 145 | 147 | * |
| 146 | 148 | * @since 1.2 |
| 149 | + * | |
| 150 | + * @return void | |
| 147 | 151 | */ |
| 148 | 152 | public function add_permastruct() { |
| 149 | - // Language information always in front of the uri ( 'with_front' => false ) | |
| 150 | - // The 3rd parameter structure has been modified in WP 3.4 | |
| 151 | - // Leads to error 404 for pages when there is no language created yet | |
| 153 | + // Language information always in front of the uri ( 'with_front' => false ). | |
| 152 | 154 | if ( $this->model->get_languages_list() ) { |
| 153 | 155 | add_permastruct( 'language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array( 'with_front' => false ) ); |
| 154 | 156 | } |
| 155 | 157 | } |
| @@ -154,27 +156,29 @@ | ||
| 154 | 156 | } |
| 155 | 157 | } |
| 156 | 158 | |
| 157 | 159 | /** |
| 158 | - * Prepares rewrite rules filters | |
| 160 | + * Prepares the rewrite rules filters. | |
| 159 | 161 | * |
| 160 | 162 | * @since 0.8.1 |
| 161 | 163 | * |
| 162 | - * @param array $pre not used | |
| 163 | - * @return unmodified $pre | |
| 164 | + * @param mixed $pre Not used as the filter is used as an action. | |
| 165 | + * @return mixed | |
| 164 | 166 | */ |
| 165 | 167 | public function prepare_rewrite_rules( $pre ) { |
| 166 | - // Don't modify the rules if there is no languages created yet | |
| 167 | - // Make sure to add filter only once and if all custom post types and taxonomies have been registered | |
| 168 | + /* | |
| 169 | + * Don't modify the rules if there is no languages created yet and make sure | |
| 170 | + * to add the filters only once and if all custom post types and taxonomies | |
| 171 | + * have been registered. | |
| 172 | + */ | |
| 168 | 173 | if ( $this->model->get_languages_list() && did_action( 'wp_loaded' ) && ! has_filter( 'language_rewrite_rules', '__return_empty_array' ) ) { |
| 169 | - // Suppress the rules created by WordPress for our taxonomy | |
| 170 | - add_filter( 'language_rewrite_rules', '__return_empty_array' ); | |
| 174 | + add_filter( 'language_rewrite_rules', '__return_empty_array' ); // Suppress the rules created by WordPress for our taxonomy. | |
| 171 | 175 | |
| 172 | 176 | foreach ( $this->get_rewrite_rules_filters() as $type ) { |
| 173 | 177 | add_filter( $type . '_rewrite_rules', array( $this, 'rewrite_rules' ) ); |
| 174 | 178 | } |
| 175 | 179 | |
| 176 | - add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules' ) ); // needed for post type archives | |
| 180 | + add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules' ) ); // Needed for post type archives. | |
| 177 | 181 | } |
| 178 | 182 | return $pre; |
| 179 | 183 | } |
| 180 | 184 | |
| @@ -179,15 +183,16 @@ | ||
| 179 | 183 | } |
| 180 | 184 | |
| 181 | 185 | /** |
| 182 | 186 | * The rewrite rules ! |
| 183 | - * always make sure the default language is at the end in case the language information is hidden for default language | |
| 184 | - * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct | |
| 185 | 187 | * |
| 188 | + * Always make sure that the default language is at the end in case the language information is hidden for default language. | |
| 189 | + * Thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct. | |
| 190 | + * | |
| 186 | 191 | * @since 0.8.1 |
| 187 | 192 | * |
| 188 | - * @param array $rules rewrite rules | |
| 189 | - * @return array modified rewrite rules | |
| 193 | + * @param string[] $rules Rewrite rules. | |
| 194 | + * @return string[] Modified rewrite rules. | |
| 190 | 195 | */ |
| 191 | 196 | public function rewrite_rules( $rules ) { |
| 192 | 197 | $filter = str_replace( '_rewrite_rules', '', current_filter() ); |
| 193 | 198 | |
| @@ -202,25 +207,25 @@ | ||
| 202 | 207 | if ( ! empty( $languages ) ) { |
| 203 | 208 | $slug = $wp_rewrite->root . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')/'; |
| 204 | 209 | } |
| 205 | 210 | |
| 206 | - // For custom post type archives | |
| 211 | + // For custom post type archives. | |
| 207 | 212 | $cpts = array_intersect( $this->model->get_translated_post_types(), get_post_types( array( '_builtin' => false ) ) ); |
| 208 | 213 | $cpts = $cpts ? '#post_type=(' . implode( '|', $cpts ) . ')#' : ''; |
| 209 | 214 | |
| 210 | 215 | foreach ( $rules as $key => $rule ) { |
| 211 | - // Special case for translated post types and taxonomies to allow canonical redirection | |
| 216 | + // Special case for translated post types and taxonomies to allow canonical redirection. | |
| 212 | 217 | if ( $this->options['force_lang'] && in_array( $filter, array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies() ) ) ) { |
| 213 | 218 | |
| 214 | 219 | /** |
| 215 | - * Filters the rewrite rules to modify | |
| 220 | + * Filters the rewrite rules to modify. | |
| 216 | 221 | * |
| 217 | 222 | * @since 1.9.1 |
| 218 | 223 | * |
| 219 | - * @param bool $modify whether to modify or not the rule, defaults to true | |
| 220 | - * @param array $rule original rewrite rule | |
| 221 | - * @param string $filter current set of rules being modified | |
| 222 | - * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive | |
| 224 | + * @param bool $modify Whether to modify or not the rule, defaults to true. | |
| 225 | + * @param array $rule Original rewrite rule. | |
| 226 | + * @param string $filter Current set of rules being modified. | |
| 227 | + * @param string|bool $archive Custom post post type archive name or false if it is not a cpt archive. | |
| 223 | 228 | */ |
| 224 | 229 | if ( isset( $slug ) && apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) { |
| 225 | 230 | $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace( |
| 226 | 231 | array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), |
| @@ -231,9 +236,9 @@ | ||
| 231 | 236 | |
| 232 | 237 | $newrules[ $key ] = $rule; |
| 233 | 238 | } |
| 234 | 239 | |
| 235 | - // Rewrite rules filtered by language | |
| 240 | + // Rewrite rules filtered by language. | |
| 236 | 241 | elseif ( in_array( $filter, $this->always_rewrite ) || in_array( $filter, $this->model->get_filtered_taxonomies() ) || ( $cpts && preg_match( $cpts, $rule, $matches ) && ! strpos( $rule, 'name=' ) ) || ( 'rewrite_rules_array' != $filter && $this->options['force_lang'] ) ) { |
| 237 | 242 | |
| 238 | 243 | /** This filter is documented in include/links-directory.php */ |
| 239 | 244 | if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) { |
| @@ -252,15 +257,15 @@ | ||
| 252 | 257 | $newrules[ $key ] = $rule; |
| 253 | 258 | } |
| 254 | 259 | } |
| 255 | 260 | |
| 256 | - // Unmodified rules | |
| 261 | + // Unmodified rules. | |
| 257 | 262 | else { |
| 258 | 263 | $newrules[ $key ] = $rule; |
| 259 | 264 | } |
| 260 | 265 | } |
| 261 | 266 | |
| 262 | - // The home rewrite rule | |
| 267 | + // The home rewrite rule. | |
| 263 | 268 | if ( 'root' == $filter && isset( $slug ) ) { |
| 264 | 269 | $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]'; |
| 265 | 270 | } |
| 266 | 271 | |