Making WordPress.org

Changeset 819


Ignore:
Timestamp:
08/27/2014 02:43:24 PM (11 years ago)
Author:
iandunn
Message:

Participation Notifier: Add support for attendee registration.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-participation-notifier/wordcamp-participation-notifier.php

    r439 r819  
    1616     */
    1717    public function __construct() {
    18         add_action( 'transition_post_status', array( $this, 'post_updated' ), 5, 3 );
     18        add_action( 'transition_post_status',                 array( $this, 'post_updated' ), 5, 3 );
     19        add_action( 'camptix_require_login_confirm_username', array( $this, 'attendee_registered' ), 10, 2 );
    1920    }
    2021
     
    2627     * This hooks in before the custom post types save their post meta fields, so that we can access the
    2728     * WordPress.org username from the previous revision and from the current one.
     29     *
     30     * @todo Maybe refactor this to work more like attendee_registered(), so the speaker/sponsor plugins just fire a
     31     *       hook when they're ready to send the notification, rather than this plugin having to be aware of (and
     32     *       coupled to) the internal logic of those plugins.
    2833     *
    2934     * @param string  $new_status
     
    3136     * @param WP_Post $post
    3237     */
    33     function post_updated( $new_status, $old_status, $post ) {
     38    public function post_updated( $new_status, $old_status, $post ) {
    3439        if ( ! $this->is_post_notifiable( $post ) ) {
    3540            return;
     
    3742
    3843        if ( 'publish' == $new_status && 'publish' == $old_status ) {
    39             $this->published_post_updated( $post );
     44            $this->published_speaker_post_updated( $post );
    4045        } elseif ( 'publish' == $new_status || 'publish' == $old_status ) {
    41             $this->post_published_or_unpublished( $new_status, $old_status, $post );
     46            $this->speaker_post_published_or_unpublished( $new_status, $old_status, $post );
    4247        }
    4348    }
     
    6469
    6570    /**
    66      * Updates the activity and associations of a profile when the WordPress.org username on a published post changes.
     71     * Updates the activity and associations of a profile when the WordPress.org username on a published speaker
     72     * or organizer post changes.
    6773     *
    6874     * @todo The handler doesn't support removing activity, but maybe do that here if support is added.
     
    7076     * @param WP_Post $post
    7177     */
    72     public function published_post_updated( $post ) {
    73         $previous_user_id = $this->get_wporg_user_id( $post, false );
    74         $new_user_id      = $this->get_wporg_user_id( $post );
    75        
     78    protected function published_speaker_post_updated( $post ) {
     79        $previous_user_id = $this->get_saved_wporg_user_id( $post );
     80        $new_user_id      = $this->get_new_wporg_user_id( $post );
     81
    7682        // There is no username, or it hasn't changed, so we don't need to do anything here.
    7783        if ( $previous_user_id === $new_user_id ) {
     
    99105
    100106    /**
    101      * Adds new activity and associations to a user's profile when custom posts are published, and removes
    102      * associations when custom posts are unpublished.
     107     * Adds new activity and associations to a user's profile when speaker or organizer posts are published, and
     108     * removes associations, when speaker or organizer posts are unpublished.
    103109     *
    104110     * @todo The handler doesn't support removing activity, but maybe do that here if support is added.
     
    108114     * @param WP_Post $post
    109115     */
    110     public function post_published_or_unpublished( $new_status, $old_status, $post ) {
     116    protected function speaker_post_published_or_unpublished( $new_status, $old_status, $post ) {
    111117        if ( 'publish' == $new_status ) {
    112118            $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $post ) );
     
    116122            // This makes sure that the association is removed from the same user that it was originally added to.
    117123           
    118             $user_id = $this->get_wporg_user_id( $post, false );
     124            $user_id = $this->get_saved_wporg_user_id( $post );
    119125            $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_association_payload( $post, 'remove', $user_id ) );
    120126        }
     
    122128
    123129    /**
     130     * Adds new activity to a user's profile when they register for a ticket.
     131     *
     132     * @todo Handle cases where the user changes, either from the admin editing the back-end post, or a from a
     133     *       different user updating via the edit token?
     134     * @todo The handler doesn't support removing activity, but maybe do that here if support is added.
     135     *
     136     * @param int $attendee_id
     137     * @param string $username
     138     */
     139    public function attendee_registered( $attendee_id, $username ) {
     140        $attendee = get_post( $attendee_id );
     141        $user_id  = $this->get_saved_wporg_user_id( $attendee );
     142
     143        $this->remote_post( self::PROFILES_HANDLER_URL, $this->get_post_activity_payload( $attendee, $user_id ) );
     144    }
     145
     146    /**
    124147     * Builds the payload for an activity notification based on a new post
    125148     *
    126149     * @param WP_Post $post
     150     * @param int     $user_id
     151     *
    127152     * @return array|false
    128153     */
    129     protected function get_post_activity_payload( $post ) {
     154    protected function get_post_activity_payload( $post, $user_id = null ) {
    130155        $activity = false;
    131         $user_id  = $this->get_wporg_user_id( $post );
    132         $wordcamp = get_wordcamp_post();
    133        
     156        $wordcamp = get_wordcamp_post();
     157
     158        if ( ! $user_id ) {
     159            $user_id = $this->get_new_wporg_user_id( $post );
     160        }
     161
    134162        if ( $user_id ) {
    135163            $activity = array(
     
    152180                    $activity['organizer_id'] = $post->ID;
    153181                break;
     182
     183                case 'tix_attendee':
     184                    $activity['attendee_id']  = $post->ID;
     185                break;
    154186   
    155187                default:
     
    174206       
    175207        if ( ! $user_id ) {
    176             $user_id = $this->get_wporg_user_id( $post );
     208            $user_id = $this->get_new_wporg_user_id( $post );
    177209        }
    178210
     
    196228                    $association['association'] = 'wordcamp-organizer';
    197229                break;
    198    
     230
     231                case 'tix_attendee':
    199232                default:
    200233                    $association = false;
     
    207240   
    208241    /**
    209      * Get the WordPress.org user_id associated with a custom post
    210      *
    211      * When creating a new post, the user_id may be needed before the post meta is saved,
    212      * so we first check the request for the latest value.
    213      *
    214      * @param WP_Post $post
    215      * @param string $username_field
     242     * Get the current WordPress.org user_id associated with a custom post
     243     *
     244     * This is called during the context of a post being updated, so the new username is the one submitted in
     245     * the $_POST request, or the currently logged in user, as opposed to the user_id saved in the database.
     246     *
     247     * @param WP_Post $post
    216248     * @return false|int
    217249     */
    218     protected function get_wporg_user_id( $post, $username_field = 'wcpt-wporg-username' ) {
     250    protected function get_new_wporg_user_id( $post ) {
     251        $user_id = $user = false;
     252
     253        if ( in_array( $post->post_type, array( 'wcb_speaker', 'wcb_organizer' ) ) && isset( $_POST['wcpt-wporg-username'] ) ) {
     254            $user = get_user_by( 'login', $_POST['wcpt-wporg-username'] );
     255        }
     256
     257        if ( ! empty( $user->ID ) ) {
     258            $user_id = $user->ID;
     259        }
     260
     261        return $user_id;
     262    }
     263
     264    /**
     265     * Get the previous WordPress.org user_id associated with a custom post
     266     *
     267     * This is called during the context of a post being updated, so the saved username is the one saved in
     268     * the database, as opposed to the one in the $_POST request or the currently logged in user.
     269     *
     270     * @param WP_Post $post
     271     * @return false|int
     272     */
     273    protected function get_saved_wporg_user_id( $post ) {
    219274        $user_id = false;
    220        
    221         if ( $username_field && isset( $_POST[ $username_field ] ) ) {
    222             $user = get_user_by( 'login', $_POST[ $username_field ] );
    223            
    224             if ( ! empty( $user->ID ) ) {
     275
     276        if ( in_array( $post->post_type, array( 'wcb_speaker', 'wcb_organizer' ) ) ) {
     277            $user_id = (int) get_post_meta( $post->ID, '_wcpt_user_id', true );
     278        } elseif ( 'tix_attendee' == $post->post_type ) {
     279            $user = get_user_by( 'login', get_post_meta( $post->ID, 'tix_username', true ) );
     280
     281            if ( is_a( $user, 'WP_User' ) ) {
    225282                $user_id = $user->ID;
    226283            }
    227         } else {
    228             $user_id = (int) get_post_meta( $post->ID, '_wcpt_user_id', true );
    229         }
    230        
     284        }
     285
    231286        return $user_id;
    232287    }
     
    244299        $response = $error = false;
    245300
    246         // todo verify/update when can see actual responses from server
    247        
    248301        if ( $body ) {
    249302            $response = wp_remote_post( $url, array( 'body' => $body ) );
Note: See TracChangeset for help on using the changeset viewer.