| @@ -1,139 +1,76 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | -/** | |
| 7 | - * Links model abstract class | |
| 3 | +/* | |
| 4 | + * links model abstract class | |
| 8 | 5 | * |
| 9 | 6 | * @since 1.5 |
| 10 | 7 | */ |
| 11 | 8 | abstract class PLL_Links_Model { |
| 12 | 9 | public $model, $options; |
| 13 | - public $home; // used to store the home url before it is filtered | |
| 14 | - public $using_permalinks; | |
| 10 | + public $home; // used to avoid several calls to get_option('home') | |
| 15 | 11 | |
| 16 | - /** | |
| 17 | - * Constructor | |
| 12 | + /* | |
| 13 | + * constructor | |
| 18 | 14 | * |
| 19 | 15 | * @since 1.5 |
| 20 | 16 | * |
| 21 | 17 | * @param object $model PLL_Model instance |
| 22 | 18 | */ |
| 23 | - public function __construct( &$model ) { | |
| 19 | + public function __construct(&$model) { | |
| 24 | 20 | $this->model = &$model; |
| 25 | 21 | $this->options = &$model->options; |
| 26 | 22 | |
| 27 | - $this->home = home_url(); | |
| 28 | - | |
| 29 | - add_filter( 'pll_languages_list', array( $this, 'pll_languages_list' ), 4 ); // after PLL_Static_Pages | |
| 30 | - add_filter( 'pll_after_languages_cache', array( $this, 'pll_after_languages_cache' ) ); | |
| 31 | - | |
| 32 | - // adds our domains or subdomains to allowed hosts for safe redirection | |
| 33 | - add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) ); | |
| 23 | + $this->home = get_option('home'); | |
| 34 | 24 | } |
| 35 | 25 | |
| 36 | - /** | |
| 37 | - * Changes the language code in url | |
| 26 | + /* | |
| 27 | + * returns the link to the first page when using pretty permalinks | |
| 38 | 28 | * |
| 39 | - * @since 1.5 | |
| 29 | + * @since 1.2 | |
| 40 | 30 | * |
| 41 | - * @param string $url url to modify | |
| 42 | - * @param object $lang language | |
| 31 | + * @param string $url url to modify | |
| 43 | 32 | * @return string modified url |
| 44 | 33 | */ |
| 45 | - public function switch_language_in_link( $url, $lang ) { | |
| 46 | - $url = $this->remove_language_from_link( $url ); | |
| 47 | - return $this->add_language_to_link( $url, $lang ); | |
| 34 | + public function remove_paged_from_link($url) { | |
| 35 | + return preg_replace('#\/page\/[0-9]+\/#', '/', $url); // FIXME trailing slash ? | |
| 48 | 36 | } |
| 49 | 37 | |
| 50 | - /** | |
| 51 | - * Get hosts managed on the website | |
| 38 | + /* | |
| 39 | + * returns the link to the paged page when using pretty permalinks | |
| 52 | 40 | * |
| 53 | 41 | * @since 1.5 |
| 54 | 42 | * |
| 55 | - * @return array list of hosts | |
| 43 | + * @param string $url url to modify | |
| 44 | + * @param int $page | |
| 45 | + * @return string modified url | |
| 56 | 46 | */ |
| 57 | - public function get_hosts() { | |
| 58 | - return array( wp_parse_url( $this->home, PHP_URL_HOST ) ); | |
| 47 | + public function add_paged_to_link($url, $page) { | |
| 48 | + return trailingslashit($url) . 'page/' . $page; // FIXME trailing slash ? | |
| 59 | 49 | } |
| 60 | 50 | |
| 61 | - /** | |
| 62 | - * Returns the home url | |
| 63 | - * | |
| 64 | - * @since 1.3.1 | |
| 65 | - * | |
| 66 | - * @param object $lang PLL_Language object | |
| 67 | - * @return string | |
| 68 | - */ | |
| 69 | - public function home_url( $lang ) { | |
| 70 | - $url = trailingslashit( $this->home ); | |
| 71 | - return $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ? $url : $this->add_language_to_link( $url, $lang ); | |
| 72 | - } | |
| 73 | 51 | |
| 74 | - /** | |
| 75 | - * Sets the home urls | |
| 52 | + /* | |
| 53 | + * changes the language code in url | |
| 76 | 54 | * |
| 77 | - * @since 1.8 | |
| 55 | + * @since 1.5 | |
| 78 | 56 | * |
| 79 | - * @param object $language | |
| 57 | + * @param string $url url to modify | |
| 58 | + * @param object $lang language | |
| 59 | + * @return string modified url | |
| 80 | 60 | */ |
| 81 | - protected function set_home_url( $language ) { | |
| 82 | - // We should always have a default language here, except, temporarily, in PHPUnit tests. The test here protects against PHP notices. | |
| 83 | - if ( isset( $this->options['default_lang'] ) ) { | |
| 84 | - $search_url = $this->home_url( $language ); | |
| 85 | - $home_url = empty( $language->page_on_front ) || $this->options['redirect_lang'] ? $search_url : $this->front_page_url( $language ); | |
| 86 | - $language->set_home_url( $search_url, $home_url ); | |
| 87 | - } | |
| 61 | + public function switch_language_in_link($url, $lang) { | |
| 62 | + $url = $this->remove_language_from_link($url); | |
| 63 | + return $this->add_language_to_link($url, $lang); | |
| 88 | 64 | } |
| 89 | 65 | |
| 90 | - /** | |
| 91 | - * Sets the home urls and flags before the languages are persistently cached | |
| 66 | + /* | |
| 67 | + * get hosts managed on the website | |
| 92 | 68 | * |
| 93 | - * @since 1.8 | |
| 69 | + * @since 1.5 | |
| 94 | 70 | * |
| 95 | - * @param array $languages array of PLL_Language objects | |
| 96 | - * @return array | |
| 71 | + * @return array list of hosts | |
| 97 | 72 | */ |
| 98 | - public function pll_languages_list( $languages ) { | |
| 99 | - foreach ( $languages as $language ) { | |
| 100 | - $this->set_home_url( $language ); | |
| 101 | - $language->set_flag(); | |
| 102 | - } | |
| 103 | - return $languages; | |
| 104 | - } | |
| 105 | - | |
| 106 | - /** | |
| 107 | - * Sets the home urls when not cached | |
| 108 | - * Sets the home urls scheme | |
| 109 | - * | |
| 110 | - * @since 1.8 | |
| 111 | - * | |
| 112 | - * @param array $languages array of PLL_Language objects | |
| 113 | - * @return array | |
| 114 | - */ | |
| 115 | - public function pll_after_languages_cache( $languages ) { | |
| 116 | - foreach ( $languages as $language ) { | |
| 117 | - // Get the home urls when not cached | |
| 118 | - if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || ( defined( 'PLL_CACHE_HOME_URL' ) && ! PLL_CACHE_HOME_URL ) ) { | |
| 119 | - $this->set_home_url( $language ); | |
| 120 | - } | |
| 121 | - | |
| 122 | - // Ensures that the ( possibly cached ) home and flag urls use the right scheme http or https. | |
| 123 | - $language->set_url_scheme(); | |
| 124 | - } | |
| 125 | - return $languages; | |
| 126 | - } | |
| 127 | - | |
| 128 | - /** | |
| 129 | - * Adds our domains or subdomains to allowed hosts for safe redirection | |
| 130 | - * | |
| 131 | - * @since 1.4.3 | |
| 132 | - * | |
| 133 | - * @param array $hosts allowed hosts | |
| 134 | - * @return array | |
| 135 | - */ | |
| 136 | - public function allowed_redirect_hosts( $hosts ) { | |
| 137 | - return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) ); | |
| 73 | + public function get_hosts() { | |
| 74 | + return array(parse_url($this->home, PHP_URL_HOST)); | |
| 138 | 75 | } |
| 139 | 76 | } |