PluginProbe
Polylang / 2.5
Polylang v2.5
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
← All changes | frontend/frontend.php +78 -88 3.0.32.5 View file →
@@ -1,70 +1,39 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 - * Main Polylang class when on frontend, accessible from @see PLL().
4 + * Frontend controller
5 + * accessible as $polylang global object
8 6 *
7 + * Properties:
8 + * options => inherited, reference to Polylang options array
9 + * model => inherited, reference to PLL_Model object
10 + * links_model => inherited, reference to PLL_Links_Model object
11 + * links => reference to PLL_Links object
12 + * static_pages => reference to PLL_Frontend_Static_Pages object
13 + * choose_lang => reference to PLL_Choose_lang object
14 + * curlang => current language
15 + * filters => reference to PLL_Frontend_Filters object
16 + * filters_links => reference to PLL_Frontend_Filters_Links object
17 + * filters_search => reference to PLL_Frontend_Filters_Search object
18 + * posts => reference to PLL_CRUD_Posts object
19 + * terms => reference to PLL_CRUD_Terms object
20 + * nav_menu => reference to PLL_Frontend_Nav_Menu object
21 + * sync => reference to PLL_Sync object
22 + * auto_translate => optional, reference to PLL_Auto_Translate object
23 + *
9 24 * @since 1.2
10 25 */
11 26 class PLL_Frontend extends PLL_Base {
12 - /**
13 - * Current language.
14 - *
15 - * @var PLL_Language
16 - */
17 27 public $curlang;
28 + public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate;
18 29
19 30 /**
20 - * @var PLL_Frontend_Auto_Translate
21 - */
22 - public $auto_translate;
23 -
24 - /**
25 - * The class selecting the current language.
31 + * Constructor
26 32 *
27 - * @var PLL_Choose_Lang
28 - */
29 - public $choose_lang;
30 -
31 - /**
32 - * @var PLL_Frontend_Filters
33 - */
34 - public $filters;
35 -
36 - /**
37 - * @var PLL_Frontend_Filters_Links
38 - */
39 - public $filters_links;
40 -
41 - /**
42 - * @var PLL_Frontend_Filters_Search
43 - */
44 - public $filters_search;
45 -
46 - /**
47 - * @var PLL_Frontend_Links
48 - */
49 - public $links;
50 -
51 - /**
52 - * @var PLL_Frontend_Nav_Menu
53 - */
54 - public $nav_menu;
55 -
56 - /**
57 - * @var PLL_Frontend_Static_Pages
58 - */
59 - public $static_pages;
60 -
61 - /**
62 - * Constructor.
63 - *
64 33 * @since 1.2
65 34 *
66 - * @param PLL_Links_Model $links_model Reference to the links model.
35 + * @param object $links_model
67 36 */
68 37 public function __construct( &$links_model ) {
69 38 parent::__construct( $links_model );
70 39
@@ -82,30 +51,56 @@
82 51 }
83 52 }
84 53
85 54 /**
55 + * Is the current request a REST API request?
56 + * Inspired by WP::parse_request()
57 + * Needed because at this point, the constant REST_REQUEST is not defined yet
58 + *
59 + * @since 2.4.1
60 + *
61 + * @return bool
62 + */
63 + public function is_rest_request() {
64 + $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
65 + $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
66 +
67 + $req_uri = trim( $_SERVER['REQUEST_URI'], '/' );
68 + $req_uri = preg_replace( $home_path_regex, '', $req_uri );
69 + $req_uri = trim( $req_uri, '/' );
70 + $req_uri = str_replace( 'index.php', '', $req_uri );
71 + $req_uri = trim( $req_uri, '/' );
72 +
73 + return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' );
74 + }
75 +
76 + /**
86 77 * Setups the language chooser based on options
87 78 *
88 79 * @since 1.2
89 80 */
90 81 public function init() {
91 - parent::init();
92 -
93 82 $this->links = new PLL_Frontend_Links( $this );
94 83
95 - // Static front page and page for posts
96 - if ( 'page' === get_option( 'show_on_front' ) ) {
97 - $this->static_pages = new PLL_Frontend_Static_Pages( $this );
98 - }
84 + // Don't set any language for REST requests when Polylang Pro is not active
85 + if ( ! class_exists( 'PLL_REST_Translated_Object' ) && $this->is_rest_request() ) {
86 + /** This action is documented in include/class-polylang.php */
87 + do_action( 'pll_no_language_defined' );
88 + } else {
89 + // Static front page and page for posts
90 + if ( 'page' === get_option( 'show_on_front' ) ) {
91 + $this->static_pages = new PLL_Frontend_Static_Pages( $this );
92 + }
99 93
100 - // Setup the language chooser
101 - $c = array( 'Content', 'Url', 'Url', 'Domain' );
102 - $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
103 - $this->choose_lang = new $class( $this );
104 - $this->choose_lang->init();
94 + // Setup the language chooser
95 + $c = array( 'Content', 'Url', 'Url', 'Domain' );
96 + $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
97 + $this->choose_lang = new $class( $this );
98 + $this->choose_lang->init();
105 99
106 - // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content
107 - $this->nav_menu = new PLL_Frontend_Nav_Menu( $this );
100 + // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content
101 + $this->nav_menu = new PLL_Frontend_Nav_Menu( $this );
102 + }
108 103 }
109 104
110 105 /**
111 106 * Setups filters and nav menus once the language has been defined
@@ -110,10 +105,8 @@
110 105 /**
111 106 * Setups filters and nav menus once the language has been defined
112 107 *
113 108 * @since 1.2
114 - *
115 - * @return void
116 109 */
117 110 public function pll_language_defined() {
118 111 // Filters
119 112 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
@@ -118,9 +111,13 @@
118 111 // Filters
119 112 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
120 113 $this->filters = new PLL_Frontend_Filters( $this );
121 114 $this->filters_search = new PLL_Frontend_Filters_Search( $this );
115 + $this->posts = new PLL_CRUD_Posts( $this );
116 + $this->terms = new PLL_CRUD_Terms( $this );
122 117
118 + $this->sync = new PLL_Sync( $this );
119 +
123 120 // Auto translate for Ajax
124 121 if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
125 122 $this->auto_translate();
126 123 }
@@ -126,14 +123,13 @@
126 123 }
127 124 }
128 125
129 126 /**
130 - * When querying multiple taxonomies, makes sure that the language is not the queried object.
127 + * When querying multiple taxonomies, makes sure that the language is not the queried object
131 128 *
132 129 * @since 1.8
133 130 *
134 - * @param WP_Query $query WP_Query object.
135 - * @return void
131 + * @param object $query WP_Query object
136 132 */
137 133 public function parse_tax_query( $query ) {
138 134 $pll_query = new PLL_Query( $query, $this->model );
139 135 $queried_taxonomies = $pll_query->get_queried_taxonomies();
@@ -143,14 +139,13 @@
143 139 }
144 140 }
145 141
146 142 /**
147 - * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts.
143 + * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
148 144 *
149 145 * @since 1.2
150 146 *
151 - * @param WP_Query $query WP_Query object.
152 - * @return void
147 + * @param object $query WP_Query object
153 148 */
154 149 public function parse_query( $query ) {
155 150 $qv = $query->query_vars;
156 151 $pll_query = new PLL_Query( $query, $this->model );
@@ -167,9 +162,9 @@
167 162 $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) );
168 163
169 164 // Remove pages query when the language is set unless we do a search
170 165 // Take care not to break the single page, attachment and taxonomies queries!
171 - if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) {
166 + if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_page && ! $query->is_attachment && empty( $taxonomies ) ) {
172 167 $query->set( 'post_type', 'post' );
173 168 }
174 169
175 170 // Unset the is_archive flag for language pages to prevent loading the archive template
@@ -189,10 +184,8 @@
189 184 /**
190 185 * Auto translate posts and terms ids
191 186 *
192 187 * @since 1.2
193 - *
194 - * @return void
195 188 */
196 189 public function auto_translate() {
197 190 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
198 191 }
@@ -197,25 +190,22 @@
197 190 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
198 191 }
199 192
200 193 /**
201 - * Resets some variables when the blog is switched.
202 - * Overrides the parent method.
194 + * Resets some variables when switching blog
195 + * Overrides parent method
203 196 *
204 197 * @since 1.5.1
205 198 *
206 - * @param int $new_blog_id New blog ID.
207 - * @param int $prev_blog_id Previous blog ID.
208 - * @return void
199 + * @param int $new_blog
200 + * @param int $old_blog
209 201 */
210 - public function switch_blog( $new_blog_id, $prev_blog_id ) {
211 - parent::switch_blog( $new_blog_id, $prev_blog_id );
212 -
213 - // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages.
214 - if ( $this->is_active_on_new_blog( $new_blog_id, $prev_blog_id ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
202 + public function switch_blog( $new_blog, $old_blog ) {
203 + // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages
204 + if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
215 205 static $restore_curlang;
216 206 if ( empty( $restore_curlang ) ) {
217 - $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs.
207 + $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs
218 208 }
219 209
220 210 $lang = $this->model->get_language( $restore_curlang );
221 211 $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );