| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * Links model base class when using pretty permalinks | |
| 7 | + * Links model base class when using pretty permalinks. | |
| 8 | 8 | * |
| 9 | 9 | * @since 1.6 |
| 10 | 10 | */ |
| 11 | 11 | abstract class PLL_Links_Permalinks extends PLL_Links_Model { |
| @@ -55,9 +55,9 @@ | ||
| 55 | 55 | */ |
| 56 | 56 | public function __construct( &$model ) { |
| 57 | 57 | parent::__construct( $model ); |
| 58 | 58 | |
| 59 | - // Inspired by wp-includes/rewrite.php | |
| 59 | + // Inspired by WP_Rewrite. | |
| 60 | 60 | $permalink_structure = get_option( 'permalink_structure' ); |
| 61 | 61 | $this->root = preg_match( '#^/*' . $this->index . '#', $permalink_structure ) ? $this->index . '/' : ''; |
| 62 | 62 | $this->use_trailing_slashes = ( '/' == substr( $permalink_structure, -1, 1 ) ); |
| 63 | 63 | } |
| @@ -62,59 +62,103 @@ | ||
| 62 | 62 | $this->use_trailing_slashes = ( '/' == substr( $permalink_structure, -1, 1 ) ); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | - * Returns the link to the first page when using pretty permalinks | |
| 66 | + * Initializes permalinks. | |
| 67 | 67 | * |
| 68 | + * @since 3.5 | |
| 69 | + * | |
| 70 | + * @return void | |
| 71 | + */ | |
| 72 | + public function init() { | |
| 73 | + parent::init(); | |
| 74 | + | |
| 75 | + if ( did_action( 'wp_loaded' ) ) { | |
| 76 | + $this->do_prepare_rewrite_rules(); | |
| 77 | + } else { | |
| 78 | + add_action( 'wp_loaded', array( $this, 'do_prepare_rewrite_rules' ), 9 ); // Just before WordPress callback `WP_Rewrite::flush_rules()`. | |
| 79 | + } | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Fires our own action telling Polylang plugins | |
| 84 | + * and third parties are able to prepare rewrite rules. | |
| 85 | + * | |
| 86 | + * @since 3.5 | |
| 87 | + * | |
| 88 | + * @return void | |
| 89 | + */ | |
| 90 | + public function do_prepare_rewrite_rules() { | |
| 91 | + self::$can_filter_rewrite_rules = true; | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Tells when Polylang is able to prepare rewrite rules filters. | |
| 95 | + * Action fired right after `wp_loaded` and just before WordPress `WP_Rewrite::flush_rules()` callback. | |
| 96 | + * | |
| 97 | + * @since 3.5 | |
| 98 | + * | |
| 99 | + * @param PLL_Links_Permalinks $links Current links object. | |
| 100 | + */ | |
| 101 | + do_action( 'pll_prepare_rewrite_rules', $this ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Returns the link to the first page when using pretty permalinks. | |
| 106 | + * | |
| 68 | 107 | * @since 1.2 |
| 69 | 108 | * |
| 70 | - * @param string $url url to modify | |
| 71 | - * @return string modified url | |
| 109 | + * @param string $url The url to modify. | |
| 110 | + * @return string The modified url. | |
| 72 | 111 | */ |
| 73 | 112 | public function remove_paged_from_link( $url ) { |
| 74 | 113 | /** |
| 75 | - * Filter an url after the paged part has been removed | |
| 114 | + * Filters an url after the paged part has been removed. | |
| 76 | 115 | * |
| 77 | 116 | * @since 2.0.6 |
| 78 | 117 | * |
| 79 | - * @param string $modified_url The link to the first page | |
| 80 | - * @param string $original_url The link to the original paged page | |
| 118 | + * @param string $modified_url The link to the first page. | |
| 119 | + * @param string $original_url The link to the original paged page. | |
| 81 | 120 | */ |
| 82 | 121 | return apply_filters( 'pll_remove_paged_from_link', preg_replace( '#/page/[0-9]+/?#', $this->use_trailing_slashes ? '/' : '', $url ), $url ); |
| 83 | 122 | } |
| 84 | 123 | |
| 85 | 124 | /** |
| 86 | - * Returns the link to the paged page when using pretty permalinks | |
| 125 | + * Returns the link to the paged page when using pretty permalinks. | |
| 87 | 126 | * |
| 88 | 127 | * @since 1.5 |
| 89 | 128 | * |
| 90 | - * @param string $url url to modify | |
| 91 | - * @param int $page | |
| 92 | - * @return string modified url | |
| 129 | + * @param string $url The url to modify. | |
| 130 | + * @param int $page The page number. | |
| 131 | + * @return string The modified url. | |
| 93 | 132 | */ |
| 94 | 133 | public function add_paged_to_link( $url, $page ) { |
| 95 | 134 | /** |
| 96 | - * Filter an url after the paged part has been added | |
| 135 | + * Filters an url after the paged part has been added. | |
| 97 | 136 | * |
| 98 | 137 | * @since 2.0.6 |
| 99 | 138 | * |
| 100 | - * @param string $modified_url The link to the paged page | |
| 101 | - * @param string $original_url The link to the original first page | |
| 102 | - * @param int $page The page number | |
| 139 | + * @param string $modified_url The link to the paged page. | |
| 140 | + * @param string $original_url The link to the original first page. | |
| 141 | + * @param int $page The page number. | |
| 103 | 142 | */ |
| 104 | 143 | return apply_filters( 'pll_add_paged_to_link', user_trailingslashit( trailingslashit( $url ) . 'page/' . $page, 'paged' ), $url, $page ); |
| 105 | 144 | } |
| 106 | 145 | |
| 107 | 146 | /** |
| 108 | - * Returns the home url. | |
| 147 | + * Returns the home url in a given language. | |
| 109 | 148 | * |
| 110 | 149 | * @since 1.3.1 |
| 150 | + * @since 3.4 Accepts now a language slug. | |
| 111 | 151 | * |
| 112 | - * @param PLL_Language $lang PLL_Language object. | |
| 152 | + * @param PLL_Language|string $language Language object or slug. | |
| 113 | 153 | * @return string |
| 114 | 154 | */ |
| 115 | - public function home_url( $lang ) { | |
| 116 | - return trailingslashit( parent::home_url( $lang ) ); | |
| 155 | + public function home_url( $language ) { | |
| 156 | + if ( $language instanceof PLL_Language ) { | |
| 157 | + $language = $language->slug; | |
| 158 | + } | |
| 159 | + | |
| 160 | + return trailingslashit( parent::home_url( $language ) ); | |
| 117 | 161 | } |
| 118 | 162 | |
| 119 | 163 | /** |
| 120 | 164 | * Returns the static front page url. |
| @@ -119,23 +163,28 @@ | ||
| 119 | 163 | /** |
| 120 | 164 | * Returns the static front page url. |
| 121 | 165 | * |
| 122 | 166 | * @since 1.8 |
| 167 | + * @since 3.4 Accepts now an array of language properties. | |
| 123 | 168 | * |
| 124 | - * @param PLL_Language $lang The language object. | |
| 169 | + * @param PLL_Language|array $language Language object or array of language properties. | |
| 125 | 170 | * @return string The static front page url. |
| 126 | 171 | */ |
| 127 | - public function front_page_url( $lang ) { | |
| 128 | - if ( $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ) { | |
| 172 | + public function front_page_url( $language ) { | |
| 173 | + if ( $language instanceof PLL_Language ) { | |
| 174 | + $language = $language->to_array(); | |
| 175 | + } | |
| 176 | + | |
| 177 | + if ( $this->options['hide_default'] && $language['is_default'] ) { | |
| 129 | 178 | return trailingslashit( $this->home ); |
| 130 | 179 | } |
| 131 | - $url = home_url( $this->root . get_page_uri( $lang->page_on_front ) ); | |
| 180 | + $url = home_url( $this->root . get_page_uri( $language['page_on_front'] ) ); | |
| 132 | 181 | $url = $this->use_trailing_slashes ? trailingslashit( $url ) : untrailingslashit( $url ); |
| 133 | - return $this->options['force_lang'] ? $this->add_language_to_link( $url, $lang ) : $url; | |
| 182 | + return $this->options['force_lang'] ? $this->add_language_to_link( $url, $language['slug'] ) : $url; | |
| 134 | 183 | } |
| 135 | 184 | |
| 136 | 185 | /** |
| 137 | - * Prepares rewrite rules filters | |
| 186 | + * Prepares rewrite rules filters. | |
| 138 | 187 | * |
| 139 | 188 | * @since 1.6 |
| 140 | 189 | * |
| 141 | 190 | * @return string[] |
| @@ -140,18 +189,31 @@ | ||
| 140 | 189 | * |
| 141 | 190 | * @return string[] |
| 142 | 191 | */ |
| 143 | 192 | public function get_rewrite_rules_filters() { |
| 144 | - // Make sure we have the right post types and taxonomies | |
| 193 | + // Make sure that we have the right post types and taxonomies. | |
| 145 | 194 | $types = array_values( array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies(), $this->model->get_filtered_taxonomies() ) ); |
| 146 | 195 | $types = array_merge( $this->always_rewrite, $types ); |
| 147 | 196 | |
| 148 | 197 | /** |
| 149 | - * Filter the list of rewrite rules filters to be used by Polylang | |
| 198 | + * Filters the list of rewrite rules filters to be used by Polylang. | |
| 150 | 199 | * |
| 151 | 200 | * @since 0.8.1 |
| 152 | 201 | * |
| 153 | - * @param array $types the list of filters (without '_rewrite_rules' at the end) | |
| 202 | + * @param array $types The list of filters (without '_rewrite_rules' at the end). | |
| 154 | 203 | */ |
| 155 | 204 | return apply_filters( 'pll_rewrite_rules', $types ); |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * Removes hooks to filter rewrite rules, called when switching blog @see {PLL_Base::switch_blog()}. | |
| 209 | + * | |
| 210 | + * @since 3.5 | |
| 211 | + * | |
| 212 | + * @return void | |
| 213 | + */ | |
| 214 | + public function remove_filters() { | |
| 215 | + parent::remove_filters(); | |
| 216 | + | |
| 217 | + remove_all_actions( 'pll_prepare_rewrite_rules' ); | |
| 156 | 218 | } |
| 157 | 219 | } |