wp_opcache_invalidate

函式
wp_opcache_invalidate ( $filepath, $force = false )
引數
  • (string) $filepath Path to the file, including extension, for which the opcode cache is to be cleared.
    Required:
  • (bool) $force Invalidate even if the modification time is not newer than the file in cache. Default false.
    Required:
    Default: false
返回值
  • (bool) True if opcache was invalidated for `$filepath`, or there was nothing to invalidate. False if opcache invalidation is not available, or is disabled via filter.
定義位置
相關方法
wp_cache_initwp_suspend_cache_invalidationwp_check_invalid_utf8wp_cache_deletewp_cache_add
引入
5.5.0
棄用
-

wp_opcache_invalidate: 這個函式用於使一個給定檔案或目錄的操作碼快取失效。它用於確保在執行檔案時使用最新版本的程式碼。

試圖清除單個PHP檔案的操作碼快取。

這個函式可以被安全地呼叫,而不需要檢查檔案的副檔名 或OPcache副檔名的可用性。

為了提高效能,是否可以失效會被快取起來。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_opcache_invalidate( $filepath, $force = false ) {
static $can_invalidate = null;
/*
* Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
*
* First, check to see if the function is available to call, then if the host has restricted
* the ability to run the function to avoid a PHP warning.
*
* `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`.
*
* If the host has this set, check whether the path in `opcache.restrict_api` matches
* the beginning of the path of the origin file.
*
* `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()`
* is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI.
*
* For more details, see:
* - https://www.php.net/manual/en/opcache.configuration.php
* - https://www.php.net/manual/en/reserved.variables.server.php
* - https://core.trac.wordpress.org/ticket/36455
*/
if ( null === $can_invalidate
&& function_exists( 'opcache_invalidate' )
&& ( ! ini_get( 'opcache.restrict_api' )
|| stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 )
) {
$can_invalidate = true;
}
// If invalidation is not available, return early.
if ( ! $can_invalidate ) {
return false;
}
// Verify that file to be invalidated has a PHP extension.
if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) {
return false;
}
/**
* Filters whether to invalidate a file from the opcode cache.
*
* @since 5.5.0
*
* @param bool $will_invalidate Whether WordPress will invalidate `$filepath`. Default true.
* @param string $filepath The path to the PHP file to invalidate.
*/
if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
return opcache_invalidate( $filepath, $force );
}
return false;
}
function wp_opcache_invalidate( $filepath, $force = false ) { static $can_invalidate = null; /* * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value. * * First, check to see if the function is available to call, then if the host has restricted * the ability to run the function to avoid a PHP warning. * * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`. * * If the host has this set, check whether the path in `opcache.restrict_api` matches * the beginning of the path of the origin file. * * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()` * is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI. * * For more details, see: * - https://www.php.net/manual/en/opcache.configuration.php * - https://www.php.net/manual/en/reserved.variables.server.php * - https://core.trac.wordpress.org/ticket/36455 */ if ( null === $can_invalidate && function_exists( 'opcache_invalidate' ) && ( ! ini_get( 'opcache.restrict_api' ) || stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 ) ) { $can_invalidate = true; } // If invalidation is not available, return early. if ( ! $can_invalidate ) { return false; } // Verify that file to be invalidated has a PHP extension. if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) { return false; } /** * Filters whether to invalidate a file from the opcode cache. * * @since 5.5.0 * * @param bool $will_invalidate Whether WordPress will invalidate `$filepath`. Default true. * @param string $filepath The path to the PHP file to invalidate. */ if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) { return opcache_invalidate( $filepath, $force ); } return false; }
function wp_opcache_invalidate( $filepath, $force = false ) {
	static $can_invalidate = null;

	/*
	 * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
	 *
	 * First, check to see if the function is available to call, then if the host has restricted
	 * the ability to run the function to avoid a PHP warning.
	 *
	 * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`.
	 *
	 * If the host has this set, check whether the path in `opcache.restrict_api` matches
	 * the beginning of the path of the origin file.
	 *
	 * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()`
	 * is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI.
	 *
	 * For more details, see:
	 * - https://www.php.net/manual/en/opcache.configuration.php
	 * - https://www.php.net/manual/en/reserved.variables.server.php
	 * - https://core.trac.wordpress.org/ticket/36455
	 */
	if ( null === $can_invalidate
		&& function_exists( 'opcache_invalidate' )
		&& ( ! ini_get( 'opcache.restrict_api' )
			|| stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 )
	) {
		$can_invalidate = true;
	}

	// If invalidation is not available, return early.
	if ( ! $can_invalidate ) {
		return false;
	}

	// Verify that file to be invalidated has a PHP extension.
	if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) {
		return false;
	}

	/**
	 * Filters whether to invalidate a file from the opcode cache.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $will_invalidate Whether WordPress will invalidate `$filepath`. Default true.
	 * @param string $filepath        The path to the PHP file to invalidate.
	 */
	if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
		return opcache_invalidate( $filepath, $force );
	}

	return false;
}

常見問題

FAQs
檢視更多 >