| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Links model base class when using pretty permalinks. |
| 8 |
* |
| 9 |
* @since 1.6 |
| 10 |
*/ |
| 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 |
*/ |
| 17 |
public $using_permalinks = true; |
| 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 |
*/ |
| 47 |
protected $always_rewrite = array( 'date', 'root', 'comments', 'search', 'author' ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor. |
| 51 |
* |
| 52 |
* @since 1.8 |
| 53 |
* |
| 54 |
* @param PLL_Model $model PLL_Model instance. |
| 55 |
*/ |
| 56 |
public function __construct( &$model ) { |
| 57 |
parent::__construct( $model ); |
| 58 |
|
| 59 |
// Inspired by WP_Rewrite. |
| 60 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 61 |
$this->root = preg_match( '#^/*' . $this->index . '#', $permalink_structure ) ? $this->index . '/' : ''; |
| 62 |
$this->use_trailing_slashes = ( '/' == substr( $permalink_structure, -1, 1 ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Initializes permalinks. |
| 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 |
* |
| 107 |
* @since 1.2 |
| 108 |
* |
| 109 |
* @param string $url The url to modify. |
| 110 |
* @return string The modified url. |
| 111 |
*/ |
| 112 |
public function remove_paged_from_link( $url ) { |
| 113 |
/** |
| 114 |
* Filters an url after the paged part has been removed. |
| 115 |
* |
| 116 |
* @since 2.0.6 |
| 117 |
* |
| 118 |
* @param string $modified_url The link to the first page. |
| 119 |
* @param string $original_url The link to the original paged page. |
| 120 |
*/ |
| 121 |
return apply_filters( 'pll_remove_paged_from_link', preg_replace( '#/page/[0-9]+/?#', $this->use_trailing_slashes ? '/' : '', $url ), $url ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the link to the paged page when using pretty permalinks. |
| 126 |
* |
| 127 |
* @since 1.5 |
| 128 |
* |
| 129 |
* @param string $url The url to modify. |
| 130 |
* @param int $page The page number. |
| 131 |
* @return string The modified url. |
| 132 |
*/ |
| 133 |
public function add_paged_to_link( $url, $page ) { |
| 134 |
/** |
| 135 |
* Filters an url after the paged part has been added. |
| 136 |
* |
| 137 |
* @since 2.0.6 |
| 138 |
* |
| 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. |
| 142 |
*/ |
| 143 |
return apply_filters( 'pll_add_paged_to_link', user_trailingslashit( trailingslashit( $url ) . 'page/' . $page, 'paged' ), $url, $page ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns the home url in a given language. |
| 148 |
* |
| 149 |
* @since 1.3.1 |
| 150 |
* @since 3.4 Accepts now a language slug. |
| 151 |
* |
| 152 |
* @param PLL_Language|string $language Language object or slug. |
| 153 |
* @return string |
| 154 |
*/ |
| 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 ) ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Returns the static front page url. |
| 165 |
* |
| 166 |
* @since 1.8 |
| 167 |
* @since 3.4 Accepts now an array of language properties. |
| 168 |
* |
| 169 |
* @param PLL_Language|array $language Language object or array of language properties. |
| 170 |
* @return string The static front page url. |
| 171 |
*/ |
| 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'] ) { |
| 178 |
return trailingslashit( $this->home ); |
| 179 |
} |
| 180 |
$url = home_url( $this->root . get_page_uri( $language['page_on_front'] ) ); |
| 181 |
$url = $this->use_trailing_slashes ? trailingslashit( $url ) : untrailingslashit( $url ); |
| 182 |
return $this->options['force_lang'] ? $this->add_language_to_link( $url, $language['slug'] ) : $url; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Prepares rewrite rules filters. |
| 187 |
* |
| 188 |
* @since 1.6 |
| 189 |
* |
| 190 |
* @return string[] |
| 191 |
*/ |
| 192 |
public function get_rewrite_rules_filters() { |
| 193 |
// Make sure that we have the right post types and taxonomies. |
| 194 |
$types = array_values( array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies(), $this->model->get_filtered_taxonomies() ) ); |
| 195 |
$types = array_merge( $this->always_rewrite, $types ); |
| 196 |
|
| 197 |
/** |
| 198 |
* Filters the list of rewrite rules filters to be used by Polylang. |
| 199 |
* |
| 200 |
* @since 0.8.1 |
| 201 |
* |
| 202 |
* @param array $types The list of filters (without '_rewrite_rules' at the end). |
| 203 |
*/ |
| 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' ); |
| 218 |
} |
| 219 |
} |
| 220 |
|