wp-staging
Last commit date
Backend
2 years ago
Backup
2 years ago
Basic
2 years ago
Core
2 years ago
Framework
2 years ago
Frontend
2 years ago
assets
2 years ago
languages
3 years ago
vendor_wpstg
2 years ago
Deactivate.php
2 years ago
README.md
3 years ago
autoloader.php
3 years ago
bootstrap.php
2 years ago
constantsFree.php
2 years ago
freeBootstrap.php
2 years ago
install.php
2 years ago
opcacheBootstrap.php
2 years ago
readme.txt
2 years ago
runtimeRequirements.php
2 years ago
uninstall.php
2 years ago
wp-staging-error-handler.php
3 years ago
wp-staging.php
2 years ago
opcacheBootstrap.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * WordPress installations with OPCache enabled might bootstrap in an |
| 5 | * incoherent state from PHP and the Filesystem, causing fatal errors if |
| 6 | * upgrading to a newer version where a file has been removed. |
| 7 | * |
| 8 | * This file clears OPCache in this plugin if needed. |
| 9 | * |
| 10 | * WordPress 5.5+ handles OPCache invalidation depending on |
| 11 | * whether \WP_Upgrader::run is called with the "clear_destination" |
| 12 | * parameter set to true. |
| 13 | * |
| 14 | * @var string $pluginFilePath |
| 15 | * @var string $wp_version |
| 16 | */ |
| 17 | global $wp_version, $pluginFilePath; |
| 18 | |
| 19 | // Early bail: WordPress 5.5+ already handles OPCache invalidation on plugin updates. |
| 20 | if (version_compare($wp_version, '5.5', '>=')) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | $filename = isset($_SERVER['SCRIPT_FILENAME']) ? sanitize_text_field($_SERVER['SCRIPT_FILENAME']) : ''; |
| 25 | |
| 26 | // Ported from WordPress 5.5 wp_opcache_invalidate |
| 27 | $canInvalidate = function_exists('opcache_invalidate') |
| 28 | && (!ini_get('opcache.restrict_api') || stripos(realpath($filename), ini_get('opcache.restrict_api')) === 0); |
| 29 | |
| 30 | // Early bail: OPCache not enabled, or we can't clear it. |
| 31 | if (!$canInvalidate) { |
| 32 | if (defined('WPSTG_DEBUG') && WPSTG_DEBUG) { |
| 33 | error_log('WP STAGING: Can not clear OPCache.'); |
| 34 | } |
| 35 | |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * When a site has OPCache enabled, it will cache the compiled "opcode" of this PHP file and all other PHP files. |
| 41 | * |
| 42 | * It doesn't cache, however, the result returned by the functions. |
| 43 | * |
| 44 | * We leverage this to run a runtime check between the version in this PHP file and |
| 45 | * the filesystem. The only possible scenario they can be different, on a regular |
| 46 | * distributed plugin, is if the variable in PHP is opcached to a different version |
| 47 | * from what's in the filesystem. |
| 48 | * |
| 49 | * We use the "Version" from the headers of the main file of the plugin to compare. |
| 50 | */ |
| 51 | $runtimeVersionDifferentFromBuildVersion = get_file_data($pluginFilePath, ['Version' => 'Version'])['Version'] !== '3.1.0'; |
| 52 | $lastCheckHappenedAfterInterval = current_time('timestamp') > (int)get_site_transient('wpstg.bootstrap.opcache.lastCleared') + 5 * MINUTE_IN_SECONDS; |
| 53 | |
| 54 | $shouldClearOpCache = apply_filters('wpstg.bootstrap.opcache.shouldClear', $runtimeVersionDifferentFromBuildVersion && $lastCheckHappenedAfterInterval); |
| 55 | |
| 56 | if ($shouldClearOpCache) { |
| 57 | set_site_transient('wpstg.bootstrap.opcache.lastCleared', current_time('timestamp'), 1 * HOUR_IN_SECONDS); |
| 58 | |
| 59 | $start = microtime(true); |
| 60 | |
| 61 | clearstatcache(true); |
| 62 | |
| 63 | try { |
| 64 | $it = new RecursiveDirectoryIterator(dirname($pluginFilePath)); |
| 65 | } catch (Exception $e) { |
| 66 | // DirectoryIterator will throw if this plugin folder is not readable. |
| 67 | if (defined('WPSTG_DEBUG') && WPSTG_DEBUG) { |
| 68 | error_log('WPSTG failed to clear OPCache because the folder does not exist or is not readable. Exception: ' . $e->getMessage()); |
| 69 | } |
| 70 | |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $it = new RecursiveIteratorIterator($it); |
| 75 | |
| 76 | $success = 0; |
| 77 | $failures = 0; |
| 78 | |
| 79 | /** @var SplFileInfo $fileInfo */ |
| 80 | foreach ($it as $fileInfo) { |
| 81 | if ( |
| 82 | $fileInfo->isFile() |
| 83 | && !$fileInfo->isLink() |
| 84 | && $fileInfo->getExtension() === 'php' |
| 85 | ) { |
| 86 | if (opcache_invalidate($fileInfo->getRealPath(), false)) { |
| 87 | $success++; |
| 88 | } else { |
| 89 | $failures++; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | add_action('admin_notices', function () use ($pluginFilePath, $start) { |
| 95 | echo '<div class="notice-warning notice is-dismissible">'; |
| 96 | echo '<p style="font-weight: bold;">' . esc_html__('WP STAGING OPCache', 'wp-staging') . '</p>'; |
| 97 | echo '<p>' . wp_kses_post( |
| 98 | sprintf( |
| 99 | __('WP STAGING detected that the OPCache was outdated and automatically cleared the OPCache for the <strong>%s</strong> folder to prevent issues. This operation took %s seconds.', 'wp-staging'), |
| 100 | plugin_basename($pluginFilePath), |
| 101 | number_format(microtime(true) - $start, 4) |
| 102 | ) |
| 103 | ) . '</p>'; |
| 104 | echo '</div>'; |
| 105 | }); |
| 106 | |
| 107 | if (defined('WPSTG_DEBUG') && WPSTG_DEBUG) { |
| 108 | error_log(sprintf('%s files were cleared from OPCache in %s seconds', $success, microtime(true) - $start)); |
| 109 | if (!empty($failures)) { |
| 110 | error_log(sprintf('WP STAGING could not clear %s files from the OpCache cache upon activation. There may be inconsistencies.', $failures)); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 |