: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
class ET_Builder_Ajax_Cache {
* @var ET_Builder_Ajax_Cache
private static $_instance;
protected $_transient = 'et_builder_ajax_cache';
protected $_dirty = false;
* ET_Builder_Ajax_Cache constructor.
public function __construct() {
add_action( 'et_builder_ajax_cache_clear', array( $this, 'clear' ), 10, 1 );
add_action( 'shutdown', array( $this, 'save' ) );
add_filter( 'et_builder_dynamic_asset_deps', array( $this, 'add_cache_dep' ), 10, 2 );
* Returns whether cache file exists or not.
public function file_exists() {
$file = $this->get_file_name();
return $file && et_()->WPFS()->is_readable( $file );
* Returns whether cache is empty or not.
public function is_empty() {
return empty( $this->_cache );
* Enqueue ajax cache as definitions dependency.
* @param array $deps Dependencies array.
* @param string $key Script handle.
public function add_cache_dep( $deps, $key ) {
// Skip all static assets but definitions.
if ( 'et-dynamic-asset-definitions' !== $key ) {
if ( ! $this->file_exists() && ! $this->write_file() ) {
// Bail out if cache is empty and cannot write the file.
// Enqueue ajax cache as definitions dependency.
$handle = 'et-ajax-cache';
wp_register_script( $handle, $this->get_url(), array(), ET_BUILDER_VERSION );
if ( is_array( $this->_cache ) ) {
// Cache was already loaded
$this->_cache = get_transient( $this->_transient );
if ( ! is_array( $this->_cache ) ) {
// Ensure cache is loaded
set_transient( $this->_transient, $this->_cache );
public function write_file() {
if ( $this->is_empty() ) {
$file = $this->get_file_name();
foreach ( $this->_cache as $key => $value ) {
$cache = sprintf( '"%s":%s,', $key, $value );
$cache = sprintf( '{"ajaxCache":{%s}}', rtrim( $cache, ',' ) );
$cache = sprintf( 'window.ETBuilderBackend=jQuery.extend(true,%s,window.ETBuilderBackend)', $cache );
et_()->WPFS()->put_contents( $file, $cache );
return $this->file_exists();
public function delete_file() {
if ( $this->file_exists() ) {
et_()->WPFS()->delete( $this->get_file_name() );
* @param string $key Cache key.
* @param string $value Cache value.
public function set( $key, $content ) {
$this->_cache[ $key ] = wp_json_encode( $content );
* @param string $key Cache key.
public function unset_( $key ) {
if ( isset( $this->_cache[ $key ] ) ) {
unset( $this->_cache[ $key ] );
public function clear() {
delete_transient( $this->_transient );
public function get_file_name() {
// Per language Cache due to some data being localized.
$lang = is_admin() || et_fb_is_enabled() ? get_user_locale() : get_locale();
$lang = trim( sanitize_file_name( $lang ), '.' );
$cache = et_()->path( et_core_cache_dir()->path, $lang );
$files = glob( "{$cache}/{$prefix}-*.js" );
$exists = is_array( $files ) && $files;
$uniq = str_replace( '.', '', (string) microtime( true ) );
$file = "{$cache}/{$prefix}-{$uniq}.js";
return et_()->WPFS()->is_writable( dirname( $file ) ) ? $file : false;
public function get_url() {
$file = $this->get_file_name();
$lang = basename( dirname( $file ) );
$name = basename( $file );
return et_()->path( et_core_cache_dir()->url, $lang, $name );
* Get the class instance.
* @return ET_Builder_Ajax_Cache
public static function instance() {
if ( ! self::$_instance ) {
self::$_instance = new self;
ET_Builder_Ajax_Cache::instance();