| @@ -1,70 +1,39 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | - * Main Polylang class when on frontend, accessible from @see PLL(). | |
| 4 | + * Frontend controller | |
| 5 | + * accessible as $polylang global object | |
| 8 | 6 | * |
| 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 | + * static_pages => reference to PLL_Frontend_Static_Pages object | |
| 13 | + * choose_lang => reference to PLL_Choose_Lang object | |
| 14 | + * curlang => current language | |
| 15 | + * filters => reference to PLL_Frontend_Filters object | |
| 16 | + * filters_links => reference to PLL_Frontend_Filters_Links object | |
| 17 | + * filters_search => reference to PLL_Frontend_Filters_Search object | |
| 18 | + * posts => reference to PLL_CRUD_Posts object | |
| 19 | + * terms => reference to PLL_CRUD_Terms object | |
| 20 | + * nav_menu => reference to PLL_Frontend_Nav_Menu object | |
| 21 | + * sync => reference to PLL_Sync object | |
| 22 | + * auto_translate => optional, reference to PLL_Auto_Translate object | |
| 23 | + * | |
| 9 | 24 | * @since 1.2 |
| 10 | 25 | */ |
| 11 | 26 | class PLL_Frontend extends PLL_Base { |
| 12 | - /** | |
| 13 | - * Current language. | |
| 14 | - * | |
| 15 | - * @var PLL_Language | |
| 16 | - */ | |
| 17 | 27 | public $curlang; |
| 28 | + public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate; | |
| 18 | 29 | |
| 19 | 30 | /** |
| 20 | - * @var PLL_Frontend_Auto_Translate | |
| 21 | - */ | |
| 22 | - public $auto_translate; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * The class selecting the current language. | |
| 31 | + * Constructor | |
| 26 | 32 | * |
| 27 | - * @var PLL_Choose_Lang | |
| 28 | - */ | |
| 29 | - public $choose_lang; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * @var PLL_Frontend_Filters | |
| 33 | - */ | |
| 34 | - public $filters; | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * @var PLL_Frontend_Filters_Links | |
| 38 | - */ | |
| 39 | - public $filters_links; | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * @var PLL_Frontend_Filters_Search | |
| 43 | - */ | |
| 44 | - public $filters_search; | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * @var PLL_Frontend_Links | |
| 48 | - */ | |
| 49 | - public $links; | |
| 50 | - | |
| 51 | - /** | |
| 52 | - * @var PLL_Frontend_Nav_Menu | |
| 53 | - */ | |
| 54 | - public $nav_menu; | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * @var PLL_Frontend_Static_Pages | |
| 58 | - */ | |
| 59 | - public $static_pages; | |
| 60 | - | |
| 61 | - /** | |
| 62 | - * Constructor. | |
| 63 | - * | |
| 64 | 33 | * @since 1.2 |
| 65 | 34 | * |
| 66 | - * @param PLL_Links_Model $links_model Reference to the links model. | |
| 35 | + * @param object $links_model | |
| 67 | 36 | */ |
| 68 | 37 | public function __construct( &$links_model ) { |
| 69 | 38 | parent::__construct( $links_model ); |
| 70 | 39 | |
| @@ -104,8 +73,35 @@ | ||
| 104 | 73 | $this->choose_lang->init(); |
| 105 | 74 | |
| 106 | 75 | // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content |
| 107 | 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 ); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 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 | + } | |
| 92 | + | |
| 93 | + // Share term slugs | |
| 94 | + if ( $this->options['force_lang'] && class_exists( 'PLL_Share_Term_Slug' ) ) { | |
| 95 | + $this->share_term_slug = version_compare( $GLOBALS['wp_version'], '4.8', '<' ) ? | |
| 96 | + new PLL_Frontend_Share_Term_Slug( $this ) : | |
| 97 | + new PLL_Share_Term_Slug( $this ); | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + if ( class_exists( 'PLL_Sync_Post' ) ) { | |
| 102 | + $this->sync_post = new PLL_Sync_Post( $this ); | |
| 103 | + } | |
| 108 | 104 | } |
| 109 | 105 | |
| 110 | 106 | /** |
| 111 | 107 | * Setups filters and nav menus once the language has been defined |
| @@ -110,10 +106,8 @@ | ||
| 110 | 106 | /** |
| 111 | 107 | * Setups filters and nav menus once the language has been defined |
| 112 | 108 | * |
| 113 | 109 | * @since 1.2 |
| 114 | - * | |
| 115 | - * @return void | |
| 116 | 110 | */ |
| 117 | 111 | public function pll_language_defined() { |
| 118 | 112 | // Filters |
| 119 | 113 | $this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| @@ -118,9 +112,13 @@ | ||
| 118 | 112 | // Filters |
| 119 | 113 | $this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| 120 | 114 | $this->filters = new PLL_Frontend_Filters( $this ); |
| 121 | 115 | $this->filters_search = new PLL_Frontend_Filters_Search( $this ); |
| 116 | + $this->posts = new PLL_CRUD_Posts( $this ); | |
| 117 | + $this->terms = new PLL_CRUD_Terms( $this ); | |
| 122 | 118 | |
| 119 | + $this->sync = new PLL_Sync( $this ); | |
| 120 | + | |
| 123 | 121 | // Auto translate for Ajax |
| 124 | 122 | if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) { |
| 125 | 123 | $this->auto_translate(); |
| 126 | 124 | } |
| @@ -126,14 +124,13 @@ | ||
| 126 | 124 | } |
| 127 | 125 | } |
| 128 | 126 | |
| 129 | 127 | /** |
| 130 | - * When querying multiple taxonomies, makes sure that the language is not the queried object. | |
| 128 | + * When querying multiple taxonomies, makes sure that the language is not the queried object | |
| 131 | 129 | * |
| 132 | 130 | * @since 1.8 |
| 133 | 131 | * |
| 134 | - * @param WP_Query $query WP_Query object. | |
| 135 | - * @return void | |
| 132 | + * @param object $query WP_Query object | |
| 136 | 133 | */ |
| 137 | 134 | public function parse_tax_query( $query ) { |
| 138 | 135 | $pll_query = new PLL_Query( $query, $this->model ); |
| 139 | 136 | $queried_taxonomies = $pll_query->get_queried_taxonomies(); |
| @@ -143,14 +140,13 @@ | ||
| 143 | 140 | } |
| 144 | 141 | } |
| 145 | 142 | |
| 146 | 143 | /** |
| 147 | - * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts. | |
| 144 | + * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts | |
| 148 | 145 | * |
| 149 | 146 | * @since 1.2 |
| 150 | 147 | * |
| 151 | - * @param WP_Query $query WP_Query object. | |
| 152 | - * @return void | |
| 148 | + * @param object $query WP_Query object | |
| 153 | 149 | */ |
| 154 | 150 | public function parse_query( $query ) { |
| 155 | 151 | $qv = $query->query_vars; |
| 156 | 152 | $pll_query = new PLL_Query( $query, $this->model ); |
| @@ -167,9 +163,9 @@ | ||
| 167 | 163 | $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) ); |
| 168 | 164 | |
| 169 | 165 | // Remove pages query when the language is set unless we do a search |
| 170 | 166 | // Take care not to break the single page, attachment and taxonomies queries! |
| 171 | - if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) { | |
| 167 | + if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) ) { | |
| 172 | 168 | $query->set( 'post_type', 'post' ); |
| 173 | 169 | } |
| 174 | 170 | |
| 175 | 171 | // Unset the is_archive flag for language pages to prevent loading the archive template |
| @@ -189,10 +185,8 @@ | ||
| 189 | 185 | /** |
| 190 | 186 | * Auto translate posts and terms ids |
| 191 | 187 | * |
| 192 | 188 | * @since 1.2 |
| 193 | - * | |
| 194 | - * @return void | |
| 195 | 189 | */ |
| 196 | 190 | public function auto_translate() { |
| 197 | 191 | $this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); |
| 198 | 192 | } |
| @@ -197,25 +191,22 @@ | ||
| 197 | 191 | $this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); |
| 198 | 192 | } |
| 199 | 193 | |
| 200 | 194 | /** |
| 201 | - * Resets some variables when the blog is switched. | |
| 202 | - * Overrides the parent method. | |
| 195 | + * Resets some variables when switching blog | |
| 196 | + * Overrides parent method | |
| 203 | 197 | * |
| 204 | 198 | * @since 1.5.1 |
| 205 | 199 | * |
| 206 | - * @param int $new_blog_id New blog ID. | |
| 207 | - * @param int $prev_blog_id Previous blog ID. | |
| 208 | - * @return void | |
| 200 | + * @param int $new_blog | |
| 201 | + * @param int $old_blog | |
| 209 | 202 | */ |
| 210 | - public function switch_blog( $new_blog_id, $prev_blog_id ) { | |
| 211 | - parent::switch_blog( $new_blog_id, $prev_blog_id ); | |
| 212 | - | |
| 213 | - // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages. | |
| 214 | - if ( $this->is_active_on_new_blog( $new_blog_id, $prev_blog_id ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { | |
| 203 | + public function switch_blog( $new_blog, $old_blog ) { | |
| 204 | + // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages | |
| 205 | + if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { | |
| 215 | 206 | static $restore_curlang; |
| 216 | 207 | if ( empty( $restore_curlang ) ) { |
| 217 | - $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs. | |
| 208 | + $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs | |
| 218 | 209 | } |
| 219 | 210 | |
| 220 | 211 | $lang = $this->model->get_language( $restore_curlang ); |
| 221 | 212 | $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); |