PluginProbe
Polylang / 3.8.3
Polylang v3.8.3
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 / src / rest-request.php

rest-request.php in Polylang 3.8.3, at src/rest-request.php

165 lines 4.2 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 * Main Polylang class for REST API requests, accessible from @see PLL().
8 *
9 * @since 2.6
10 */
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;
17
18 /**
19 * @var PLL_Default_Term|null
20 */
21 public $default_term;
22
23 /**
24 * @var PLL_Filters|null
25 */
26 public $filters;
27
28 /**
29 * @var PLL_Filters_Links|null
30 */
31 public $filters_links;
32
33 /**
34 * @var PLL_Admin_Links|null
35 */
36 public $links;
37
38 /**
39 * @var PLL_Nav_Menu|null
40 */
41 public $nav_menu;
42
43 /**
44 * @var PLL_Static_Pages|null
45 */
46 public $static_pages;
47
48 /**
49 * @var PLL_Filters_Widgets_Options|null
50 */
51 public $filters_widgets_options;
52
53 /**
54 * @var PLL_Filters_Sanitization|null
55 */
56 public $filters_sanitization;
57
58 /**
59 * Constructor.
60 *
61 * @since 3.4
62 *
63 * @param PLL_Links_Model $links_model Reference to the links model.
64 */
65 public function __construct( &$links_model ) {
66 parent::__construct( $links_model );
67
68 // Static front page and page for posts.
69 // Early instantiated to be able to correctly initialize language properties.
70 if ( 'page' === get_option( 'show_on_front' ) ) {
71 $this->static_pages = new PLL_Static_Pages( $this );
72 }
73
74 $this->model->set_languages_ready();
75 }
76
77 /**
78 * Setup filters.
79 *
80 * @since 2.6
81 *
82 * @return void
83 */
84 public function init() {
85 parent::init();
86
87 $this->default_term = new PLL_Default_Term( $this );
88 $this->default_term->add_hooks();
89
90 if ( ! $this->model->has_languages() ) {
91 return;
92 }
93
94 add_filter( 'rest_pre_dispatch', array( $this, 'set_language' ), 10, 3 );
95 add_filter( 'rest_request_before_callbacks', array( $this, 'set_filters_sanitization' ) );
96
97 $this->filters_links = new PLL_Filters_Links( $this );
98 $this->filters = new PLL_Filters( $this );
99 $this->filters_widgets_options = new PLL_Filters_Widgets_Options( $this );
100
101 $this->links = new PLL_Admin_Links( $this );
102 $this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); // For auto added pages to menu.
103 }
104
105 /**
106 * Sets the current language during a REST request if sent.
107 *
108 * @since 3.3
109 *
110 * @param mixed $result Response to replace the requested version with. Remains untouched.
111 * @param WP_REST_Server $server Server instance.
112 * @param WP_REST_Request $request Request used to generate the response.
113 * @return mixed Untouched $result.
114 *
115 * @phpstan-param WP_REST_Request<array{lang?: string}> $request
116 */
117 public function set_language( $result, $server, $request ) {
118 $lang = $request->get_param( 'lang' );
119
120 if ( ! empty( $lang ) && is_string( $lang ) ) {
121 $this->curlang = $this->model->get_language( sanitize_key( $lang ) );
122
123 if ( empty( $this->curlang ) && ! empty( $this->options['default_lang'] ) ) {
124 // A lang has been requested but it is invalid, let's fall back to the default one.
125 $this->curlang = $this->model->get_language( sanitize_key( $this->options['default_lang'] ) );
126 }
127 }
128
129 if ( ! empty( $this->curlang ) ) {
130 /** This action is documented in src/frontend/choose-lang.php */
131 do_action( 'pll_language_defined', $this->curlang->slug, $this->curlang );
132 } else {
133 /** This action is documented in src/class-polylang.php */
134 do_action( 'pll_no_language_defined' ); // To load overridden textdomains.
135 }
136
137 return $result;
138 }
139
140 /**
141 * Initialize sanitization filters with the correct language locale.
142 *
143 * @see WP_REST_Server::dispatch()
144 *
145 * @since 2.9
146 * @since 3.8 Moved from Polylang Pro and hooked on 'rest_request_before_callbacks' instead of 'rest_pre_dispatch'.
147 *
148 * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client.
149 * @return WP_REST_Response|WP_HTTP_Response|WP_Error|mixed
150 */
151 public function set_filters_sanitization( $response ) {
152 $language = $this->request->get_language();
153 if ( empty( $language ) ) {
154 $type = $this->request->get_object_type();
155 $language = $type ? $this->model->$type->get_language( $this->request->get_id() ) : null;
156 }
157
158 if ( ! empty( $language ) ) {
159 $this->filters_sanitization = new PLL_Filters_Sanitization( $language->locale );
160 }
161
162 return $response;
163 }
164 }
165