OllieJones / sqlite-object-cache

A WordPress persistent object cache for the rest of us.
GNU General Public License v2.0
24 stars 4 forks source link

Sometimes need to load __() i18n functions. #11

Closed OllieJones closed 1 year ago

OllieJones commented 1 year ago

Under some circumstances, the drop-in needs to use i18n functions before the rest of WordPress is loaded. Props to @carstenbach.

spacedmonkey commented 1 year ago

You need to call wp_load_translations_early. See the following example from core.

https://github.com/WordPress/wordpress-develop/blob/0cb8475c0d07d23893b1d73d755eda5f12024585/src/wp-includes/class-wp-fatal-error-handler.php#L173-L175

carstingaxion commented 1 year ago

:+1:

OllieJones commented 1 year ago

All set.

spacedmonkey commented 1 year ago

I am not seeing a different error here.


  | <br />
-- | --
  | <b>Fatal error</b>:  Uncaught Error: Call to a member function get() on null in /var/www/src/wp-content/object-cache.php:2155
  | Stack trace:
  | #0 /var/www/src/wp-includes/option.php(165): wp_cache_get('notoptions', 'options')
  | #1 /var/www/src/wp-includes/l10n.php(63): get_option('WPLANG')
  | #2 /var/www/src/wp-includes/l10n.php(139): get_locale()
  | #3 /var/www/src/wp-includes/l10n.php(1274): determine_locale()
  | #4 /var/www/src/wp-includes/l10n.php(1305): _load_textdomain_just_in_time('sqlite-object-c...')
  | #5 /var/www/src/wp-includes/l10n.php(187): get_translations_for_domain('sqlite-object-c...')
  | #6 /var/www/src/wp-includes/l10n.php(299): translate('The SQLite Obje...', 'sqlite-object-c...')
  | #7 /var/www/src/wp-content/object-cache.php(726): __('The SQLite Obje...', 'sqlite-object-c...')
  | #8 /var/www/src/wp-content/object-cache.php(2012): WP_Object_Cache::has_sqlite()
  | #9 /var/www/src/wp-includes/load.php(730): wp_cache_init()
  | #10 /var/www/src/wp-settings.php(131): wp_start_object_cache()
  | #11 /var/www/wp-config.php(109): require_once('/var/www/src/wp...')
  | #12 /var/www/src/wp-load.php(55): require_once('/var/www/wp-con...')
  | #13 /var/www/src/wp-blog-header.php(13): require_once('/var/www/src/wp...')
  | #14 /var/www/src/_index.php(17): require('/var/www/src/wp...')
  | #15 /var/www/src/index.php(19): require_once('/var/www/src/_i...')
  | #16 {main}
  | thrown in <b>/var/www/src/wp-content/object-cache.php</b> on line <b>2155</b><br />

The fix is simple.

    function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
        global $wp_object_cache;

                if( ! $wp_object_cache ){
                    return false;
                }

        return $wp_object_cache->get( $key, $group, $force, $found );
    }

We need to do the same for wp_cache_get, wp_cache_set and wp_cache_add.

CC @OllieJones