Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,310 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\ThirdParty\Plugins\I18n;
use TRP_Translate_Press;
use WP_Rocket\Event_Management\Subscriber_Interface;
class TranslatePress implements Subscriber_Interface {
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
if ( ! class_exists( 'TRP_Translate_Press' ) ) {
return [];
}
return [
'rocket_rucss_is_home_url' => [ 'detect_homepage', 10, 2 ],
'rocket_has_i18n' => 'is_translatepress',
'rocket_i18n_admin_bar_menu' => 'add_langs_to_admin_bar',
'rocket_i18n_current_language' => 'set_current_language',
'rocket_get_i18n_uri' => 'get_active_languages_uri',
'rocket_get_i18n_code' => 'get_active_languages_codes',
'rocket_i18n_subdomains' => 'get_active_languages_uri',
'rocket_i18n_home_url' => [ 'get_home_url_for_lang', 10, 2 ],
'rocket_i18n_translated_post_urls' => [ 'get_translated_post_urls', 10, 4 ],
'post_updated' => 'clear_post_languages',
'trp_save_editor_translations_regular_strings' => [ 'clear_post_after_updating_translation', 10, 2 ],
];
}
/**
* Detect homepage.
*
* @param string $home_url home url.
* @param string $url url of current page.
* @return string
*/
public function detect_homepage( $home_url, $url ) {
$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );
$language = $converter->get_lang_from_url_string( $url );
$url_language = $converter->get_url_for_language( $language, home_url() );
return untrailingslashit( $url ) === untrailingslashit( $url_language ) ? $url : $home_url;
}
/**
* Adds TranslatePress as identifier for i18n detection
*
* @param string|bool $identifier An identifier value, false otherwise.
*
* @return string|bool
*/
public function is_translatepress( $identifier ) {
if (
function_exists( 'trp_get_languages' )
&&
! empty( trp_get_languages( 'nodefault' ) )
) {
return 'translatepress';
}
return $identifier;
}
/**
* Adds languages to the admin bar menu
*
* @param array $langlinks Array of languages.
*
* @return array
*/
public function add_langs_to_admin_bar( $langlinks ) {
$translatepress = TRP_Translate_Press::get_trp_instance();
$language_switcher = $translatepress->get_component( 'language_switcher' );
$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$trp_settings = $settings->get_settings();
$languages_to_display = $trp_settings['publish-languages'];
$published_languages = $languages->get_language_names( $languages_to_display );
foreach ( $published_languages as $code => $name ) {
$langlinks[ $code ] = [
'code' => $trp_settings['url-slugs'][ $code ],
'flag' => $language_switcher->add_flag( $code, $name ),
'anchor' => $name,
];
}
return $langlinks;
}
/**
* Sets the current language value
*
* @param string|bool $current_language Current language.
*
* @return string|bool
*/
public function set_current_language( $current_language ) {
if ( empty( $GLOBALS['TRP_LANGUAGE'] ) ) {
return $current_language;
}
return $GLOBALS['TRP_LANGUAGE'];
}
/**
* Gets URLs for active languages
*
* @param array $urls Array of active languages URI.
*
* @return array
*/
public function get_active_languages_uri( $urls ) {
if ( ! is_array( $urls ) ) {
$urls = (array) $urls;
}
$home_url = home_url();
$translatepress = TRP_Translate_Press::get_trp_instance();
$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$converter = $translatepress->get_component( 'url_converter' );
$trp_settings = $settings->get_settings();
$languages_to_display = $trp_settings['publish-languages'];
$published_languages = $languages->get_language_names( $languages_to_display );
foreach ( $published_languages as $code => $name ) {
$urls[] = $converter->get_url_for_language( $code, $home_url );
}
return $urls;
}
/**
* Gets the active languages slugs
*
* @param Array $codes Array of languages codes.
*
* @return array
*/
public function get_active_languages_codes( $codes ) {
if ( ! is_array( $codes ) ) {
$codes = (array) $codes;
}
$translatepress = TRP_Translate_Press::get_trp_instance();
$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$trp_settings = $settings->get_settings();
$languages_to_display = $trp_settings['publish-languages'];
$published_languages = $languages->get_language_names( $languages_to_display );
foreach ( $published_languages as $code => $name ) {
$codes[] = $trp_settings['url-slugs'][ $code ];
}
return $codes;
}
/**
* Gets home URL in given language
*
* @param string $home_url Home URL.
* @param string $lang Language code.
*
* @return string
*/
public function get_home_url_for_lang( $home_url, $lang ) {
if ( empty( $lang ) ) {
return $home_url;
}
$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );
$settings = $translatepress->get_component( 'settings' );
$trp_settings = $settings->get_settings();
$code = '';
add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
foreach ( $trp_settings['url-slugs'] as $index => $slug ) {
if ( $lang === $slug ) {
$code = $index;
break;
}
}
$url = $converter->get_url_for_language( $code, $home_url );
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
return $url;
}
/**
* Gets all translations URLs for a post
*
* @param array $urls Array of translated URLs.
* @param string $url URL to use.
* @param string $post_type Post type.
* @param string $regex Pattern to include at the end.
*
* @return array
*/
public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
if ( ! is_array( $urls ) ) {
$urls = (array) $urls;
}
$translatepress = TRP_Translate_Press::get_trp_instance();
$settings = $translatepress->get_component( 'settings' );
$languages = $translatepress->get_component( 'languages' );
$converter = $translatepress->get_component( 'url_converter' );
$trp_settings = $settings->get_settings();
$languages_to_display = $trp_settings['publish-languages'];
$published_languages = $languages->get_language_names( $languages_to_display );
foreach ( $published_languages as $code => $name ) {
$urls[] = wp_parse_url( $converter->get_url_for_language( $code, $url ), PHP_URL_PATH ) . $regex;
}
return $urls;
}
/**
* Clear all languages of a specific post
*
* @param int $post_id Post ID.
*
* @return void
*/
public function clear_post_languages( $post_id ) {
$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );
$settings = $translatepress->get_component( 'settings' );
$trp_settings = $settings->get_settings();
add_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
$clear_urls = [];
$default_permalink = get_permalink( $post_id );
foreach ( $trp_settings['translation-languages'] as $language ) {
if ( $language === $trp_settings['default-language'] ) {
continue;
}
$clear_urls[] = $converter->get_url_for_language( $language, $default_permalink, '' );
}
remove_filter( 'trp_add_language_to_home_url_check_for_admin', '__return_false' );
if ( empty( $clear_urls ) ) {
return;
}
rocket_clean_files( $clear_urls );
}
/**
* Clear the post cache when the translation is updated
*
* @param array $update_strings Array of updated strings.
* @param array $settings Array of settings.
*
* @return void
*/
public function clear_post_after_updating_translation( $update_strings, $settings ) {
$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );
if ( empty( $_POST['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return;
}
$url = esc_url_raw( wp_unslash( $_POST['url'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$clear_urls = [];
$current_url = remove_query_arg( 'trp-edit-translation', $url );
foreach ( $settings['translation-languages'] as $language ) {
if ( ! empty( $update_strings[ $language ] ) ) {
$clear_urls[] = $converter->get_url_for_language( $language, $current_url, '' );
}
}
rocket_clean_files( $clear_urls );
}
}
@@ -0,0 +1,195 @@
<?php
namespace WP_Rocket\ThirdParty\Plugins\I18n;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Filesystem_Direct;
use WP_Rocket\ThirdParty\ReturnTypesTrait;
/**
* Subscriber for compatibility with WPML.
*/
class WPML implements Subscriber_Interface {
use ReturnTypesTrait;
/**
* Filesystem instance.
*
* @var WP_Filesystem_Direct
*/
protected $filesystem;
/**
* Instantiate class.
*
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
*/
public function __construct( WP_Filesystem_Direct $filesystem = null ) {
$this->filesystem = ! empty( $filesystem ) ? $filesystem : rocket_direct_filesystem();
}
/**
* Events for subscriber to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
$events = [
'activate_sitepress-multilingual-cms/sitepress.php' => 'maybe_clear_on_disable',
'deactivate_sitepress-multilingual-cms/sitepress.php' => 'maybe_clear_on_disable',
];
if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
return $events;
}
$events['rocket_rucss_is_home_url'] = [ 'is_secondary_home', 10, 2 ];
$events['rocket_preload_all_to_pending_condition'] = 'clean_only_right_domain';
$events['rocket_preload_sitemap_before_queue'] = 'add_languages_sitemaps';
$events['after_rocket_clean_home'] = 'remove_root_cached_files';
$events['after_rocket_clean_domain'] = 'remove_root_cached_files';
$events['pre_update_option_icl_sitepress_settings'] = [ 'on_change_directory_for_default_language_clean_cache', 10, 2 ];
return $events;
}
/**
* Checks if page to be processed is secondary homepage.
*
* @param string $home_url home url.
* @param string $url url of current page.
* @return string
*/
public function is_secondary_home( string $home_url, string $url ): string {
global $sitepress;
// Get active languages on site.
$languages = $sitepress->get_active_languages();
foreach ( $languages as $lang ) {
$lang_url = $sitepress->language_url( $lang['code'] );
// Check if current url is a secondary homepage.
if ( untrailingslashit( $lang_url ) !== $url ) {
continue;
}
return $url;
}
return $home_url;
}
/**
* Add a condition to clean only urls from the domain when it is the case.
*
* @param string $condition condition used to clean URLS in the database.
* @return string
*/
public function clean_only_right_domain( $condition ) {
global $sitepress;
$lang = isset( $_GET['lang'] ) && 'all' !== $_GET['lang'] ? sanitize_key( $_GET['lang'] ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! $lang ) {
return $condition;
}
$lang_url = $sitepress->language_url( $lang );
return ' WHERE url LIKE "' . $lang_url . '%"';
}
/**
* Add sitemaps from translations.
*
* @param array $sitemaps list of sitemaps to be fetched.
* @return array
*/
public function add_languages_sitemaps( $sitemaps ) {
global $sitepress;
$new_sitemaps = [];
// Get active languages on site.
$languages = $sitepress->get_active_languages();
$base_url = home_url();
foreach ( $sitemaps as $sitemap ) {
$new_sitemaps[] = $sitemap;
foreach ( $languages as $lang ) {
$lang_url = $sitepress->language_url( $lang['code'] );
$new_sitemaps[] = str_replace( $base_url, $lang_url, $sitemap );
}
}
return array_unique( $new_sitemaps );
}
/**
* Remove root files when WPML is active.
*
* @return void
*/
public function remove_root_cached_files() {
$site_url = home_url();
$host_name = wp_parse_url( $site_url, PHP_URL_HOST );
$cache_folder_path = _rocket_get_wp_rocket_cache_path() . $host_name . '/';
$cache_folder_directory = $this->filesystem->dirlist( $cache_folder_path );
if ( ! is_array( $cache_folder_directory ) || ! is_array( array_keys( $cache_folder_directory ) ) ) {
return;
}
foreach ( array_keys( $cache_folder_directory ) as $entry ) {
if ( $this->filesystem->is_dir( $cache_folder_path . $entry ) ) {
continue;
}
$this->filesystem->delete( $cache_folder_path . $entry );
}
}
/**
* Reset cache when changing the option.
*
* @param array $new new configurations.
* @param array $old old configurations.
*
* @return array
*/
public function on_change_directory_for_default_language_clean_cache( $new, $old ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.newFound
if ( ! is_array( $old ) || ! is_array( $new ) ) {
return $new;
}
if ( ! key_exists( 'urls', $old ) || ! key_exists( 'directory_for_default_language', $old['urls'] ) || ! key_exists( 'urls', $new ) || ! key_exists( 'directory_for_default_language', $new['urls'] ) || $new['urls']['directory_for_default_language'] === $old['urls']['directory_for_default_language'] ) {
return $new;
}
/**
* Reset WP Rocket Preload.
*/
do_action( 'rocket_reset_preload' );
rocket_clean_domain();
return $new;
}
/**
* Clear the cache when the option language directory is enabled.
*
* @return void
*/
public function maybe_clear_on_disable() {
$option = get_option( 'icl_sitepress_settings' );
if ( ! $option || ! is_array( $option ) || ! key_exists( 'urls', $option ) || ! key_exists( 'directory_for_default_language', $option['urls'] ) || false === $option['urls']['directory_for_default_language'] ) {
return;
}
/**
* Reset WP Rocket Preload.
*/
do_action( 'rocket_reset_preload' );
rocket_clean_cache_dir();
rocket_clean_domain();
}
}