| 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 | /* |
| 2299 | * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value. |
| 2300 | * |
| 2301 | * First, check to see if the function is available to call, then if the host has restricted |
| 2302 | * the ability to run the function to avoid a PHP warning. |
| 2303 | * |
| 2304 | * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`. |
| 2305 | * |
| 2306 | * If the host has this set, check whether the path in `opcache.restrict_api` matches |
| 2307 | * the beginning of the path of the current file. |
| 2308 | * |
| 2309 | * See: https://www.php.net/manual/en/opcache.configuration.php |
| 2310 | */ |
| 2311 | if ( $can_invalidate === null ) { |
| 2312 | $can_invalidate = function_exists( 'opcache_invalidate' ) && |
| 2313 | ( ! ini_get( 'opcache.restrict_api' ) || stripos( __FILE__, ini_get( 'opcache.restrict_api' ) ) === 0 ); |
| 2314 | } |
| 2315 | |
| 2316 | // If invalidation is not available, return early. |
| 2317 | if ( ! $can_invalidate ) { |
| 2318 | return false; |
| 2319 | } |
| 2320 | |
| 2321 | // Verify that file to be invalidated has a PHP-related extension. |
| 2322 | if ( ! preg_match( '/\.(?:php)$/i', $filepath ) ) { |
| 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 | } |