<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[48160] trunk/src/wp-admin/includes: Upgrade/Install: Invalidate OPcache for PHP files during updates.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/48160">48160</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/48160","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>mikeschroder</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-06-25 07:13:22 +0000 (Thu, 25 Jun 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Upgrade/Install: Invalidate OPcache for PHP files during updates.

When files are copied into place, check whether opcode invalidation is available and attempt to invalidate to avoid unintended behavior or fatal errors from themes, plugins, or core.

Introduces `wp_opcache_invalidate()` to allow safe invalidation of PHP files from opcode cache, and a filter, `wp_opcache_invalidate_file` to override the behavior.

Replaces the existing calls to `opcache_invalidate()` in the plugin and theme editors to use the new function.

Thanks to jnylen0 for porting over a patch from ClassicPress that provided much of the approach for what is being committed.

Props nigro.simone, dd32, JasWSInc, szepe.viktor, swissspidy, JanR, asalce, Garavani, pavelevap, pputzer, GregLone, benoitchantre, jadonn, doc987, kraftbj, Krstarica, jnylen0, nextendweb, williampatton, ayeshrajans, joostdevalk, stevenkussmaul, boogah, jorbin, mikeschroder.
Fixes <a href="https://core.trac.wordpress.org/ticket/36455">#36455</a>, <a href="https://core.trac.wordpress.org/ticket/50354">#50354</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasscoreupgraderphp">trunk/src/wp-admin/includes/class-core-upgrader.php</a></li>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
<li><a href="#trunksrcwpadminincludesupdatecorephp">trunk/src/wp-admin/includes/update-core.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasscoreupgraderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-core-upgrader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-core-upgrader.php       2020-06-25 05:14:43 UTC (rev 48159)
+++ trunk/src/wp-admin/includes/class-core-upgrader.php 2020-06-25 07:13:22 UTC (rev 48160)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,6 +161,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_filesystem->chmod( $wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                wp_opcache_invalidate( ABSPATH . 'wp-admin/includes/update-core.php' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 require_once ABSPATH . 'wp-admin/includes/update-core.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! function_exists( 'update_core' ) ) {
</span></span></pre></div>
<a id="trunksrcwpadminincludesfilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/file.php      2020-06-25 05:14:43 UTC (rev 48159)
+++ trunk/src/wp-admin/includes/file.php        2020-06-25 07:13:22 UTC (rev 48160)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -498,9 +498,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( false === $written ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( 'php' === $extension && function_exists( 'opcache_invalidate' ) ) {
-               opcache_invalidate( $real_file, true );
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_opcache_invalidate( $real_file, true );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $is_active && 'php' === $extension ) {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -608,9 +606,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Roll-back file change.
</span><span class="cx" style="display: block; padding: 0 10px">                        file_put_contents( $real_file, $previous_content );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( function_exists( 'opcache_invalidate' ) ) {
-                               opcache_invalidate( $real_file, true );
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 wp_opcache_invalidate( $real_file, true );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! isset( $result['message'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $message = __( 'Something went wrong.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1743,6 +1739,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        wp_opcache_invalidate( $to . $filename );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( 'd' === $fileinfo['type'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2277,3 +2274,75 @@
</span><span class="cx" style="display: block; padding: 0 10px">        </div>
</span><span class="cx" style="display: block; padding: 0 10px">        <?php
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Attempt to clear the opcode cache for an individual PHP file.
+ *
+ * This function can be called safely without having to check the file extension
+ * or availability of the OPcache extension.
+ *
+ * Whether or not invalidation is possible is cached to improve performance.
+ *
+ * @since 5.5
+ *
+ * @link https://www.php.net/manual/en/function.opcache-invalidate.php
+ *
+ * @param string $filepath Path to the file, including extension, for which the opcode cache is to be cleared.
+ * @param bool   $force    Invalidate even if the modification time is not newer than the file in cache. Default `false`.
+ *
+ * @return bool `true` if opcache was invalidated for `$filepath`, or there was nothing to invalidate.
+ *              `false` if opcache invalidation is not available, or is disabled via filter.
+ */
+function wp_opcache_invalidate( $filepath, $force = false ) {
+       static $can_invalidate = null;
+
+       /*
+        * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
+        *
+        * First, check to see if the function is available to call, then if the host has restricted
+        * the ability to run the function to avoid a PHP warning.
+        *
+        * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`.
+        *
+        * If the host has this set, check whether the path in `opcache.restrict_api` matches
+        * the beginning of the path of the origin file.
+        *
+        * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but
+        * `realpath()` is necessary because `SCRIPT_FILENAME` can be a relative path
+        * when run from CLI.
+        *
+        * For more details, see:
+        * - https://www.php.net/manual/en/opcache.configuration.php
+        * - https://www.php.net/manual/en/reserved.variables.server.php
+        * - https://core.trac.wordpress.org/ticket/36455
+        */
+       if ( $can_invalidate === null ) {
+               $can_invalidate = function_exists( 'opcache_invalidate' ) &&
+                       ( ! ini_get( 'opcache.restrict_api' ) ||
+                               stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 );
+       }
+
+       // If invalidation is not available, return early.
+       if ( ! $can_invalidate ) {
+               return false;
+       }
+
+       // Verify that file to be invalidated has a PHP extension.
+       if ( ! preg_match( '/\.(?:php)$/i', $filepath ) ) {
+               return false;
+       }
+
+       /**
+        * Filters whether to invalidate a file from the opcode cache.
+        *
+        * @since 5.5
+        *
+        * @param bool   $will_invalidate Whether WordPress will invalidate `$filename`. Default `true`.
+        * @param string $filename        The PHP filename to invalidate.
+        */
+       if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
+               return opcache_invalidate( $filepath, $force );
+       }
+
+       return false;
+}
</ins></span></pre></div>
<a id="trunksrcwpadminincludesupdatecorephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/update-core.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/update-core.php       2020-06-25 05:14:43 UTC (rev 48159)
+++ trunk/src/wp-admin/includes/update-core.php 2020-06-25 07:13:22 UTC (rev 48160)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1318,14 +1318,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * Assumes that WP_Filesystem() has already been called and setup.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * This is a temporary function for the 3.1 -> 3.2 upgrade, as well as for those
- * upgrading to 3.7+.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * This is a standalone copy of the `copy_dir()` function that is used to
+ * upgrade the core files. It is placed here so that the version of this
+ * function from the *new* WordPress version will be called.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * It was initially added for the 3.1 -> 3.2 upgrade.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * @ignore
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.2.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.7.0 Updated not to use a regular expression for the skip list.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @see copy_dir()
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @link https://core.trac.wordpress.org/ticket/17173
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global WP_Filesystem_Base $wp_filesystem
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1355,6 +1359,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        return new WP_Error( 'copy_failed__copy_dir', __( 'Could not copy file.' ), $to . $filename );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // `wp_opcache_invalidate()` only exists in WordPress 5.5, so don't run it when upgrading to 5.5.
+                       if ( function_exists( 'wp_opcache_invalidate' ) ) {
+                               wp_opcache_invalidate( $to . $filename );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( 'd' === $fileinfo['type'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
</span></span></pre>
</div>
</div>

</body>
</html>