version 4.13.0
This commit is contained in:
212
core/components/cache/Directory.php
vendored
Normal file
212
core/components/cache/Directory.php
vendored
Normal file
@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
class ET_Core_Cache_Directory {
|
||||
|
||||
/**
|
||||
* @since 4.0.8
|
||||
* @var self
|
||||
*/
|
||||
protected static $_instance;
|
||||
|
||||
/**
|
||||
* Whether or not we can write to the cache directory.
|
||||
*
|
||||
* @since 4.0.8
|
||||
* @var bool
|
||||
*/
|
||||
public $can_write;
|
||||
|
||||
/**
|
||||
* Absolute path to cache directory
|
||||
*
|
||||
* @since 4.0.8
|
||||
* @var string
|
||||
*/
|
||||
public $path = '';
|
||||
|
||||
/**
|
||||
* URL for {@see self::$path}
|
||||
*
|
||||
* @since 4.0.8
|
||||
* @var string
|
||||
*/
|
||||
public $url = '';
|
||||
|
||||
/**
|
||||
* @since 4.0.8
|
||||
* @var WP_Filesystem_Base
|
||||
*/
|
||||
public $wpfs;
|
||||
|
||||
/**
|
||||
* ET_Core_Cache_Directory constructor.
|
||||
*
|
||||
* @since 4.0.8
|
||||
*/
|
||||
public function __construct() {
|
||||
if ( self::$_instance ) {
|
||||
et_wrong( 'Use "ET_Core_Cache_Directory::instance()" instead of "new ET_Core_Cache_Directory".' );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
self::$_instance = $this;
|
||||
|
||||
$this->_initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the cache directory path and url based on where we can write files
|
||||
* and whether or not the user has defined a custom path and url.
|
||||
*
|
||||
* @since 4.0.8
|
||||
*/
|
||||
protected function _initialize() {
|
||||
$this->_initialize_wpfs();
|
||||
|
||||
if ( $this->_maybe_use_custom_path() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$uploads_dir_info = (object) wp_get_upload_dir();
|
||||
$path = et_()->path( WP_CONTENT_DIR, 'et-cache' );
|
||||
$url = content_url( 'et-cache' );
|
||||
|
||||
$can_write = $this->wpfs->is_writable( $path ) && ! is_file( $path );
|
||||
$can_create = ! $can_write && $this->wpfs->is_writable( WP_CONTENT_DIR );
|
||||
|
||||
if ( ! $can_write && ! $can_create && $this->wpfs->is_writable( $uploads_dir_info->basedir ) ) {
|
||||
// We can create our cache directory in the uploads directory
|
||||
$can_create = true;
|
||||
$path = et_()->path( $uploads_dir_info->basedir, 'et-cache' );
|
||||
$url = et_()->path( $uploads_dir_info->baseurl, 'et-cache' );
|
||||
}
|
||||
|
||||
$this->can_write = $can_write || $can_create;
|
||||
$this->path = $path;
|
||||
$this->url = $url;
|
||||
|
||||
$this->_maybe_adjust_path_for_multisite( $uploads_dir_info );
|
||||
|
||||
/**
|
||||
* Absolute path to directory where we can store cache files.
|
||||
*
|
||||
* @since 4.0.8
|
||||
* @var string
|
||||
*/
|
||||
define( 'ET_CORE_CACHE_DIR', $this->path );
|
||||
|
||||
/**
|
||||
* URL to {@see ET_CORE_CACHE_DIR}.
|
||||
*
|
||||
* @since 4.0.8
|
||||
* @var string
|
||||
*/
|
||||
define( 'ET_CORE_CACHE_DIR_URL', $this->url );
|
||||
|
||||
$this->can_write && et_()->ensure_directory_exists( $this->path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the WP Filesystem API has been initialized.
|
||||
*
|
||||
* @since??
|
||||
*
|
||||
* @return WP_Filesystem_Base
|
||||
*/
|
||||
protected function _initialize_wpfs() {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
|
||||
if ( defined( 'ET_CORE_CACHE_DIR' ) && @WP_Filesystem( array(), ET_CORE_CACHE_DIR, true ) ) {
|
||||
// We can write to a user-specified directory
|
||||
return $this->wpfs = $GLOBALS['wp_filesystem'];
|
||||
}
|
||||
|
||||
if ( @WP_Filesystem( array(), false, true ) ) {
|
||||
// We can write to WP_CONTENT_DIR
|
||||
return $this->wpfs = $GLOBALS['wp_filesystem'];
|
||||
}
|
||||
|
||||
$uploads_dir = (object) wp_get_upload_dir();
|
||||
|
||||
if ( @WP_Filesystem( array(), $uploads_dir->basedir, true ) ) {
|
||||
// We can write to the uploads directory
|
||||
return $this->wpfs = $GLOBALS['wp_filesystem'];
|
||||
}
|
||||
|
||||
// We aren't able to write to the filesystem so let's just make sure $this->wpfs
|
||||
// is an instance of the filesystem base class so that calling it won't cause errors.
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
|
||||
|
||||
// Write notice to log when WP_DEBUG is enabled.
|
||||
$nl = PHP_EOL;
|
||||
$msg = 'Unable to write to filesystem. Please ensure that PHP has write access to one of ';
|
||||
$msg .= "the following directories:{$nl}{$nl}\t- WP_CONTENT_DIR{$nl}\t- wp_upload_dir(){$nl}\t- ET_CORE_CACHE_DIR.";
|
||||
|
||||
et_debug( $msg );
|
||||
|
||||
return $this->wpfs = new WP_Filesystem_Base;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the path for multisite if necessary.
|
||||
*
|
||||
* @since 4.0.8
|
||||
*
|
||||
* @param stdClass $uploads_dir_info (object) wp_get_upload_dir()
|
||||
*/
|
||||
protected function _maybe_adjust_path_for_multisite( $uploads_dir_info ) {
|
||||
if ( et_()->starts_with( $this->path, $uploads_dir_info->basedir ) || ! is_multisite() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$site = get_site();
|
||||
$network_id = $site->site_id;
|
||||
$site_id = $site->blog_id;
|
||||
|
||||
$this->path = et_()->path( $this->path, $network_id, $site_id );
|
||||
$this->url = et_()->path( $this->url, $network_id, $site_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the user has defined a custom path for the cache directory.
|
||||
*
|
||||
* @since 4.0.8
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function _maybe_use_custom_path() {
|
||||
if ( ! defined( 'ET_CORE_CACHE_DIR' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->path = ET_CORE_CACHE_DIR;
|
||||
|
||||
if ( ! $this->can_write = $this->wpfs->is_writable( $this->path ) ) {
|
||||
et_wrong( 'ET_CORE_CACHE_DIR is defined but not writable.', true );
|
||||
}
|
||||
|
||||
if ( defined( 'ET_CORE_CACHE_DIR_URL' ) ) {
|
||||
$this->url = ET_CORE_CACHE_DIR_URL;
|
||||
} else {
|
||||
et_wrong( 'When ET_CORE_CACHE_DIR is defined, ET_CORE_CACHE_DIR_URL must also be defined.', true );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the class instance.
|
||||
*
|
||||
* @since 4.0.8
|
||||
*
|
||||
* @return ET_Core_Cache_Directory
|
||||
*/
|
||||
public static function instance() {
|
||||
if ( ! self::$_instance ) {
|
||||
self::$_instance = new self;
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
}
|
148
core/components/cache/File.php
vendored
Normal file
148
core/components/cache/File.php
vendored
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/**
|
||||
* Core class that implements an file cache.
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* The Object Cache stores all of the cache data to file and makes the cache
|
||||
* contents available by using a file name as key, which is used to name and later retrieve
|
||||
* the cache contents.
|
||||
*
|
||||
* @package ET\Core\Cache_File
|
||||
*/
|
||||
class ET_Core_Cache_File {
|
||||
|
||||
/**
|
||||
* Cached data holder.
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $_cache = array();
|
||||
|
||||
/**
|
||||
* Loaded cache file data.
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $_cache_loaded = array();
|
||||
|
||||
/**
|
||||
* Cached data status.
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $_dirty = array();
|
||||
|
||||
/**
|
||||
* Sets the data contents into the cache.
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* @param string $cache_name What is the file name that storing the cache data.
|
||||
* @param mixed $data The cache data to be set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function set( $cache_name, $data ) {
|
||||
if ( self::is_disabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
self::$_cache[ $cache_name ] = $data;
|
||||
self::$_dirty[ $cache_name ] = $cache_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the cache contents, if it exists.
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* @param string $cache_name What is the file name that storing the cache data.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get( $cache_name ) {
|
||||
if ( self::is_disabled() ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if ( ! isset( self::$_cache_loaded[ $cache_name ] ) ) {
|
||||
$file = self::get_cache_file_name( $cache_name );
|
||||
|
||||
if ( et_()->WPFS()->is_readable( $file ) ) {
|
||||
self::$_cache[ $cache_name ] = unserialize( et_()->WPFS()->get_contents( $file ) );
|
||||
} else {
|
||||
self::$_cache[ $cache_name ] = array();
|
||||
}
|
||||
|
||||
self::$_cache_loaded[ $cache_name ] = true;
|
||||
}
|
||||
|
||||
return isset( self::$_cache[ $cache_name ] ) ? self::$_cache[ $cache_name ] : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves Cache.
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function save_cache() {
|
||||
if ( self::is_disabled() || ! self::$_dirty || ! self::$_cache ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( self::$_dirty as $cache_name ) {
|
||||
if ( ! isset( self::$_cache[ $cache_name ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = self::$_cache[ $cache_name ];
|
||||
$file = self::get_cache_file_name( $cache_name );
|
||||
|
||||
if ( ! wp_is_writable( dirname( $file ) ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
et_()->WPFS()->put_contents( $file, serialize( $data ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get full path of cache file name.
|
||||
*
|
||||
* The file name will be suffixed with .data
|
||||
*
|
||||
* @since 3.27.3
|
||||
*
|
||||
* @param string $cache_name What is the file name that storing the cache data.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_cache_file_name( $cache_name ) {
|
||||
return sprintf( '%1$s/%2$s.data', ET_Core_PageResource::get_cache_directory(), $cache_name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is file based caching is disabled.
|
||||
*
|
||||
* @since 4.0.8
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_disabled() {
|
||||
return defined( 'ET_DISABLE_FILE_BASED_CACHE' ) && ET_DISABLE_FILE_BASED_CACHE;
|
||||
}
|
||||
}
|
||||
|
||||
// Hook the shutdown action once.
|
||||
if ( ! has_action( 'shutdown', 'ET_Core_Cache_File::save_cache' ) ) {
|
||||
add_action( 'shutdown', 'ET_Core_Cache_File::save_cache' );
|
||||
}
|
12
core/components/cache/init.php
vendored
Normal file
12
core/components/cache/init.php
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'et_core_cache_init' ) ):
|
||||
function et_core_cache_init() {}
|
||||
endif;
|
||||
|
||||
|
||||
if ( ! defined( 'et_core_cache_dir' ) ):
|
||||
function et_core_cache_dir() {
|
||||
return ET_Core_Cache_Directory::instance();
|
||||
}
|
||||
endif;
|
Reference in New Issue
Block a user