<!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>[3510] sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory: Plugin Directory: Allow a user to subscribe/unsubscribe from plugin commit emails.</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="http://meta.trac.wordpress.org/changeset/3510">3510</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/3510","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-20 16:37:50 +0000 (Mon, 20 Jun 2016)</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'>Plugin Directory: Allow a user to subscribe/unsubscribe from plugin commit emails.

Note: Plugin committers are automatically subscribed, and cannot unsubscribe.

See <a href="http://meta.trac.wordpress.org/ticket/1597">#1597</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiclassbasephp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclasstoolsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryshortcodesclassdevelopersphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-developers.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiroutesclasscommitsubscriptionsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-commit-subscriptions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiclassbasephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php      2016-06-20 16:21:58 UTC (rev 3509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/class-base.php        2016-06-20 16:37:50 UTC (rev 3510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -11,6 +11,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Initialises each API route we offer.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        static function load_routes() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                new Routes\Commit_Subscriptions();
</ins><span class="cx" style="display: block; padding: 0 10px">                 new Routes\Internal_Stats();
</span><span class="cx" style="display: block; padding: 0 10px">                new Routes\Plugin();
</span><span class="cx" style="display: block; padding: 0 10px">                new Routes\Plugin_Translations();
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryapiroutesclasscommitsubscriptionsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-commit-subscriptions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-commit-subscriptions.php                               (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-commit-subscriptions.php 2016-06-20 16:37:50 UTC (rev 3510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,56 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Plugin_Directory\API\Routes;
+use WordPressdotorg\Plugin_Directory\Plugin_Directory;
+use WordPressdotorg\Plugin_Directory\API\Base;
+use WordPressdotorg\Plugin_Directory\Tools;
+
+/**
+ * An API endpoint for subscribing to commits for a particular plugin.
+ *
+ * @package WordPressdotorg_Plugin_Directory
+ */
+class Commit_Subscriptions extends Base {
+
+       public function __construct() {
+               register_rest_route( 'plugins/v1', '/plugin/(?P<plugin_slug>[^/]+)/commit-subscription', array(
+                       'methods'  => \WP_REST_Server::READABLE,
+                       'callback' => array( $this, 'subscribe' ),
+                       'args' => array(
+                               'plugin_slug' => array(
+                                       'validate_callback' => array( $this, 'validate_plugin_slug_callback' ),
+                               ),
+                               'subscribe' => array(
+                                       'validate_callback' => function( $bool ) { return is_numeric( $bool ); },
+                               ),
+                               'unsubscribe' => array(
+                                       'validate_callback' => function( $bool ) { return is_numeric( $bool ); },
+                               ),
+                       ),
+                       'permission_callback' => 'is_user_logged_in'
+               ) );
+       }
+
+       /**
+        * Endpoint to subscribe to a plugin's commits.
+        *
+        * @param \WP_REST_Request $request The Rest API Request.
+        * @return bool True if the subscription was successful.
+        */
+       public function subscribe( $request ) {
+               $location = get_permalink( Plugin_Directory::get_plugin_post( $request['plugin_slug'] ) ) . '#developers';
+               header( "Location: $location" );
+
+               $result = array(
+                       "location" => $location,
+               );
+
+               if ( ! isset( $request['subscribe'] ) && ! isset( $request['unsubscribe'] ) ) {
+                       $result['error'] = 'Unknown Action';
+               }
+
+               $result['subscribed'] = Tools::subscribe_to_plugin_commits( $request['plugin_slug'], get_current_user_id(), isset( $request['subscribe'] ) );
+
+               return (object) $result;
+       }
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-commit-subscriptions.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryclasstoolsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php 2016-06-20 16:21:58 UTC (rev 3509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php   2016-06-20 16:37:50 UTC (rev 3510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,5 +1,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> namespace WordPressdotorg\Plugin_Directory;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+use WP_User;
</ins><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">  * Various functions used by other processes, will make sense to move to specific classes.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -168,4 +169,70 @@
</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">+
+       /**
+        * Subscribe/Unsubscribe a user to a plugins commits.
+        *
+        * Plugin Committers are automatically subscribed to plugin commit
+        * emails and cannot unsubscribe.
+        *
+        * @param string      $plugin_slug The plugin to subscribe to.
+        * @param int|WP_User $user        The user to subscribe. Default current user.
+        * @param bool        $subscribe   Whether to subscribe (true) or unsubscribe (false).
+        *
+        * @return bool Whether the user is subscribed.
+        */
+       public static function subscribe_to_plugin_commits( $plugin_slug, $user = 0, $subscribe = true ) {
+               $post = Plugin_Directory::get_plugin_post( $plugin_slug );
+               if ( ! $post ) {
+                       return false;
+               }
+
+               $user = new WP_User( $user ?: get_current_user_id() );
+               if ( ! $user->exists() ) {
+                       return false;
+               }
+
+               $users = get_post_meta( $post->ID, '_commit_subscribed', true ) ?: array();
+
+               if ( $subscribe ) {
+                       $users[] = $user->ID;
+                       $users = array_unique( $users );
+               } else {
+                       if ( false !== ( $pos = array_search( $user->ID, $users, true ) ) ) {
+                               unset( $users[ $pos ] );
+                       }
+               }
+
+               update_post_meta( $post->ID, '_commit_subscribed', $users );
+
+               return self::subscribed_to_plugin_commits( $plugin_slug, $user->ID );
+       }
+
+       /**
+        * Determine if a user is subscribed to a plugins commits.
+        *
+        * Plugin Committers are automatically subscribed to commits, and this
+        * function does not respect that status.
+        *
+        * @param string      $plugin_slug The plugin to subscribe to.
+        * @param int|WP_User $user        The user to check. Default current user.
+        *
+        * @return bool Whether the specified user is subscribed to commits.
+        */
+       public static function subscribed_to_plugin_commits( $plugin_slug, $user = 0 ) {
+               $post = Plugin_Directory::get_plugin_post( $plugin_slug );
+               if ( ! $post ) {
+                       return false;
+               }
+
+               $user = new WP_User( $user ?: get_current_user_id() );
+               if ( ! $user->exists() ) {
+                       return false;
+               }
+
+               $users = get_post_meta( $post->ID, '_commit_subscribed', true ) ?: array();
+
+               return in_array( $user->ID, $users, true );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryshortcodesclassdevelopersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-developers.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-developers.php 2016-06-20 16:21:58 UTC (rev 3509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-developers.php   2016-06-20 16:37:50 UTC (rev 3510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,16 +31,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                $output .= '</ul>';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( is_user_logged_in() ) {
+                       $subscribed = Tools::subscribed_to_plugin_commits( $post, get_current_user_id() );
+
+                       $subscribe_change_url = esc_url( add_query_arg(
+                               array(
+                                       '_wpnonce' => wp_create_nonce( 'wp_rest' ),
+                                       ( $subscribed ? 'unsubscribe' : 'subscribe' ) => '1'
+                               ),
+                               home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/commit-subscription' )
+                       ) );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $output .
</span><span class="cx" style="display: block; padding: 0 10px">                        '<h5>' . __( 'Browse the code', 'wporg-plugins' ) . '</h5>' .
</span><span class="cx" style="display: block; padding: 0 10px">                        '<ul>' .
</span><span class="cx" style="display: block; padding: 0 10px">                                '<li>' .
</span><span class="cx" style="display: block; padding: 0 10px">                                        '<a href="' . esc_url( "https://plugins.trac.wordpress.org/log/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Development Log', 'wporg-plugins' ) . '</a>' . "\n" .
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        '<a href="' . esc_url( "https://plugins.trac.wordpress.org/log/{$post->post_name}/?limit=100&mode=stop_on_copy&format=rss" ) . '" rel="nofollow"><img src="//s.w.org/style/images/feedicon.png" /></a>' .
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 '<a href="' . esc_url( "https://plugins.trac.wordpress.org/log/{$post->post_name}/?limit=100&mode=stop_on_copy&format=rss" ) . '" rel="nofollow"><img src="https://s.w.org/style/images/feedicon.png" /></a>' .
</ins><span class="cx" style="display: block; padding: 0 10px">                                 '</li>' .
</span><span class="cx" style="display: block; padding: 0 10px">                                '<li><a href="' . esc_url( "https://plugins.svn.wordpress.org/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Subversion Repository', 'wporg-plugins' ) . '</a></li>' .
</span><span class="cx" style="display: block; padding: 0 10px">                                '<li><a href="' . esc_url( "https://plugins.trac.wordpress.org/browser/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Browse in Trac', 'wporg-plugins' ) . '</a></li>' .
</span><span class="cx" style="display: block; padding: 0 10px">                                '<li><a href="' . esc_url( "https://translate.wordpress.org/projects/wp-plugins/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Translation Contributors', 'wporg-plugins' ) . '</a></li>' .
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 ( !is_user_logged_in() ? '' : (
+                                       $subscribed ?
+                                       "<li><a href='$subscribe_change_url'>" . __( 'Unsubscribe from plugin commits', 'wporg-plugins' ) . '</a></li>' :
+                                       "<li><a href='$subscribe_change_url'>" . __( 'Subscribe to plugin commits via email', 'wporg-plugins' ) . '</a></li>'
+                                ) ) .
</ins><span class="cx" style="display: block; padding: 0 10px">                         '</ul>';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>