<!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>[8907] sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php: WC: Refactor participant notifier to fix design flaw.</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="http://meta.trac.wordpress.org/changeset/8907">8907</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/8907","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>vedjain</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-06-03 14:36:46 +0000 (Mon, 03 Jun 2019)</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'>WC: Refactor participant notifier to fix design flaw. See 703. See desc.
We currently assume that a profile will be associated with only single WordCamp, and handle adding and removing badges (speaker/organizer) with this assumption.
However, this assumption is incorrect and causes badge disappearing bug, as described in 703.
This patch refactors the participant-notifier class such that:
1. Whenever a badge is added, an entry is made in to usermeta table, with meta key as wc_{post_type}_{blog_id}_{post_id]. This has following benefits:
a. Usermeta table is shared across all WordCamp, so this can act as a central place to record whenever a badge is added for a user.
b. Since meta key columns is indexed, a count query can be done on `wc_{post_type}_%` with `user_id = {user_id}` clause.
Because of how MySQL stores indexes, prefix queries are also as performant as full text queries. This gives us ability to find out in how many WordCamp a user is registered as a speaker/organizer using a single performant query.
We can use the results of this query to decide if we want to remove a badge or not.
2. In block editor, as per https://github.com/WordPress/gutenberg/issues/12897, POST data will not be there when publishing a draft post. This patch adds a workaround by checking if we need to add/remove badge even if both previous and next status are `publish`.
3. Uses a meta data to prevent duplicate activity log request. This is necessary because of workaround described in above patch.
Fixes 703</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 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php 2019-06-03 14:36:38 UTC (rev 8906)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php 2019-06-03 14:36:46 UTC (rev 8907)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -112,38 +112,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Updates the activity and associations of a profile when the WordPress.org username on a published speaker
</span><span class="cx" style="display: block; padding: 0 10px"> * or organizer post changes.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * IMPORTANT NOTE: If a draft post is published via block editor, we will have to add badges and activity here instead of `post_published_or_unpublished` method.
+ * This is because when post is updated via Block editor, the status change request will not have any POST data, see @link https://github.com/WordPress/gutenberg/issues/12897
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @todo The handler doesn't support removing activity, but maybe do that here if support is added.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Post $post
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function published_post_updated( $post ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $previous_user_id = $this->get_saved_wporg_user_id( $post );
- $new_user_id = $this->get_new_wporg_user_id( $post );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $previous_user_id = $this->get_saved_wporg_user_id( $post );
+ $new_user_id = $this->get_new_wporg_user_id( $post );
+ $published_activity_key = $this->get_published_activity_key( $post );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // There is no username, or it hasn't changed, so we don't need to do anything here.
- if ( $previous_user_id === $new_user_id ) {
- return;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ if ( $previous_user_id ) {
+ $this->maybe_remove_badge( $post, $previous_user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // todo change this to use an if/elseif/elseif structure, just to be safe
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $new_user_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // A new username was added, so add the activity and association.
- if ( $new_user_id && ! $previous_user_id ) {
- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) );
- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'add' ) );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! get_user_meta( $new_user_id, $published_activity_key ) ) {
+ $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) );
+ update_user_meta( $new_user_id, $published_activity_key, true );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The username was removed, so remove the association.
- if ( ! $new_user_id && $previous_user_id ) {
- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'remove', $previous_user_id ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->add_badge( $post, $new_user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- // The username changed, so remove the association from the previous user and add both the activity and association to the new user.
- if ( $new_user_id && $previous_user_id ) {
- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'remove', $previous_user_id ) );
- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) );
- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'add' ) );
- }
</del><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -158,18 +152,127 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function post_published_or_unpublished( $new_status, $old_status, $post ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'publish' == $new_status ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) );
- $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'add' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user_id = $this->get_new_wporg_user_id( $post );
+
+ if ( ! get_user_meta( $user_id, $this->get_published_activity_key( $post ) ) ) {
+ $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) );
+ update_user_meta( $user_id, $this->get_published_activity_key( $post ), true );
+ }
+
+ $this->add_badge( $post, $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif( 'publish' == $old_status ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Get the $user_id from post meta instead of $_POST in case it changed during the unpublish update.
- // This makes sure that the association is removed from the same user that it was originally added to.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user_id = $this->get_saved_wporg_user_id( $post );
+ $this->maybe_remove_badge( $post, $user_id );
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user_id = $this->get_saved_wporg_user_id( $post );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Makes request to Profile URL to add badge to organizer/speaker. Also adds a meta entry which is used by `maybe_remove_badge` function to figure out whether to remove a badge or not.
+ *
+ * @param WP_Post $post Speaker/Organizer Post Object.
+ * @param int $user_id User ID to add badge for.
+ */
+ protected function add_badge( $post, $user_id ) {
+ if ( ! in_array( $post->post_type, [ 'wcb_speaker', 'wcb_organizer' ] ) || ! $user_id ) {
+ return;
+ }
+
+ $meta_key = $this->get_user_meta_key( $post );
+
+ // User already has a badge. Prevent wasteful API call and bail.
+ if ( get_user_meta( $user_id, $meta_key ) ) {
+ return;
+ }
+
+ update_user_meta( $user_id ,$meta_key, true );
+
+ $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'add', $user_id ) );
+ }
+
+ /**
+ * Makes a request to remove speaker/organizer badge from a user if the user is removed from all WordCamp where they were speaker/organizer.
+ *
+ * @param WP_Post $post Speaker/Organizer Post Object.
+ * @param int $user_id User ID to remove the badge for.
+ */
+ protected function maybe_remove_badge( $post, $user_id ) {
+ global $wpdb;
+
+ if ( ! in_array( $post->post_type, [ 'wcb_speaker', 'wcb_organizer' ] ) ) {
+ return;
+ }
+
+ $meta_key = $this->get_user_meta_key( $post );
+
+ // User does not have a badge anyway. Prevent wasteful API call and bail.
+ if ( ! $user_id || ! get_user_meta( $user_id, $meta_key ) ) {
+ return;
+ }
+
+ delete_user_meta( $user_id, $meta_key );
+
+ $meta_key_prefix = $this->get_user_meta_key_prefix( $post );
+
+ $count = $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT COUNT(*) FROM $wpdb->usermeta
+ WHERE
+ user_id = %d
+ AND meta_key like '$meta_key_prefix%';
+ ",
+ $user_id
+ )
+ );
+
+ if ( '0' === $count ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'remove', $user_id ) );
</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><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Meta key to store user metadata for organizer/speaker association.
+ *
+ * We store meta_key in form of wc_{post_type}_{blog_id}_{post_id} because
+ *
+ * 1. Since meta_key is indexed, count query on wc_{post_type}% for a specific user_id will be performant. We will use this query to figure out if we want to remove a badge.
+ *
+ * 2. Adding separate rows per WordCamp per User per Post allows us to avoid incorrect badge removal in some cases.
+ * For egs, when an organizer accidentally adds same wporg name to multiple speaker post, and then corrects their mistake by removing it in all but one post.
+ *
+ * 3. Because of verbosity, deleting user meta entry will be less error prone.
+ *
+ * @param WP_Post $post Sponsor/Organizer post object.
+ *
+ * @return string
+ */
+ private function get_user_meta_key( $post ) {
+ return $this->get_user_meta_key_prefix( $post ) . get_current_blog_id() . '_' . $post->ID;
+ }
+
+ /**
+ * Meta key prefix to store user metadata for organizer/speaker association. This prefix is used in count query to figure if a user should have an organizer/speaker badge.
+ *
+ * @param WP_Post $post Sponsor/Organizer post object.
+ *
+ * @return string
+ */
+ private function get_user_meta_key_prefix( $post ) {
+ return 'wc_' . $post->post_type . '_';
+ }
+
+ /**
+ * Meta key name to store user matadata for whether activity is published or not.
+ * Used to prevent publishing duplicate activities.
+ *
+ * @param WP_Post $post Post object, will be organizer/sponsor/tix_attendee.
+ *
+ * @return string
+ */
+ private function get_published_activity_key( $post ) {
+ return 'wc_published_activity_' . get_current_blog_id() . "_$post->ID";
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Send a notification when someone successfully registered for a ticket.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * If they purchased multiple tickets, this will only send a notification for the one they bought for theirself.
</span></span></pre>
</div>
</div>
</body>
</html>