Last active
December 17, 2023 01:53
-
-
Save wpmudev-sls/426d2ee43c5fd33b0e9d59d24ef24adb to your computer and use it in GitHub Desktop.
[SmartCrawl Pro] Multilingual sitemap (WPML integration) [Alt version]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [SmartCrawl Pro] Multilingual sitemap (WPML integration) [Alt version] | |
* Description: Improves the SmartCrawl sitemaps with multilingual-ready capabilities when the WPML plugin is present | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-5524 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
// This snippet is a simplified version of: | |
// https://gist.github.com/wpmudev-sls/d14cbe94eef14de670b7c9e0a77631b2 | |
add_action( 'plugins_loaded', function() { | |
if ( ! class_exists( 'Smartcrawl_Sitemap_Post_Fetcher' ) ) { | |
return; // SmartCrawl is not installed/enabled. | |
} | |
if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) { | |
return; // WPML is not installed/enabled. | |
} | |
add_filter( 'wds_partial_sitemap_items', function( $items, $type, $page ) { | |
global $wpdb; | |
// ---------------------------------------------------------------------------- // | |
// Modify this array with the Custom Post Types to be translated in the Sitemap | |
// or leave it as a blank array to use all the public available types | |
// ---------------------------------------------------------------------------- // | |
$allowed_types = array(); | |
// ---------------------------------------------------------------------------- // | |
if ( empty( $allowed_types ) ) { | |
$allowed_types = get_post_types( | |
array( | |
'show_ui' => true, | |
'public' => true, | |
'_builtin' => false, | |
) | |
); | |
$allowed_types = array_keys( $allowed_types ); | |
} | |
if ( empty( $allowed_types ) || ( ! empty( $allowed_types ) && ! in_array( $type, $allowed_types ) ) ) { | |
return $items; // Type not allowed. | |
} | |
if ( empty( $items ) ) { | |
return $items; // Nothing to handle. | |
} | |
$grouped_items = array(); | |
foreach( $items as $item ) { | |
if ( ! isset( $grouped_items[ $item->get_location() ] ) ) { | |
$grouped_items[ $item->get_location() ] = array( | |
'id' => url_to_postid( $item->get_location() ), | |
'variants' => array(), | |
'item' => $item, | |
); | |
} else { | |
$grouped_items[ $item->get_location() ]['variants'][ $item->get_last_modified() ] = $item; | |
} | |
} | |
if ( empty( $items ) ) { | |
return $items; | |
} | |
$p = $wpdb->prefix; | |
$ids_in = implode( ',', array_column( $grouped_items, 'id' ) ); | |
$query = "SELECT | |
( | |
SELECT | |
element_id | |
FROM | |
{$p}icl_translations | |
WHERE | |
trid = t.trid | |
AND element_type = 'post_$type' | |
AND source_language_code IS NULL LIMIT 1 | |
) parent_id, | |
p.id post_id, | |
p.guid post_url, | |
t.language_code post_lang, | |
UNIX_TIMESTAMP( p.post_modified_gmt ) post_date | |
FROM | |
{$p}icl_translations t | |
LEFT JOIN {$p}posts p ON p.ID = t.element_id | |
WHERE | |
trid IN ( | |
SELECT | |
trid | |
FROM | |
{$p}icl_translations | |
WHERE | |
element_id IN ($ids_in) | |
AND element_type = 'post_$type' | |
AND p.post_status = 'publish' | |
) | |
AND t.element_id NOT IN ($ids_in)"; | |
$posts = $wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
if ( empty( $posts ) ) { | |
return $items; // Empty result set (probably due to a DB query error). | |
} | |
$result = array(); | |
foreach( $grouped_items as &$item ) { | |
$result[] = $item['item']; | |
foreach( $posts as $post ) { | |
if( $item['id'] === (int) $post['parent_id'] ) { | |
$variant = clone $item['item']; | |
$variant->set_location( apply_filters( 'wpml_permalink', $post['post_url'], $post['post_lang'] ) ); | |
$result[] = $variant; | |
} | |
} | |
} | |
return $result; | |
}, 10, 3 ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment