| @@ -1,77 +1,133 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | - * REST API controller | |
| 5 | - * accessible as $polylang global object | |
| 7 | + * Main Polylang class for REST API requests, accessible from @see PLL(). | |
| 6 | 8 | * |
| 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_Admin_Links object | |
| 12 | - * static_pages => reference to PLL_Static_Pages object | |
| 13 | - * filters => reference to PLL_Frontend_Filters object | |
| 14 | - * filters_links => reference to PLL_Filters_Links object | |
| 15 | - * posts => reference to PLL_CRUD_Posts object | |
| 16 | - * terms => reference to PLL_CRUD_Terms object | |
| 17 | - * sync => reference to PLL_Sync object | |
| 18 | - * | |
| 19 | 9 | * @since 2.6 |
| 20 | 10 | */ |
| 21 | 11 | class PLL_REST_Request extends PLL_Base { |
| 22 | - public $links, $static_pages, $posts, $terms, $filters, $filters_links, $sync; | |
| 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; | |
| 23 | 17 | |
| 24 | 18 | /** |
| 25 | - * Setup filters | |
| 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 | + * Constructor. | |
| 26 | 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 | + /** | |
| 73 | + * Setup filters. | |
| 74 | + * | |
| 27 | 75 | * @since 2.6 |
| 76 | + * | |
| 77 | + * @return void | |
| 28 | 78 | */ |
| 29 | 79 | public function init() { |
| 30 | 80 | parent::init(); |
| 31 | 81 | |
| 32 | - if ( $this->model->get_languages_list() ) { | |
| 82 | + $this->default_term = new PLL_Default_Term( $this ); | |
| 83 | + $this->default_term->add_hooks(); | |
| 33 | 84 | |
| 34 | - /** This action is documented in include/class-polylang.php */ | |
| 35 | - do_action( 'pll_no_language_defined' ); // To load overridden textdomains. | |
| 85 | + if ( ! $this->model->has_languages() ) { | |
| 86 | + return; | |
| 87 | + } | |
| 36 | 88 | |
| 37 | - $this->filters_links = new PLL_Filters_Links( $this ); | |
| 38 | - $this->filters = new PLL_Filters( $this ); | |
| 89 | + add_filter( 'rest_pre_dispatch', array( $this, 'set_language' ), 10, 3 ); | |
| 39 | 90 | |
| 40 | - // Static front page and page for posts | |
| 41 | - if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 42 | - $this->static_pages = new PLL_Static_Pages( $this ); | |
| 43 | - } | |
| 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 ); | |
| 44 | 94 | |
| 45 | - $this->links = new PLL_Admin_Links( $this ); | |
| 46 | - $this->posts = new PLL_CRUD_Posts( $this ); | |
| 47 | - $this->terms = new PLL_CRUD_Terms( $this ); | |
| 48 | - $this->sync = new PLL_Sync( $this ); | |
| 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 | + } | |
| 49 | 98 | |
| 50 | - $this->nav_menu = new PLL_Nav_Menu( $this ); // For auto added pages to menu | |
| 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' ); | |
| 51 | 113 | |
| 52 | - // Share term slugs | |
| 53 | - if ( get_option( 'permalink_structure' ) && $this->options['force_lang'] && class_exists( 'PLL_Share_Term_Slug' ) ) { | |
| 54 | - $this->share_term_slug = new PLL_Share_Term_Slug( $this ); | |
| 55 | - } | |
| 114 | + if ( ! empty( $lang ) && is_string( $lang ) ) { | |
| 115 | + $this->curlang = $this->model->get_language( sanitize_key( $lang ) ); | |
| 56 | 116 | |
| 57 | - // Translate slugs, only for pretty permalinks | |
| 58 | - if ( get_option( 'permalink_structure' ) && class_exists( 'PLL_Translate_Slugs' ) ) { | |
| 59 | - $curlang = null; | |
| 60 | - $slugs_model = new PLL_Translate_Slugs_Model( $this ); | |
| 61 | - $this->translate_slugs = new PLL_Translate_Slugs( $slugs_model, $curlang ); | |
| 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'] ) ); | |
| 62 | 120 | } |
| 121 | + } | |
| 63 | 122 | |
| 64 | - if ( class_exists( 'PLL_Sync_Post_Model' ) ) { | |
| 65 | - $this->sync_post_model = new PLL_Sync_Post_Model( $this ); | |
| 66 | - } | |
| 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 | + } | |
| 67 | 130 | |
| 68 | - if ( class_exists( 'PLL_Sync_Post_REST' ) ) { | |
| 69 | - $this->sync_post = new PLL_Sync_Post_REST( $this ); | |
| 70 | - } | |
| 71 | - | |
| 72 | - if ( class_exists( 'PLL_Duplicate_REST' ) ) { | |
| 73 | - $this->duplicate_rest = new PLL_Duplicate_REST(); | |
| 74 | - } | |
| 75 | - } | |
| 131 | + return $result; | |
| 76 | 132 | } |
| 77 | 133 | } |