PluginProbe
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.12.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.12.0
4.12.0 4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 All 67 releases
wp-staging / opcacheBootstrap.php
opcacheBootstrap.php
114 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.12.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