Last active
January 4, 2022 04:03
-
-
Save wpmudev-sls/d32b7a5911fec6c71e9080424c10e697 to your computer and use it in GitHub Desktop.
[SmartCrawl] Separated the sitemaps by languages.
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] Separated the sitemaps by languages. | |
* Description: [SmartCrawl] Separated the sitemaps by languages - supported TranslatePress - Multilingual plugin | |
* Task: SLS-323 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
/** | |
* This MU will separated the sitemap by languages: | |
* url: example.com/fr/sitemap.xml | |
* path: wp-content/uploads/sc-languages/fr/smartcrawl/sitemap/index-sitemap0.xml | |
*/ | |
add_action( 'after_setup_theme', 'wpmudev_sc_separated_the_sitemap_by_languages', 100 ); | |
function wpmudev_sc_separated_the_sitemap_by_languages() { | |
if ( defined( 'SMARTCRAWL_VERSION' ) && class_exists('Smartcrawl_Settings') ) { | |
class WPMUDEV_SC_Separated_Sitemap_By_Languages{ | |
public $sub_folder = 'sc-languages';//wp-content/uploads/[sub-folder]/... | |
// Enter list of language codes in your site. | |
public $all_lang_codes = array( | |
// 'en', | |
// 'fr' | |
); | |
private $current_language; | |
public function __construct(){ | |
// check enabled sitemap | |
if ( ! Smartcrawl_Sitemap_UI::get()->should_run() ) { | |
return; | |
} | |
add_action( 'init', array( $this, 'add_rewrites' ) ); | |
// add_action( 'wds_before_sitemap_rebuild', array( $this, 'before_build_sitemap' ) ); | |
add_action( 'wp', array( $this, 'serve_sitemap' ), 998 ); | |
add_action( 'wp', array( $this, 'disable_custom_filter' ), 1000 ); | |
// maybe re-generate sitemaps. | |
add_action( 'admin_init', array( $this, 'prime_cache_on_sitemap_settings_page_load' ), 10 ); | |
} | |
public function add_rewrites(){ | |
/** | |
* @var $wp \WP | |
*/ | |
global $wp; | |
$wp->add_query_var( 'lang' ); | |
// add_rewrite_rule( '([a-z]{2})/sitemap\.xml(\.gz)?$', 'index.php?wds_sitemap=1&wds_sitemap_type=index&lang=$matches[1]&wds_sitemap_gzip=$matches[2]', 'top' ); | |
// add_rewrite_rule( '([a-z]{2})/([^/]+?)-sitemap([0-9]+)?\.xml(\.gz)?$', 'index.php?wds_sitemap=1&lang=$matches[1]&wds_sitemap_type=$matches[2]&wds_sitemap_page=$matches[3]&wds_sitemap_gzip=$matches[4]', 'top' ); | |
add_rewrite_rule( 'sitemap-([a-z]{2})\.xml(\.gz)?$', 'index.php?wds_sitemap=1&wds_sitemap_type=index&lang=$matches[1]&wds_sitemap_gzip=$matches[2]', 'top' ); | |
add_rewrite_rule( '([a-z]{2})/([^/]+?)-sitemap([0-9]+)?\.xml(\.gz)?$', 'index.php?wds_sitemap=1&lang=$matches[1]&wds_sitemap_type=$matches[2]&wds_sitemap_page=$matches[3]&wds_sitemap_gzip=$matches[4]', 'top' ); | |
// flush rewrite rules | |
$this->maybe_flush_rewrite_rules(); | |
} | |
protected function maybe_flush_rewrite_rules() { | |
$flushed = get_option( 'wpmudev-sc-flush-rewrite-rules', false ); | |
if ( $flushed !== SMARTCRAWL_VERSION ) { | |
flush_rewrite_rules(); | |
update_option( 'wpmudev-sc-flush-rewrite-rules', SMARTCRAWL_VERSION ); | |
} | |
} | |
// public function before_build_sitemap(){ | |
// } | |
// ['en','vi']. | |
public function get_all_language_codes( $exclude_default = false ){ | |
static $all_lang_codes; | |
if( ! isset( $all_lang_codes ) ){ | |
$all_lang_codes = array(); | |
// support for TranslatePress - Multilingual plugin. | |
if( class_exists('TRP_Translate_Press') ){ | |
$trp_settings = TRP_Translate_Press::get_trp_instance()->get_component( 'settings' ); | |
$settings = $trp_settings->get_settings(); | |
$all_lang_codes = $settings['url-slugs']; | |
// exclude default language. | |
if( $exclude_default ){ | |
$default_language = $settings["default-language"]; | |
if( isset( $all_lang_codes[ $default_language ] ) ){ | |
unset( $all_lang_codes[ $default_language ] ); | |
} | |
} | |
} elseif ( $this->all_lang_codes ) { | |
$all_lang_codes = $ths->all_lang_codes; | |
} | |
} | |
return $all_lang_codes; | |
} | |
public function exist_language( $lang ){ | |
if( $lang ){ | |
// exclude default language. | |
$all_lang_codes = $this->get_all_language_codes( true ); | |
if( ! in_array( $lang, $all_lang_codes ) ){ | |
$lang = false; | |
} | |
} | |
return $lang; | |
} | |
public function should_custom_home_url(){ | |
static $should_change; | |
if( ! isset( $should_change ) ){ | |
$should_change = false; | |
// support for TranslatePress - Multilingual plugin. | |
if( class_exists('TRP_Translate_Press') ){ | |
$should_change = true; | |
}else{ | |
$should_change = ! empty( $this->all_lang_codes ); | |
} | |
} | |
return $should_change; | |
} | |
public function serve_sitemap(){ | |
// maybe clear sitemap cache | |
if( ! Smartcrawl_Sitemap_Cache::get()->is_cache_pristine() ){ | |
$this->drop_cache(); | |
} | |
if( ! empty( get_query_var( 'wds_sitemap' ) ) && ( $this->current_language = $this->exist_language( get_query_var( 'lang' ) ) ) ){ | |
add_filter( 'upload_dir', array( $this, 'custom_upload_dir_path_for_sitemap' ) ); | |
if( $this->should_custom_home_url() ){ | |
add_filter( 'home_url', array( $this, 'custom_home_url_by_lang'), 101, 2 ); | |
} | |
} | |
} | |
public function get_sub_folder_sitemap(){ | |
return "/{$this->sub_folder}/"; | |
} | |
public function custom_upload_dir_path_for_sitemap( $uploads ){ | |
if( $this->current_language ){ | |
$uploads['basedir'] .= $this->get_sub_folder_sitemap() . $this->current_language; | |
} | |
return $uploads; | |
} | |
public function custom_home_url_by_lang( $url, $path ){ | |
if( $this->current_language ){ | |
$home_lang_url = site_url( '/'. $this->current_language .'/' ); | |
if( false === strpos( $url, $home_lang_url ) ){ | |
$url = str_replace( site_url( '/' ), $home_lang_url, $url ); | |
} | |
} | |
return $url; | |
} | |
public function disable_custom_filter(){ | |
if( $this->current_language ){ | |
remove_filter( 'upload_dir', array( $this, 'custom_upload_dir_path_for_sitemap' ) ); | |
if( $this->should_custom_home_url() ){ | |
remove_filter( 'home_url', array( $this, 'custom_home_url_by_lang'), 101, 2 ); | |
} | |
} | |
} | |
public function drop_cache() { | |
$file_system = $this->fs_direct(); | |
$uploads = wp_upload_dir( null, false ); | |
$cache_dir = $uploads['basedir'] . $this->get_sub_folder_sitemap(); | |
if ( ! @file_exists( $cache_dir ) ) { | |
Smartcrawl_Logger::error( "Sitemap cache by languages could not be dropped because they do not exist" ); | |
return false; | |
} | |
$removed = $file_system->rmdir( $cache_dir, true ); | |
if ( ! $removed ) { | |
Smartcrawl_Logger::error( "Sitemap cache by languages directory could not be removed" ); | |
return false; | |
} | |
Smartcrawl_Logger::info( "Sitemap cache by languages dropped" ); | |
return true; | |
} | |
private function fs_direct() { | |
if ( ! class_exists( 'WP_Filesystem_Direct', false ) ) { | |
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; | |
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; | |
} | |
return new WP_Filesystem_Direct( null ); | |
} | |
public function get_all_sitemap_urls(){ | |
$all_sitemap_urls = array(); | |
$all_lang_codes = $this->get_all_language_codes( true ); | |
if ( ! empty( $all_lang_codes ) ) { | |
foreach ( $all_lang_codes as $lang ) { | |
$all_sitemap_urls[] = home_url( $lang . '/sitemap.xml' ); | |
} | |
} | |
return $all_sitemap_urls; | |
} | |
public function prime_cache_on_sitemap_settings_page_load() { | |
global $plugin_page; | |
$is_sitemap_page = isset( $plugin_page ) && Smartcrawl_Settings::TAB_SITEMAP === $plugin_page; | |
if ( ! $is_sitemap_page ) { | |
return; | |
} | |
if ( Smartcrawl_Sitemap_Cache::get()->is_index_cached() ) { | |
return; | |
} | |
$all_sitemap_urls = $this->get_all_sitemap_urls(); | |
if( $all_sitemap_urls ){ | |
foreach( $all_sitemap_urls as $sitemap_url ){ | |
wp_remote_get( $sitemap_url, array( 'blocking' => false ) ); | |
} | |
} | |
} | |
} | |
$run = new WPMUDEV_SC_Separated_Sitemap_By_Languages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment