PLL_Frontend_Static_Pages

Manages the static front page and the page for posts on frontend.

Defined (1)

The class is defined in the following location(s).

/frontend/frontend-static-pages.php  
  1. class PLL_Frontend_Static_Pages extends PLL_Static_Pages { 
  2.  
  3. /** 
  4. * Constructor: setups filters and actions 
  5. * @since 1.8 
  6. * @param object $polylang 
  7. */ 
  8. public function __construct( &$polylang ) { 
  9. parent::__construct( $polylang ); 
  10.  
  11. $this->links_model = &$polylang->links_model; 
  12. $this->links = &$polylang->links; 
  13. $this->curlang = &$polylang->curlang; 
  14.  
  15. add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ) ); 
  16. add_action( 'pll_home_requested', array( $this, 'pll_home_requested' ) ); 
  17.  
  18. // Manages the redirection of the homepage 
  19. add_filter( 'redirect_canonical', array( $this, 'redirect_canonical' ), 10, 2 ); 
  20.  
  21. add_filter( 'pll_pre_translation_url', array( $this, 'pll_pre_translation_url' ), 10, 3 ); 
  22. add_filter( 'pll_check_canonical_url', array( $this, 'pll_check_canonical_url' ) ); 
  23.  
  24. add_filter( 'pll_set_language_from_query', array( $this, 'page_on_front_query' ), 10, 2 ); 
  25. add_filter( 'pll_set_language_from_query', array( $this, 'page_for_posts_query' ), 10, 2 ); 
  26.  
  27. /** 
  28. * Init the filters 
  29. * @since 1.8 
  30. */ 
  31. public function pll_language_defined() { 
  32. // Translates our page on front and page for posts properties 
  33. $this->init(); 
  34.  
  35. // Translates page for posts and page on front 
  36. add_filter( 'option_page_on_front', array( $this, 'translate_page_on_front' ) ); 
  37. add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) ); 
  38.  
  39. // Support theme customizer 
  40. if ( isset( $_POST['wp_customize'], $_POST['customized'] ) ) { 
  41. add_filter( 'pre_option_page_on_front', 'pll_get_post', 20 ); 
  42. add_filter( 'pre_option_page_for_post', 'pll_get_post', 20 ); 
  43.  
  44. /** 
  45. * Translates the page_id query var when the site root page is requested 
  46. * @since 1.8 
  47. */ 
  48. public function pll_home_requested() { 
  49. set_query_var( 'page_id', $this->curlang->page_on_front ); 
  50.  
  51. /** 
  52. * Translates page on front 
  53. * @since 1.8 
  54. * @param int $v page on front page id 
  55. * @return int 
  56. */ 
  57. public function translate_page_on_front( $v ) { 
  58. // returns the current page if there is no translation to avoid ugly notices 
  59. return isset( $this->curlang->page_on_front ) ? $this->curlang->page_on_front : $v; 
  60.  
  61. /** 
  62. * Translates page for posts 
  63. * @since 1.8 
  64. * @param int $v page for posts page id 
  65. * @return int 
  66. */ 
  67. public function translate_page_for_posts( $v ) { 
  68. // Returns the current page if there is no translation to avoid ugly notices 
  69. return isset( $this->curlang->page_for_posts ) ? $this->curlang->page_for_posts : $v; 
  70.  
  71. /** 
  72. * Manages canonical redirection of the homepage when using page on front 
  73. * @since 0.1 
  74. * @param string $redirect_url 
  75. * @param string $requested_url 
  76. * @return bool|string modified url, false if redirection is canceled 
  77. */ 
  78. public function redirect_canonical( $redirect_url, $requested_url ) { 
  79. global $wp_query; 
  80. if ( is_page() && ! is_feed() && isset( $wp_query->queried_object ) && $wp_query->queried_object->ID == $this->curlang->page_on_front ) { 
  81. $url = is_paged() ? $this->links_model->add_paged_to_link( $this->links->get_home_url(), $wp_query->query_vars['page'] ) : $this->links->get_home_url(); 
  82.  
  83. // Don't forget additional query vars 
  84. $query = parse_url( $redirect_url, PHP_URL_QUERY ); 
  85. if ( ! empty( $query ) ) { 
  86. parse_str( $query, $query_vars ); 
  87. $query_vars = rawurlencode_deep( $query_vars ); // WP encodes query vars values 
  88. $url = add_query_arg( $query_vars, $url ); 
  89.  
  90. return $url; 
  91.  
  92. return $redirect_url; 
  93.  
  94. /** 
  95. * Translates the url of the page on front and page for posts 
  96. * @since 1.8 
  97. * @param string $url not used 
  98. * @param object $language language in which we want the translation 
  99. * @param int $queried_object_id id of the queried object 
  100. * @return string 
  101. */ 
  102. public function pll_pre_translation_url( $url, $language, $queried_object_id ) { 
  103. if ( ! empty( $queried_object_id ) ) { 
  104. // Page for posts 
  105. if ( $GLOBALS['wp_query']->is_posts_page && ( $id = $this->model->post->get( $queried_object_id, $language ) ) ) { 
  106. $url = get_permalink( $id ); 
  107.  
  108. // Page on front 
  109. elseif ( is_front_page() && $language->page_on_front && ( $language->page_on_front == $this->model->post->get( $queried_object_id, $language ) ) ) { 
  110. $url = $language->home_url; 
  111.  
  112. return $url; 
  113.  
  114. /** 
  115. * Prevents canonical redirection if we are on a static front page 
  116. * @since 1.8 
  117. * @param string $redirect_url 
  118. * @return bool|string 
  119. */ 
  120. public function pll_check_canonical_url( $redirect_url ) { 
  121. return $this->options['redirect_lang'] && ! empty( $this->curlang->page_on_front ) && is_page( $this->curlang->page_on_front ) ? false : $redirect_url; 
  122.  
  123. /** 
  124. * Setups query vars when requesting a static front page 
  125. * @since 1.8 
  126. * @param bool|object $lang 
  127. * @param object $query 
  128. * @return bool|object 
  129. */ 
  130. public function page_on_front_query( $lang, $query ) { 
  131. if ( ! empty( $lang ) || ! $this->page_on_front ) { 
  132. return $lang; 
  133.  
  134. // The home page is requested 
  135. if ( did_action( 'home_requested' ) ) { 
  136. $query->set( 'page_id', $lang->page_on_front ); 
  137.  
  138. // Redirect the language page to the homepage when using a static front page 
  139. elseif ( ( $this->options['redirect_lang'] || $this->options['hide_default'] ) && ( count( $query->query ) == 1 || ( ( is_preview() || is_paged() || ! empty( $query->query['page'] ) ) && count( $query->query ) == 2 ) || ( ( is_preview() && ( is_paged() || ! empty( $query->query['page'] ) ) ) && count( $query->query ) == 3 ) ) && is_tax( 'language' ) ) { 
  140. $lang = $this->model->get_language( get_query_var( 'lang' ) ); 
  141. $query->set( 'page_id', $lang->page_on_front ); 
  142. $query->is_singular = $query->is_page = true; 
  143. $query->is_archive = $query->is_tax = false; 
  144. unset( $query->query_vars['lang'], $query->queried_object ); // Reset queried object 
  145.  
  146. // Fix paged static front page in plain permalinks when Settings > Reading doesn't match the default language 
  147. elseif ( ! $this->links_model->using_permalinks && count( $query->query ) === 1 && ! empty( $query->query['page'] ) ) { 
  148. $lang = $this->model->get_language( $this->options['default_lang'] ); 
  149. $query->set( 'page_id', $lang->page_on_front ); 
  150. $query->is_singular = $query->is_page = true; 
  151. $query->is_archive = $query->is_tax = false; 
  152. unset( $query->query_vars['lang'], $query->queried_object ); // Reset queried object 
  153.  
  154. // Set the language when requesting a static front page 
  155. else { 
  156. $page_id = $this->get_page_id( $query ); 
  157. $languages = $this->model->get_languages_list(); 
  158. $pages = wp_list_pluck( $languages, 'page_on_front' ); 
  159.  
  160. if ( ! empty( $page_id ) && false !== $n = array_search( $page_id, $pages ) ) { 
  161. $lang = $languages[ $n ]; 
  162.  
  163. // Fix <!--nextpage--> for page_on_front 
  164. if ( ( $this->options['force_lang'] < 2 || ! $this->options['redirect_lang'] ) && $this->links_model->using_permalinks && ! empty( $lang ) && isset( $query->query['paged'] ) ) { 
  165. $query->set( 'page', $query->query['paged'] ); 
  166. unset( $query->query['paged'] ); 
  167. } elseif ( ! $this->links_model->using_permalinks && ! empty( $query->query['page'] ) ) { 
  168. $query->is_paged = true; 
  169.  
  170. return $lang; 
  171.  
  172. /** 
  173. * Setups query vars when requesting a posts page 
  174. * @since 1.8 
  175. * @param bool|object $lang 
  176. * @param object $query 
  177. * @return bool|object 
  178. */ 
  179. public function page_for_posts_query( $lang, $query ) { 
  180. if ( empty( $lang ) && $this->page_for_posts ) { 
  181. $page_id = $this->get_page_id( $query ); 
  182.  
  183. if ( ! empty( $page_id ) && in_array( $page_id, $pages = $this->model->get_languages_list( array( 'fields' => 'page_for_posts' ) ) ) ) { 
  184. // Fill the cache with all pages for posts to avoid one query per page later 
  185. // The posts_per_page limit is a trick to avoid splitting the query 
  186. get_posts( array( 'posts_per_page' => 999, 'post_type' => 'page', 'post__in' => $pages, 'lang' => '' ) ); 
  187.  
  188. $lang = $this->model->post->get_language( $page_id ); 
  189. $query->is_singular = $query->is_page = false; 
  190. $query->is_home = $query->is_posts_page = true; 
  191. return $lang; 
  192.  
  193. /** 
  194. * Get queried page_id ( if exists ) 
  195. * If permalinks are used, WordPress does set and use $query->queried_object_id and sets $query->query_vars['page_id'] to 0 
  196. * and does set and use $query->query_vars['page_id'] if permalinks are not used :( 
  197. * @since 1.5 
  198. * @param object $query instance of WP_Query 
  199. * @return int page_id 
  200. */ 
  201. protected function get_page_id( $query ) { 
  202. if ( ! empty( $query->query_vars['pagename'] ) && isset( $query->queried_object_id ) ) { 
  203. return $query->queried_object_id; 
  204.  
  205. if ( isset( $query->query_vars['page_id'] ) ) { 
  206. return $query->query_vars['page_id']; 
  207.  
  208. return 0; // No page queried