PLL_Frontend_Filters

Filters content by language on frontend.

Defined (1)

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

/frontend/frontend-filters.php  
  1. class PLL_Frontend_Filters extends PLL_Filters{ 
  2.  
  3. /** 
  4. * Constructor: setups filters and actions 
  5. * @since 1.2 
  6. * @param object $polylang 
  7. */ 
  8. public function __construct( &$polylang ) { 
  9. parent::__construct( $polylang ); 
  10.  
  11. // Filters the WordPress locale 
  12. add_filter( 'locale', array( $this, 'get_locale' ) ); 
  13.  
  14. // Filter sticky posts by current language 
  15. add_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) ); 
  16.  
  17. // Adds cache domain when querying terms 
  18. add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) ); 
  19.  
  20. // Filters categories and post tags by language 
  21. add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 3 ); 
  22.  
  23. // Rewrites archives, next and previous post links to filter them by language 
  24. add_filter( 'getarchives_join', array( $this, 'getarchives_join' ), 10, 2 ); 
  25. add_filter( 'getarchives_where', array( $this, 'getarchives_where' ), 10, 2 ); 
  26. add_filter( 'get_previous_post_join', array( $this, 'posts_join' ), 10, 5 ); 
  27. add_filter( 'get_next_post_join', array( $this, 'posts_join' ), 10, 5 ); 
  28. add_filter( 'get_previous_post_where', array( $this, 'posts_where' ), 10, 5 ); 
  29. add_filter( 'get_next_post_where', array( $this, 'posts_where' ), 10, 5 ); 
  30.  
  31. // Filters the widgets according to the current language 
  32. add_filter( 'widget_display_callback', array( $this, 'widget_display_callback' ), 10, 2 ); 
  33. add_filter( 'sidebars_widgets', array( $this, 'sidebars_widgets' ) ); 
  34.  
  35. // Strings translation ( must be applied before WordPress applies its default formatting filters ) 
  36. foreach ( array( 'widget_text', 'widget_title', 'option_blogname', 'option_blogdescription', 'option_date_format', 'option_time_format' ) as $filter ) { 
  37. add_filter( $filter, 'pll__', 1 ); 
  38.  
  39. // Translates biography 
  40. add_filter( 'get_user_metadata', array( $this, 'get_user_metadata' ), 10, 4 ); 
  41.  
  42. // Set posts and terms language when created from frontend ( ex with P2 theme ) 
  43. add_action( 'save_post', array( $this, 'save_post' ), 200, 2 ); 
  44. add_action( 'create_term', array( $this, 'save_term' ), 10, 3 ); 
  45. add_action( 'edit_term', array( $this, 'save_term' ), 10, 3 ); 
  46.  
  47. if ( $this->options['media_support'] ) { 
  48. add_action( 'add_attachment', array( $this, 'set_default_language' ) ); 
  49.  
  50. // Support theme customizer 
  51. // FIXME of course does not work if 'transport' is set to 'postMessage' 
  52. if ( isset( $_POST['wp_customize'], $_POST['customized'] ) ) { 
  53. add_filter( 'pre_option_blogname', 'pll__', 20 ); 
  54. add_filter( 'pre_option_blogdescription', 'pll__', 20 ); 
  55.  
  56. /** 
  57. * Returns the locale based on current language 
  58. * @since 0.1 
  59. * @param string $locale 
  60. * @return string 
  61. */ 
  62. public function get_locale( $locale ) { 
  63. return $this->curlang->locale; 
  64.  
  65. /** 
  66. * Filters sticky posts by current language 
  67. * @since 0.8 
  68. * @param array $posts list of sticky posts ids 
  69. * @return array modified list of sticky posts ids 
  70. */ 
  71. public function option_sticky_posts( $posts ) { 
  72. if ( $this->curlang && ! empty( $posts ) ) { 
  73. update_object_term_cache( $posts, 'post' ); // to avoid queries in foreach 
  74. foreach ( $posts as $key => $post_id ) { 
  75. $lang = $this->model->post->get_language( $post_id ); 
  76. if ( empty( $lang ) || $lang->term_id != $this->curlang->term_id ) { 
  77. unset( $posts[ $key ] ); 
  78. return $posts; 
  79.  
  80. /** 
  81. * Adds language dependent cache domain when querying terms 
  82. * useful as the 'lang' parameter is not included in cache key by WordPress 
  83. * @since 1.3 
  84. * @param array $args 
  85. * @return array 
  86. */ 
  87. public function get_terms_args( $args ) { 
  88. $lang = isset( $args['lang'] ) ? $args['lang'] : $this->curlang->slug; 
  89. $key = '_' . ( is_array( $lang ) ? implode( ', ', $lang ) : $lang ); 
  90. $args['cache_domain'] = empty( $args['cache_domain'] ) ? 'pll' . $key : $args['cache_domain'] . $key; 
  91. return $args; 
  92.  
  93. /** 
  94. * Filters categories and post tags by language when needed 
  95. * @since 0.2 
  96. * @param array $clauses sql clauses 
  97. * @param array $taxonomies 
  98. * @param array $args get_terms arguments 
  99. * @return array modified sql clauses 
  100. */ 
  101. public function terms_clauses( $clauses, $taxonomies, $args ) { 
  102. // Does nothing except on taxonomies which are filterable 
  103. // Since WP 4.7, make sure not to filter wp_get_object_terms() 
  104. if ( ! $this->model->is_translated_taxonomy( $taxonomies ) || ! empty( $args['object_ids'] ) ) { 
  105. return $clauses; 
  106.  
  107. // Adds our clauses to filter by language 
  108. return $this->model->terms_clauses( $clauses, isset( $args['lang'] ) ? $args['lang'] : $this->curlang ); 
  109.  
  110. /** 
  111. * Modifies the sql request for wp_get_archives to filter by the current language 
  112. * @since 1.9 
  113. * @param string $sql JOIN clause 
  114. * @param array $r wp_get_archives arguments 
  115. * @return string modified JOIN clause 
  116. */ 
  117. public function getarchives_join( $sql, $r ) { 
  118. return ! empty( $r['post_type'] ) && $this->model->is_translated_post_type( $r['post_type'] ) ? $sql . $this->model->post->join_clause() : $sql; 
  119.  
  120. /** 
  121. * Modifies the sql request for wp_get_archives to filter by the current language 
  122. * @since 1.9 
  123. * @param string $sql WHERE clause 
  124. * @param array $r wp_get_archives arguments 
  125. * @return string modified WHERE clause 
  126. */ 
  127. public function getarchives_where( $sql, $r ) { 
  128. return ! empty( $r['post_type'] ) && $this->model->is_translated_post_type( $r['post_type'] ) ? $sql . $this->model->post->where_clause( $this->curlang ) : $sql; 
  129.  
  130. /** 
  131. * Modifies the sql request for get_adjacent_post to filter by the current language 
  132. * @since 0.1 
  133. * @param string $sql The JOIN clause in the SQL. 
  134. * @param bool $in_same_term Whether post should be in a same taxonomy term. 
  135. * @param array $excluded_terms Array of excluded term IDs. 
  136. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 
  137. * @param WP_Post $post WP_Post object. 
  138. * @return string modified JOIN clause 
  139. */ 
  140. public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) { 
  141. return $this->model->is_translated_post_type( $post->post_type ) ? $sql . $this->model->post->join_clause( 'p' ) : $sql; 
  142.  
  143. /** 
  144. * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language 
  145. * @since 0.1 
  146. * @param string $sql The WHERE clause in the SQL. 
  147. * @param bool $in_same_term Whether post should be in a same taxonomy term. 
  148. * @param array $excluded_terms Array of excluded term IDs. 
  149. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 
  150. * @param WP_Post $post WP_Post object. 
  151. * @return string modified WHERE clause 
  152. */ 
  153. public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) { 
  154. return $this->model->is_translated_post_type( $post->post_type ) ? $sql . $this->model->post->where_clause( $this->curlang ) : $sql; 
  155.  
  156. /** 
  157. * Filters the widgets according to the current language 
  158. * Don't display if a language filter is set and this is not the current one 
  159. * @since 0.3 
  160. * @param array $instance widget settings 
  161. * @param object $widget WP_Widget object 
  162. * @return bool|array false if we hide the widget, unmodified $instance otherwise 
  163. */ 
  164. public function widget_display_callback( $instance, $widget ) { 
  165. return ! empty( $instance['pll_lang'] ) && $instance['pll_lang'] != $this->curlang->slug ? false : $instance; 
  166.  
  167. /** 
  168. * Remove widgets from sidebars if they are not visible in the current language 
  169. * Needed to allow is_active_sidebar() to return false if all widgets are not for the current language. See #54 
  170. * @since 2.1 
  171. * @param array $sidebars_widgets An associative array of sidebars and their widgets 
  172. * @return array 
  173. */ 
  174. public function sidebars_widgets( $sidebars_widgets ) { 
  175. global $wp_registered_widgets; 
  176.  
  177. foreach ( $sidebars_widgets as $sidebar => $widgets ) { 
  178. if ( 'wp_inactive_widgets' == $sidebar || empty( $widgets ) ) { 
  179. continue; 
  180.  
  181. foreach ( $widgets as $key => $widget ) { 
  182. // Nothing can be done if the widget is created using pre WP2.8 API :( 
  183. // There is no object, so we can't access it to get the widget options 
  184. if ( ! isset( $wp_registered_widgets[ $widget ]['callback'][0] ) || ! is_object( $wp_registered_widgets[ $widget ]['callback'][0] ) || ! method_exists( $wp_registered_widgets[ $widget ]['callback'][0], 'get_settings' ) ) { 
  185. continue; 
  186.  
  187. $widget_settings = $wp_registered_widgets[ $widget ]['callback'][0]->get_settings(); 
  188. $number = $wp_registered_widgets[ $widget ]['params'][0]['number']; 
  189.  
  190. // Remove the widget if not visible in the current language 
  191. if ( ! empty( $widget_settings[ $number ]['pll_lang'] ) && $widget_settings[ $number ]['pll_lang'] !== $this->curlang->slug ) { 
  192. unset( $sidebars_widgets[ $sidebar ][ $key ] ); 
  193.  
  194. return $sidebars_widgets; 
  195.  
  196. /** 
  197. * Translates biography 
  198. * @since 0.9 
  199. * @param null $null 
  200. * @param int $id User id 
  201. * @param string $meta_key 
  202. * @param bool $single Whether to return only the first value of the specified $meta_key 
  203. * @return null|string 
  204. */ 
  205. public function get_user_metadata( $null, $id, $meta_key, $single ) { 
  206. return 'description' === $meta_key && $this->curlang->slug !== $this->options['default_lang'] ? get_user_meta( $id, 'description_'.$this->curlang->slug, $single ) : $null; 
  207.  
  208. /** 
  209. * Allows to set a language by default for posts if it has no language yet 
  210. * @since 1.5.4 
  211. * @param int $post_id 
  212. */ 
  213. public function set_default_language( $post_id ) { 
  214. if ( ! $this->model->post->get_language( $post_id ) ) { 
  215. if ( isset( $_REQUEST['lang'] ) ) { 
  216. $this->model->post->set_language( $post_id, $_REQUEST['lang'] ); 
  217. } elseif ( ( $parent_id = wp_get_post_parent_id( $post_id ) ) && $parent_lang = $this->model->post->get_language( $parent_id ) ) { 
  218. $this->model->post->set_language( $post_id, $parent_lang ); 
  219. } else { 
  220. $this->model->post->set_language( $post_id, $this->curlang ); 
  221.  
  222. /** 
  223. * Called when a post ( or page ) is saved, published or updated 
  224. * Does nothing except on post types which are filterable 
  225. * Sets the language but does not allow to modify it 
  226. * @since 1.1 
  227. * @param int $post_id 
  228. * @param object $post 
  229. * @param bool $update Whether it is an update or not 
  230. */ 
  231. public function save_post( $post_id, $post ) { 
  232. if ( $this->model->is_translated_post_type( $post->post_type ) ) { 
  233. $this->set_default_language( $post_id ); 
  234.  
  235. /** 
  236. * Called when a category or post tag is created or edited 
  237. * Does nothing except on taxonomies which are filterable 
  238. * Sets the language but does not allow to modify it 
  239. * @since 1.1 
  240. * @param int $term_id 
  241. * @param int $tt_id Term taxonomy id 
  242. * @param string $taxonomy 
  243. */ 
  244. public function save_term( $term_id, $tt_id, $taxonomy ) { 
  245. if ( $this->model->is_translated_taxonomy( $taxonomy ) && ! $this->model->term->get_language( $term_id ) ) { 
  246. if ( isset( $_REQUEST['lang'] ) ) { 
  247. $this->model->term->set_language( $term_id, $_REQUEST['lang'] ); 
  248. } elseif ( ( $term = get_term( $term_id, $taxonomy ) ) && ! empty( $term->parent ) && $parent_lang = $this->model->term->get_language( $term->parent ) ) { 
  249. $this->model->term->set_language( $term_id, $parent_lang ); 
  250. } else { 
  251. $this->model->term->set_language( $term_id, $this->curlang );