{"id":13534719,"url":"https://github.com/inpsyde/translation-cache","last_synced_at":"2025-04-02T00:30:38.958Z","repository":{"id":56991896,"uuid":"72410213","full_name":"inpsyde/translation-cache","owner":"inpsyde","description":"Improves site performance by caching translation files using WordPress object cache.","archived":true,"fork":false,"pushed_at":"2024-03-05T13:21:35.000Z","size":40,"stargazers_count":42,"open_issues_count":2,"forks_count":4,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-15T20:33:33.613Z","etag":null,"topics":["cache","translation","translation-files","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inpsyde.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-31T06:45:59.000Z","updated_at":"2024-07-17T12:46:42.000Z","dependencies_parsed_at":"2024-11-02T22:40:56.652Z","dependency_job_id":null,"html_url":"https://github.com/inpsyde/translation-cache","commit_stats":{"total_commits":18,"total_committers":2,"mean_commits":9.0,"dds":0.2777777777777778,"last_synced_commit":"eddff8a46489c133c8c68d2aa22fb3a9d0c92e5c"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpsyde%2Ftranslation-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpsyde%2Ftranslation-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpsyde%2Ftranslation-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inpsyde%2Ftranslation-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inpsyde","download_url":"https://codeload.github.com/inpsyde/translation-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246734863,"owners_count":20825210,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","translation","translation-files","wordpress"],"created_at":"2024-08-01T08:00:37.436Z","updated_at":"2025-04-02T00:30:38.673Z","avatar_url":"https://github.com/inpsyde.png","language":"PHP","funding_links":[],"categories":["Caching Helping Plugins"],"sub_categories":[],"readme":"Inpsyde Translation Cache\n=========================\n\n\u003e Improves site performance by caching translation files using WordPress object cache.\n\n----\n\n## Description\n\n_'Inpsyde Translation Cache'_ provides caching of the translation `.mo` files using [WP Object Cache](http://codex.wordpress.org/Class_Reference/WP_Object_Cache) \nmechanism.\n \nPerformance benefit of the plugin can be seen when using [persistent cache plugin](http://codex.wordpress.org/Class_Reference/WP_Object_Cache#Persistent_Cache_Plugins)\nbecause, by default, WordPress object cache does not 'survive' the single request and translation files are _already_\ncached on a per-request basis in WP.\n\nFor this reason **the plugin, by default, does nothing when there's no persistent caching plugin installed**.\n\n--------\n\n## MU plugin self-installation\n\nConsidering that many plugins have their language files, to be allowed to cache plugins translations, _'Inpsyde Translation Cache'_\nhas to run as [MU plugin](https://codex.wordpress.org/Must_Use_Plugins).\n\nFor this reason, **_'Inpsyde Translation Cache'_ copies its main file to the MU plugin folder on installation**, \nso it can work as MU plugin.\n\nThe 'regular' plugin stays active though, but doing nothing, or better, it listed for deactivation to delete the MU\nplugin copy.\n\nIn fact, when 'regular' plugin is deactivated, the MU plugin copy is deleted (unless it was modified).\n\nPlease note that on some configurations this 'self-installation' routine may not work, e.g. if MU plugins folder is not\nwritable.\n\nIn those cases _'Inpsyde Translation Cache'_ will continue to work as a regular plugin, but it will not be able to cache\ntranslation files loaded before `'plugins_loaded'` hook is fired. Moreover, an admin notice is also shown to suggest users\n to manually copy main plugin file to MU plugins folder to improve performance.\n\n--------\n\n## Cache invalidation\n\n_'Inpsyde Translation Cache'_ stores all the text domains:\n\n- core\n- plugins\n- themes\n\nit means that when a new version of those is installed, the cache need to cleared.\n\n### Automatic invalidation\n\nFor invalidation of WordPress core translations the plugin relies on WordPress version: if that change all cached core \ntranslations are invalidated.\n\nFor plugin and themes, things are less straightforward.\n\n_'Inpsyde Translation Cache'_ uses [`'switch_theme'`](https://developer.wordpress.org/reference/functions/switch_theme/) \nhook to invalidate cache of both the 'old' and the 'new' theme.\n\nIt also uses [`'activated_plugin'`](https://developer.wordpress.org/reference/hooks/activated_plugin/) and\n [`'deactivated_plugin'`](https://developer.wordpress.org/reference/hooks/deactivated_plugin/) to invalidate cache for \n plugins.\n\nHowever, to invalidate cache of themes and plugins _'Inpsyde Translation Cache'_ needs to know the text domain they use.\n\nFor that scope, _'Inpsyde Translation Cache'_ uses 'TextDomain' file header of plugin and themes.\n\nConsidering that 'TextDomain' file header is not mandatory, it is possible that some plugins or themes that don't provide \nthat header (even if they should).\n\n### Manual invalidation\n \nIn those cases it is possible to invalidate the cache 'manually' by calling `Inpsyde\\MoCache::flush_cache()` method.\n\nFor example, let's assume there's a plugin that loads a text domain like this:\n\n```php\n load_plugin_textdomain( 'some-plugin-txt-domain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); \n```\n\nand let's assume this plugin has **not** in plugin headers something like:\n\n```\nText Domain: some-plugin-txt-domain\n```\n\n_'Inpsyde Translation Cache'_ will not be able to flush cache for that plugin automatically when activated and deactivated.\n\nHowever, 'manually' invalidating cache for that plugin would be just a matter of:\n\n```php\nfunction invalidate_some_plugin_translation_cache() {\n\tif ( class_exists( 'Inpsyde\\TranslationCache\\MoCache' ) ) {\n\t\tInpsyde\\TranslationCache\\MoCache::flush_cache( 'some-plugin-txt-domain' );\n    }\n}\n \nregister_activation_hook(\n    WP_PLUGIN_DIR . '/some-plugin-dir/some-plugin-filename.php',\n    'invalidate_some_plugin_translation_cache'\n);\n\nregister_deactivation_hook(\n    WP_PLUGIN_DIR . '/some-plugin-dir/some-plugin-filename.php',\n    'invalidate_some_plugin_translation_cache'\n);\n```\n\nOf course, when it is possible to edit the plugin to include the `Text Domain` plugin header, it is surely better,\nbecause _'Inpsyde Translation Cache'_ will be able to invalidate the cache for plugin translations automatically.\n\nFor themes the workflow is very similar, just use `'switch_theme'` hook to intercept theme change.\n\n### Invalidation by version\n\nThere's another way to invalidate cached translations.\n\n_'Inpsyde Translation Cache'_ builds an unique key for the combination of text domain and `.mo` file path.\n\nThis unique key is built using an 'hash' of:\n\n- text domain\n- `.mo` file path\n- _'Inpsyde Translation Cache'_ own version\n- an arbitrary 'version' string that can be set via `mocache_cache_version` filter\n\nIt means that using `mocache_cache_version` filter it is possible to invalidate all the stored keys for a \ngiven text domain.\n\nFor example, assuming same plugin as above we could do:\n\n```php\nadd_filter( 'mocache_cache_version', function( $version, $domain ) {\n    $plugin_path = WP_PLUGIN_DIR . '/some-plugin-dir/some-plugin-filename.php';\n    $headers     = get_plugin_data( $plugin_file_path, FALSE, FALSE );\n    \n    return $headers['Version'];\n}, 10, 2 );\n```\n\nSo we use the 'Version' plugin header to invalidate the cache whenever the version change.\n\nCompared to the 'manual' approach above, this approach has the benefit to do not explicitly call _'Inpsyde Translation Cache'_\n objects, so there's no need to check for class existence, in fact, if _'Inpsyde Translation Cache'_ is not installed or \n not activated, then the code does nothing with no effect.\n\nBut it also has two flaws:\n\n- Cache entries for 'old' versions are **not** deleted, they are just ignored because there's a new version.\n  However, they will be discarded when 'natural' expiration for keys is reached and considering that, by default, the\n  'natural' expiration is 12 hours, this issue should not be that critical.\n- A code like the one above requires the plugin to define the version in headers and to be effective it relies on the \n  version be updated every time any translation file is changed.\n  Even if these are common practices, theme / plugins authors may just ignore or forget them.\n\nSo, again, the suggestion is to use `Text Domain` header whenever possible, maybe suggesting to maintainers of \nthird party plugins and themes to add it when not present.\n\n--------\n\n## Available hooks\n\n_'Inpsyde Translation Cache'_ provides some hooks for customization. Most of them will only work properly from a MU plugin, \nespecially if _'Inpsyde Translation Cache'_ is running as MU plugin.\n\nAvailable **action** hooks:\n\n- **`inpsyde_translation_cache`**, Fires just after the plugin class has been loaded.\n   Useful to wrap calls to `Inpsyde\\TranslationCache\\MoCache::flush_cache()`\n   \nAvailable **filter** hooks:\n\n- **`mocache_cache_enabled`** Can be used to disable plugin programmatically returning `false` via hooked callback.\n  \n- **`mocache_cache_version`** Can be used to invalidate the cache for given text domain (passed as argument)\n  If returned version change, cache is invalidated. See _' Invalidation by version'_ above.\n \n- **`mocache_cache_expire`** Can be used to customize the duration in seconds of cached values. Returning a value\n  less or equal to zero prevent cache to be done at all.\n\n--------\n\n## Installation\n\n_'Inpsyde Translation Cache'_ is available via Composer with package name **`inpsyde/mo-cache`**, but it does not\nrequire Composer to be installed or used.\n\nThe 'classical' installation method (_download_ -\u003e _put in plugins folder_ -\u003e _activate_) works as well.\n\n--------\n\n## Requirements\n\n * PHP 5.5 or higher.\n * WordPress, tested currently in version 5.1\n \n--------\n\n## License\n\nThis repository is a free software, and is released under the terms of the GNU General Public License version 2 or (at your option) any later version. See [LICENSE](./LICENSE) for complete license.\n\n_'Inpsyde Translation Cache'_ incorporates work covered by the following copyright and\npermission notices:\n\n - ['MO Cache' WordPress plugin](https://wordpress.org/plugins/mo-cache/),\n   Copyright (c) Masaki Takeuchi (m4i)\n   Released under the MIT.\n\n\n```\n  ___                           _      \n |_ _|_ __  _ __  ___ _   _  __| | ___ \n  | || '_ \\| '_ \\/ __| | | |/ _` |/ _ \\\n  | || | | | |_) \\__ \\ |_| | (_| |  __/\n |___|_| |_| .__/|___/\\__, |\\__,_|\\___|\n           |_|        |___/            \n```\n\nThe team at [Inpsyde](https://inpsyde.com) is engineering the Web since 2006.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finpsyde%2Ftranslation-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finpsyde%2Ftranslation-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finpsyde%2Ftranslation-cache/lists"}