<!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>[29635] trunk: Rename the public methods in the session tokens API.</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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/29635">29635</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-08-27 02:06:53 +0000 (Wed, 27 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Rename the public methods in the session tokens API.

Introduces a new get( $token ) method. get_token() would not have made sense and spurred the overall renaming. Public methods are now get, get_all, verify, create, update, destroy, destroy_others, and destroy_all.

The protected abstract methods designed for alternative implementations remain the same.

props mdawaffe.
see <a href="http://core.trac.wordpress.org/ticket/20276">#20276</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespluggablephp">trunk/src/wp-includes/pluggable.php</a></li>
<li><a href="#trunksrcwpincludessessionphp">trunk/src/wp-includes/session.php</a></li>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.php</a></li>
<li><a href="#trunktestsphpunittestsusersessionphp">trunk/tests/phpunit/tests/user/session.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludespluggablephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/pluggable.php (29634 => 29635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/pluggable.php      2014-08-27 01:31:05 UTC (rev 29634)
+++ trunk/src/wp-includes/pluggable.php 2014-08-27 02:06:53 UTC (rev 29635)
</span><span class="lines">@@ -684,7 +684,7 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  $manager = WP_Session_Tokens::get_instance( $user->ID );
</span><del>-       if ( ! $manager->verify_token( $token ) ) {
</del><ins>+        if ( ! $manager->verify( $token ) ) {
</ins><span class="cx">           do_action( 'auth_cookie_bad_session_token', $cookie_elements );
</span><span class="cx">          return false;
</span><span class="cx">  }
</span><span class="lines">@@ -728,7 +728,7 @@
</span><span class="cx"> 
</span><span class="cx">  if ( ! $token ) {
</span><span class="cx">          $manager = WP_Session_Tokens::get_instance( $user_id );
</span><del>-               $token = $manager->create_token( $expiration );
</del><ins>+                $token = $manager->create( $expiration );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  $pass_frag = substr($user->user_pass, 8, 4);
</span><span class="lines">@@ -877,7 +877,7 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  $manager = WP_Session_Tokens::get_instance( $user_id );
</span><del>-       $token = $manager->create_token( $expiration );
</del><ins>+        $token = $manager->create( $expiration );
</ins><span class="cx"> 
</span><span class="cx">  $auth_cookie = wp_generate_auth_cookie( $user_id, $expiration, $scheme, $token );
</span><span class="cx">  $logged_in_cookie = wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token );
</span></span></pre></div>
<a id="trunksrcwpincludessessionphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/session.php (29634 => 29635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/session.php        2014-08-27 01:31:05 UTC (rev 29634)
+++ trunk/src/wp-includes/session.php   2014-08-27 02:06:53 UTC (rev 29635)
</span><span class="lines">@@ -18,6 +18,8 @@
</span><span class="cx">  /**
</span><span class="cx">   * Protected constructor.
</span><span class="cx">   *
</span><ins>+        * @since 4.0.0
+        *
</ins><span class="cx">    * @param int $user_id User whose session to manage.
</span><span class="cx">   */
</span><span class="cx">  protected function __construct( $user_id ) {
</span><span class="lines">@@ -50,19 +52,33 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Hashes a token for storage.
</del><ins>+         * Hashes a session token for storage.
</ins><span class="cx">    *
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access private
</span><span class="cx">   *
</span><del>-        * @param string $token Token to hash.
-        * @return string A hash of the token (a verifier).
</del><ins>+         * @param string $token Session token to hash.
+        * @return string A hash of the session token (a verifier).
</ins><span class="cx">    */
</span><span class="cx">  final private function hash_token( $token ) {
</span><span class="cx">          return hash( 'sha256', $token );
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Get a user's session.
+        *
+        * @since 4.0.0
+        * @access public
+        *
+        * @param string $token Session token
+        * @return array User session
+        */
+       final public function get( $token ) {
+               $verifier = $this->hash_token( $token );
+               return $this->get_session( $verifier );
+       }
+
+       /**
</ins><span class="cx">    * Validate a user's session token as authentic.
</span><span class="cx">   *
</span><span class="cx">   * Checks that the given token is present and hasn't expired.
</span><span class="lines">@@ -73,26 +89,29 @@
</span><span class="cx">   * @param string $token Token to verify.
</span><span class="cx">   * @return bool Whether the token is valid for the user.
</span><span class="cx">   */
</span><del>-       final public function verify_token( $token ) {
</del><ins>+        final public function verify( $token ) {
</ins><span class="cx">           $verifier = $this->hash_token( $token );
</span><span class="cx">          return (bool) $this->get_session( $verifier );
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Generate a cookie session identification token.
</del><ins>+         * Generate a session token and attach session information to it.
</ins><span class="cx">    *
</span><del>-        * A session identification token is a long, random string. It is used to
-        * link a cookie to an expiration time and to ensure that cookies become
-        * invalidated upon logout. This function generates a token and stores it
-        * with the associated expiration time.
</del><ins>+         * A session token is a long, random string. It is used in a cookie
+        * link that cookie to an expiration time and to ensure the cookie
+        * becomes invalidated upon logout.
</ins><span class="cx">    *
</span><ins>+        * This function generates a token and stores it with the associated
+        * expiration time (and potentially other session information via the
+        * `attach_session_information` filter).
+        *
</ins><span class="cx">    * @since 4.0.0
</span><span class="cx">   * @access public
</span><span class="cx">   *
</span><span class="cx">   * @param int $expiration Session expiration timestamp.
</span><del>-        * @return string Session identification token.
</del><ins>+         * @return string Session token.
</ins><span class="cx">    */
</span><del>-       final public function create_token( $expiration ) {
</del><ins>+        final public function create( $expiration ) {
</ins><span class="cx">           /**
</span><span class="cx">           * Filter the information attached to the newly created session.
</span><span class="cx">           *
</span><span class="lines">@@ -109,21 +128,21 @@
</span><span class="cx"> 
</span><span class="cx">          $token = wp_generate_password( 43, false, false );
</span><span class="cx"> 
</span><del>-               $this->update_token( $token, $session );
</del><ins>+                $this->update( $token, $session );
</ins><span class="cx"> 
</span><span class="cx">          return $token;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Updates a session based on its token.
</del><ins>+         * Update a session token.
</ins><span class="cx">    *
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access public
</span><span class="cx">   *
</span><del>-        * @param string $token Token to update.
</del><ins>+         * @param string $token Session token to update.
</ins><span class="cx">    * @param array  $session Session information.
</span><span class="cx">   */
</span><del>-       final public function update_token( $token, $session ) {
</del><ins>+        final public function update( $token, $session ) {
</ins><span class="cx">           $verifier = $this->hash_token( $token );
</span><span class="cx">          $this->update_session( $verifier, $session );
</span><span class="cx">  }
</span><span class="lines">@@ -134,9 +153,9 @@
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access public
</span><span class="cx">   *
</span><del>-        * @param string $token Token to destroy.
</del><ins>+         * @param string $token Session token to destroy.
</ins><span class="cx">    */
</span><del>-       final public function destroy_token( $token ) {
</del><ins>+        final public function destroy( $token ) {
</ins><span class="cx">           $verifier = $this->hash_token( $token );
</span><span class="cx">          $this->update_session( $verifier, null );
</span><span class="cx">  }
</span><span class="lines">@@ -148,15 +167,15 @@
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access public
</span><span class="cx">   *
</span><del>-        * @param string $token_to_keep Token to keep.
</del><ins>+         * @param string $token_to_keep Session token to keep.
</ins><span class="cx">    */
</span><del>-       final public function destroy_other_tokens( $token_to_keep ) {
</del><ins>+        final public function destroy_others( $token_to_keep ) {
</ins><span class="cx">           $verifier = $this->hash_token( $token_to_keep );
</span><span class="cx">          $session = $this->get_session( $verifier );
</span><span class="cx">          if ( $session ) {
</span><span class="cx">                  $this->destroy_other_sessions( $verifier );
</span><span class="cx">          } else {
</span><del>-                       $this->destroy_all_tokens();
</del><ins>+                        $this->destroy_all_sessions();
</ins><span class="cx">           }
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -175,23 +194,23 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Destroy all tokens for a user.
</del><ins>+         * Destroy all session tokens for a user.
</ins><span class="cx">    *
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access public
</span><span class="cx">   */
</span><del>-       final public function destroy_all_tokens() {
</del><ins>+        final public function destroy_all() {
</ins><span class="cx">           $this->destroy_all_sessions();
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Destroy all tokens for all users.
</del><ins>+         * Destroy all session tokens for all users.
</ins><span class="cx">    *
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access public
</span><span class="cx">   * @static
</span><span class="cx">   */
</span><del>-       final public static function destroy_all_tokens_for_all_users() {
</del><ins>+        final public static function destroy_all_for_all_users() {
</ins><span class="cx">           $manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
</span><span class="cx">          call_user_func( array( $manager, 'drop_sessions' ) );
</span><span class="cx">  }
</span><span class="lines">@@ -204,7 +223,7 @@
</span><span class="cx">   *
</span><span class="cx">   * @return array Sessions of a user.
</span><span class="cx">   */
</span><del>-       final public function get_all_sessions() {
</del><ins>+        final public function get_all() {
</ins><span class="cx">           return array_values( $this->get_sessions() );
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -224,7 +243,7 @@
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access protected
</span><span class="cx">   *
</span><del>-        * @param $verifier Verifier of the session to retrieve.
</del><ins>+         * @param string $verifier Verifier of the session to retrieve.
</ins><span class="cx">    * @return array|null The session, or null if it does not exist.
</span><span class="cx">   */
</span><span class="cx">  abstract protected function get_session( $verifier );
</span><span class="lines">@@ -237,7 +256,7 @@
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access protected
</span><span class="cx">   *
</span><del>-        * @param $verifier Verifier of the session to update.
</del><ins>+         * @param string $verifier Verifier of the session to update.
</ins><span class="cx">    */
</span><span class="cx">  abstract protected function update_session( $verifier, $session = null );
</span><span class="cx"> 
</span><span class="lines">@@ -248,7 +267,7 @@
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access protected
</span><span class="cx">   *
</span><del>-        * @param $verifier Verifier of the session to keep.
</del><ins>+         * @param string $verifier Verifier of the session to keep.
</ins><span class="cx">    */
</span><span class="cx">  abstract protected function destroy_other_sessions( $verifier );
</span><span class="cx"> 
</span><span class="lines">@@ -316,7 +335,7 @@
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access protected
</span><span class="cx">   *
</span><del>-        * @param $verifier Verifier of the session to retrieve.
</del><ins>+         * @param string $verifier Verifier of the session to retrieve.
</ins><span class="cx">    * @return array|null The session, or null if it does not exist
</span><span class="cx">   */
</span><span class="cx">  protected function get_session( $verifier ) {
</span><span class="lines">@@ -376,7 +395,7 @@
</span><span class="cx">   * @since 4.0.0
</span><span class="cx">   * @access protected
</span><span class="cx">   *
</span><del>-        * @param $verifier Verifier of the session to keep.
</del><ins>+         * @param string $verifier Verifier of the session to keep.
</ins><span class="cx">    */
</span><span class="cx">  protected function destroy_other_sessions( $verifier ) {
</span><span class="cx">          $session = $this->get_session( $verifier );
</span></span></pre></div>
<a id="trunksrcwpincludesuserphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/user.php (29634 => 29635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/user.php   2014-08-27 01:31:05 UTC (rev 29634)
+++ trunk/src/wp-includes/user.php      2014-08-27 02:06:53 UTC (rev 29635)
</span><span class="lines">@@ -2207,7 +2207,7 @@
</span><span class="cx">  */
</span><span class="cx"> function wp_get_all_sessions() {
</span><span class="cx">  $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
</span><del>-       return $manager->get_all_sessions();
</del><ins>+        return $manager->get_all();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -2219,7 +2219,7 @@
</span><span class="cx">  $token = wp_get_session_token();
</span><span class="cx">  if ( $token ) {
</span><span class="cx">          $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
</span><del>-               $manager->destroy_token( $token );
</del><ins>+                $manager->destroy( $token );
</ins><span class="cx">   }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -2232,7 +2232,7 @@
</span><span class="cx">  $token = wp_get_session_token();
</span><span class="cx">  if ( $token ) {
</span><span class="cx">          $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
</span><del>-               $manager->destroy_other_tokens( $token );
</del><ins>+                $manager->destroy_others( $token );
</ins><span class="cx">   }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -2243,5 +2243,5 @@
</span><span class="cx">  */
</span><span class="cx"> function wp_destroy_all_sessions() {
</span><span class="cx">  $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
</span><del>-       $manager->destroy_all_tokens();
</del><ins>+        $manager->destroy_all();
</ins><span class="cx"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsusersessionphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/user/session.php (29634 => 29635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/user/session.php       2014-08-27 01:31:05 UTC (rev 29634)
+++ trunk/tests/phpunit/tests/user/session.php  2014-08-27 02:06:53 UTC (rev 29635)
</span><span class="lines">@@ -18,35 +18,35 @@
</span><span class="cx"> 
</span><span class="cx">  function test_verify_and_destroy_token() {
</span><span class="cx">          $expiration = time() + DAY_IN_SECONDS;
</span><del>-               $token = $this->manager->create_token( $expiration );
-               $this->assertFalse( $this->manager->verify_token( 'foo' ) );
-               $this->assertTrue( $this->manager->verify_token( $token ) );
-               $this->manager->destroy_token( $token );
-               $this->assertFalse( $this->manager->verify_token( $token ) );
</del><ins>+                $token = $this->manager->create( $expiration );
+               $this->assertFalse( $this->manager->verify( 'foo' ) );
+               $this->assertTrue( $this->manager->verify( $token ) );
+               $this->manager->destroy( $token );
+               $this->assertFalse( $this->manager->verify( $token ) );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  function test_destroy_other_tokens() {
</span><span class="cx">          $expiration = time() + DAY_IN_SECONDS;
</span><del>-               $token_1 = $this->manager->create_token( $expiration );
-               $token_2 = $this->manager->create_token( $expiration );
-               $token_3 = $this->manager->create_token( $expiration );
-               $this->assertTrue( $this->manager->verify_token( $token_1 ) );
-               $this->assertTrue( $this->manager->verify_token( $token_2 ) );
-               $this->assertTrue( $this->manager->verify_token( $token_3 ) );
-               $this->manager->destroy_other_tokens( $token_2 );
-               $this->assertFalse( $this->manager->verify_token( $token_1 ) );
-               $this->assertTrue( $this->manager->verify_token( $token_2 ) );
-               $this->assertFalse( $this->manager->verify_token( $token_3 ) );
</del><ins>+                $token_1 = $this->manager->create( $expiration );
+               $token_2 = $this->manager->create( $expiration );
+               $token_3 = $this->manager->create( $expiration );
+               $this->assertTrue( $this->manager->verify( $token_1 ) );
+               $this->assertTrue( $this->manager->verify( $token_2 ) );
+               $this->assertTrue( $this->manager->verify( $token_3 ) );
+               $this->manager->destroy_others( $token_2 );
+               $this->assertFalse( $this->manager->verify( $token_1 ) );
+               $this->assertTrue( $this->manager->verify( $token_2 ) );
+               $this->assertFalse( $this->manager->verify( $token_3 ) );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  function test_destroy_all_tokens() {
</span><span class="cx">          $expiration = time() + DAY_IN_SECONDS;
</span><del>-               $token_1 = $this->manager->create_token( $expiration );
-               $token_2 = $this->manager->create_token( $expiration );
-               $this->assertTrue( $this->manager->verify_token( $token_1 ) );
-               $this->assertTrue( $this->manager->verify_token( $token_2 ) );
-               $this->manager->destroy_all_tokens();
-               $this->assertFalse( $this->manager->verify_token( $token_1 ) );
-               $this->assertFalse( $this->manager->verify_token( $token_2 ) );
</del><ins>+                $token_1 = $this->manager->create( $expiration );
+               $token_2 = $this->manager->create( $expiration );
+               $this->assertTrue( $this->manager->verify( $token_1 ) );
+               $this->assertTrue( $this->manager->verify( $token_2 ) );
+               $this->manager->destroy_all();
+               $this->assertFalse( $this->manager->verify( $token_1 ) );
+               $this->assertFalse( $this->manager->verify( $token_2 ) );
</ins><span class="cx">   }
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>