-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha-faster-load-textdomain.php
127 lines (105 loc) · 4.35 KB
/
a-faster-load-textdomain.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Plugin Name: A faster load_textdomain
* Version: 2.3.2
* Description: Cache the .mo file as an PHP array, and load the array instead of the .mo file.
* Author: Per Soderlind
* Author URI: https://soderlind.no
* Plugin URI: https://github.com/soderlind/a-faster-load-textdomain
* License: GPLv2 or later
*
* @package a-faster-load-textdomain
*/
declare(strict_types=1);
namespace Soderlind\Plugin\A_Faster_Load_Textdomain;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
if ( ! \class_exists( 'AFLD_CacheHandler' ) ) {
require_once __DIR__ . '/includes/class-afld-cachehandler.php';
}
require_once __DIR__ . '/includes/class-translation-entry.php';
/**
* Load a text domain faster by caching the parsed .mo file data.
*
* @param bool $loaded Whether the text domain was loaded.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string $mofile Path to the .mo file.
* @param string $locale Optional. The locale to use. Default is null.
*
* @return bool Whether the text domain was loaded successfully.
*/
function a_faster_load_textdomain( $loaded, $domain, $mofile, $locale = null ) {
// Get the global $l10n variable.
global $l10n;
// Check if the .mo file is readable.
if ( ! is_readable( $mofile ) ) {
// If the file is not readable, return false.
return false;
}
// Apply filter to get cache path, default is 'WP_CONTENT_DIR/cache/a-faster-load-textdomain'.
$cache_path = apply_filters( 'a_faster_load_textdomain_cache_path', WP_CONTENT_DIR . '/cache/a-faster-load-textdomain' );
// Create a new instance of the CacheHandler class.
$cache_handler = new \AFLD_CacheHandler( $cache_path, 'mo' );
if ( $cache_handler->failed ) {
// If the cache directory could not be created, return false.
return false;
}
// Get the cached data for the MO file.
$data = $cache_handler->get_cache_data( $mofile );
// Get the last modification time of the MO file.
$mtime = filemtime( $mofile );
// Create a new instance of the MO class.
$mo = new \MO();
// If there's no cached data or the MO file has been modified since the last cache update...
if ( ! $data || ! isset( $data['mtime'] ) || $mtime > $data['mtime'] ) {
// ...try to import the MO file.
if ( ! $mo->import_from_file( $mofile ) ) {
return false;
}
// Prepare the data to be cached.
$data = [
'mtime' => $mtime, // The last modification time of the MO file.
'file' => $mofile, // The path to the MO file.
'entries' => $mo->entries, // The entries from the MO file.
'headers' => $mo->headers, // The headers from the MO file.
];
// Update the cached data.
$cache_handler->update_cache_data( $mofile, $data, __NAMESPACE__ . '\Translation_Entry' );
} else {
// If the cached data is up-to-date, use it to set the entries and headers of the MO object.
$mo->entries = $data['entries'];
$mo->headers = $data['headers'];
}
// Merge the translations with the existing translations for the domain.
if ( isset( $l10n[ $domain ] ) ) {
$mo->merge_with( $l10n[ $domain ] );
}
// Set the translations for the domain.
$l10n[ $domain ] = &$mo; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// Return true to indicate success.
return true;
}
if ( version_compare( $GLOBALS['wp_version'], '6.3' ) >= 0 ) {
\add_filter( 'pre_load_textdomain', __NAMESPACE__ . '\a_faster_load_textdomain', 1, 4 );
} else {
\add_filter( 'override_load_textdomain', __NAMESPACE__ . '\a_faster_load_textdomain', 0, 3 );
}
if ( version_compare( $GLOBALS['wp_version'], '6.5', '>=' ) ) {
// admin messages.
\add_action(
'admin_notices',
function () {
?>
<div class="notice notice-error">
<p><?php _e( '<strong>A faster load_textdomain</strong> is not needed in WordPress 6.5 and later, please deactivate the plugin.', 'a-faster-load-textdomain' ); ?></p>
<p><?php _e( 'The functionality is now <a href="https://make.wordpress.org/core/2024/02/27/i18n-improvements-6-5-performant-translations/">built into WordPress core</a>.', 'a-faster-load-textdomain' ); ?></p>
<p><?php _e( 'If you need to generate translation in the new <code>.l10n.php</code> format, use the <a href="https://wordpress.org/plugins/performant-translations/">Performant Translations</a> plugin', 'a-faster-load-textdomain' ); ?></p>
</div>
<?php
}
);
}