Skip to content

Commit

Permalink
Use pre_load_textdomain (#5)
Browse files Browse the repository at this point in the history
* Use pre_load_textdomain

* Update type of first argument, return true by default

* Bump version and require at least WordPress 6.3
  • Loading branch information
ocean90 authored Sep 26, 2023
1 parent 7a66855 commit f837613
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "2.x-dev"
}
},
"scripts": {
Expand Down
41 changes: 18 additions & 23 deletions translations-cache.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php // phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
/**
* Plugin Name: Translations Cache
* Plugin URI: https://github.com/wearerequired/translations-cache
* Description: Reduces file reads for translations by caching the first read via APCu.
* Version: 1.1.0
* Author: required
* Author URI: https://required.com/
* License: GPL-2.0+
* Version: 2.0.0-beta.1
* Requires at least: 6.3
* Requires PHP: 7.4
* Author: required
* Author URI: https://required.com/
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Update URI: false
* Update URI: false
*/

declare( strict_types=1 );
Expand Down Expand Up @@ -81,16 +84,16 @@ function load_script_translations( $translations, $file, string $handle, string
/**
* Caches reading gettext translation files.
*
* @param bool $override Whether to override the .mo file loading. Default false.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string $mofile Path to the MO file.
* @param string $locale Optional. Locale. Default is the current locale.
* @param bool|null $loaded The result of loading a .mo file. Default null.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string $mofile Path to the MO file.
* @param string|null $locale Locale.
* @return bool True if the .mo file was loaded, false otherwise.
*/
function load_textdomain( bool $override, string $domain, string $mofile, ?string $locale = null ): bool {
function load_textdomain( ?bool $loaded, string $domain, string $mofile, ?string $locale = null ): bool {
// Another plugin has already overridden the loading.
if ( false !== $override ) {
return $override;
if ( null !== $loaded ) {
return $loaded;
}

/** @var \WP_Textdomain_Registry $wp_textdomain_registry */
Expand All @@ -110,11 +113,9 @@ function load_textdomain( bool $override, string $domain, string $mofile, ?strin
$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Core filter.

if ( ! is_readable( $mofile ) ) {
// Cache the result but still return false to not prevent
// looking up translations in the plugin/theme directory.
cache_add( $cache_key, false, DEFAULT_EXPIRE );

return false;
return true;
}

$mo = new \MO();
Expand All @@ -124,7 +125,6 @@ function load_textdomain( bool $override, string $domain, string $mofile, ?strin
// Use a short cache time to avoid repeated failed lookups.
cache_add( $cache_key, false, HOUR_IN_SECONDS );

// Return true since we still override the .mo file loading.
return true;
}

Expand All @@ -142,8 +142,6 @@ function load_textdomain( bool $override, string $domain, string $mofile, ?strin
}

$l10n[ $domain ] = &$mo; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

return true;
} elseif ( \is_array( $data ) ) { // false if a mo file was not read/found.
$mo = new \MO();
$mo->entries = $data['entries'];
Expand All @@ -154,14 +152,11 @@ function load_textdomain( bool $override, string $domain, string $mofile, ?strin
}

$l10n[ $domain ] = &$mo; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

return true;
}

// Return false since we had nothing to/in cache.
return false;
return true;
}
add_filter( 'override_load_textdomain', __NAMESPACE__ . '\load_textdomain', 9999, 4 );
add_filter( 'pre_load_textdomain', __NAMESPACE__ . '\load_textdomain', 9999, 4 );

/**
* Caches data to APCu, only if it's not already stored.
Expand Down

0 comments on commit f837613

Please sign in to comment.