: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$copy = empty($content) || strpos($content, 'class-themify-cache.php', 10) === false;
if ($copy === false && md5($content) !== md5_file($fw_dir . 'advanced-cache.php')) {
if ($copy === true && !copy($fw_dir . 'advanced-cache.php', $cache_dir . 'advanced-cache.php')) {
Themify_Filesystem::delete($cache_config, 'f');
return sprintf(__('Can`t copy %s to %s. Please check permission or do manually it.', 'themify'), $fw_dir . 'advanced-cache.php', $cache_dir . 'advanced-cache.php');
return self::write_wp_config();
return __('Themify Cache can not be enabled due to another cache plugin is activated.', 'themify');
return sprintf(__('Folder %s isn`t writable.Please check permission to allow write cache.', 'themify'), $cache_dir);
public static function get_wp_content_dir():string {
return rtrim(WP_CONTENT_DIR, self::SEP) . self::SEP;
public static function get_cache_main_dir():string {
return self::get_wp_content_dir() . 'tf_cache' . self::SEP;
public static function get_cache_blog_dir($blog_id = false):string {
$dir = self::get_cache_main_dir();
if ($blog_id === false) {
$bid = get_current_blog_id();
$dir .= $bid . self::SEP;
$dir .= $blog_id . self::SEP;
public static function get_cache_folder(string $request,bool $create = false):string {
$dir = explode('?', $request);
//group the files in directory by the pre last slash(e.g /blog/slug return blog,2014/06/09/slug return 2014/06/09/)
$domain = apply_filters('site_url', get_option('siteurl'), '', null, null);
$domain = parse_url($dir);
$domain = isset($domain['host']) ? $domain['host'] : '';
$scheme = is_ssl() ? 'https' : 'http';
$domain = str_replace(array('https:', 'http:'), '', trim($domain));
$domain = $scheme . '://' . trim(ltrim($domain, '//'));
$domain = trim(strtr($dir, array($domain => '')), '/');
} elseif (strpos($domain, '/') !== false) {
$domain = explode('/', $domain);
$dir = implode('/', $domain);
$blog_dir = self::get_cache_blog_dir() . md5($dir);
Themify_Filesystem::mkdir($blog_dir, true);
return $blog_dir . self::SEP;
public static function admin_check() {
if (false !== self::get_cache_plugins()) {
public static function disable_cache() {
$cache_dir = self::get_wp_content_dir();
$config_f = self::get_cache_config_file();
Themify_Filesystem::delete($config_f, 'f');
$config_f = $cache_dir . 'advanced-cache.php';
if (Themify_Filesystem::is_file($config_f)) {
$content = Themify_Filesystem::get_contents($config_f);
$remove = !empty($content) && strpos($content, 'class-themify-cache.php', 10) !== false;
if ($remove === true) {//only when advanced-cache.php belongs to us or file doesn't exist try to disable WP_CACHE
$wp_config = ABSPATH . 'wp-config.php';
if (Themify_Filesystem::is_writable($wp_config)) {
$content = Themify_Filesystem::get_contents($wp_config);
$content = str_replace(array(self::get_replace_str(), "define('WP_CACHE',true);"), '', $content);
if (strpos($content, 'Themify Cache', 2) !== false) {//try again
$content = preg_replace('/define/', self::get_replace_str() . PHP_EOL . PHP_EOL . 'define', $content, 1);
if (!file_put_contents($wp_config, $content, LOCK_EX)) {
$remove = false; //otherwise will give error file doesn't exist,it's safe to keep it
Themify_Filesystem::delete($config_f, 'f');
private static function get_replace_str():string {
$replace = '/* Themify Cache Start */' . PHP_EOL;
$replace .= "define('WP_CACHE',true);";
$replace .= PHP_EOL . '/* Themify Cache End */';
public static function ajax_write_wp_cache() {
check_ajax_referer('tf_nonce', 'nonce');
if ( ! current_user_can( 'manage_options' ) ) {
if (!empty($_POST['data'])) {
$data = themify_normalize_save_data($_POST['data']);
$msg = self::create_config($data);
die(json_encode(array('remove_after' => 1)));
die(json_encode(array('error' => $msg)));
public static function write_wp_config() {
$cache_dir = self::get_wp_content_dir();
$wp_config = ABSPATH . 'wp-config.php';
if (Themify_Filesystem::is_writable($wp_config)) {
if (Themify_Filesystem::is_file(self::get_cache_config_file()) && Themify_Filesystem::is_file($cache_dir . 'advanced-cache.php')) {
$content = Themify_Filesystem::get_contents($wp_config);
$str = self::get_replace_str();
if (!empty($content) && strpos($content, $str, 3) === false) {
$content = preg_replace('/define/', $str . PHP_EOL . PHP_EOL . 'define', $content, 1);
if (file_put_contents($wp_config, $content, LOCK_EX)) {
return sprintf(__('File %s is`t writable. Please add %s %s.', 'themify'), $wp_config, "define('WP_CACHE',true)", $wp_config);
} elseif (!Themify_Filesystem::is_file(self::get_cache_config_file())) {
public static function get_cache_config_file():string {
$fname .= '-' . get_current_blog_id();
$dir = self::get_wp_content_dir() . 'tf_cache_config';
Themify_Filesystem::mkdir($dir, true,0755);
return $dir . self::SEP . $fname;
public static function cache_menu($wp_admin_bar) {
if (!current_user_can('manage_options')) {
$link = remove_query_arg(['tf-cache','nonce'], self::get_current_url());
$isDevmode = themify_is_dev_mode();
'id' => 'tf_clear_cache',
'title' => __('Themify Cache', 'themify')
$nonce= wp_create_nonce('tf_cache');
$cache_plugins = false !== self::get_cache_plugins();
$hasCache = WP_CACHE && $cache_plugins === false && Themify_Filesystem::is_file(self::get_cache_config_file());
if ($isDevmode === true) {
$args[0]['id'] = 'tf_dev_mode';
$args[0]['title'] = '<span class="tf_admin_bar_tooltip">' . __('Warning: Dev Mode is enabled (Themify cache, menu cache, concate cache and .gz are disabled). Only enable this for development purposes.', 'themify') . '</span>' . esc_html__('Dev Mode', 'themify');
$args[0]['meta'] = array('class' => 'tf_admin_bar_alert');
'id' => 'tf_disable_dev',
'parent' => $args[0]['id'],
'href' => add_query_arg(array('tf-cache' => 4,'nonce'=>$nonce), $link),
'title' => __('Disable Dev Mode', 'themify')
if (isset($_GET['tf-cache'],$_GET['nonce']) && wp_verify_nonce($_GET['nonce'],'tf_cache')) {
$cache_type = (int) $_GET['tf-cache'];
themify_clear_menu_cache();
elseif ($cache_type === 1) {
add_filter('themify_concate_css', '__return_false');
if ($hasCache === true) {
$link = self::get_current_cache($link);
Themify_Filesystem::delete($link . '_safari.html', 'f');
Themify_Filesystem::delete($link . '_safari.html.gz', 'f');
Themify_Filesystem::delete($link, 'f');
Themify_Filesystem::delete($link, 'f');
themify_clear_menu_cache();
'parent' => 'tf_clear_cache',
'href' => add_query_arg(array('tf-cache' => 1,'nonce'=>$nonce), $link),
'title' => $hasCache === true ? __('Purge Page Cache', 'themify') : __('Regenerate Page CSS', 'themify')
'parent' => 'tf_clear_cache',
'href' => add_query_arg(array('tf-cache' => 2,'nonce'=>$nonce), $link),
'title' => $hasCache === true ? __('Purge All Cache', 'themify') : __('Regenerate All CSS', 'themify')
if ($hasCache === false && $cache_plugins === false && !themify_check('setting-cache-menu', true)) {
'parent' => 'tf_clear_cache',
'href' => add_query_arg(array('tf-cache' => 3,'nonce'=>$nonce), $link),
'title' => __('Clear Menu Cache', 'themify')
foreach ($args as $arg) {
$wp_admin_bar->add_node($arg);
public static function check_clear() {
if (isset($_GET['nonce']) && wp_verify_nonce($_GET['nonce'],'tf_cache') && current_user_can('manage_options')) {
if ($_GET['tf-cache'] === '2') {
Themify_Enqueue_Assets::clearConcateCss();
themify_clear_menu_cache();
$tmp = themify_get_data();
unset($tmp['setting-dev-mode']);
$link = remove_query_arg(['tf-cache','nonce'], self::get_current_url());
if (wp_redirect($link)) {
public static function clear_3rd_plugins_cache($post_id = 0) {
$cache_plugins = self::get_cache_plugins('others');
if (false === $cache_plugins) {
$post_id = (int) $post_id <= 0 ? 0 : (int) $post_id;
// Sometimes we need to clear all caches ex. when Pro template or Layout Part is edited
$type = get_post_type($post_id);
$post_id = 'post' === $type || 'page' === $type ? $post_id : 0;
foreach ($cache_plugins as $k => $v) {
wp_cache_post_change($post_id);
w3tc_flush_post($post_id);
wpfc_clear_post_cache_by_id($post_id);
wpfc_clear_all_cache(true);
autoptimizeCache::clearall();
WPO_Page_Cache::delete_single_post_cache($post_id);
WP_Optimize()->get_page_cache()->purge();
do_action('litespeed_purge_post', $post_id);
do_action('litespeed_purge_all');
do_action('wphb_clear_page_cache', $post_id);
do_action('wphb_clear_page_cache');
// Cloudflare use this hook to purge the cache
do_action('autoptimize_action_cachepurged');
$post_id = $post_id > 0 ? get_permalink($post_id) : false;
if ($post_id !== false) {
sg_cachepress_purge_cache($post_id);
sg_cachepress_purge_cache();
do_action('breeze_clear_all_cache');
rocket_clean_post($post_id);
comet_cache::clearPost($post_id);
do_action('cache_enabler_clear_page_cache_by_post', $post_id);
do_action('cache_enabler_clear_site_cache');
if (method_exists('WpeCommon', 'purge_memcached')) {
WpeCommon::purge_memcached($post_id);
if (method_exists('WpeCommon', 'clear_maxcdn_cache')) {
WpeCommon::clear_maxcdn_cache($post_id);
if (method_exists('WpeCommon', 'purge_varnish_cache')) {
WpeCommon::purge_varnish_cache($post_id);
do_action('cachify_remove_post_cache', $post_id);
do_action('cachify_flush_cache');
rapidcache_clear_post_cache($post_id);
rapidcache_clear_cache();
Swift_Performance_Cache::clear_post_cache($post_id);
Swift_Performance_Cache::clear_all_cache();
$nginx_cache = new NginxCache();
$nginx_cache->purge_zone_once();
public static function get_cache_plugins($slug = 'others') {
if (function_exists('w3tc_flush_post') && function_exists('w3tc_flush_all')) {
if (function_exists('wp_cache_clear_cache') && function_exists('wp_cache_post_change')) {
if (function_exists('wpfc_clear_post_cache_by_id') && function_exists('wpfc_clear_all_cache')) {
if (function_exists('rocket_clean_domain') && function_exists('rocket_clean_post')) {
//wp-cloudflare-page-cache
if (class_exists('SW_CLOUDFLARE_PAGECACHE',false)) {
if (method_exists('WP_Optimize', 'get_page_cache') && WP_Optimize()->get_page_cache()->is_enabled()) {
if (defined('LSCWP_CONTENT_DIR')) {
if (method_exists('comet_cache', 'clear') && method_exists('comet_cache', 'clearPost')) {
if (class_exists('Cache_Enabler',false)) {
if (class_exists('Breeze_Admin',false)) {
if (defined('WPHB_DIR_PATH')) {
if (defined('WPSOL_PLUGIN_URL')) {
if (method_exists('autoptimizeCache', 'clearall')) {
if (defined('CLOUDFLARE_PLUGIN_DIR')) {
if (function_exists('sg_cachepress_purge_cache')) {
if (defined('CACHIFY_FILE')) {
$items['Cachify'] = true;
if (class_exists('WpeCommon',false)) {
if (function_exists('rapidcache_clear_post_cache') && function_exists('rapidcache_clear_cache')) {
if (class_exists('Swift_Performance_Cache',false)) {
if (class_exists('NginxCache',false)) {