This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
CP_Settings::_cp_maybe_install_apcu_object_cache()
Installs an object-cache.php file, if one does not already exist, to make use of the APCu extension as an external object cache.
Source
File: wp-includes/classicpress/class-cp-settings.php
public function _cp_maybe_install_apcu_object_cache() {
// Abort if the apcu extension is not installed.
if ( ! extension_loaded( 'apcu' ) ) {
return;
}
// Setup the filesystem abstraction
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
global $wp_filesystem;
if ( empty( $wp_filesystem ) ) {
WP_Filesystem();
}
// Define wp-content/object-cache.php path.
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
$object_cache_file = $wp_content_dir . '/object-cache.php';
// Remove object-cache.php file if set by ClassicPress and the user requests.
$cp_object_cache = absint( get_option( 'cp_object_cache' ) );
if ( 0 === $cp_object_cache ) {
// Check if object-cache.php exists using $wp_filesystem.
if ( $wp_filesystem->exists( $object_cache_file ) ) {
// Match the header comment block
$file_data = file_get_contents( $object_cache_file );
if ( preg_match( '/\/\*.*?\*\//s', $file_data, $matches ) ) {
$header_block = $matches[0];
// Look for the Plugin Name line
if ( preg_match( '/Plugin Name:\s*(.*)/i', $header_block, $plugin_name_match ) ) {
$plugin_name = trim( $plugin_name_match[1] );
// Compare to your target string
if ( $plugin_name === 'WordPress APCu Object Cache Backend' ) {
// Match found, so clear the object cache ...
if ( function_exists( 'apcu_clear_cache' ) ) {
apcu_clear_cache();
}
// ... and delete the object cache file.
$success = $wp_filesystem->delete( $object_cache_file );
if ( ! $success ) {
return new WP_Error( 'APCu-cache-file-error', __( 'Failed to delete the file: ' ) . $object_cache_file );
}
}
}
}
}
} else { // Otherwise install object cache
// Check if object-cache.php exists using $wp_filesystem.
if ( ! $wp_filesystem->exists( $object_cache_file ) ) {
$source_file = ABSPATH . WPINC . '/object-cache.php';
if ( $wp_filesystem->exists( $source_file ) ) {
// Copy and paste the file using the WP_Filesystem
if ( ! $wp_filesystem->copy( $source_file, $object_cache_file ) ) {
return new WP_Error( 'APCu-cache-file-error', __( 'Failed to copy object-cache.php to wp-content folder.' ) );
}
}
}
}
}
Changelog
| Version | Description |
|---|---|
| CP-2.7.0 | Introduced. CP-2.7.0 |