| @@ -3,29 +3,61 @@ | ||
| 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 { |
| 12 | + /** | |
| 13 | + * Tells this child class of PLL_Links_Model is for pretty permalinks. | |
| 14 | + * | |
| 15 | + * @var bool | |
| 16 | + */ | |
| 12 | 17 | public $using_permalinks = true; |
| 13 | - protected $index = 'index.php'; // Need this before $wp_rewrite is created, also hardcoded in wp-includes/rewrite.php | |
| 14 | - protected $root, $use_trailing_slashes; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * The name of the index file which is the entry point to all requests. | |
| 21 | + * We need this before the global $wp_rewrite is created. | |
| 22 | + * Also hardcoded in WP_Rewrite. | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + protected $index = 'index.php'; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * The prefix for all permalink structures. | |
| 30 | + * | |
| 31 | + * @var string | |
| 32 | + */ | |
| 33 | + protected $root; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Whether to add trailing slashes. | |
| 37 | + * | |
| 38 | + * @var bool | |
| 39 | + */ | |
| 40 | + protected $use_trailing_slashes; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * The name of the rewrite rules to always modify. | |
| 44 | + * | |
| 45 | + * @var string[] | |
| 46 | + */ | |
| 15 | 47 | protected $always_rewrite = array( 'date', 'root', 'comments', 'search', 'author' ); |
| 16 | 48 | |
| 17 | 49 | /** |
| 18 | - * Constructor | |
| 50 | + * Constructor. | |
| 19 | 51 | * |
| 20 | 52 | * @since 1.8 |
| 21 | 53 | * |
| 22 | - * @param object $model PLL_Model instance | |
| 54 | + * @param PLL_Model $model PLL_Model instance. | |
| 23 | 55 | */ |
| 24 | 56 | public function __construct( &$model ) { |
| 25 | 57 | parent::__construct( $model ); |
| 26 | 58 | |
| 27 | - // Inspired by wp-includes/rewrite.php | |
| 59 | + // Inspired by WP_Rewrite. | |
| 28 | 60 | $permalink_structure = get_option( 'permalink_structure' ); |
| 29 | 61 | $this->root = preg_match( '#^/*' . $this->index . '#', $permalink_structure ) ? $this->index . '/' : ''; |
| 30 | 62 | $this->use_trailing_slashes = ( '/' == substr( $permalink_structure, -1, 1 ) ); |
| 31 | 63 | } |
| @@ -30,94 +62,158 @@ | ||
| 30 | 62 | $this->use_trailing_slashes = ( '/' == substr( $permalink_structure, -1, 1 ) ); |
| 31 | 63 | } |
| 32 | 64 | |
| 33 | 65 | /** |
| 34 | - * Returns the link to the first page when using pretty permalinks | |
| 66 | + * Initializes permalinks. | |
| 35 | 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 | + * | |
| 36 | 107 | * @since 1.2 |
| 37 | 108 | * |
| 38 | - * @param string $url url to modify | |
| 39 | - * @return string modified url | |
| 109 | + * @param string $url The url to modify. | |
| 110 | + * @return string The modified url. | |
| 40 | 111 | */ |
| 41 | 112 | public function remove_paged_from_link( $url ) { |
| 42 | 113 | /** |
| 43 | - * Filter an url after the paged part has been removed | |
| 114 | + * Filters an url after the paged part has been removed. | |
| 44 | 115 | * |
| 45 | 116 | * @since 2.0.6 |
| 46 | 117 | * |
| 47 | - * @param string $modified_url The link to the first page | |
| 48 | - * @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. | |
| 49 | 120 | */ |
| 50 | 121 | return apply_filters( 'pll_remove_paged_from_link', preg_replace( '#/page/[0-9]+/?#', $this->use_trailing_slashes ? '/' : '', $url ), $url ); |
| 51 | 122 | } |
| 52 | 123 | |
| 53 | 124 | /** |
| 54 | - * Returns the link to the paged page when using pretty permalinks | |
| 125 | + * Returns the link to the paged page when using pretty permalinks. | |
| 55 | 126 | * |
| 56 | 127 | * @since 1.5 |
| 57 | 128 | * |
| 58 | - * @param string $url url to modify | |
| 59 | - * @param int $page | |
| 60 | - * @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. | |
| 61 | 132 | */ |
| 62 | 133 | public function add_paged_to_link( $url, $page ) { |
| 63 | 134 | /** |
| 64 | - * Filter an url after the paged part has been added | |
| 135 | + * Filters an url after the paged part has been added. | |
| 65 | 136 | * |
| 66 | 137 | * @since 2.0.6 |
| 67 | 138 | * |
| 68 | - * @param string $modified_url The link to the paged page | |
| 69 | - * @param string $original_url The link to the original first page | |
| 70 | - * @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. | |
| 71 | 142 | */ |
| 72 | 143 | return apply_filters( 'pll_add_paged_to_link', user_trailingslashit( trailingslashit( $url ) . 'page/' . $page, 'paged' ), $url, $page ); |
| 73 | 144 | } |
| 74 | 145 | |
| 75 | 146 | /** |
| 76 | - * Returns the home url | |
| 147 | + * Returns the home url in a given language. | |
| 77 | 148 | * |
| 78 | 149 | * @since 1.3.1 |
| 150 | + * @since 3.4 Accepts now a language slug. | |
| 79 | 151 | * |
| 80 | - * @param object $lang PLL_Language object | |
| 152 | + * @param PLL_Language|string $language Language object or slug. | |
| 81 | 153 | * @return string |
| 82 | 154 | */ |
| 83 | - public function home_url( $lang ) { | |
| 84 | - 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 ) ); | |
| 85 | 161 | } |
| 86 | 162 | |
| 87 | 163 | /** |
| 88 | - * Returns the static front page url | |
| 164 | + * Returns the static front page url. | |
| 89 | 165 | * |
| 90 | 166 | * @since 1.8 |
| 167 | + * @since 3.4 Accepts now an array of language properties. | |
| 91 | 168 | * |
| 92 | - * @param object $lang | |
| 93 | - * @return string | |
| 169 | + * @param PLL_Language|array $language Language object or array of language properties. | |
| 170 | + * @return string The static front page url. | |
| 94 | 171 | */ |
| 95 | - public function front_page_url( $lang ) { | |
| 96 | - 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'] ) { | |
| 97 | 178 | return trailingslashit( $this->home ); |
| 98 | 179 | } |
| 99 | - $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'] ) ); | |
| 100 | 181 | $url = $this->use_trailing_slashes ? trailingslashit( $url ) : untrailingslashit( $url ); |
| 101 | - 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; | |
| 102 | 183 | } |
| 103 | 184 | |
| 104 | 185 | /** |
| 105 | - * Prepares rewrite rules filters | |
| 186 | + * Prepares rewrite rules filters. | |
| 106 | 187 | * |
| 107 | 188 | * @since 1.6 |
| 189 | + * | |
| 190 | + * @return string[] | |
| 108 | 191 | */ |
| 109 | 192 | public function get_rewrite_rules_filters() { |
| 110 | - // Make sure we have the right post types and taxonomies | |
| 193 | + // Make sure that we have the right post types and taxonomies. | |
| 111 | 194 | $types = array_values( array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies(), $this->model->get_filtered_taxonomies() ) ); |
| 112 | 195 | $types = array_merge( $this->always_rewrite, $types ); |
| 113 | 196 | |
| 114 | 197 | /** |
| 115 | - * 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. | |
| 116 | 199 | * |
| 117 | 200 | * @since 0.8.1 |
| 118 | 201 | * |
| 119 | - * @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). | |
| 120 | 203 | */ |
| 121 | 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' ); | |
| 122 | 218 | } |
| 123 | 219 | } |