| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | * model => inherited, reference to PLL_Model object |
| 10 | 10 | * links_model => inherited, reference to PLL_Links_Model object |
| 11 | 11 | * links => reference to PLL_Links object |
| 12 | 12 | * static_pages => reference to PLL_Frontend_Static_Pages object |
| 13 | - * choose_lang => reference to PLL_Choose_Lang object | |
| 13 | + * choose_lang => reference to PLL_Choose_lang object | |
| 14 | 14 | * curlang => current language |
| 15 | 15 | * filters => reference to PLL_Frontend_Filters object |
| 16 | 16 | * filters_links => reference to PLL_Frontend_Filters_Links object |
| 17 | 17 | * filters_search => reference to PLL_Frontend_Filters_Search object |
| @@ -51,59 +51,56 @@ | ||
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | + * Is the current request a REST API request? | |
| 56 | + * Inspired by WP::parse_request() | |
| 57 | + * Needed because at this point, the constant REST_REQUEST is not defined yet | |
| 58 | + * | |
| 59 | + * @since 2.4.1 | |
| 60 | + * | |
| 61 | + * @return bool | |
| 62 | + */ | |
| 63 | + public function is_rest_request() { | |
| 64 | + $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); | |
| 65 | + $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); | |
| 66 | + | |
| 67 | + $req_uri = trim( $_SERVER['REQUEST_URI'], '/' ); | |
| 68 | + $req_uri = preg_replace( $home_path_regex, '', $req_uri ); | |
| 69 | + $req_uri = trim( $req_uri, '/' ); | |
| 70 | + $req_uri = str_replace( 'index.php', '', $req_uri ); | |
| 71 | + $req_uri = trim( $req_uri, '/' ); | |
| 72 | + | |
| 73 | + return 0 === strpos( $req_uri, rest_get_url_prefix() . '/' ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 55 | 77 | * Setups the language chooser based on options |
| 56 | 78 | * |
| 57 | 79 | * @since 1.2 |
| 58 | 80 | */ |
| 59 | 81 | public function init() { |
| 60 | - parent::init(); | |
| 61 | - | |
| 62 | 82 | $this->links = new PLL_Frontend_Links( $this ); |
| 63 | 83 | |
| 64 | - // Static front page and page for posts | |
| 65 | - if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 66 | - $this->static_pages = new PLL_Frontend_Static_Pages( $this ); | |
| 67 | - } | |
| 68 | - | |
| 69 | - // Setup the language chooser | |
| 70 | - $c = array( 'Content', 'Url', 'Url', 'Domain' ); | |
| 71 | - $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; | |
| 72 | - $this->choose_lang = new $class( $this ); | |
| 73 | - $this->choose_lang->init(); | |
| 74 | - | |
| 75 | - // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content | |
| 76 | - $this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); | |
| 77 | - | |
| 78 | - // Cross domain | |
| 79 | - if ( PLL_COOKIE ) { | |
| 80 | - $class = array( 2 => 'PLL_Xdata_Subdomain', 3 => 'PLL_Xdata_Domain' ); | |
| 81 | - if ( isset( $class[ $this->options['force_lang'] ] ) && class_exists( $class[ $this->options['force_lang'] ] ) ) { | |
| 82 | - $this->xdata = new $class[ $this->options['force_lang'] ]( $this ); | |
| 84 | + // Don't set any language for REST requests when Polylang Pro is not active | |
| 85 | + if ( ! class_exists( 'PLL_REST_Translated_Object' ) && $this->is_rest_request() ) { | |
| 86 | + /** This action is documented in include/class-polylang.php */ | |
| 87 | + do_action( 'pll_no_language_defined' ); | |
| 88 | + } else { | |
| 89 | + // Static front page and page for posts | |
| 90 | + if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 91 | + $this->static_pages = new PLL_Frontend_Static_Pages( $this ); | |
| 83 | 92 | } |
| 84 | - } | |
| 85 | 93 | |
| 86 | - if ( get_option( 'permalink_structure' ) ) { | |
| 87 | - // Translate slugs | |
| 88 | - if ( class_exists( 'PLL_Frontend_Translate_Slugs' ) ) { | |
| 89 | - $slugs_model = new PLL_Translate_Slugs_Model( $this ); | |
| 90 | - $this->translate_slugs = new PLL_Frontend_Translate_Slugs( $slugs_model, $this->curlang ); | |
| 91 | - } | |
| 94 | + // Setup the language chooser | |
| 95 | + $c = array( 'Content', 'Url', 'Url', 'Domain' ); | |
| 96 | + $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; | |
| 97 | + $this->choose_lang = new $class( $this ); | |
| 98 | + $this->choose_lang->init(); | |
| 92 | 99 | |
| 93 | - // Share term slugs | |
| 94 | - if ( $this->options['force_lang'] && class_exists( 'PLL_Share_Term_Slug' ) ) { | |
| 95 | - $this->share_term_slug = new PLL_Share_Term_Slug( $this ); | |
| 96 | - } | |
| 100 | + // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content | |
| 101 | + $this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); | |
| 97 | 102 | } |
| 98 | - | |
| 99 | - if ( class_exists( 'PLL_Sync_Post_Model' ) ) { | |
| 100 | - $this->sync_post_model = new PLL_Sync_Post_Model( $this ); | |
| 101 | - } | |
| 102 | - | |
| 103 | - if ( class_exists( 'PLL_Sync_Post' ) ) { | |
| 104 | - $this->sync_post = new PLL_Sync_Post( $this ); | |
| 105 | - } | |
| 106 | 103 | } |
| 107 | 104 | |
| 108 | 105 | /** |
| 109 | 106 | * Setups filters and nav menus once the language has been defined |
| @@ -165,9 +162,9 @@ | ||
| 165 | 162 | $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) ); |
| 166 | 163 | |
| 167 | 164 | // Remove pages query when the language is set unless we do a search |
| 168 | 165 | // Take care not to break the single page, attachment and taxonomies queries! |
| 169 | - if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) { | |
| 166 | + if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_page && ! $query->is_attachment && empty( $taxonomies ) ) { | |
| 170 | 167 | $query->set( 'post_type', 'post' ); |
| 171 | 168 | } |
| 172 | 169 | |
| 173 | 170 | // Unset the is_archive flag for language pages to prevent loading the archive template |