| @@ -1,9 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | 4 | * Manages the compatibility with Yoast SEO |
| 5 | - * Version tested: 11.5 | |
| 6 | 5 | * |
| 7 | 6 | * @since 2.3 |
| 8 | 7 | */ |
| 9 | 8 | class PLL_WPSEO { |
| @@ -12,11 +11,25 @@ | ||
| 12 | 11 | * |
| 13 | 12 | * @since 1.6.4 |
| 14 | 13 | */ |
| 15 | 14 | public function init() { |
| 15 | + if ( ! defined( 'WPSEO_VERSION' ) ) { | |
| 16 | + return; | |
| 17 | + } | |
| 18 | + | |
| 16 | 19 | if ( PLL() instanceof PLL_Frontend ) { |
| 17 | 20 | add_filter( 'option_wpseo_titles', array( $this, 'wpseo_translate_titles' ) ); |
| 18 | 21 | |
| 22 | + // Reloads options once the language has been defined to enable translations | |
| 23 | + // Useful only when the language is set from content | |
| 24 | + if ( version_compare( WPSEO_VERSION, '7.0', '<' ) && did_action( 'wp_loaded' ) ) { | |
| 25 | + $wpseo_front = WPSEO_Frontend::get_instance(); | |
| 26 | + $options = WPSEO_Options::get_option_names(); | |
| 27 | + foreach ( $options as $opt ) { | |
| 28 | + $wpseo_front->options = array_merge( $wpseo_front->options, (array) get_option( $opt ) ); | |
| 29 | + } | |
| 30 | + } | |
| 31 | + | |
| 19 | 32 | // Filters sitemap queries to remove inactive language or to get |
| 20 | 33 | // one sitemap per language when using multiple domains or subdomains |
| 21 | 34 | // because WPSEO does not accept several domains or subdomains in one sitemap |
| 22 | 35 | add_filter( 'wpseo_posts_join', array( $this, 'wpseo_posts_join' ), 10, 2 ); |
| @@ -26,13 +39,16 @@ | ||
| 26 | 39 | |
| 27 | 40 | if ( PLL()->options['force_lang'] > 1 ) { |
| 28 | 41 | add_filter( 'wpseo_enable_xml_sitemap_transient_caching', '__return_false' ); // Disable cache! otherwise WPSEO keeps only one domain (thanks to Junaid Bhura) |
| 29 | 42 | add_filter( 'home_url', array( $this, 'wpseo_home_url' ), 10, 2 ); // Fix home_url |
| 30 | - add_action( 'setup_theme', array( $this, 'maybe_deactivate_sitemap' ) ); // Deactivate sitemaps for inactive languages. | |
| 31 | 43 | } else { |
| 32 | 44 | // Get all terms in all languages when the language is set from the content or directory name |
| 33 | 45 | add_filter( 'get_terms_args', array( $this, 'wpseo_remove_terms_filter' ) ); |
| 34 | - add_action( 'pre_get_posts', array( $this, 'before_sitemap' ), 0 ); // Needs to be fired before WPSEO_Sitemaps::redirect() | |
| 46 | + | |
| 47 | + // Add the homepages for all languages to the sitemap when the front page displays posts | |
| 48 | + if ( ! get_option( 'page_on_front' ) ) { | |
| 49 | + add_filter( 'wpseo_sitemap_post_content', array( $this, 'add_language_home_urls' ) ); | |
| 50 | + } | |
| 35 | 51 | } |
| 36 | 52 | |
| 37 | 53 | add_filter( 'pll_home_url_white_list', array( $this, 'wpseo_home_url_white_list' ) ); |
| 38 | 54 | add_action( 'wpseo_opengraph', array( $this, 'wpseo_ogp' ), 2 ); |
| @@ -50,9 +66,9 @@ | ||
| 50 | 66 | * Registers strings for custom post types and custom taxonomies titles and meta descriptions |
| 51 | 67 | * |
| 52 | 68 | * @since 2.0 |
| 53 | 69 | */ |
| 54 | - public function wpseo_register_strings() { | |
| 70 | + function wpseo_register_strings() { | |
| 55 | 71 | $options = get_option( 'wpseo_titles' ); |
| 56 | 72 | foreach ( get_post_types( array( 'public' => true, '_builtin' => false ) ) as $t ) { |
| 57 | 73 | if ( pll_is_translated_post_type( $t ) ) { |
| 58 | 74 | $this->_wpseo_register_strings( $options, array( 'title-' . $t, 'metadesc-' . $t ) ); |
| @@ -95,9 +111,9 @@ | ||
| 95 | 111 | * |
| 96 | 112 | * @param array $options |
| 97 | 113 | * @return array |
| 98 | 114 | */ |
| 99 | - public function wpseo_translate_titles( $options ) { | |
| 115 | + function wpseo_translate_titles( $options ) { | |
| 100 | 116 | if ( PLL() instanceof PLL_Frontend ) { |
| 101 | 117 | foreach ( get_post_types( array( 'public' => true, '_builtin' => false ) ) as $t ) { |
| 102 | 118 | if ( pll_is_translated_post_type( $t ) ) { |
| 103 | 119 | $options = $this->_wpseo_translate_titles( $options, array( 'title-' . $t, 'metadesc-' . $t ) ); |
| @@ -127,13 +143,11 @@ | ||
| 127 | 143 | * @param string $path |
| 128 | 144 | * @return $url |
| 129 | 145 | */ |
| 130 | 146 | public function wpseo_home_url( $url, $path ) { |
| 131 | - if ( empty( $path ) ) { | |
| 132 | - $path = ltrim( wp_parse_url( pll_get_requested_url(), PHP_URL_PATH ), '/' ); | |
| 133 | - } | |
| 147 | + $uri = empty( $path ) ? ltrim( $_SERVER['REQUEST_URI'], '/' ) : $path; | |
| 134 | 148 | |
| 135 | - if ( 'sitemap_index.xml' === $path || preg_match( '#([^/]+?)-sitemap([0-9]+)?\.xml|([a-z]+)?-?sitemap\.xsl#', $path ) ) { | |
| 149 | + if ( 'sitemap_index.xml' === $uri || preg_match( '#([^/]+?)-sitemap([0-9]+)?\.xml|([a-z]+)?-?sitemap\.xsl#', $uri ) ) { | |
| 136 | 150 | $url = PLL()->links_model->switch_language_in_link( $url, PLL()->curlang ); |
| 137 | 151 | } |
| 138 | 152 | |
| 139 | 153 | return $url; |
| @@ -207,100 +221,30 @@ | ||
| 207 | 221 | return $args; |
| 208 | 222 | } |
| 209 | 223 | |
| 210 | 224 | /** |
| 211 | - * Deactivates the sitemap for inactive languages when using subdomains or multiple domains | |
| 225 | + * Adds the home urls for all (active) languages to the sitemap | |
| 212 | 226 | * |
| 213 | - * @since 2.6.1 | |
| 214 | - */ | |
| 215 | - public function maybe_deactivate_sitemap() { | |
| 216 | - global $wpseo_sitemaps; | |
| 217 | - | |
| 218 | - if ( isset( $wpseo_sitemaps ) ) { | |
| 219 | - $active_languages = $this->wpseo_get_active_languages(); | |
| 220 | - if ( ! empty( $active_languages ) && ! in_array( pll_current_language(), $active_languages ) ) { | |
| 221 | - remove_action( 'pre_get_posts', array( $wpseo_sitemaps, 'redirect' ), 1 ); | |
| 222 | - } | |
| 223 | - } | |
| 224 | - } | |
| 225 | - | |
| 226 | - /** | |
| 227 | - * Add filters before the sitemap is evaluated and outputed | |
| 227 | + * @since 1.9 | |
| 228 | 228 | * |
| 229 | - * @since 2.6 | |
| 230 | - * | |
| 231 | - * @param object $query Instance of WP_Query being filtered. | |
| 232 | - */ | |
| 233 | - public function before_sitemap( $query ) { | |
| 234 | - $type = $query->get( 'sitemap' ); | |
| 235 | - | |
| 236 | - // Add the post post type archives in all languages to the sitemap | |
| 237 | - // Add the homepages for all languages to the sitemap when the front page displays posts | |
| 238 | - if ( $type && pll_is_translated_post_type( $type ) && ( 'post' !== $type || ! get_option( 'page_on_front' ) ) ) { | |
| 239 | - add_filter( "wpseo_sitemap_{$type}_content", array( $this, 'add_post_type_archive' ) ); | |
| 240 | - } | |
| 241 | - } | |
| 242 | - | |
| 243 | - /** | |
| 244 | - * Generates a post type archive sitemap url | |
| 245 | - * | |
| 246 | - * @since 2.6.1 | |
| 247 | - * | |
| 248 | - * @param string $link The url. | |
| 249 | - * @param string $post_type The post type name. | |
| 250 | - * @return string Formatted sitemap url. | |
| 251 | - */ | |
| 252 | - protected function format_sitemap_url( $link, $post_type ) { | |
| 253 | - global $wpseo_sitemaps; | |
| 254 | - | |
| 255 | - return $wpseo_sitemaps->renderer->sitemap_url( | |
| 256 | - array( | |
| 257 | - 'loc' => $link, | |
| 258 | - 'mod' => WPSEO_Sitemaps::get_last_modified_gmt( $post_type ), | |
| 259 | - 'pri' => 1, | |
| 260 | - 'chf' => 'daily', | |
| 261 | - ) | |
| 262 | - ); | |
| 263 | - } | |
| 264 | - | |
| 265 | - /** | |
| 266 | - * Adds the home and post type archives urls for all (active) languages to the sitemap | |
| 267 | - * | |
| 268 | - * @since 2.6 | |
| 269 | - * | |
| 270 | 229 | * @param string $str additional urls to sitemap post |
| 271 | 230 | * @return string |
| 272 | 231 | */ |
| 273 | - public function add_post_type_archive( $str ) { | |
| 274 | - $post_type = substr( substr( current_filter(), 14 ), 0, -8 ); | |
| 275 | - $post_type_obj = get_post_type_object( $post_type ); | |
| 276 | - $languages = wp_list_filter( PLL()->model->get_languages_list(), array( 'active' => false ), 'NOT' ); | |
| 232 | + public function add_language_home_urls( $str ) { | |
| 233 | + global $wpseo_sitemaps; | |
| 234 | + $renderer = version_compare( WPSEO_VERSION, '3.2', '<' ) ? $wpseo_sitemaps : $wpseo_sitemaps->renderer; | |
| 277 | 235 | |
| 278 | - if ( 'post' === $post_type ) { | |
| 279 | - if ( ! empty( PLL()->options['hide_default'] ) ) { | |
| 280 | - // The home url is of course already added by WPSEO. | |
| 281 | - $languages = wp_list_filter( $languages, array( 'slug' => pll_default_language() ), 'NOT' ); | |
| 282 | - } | |
| 236 | + $languages = wp_list_pluck( wp_list_filter( PLL()->model->get_languages_list(), array( 'active' => false ), 'NOT' ), 'slug' ); | |
| 283 | 237 | |
| 284 | - foreach ( $languages as $lang ) { | |
| 285 | - $str .= $this->format_sitemap_url( pll_home_url( $lang->slug ), $post_type ); | |
| 238 | + foreach ( $languages as $lang ) { | |
| 239 | + if ( empty( PLL()->options['hide_default'] ) || pll_default_language() !== $lang ) { | |
| 240 | + $str .= $renderer->sitemap_url( array( | |
| 241 | + 'loc' => pll_home_url( $lang ), | |
| 242 | + 'pri' => 1, | |
| 243 | + 'chf' => apply_filters( 'wpseo_sitemap_homepage_change_freq', 'daily', pll_home_url( $lang ) ), | |
| 244 | + ) ); | |
| 286 | 245 | } |
| 287 | - } elseif ( $post_type_obj->has_archive ) { | |
| 288 | - // Exclude cases where a post type archive is attached to a page (ex: WooCommerce). | |
| 289 | - $slug = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive; | |
| 290 | - | |
| 291 | - if ( ! get_page_by_path( $slug ) ) { | |
| 292 | - // The post type archive in the current language is already added by WPSEO. | |
| 293 | - $languages = wp_list_filter( $languages, array( 'slug' => pll_current_language() ), 'NOT' ); | |
| 294 | - | |
| 295 | - foreach ( $languages as $lang ) { | |
| 296 | - PLL()->curlang = $lang; // Switch the language to get the correct archive link. | |
| 297 | - $link = get_post_type_archive_link( $post_type ); | |
| 298 | - $str .= $this->format_sitemap_url( $link, $post_type ); | |
| 299 | - } | |
| 300 | - } | |
| 301 | 246 | } |
| 302 | - | |
| 303 | 247 | return $str; |
| 304 | 248 | } |
| 305 | 249 | |
| 306 | 250 | /** |
| @@ -324,22 +268,13 @@ | ||
| 324 | 268 | global $wpseo_og; |
| 325 | 269 | |
| 326 | 270 | // WPSEO already deals with the locale |
| 327 | 271 | if ( did_action( 'pll_init' ) && method_exists( $wpseo_og, 'og_tag' ) ) { |
| 328 | - $alternates = array(); | |
| 329 | - | |
| 330 | 272 | foreach ( PLL()->model->get_languages_list() as $language ) { |
| 331 | 273 | if ( PLL()->curlang->slug !== $language->slug && PLL()->links->get_translation_url( $language ) && isset( $language->facebook ) ) { |
| 332 | - $alternates[] = $language->facebook; | |
| 274 | + $wpseo_og->og_tag( 'og:locale:alternate', $language->facebook ); | |
| 333 | 275 | } |
| 334 | 276 | } |
| 335 | - | |
| 336 | - // There is a risk that 2 languages have the same Facebook locale. So let's make sure to output each locale only once. | |
| 337 | - $alternates = array_unique( $alternates ); | |
| 338 | - | |
| 339 | - foreach ( $alternates as $lang ) { | |
| 340 | - $wpseo_og->og_tag( 'og:locale:alternate', $lang ); | |
| 341 | - } | |
| 342 | 277 | } |
| 343 | 278 | } |
| 344 | 279 | |
| 345 | 280 | /** |
| @@ -372,42 +307,32 @@ | ||
| 372 | 307 | return $options; |
| 373 | 308 | } |
| 374 | 309 | |
| 375 | 310 | /** |
| 376 | - * Synchronize the primary term | |
| 311 | + * Synchronize the primary category | |
| 377 | 312 | * |
| 378 | 313 | * @since 2.3.3 |
| 379 | 314 | * |
| 380 | - * @param array $keys List of custom fields names. | |
| 315 | + * @param array $keys List of custom fields names | |
| 381 | 316 | * @return array |
| 382 | 317 | */ |
| 383 | 318 | public function copy_post_metas( $keys ) { |
| 384 | - $taxonomies = get_taxonomies( | |
| 385 | - array( | |
| 386 | - 'hierarchical' => true, | |
| 387 | - 'public' => true, | |
| 388 | - ) | |
| 389 | - ); | |
| 390 | - | |
| 391 | - foreach ( $taxonomies as $taxonomy ) { | |
| 392 | - $keys[] = '_yoast_wpseo_primary_' . $taxonomy; | |
| 393 | - } | |
| 394 | - | |
| 319 | + $keys[] = '_yoast_wpseo_primary_category'; | |
| 395 | 320 | return $keys; |
| 396 | 321 | } |
| 397 | 322 | |
| 398 | 323 | /** |
| 399 | - * Translate the primary term during the synchronization process | |
| 324 | + * Translate the primary category during the synchronization process | |
| 400 | 325 | * |
| 401 | 326 | * @since 2.3.3 |
| 402 | 327 | * |
| 403 | - * @param int $value Meta value. | |
| 404 | - * @param string $key Meta key. | |
| 405 | - * @param string $lang Language of target. | |
| 328 | + * @param int $value Meta value | |
| 329 | + * @param string $key Meta key | |
| 330 | + * @param string $lang Language of target | |
| 406 | 331 | * @return int |
| 407 | 332 | */ |
| 408 | 333 | public function translate_post_meta( $value, $key, $lang ) { |
| 409 | - if ( false !== strpos( $key, '_yoast_wpseo_primary_' ) ) { | |
| 334 | + if ( '_yoast_wpseo_primary_category' === $key ) { | |
| 410 | 335 | $value = pll_get_term( $value, $lang ); |
| 411 | 336 | } |
| 412 | 337 | return $value; |
| 413 | 338 | } |