| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Links model base class when using pretty permalinks |
| 5 |
* |
| 6 |
* @since 1.6 |
| 7 |
*/ |
| 8 |
abstract class PLL_Links_Permalinks extends PLL_Links_Model { |
| 9 |
public $using_permalinks = true; |
| 10 |
protected $index = 'index.php'; // Need this before $wp_rewrite is created, also hardcoded in wp-includes/rewrite.php |
| 11 |
protected $root, $use_trailing_slashes; |
| 12 |
protected $always_rewrite = array( 'date', 'root', 'comments', 'search', 'author' ); |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor |
| 16 |
* |
| 17 |
* @since 1.8 |
| 18 |
* |
| 19 |
* @param object $model PLL_Model instance |
| 20 |
*/ |
| 21 |
public function __construct( &$model ) { |
| 22 |
parent::__construct( $model ); |
| 23 |
|
| 24 |
// Inspired by wp-includes/rewrite.php |
| 25 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 26 |
$this->root = preg_match( '#^/*' . $this->index . '#', $permalink_structure ) ? $this->index . '/' : ''; |
| 27 |
$this->use_trailing_slashes = ( '/' == substr( $permalink_structure, -1, 1 ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns the link to the first page when using pretty permalinks |
| 32 |
* |
| 33 |
* @since 1.2 |
| 34 |
* |
| 35 |
* @param string $url url to modify |
| 36 |
* @return string modified url |
| 37 |
*/ |
| 38 |
public function remove_paged_from_link( $url ) { |
| 39 |
/** |
| 40 |
* Filter an url after the paged part has been removed |
| 41 |
* |
| 42 |
* @since 2.0.6 |
| 43 |
* |
| 44 |
* @param string $modified_url The link to the first page |
| 45 |
* @param string $original_url The link to the original paged page |
| 46 |
*/ |
| 47 |
return apply_filters( 'pll_remove_paged_from_link', preg_replace( '#\/page\/[0-9]+\/?#', $this->use_trailing_slashes ? '/' : '', $url ), $url ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the link to the paged page when using pretty permalinks |
| 52 |
* |
| 53 |
* @since 1.5 |
| 54 |
* |
| 55 |
* @param string $url url to modify |
| 56 |
* @param int $page |
| 57 |
* @return string modified url |
| 58 |
*/ |
| 59 |
public function add_paged_to_link( $url, $page ) { |
| 60 |
/** |
| 61 |
* Filter an url after the paged part has been added |
| 62 |
* |
| 63 |
* @since 2.0.6 |
| 64 |
* |
| 65 |
* @param string $modified_url The link to the paged page |
| 66 |
* @param string $original_url The link to the original first page |
| 67 |
* @param int $page The page number |
| 68 |
*/ |
| 69 |
return apply_filters( 'pll_add_paged_to_link', user_trailingslashit( trailingslashit( $url ) . 'page/' . $page, 'paged' ), $url, $page ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the home url |
| 74 |
* |
| 75 |
* @since 1.3.1 |
| 76 |
* |
| 77 |
* @param object $lang PLL_Language object |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
public function home_url( $lang ) { |
| 81 |
return trailingslashit( parent::home_url( $lang ) ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns the static front page url |
| 86 |
* |
| 87 |
* @since 1.8 |
| 88 |
* |
| 89 |
* @param object $lang |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function front_page_url( $lang ) { |
| 93 |
if ( $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ) { |
| 94 |
return trailingslashit( $this->home ); |
| 95 |
} |
| 96 |
$url = home_url( $this->root . get_page_uri( $lang->page_on_front ) ); |
| 97 |
$url = $this->use_trailing_slashes ? trailingslashit( $url ) : untrailingslashit( $url ); |
| 98 |
return $this->options['force_lang'] ? $this->add_language_to_link( $url, $lang ) : $url; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Prepares rewrite rules filters |
| 103 |
* |
| 104 |
* @since 1.6 |
| 105 |
*/ |
| 106 |
public function get_rewrite_rules_filters() { |
| 107 |
// Make sure we have the right post types and taxonomies |
| 108 |
$types = array_values( array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies(), $this->model->get_filtered_taxonomies() ) ); |
| 109 |
$types = array_merge( $this->always_rewrite, $types ); |
| 110 |
|
| 111 |
/** |
| 112 |
* Filter the list of rewrite rules filters to be used by Polylang |
| 113 |
* |
| 114 |
* @since 0.8.1 |
| 115 |
* |
| 116 |
* @param array $types the list of filters (without '_rewrite_rules' at the end) |
| 117 |
*/ |
| 118 |
return apply_filters( 'pll_rewrite_rules', $types ); |
| 119 |
} |
| 120 |
} |
| 121 |
|