<!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>[41617] trunk/src: Options, Meta APIs: Require a confirmation link in an email to be clicked when a user attempts to change the network</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 { 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/41617">41617</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/41617","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-09-27 14:16:21 +0000 (Wed, 27 Sep 2017)</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'>Options, Meta APIs: Require a confirmation link in an email to be clicked when a user attempts to change the network
admin email address on Multisite.

This mirrors the same functionality for the site admin email address and user profile email address.

Fixes <a href="https://core.trac.wordpress.org/ticket/41254">#41254</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesmsadminfiltersphp">trunk/src/wp-admin/includes/ms-admin-filters.php</a></li>
<li><a href="#trunksrcwpadminnetworksettingsphp">trunk/src/wp-admin/network/settings.php</a></li>
<li><a href="#trunksrcwpincludesmsfunctionsphp">trunk/src/wp-includes/ms-functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesmsadminfiltersphp"></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/ms-admin-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ms-admin-filters.php  2017-09-27 13:26:53 UTC (rev 41616)
+++ trunk/src/wp-admin/includes/ms-admin-filters.php    2017-09-27 14:16:21 UTC (rev 41617)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -38,3 +38,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Update Hooks
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'network_admin_notices', 'update_nag',      3  );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'network_admin_notices', 'maintenance_nag', 10 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+// Network Admin Hooks
+add_action( 'add_site_option_new_admin_email',    'update_network_option_new_admin_email', 10, 2 );
+add_action( 'update_site_option_new_admin_email', 'update_network_option_new_admin_email', 10, 2 );
</ins></span></pre></div>
<a id="trunksrcwpadminnetworksettingsphp"></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/network/settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/network/settings.php   2017-09-27 13:26:53 UTC (rev 41616)
+++ trunk/src/wp-admin/network/settings.php     2017-09-27 14:16:21 UTC (rev 41617)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -19,6 +19,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $title = __( 'Network Settings' );
</span><span class="cx" style="display: block; padding: 0 10px"> $parent_file = 'settings.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">+// Handle network admin email change requests
+if ( ! empty( $_GET[ 'network_admin_hash' ] ) ) {
+       $new_admin_details = get_site_option( 'network_admin_hash' );
+       $redirect = 'settings.php?updated=false';
+       if ( is_array( $new_admin_details ) && hash_equals( $new_admin_details[ 'hash' ], $_GET[ 'network_admin_hash' ] ) && ! empty( $new_admin_details[ 'newemail' ] ) ) {
+               update_site_option( 'admin_email', $new_admin_details[ 'newemail' ] );
+               delete_site_option( 'network_admin_hash' );
+               delete_site_option( 'new_admin_email' );
+               $redirect = 'settings.php?updated=true';
+       }
+       wp_redirect( network_admin_url( $redirect ) );
+       exit;
+} elseif ( ! empty( $_GET['dismiss'] ) && 'new_network_admin_email' == $_GET['dismiss'] ) {
+       check_admin_referer( 'dismiss_new_network_admin_email' );
+       delete_site_option( 'network_admin_hash' );
+       delete_site_option( 'new_admin_email' );
+       wp_redirect( network_admin_url( 'settings.php?updated=true' ) );
+       exit;
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> add_action( 'admin_head', 'network_settings_add_js' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> get_current_screen()->add_help_tab( array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -58,7 +78,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'upload_space_check_disabled', 'blog_upload_space', 'upload_filetypes', 'site_name',
</span><span class="cx" style="display: block; padding: 0 10px">                'first_post', 'first_page', 'first_comment', 'first_comment_url', 'first_comment_author',
</span><span class="cx" style="display: block; padding: 0 10px">                'welcome_email', 'welcome_user_email', 'fileupload_maxk', 'global_terms_enabled',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'illegal_names', 'limited_email_domains', 'banned_email_domains', 'WPLANG', 'admin_email',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'illegal_names', 'limited_email_domains', 'banned_email_domains', 'WPLANG', 'new_admin_email',
</ins><span class="cx" style="display: block; padding: 0 10px">                 'first_comment_email',
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -111,10 +131,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        <tr>
</span><span class="cx" style="display: block; padding: 0 10px">                                <th scope="row"><label for="admin_email"><?php _e( 'Network Admin Email' ) ?></label></th>
</span><span class="cx" style="display: block; padding: 0 10px">                                <td>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        <input name="admin_email" type="email" id="admin_email" aria-describedby="admin-email-desc" class="regular-text" value="<?php echo esc_attr( get_site_option( 'admin_email' ) ) ?>" />
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 <input name="new_admin_email" type="email" id="admin_email" aria-describedby="admin-email-desc" class="regular-text" value="<?php echo esc_attr( get_site_option( 'admin_email' ) ) ?>" />
</ins><span class="cx" style="display: block; padding: 0 10px">                                         <p class="description" id="admin-email-desc">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                <?php _e( 'This email address will receive notifications. Registration and support emails will also come from this address.' ); ?>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         <?php _e( 'This address is used for admin purposes. If you change this we will send you an email at your new address to confirm it. <strong>The new address will not become active until confirmed.</strong>' ); ?>
</ins><span class="cx" style="display: block; padding: 0 10px">                                         </p>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        <?php
+                                       $new_admin_email = get_site_option( 'new_admin_email' );
+                                       if ( $new_admin_email && $new_admin_email != get_site_option( 'admin_email' ) ) : ?>
+                                               <div class="updated inline">
+                                               <p><?php
+                                                       printf(
+                                                               /* translators: %s: new network admin email */
+                                                               __( 'There is a pending change of the network admin email to %s.' ),
+                                                               '<code>' . esc_html( $new_admin_email ) . '</code>'
+                                                       );
+                                                       printf(
+                                                               ' <a href="%1$s">%2$s</a>',
+                                                               esc_url( wp_nonce_url( network_admin_url( 'settings.php?dismiss=new_network_admin_email' ), 'dismiss_new_network_admin_email' ) ),
+                                                               __( 'Cancel' )
+                                                       );
+                                               ?></p>
+                                               </div>
+                                       <?php endif; ?>
</ins><span class="cx" style="display: block; padding: 0 10px">                                 </td>
</span><span class="cx" style="display: block; padding: 0 10px">                        </tr>
</span><span class="cx" style="display: block; padding: 0 10px">                </table>
</span></span></pre></div>
<a id="trunksrcwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-functions.php    2017-09-27 13:26:53 UTC (rev 41616)
+++ trunk/src/wp-includes/ms-functions.php      2017-09-27 14:16:21 UTC (rev 41617)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2603,6 +2603,84 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</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">+ * Send a confirmation request email when a change of network admin email address is attempted.
+ *
+ * The new network admin address will not become active until confirmed.
+ *
+ * @since 4.9.0
+ *
+ * @param string $old_value The old network admin email address.
+ * @param string $value     The proposed new network admin email address.
+ */
+function update_network_option_new_admin_email( $old_value, $value ) {
+       if ( $value == get_site_option( 'admin_email' ) || ! is_email( $value ) ) {
+               return;
+       }
+
+       $hash = md5( $value . time() . mt_rand() );
+       $new_admin_email = array(
+               'hash'     => $hash,
+               'newemail' => $value,
+       );
+       update_site_option( 'network_admin_hash', $new_admin_email );
+
+       $switched_locale = switch_to_locale( get_user_locale() );
+
+       /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
+       $email_text = __( 'Howdy ###USERNAME###,
+
+You recently requested to have the network admin email address on
+your network changed.
+
+If this is correct, please click on the following link to change it:
+###ADMIN_URL###
+
+You can safely ignore and delete this email if you do not want to
+take this action.
+
+This email has been sent to ###EMAIL###
+
+Regards,
+All at ###SITENAME###
+###SITEURL###' );
+
+       /**
+        * Filters the text of the email sent when a change of network admin email address is attempted.
+        *
+        * The following strings have a special meaning and will get replaced dynamically:
+        * ###USERNAME###  The current user's username.
+        * ###ADMIN_URL### The link to click on to confirm the email change.
+        * ###EMAIL###     The proposed new network admin email address.
+        * ###SITENAME###  The name of the network.
+        * ###SITEURL###   The URL to the network.
+        *
+        * @since 4.9.0
+        *
+        * @param string $email_text      Text in the email.
+        * @param array  $new_admin_email {
+        *     Data relating to the new network admin email address.
+        *
+        *     @type string $hash     The secure hash used in the confirmation link URL.
+        *     @type string $newemail The proposed new network admin email address.
+        * }
+        */
+       $content = apply_filters( 'new_network_admin_email_content', $email_text, $new_admin_email );
+
+       $current_user = wp_get_current_user();
+       $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
+       $content = str_replace( '###ADMIN_URL###', esc_url( network_admin_url( 'settings.php?network_admin_hash=' . $hash ) ), $content );
+       $content = str_replace( '###EMAIL###', $value, $content );
+       $content = str_replace( '###SITENAME###', wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ), $content );
+       $content = str_replace( '###SITEURL###', network_home_url(), $content );
+
+       wp_mail( $value, sprintf( __( '[%s] New Network Admin Email Address' ), wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ) ), $content );
+
+       if ( $switched_locale ) {
+               restore_previous_locale();
+       }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Send an email to the old network admin email address when the network admin email address changes.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.9.0
</span></span></pre>
</div>
</div>

</body>
</html>