PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / frontend / frontend-filters.php

frontend-filters.php in Polylang trunk, at src/frontend/frontend-filters.php

217 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Filters content by language on frontend
8 *
9 * @since 1.2
10 */
11 class PLL_Frontend_Filters extends PLL_Filters {
12 /**
13 * Constructor: setups filters and actions
14 *
15 * @since 1.2
16 *
17 * @param PLL_Frontend $polylang The Polylang object.
18 */
19 public function __construct( &$polylang ) {
20 parent::__construct( $polylang );
21
22 // Filters the WordPress locale
23 add_filter( 'locale', array( $this, 'get_locale' ) );
24
25 // Filter sticky posts by current language
26 add_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) );
27
28 // Rewrites archives links to filter them by language
29 add_filter( 'getarchives_join', array( $this, 'getarchives_join' ), 10, 2 );
30 add_filter( 'getarchives_where', array( $this, 'getarchives_where' ), 10, 2 );
31
32 // Filters the widgets according to the current language
33 add_filter( 'widget_display_callback', array( $this, 'widget_display_callback' ) );
34
35 if ( $this->options['media_support'] ) {
36 add_filter( 'widget_media_image_instance', array( $this, 'widget_media_instance' ), 1 ); // Since WP 4.8
37 }
38
39 // Strings translation ( must be applied before WordPress applies its default formatting filters )
40 foreach ( array( 'widget_text', 'widget_title' ) as $filter ) {
41 add_filter( $filter, 'pll__', 1 );
42 }
43
44 // Translates biography
45 add_filter( 'get_user_metadata', array( $this, 'get_user_metadata' ), 10, 4 );
46
47 if ( Polylang::is_ajax_on_front() ) {
48 add_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ) );
49 }
50 }
51
52 /**
53 * Returns the locale based on current language
54 *
55 * @since 0.1
56 *
57 * @return string
58 */
59 public function get_locale() {
60 return $this->curlang->locale;
61 }
62
63 /**
64 * Filters sticky posts by current language.
65 *
66 * @since 0.8
67 *
68 * @param int[] $posts List of sticky posts ids.
69 * @return int[] Modified list of sticky posts ids.
70 */
71 public function option_sticky_posts( $posts ) {
72 global $wpdb;
73
74 // Do not filter sticky posts on REST requests as $this->curlang is *not* the 'lang' parameter set in the request.
75 if ( defined( 'REST_REQUEST' ) || empty( $this->curlang ) || empty( $posts ) ) {
76 return $posts;
77 }
78
79 $_posts = wp_cache_get( 'sticky_posts', 'options' ); // This option is usually cached in 'all_options' by WP.
80 $tt_id = $this->curlang->get_tax_prop( 'language', 'term_taxonomy_id' );
81
82 if ( ! empty( $_posts ) && is_array( $_posts ) && ! empty( $_posts[ $tt_id ] ) && is_array( $_posts[ $tt_id ] ) ) {
83 return $_posts[ $tt_id ];
84 }
85
86 $languages = array();
87 foreach ( $this->model->get_languages_list() as $language ) {
88 $languages[] = $language->get_tax_prop( 'language', 'term_taxonomy_id' );
89 }
90
91 $relations = $wpdb->get_results(
92 $wpdb->prepare(
93 sprintf(
94 "SELECT object_id, term_taxonomy_id FROM {$wpdb->term_relationships} WHERE object_id IN (%s) AND term_taxonomy_id IN (%s)",
95 implode( ',', array_fill( 0, count( $posts ), '%d' ) ),
96 implode( ',', array_fill( 0, count( $languages ), '%d' ) )
97 ),
98 array_merge( $posts, $languages )
99 )
100 );
101
102 $_posts = array_fill_keys( $languages, array() ); // Init with empty arrays.
103
104 foreach ( $relations as $relation ) {
105 $_posts[ $relation->term_taxonomy_id ][] = (int) $relation->object_id;
106 }
107
108 wp_cache_add( 'sticky_posts', $_posts, 'options' );
109 return $_posts[ $tt_id ];
110 }
111
112 /**
113 * Modifies the sql request for wp_get_archives to filter by the current language
114 *
115 * @since 1.9
116 *
117 * @param string $sql JOIN clause
118 * @param array $r wp_get_archives arguments
119 * @return string modified JOIN clause
120 */
121 public function getarchives_join( $sql, $r ) {
122 return ! empty( $r['post_type'] ) && $this->model->is_translated_post_type( $r['post_type'] ) ? $sql . $this->model->post->join_clause() : $sql;
123 }
124
125 /**
126 * Modifies the sql request for wp_get_archives to filter by the current language
127 *
128 * @since 1.9
129 *
130 * @param string $sql WHERE clause
131 * @param array $r wp_get_archives arguments
132 * @return string modified WHERE clause
133 */
134 public function getarchives_where( $sql, $r ) {
135 if ( ! $this->curlang instanceof PLL_Language ) {
136 return $sql;
137 }
138
139 if ( empty( $r['post_type'] ) || ! $this->model->is_translated_post_type( $r['post_type'] ) ) {
140 return $sql;
141 }
142
143 return $sql . $this->model->post->where_clause( $this->curlang );
144 }
145
146 /**
147 * Filters the widgets according to the current language
148 * Don't display if a language filter is set and this is not the current one
149 * Needed for {@see https://developer.wordpress.org/reference/functions/the_widget/ the_widget()}.
150 *
151 * @since 0.3
152 *
153 * @param array $instance Widget settings
154 * @return bool|array false if we hide the widget, unmodified $instance otherwise
155 */
156 public function widget_display_callback( $instance ) {
157 return ! empty( $instance['pll_lang'] ) && $instance['pll_lang'] != $this->curlang->slug ? false : $instance;
158 }
159
160 /**
161 * Translates media in media widgets
162 *
163 * @since 2.1.5
164 *
165 * @param array $instance Widget instance data
166 * @return array
167 */
168 public function widget_media_instance( $instance ) {
169 if ( empty( $instance['pll_lang'] ) && $instance['attachment_id'] && $tr_id = pll_get_post( $instance['attachment_id'] ) ) {
170 $instance['attachment_id'] = $tr_id;
171 $attachment = get_post( $tr_id );
172
173 if ( $instance['caption'] && ! empty( $attachment->post_excerpt ) ) {
174 $instance['caption'] = $attachment->post_excerpt;
175 }
176
177 if ( $instance['alt'] && $alt_text = get_post_meta( $tr_id, '_wp_attachment_image_alt', true ) ) {
178 $instance['alt'] = $alt_text;
179 }
180
181 if ( $instance['image_title'] && ! empty( $attachment->post_title ) ) {
182 $instance['image_title'] = $attachment->post_title;
183 }
184 }
185 return $instance;
186 }
187
188 /**
189 * Translates the biography.
190 *
191 * @since 0.9
192 *
193 * @param null $null Expecting the default null value.
194 * @param int $id The user id.
195 * @param string $meta_key The metadata key.
196 * @param bool $single Whether to return only the first value of the specified $meta_key.
197 * @return string|null
198 */
199 public function get_user_metadata( $null, $id, $meta_key, $single ) {
200 return 'description' === $meta_key && ! empty( $this->curlang ) && ! $this->curlang->is_default ? get_user_meta( $id, 'description_' . $this->curlang->slug, $single ) : $null;
201 }
202
203 /**
204 * Filters the translation files to load when doing ajax on front
205 * This is needed because WP the language files associated to the user locale when a user is logged in
206 *
207 * @since 2.2.6
208 *
209 * @param string $mofile Translation file name
210 * @return string
211 */
212 public function load_textdomain_mofile( $mofile ) {
213 $user_locale = get_user_locale();
214 return str_replace( "{$user_locale}.mo", "{$this->curlang->locale}.mo", $mofile );
215 }
216 }
217