| @@ -3,50 +3,123 @@ | ||
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * REST API controller | |
| 8 | - * accessible as $polylang global object | |
| 7 | + * Main Polylang class for REST API requests, accessible from @see PLL(). | |
| 9 | 8 | * |
| 10 | - * Properties: | |
| 11 | - * options => inherited, reference to Polylang options array | |
| 12 | - * model => inherited, reference to PLL_Model object | |
| 13 | - * links_model => inherited, reference to PLL_Links_Model object | |
| 14 | - * links => reference to PLL_Admin_Links object | |
| 15 | - * static_pages => reference to PLL_Static_Pages object | |
| 16 | - * filters => reference to PLL_Frontend_Filters object | |
| 17 | - * filters_links => reference to PLL_Filters_Links object | |
| 18 | - * posts => reference to PLL_CRUD_Posts object | |
| 19 | - * terms => reference to PLL_CRUD_Terms object | |
| 20 | - * | |
| 21 | 9 | * @since 2.6 |
| 22 | 10 | */ |
| 23 | 11 | class PLL_REST_Request extends PLL_Base { |
| 24 | - public $links, $static_pages, $posts, $terms, $filters, $filters_links; | |
| 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; | |
| 25 | 17 | |
| 26 | 18 | /** |
| 27 | - * Setup filters | |
| 19 | + * @var PLL_Filters|null | |
| 20 | + */ | |
| 21 | + public $filters; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * @var PLL_Filters_Links|null | |
| 25 | + */ | |
| 26 | + public $filters_links; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * @var PLL_Admin_Links|null | |
| 30 | + */ | |
| 31 | + public $links; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @var PLL_Nav_Menu|null | |
| 35 | + */ | |
| 36 | + public $nav_menu; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @var PLL_Static_Pages|null | |
| 40 | + */ | |
| 41 | + public $static_pages; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @var PLL_Filters_Widgets_Options|null | |
| 45 | + */ | |
| 46 | + public $filters_widgets_options; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Constructor. | |
| 28 | 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 | + /** | |
| 68 | + * Setup filters. | |
| 69 | + * | |
| 29 | 70 | * @since 2.6 |
| 71 | + * | |
| 72 | + * @return void | |
| 30 | 73 | */ |
| 31 | 74 | public function init() { |
| 32 | 75 | parent::init(); |
| 33 | 76 | |
| 34 | - if ( $this->model->get_languages_list() ) { | |
| 77 | + if ( ! $this->model->has_languages() ) { | |
| 78 | + return; | |
| 79 | + } | |
| 35 | 80 | |
| 36 | - /** This action is documented in include/class-polylang.php */ | |
| 37 | - do_action( 'pll_no_language_defined' ); // To load overridden textdomains. | |
| 81 | + add_filter( 'rest_pre_dispatch', array( $this, 'set_language' ), 10, 3 ); | |
| 38 | 82 | |
| 39 | - $this->filters_links = new PLL_Filters_Links( $this ); | |
| 40 | - $this->filters = new PLL_Filters( $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 ); | |
| 41 | 86 | |
| 42 | - // Static front page and page for posts | |
| 43 | - if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 44 | - $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'] ) ); | |
| 45 | 112 | } |
| 113 | + } | |
| 46 | 114 | |
| 47 | - $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 | + } | |
| 48 | 122 | |
| 49 | - $this->nav_menu = new PLL_Nav_Menu( $this ); // For auto added pages to menu | |
| 50 | - } | |
| 123 | + return $result; | |
| 51 | 124 | } |
| 52 | 125 | } |