wp_opcache_invalidate_directory()
云策文档标注
概述
wp_opcache_invalidate_directory() 函数用于尝试清除指定目录下文件的 opcode 缓存。它通过递归遍历目录结构,对每个文件调用 wp_opcache_invalidate() 来清除缓存。
关键要点
- 函数接受一个字符串参数 $dir,表示要清除 opcode 缓存的目录路径。
- 如果 $dir 不是非空字符串,在 WP_DEBUG 启用时会触发错误并返回。
- 使用 WP_Filesystem 的 dirlist() 方法获取目录列表,并递归处理文件和子目录。
- 递归函数 $invalidate_directory 使用 trailingslashit() 确保路径正确,并根据文件类型调用 wp_opcache_invalidate()。
- 该函数从 WordPress 6.2.0 版本引入。
代码示例
function wp_opcache_invalidate_directory( $dir ) {
global $wp_filesystem;
if ( ! is_string( $dir ) || '' === trim( $dir ) ) {
if ( WP_DEBUG ) {
$error_message = sprintf(
/* translators: %s: The function name. */
__( '%s expects a non-empty string.' ),
'wp_opcache_invalidate_directory()'
);
wp_trigger_error( '', $error_message );
}
return;
}
$dirlist = $wp_filesystem->dirlist( $dir, false, true );
if ( empty( $dirlist ) ) {
return;
}
$invalidate_directory = static function ( $dirlist, $path ) use ( &$invalidate_directory ) {
$path = trailingslashit( $path );
foreach ( $dirlist as $name => $details ) {
if ( 'f' === $details['type'] ) {
wp_opcache_invalidate( $path . $name, true );
} elseif ( is_array( $details['files'] ) && ! empty( $details['files'] ) ) {
$invalidate_directory( $details['files'], $path . $name );
}
}
};
$invalidate_directory( $dirlist, $dir );
}注意事项
- 函数依赖于 WP_Filesystem 全局变量,确保文件系统已正确初始化。
- 递归处理可能影响性能,特别是在大型目录结构中,使用时需注意。
- 错误处理仅在 WP_DEBUG 启用时触发,生产环境中可能不会显示错误信息。
原文内容
Attempts to clear the opcode cache for a directory of files.
Description
See also
Parameters
$dirstringrequired-
The path to the directory for which the opcode cache is to be cleared.
Source
function wp_opcache_invalidate_directory( $dir ) {
global $wp_filesystem;
if ( ! is_string( $dir ) || '' === trim( $dir ) ) {
if ( WP_DEBUG ) {
$error_message = sprintf(
/* translators: %s: The function name. */
__( '%s expects a non-empty string.' ),
'<code>wp_opcache_invalidate_directory()</code>'
);
wp_trigger_error( '', $error_message );
}
return;
}
$dirlist = $wp_filesystem->dirlist( $dir, false, true );
if ( empty( $dirlist ) ) {
return;
}
/*
* Recursively invalidate opcache of files in a directory.
*
* WP_Filesystem_*::dirlist() returns an array of file and directory information.
*
* This does not include a path to the file or directory.
* To invalidate files within sub-directories, recursion is needed
* to prepend an absolute path containing the sub-directory's name.
*
* @param array $dirlist Array of file/directory information from WP_Filesystem_Base::dirlist(),
* with sub-directories represented as nested arrays.
* @param string $path Absolute path to the directory.
*/
$invalidate_directory = static function ( $dirlist, $path ) use ( &$invalidate_directory ) {
$path = trailingslashit( $path );
foreach ( $dirlist as $name => $details ) {
if ( 'f' === $details['type'] ) {
wp_opcache_invalidate( $path . $name, true );
} elseif ( is_array( $details['files'] ) && ! empty( $details['files'] ) ) {
$invalidate_directory( $details['files'], $path . $name );
}
}
};
$invalidate_directory( $dirlist, $dir );
}
Changelog
| Version | Description |
|---|---|
| 6.2.0 | Introduced. |