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