<!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>[819] sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php: Participation Notifier: Add support for attendee registration.</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://meta.trac.wordpress.org/changeset/819">819</a></dd>
<dt>Author</dt> <dd>iandunn</dd>
<dt>Date</dt> <dd>2014-08-27 14:43:24 +0000 (Wed, 27 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Participation Notifier: Add support for attendee registration.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcampparticipationnotifierwordcampparticipationnotifierphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcampparticipationnotifierwordcampparticipationnotifierphp"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php (818 => 819)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php      2014-08-27 00:43:35 UTC (rev 818)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php 2014-08-27 14:43:24 UTC (rev 819)
</span><span class="lines">@@ -15,7 +15,8 @@
</span><span class="cx">   * Constructor
</span><span class="cx">   */
</span><span class="cx">  public function __construct() {
</span><del>-               add_action( 'transition_post_status', array( $this, 'post_updated' ), 5, 3 );
</del><ins>+                add_action( 'transition_post_status',                 array( $this, 'post_updated' ), 5, 3 );
+               add_action( 'camptix_require_login_confirm_username', array( $this, 'attendee_registered' ), 10, 2 );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="lines">@@ -25,20 +26,24 @@
</span><span class="cx">   *
</span><span class="cx">   * This hooks in before the custom post types save their post meta fields, so that we can access the
</span><span class="cx">   * WordPress.org username from the previous revision and from the current one.
</span><ins>+        *
+        * @todo Maybe refactor this to work more like attendee_registered(), so the speaker/sponsor plugins just fire a
+        *       hook when they're ready to send the notification, rather than this plugin having to be aware of (and
+        *       coupled to) the internal logic of those plugins.
</ins><span class="cx">    * 
</span><span class="cx">   * @param string  $new_status
</span><span class="cx">   * @param string  $old_status
</span><span class="cx">   * @param WP_Post $post
</span><span class="cx">   */
</span><del>-       function post_updated( $new_status, $old_status, $post ) {
</del><ins>+        public function post_updated( $new_status, $old_status, $post ) {
</ins><span class="cx">           if ( ! $this->is_post_notifiable( $post ) ) {
</span><span class="cx">                  return;
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="cx">          if ( 'publish' == $new_status && 'publish' == $old_status ) {
</span><del>-                       $this->published_post_updated( $post );
</del><ins>+                        $this->published_speaker_post_updated( $post );
</ins><span class="cx">           } elseif ( 'publish' == $new_status || 'publish' == $old_status ) {
</span><del>-                       $this->post_published_or_unpublished( $new_status, $old_status, $post );
</del><ins>+                        $this->speaker_post_published_or_unpublished( $new_status, $old_status, $post );
</ins><span class="cx">           }
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -63,16 +68,17 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Updates the activity and associations of a profile when the WordPress.org username on a published post changes.
</del><ins>+         * Updates the activity and associations of a profile when the WordPress.org username on a published speaker
+        * or organizer post changes.
</ins><span class="cx">    * 
</span><span class="cx">   * @todo The handler doesn't support removing activity, but maybe do that here if support is added.
</span><span class="cx">   * 
</span><span class="cx">   * @param WP_Post $post
</span><span class="cx">   */
</span><del>-       public function published_post_updated( $post ) {
-               $previous_user_id = $this->get_wporg_user_id( $post, false );
-               $new_user_id      = $this->get_wporg_user_id( $post );
-               
</del><ins>+        protected function published_speaker_post_updated( $post ) {
+               $previous_user_id = $this->get_saved_wporg_user_id( $post );
+               $new_user_id      = $this->get_new_wporg_user_id( $post );
+
</ins><span class="cx">           // There is no username, or it hasn't changed, so we don't need to do anything here.
</span><span class="cx">          if ( $previous_user_id === $new_user_id ) {
</span><span class="cx">                  return;
</span><span class="lines">@@ -98,8 +104,8 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Adds new activity and associations to a user's profile when custom posts are published, and removes
-        * associations when custom posts are unpublished.
</del><ins>+         * Adds new activity and associations to a user's profile when speaker or organizer posts are published, and
+        * removes associations, when speaker or organizer posts are unpublished.
</ins><span class="cx">    *
</span><span class="cx">   * @todo The handler doesn't support removing activity, but maybe do that here if support is added.
</span><span class="cx">   *
</span><span class="lines">@@ -107,7 +113,7 @@
</span><span class="cx">   * @param string  $old_status
</span><span class="cx">   * @param WP_Post $post
</span><span class="cx">   */
</span><del>-       public function post_published_or_unpublished( $new_status, $old_status, $post ) {
</del><ins>+        protected function speaker_post_published_or_unpublished( $new_status, $old_status, $post ) {
</ins><span class="cx">           if ( 'publish' == $new_status ) {
</span><span class="cx">                  $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) );
</span><span class="cx">                  $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'add' ) );
</span><span class="lines">@@ -115,22 +121,44 @@
</span><span class="cx">                  // Get the $user_id from post meta instead of $_POST in case it changed during the unpublish update.
</span><span class="cx">                  // This makes sure that the association is removed from the same user that it was originally added to.
</span><span class="cx">                  
</span><del>-                       $user_id = $this->get_wporg_user_id( $post, false );
</del><ins>+                        $user_id = $this->get_saved_wporg_user_id( $post );
</ins><span class="cx">                   $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'remove', $user_id ) );
</span><span class="cx">          }
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Adds new activity to a user's profile when they register for a ticket.
+        *
+        * @todo Handle cases where the user changes, either from the admin editing the back-end post, or a from a
+        *       different user updating via the edit token?
+        * @todo The handler doesn't support removing activity, but maybe do that here if support is added.
+        *
+        * @param int $attendee_id
+        * @param string $username
+        */
+       public function attendee_registered( $attendee_id, $username ) {
+               $attendee = get_post( $attendee_id );
+               $user_id  = $this->get_saved_wporg_user_id( $attendee );
+
+               $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $attendee, $user_id ) );
+       }
+
+       /**
</ins><span class="cx">    * Builds the payload for an activity notification based on a new post
</span><span class="cx">   * 
</span><span class="cx">   * @param WP_Post $post
</span><ins>+        * @param int     $user_id
+        *
</ins><span class="cx">    * @return array|false
</span><span class="cx">   */
</span><del>-       protected function get_post_activity_payload( $post ) {
</del><ins>+        protected function get_post_activity_payload( $post, $user_id = null ) {
</ins><span class="cx">           $activity = false;
</span><del>-               $user_id  = $this->get_wporg_user_id( $post );
-               $wordcamp = get_wordcamp_post(); 
-               
</del><ins>+                $wordcamp = get_wordcamp_post();
+
+               if ( ! $user_id ) {
+                       $user_id = $this->get_new_wporg_user_id( $post );
+               }
+
</ins><span class="cx">           if ( $user_id ) {
</span><span class="cx">                  $activity = array(
</span><span class="cx">                          'action'        => 'wporg_handle_activity',
</span><span class="lines">@@ -151,6 +179,10 @@
</span><span class="cx">                          case 'wcb_organizer':
</span><span class="cx">                                  $activity['organizer_id'] = $post->ID;
</span><span class="cx">                          break;
</span><ins>+
+                               case 'tix_attendee':
+                                       $activity['attendee_id']  = $post->ID;
+                               break;
</ins><span class="cx">   
</span><span class="cx">                          default:
</span><span class="cx">                                  $activity = false;
</span><span class="lines">@@ -173,7 +205,7 @@
</span><span class="cx">          $association = false;
</span><span class="cx">          
</span><span class="cx">          if ( ! $user_id ) {
</span><del>-                       $user_id = $this->get_wporg_user_id( $post );
</del><ins>+                        $user_id = $this->get_new_wporg_user_id( $post );
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          if ( $user_id ) {
</span><span class="lines">@@ -195,7 +227,8 @@
</span><span class="cx">                          case 'wcb_organizer':
</span><span class="cx">                                  $association['association'] = 'wordcamp-organizer';
</span><span class="cx">                          break;
</span><del>-       
</del><ins>+
+                               case 'tix_attendee':
</ins><span class="cx">                           default:
</span><span class="cx">                                  $association = false;
</span><span class="cx">                          break;
</span><span class="lines">@@ -206,28 +239,50 @@
</span><span class="cx">  }
</span><span class="cx">  
</span><span class="cx">  /**
</span><del>-        * Get the WordPress.org user_id associated with a custom post
</del><ins>+         * Get the current WordPress.org user_id associated with a custom post
</ins><span class="cx">    *
</span><del>-        * When creating a new post, the user_id may be needed before the post meta is saved,
-        * so we first check the request for the latest value.
</del><ins>+         * This is called during the context of a post being updated, so the new username is the one submitted in
+        * the $_POST request, or the currently logged in user, as opposed to the user_id saved in the database.
</ins><span class="cx">    * 
</span><span class="cx">   * @param WP_Post $post
</span><del>-        * @param string $username_field
</del><span class="cx">    * @return false|int
</span><span class="cx">   */
</span><del>-       protected function get_wporg_user_id( $post, $username_field = 'wcpt-wporg-username' ) {
</del><ins>+        protected function get_new_wporg_user_id( $post ) {
+               $user_id = $user = false;
+
+               if ( in_array( $post->post_type, array( 'wcb_speaker', 'wcb_organizer' ) ) && isset( $_POST['wcpt-wporg-username'] ) ) {
+                       $user = get_user_by( 'login', $_POST['wcpt-wporg-username'] );
+               }
+
+               if ( ! empty( $user->ID ) ) {
+                       $user_id = $user->ID;
+               }
+
+               return $user_id;
+       }
+
+       /**
+        * Get the previous WordPress.org user_id associated with a custom post
+        *
+        * This is called during the context of a post being updated, so the saved username is the one saved in
+        * the database, as opposed to the one in the $_POST request or the currently logged in user.
+        *
+        * @param WP_Post $post
+        * @return false|int
+        */
+       protected function get_saved_wporg_user_id( $post ) {
</ins><span class="cx">           $user_id = false;
</span><del>-               
-               if ( $username_field && isset( $_POST[ $username_field ] ) ) {
-                       $user = get_user_by( 'login', $_POST[ $username_field ] );
-                       
-                       if ( ! empty( $user->ID ) ) {
</del><ins>+
+               if ( in_array( $post->post_type, array( 'wcb_speaker', 'wcb_organizer' ) ) ) {
+                       $user_id = (int) get_post_meta( $post->ID, '_wcpt_user_id', true );
+               } elseif ( 'tix_attendee' == $post->post_type ) {
+                       $user = get_user_by( 'login', get_post_meta( $post->ID, 'tix_username', true ) );
+
+                       if ( is_a( $user, 'WP_User' ) ) {
</ins><span class="cx">                           $user_id = $user->ID;
</span><span class="cx">                  }
</span><del>-               } else {
-                       $user_id = (int) get_post_meta( $post->ID, '_wcpt_user_id', true );
</del><span class="cx">           }
</span><del>-               
</del><ins>+
</ins><span class="cx">           return $user_id;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -243,8 +298,6 @@
</span><span class="cx">  protected function remote_post( $url, $body ) {
</span><span class="cx">          $response = $error = false;
</span><span class="cx"> 
</span><del>-               // todo verify/update when can see actual responses from server
-               
</del><span class="cx">           if ( $body ) {
</span><span class="cx">                  $response = wp_remote_post( $url, array( 'body' => $body ) );
</span><span class="cx">          
</span></span></pre>
</div>
</div>

</body>
</html>