| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\EditorOne\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Top_Bar_Locale_Assets { |
| 10 |
|
| 11 |
private const LOCALES_RELATIVE_DIRECTORY = 'js/locales/elementor-one-assets/'; |
| 12 |
private const SCRIPT_HANDLE_PREFIX = 'elementor-one-top-bar-locale-'; |
| 13 |
|
| 14 |
public static function register_script_loader_filter(): void { |
| 15 |
add_filter( 'script_loader_tag', [ self::class, 'filter_locale_json_script_tag' ], 10, 3 ); |
| 16 |
} |
| 17 |
|
| 18 |
public static function filter_locale_json_script_tag( string $tag, string $handle, string $src ): string { |
| 19 |
if ( ! str_starts_with( $handle, self::SCRIPT_HANDLE_PREFIX ) ) { |
| 20 |
return $tag; |
| 21 |
} |
| 22 |
|
| 23 |
return sprintf( |
| 24 |
'<link rel="prefetch" href="%1$s" as="fetch" crossorigin="anonymous" id="%2$s-prefetch" />', |
| 25 |
esc_url( $src ), |
| 26 |
esc_attr( $handle ) |
| 27 |
) . "\n"; |
| 28 |
} |
| 29 |
|
| 30 |
public static function enqueue_for_languages( array $language_codes ): array { |
| 31 |
$language_base_urls = []; |
| 32 |
|
| 33 |
foreach ( $language_codes as $language_code ) { |
| 34 |
$language_directory = ELEMENTOR_ASSETS_PATH . self::LOCALES_RELATIVE_DIRECTORY . $language_code; |
| 35 |
|
| 36 |
if ( ! is_dir( $language_directory ) ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$language_base_urls[ $language_code ] = ELEMENTOR_ASSETS_URL . self::LOCALES_RELATIVE_DIRECTORY . $language_code . '/'; |
| 41 |
|
| 42 |
$locale_files = glob( $language_directory . '/*.json' ); |
| 43 |
|
| 44 |
if ( ! is_array( $locale_files ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
foreach ( $locale_files as $locale_file_path ) { |
| 49 |
$namespace = basename( $locale_file_path, '.json' ); |
| 50 |
$handle = self::SCRIPT_HANDLE_PREFIX . $language_code . '-' . $namespace; |
| 51 |
$url = $language_base_urls[ $language_code ] . $namespace . '.json'; |
| 52 |
|
| 53 |
wp_enqueue_script( |
| 54 |
$handle, |
| 55 |
$url, |
| 56 |
[ 'editor-one-top-bar' ], |
| 57 |
ELEMENTOR_VERSION, |
| 58 |
true |
| 59 |
); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $language_base_urls; |
| 64 |
} |
| 65 |
} |
| 66 |
|