| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Frontend controller |
| 8 | 5 | * accessible as $polylang global object |
| @@ -12,9 +9,9 @@ | ||
| 12 | 9 | * model => inherited, reference to PLL_Model object |
| 13 | 10 | * links_model => inherited, reference to PLL_Links_Model object |
| 14 | 11 | * links => reference to PLL_Links object |
| 15 | 12 | * static_pages => reference to PLL_Frontend_Static_Pages object |
| 16 | - * choose_lang => reference to PLL_Choose_Lang object | |
| 13 | + * choose_lang => reference to PLL_Choose_lang object | |
| 17 | 14 | * curlang => current language |
| 18 | 15 | * filters => reference to PLL_Frontend_Filters object |
| 19 | 16 | * filters_links => reference to PLL_Frontend_Filters_Links object |
| 20 | 17 | * filters_search => reference to PLL_Frontend_Filters_Search object |
| @@ -20,8 +17,9 @@ | ||
| 20 | 17 | * filters_search => reference to PLL_Frontend_Filters_Search object |
| 21 | 18 | * posts => reference to PLL_CRUD_Posts object |
| 22 | 19 | * terms => reference to PLL_CRUD_Terms object |
| 23 | 20 | * nav_menu => reference to PLL_Frontend_Nav_Menu object |
| 21 | + * sync => reference to PLL_Sync object | |
| 24 | 22 | * auto_translate => optional, reference to PLL_Auto_Translate object |
| 25 | 23 | * |
| 26 | 24 | * @since 1.2 |
| 27 | 25 | */ |
| @@ -53,30 +51,56 @@ | ||
| 53 | 51 | } |
| 54 | 52 | } |
| 55 | 53 | |
| 56 | 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 | + /** | |
| 57 | 77 | * Setups the language chooser based on options |
| 58 | 78 | * |
| 59 | 79 | * @since 1.2 |
| 60 | 80 | */ |
| 61 | 81 | public function init() { |
| 62 | - parent::init(); | |
| 63 | - | |
| 64 | 82 | $this->links = new PLL_Frontend_Links( $this ); |
| 65 | 83 | |
| 66 | - // Static front page and page for posts | |
| 67 | - if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 68 | - $this->static_pages = new PLL_Frontend_Static_Pages( $this ); | |
| 69 | - } | |
| 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 ); | |
| 92 | + } | |
| 70 | 93 | |
| 71 | - // Setup the language chooser | |
| 72 | - $c = array( 'Content', 'Url', 'Url', 'Domain' ); | |
| 73 | - $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; | |
| 74 | - $this->choose_lang = new $class( $this ); | |
| 75 | - $this->choose_lang->init(); | |
| 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(); | |
| 76 | 99 | |
| 77 | - // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content | |
| 78 | - $this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); | |
| 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 ); | |
| 102 | + } | |
| 79 | 103 | } |
| 80 | 104 | |
| 81 | 105 | /** |
| 82 | 106 | * Setups filters and nav menus once the language has been defined |
| @@ -87,9 +111,13 @@ | ||
| 87 | 111 | // Filters |
| 88 | 112 | $this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| 89 | 113 | $this->filters = new PLL_Frontend_Filters( $this ); |
| 90 | 114 | $this->filters_search = new PLL_Frontend_Filters_Search( $this ); |
| 115 | + $this->posts = new PLL_CRUD_Posts( $this ); | |
| 116 | + $this->terms = new PLL_CRUD_Terms( $this ); | |
| 91 | 117 | |
| 118 | + $this->sync = new PLL_Sync( $this ); | |
| 119 | + | |
| 92 | 120 | // Auto translate for Ajax |
| 93 | 121 | if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) { |
| 94 | 122 | $this->auto_translate(); |
| 95 | 123 | } |
| @@ -134,9 +162,9 @@ | ||
| 134 | 162 | $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) ); |
| 135 | 163 | |
| 136 | 164 | // Remove pages query when the language is set unless we do a search |
| 137 | 165 | // Take care not to break the single page, attachment and taxonomies queries! |
| 138 | - 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 ) ) { | |
| 139 | 167 | $query->set( 'post_type', 'post' ); |
| 140 | 168 | } |
| 141 | 169 | |
| 142 | 170 | // Unset the is_archive flag for language pages to prevent loading the archive template |