| @@ -8,11 +8,35 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 1.4 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Filters { |
| 12 | - public $links_model, $model, $options, $curlang; | |
| 12 | + /** | |
| 13 | + * Stores the plugin options. | |
| 14 | + * | |
| 15 | + * @var array | |
| 16 | + */ | |
| 17 | + public $options; | |
| 13 | 18 | |
| 14 | 19 | /** |
| 20 | + * @var PLL_Model | |
| 21 | + */ | |
| 22 | + public $model; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Instance of a child class of PLL_Links_Model. | |
| 26 | + * | |
| 27 | + * @var PLL_Links_Model | |
| 28 | + */ | |
| 29 | + public $links_model; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Current language. | |
| 33 | + * | |
| 34 | + * @var PLL_Language | |
| 35 | + */ | |
| 36 | + public $curlang; | |
| 37 | + | |
| 38 | + /** | |
| 15 | 39 | * Constructor: setups filters |
| 16 | 40 | * |
| 17 | 41 | * @since 1.4 |
| 18 | 42 | * |
| @@ -44,11 +68,8 @@ | ||
| 44 | 68 | |
| 45 | 69 | // Converts the locale to a valid W3C locale |
| 46 | 70 | add_filter( 'language_attributes', array( $this, 'language_attributes' ) ); |
| 47 | 71 | |
| 48 | - // Prevents deleting all the translations of the default category | |
| 49 | - add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_category' ), 10, 4 ); | |
| 50 | - | |
| 51 | 72 | // Translate the site title in emails sent to users |
| 52 | 73 | add_filter( 'password_change_email', array( $this, 'translate_user_email' ) ); |
| 53 | 74 | add_filter( 'email_change_email', array( $this, 'translate_user_email' ) ); |
| 54 | 75 | |
| @@ -63,8 +84,10 @@ | ||
| 63 | 84 | /** |
| 64 | 85 | * Deletes the cache for multilingual sticky posts. |
| 65 | 86 | * |
| 66 | 87 | * @since 2.8.4 |
| 88 | + * | |
| 89 | + * @return void | |
| 67 | 90 | */ |
| 68 | 91 | public function delete_sticky_posts_cache() { |
| 69 | 92 | wp_cache_delete( 'sticky_posts', 'options' ); |
| 70 | 93 | } |
| @@ -69,64 +92,83 @@ | ||
| 69 | 92 | wp_cache_delete( 'sticky_posts', 'options' ); |
| 70 | 93 | } |
| 71 | 94 | |
| 72 | 95 | /** |
| 73 | - * Get the language to filter a comments query | |
| 96 | + * Get the language to filter a comments query. | |
| 74 | 97 | * |
| 75 | 98 | * @since 2.0 |
| 99 | + * @since 3.1 Always returns an array. Renamed from get_comments_queried_language(). | |
| 76 | 100 | * |
| 77 | - * @param object $query | |
| 78 | - * @return object|bool the language(s) to use in the filter, false otherwise | |
| 101 | + * @param WP_Comment_Query $query WP_Comment_Query object. | |
| 102 | + * @return PLL_Language[] The languages to use in the filter. | |
| 79 | 103 | */ |
| 80 | - protected function get_comments_queried_language( $query ) { | |
| 81 | - // Don't filter comments if comment ids or post ids are specified | |
| 104 | + protected function get_comments_queried_languages( $query ) { | |
| 105 | + // Don't filter comments if comment ids or post ids are specified. | |
| 82 | 106 | $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) ); |
| 83 | 107 | $fields = array_filter( $plucked ); |
| 84 | 108 | if ( ! empty( $fields ) ) { |
| 85 | - return false; | |
| 109 | + return array(); | |
| 86 | 110 | } |
| 87 | 111 | |
| 88 | - // Don't filter comments if a non translated post type is specified | |
| 112 | + // Don't filter comments if a non translated post type is specified. | |
| 89 | 113 | if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) { |
| 90 | - return false; | |
| 114 | + return array(); | |
| 91 | 115 | } |
| 92 | 116 | |
| 93 | - return empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] ); | |
| 117 | + // If comments are queried with a 'lang' parameter, keeps only language codes. | |
| 118 | + if ( isset( $query->query_vars['lang'] ) ) { | |
| 119 | + $languages = is_string( $query->query_vars['lang'] ) ? explode( ',', $query->query_vars['lang'] ) : $query->query_vars['lang']; | |
| 120 | + if ( is_array( $languages ) ) { | |
| 121 | + $languages = array_map( array( $this->model, 'get_language' ), $languages ); | |
| 122 | + return array_filter( $languages ); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + if ( ! empty( $this->curlang ) ) { | |
| 127 | + return array( $this->curlang ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + return array(); | |
| 94 | 131 | } |
| 95 | 132 | |
| 96 | 133 | /** |
| 97 | - * Adds language dependent cache domain when querying comments | |
| 98 | - * Useful as the 'lang' parameter is not included in cache key by WordPress | |
| 99 | - * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419 | |
| 134 | + * Adds a language dependent cache domain when querying comments. | |
| 135 | + * Useful as the 'lang' parameter is not included in cache key by WordPress. | |
| 136 | + * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419. | |
| 100 | 137 | * |
| 101 | 138 | * @since 2.0 |
| 102 | 139 | * |
| 103 | - * @param object $query | |
| 140 | + * @param WP_Comment_Query $query WP_Comment_Query object. | |
| 141 | + * @return void | |
| 104 | 142 | */ |
| 105 | 143 | public function parse_comment_query( $query ) { |
| 106 | - if ( $lang = $this->get_comments_queried_language( $query ) ) { | |
| 107 | - $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug ); | |
| 144 | + $lang = $this->get_comments_queried_languages( $query ); | |
| 145 | + if ( ! empty( $lang ) ) { | |
| 146 | + $lang = wp_list_pluck( $lang, 'slug' ); | |
| 147 | + $key = '_' . implode( ',', $lang ); | |
| 108 | 148 | $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key; |
| 109 | 149 | } |
| 110 | 150 | } |
| 111 | 151 | |
| 112 | 152 | /** |
| 113 | - * Filters the comments according to the current language | |
| 114 | - * Used by the recent comments widget and admin language filter | |
| 153 | + * Filters the comments according to the current language. | |
| 154 | + * Used by the recent comments widget and admin language filter. | |
| 115 | 155 | * |
| 116 | 156 | * @since 0.2 |
| 117 | 157 | * |
| 118 | - * @param array $clauses sql clauses | |
| 119 | - * @param object $query WP_Comment_Query object | |
| 120 | - * @return array modified $clauses | |
| 158 | + * @param string[] $clauses SQL clauses. | |
| 159 | + * @param WP_Comment_Query $query WP_Comment_Query object. | |
| 160 | + * @return string[] Modified $clauses. | |
| 121 | 161 | */ |
| 122 | 162 | public function comments_clauses( $clauses, $query ) { |
| 123 | 163 | global $wpdb; |
| 124 | 164 | |
| 125 | - $lang = $this->get_comments_queried_language( $query ); | |
| 165 | + $lang = $this->get_comments_queried_languages( $query ); | |
| 126 | 166 | |
| 127 | 167 | if ( ! empty( $lang ) ) { |
| 128 | - // If this clause is not already added by WP | |
| 168 | + $lang = wp_list_pluck( $lang, 'slug' ); | |
| 169 | + | |
| 170 | + // If this clause is not already added by WP. | |
| 129 | 171 | if ( ! strpos( $clauses['join'], '.ID' ) ) { |
| 130 | 172 | $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; |
| 131 | 173 | } |
| 132 | 174 | |
| @@ -136,15 +178,15 @@ | ||
| 136 | 178 | return $clauses; |
| 137 | 179 | } |
| 138 | 180 | |
| 139 | 181 | /** |
| 140 | - * Filters get_pages per language | |
| 182 | + * Filters get_pages() per language. | |
| 141 | 183 | * |
| 142 | 184 | * @since 1.4 |
| 143 | 185 | * |
| 144 | - * @param array $pages an array of pages already queried | |
| 145 | - * @param array $args get_pages arguments | |
| 146 | - * @return array modified list of pages | |
| 186 | + * @param WP_Post[] $pages An array of pages already queried. | |
| 187 | + * @param array $args Array of get_pages() arguments. | |
| 188 | + * @return WP_Post[] Modified list of pages. | |
| 147 | 189 | */ |
| 148 | 190 | public function get_pages( $pages, $args ) { |
| 149 | 191 | if ( isset( $args['lang'] ) && empty( $args['lang'] ) ) { |
| 150 | 192 | return $pages; |
| @@ -177,10 +219,10 @@ | ||
| 177 | 219 | ), |
| 178 | 220 | ), |
| 179 | 221 | ); |
| 180 | 222 | |
| 181 | - // Take care that 'exclude' argument accepts integer or strings too | |
| 182 | - $args['exclude'] = array_merge( wp_parse_id_list( $args['exclude'] ), get_posts( $r ) ); | |
| 223 | + // Take care that 'exclude' argument accepts integer or strings too. | |
| 224 | + $args['exclude'] = array_merge( wp_parse_id_list( $args['exclude'] ), get_posts( $r ) ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude | |
| 183 | 225 | $pages = get_pages( $args ); |
| 184 | 226 | } |
| 185 | 227 | |
| 186 | 228 | $ids = wp_list_pluck( $pages, 'ID' ); |
| @@ -205,36 +247,36 @@ | ||
| 205 | 247 | return $pages; |
| 206 | 248 | } |
| 207 | 249 | |
| 208 | 250 | /** |
| 209 | - * Modifies the sql request for get_adjacent_post to filter by the current language | |
| 251 | + * Modifies the sql request for get_adjacent_post to filter by the current language. | |
| 210 | 252 | * |
| 211 | 253 | * @since 0.1 |
| 212 | 254 | * |
| 213 | 255 | * @param string $sql The JOIN clause in the SQL. |
| 214 | 256 | * @param bool $in_same_term Whether post should be in a same taxonomy term. |
| 215 | - * @param array $excluded_terms Array of excluded term IDs. | |
| 257 | + * @param int[] $excluded_terms Array of excluded term IDs. | |
| 216 | 258 | * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. |
| 217 | 259 | * @param WP_Post $post WP_Post object. |
| 218 | - * @return string modified JOIN clause | |
| 260 | + * @return string Modified JOIN clause. | |
| 219 | 261 | */ |
| 220 | - public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) { | |
| 262 | + public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) { | |
| 221 | 263 | return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->join_clause( 'p' ) : $sql; |
| 222 | 264 | } |
| 223 | 265 | |
| 224 | 266 | /** |
| 225 | - * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language | |
| 267 | + * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language. | |
| 226 | 268 | * |
| 227 | 269 | * @since 0.1 |
| 228 | 270 | * |
| 229 | 271 | * @param string $sql The WHERE clause in the SQL. |
| 230 | 272 | * @param bool $in_same_term Whether post should be in a same taxonomy term. |
| 231 | - * @param array $excluded_terms Array of excluded term IDs. | |
| 273 | + * @param int[] $excluded_terms Array of excluded term IDs. | |
| 232 | 274 | * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. |
| 233 | 275 | * @param WP_Post $post WP_Post object. |
| 234 | - * @return string modified WHERE clause | |
| 276 | + * @return string Modified WHERE clause. | |
| 235 | 277 | */ |
| 236 | - public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) { | |
| 278 | + public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) { | |
| 237 | 279 | return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->where_clause( $this->curlang ) : $sql; |
| 238 | 280 | } |
| 239 | 281 | |
| 240 | 282 | /** |
| @@ -252,40 +294,15 @@ | ||
| 252 | 294 | return $output; |
| 253 | 295 | } |
| 254 | 296 | |
| 255 | 297 | /** |
| 256 | - * Prevents deleting all the translations of the default category | |
| 257 | - * | |
| 258 | - * @since 2.1 | |
| 259 | - * | |
| 260 | - * @param array $caps The user's actual capabilities. | |
| 261 | - * @param string $cap Capability name. | |
| 262 | - * @param int $user_id The user ID. | |
| 263 | - * @param array $args Adds the context to the cap. The category id. | |
| 264 | - * @return array | |
| 265 | - */ | |
| 266 | - public function fix_delete_default_category( $caps, $cap, $user_id, $args ) { | |
| 267 | - if ( 'delete_term' === $cap ) { | |
| 268 | - $term = get_term( reset( $args ) ); // Since WP 4.4, we can get the term to get the taxonomy | |
| 269 | - if ( $term instanceof WP_Term ) { | |
| 270 | - $default_cat = get_option( 'default_' . $term->taxonomy ); | |
| 271 | - if ( $default_cat && array_intersect( $args, $this->model->term->get_translations( $default_cat ) ) ) { | |
| 272 | - $caps[] = 'do_not_allow'; | |
| 273 | - } | |
| 274 | - } | |
| 275 | - } | |
| 276 | - | |
| 277 | - return $caps; | |
| 278 | - } | |
| 279 | - | |
| 280 | - /** | |
| 281 | 298 | * Translates the site title in emails sent to the user (change email, reset password) |
| 282 | 299 | * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale() |
| 283 | 300 | * |
| 284 | 301 | * @since 2.1.3 |
| 285 | 302 | * |
| 286 | - * @param array $email | |
| 287 | - * @return array | |
| 303 | + * @param string[] $email Email contents. | |
| 304 | + * @return string[] Translated email contents. | |
| 288 | 305 | */ |
| 289 | 306 | public function translate_user_email( $email ) { |
| 290 | 307 | $blog_name = wp_specialchars_decode( pll__( get_option( 'blogname' ) ), ENT_QUOTES ); |
| 291 | 308 | $email['subject'] = sprintf( $email['subject'], $blog_name ); |
| @@ -332,9 +349,9 @@ | ||
| 332 | 349 | * |
| 333 | 350 | * @since 2.3.6 |
| 334 | 351 | * |
| 335 | 352 | * @param array $exporters Personal data exporters |
| 336 | - * @retun array | |
| 353 | + * @return array | |
| 337 | 354 | */ |
| 338 | 355 | public function register_personal_data_exporter( $exporters ) { |
| 339 | 356 | $exporters[] = array( |
| 340 | 357 | 'exporter_friendly_name' => __( 'Translated user descriptions', 'polylang' ), |