PluginProbe
Polylang / 3.6.7
Polylang v3.6.7
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 | include/rest-request.php +73 -20 3.0.33.6.7 View file →
@@ -3,45 +3,69 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * Main Polylang class for REST API requrests, accessible from @see PLL().
7 + * Main Polylang class for REST API requests, accessible from @see PLL().
8 8 *
9 9 * @since 2.6
10 10 */
11 11 class PLL_REST_Request extends PLL_Base {
12 + /**
13 + * @var PLL_Language|false|null A `PLL_Language` when defined, `false` otherwise. `null` until the language
14 + * definition process runs.
15 + */
16 + public $curlang;
12 17
13 18 /**
14 - * @var PLL_Filters
19 + * @var PLL_Filters|null
15 20 */
16 21 public $filters;
17 22
18 23 /**
19 - * @var PLL_Filters_Links
24 + * @var PLL_Filters_Links|null
20 25 */
21 26 public $filters_links;
22 27
23 28 /**
24 - * @var PLL_Admin_Links
29 + * @var PLL_Admin_Links|null
25 30 */
26 31 public $links;
27 32
28 33 /**
29 - * @var PLL_Nav_Menu
34 + * @var PLL_Nav_Menu|null
30 35 */
31 36 public $nav_menu;
32 37
33 38 /**
34 - * @var PLL_Static_Pages
39 + * @var PLL_Static_Pages|null
35 40 */
36 41 public $static_pages;
37 42
38 43 /**
39 - * @var PLL_Filters_Widgets_Options
44 + * @var PLL_Filters_Widgets_Options|null
40 45 */
41 - public $filters_widgets;
46 + public $filters_widgets_options;
42 47
43 48 /**
49 + * Constructor.
50 + *
51 + * @since 3.4
52 + *
53 + * @param PLL_Links_Model $links_model Reference to the links model.
54 + */
55 + public function __construct( &$links_model ) {
56 + parent::__construct( $links_model );
57 +
58 + // Static front page and page for posts.
59 + // Early instantiated to be able to correctly initialize language properties.
60 + if ( 'page' === get_option( 'show_on_front' ) ) {
61 + $this->static_pages = new PLL_Static_Pages( $this );
62 + }
63 +
64 + $this->model->set_languages_ready();
65 + }
66 +
67 + /**
44 68 * Setup filters.
45 69 *
46 70 * @since 2.6
47 71 *
@@ -49,24 +73,53 @@
49 73 */
50 74 public function init() {
51 75 parent::init();
52 76
53 - if ( $this->model->get_languages_list() ) {
77 + if ( ! $this->model->has_languages() ) {
78 + return;
79 + }
54 80
55 - /** This action is documented in include/class-polylang.php */
56 - do_action( 'pll_no_language_defined' ); // To load overridden textdomains.
81 + add_filter( 'rest_pre_dispatch', array( $this, 'set_language' ), 10, 3 );
57 82
58 - $this->filters_links = new PLL_Filters_Links( $this );
59 - $this->filters = new PLL_Filters( $this );
60 - $this->filters_widgets = new PLL_Filters_Widgets_Options( $this );
83 + $this->filters_links = new PLL_Filters_Links( $this );
84 + $this->filters = new PLL_Filters( $this );
85 + $this->filters_widgets_options = new PLL_Filters_Widgets_Options( $this );
61 86
62 - // Static front page and page for posts.
63 - if ( 'page' === get_option( 'show_on_front' ) ) {
64 - $this->static_pages = new PLL_Static_Pages( $this );
87 + $this->links = new PLL_Admin_Links( $this );
88 + $this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); // For auto added pages to menu.
89 + }
90 +
91 + /**
92 + * Sets the current language during a REST request if sent.
93 + *
94 + * @since 3.3
95 + *
96 + * @param mixed $result Response to replace the requested version with. Remains untouched.
97 + * @param WP_REST_Server $server Server instance.
98 + * @param WP_REST_Request $request Request used to generate the response.
99 + * @return mixed Untouched $result.
100 + *
101 + * @phpstan-param WP_REST_Request<array{lang?: string}> $request
102 + */
103 + public function set_language( $result, $server, $request ) {
104 + $lang = $request->get_param( 'lang' );
105 +
106 + if ( ! empty( $lang ) && is_string( $lang ) ) {
107 + $this->curlang = $this->model->get_language( sanitize_key( $lang ) );
108 +
109 + if ( empty( $this->curlang ) && ! empty( $this->options['default_lang'] ) && is_string( $this->options['default_lang'] ) ) {
110 + // A lang has been requested but it is invalid, let's fall back to the default one.
111 + $this->curlang = $this->model->get_language( sanitize_key( $this->options['default_lang'] ) );
65 112 }
113 + }
66 114
67 - $this->links = new PLL_Admin_Links( $this );
115 + if ( ! empty( $this->curlang ) ) {
116 + /** This action is documented in frontend/choose-lang.php */
117 + do_action( 'pll_language_defined', $this->curlang->slug, $this->curlang );
118 + } else {
119 + /** This action is documented in include/class-polylang.php */
120 + do_action( 'pll_no_language_defined' ); // To load overridden textdomains.
121 + }
68 122
69 - $this->nav_menu = new PLL_Nav_Menu( $this ); // For auto added pages to menu.
70 - }
123 + return $result;
71 124 }
72 125 }