PluginProbe
Polylang / 2.8.2
Polylang v2.8.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / links-permalinks.php

links-permalinks.php in Polylang 2.8.2, at include/links-permalinks.php

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