| | 2276 | |
| | 2277 | /** |
| | 2278 | * Attempt to clear the opcode cache for an individual PHP file. |
| | 2279 | * |
| | 2280 | * This function can be called safely without having to check the file extension |
| | 2281 | * or availability of the OPcache extension. |
| | 2282 | * |
| | 2283 | * Whether or not invalidation is possible is cached to improve performance. |
| | 2284 | * |
| | 2285 | * @since 5.5 |
| | 2286 | * |
| | 2287 | * @link https://www.php.net/manual/en/function.opcache-invalidate.php |
| | 2288 | * |
| | 2289 | * @param string $filepath Path to the file, including extension, for which the opcode cache is to be cleared. |
| | 2290 | * @param bool $force Invalidate even if the modification time is not newer than the file in cache. Default `false`. |
| | 2291 | * |
| | 2292 | * @return bool `true` if opcache was invalidated for `$filepath`, or there was nothing to invalidate. |
| | 2293 | * `false` if opcache invalidation is not available, or is disabled via filter. |
| | 2294 | */ |
| | 2295 | function wp_opcache_invalidate( $filepath, $force = false ) { |
| | 2296 | static $can_invalidate = null; |
| | 2297 | |
| | 2298 | // Verify that file to be invalidated has a PHP-related extension. |
| | 2299 | if ( ! preg_match( '/\.(?:php|phtml)/i', $filepath ) ) { |
| | 2300 | return false; |
| | 2301 | } |
| | 2302 | |
| | 2303 | /* |
| | 2304 | * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value. |
| | 2305 | * |
| | 2306 | * First, check to see if the function is available to call, then if the host has restricted |
| | 2307 | * the ability to run the function to avoid a PHP warning. |
| | 2308 | * |
| | 2309 | * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`. |
| | 2310 | * |
| | 2311 | * If the host has this set, check whether the path in `opcache.restrict_api` matches |
| | 2312 | * the beginning of the path of the current file. |
| | 2313 | * |
| | 2314 | * See: https://www.php.net/manual/en/opcache.configuration.php |
| | 2315 | */ |
| | 2316 | if ( $can_invalidate === null ) { |
| | 2317 | $can_invalidate = function_exists( 'opcache_invalidate' ) && |
| | 2318 | ( ! ini_get( 'opcache.restrict_api' ) || stripos( __FILE__, ini_get( 'opcache.restrict_api' ) ) === 0 ); |
| | 2319 | } |
| | 2320 | |
| | 2321 | // If invalidation is not available, return early. |
| | 2322 | if ( ! $can_invalidate ) { |
| | 2323 | return false; |
| | 2324 | } |
| | 2325 | |
| | 2326 | /** |
| | 2327 | * Filters whether to invalidate a file from the opcode cache. |
| | 2328 | * |
| | 2329 | * @since 5.5 |
| | 2330 | * |
| | 2331 | * @param bool $will_invalidate Whether WordPress will invalidate `$filename`. Default `true`. |
| | 2332 | * @param string $filename The PHP filename to invalidate. |
| | 2333 | */ |
| | 2334 | if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) { |
| | 2335 | return opcache_invalidate( $filepath, $force ); |
| | 2336 | } |
| | 2337 | |
| | 2338 | return false; |
| | 2339 | } |