Making WordPress.org

Changeset 7686


Ignore:
Timestamp:
09/20/2018 09:14:39 AM (7 years ago)
Author:
vedjain
Message:

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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-meetup-admin.php

    r7654 r7686  
    2323            parent::__construct();
    2424
     25            add_action( 'plugins_loaded', array( $this, 'schedule_cron_jobs' ) );
     26            add_action( 'wcpt_meetup_api_sync', array( $this, 'meetup_api_sync' ) );
    2527            add_action( 'wcpt_metabox_save_done', array( $this, 'maybe_update_meetup_data' ) );
    2628            add_action( 'wcpt_metabox_value', array( $this, 'render_co_organizers_list' ) );
     29            add_action( 'wcpt_metabox_save_done', array( $this, 'meetup_organizers_changed' ), 10, 2 );
     30            add_action( 'transition_post_status', array( $this, 'maybe_update_organizers' ), 10, 3 );
    2731        }
    2832
     
    329333                return;
    330334            }
    331 
    332             update_post_meta( $post_id, 'Last meetup.com API sync', time() );
    333 
    334335        }
    335336
     
    408409                update_post_meta( $post_id, 'Last meetup on', $group_details['last_event']['time'] / 1000 );
    409410                update_post_meta( $post_id, 'Last meetup RSVP count', $group_details['last_event']['yes_rsvp_count'] );
     411            }
     412            update_post_meta( $post_id, 'Last meetup.com API sync', time() );
     413        }
     414
     415        /**
     416         * Trigger action `update_meetup_organizers` when new organizers are added.
     417         * Note: While we add badges to new organizers, we do not remove them from organizers who has stepped down.
     418         * This is because, we want to give credit if someone has organized a meetup in the past even if they are not active presently.
     419         *
     420         * @param int   $post_id
     421         * @param array $original_data
     422         */
     423        public function meetup_organizers_changed( $post_id, $original_data ){
     424            global $post;
     425
     426            if ( $this->get_event_type() !== get_post_type() ) {
     427                return;
     428            }
     429
     430            if ( 'wcpt-mtp-active' !== $post->post_status ) {
     431                return;
     432            }
     433
     434            $organizers_list = $this->get_organizer_list(
     435                get_post_meta( $post_id, 'Primary organizer WordPress.org username', true ),
     436                get_post_meta( $post_id, 'Co-Organizers usernames (seperated by comma)', true )
     437            );
     438
     439            $original_organizers_list = $this->get_organizer_list(
     440                $original_data['Primary organizer WordPress.org username'][0],
     441                $original_data['Co-Organizers usernames (seperated by comma)'][0]
     442            );
     443
     444            $new_organizers = array_diff( $organizers_list, $original_organizers_list );
     445
     446            $this->update_meetup_organizers( $new_organizers, $post );
     447
     448        }
     449
     450        /**
     451         * If status is set to `Active in the Chapter` then add badges for all organizers in the list.
     452         *
     453         * @param string  $new_status
     454         * @param string  $old_status
     455         * @param WP_Post $post
     456         */
     457        public function maybe_update_organizers( $new_status, $old_status, $post ) {
     458
     459            if ( $this->get_event_type() !== get_post_type() ) {
     460                return;
     461            }
     462
     463            if ( 'wcpt-mtp-active' !== $post->post_status ) {
     464                return;
     465            }
     466
     467            if ( $new_status === $old_status ) {
     468                // 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.
     469                return;
     470            }
     471
     472            $organizers_list = $this->get_organizer_list(
     473                get_post_meta( $post->ID, 'Primary organizer WordPress.org username', true ),
     474                get_post_meta( $post->ID, 'Co-Organizers usernames (seperated by comma)', true )
     475            );
     476
     477            $this->update_meetup_organizers( $organizers_list, $post );
     478
     479        }
     480
     481        /**
     482         * Helper function for getting list of organizers.
     483         *
     484         * @param string $main_organizer
     485         * @param string $co_organizers
     486         *
     487         * @return array
     488         */
     489        private function get_organizer_list( $main_organizer, $co_organizers ) {
     490            $organizer_list = array();
     491            if ( ! empty( $main_organizer ) ) {
     492                $organizer_list[] = $main_organizer;
     493            }
     494
     495            if ( ! empty( $co_organizers ) ) {
     496                $co_organizers_list = array_map( 'trim', explode( ',', $co_organizers ) );
     497                $organizer_list = array_merge( $organizer_list, $co_organizers_list );
     498            }
     499            return $organizer_list;
     500        }
     501
     502        /**
     503         * Helper method which triggers action `update_meetup_organizers`
     504         *
     505         * @param $organizers
     506         * @param $post
     507         */
     508        protected function update_meetup_organizers( $organizers, $post ) {
     509            if ( ! empty( $organizers ) ) {
     510                do_action( 'update_meetup_organizers', $organizers, $post );
    410511            }
    411512        }
     
    484585
    485586            $info_keys = array(
    486                 'Meetup URL'                        => 'text',
    487                 'Meetup Co-organizer names'         => 'meetup_coorganizers',
    488                 'Meetup Location (From meetup.com)' => 'text',
    489                 'Meetup members count'              => 'text',
    490                 'Meetup group created on'           => 'date',
    491                 'Number of past meetups'            => 'text',
    492                 'Last meetup on'                    => 'date',
    493                 'Last meetup RSVP count'            => 'text',
    494                 'HelpScout link'                    => 'text',
    495                 'Meetup Location'                   => 'text',
     587                'Meetup URL'                                   => 'text',
     588                'Meetup Co-organizer names'                    => 'meetup_coorganizers',
     589                'Primary organizer WordPress.org username'     => 'text',
     590                'Co-Organizers usernames (seperated by comma)' => 'text',
     591                'Meetup Location (From meetup.com)'            => 'text',
     592                'Meetup group created on'                      => 'date',
     593                'Number of past meetups'                       => 'text',
     594                'Last meetup on'                               => 'date',
     595                'Last meetup RSVP count'                       => 'text',
     596                'HelpScout link'                               => 'text',
     597                'Meetup Location'                              => 'text',
    496598            );
    497599
     
    510612
    511613            $organizer_keys = array(
    512                 'Organizer Name'                               => 'text',
    513                 'Email'                                        => 'text',
    514                 'Primary organizer WordPress.org username'     => 'text',
    515                 'Co-Organizers usernames (seperated by comma)' => 'text',
    516                 'Organizer description'                        => 'text',
    517                 'Date closed'                                  => 'date',
    518                 'Slack'                                        => 'text',
    519                 'Region'                                       => 'text',
    520                 'Address'                                      => 'textarea',
    521                 'Extra Comments'                               => 'textarea',
     614                'Organizer Name'        => 'text',
     615                'Email'                 => 'text',
     616                'Organizer description' => 'text',
     617                'Date closed'           => 'date',
     618                'Slack'                 => 'text',
     619                'Region'                => 'text',
     620                'Address'               => 'textarea',
     621                'Extra Comments'        => 'textarea',
    522622            );
    523623
     
    559659        }
    560660
    561 
     661        /**
     662         * Schedule cron job for updating data from meetup API
     663         */
     664        public function schedule_cron_jobs() {
     665            if ( wp_next_scheduled( 'wcpt_meetup_api_sync' ) ) {
     666                return;
     667            }
     668
     669            wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'wcpt_meetup_api_sync' );
     670        }
     671
     672
     673        /**
     674         * Cron worker for syncing with Meetup.com API data
     675         */
     676        public static function meetup_api_sync() {
     677            $query = new WP_Query( array(
     678                'post_type'   => self::get_event_type(),
     679                'post_status' => 'wcpt-mtp-active',
     680                'fields'      => 'ids',
     681                'posts_per_page' => -1,
     682            ) );
     683
     684            $new_meetup_org_data = array();
     685            foreach ( $query->posts as $post_id ) {
     686
     687                $meetup_organizers = get_post_meta( $post_id, 'Meetup Co-organizer names', true );
     688                self::update_meetup_data( $post_id );
     689                $new_meetup_organizers = get_post_meta( $post_id, 'Meetup Co-organizer names', true );
     690
     691                if ( empty( $new_meetup_organizers ) ) {
     692                    continue;
     693                }
     694
     695                if ( empty( $meetup_organizers ) ) {
     696                    $new_ids = wp_list_pluck( $new_meetup_organizers, 'id' );
     697                } else {
     698                    $new_ids = array_diff(
     699                        wp_list_pluck( $new_meetup_organizers, 'id' ),
     700                        wp_list_pluck( $meetup_organizers, 'id' )
     701                    );
     702                }
     703
     704                if ( empty ( $new_ids ) ) {
     705                    continue;
     706                }
     707
     708                $new_meetup_org_data[ $post_id ] = array();
     709
     710                foreach ( $new_meetup_organizers as $org ) {
     711                    if ( in_array( $org['id'], $new_ids ) ) {
     712                        $new_meetup_org_data[ $post_id ][] = $org;
     713                    }
     714                }
     715            }
     716            self::new_meetup_organizers_notify( $new_meetup_org_data );
     717        }
     718
     719        /**
     720         * Send email containing new meetup organizers to WordCamp Support team.
     721         *
     722         * @param array $new_meetup_org_data
     723         */
     724        public static function new_meetup_organizers_notify( $new_meetup_org_data ) {
     725            if ( empty( $new_meetup_org_data ) ) {
     726                return;
     727            }
     728
     729            $template = <<<HTML
     730Hi,
     731<br><br>
     732New organizers have been added for following meetups. Please update their wporg usernames in their meetup tracker page.
     733<br><br>
     734HTML;
     735            $count = 0;
     736            foreach ( $new_meetup_org_data as $post_id => $new_meetup_org ) {
     737                $count += 1;
     738                $title = get_the_title( $post_id );
     739                $meetup_tracker_url = get_site_url() . "/wp-admin/post.php?post=$post_id&action=edit";
     740                $template = $template . "$count. <a href='$meetup_tracker_url' rel='noreferrer' target='_blank' >$title</a> : ";
     741                $meetup_group_url = get_post_meta( $post_id, 'Meetup URL', true );
     742                $meetup_members = array();
     743                foreach ( $new_meetup_org as $organizer ) {
     744                    $organizer_id = esc_html( $organizer['id'] );
     745                    $organizer_name = esc_html( $organizer['name'] );
     746                    $meetup_members[] = "<a href='$meetup_group_url/members/$organizer_id' target='_blank' rel='noreferrer' >$organizer_name</a>";
     747                }
     748                $template = $template . join( ', ', $meetup_members ) . "<br>";
     749            }
     750            // TODO: Change to email before merging
     751            wp_mail(
     752                array( 'support@wordcamp.com' ),
     753                'New Meetup organizer added',
     754                $template,
     755                array(
     756                    'From:         noreply@wordcamp.org',
     757                    'Content-Type: text/html; charset=UTF-8',
     758                )
     759            );
     760        }
    562761    }
    563762
Note: See TracChangeset for help on using the changeset viewer.