| 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-includes/rewrite.php |
| 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 |
* Returns the link to the first page when using pretty permalinks |
| 67 |
* |
| 68 |
* @since 1.2 |
| 69 |
* |
| 70 |
* @param string $url url to modify |
| 71 |
* @return string modified url |
| 72 |
*/ |
| 73 |
public function remove_paged_from_link( $url ) { |
| 74 |
/** |
| 75 |
* Filter an url after the paged part has been removed |
| 76 |
* |
| 77 |
* @since 2.0.6 |
| 78 |
* |
| 79 |
* @param string $modified_url The link to the first page |
| 80 |
* @param string $original_url The link to the original paged page |
| 81 |
*/ |
| 82 |
return apply_filters( 'pll_remove_paged_from_link', preg_replace( '#/page/[0-9]+/?#', $this->use_trailing_slashes ? '/' : '', $url ), $url ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the link to the paged page when using pretty permalinks |
| 87 |
* |
| 88 |
* @since 1.5 |
| 89 |
* |
| 90 |
* @param string $url url to modify |
| 91 |
* @param int $page |
| 92 |
* @return string modified url |
| 93 |
*/ |
| 94 |
public function add_paged_to_link( $url, $page ) { |
| 95 |
/** |
| 96 |
* Filter an url after the paged part has been added |
| 97 |
* |
| 98 |
* @since 2.0.6 |
| 99 |
* |
| 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 |
| 103 |
*/ |
| 104 |
return apply_filters( 'pll_add_paged_to_link', user_trailingslashit( trailingslashit( $url ) . 'page/' . $page, 'paged' ), $url, $page ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns the home url. |
| 109 |
* |
| 110 |
* @since 1.3.1 |
| 111 |
* |
| 112 |
* @param PLL_Language $lang PLL_Language object. |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public function home_url( $lang ) { |
| 116 |
return trailingslashit( parent::home_url( $lang ) ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Returns the static front page url. |
| 121 |
* |
| 122 |
* @since 1.8 |
| 123 |
* |
| 124 |
* @param PLL_Language $lang The language object. |
| 125 |
* @return string The static front page url. |
| 126 |
*/ |
| 127 |
public function front_page_url( $lang ) { |
| 128 |
if ( $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ) { |
| 129 |
return trailingslashit( $this->home ); |
| 130 |
} |
| 131 |
$url = home_url( $this->root . get_page_uri( $lang->page_on_front ) ); |
| 132 |
$url = $this->use_trailing_slashes ? trailingslashit( $url ) : untrailingslashit( $url ); |
| 133 |
return $this->options['force_lang'] ? $this->add_language_to_link( $url, $lang ) : $url; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Prepares rewrite rules filters |
| 138 |
* |
| 139 |
* @since 1.6 |
| 140 |
* |
| 141 |
* @return string[] |
| 142 |
*/ |
| 143 |
public function get_rewrite_rules_filters() { |
| 144 |
// Make sure we have the right post types and taxonomies |
| 145 |
$types = array_values( array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies(), $this->model->get_filtered_taxonomies() ) ); |
| 146 |
$types = array_merge( $this->always_rewrite, $types ); |
| 147 |
|
| 148 |
/** |
| 149 |
* Filter the list of rewrite rules filters to be used by Polylang |
| 150 |
* |
| 151 |
* @since 0.8.1 |
| 152 |
* |
| 153 |
* @param array $types the list of filters (without '_rewrite_rules' at the end) |
| 154 |
*/ |
| 155 |
return apply_filters( 'pll_rewrite_rules', $types ); |
| 156 |
} |
| 157 |
} |
| 158 |
|