wp-staging
Last commit date
Backend
1 day ago
Backup
1 day ago
Basic
1 day ago
Component
1 day ago
Core
1 day ago
Framework
1 day ago
Frontend
1 day ago
Notifications
1 day ago
Staging
1 day ago
assets
1 day ago
languages
1 day ago
resources
1 day ago
vendor_wpstg
1 day ago
views
1 day ago
CONTRIBUTING.md
2 years ago
Deactivate.php
1 day ago
README.md
5 months ago
SECURITY.md
3 weeks ago
autoloader.php
1 day ago
bootstrap.php
1 day ago
commonBootstrap.php
1 day ago
constantsFree.php
1 day ago
freeBootstrap.php
1 day ago
install.php
1 day ago
opcacheBootstrap.php
1 day ago
readme.txt
1 day ago
runtimeRequirements.php
1 day ago
uninstall.php
1 day ago
wp-staging-error-handler.php
1 day ago
wp-staging.php
1 day ago
opcacheBootstrap.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | global $wp_version, $pluginFilePath; |
| 18 | |
| 19 | |
| 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 | |
| 27 | $canInvalidate = function_exists('opcache_invalidate') |
| 28 | && (!ini_get('opcache.restrict_api') || stripos(realpath($filename), ini_get('opcache.restrict_api')) === 0); |
| 29 | |
| 30 | |
| 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 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | $runtimeVersionDifferentFromBuildVersion = get_file_data($pluginFilePath, ['Version' => 'Version'])['Version'] !== '4.11.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 | |
| 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 | |
| 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 |