| @@ -1,192 +1,135 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | -/** | |
| 7 | - * Frontend controller | |
| 3 | +/* | |
| 4 | + * frontend controller | |
| 8 | 5 | * accessible as $polylang global object |
| 9 | 6 | * |
| 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_Links object | |
| 15 | - * static_pages => reference to PLL_Frontend_Static_Pages object | |
| 16 | - * choose_lang => reference to PLL_Choose_Lang object | |
| 17 | - * curlang => current language | |
| 18 | - * filters => reference to PLL_Frontend_Filters object | |
| 19 | - * filters_links => reference to PLL_Frontend_Filters_Links object | |
| 20 | - * filters_search => reference to PLL_Frontend_Filters_Search object | |
| 21 | - * posts => reference to PLL_CRUD_Posts object | |
| 22 | - * terms => reference to PLL_CRUD_Terms object | |
| 23 | - * nav_menu => reference to PLL_Frontend_Nav_Menu object | |
| 24 | - * auto_translate => optional, reference to PLL_Auto_Translate object | |
| 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_Links object | |
| 12 | + * choose_lang => reference to PLL_Choose_lang object | |
| 13 | + * curlang => current language | |
| 14 | + * filters => reference to PLL_Filters object | |
| 15 | + * filters_search => reference to PLL_Frontend_Filters_Search object | |
| 16 | + * nav_menu => reference to PLL_Frontend_Nav_Menu object | |
| 17 | + * auto_translate => optional, reference to PLL_Auto_Translate object | |
| 25 | 18 | * |
| 26 | 19 | * @since 1.2 |
| 27 | 20 | */ |
| 28 | -class PLL_Frontend extends PLL_Base { | |
| 21 | +class PLL_Frontend extends PLL_Base{ | |
| 29 | 22 | public $curlang; |
| 30 | - public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate; | |
| 23 | + public $links, $choose_lang, $filters, $filters_search, $auto_translate; | |
| 31 | 24 | |
| 32 | - /** | |
| 33 | - * Constructor | |
| 25 | + /* | |
| 26 | + * constructor | |
| 34 | 27 | * |
| 35 | 28 | * @since 1.2 |
| 36 | 29 | * |
| 37 | 30 | * @param object $links_model |
| 38 | 31 | */ |
| 39 | - public function __construct( &$links_model ) { | |
| 40 | - parent::__construct( $links_model ); | |
| 32 | + public function __construct(&$links_model) { | |
| 33 | + parent::__construct($links_model); | |
| 41 | 34 | |
| 42 | - add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 ); | |
| 35 | + add_action('pll_language_defined', array(&$this, 'pll_language_defined'), 1, 2); | |
| 43 | 36 | |
| 44 | - // Avoids the language being the queried object when querying multiple taxonomies | |
| 45 | - add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 ); | |
| 37 | + // filters posts by language | |
| 38 | + add_filter('parse_query', array(&$this, 'parse_query'), 6); | |
| 46 | 39 | |
| 47 | - // Filters posts by language | |
| 48 | - add_action( 'parse_query', array( $this, 'parse_query' ), 6 ); | |
| 49 | - | |
| 50 | - // Not before 'check_canonical_url' | |
| 51 | - if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) { | |
| 52 | - add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 ); | |
| 53 | - } | |
| 40 | + // not before 'check_language_code_in_url' | |
| 41 | + if (!defined('PLL_AUTO_TRANSLATE') || PLL_AUTO_TRANSLATE) | |
| 42 | + add_action('wp', array(&$this, 'auto_translate'), 20); | |
| 54 | 43 | } |
| 55 | 44 | |
| 56 | - /** | |
| 57 | - * Setups the language chooser based on options | |
| 45 | + /* | |
| 46 | + * setups url modifications based on the links mode | |
| 47 | + * setups the language chooser based on options | |
| 58 | 48 | * |
| 59 | 49 | * @since 1.2 |
| 50 | + * | |
| 51 | + * @param object $links_model | |
| 60 | 52 | */ |
| 61 | 53 | public function init() { |
| 62 | - parent::init(); | |
| 54 | + $this->links = new PLL_Frontend_Links($this); | |
| 63 | 55 | |
| 64 | - $this->links = new PLL_Frontend_Links( $this ); | |
| 65 | - | |
| 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 | - } | |
| 70 | - | |
| 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(); | |
| 76 | - | |
| 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 ); | |
| 56 | + $c = array('Content', 'Url', 'Url', 'Domain'); | |
| 57 | + $class = 'PLL_Choose_Lang_' . $c[$this->options['force_lang'] * (get_option('permalink_structure') ? 1 : 0 )]; | |
| 58 | + $this->choose_lang = new $class($this); | |
| 79 | 59 | } |
| 80 | 60 | |
| 81 | - /** | |
| 82 | - * Setups filters and nav menus once the language has been defined | |
| 61 | + /* | |
| 62 | + * setups filters and nav menus once the language has been defined | |
| 83 | 63 | * |
| 84 | 64 | * @since 1.2 |
| 65 | + * | |
| 66 | + * @param string $slug current language slug | |
| 67 | + * @param object $curlang current language object | |
| 85 | 68 | */ |
| 86 | - public function pll_language_defined() { | |
| 87 | - // Filters | |
| 88 | - $this->filters_links = new PLL_Frontend_Filters_Links( $this ); | |
| 89 | - $this->filters = new PLL_Frontend_Filters( $this ); | |
| 90 | - $this->filters_search = new PLL_Frontend_Filters_Search( $this ); | |
| 69 | + public function pll_language_defined($slug, $curlang) { | |
| 70 | + $this->curlang = $curlang; | |
| 91 | 71 | |
| 92 | - // Auto translate for Ajax | |
| 93 | - if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) { | |
| 94 | - $this->auto_translate(); | |
| 95 | - } | |
| 96 | - } | |
| 72 | + // filters | |
| 73 | + $this->filters = new PLL_Frontend_Filters($this); | |
| 74 | + $this->filters_search = new PLL_Frontend_Filters_Search($this); | |
| 97 | 75 | |
| 98 | - /** | |
| 99 | - * When querying multiple taxonomies, makes sure that the language is not the queried object | |
| 100 | - * | |
| 101 | - * @since 1.8 | |
| 102 | - * | |
| 103 | - * @param object $query WP_Query object | |
| 104 | - */ | |
| 105 | - public function parse_tax_query( $query ) { | |
| 106 | - $pll_query = new PLL_Query( $query, $this->model ); | |
| 107 | - $queried_taxonomies = $pll_query->get_queried_taxonomies(); | |
| 108 | - | |
| 109 | - if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) { | |
| 110 | - $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms ); | |
| 111 | - } | |
| 76 | + // nav menu | |
| 77 | + $this->nav_menu = new PLL_Frontend_Nav_Menu($this); | |
| 112 | 78 | } |
| 113 | 79 | |
| 114 | - /** | |
| 115 | - * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts | |
| 80 | + /* | |
| 81 | + * modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts | |
| 116 | 82 | * |
| 117 | 83 | * @since 1.2 |
| 118 | 84 | * |
| 119 | 85 | * @param object $query WP_Query object |
| 120 | 86 | */ |
| 121 | - public function parse_query( $query ) { | |
| 87 | + public function parse_query($query) { | |
| 122 | 88 | $qv = $query->query_vars; |
| 123 | - $pll_query = new PLL_Query( $query, $this->model ); | |
| 124 | - $taxonomies = $pll_query->get_queried_taxonomies(); | |
| 125 | 89 | |
| 126 | - // Allow filtering recent posts and secondary queries by the current language | |
| 127 | - if ( ! empty( $this->curlang ) ) { | |
| 128 | - $pll_query->filter_query( $this->curlang ); | |
| 90 | + // to avoid conflict beetwen taxonomies | |
| 91 | + // FIXME generalize post format like taxonomies (untranslated but filtered) | |
| 92 | + $has_tax = false; | |
| 93 | + if (isset($query->tax_query->queries)) | |
| 94 | + foreach ($query->tax_query->queries as $tax) | |
| 95 | + if ('post_format' != $tax['taxonomy']) | |
| 96 | + $has_tax = true; | |
| 97 | + | |
| 98 | + // allow filtering recent posts and secondary queries by the current language | |
| 99 | + // take care not to break queries for non visible post types such as nav_menu_items | |
| 100 | + // do not filter if lang is set to an empty value | |
| 101 | + // do not filter single page and translated taxonomies to avoid conflicts | |
| 102 | + if (!empty($this->curlang) && !isset($qv['lang']) && !$has_tax && empty($qv['page_id']) && empty($qv['pagename']) && (empty($qv['post_type']) || $this->model->is_translated_post_type($qv['post_type']))) { | |
| 103 | + $this->choose_lang->set_lang_query_var($query, $this->curlang); | |
| 129 | 104 | } |
| 130 | 105 | |
| 131 | - // Modifies query vars when the language is queried | |
| 132 | - if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) { | |
| 133 | - // Do we query a custom taxonomy? | |
| 134 | - $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) ); | |
| 106 | + // modifies query vars when the language is queried | |
| 107 | + if (!empty($qv['lang'])) { | |
| 108 | + // remove pages query when the language is set unless we do a search | |
| 109 | + if (empty($qv['post_type']) && !$query->is_search) | |
| 110 | + $query->set('post_type', 'post'); | |
| 135 | 111 | |
| 136 | - // Remove pages query when the language is set unless we do a search | |
| 137 | - // 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 ) { | |
| 139 | - $query->set( 'post_type', 'post' ); | |
| 140 | - } | |
| 141 | - | |
| 142 | - // Unset the is_archive flag for language pages to prevent loading the archive template | |
| 143 | - // Keep archive flag for comment feed otherwise the language filter does not work | |
| 144 | - if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) { | |
| 112 | + // unset the is_archive flag for language pages to prevent loading the archive template | |
| 113 | + // keep archive flag for comment feed otherwise the language filter does not work | |
| 114 | + if (!$query->is_comment_feed && !$query->is_post_type_archive && !$query->is_date && !$query->is_author && !$query->is_category && !$query->is_tag && !$query->is_tax('post_format')) | |
| 145 | 115 | $query->is_archive = false; |
| 146 | - } | |
| 147 | 116 | |
| 148 | - // Unset the is_tax flag except if another custom tax is queried | |
| 149 | - if ( empty( $taxonomies ) && ( $query->is_category || $query->is_tag || $query->is_author || $query->is_post_type_archive || $query->is_date || $query->is_search || $query->is_feed ) ) { | |
| 117 | + // unset the is_tax flag for authors pages and post types archives | |
| 118 | + // FIXME Should I do this for other cases? | |
| 119 | + if ($query->is_author || $query->is_post_type_archive || $query->is_date || $query->is_search) { | |
| 150 | 120 | $query->is_tax = false; |
| 151 | - unset( $query->queried_object ); // FIXME useless? | |
| 121 | + unset($query->queried_object); | |
| 152 | 122 | } |
| 153 | 123 | } |
| 154 | 124 | } |
| 155 | 125 | |
| 156 | - /** | |
| 157 | - * Auto translate posts and terms ids | |
| 126 | + /* | |
| 127 | + * auto translate posts and terms ids | |
| 158 | 128 | * |
| 159 | 129 | * @since 1.2 |
| 160 | 130 | */ |
| 161 | 131 | public function auto_translate() { |
| 162 | - $this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); | |
| 132 | + $this->auto_translate = new PLL_Frontend_Auto_Translate($this); | |
| 163 | 133 | } |
| 134 | +} | |
| 164 | 135 | |
| 165 | - /** | |
| 166 | - * Resets some variables when switching blog | |
| 167 | - * Overrides parent method | |
| 168 | - * | |
| 169 | - * @since 1.5.1 | |
| 170 | - * | |
| 171 | - * @param int $new_blog | |
| 172 | - * @param int $old_blog | |
| 173 | - */ | |
| 174 | - public function switch_blog( $new_blog, $old_blog ) { | |
| 175 | - // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages | |
| 176 | - if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { | |
| 177 | - static $restore_curlang; | |
| 178 | - if ( empty( $restore_curlang ) ) { | |
| 179 | - $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs | |
| 180 | - } | |
| 181 | - | |
| 182 | - $lang = $this->model->get_language( $restore_curlang ); | |
| 183 | - $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); | |
| 184 | - | |
| 185 | - if ( isset( $this->static_pages ) ) { | |
| 186 | - $this->static_pages->init(); | |
| 187 | - } | |
| 188 | - | |
| 189 | - $this->load_strings_translations(); | |
| 190 | - } | |
| 191 | - } | |
| 192 | -} | |