| @@ -8,17 +8,22 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 1.2 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Frontend_Links extends PLL_Links { |
| 12 | - public $curlang; | |
| 13 | - public $cache; // Our internal non persistent cache object | |
| 14 | 12 | |
| 15 | 13 | /** |
| 14 | + * Internal non persistent cache object. | |
| 15 | + * | |
| 16 | + * @var PLL_Cache<string> | |
| 17 | + */ | |
| 18 | + public $cache; | |
| 19 | + | |
| 20 | + /** | |
| 16 | 21 | * Constructor |
| 17 | 22 | * |
| 18 | 23 | * @since 1.2 |
| 19 | 24 | * |
| 20 | - * @param object $polylang | |
| 25 | + * @param object $polylang The Polylang object. | |
| 21 | 26 | */ |
| 22 | 27 | public function __construct( &$polylang ) { |
| 23 | 28 | parent::__construct( $polylang ); |
| 24 | 29 | |
| @@ -23,17 +28,16 @@ | ||
| 23 | 28 | parent::__construct( $polylang ); |
| 24 | 29 | |
| 25 | 30 | $this->curlang = &$polylang->curlang; |
| 26 | 31 | $this->cache = new PLL_Cache(); |
| 27 | - | |
| 28 | 32 | } |
| 29 | 33 | |
| 30 | 34 | /** |
| 31 | - * Returns the url of the translation ( if exists ) of the current page | |
| 35 | + * Returns the url of the translation (if it exists) of the current page. | |
| 32 | 36 | * |
| 33 | 37 | * @since 0.1 |
| 34 | 38 | * |
| 35 | - * @param object $language | |
| 39 | + * @param PLL_Language $language Language object. | |
| 36 | 40 | * @return string |
| 37 | 41 | */ |
| 38 | 42 | public function get_translation_url( $language ) { |
| 39 | 43 | global $wp_query; |
| @@ -46,16 +50,16 @@ | ||
| 46 | 50 | // See https://wordpress.org/support/topic/patch-for-fixing-a-notice |
| 47 | 51 | $queried_object_id = $wp_query->get_queried_object_id(); |
| 48 | 52 | |
| 49 | 53 | /** |
| 50 | - * Filter the translation url before Polylang attempts to find one | |
| 51 | - * Internally used by Polylang for the static front page and posts page | |
| 54 | + * Filters the translation url before Polylang attempts to find one. | |
| 55 | + * Internally used by Polylang for the static front page and posts page. | |
| 52 | 56 | * |
| 53 | 57 | * @since 1.8 |
| 54 | 58 | * |
| 55 | - * @param string $url Empty or the url of the translation of teh current page | |
| 56 | - * @param object $language Language of the translation | |
| 57 | - * @param int $queried_object_id Queried object id | |
| 59 | + * @param string $url Empty string or the url of the translation of the current page. | |
| 60 | + * @param PLL_Language $language Language of the translation. | |
| 61 | + * @param int $queried_object_id Queried object ID. | |
| 58 | 62 | */ |
| 59 | 63 | if ( ! $url = apply_filters( 'pll_pre_translation_url', '', $language, $queried_object_id ) ) { |
| 60 | 64 | $qv = $wp_query->query_vars; |
| 61 | 65 | |
| @@ -77,14 +81,19 @@ | ||
| 77 | 81 | foreach ( $wp_query->tax_query->queries as $tax_query ) { |
| 78 | 82 | if ( ! empty( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) ) { |
| 79 | 83 | |
| 80 | 84 | $tax = get_taxonomy( $tax_query['taxonomy'] ); |
| 81 | - $terms = get_terms( $tax->name, array( 'fields' => 'id=>slug' ) ); // Filtered by current language | |
| 85 | + $terms = get_terms( array( 'taxonomy' => $tax->name, 'fields' => 'id=>slug' ) ); // Filtered by current language | |
| 82 | 86 | |
| 83 | 87 | foreach ( $tax_query['terms'] as $slug ) { |
| 84 | 88 | $term_id = array_search( $slug, $terms ); // What is the term_id corresponding to taxonomy term? |
| 85 | 89 | if ( $term_id && $term_id = $this->model->term->get_translation( $term_id, $language ) ) { // Get the translated term_id |
| 86 | 90 | $term = get_term( $term_id, $tax->name ); |
| 91 | + | |
| 92 | + if ( ! $term instanceof WP_Term ) { | |
| 93 | + continue; | |
| 94 | + } | |
| 95 | + | |
| 87 | 96 | $url = str_replace( $slug, $term->slug, $url ); |
| 88 | 97 | } |
| 89 | 98 | } |
| 90 | 99 | } |
| @@ -101,11 +110,12 @@ | ||
| 101 | 110 | $url = get_term_link( $term, $term->taxonomy ); // Self link |
| 102 | 111 | } |
| 103 | 112 | |
| 104 | 113 | elseif ( $tr_id = $this->model->term->get_translation( $term->term_id, $language ) ) { |
| 105 | - if ( $tr_term = get_term( $tr_id, $term->taxonomy ) ) { | |
| 114 | + $tr_term = get_term( $tr_id, $term->taxonomy ); | |
| 115 | + if ( $tr_term instanceof WP_Term ) { | |
| 106 | 116 | // Check if translated term ( or children ) have posts |
| 107 | - $count = $tr_term->count || ( is_taxonomy_hierarchical( $term->taxonomy ) && array_sum( wp_list_pluck( get_terms( $term->taxonomy, array( 'child_of' => $tr_term->term_id, 'lang' => $language->slug ) ), 'count' ) ) ); | |
| 117 | + $count = $tr_term->count || ( is_taxonomy_hierarchical( $term->taxonomy ) && array_sum( wp_list_pluck( get_terms( array( 'taxonomy' => $term->taxonomy, 'child_of' => $tr_term->term_id, 'lang' => $language->slug ) ), 'count' ) ) ); | |
| 108 | 118 | |
| 109 | 119 | /** |
| 110 | 120 | * Filter whether to hide an archive translation url |
| 111 | 121 | * |
| @@ -154,8 +164,10 @@ | ||
| 154 | 164 | $url = $this->get_home_url( $language ); |
| 155 | 165 | } |
| 156 | 166 | } |
| 157 | 167 | |
| 168 | + $url = ! empty( $url ) && ! is_wp_error( $url ) ? $url : null; | |
| 169 | + | |
| 158 | 170 | /** |
| 159 | 171 | * Filter the translation url of the current page before Polylang caches it |
| 160 | 172 | * |
| 161 | 173 | * @since 1.1.2 |
| @@ -162,9 +174,9 @@ | ||
| 162 | 174 | * |
| 163 | 175 | * @param null|string $url The translation url, null if none was found |
| 164 | 176 | * @param string $language The language code of the translation |
| 165 | 177 | */ |
| 166 | - $translation_url = apply_filters( 'pll_translation_url', ( isset( $url ) && ! is_wp_error( $url ) ? $url : null ), $language->slug ); | |
| 178 | + $translation_url = (string) apply_filters( 'pll_translation_url', $url, $language->slug ); | |
| 167 | 179 | |
| 168 | 180 | // Don't cache before template_redirect to avoid a conflict with Barrel + WP Bakery Page Builder |
| 169 | 181 | if ( did_action( 'template_redirect' ) ) { |
| 170 | 182 | $this->cache->set( 'translation_url:' . $language->slug, $translation_url ); |
| @@ -178,9 +190,9 @@ | ||
| 178 | 190 | * used also for search |
| 179 | 191 | * |
| 180 | 192 | * @since 1.2 |
| 181 | 193 | * |
| 182 | - * @param object $language | |
| 194 | + * @param PLL_Language $language An object representing a language. | |
| 183 | 195 | * @return string |
| 184 | 196 | */ |
| 185 | 197 | public function get_archive_url( $language ) { |
| 186 | 198 | $url = pll_get_requested_url(); |
| @@ -198,14 +210,14 @@ | ||
| 198 | 210 | return apply_filters( 'pll_get_archive_url', $url, $language ); |
| 199 | 211 | } |
| 200 | 212 | |
| 201 | 213 | /** |
| 202 | - * Returns the home url in the right language | |
| 214 | + * Returns the home url in the right language. | |
| 203 | 215 | * |
| 204 | 216 | * @since 0.1 |
| 205 | 217 | * |
| 206 | - * @param object $language Optional, defaults to current language | |
| 207 | - * @param bool $is_search Optional, whether we need the home url for a search form, defaults to false | |
| 218 | + * @param PLL_Language|string $language Optional, defaults to current language. | |
| 219 | + * @param bool $is_search Optional, whether we need the home url for a search form, defaults to false. | |
| 208 | 220 | */ |
| 209 | 221 | public function get_home_url( $language = '', $is_search = false ) { |
| 210 | 222 | if ( empty( $language ) ) { |
| 211 | 223 | $language = $this->curlang; |