<!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>[7686] sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-meetup-admin.php: WordCamp: Make meetup badges go live</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/7686">7686</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/7686","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>2018-09-20 09:14:39 +0000 (Thu, 20 Sep 2018)</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'>WordCamp: Make meetup badges go live

This commit has two major changes:
1. Whenever wporg usernames for organizers are updated in meetup admin for a meetup, badges to wporg profiles will be pushed automatically.
2. Added a weekly cron, which will sync list of meetup organizers from meetup.com API and notify Community Support Team if new organizers are added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswcptwcptmeetupclassmeetupadminphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-meetup-admin.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswcptwcptmeetupclassmeetupadminphp"></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/wcpt/wcpt-meetup/class-meetup-admin.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/wcpt/wcpt-meetup/class-meetup-admin.php   2018-09-20 08:43:38 UTC (rev 7685)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-meetup-admin.php     2018-09-20 09:14:39 UTC (rev 7686)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,8 +22,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                public function __construct() {
</span><span class="cx" style="display: block; padding: 0 10px">                        parent::__construct();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        add_action( 'plugins_loaded', array( $this, 'schedule_cron_jobs' ) );
+                       add_action( 'wcpt_meetup_api_sync', array( $this, 'meetup_api_sync' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                         add_action( 'wcpt_metabox_save_done', array( $this, 'maybe_update_meetup_data' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        add_action( 'wcpt_metabox_value', array( $this, 'render_co_organizers_list' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        add_action( 'wcpt_metabox_save_done', array( $this, 'meetup_organizers_changed' ), 10, 2 );
+                       add_action( 'transition_post_status', array( $this, 'maybe_update_organizers' ), 10, 3 );
</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">                /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -328,9 +332,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->active_admin_notices[] = $result->get_error_code();
</span><span class="cx" style="display: block; padding: 0 10px">                                return;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       update_post_meta( $post_id, 'Last meetup.com API sync', time() );
-
</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">@@ -408,9 +409,109 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                update_post_meta( $post_id, 'Last meetup on', $group_details['last_event']['time'] / 1000 );
</span><span class="cx" style="display: block; padding: 0 10px">                                update_post_meta( $post_id, 'Last meetup RSVP count', $group_details['last_event']['yes_rsvp_count'] );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        update_post_meta( $post_id, 'Last meetup.com API sync', time() );
</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">                /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * Trigger action `update_meetup_organizers` when new organizers are added.
+                * Note: While we add badges to new organizers, we do not remove them from organizers who has stepped down.
+                * This is because, we want to give credit if someone has organized a meetup in the past even if they are not active presently.
+                *
+                * @param int   $post_id
+                * @param array $original_data
+                */
+               public function meetup_organizers_changed( $post_id, $original_data ){
+                       global $post;
+
+                       if ( $this->get_event_type() !== get_post_type() ) {
+                               return;
+                       }
+
+                       if ( 'wcpt-mtp-active' !== $post->post_status ) {
+                               return;
+                       }
+
+                       $organizers_list = $this->get_organizer_list(
+                               get_post_meta( $post_id, 'Primary organizer WordPress.org username', true ),
+                               get_post_meta( $post_id, 'Co-Organizers usernames (seperated by comma)', true )
+                       );
+
+                       $original_organizers_list = $this->get_organizer_list(
+                               $original_data['Primary organizer WordPress.org username'][0],
+                               $original_data['Co-Organizers usernames (seperated by comma)'][0]
+                       );
+
+                       $new_organizers = array_diff( $organizers_list, $original_organizers_list );
+
+                       $this->update_meetup_organizers( $new_organizers, $post );
+
+               }
+
+               /**
+                * If status is set to `Active in the Chapter` then add badges for all organizers in the list.
+                *
+                * @param string  $new_status
+                * @param string  $old_status
+                * @param WP_Post $post
+                */
+               public function maybe_update_organizers( $new_status, $old_status, $post ) {
+
+                       if ( $this->get_event_type() !== get_post_type() ) {
+                               return;
+                       }
+
+                       if ( 'wcpt-mtp-active' !== $post->post_status ) {
+                               return;
+                       }
+
+                       if ( $new_status === $old_status ) {
+                               // When both the status are same (and set to active), then we do not need to do anything. This is handled by meetup_organizers_changed function.
+                               return;
+                       }
+
+                       $organizers_list = $this->get_organizer_list(
+                               get_post_meta( $post->ID, 'Primary organizer WordPress.org username', true ),
+                               get_post_meta( $post->ID, 'Co-Organizers usernames (seperated by comma)', true )
+                       );
+
+                       $this->update_meetup_organizers( $organizers_list, $post );
+
+               }
+
+               /**
+                * Helper function for getting list of organizers.
+                *
+                * @param string $main_organizer
+                * @param string $co_organizers
+                *
+                * @return array
+                */
+               private function get_organizer_list( $main_organizer, $co_organizers ) {
+                       $organizer_list = array();
+                       if ( ! empty( $main_organizer ) ) {
+                               $organizer_list[] = $main_organizer;
+                       }
+
+                       if ( ! empty( $co_organizers ) ) {
+                               $co_organizers_list = array_map( 'trim', explode( ',', $co_organizers ) );
+                               $organizer_list = array_merge( $organizer_list, $co_organizers_list );
+                       }
+                       return $organizer_list;
+               }
+
+               /**
+                * Helper method which triggers action `update_meetup_organizers`
+                *
+                * @param $organizers
+                * @param $post
+                */
+               protected function update_meetup_organizers( $organizers, $post ) {
+                       if ( ! empty( $organizers ) ) {
+                               do_action( 'update_meetup_organizers', $organizers, $post );
+                       }
+               }
+
+               /**
</ins><span class="cx" style="display: block; padding: 0 10px">                  * List of admin notices.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @return array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -483,16 +584,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                public static function meta_keys( $meta_group = '' ) {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $info_keys = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'Meetup URL'                        => 'text',
-                               'Meetup Co-organizer names'         => 'meetup_coorganizers',
-                               'Meetup Location (From meetup.com)' => 'text',
-                               'Meetup members count'              => 'text',
-                               'Meetup group created on'           => 'date',
-                               'Number of past meetups'            => 'text',
-                               'Last meetup on'                    => 'date',
-                               'Last meetup RSVP count'            => 'text',
-                               'HelpScout link'                    => 'text',
-                               'Meetup Location'                   => 'text',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'Meetup URL'                                   => 'text',
+                               'Meetup Co-organizer names'                    => 'meetup_coorganizers',
+                               'Primary organizer WordPress.org username'     => 'text',
+                               'Co-Organizers usernames (seperated by comma)' => 'text',
+                               'Meetup Location (From meetup.com)'            => 'text',
+                               'Meetup group created on'                      => 'date',
+                               'Number of past meetups'                       => 'text',
+                               'Last meetup on'                               => 'date',
+                               'Last meetup RSVP count'                       => 'text',
+                               'HelpScout link'                               => 'text',
+                               'Meetup Location'                              => 'text',
</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">                        $application_keys = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -509,16 +611,14 @@
</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">                        $organizer_keys = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'Organizer Name'                               => 'text',
-                               'Email'                                        => 'text',
-                               'Primary organizer WordPress.org username'     => 'text',
-                               'Co-Organizers usernames (seperated by comma)' => 'text',
-                               'Organizer description'                        => 'text',
-                               'Date closed'                                  => 'date',
-                               'Slack'                                        => 'text',
-                               'Region'                                       => 'text',
-                               'Address'                                      => 'textarea',
-                               'Extra Comments'                               => 'textarea',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'Organizer Name'        => 'text',
+                               'Email'                 => 'text',
+                               'Organizer description' => 'text',
+                               'Date closed'           => 'date',
+                               'Slack'                 => 'text',
+                               'Region'                => 'text',
+                               'Address'               => 'textarea',
+                               'Extra Comments'        => 'textarea',
</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">                        $metadata_keys = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -558,7 +658,106 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $data;
</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">+                /**
+                * Schedule cron job for updating data from meetup API
+                */
+               public function schedule_cron_jobs() {
+                       if ( wp_next_scheduled( 'wcpt_meetup_api_sync' ) ) {
+                               return;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'wcpt_meetup_api_sync' );
+               }
+
+
+               /**
+                * Cron worker for syncing with Meetup.com API data
+                */
+               public static function meetup_api_sync() {
+                       $query = new WP_Query( array(
+                               'post_type'   => self::get_event_type(),
+                               'post_status' => 'wcpt-mtp-active',
+                               'fields'      => 'ids',
+                               'posts_per_page' => -1,
+                       ) );
+
+                       $new_meetup_org_data = array();
+                       foreach ( $query->posts as $post_id ) {
+
+                               $meetup_organizers = get_post_meta( $post_id, 'Meetup Co-organizer names', true );
+                               self::update_meetup_data( $post_id );
+                               $new_meetup_organizers = get_post_meta( $post_id, 'Meetup Co-organizer names', true );
+
+                               if ( empty( $new_meetup_organizers ) ) {
+                                       continue;
+                               }
+
+                               if ( empty( $meetup_organizers ) ) {
+                                       $new_ids = wp_list_pluck( $new_meetup_organizers, 'id' );
+                               } else {
+                                       $new_ids = array_diff(
+                                               wp_list_pluck( $new_meetup_organizers, 'id' ),
+                                               wp_list_pluck( $meetup_organizers, 'id' )
+                                       );
+                               }
+
+                               if ( empty ( $new_ids ) ) {
+                                       continue;
+                               }
+
+                               $new_meetup_org_data[ $post_id ] = array();
+
+                               foreach ( $new_meetup_organizers as $org ) {
+                                       if ( in_array( $org['id'], $new_ids ) ) {
+                                               $new_meetup_org_data[ $post_id ][] = $org;
+                                       }
+                               }
+                       }
+                       self::new_meetup_organizers_notify( $new_meetup_org_data );
+               }
+
+               /**
+                * Send email containing new meetup organizers to WordCamp Support team.
+                *
+                * @param array $new_meetup_org_data
+                */
+               public static function new_meetup_organizers_notify( $new_meetup_org_data ) {
+                       if ( empty( $new_meetup_org_data ) ) {
+                               return;
+                       }
+
+                       $template = <<<HTML
+Hi,
+<br><br>
+New organizers have been added for following meetups. Please update their wporg usernames in their meetup tracker page.
+<br><br>
+HTML;
+                       $count = 0;
+                       foreach ( $new_meetup_org_data as $post_id => $new_meetup_org ) {
+                               $count += 1;
+                               $title = get_the_title( $post_id );
+                               $meetup_tracker_url = get_site_url() . "/wp-admin/post.php?post=$post_id&action=edit";
+                               $template = $template . "$count. <a href='$meetup_tracker_url' rel='noreferrer' target='_blank' >$title</a> : ";
+                               $meetup_group_url = get_post_meta( $post_id, 'Meetup URL', true );
+                               $meetup_members = array();
+                               foreach ( $new_meetup_org as $organizer ) {
+                                       $organizer_id = esc_html( $organizer['id'] );
+                                       $organizer_name = esc_html( $organizer['name'] );
+                                       $meetup_members[] = "<a href='$meetup_group_url/members/$organizer_id' target='_blank' rel='noreferrer' >$organizer_name</a>";
+                               }
+                               $template = $template . join( ', ', $meetup_members ) . "<br>";
+                       }
+                       // TODO: Change to email before merging
+                       wp_mail(
+                               array( 'support@wordcamp.com' ),
+                               'New Meetup organizer added',
+                               $template,
+                               array(
+                                       'From:         noreply@wordcamp.org',
+                                       'Content-Type: text/html; charset=UTF-8',
+                               )
+                       );
+               }
</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"> endif;
</span></span></pre>
</div>
</div>

</body>
</html>