PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.2
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 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Filesystem / OPcache.php
wp-staging / Framework / Filesystem Last commit date
Filters 2 years ago Scanning 5 years ago DebugLogReader.php 2 years ago DirectoryListing.php 2 years ago DiskWriteCheck.php 3 years ago FileObject.php 2 years ago Filesystem.php 1 year ago FilesystemExceptions.php 5 years ago FilterableDirectoryIterator.php 2 years ago LogCleanup.php 2 years ago LogFiles.php 2 years ago MissingFileException.php 3 years ago OPcache.php 2 years ago PathChecker.php 2 years ago PathIdentifier.php 2 years ago Permissions.php 5 years ago WpUploadsFolderSymlinker.php 5 years ago
OPcache.php
164 lines
1 <?php
2
3 namespace WPStaging\Framework\Filesystem;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Utils\ServerVars;
7 use WPStaging\Framework\Facades\Hooks;
8
9 use function WPStaging\functions\debug_log;
10
11 class OPcache
12 {
13 /**
14 * @var string
15 */
16 const FILTER_OPCACHE_MAYBE_INVALIDATE = 'wpstg.opcache.maybe_invalidate';
17
18 /**
19 * @var ServerVars
20 */
21 private $serverVars;
22
23 public function __construct()
24 {
25 $this->serverVars = WPStaging::make(ServerVars::class);
26 }
27
28 public function reset(): bool
29 {
30 if (!function_exists('opcache_reset') || $this->serverVars->isFunctionDisabled('opcache_reset')) {
31 return false;
32 }
33
34 return opcache_reset();
35 }
36
37 /**
38 * @see https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/
39 * @param string $filePath
40 * @param bool $force
41 * @return bool
42 */
43 public function invalidateFile(string $filePath, bool $force = false): bool
44 {
45 static $canInvalidate = null;
46 if (
47 $canInvalidate === null
48 && function_exists('opcache_invalidate')
49 && (
50 !ini_get('opcache.restrict_api')
51 || !empty($_SERVER['SCRIPT_FILENAME']) && stripos(realpath($_SERVER['SCRIPT_FILENAME']), ini_get('opcache.restrict_api')) === 0 // phpcs:ignore
52 )
53 ) {
54 $canInvalidate = true;
55 }
56
57 if (!$canInvalidate || strtolower(substr($filePath, -4)) !== '.php') {
58 return false;
59 }
60
61 return opcache_invalidate($filePath, $force);
62 }
63
64 /**
65 * @param string $dirPath
66 * @return void
67 */
68 public function invalidateDirectory(string $dirPath)
69 {
70 if (!is_dir($dirPath)) {
71 return;
72 }
73
74 $dirIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dirPath));
75 foreach ($dirIterator as $fileInfo) {
76 if (!$fileInfo->isFile() || $fileInfo->isLink() || $fileInfo->getExtension() !== 'php') {
77 continue;
78 }
79
80 $this->invalidateFile($fileInfo->getRealPath(), true);
81 }
82 }
83
84 /** @return void */
85 public function maybeInvalidate()
86 {
87 if (!Hooks::applyFilters(self::FILTER_OPCACHE_MAYBE_INVALIDATE, true)) {
88 debug_log('opcache invalidate disabled.', 'info', false);
89 return;
90 }
91
92 // If can use opcache_reset
93 if ($this->reset()) {
94 debug_log('opcache_reset executed.', 'info', false);
95 return;
96 }
97
98 // Abort if opcache_invalidate not available
99 if (!function_exists('opcache_invalidate') || $this->serverVars->isFunctionDisabled('opcache_invalidate')) {
100 return;
101 }
102
103 debug_log('Trigger opcache invalidate.', 'info', false);
104
105 // If can use opcache_get_status
106 if (function_exists('opcache_get_status') && !$this->serverVars->isFunctionDisabled('opcache_get_status')) {
107 $opcacheStatus = opcache_get_status();
108 if (!empty($opcacheStatus['scripts'])) {
109 foreach ($opcacheStatus['scripts'] as $file => $data) {
110 $this->invalidateFile($file, true);
111 }
112 }
113
114 return;
115 }
116
117 // Invalidate wp core files
118 $wpCoreFiles = [
119 'index.php',
120 'wp-activate.php',
121 'wp-blog-header.php',
122 'wp-comments-post.php',
123 'wp-config-sample.php',
124 'wp-config.php',
125 'wp-cron.php',
126 'wp-links-opml.php',
127 'wp-load.php',
128 'wp-login.php',
129 'wp-mail.php',
130 'wp-settings.php',
131 'wp-signup.php',
132 'wp-trackback.php',
133 'xmlrpc.php'
134 ];
135
136 foreach ($wpCoreFiles as $file) {
137 $this->invalidateFile(ABSPATH . $file);
138
139 $parentFile = dirname(ABSPATH) . '/' . $file;
140 if (file_exists($parentFile)) {
141 $this->invalidateFile($parentFile);
142 }
143 }
144
145 // Invalidate directory
146 $wpCoreDirs = [
147 'wp-admin/',
148 'wp-includes/',
149 'wp-content/plugins/',
150 'wp-content/mu-plugins/',
151 'wp-content/themes/',
152 ];
153
154 foreach ($wpCoreDirs as $dir) {
155 $this->invalidateDirectory(ABSPATH . $dir);
156
157 $parentDir = dirname(ABSPATH) . '/' . $dir;
158 if (is_dir($parentDir)) {
159 $this->invalidateDirectory($parentDir);
160 }
161 }
162 }
163 }
164