Making WordPress.org


Ignore:
Timestamp:
02/20/2015 07:55:57 PM (10 years ago)
Author:
iandunn
Message:

WordCamp Forms to Drafts: Add handler for Call for Speakers form.

Props nvwd
See #567

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-forms-to-drafts/wordcamp-forms-to-drafts.php

    r698 r1287  
    2222     */
    2323    public function __construct() {
     24        add_filter( 'the_content',              array( $this, 'force_login_to_view_form' ), 9 );
     25        add_action( 'template_redirect',        array( $this, 'populate_form_based_on_user' ), 9 );
    2426        add_action( 'grunion_pre_message_sent', array( $this, 'returning_organizer_application' ), 10, 3 );
    2527        add_action( 'grunion_pre_message_sent', array( $this, 'new_organizer_application' ), 10, 3 );
    2628        add_action( 'grunion_pre_message_sent', array( $this, 'call_for_sponsors' ), 10, 3 );
     29        add_action( 'grunion_pre_message_sent', array( $this, 'call_for_speakers' ), 10, 3 );
     30    }
     31
     32    /**
     33     * Force user to login to view certain forms.
     34     *
     35     * @param string $content
     36     *
     37     * @return string
     38     */
     39    public function force_login_to_view_form( $content ) {
     40        global $post;
     41        $forms_that_require_login = array( 'call-for-speakers' );
     42        $shortcode_pattern        = '/\[contact-form[\D\S]+\[\/contact-form\]/';
     43        $form_id                  = get_post_meta( $post->ID, 'wcfd-key', true );
     44
     45        if ( in_array( $form_id, $forms_that_require_login ) && ! is_user_logged_in() ) {
     46            switch ( $form_id ) {
     47                case 'call-for-speakers':
     48                    $please_login_message = sprintf(
     49                        __( 'Before submitting your speaker proposal, please <a href="%s">log into your WordPress.org account</a>*.', 'wordcamporg' ),
     50                        wp_login_url( get_permalink() )
     51                    );
     52                    break;
     53            }
     54
     55            if ( ! empty( $please_login_message ) ) {
     56                $please_login_message = str_replace(
     57                    __( 'Please use your <strong>WordPress.org</strong>* account to log in.', 'wordcamporg' ),
     58                    $please_login_message,
     59                    wcorg_login_message( '', get_permalink() )
     60                );
     61
     62                $content = preg_replace( $shortcode_pattern, $please_login_message, $content );
     63            }
     64        }
     65
     66        return $content;
     67    }
     68
     69    /**
     70     * Populate certain form fields based on the current user.
     71     *
     72     * @todo Maybe remove username field, or make it readonly. We don't need an explicit field since we can just
     73     *       grab the current user value, but it might be good to let them see/change which one they're using.
     74     */
     75    public function populate_form_based_on_user() {
     76        global $current_user, $post;
     77
     78        if ( ! is_user_logged_in() ) {
     79            return;
     80        }
     81
     82        get_currentuserinfo();
     83        $form_id = get_post_meta( $post->ID, 'wcfd-key', true );
     84
     85        switch ( $form_id ) {
     86            case 'call-for-speakers':
     87                $default_values = array(
     88                    'Name'                   => $current_user->display_name,
     89                    'Email Address'          => $current_user->user_email,
     90                    'WordPress.org Username' => $current_user->user_login,
     91                );
     92
     93                foreach ( $default_values as $field_label => $default_value ) {
     94                    $field_id = $this->get_grunion_field_id( $post->ID, $field_label );
     95
     96                    if ( ! isset( $_POST[ $field_id ] ) ) {
     97                        $_POST[ $field_id ] = $default_value;
     98                    }
     99                }
     100
     101                break;
     102        }
     103    }
     104
     105    /**
     106     * Get the Grunion field ID
     107     *
     108     * This is a simplified version of what happens in Grunion_Contact_Form_Field::__construct()
     109     *
     110     * @todo submit Jetpack PR to modularize that logic so we can just call it directly instead of duplicating it
     111     *
     112     * @param int $page_id
     113     * @param string $label
     114     *
     115     * @return string
     116     */
     117    protected function get_grunion_field_id( $page_id, $label ) {
     118        $id = sprintf(
     119            'g%s-%s',
     120            $page_id,
     121            sanitize_title_with_dashes( preg_replace( '/[^a-zA-Z0-9.-_:]/', '', $label ) )
     122        );
     123
     124        return $id;
     125    }
     126
     127    /**
     128     * Remove prefixes from form labels
     129     *
     130     * Grunion prefixes the field keys with 'N_', so that 'Name becomes '1_Name'. That prevents directly accessing
     131     * the values, since the number is unknown.
     132     *
     133     * @param array $prefixed_values
     134     *
     135     * @return array
     136     */
     137    protected function get_unprefixed_grunion_form_values( $prefixed_values ) {
     138        $unprefixed_values = array();
     139
     140        foreach ( $prefixed_values as $key => $value ) {
     141            $unprefixed_values[ preg_replace( '#^\d+_#i', '', $key ) ] = $value;
     142        }
     143
     144        return $unprefixed_values;
    27145    }
    28146
     
    34152     *
    35153     * @param int $submission_id
     154     *
    36155     * @return string | false
    37156     */
     
    51170     *
    52171     * @param string $username
     172     *
    53173     * @return int
    54174     */
     
    62182     *
    63183     * This plugin may need to insert a form into a different site, and the targeted post type may not be active
    64      * on the current site. If we don't do this, PHP notices will be generated and will break the post/get/redirect
     184     * on the current site. If we don't do this, PHP notices will be generated and will break the post/redirect/get
    65185     * flow because of the early headers.
    66186     *
     
    185305     *
    186306     * @todo
     307     * - Update WordCamp_New_Site to inject wcfd-key meta and anything else necessary to make this active for new
     308     *   sites
    187309     * - Add jetpack form field for Sponsor Level, where options are automatically pulled from wcb_sponsor_level
    188310     *   taxonomy and the selected term is applied to the drafted post. Maybe need to send PR to add filter to
     
    206328
    207329        // Create the post
     330        // todo need to unprefix $all_values, like call_for_speakers() does? if so, extract into DRY function
    208331        $draft_id = wp_insert_post( array(
    209332            'post_type'    => 'wcb_sponsor',
     
    223346        }
    224347    }
     348
     349    /**
     350     * Create draft Speaker and Session posts from a Call for Speakers form submission.
     351     *
     352     * @todo Add jetpack form field for Track, where options are automatically pulled from wcb_track
     353     *       taxonomy and the selected term(s) is applied to the drafted post.
     354     * @todo If creating speaker or session fails, report to organizer so that submission doesn't get missed
     355     *
     356     * @param int   $submission_id
     357     * @param array $all_values
     358     * @param array $extra_values
     359     */
     360    public function call_for_speakers( $submission_id, $all_values, $extra_values ) {
     361        if ( 'call-for-speakers' != $this->get_form_key( $submission_id ) ) {
     362            return;
     363        }
     364
     365        global $current_user;
     366
     367        $all_values = $this->get_unprefixed_grunion_form_values( $all_values );
     368
     369        if ( ! $speaker_user_id = $this->get_user_id_from_username( $all_values['WordPress.org Username'] ) ) {
     370            $speaker_user_id                      = $current_user->ID;
     371            $all_values['WordPress.org Username'] = $current_user->user_login;
     372        }
     373
     374        $speaker = $this->get_speaker_from_user_id( $speaker_user_id );
     375
     376        if ( ! is_a( $speaker, 'WP_Post' ) ) {
     377            $speaker_id = $this->create_draft_speaker( $all_values );
     378
     379            if ( ! is_a( $speaker_id, 'WP_Error' ) ) {
     380                $speaker = get_post( $speaker_id );
     381            }
     382        }
     383
     384        if ( is_a( $speaker, 'WP_Post' ) ) {
     385            $this->create_draft_session( $all_values, $speaker );
     386        }
     387    }
     388
     389    /**
     390     * Get speaker post based on WordPress.org user name
     391     *
     392     * @param int $user_id
     393     *
     394     * @return WP_Post | false
     395     */
     396    protected function get_speaker_from_user_id( $user_id ) {
     397        $speaker_query = new WP_Query( array(
     398            'post_type'      => 'wcb_speaker',
     399            'posts_per_page' => 1,
     400            'post_status'    => array( 'publish', 'pending', 'draft', 'future', 'private' ),    // Trashed speakers are ignored because they'll likely be deleted
     401            'meta_query'     => array(
     402                array(
     403                    'key'     => '_wcpt_user_id',
     404                    'value'   => $user_id,
     405                    'compare' => '=',
     406                ),
     407            ),
     408        ) );
     409
     410        return empty( $speaker_query->post ) ? false : $speaker_query->post;
     411    }
     412
     413    /**
     414     * Create a drafted speaker post
     415     *
     416     * @param array $speaker
     417     *
     418     * @return int | WP_Error
     419     */
     420    protected function create_draft_speaker( $speaker ) {
     421        $speaker_id = wp_insert_post(
     422            array(
     423                'post_type'    => 'wcb_speaker',
     424                'post_title'   => $speaker['Name'],
     425                'post_content' => $speaker['Your Bio'],
     426                'post_status'  => 'draft',
     427                'post_author'  => $this->get_user_id_from_username( 'wordcamp' ),
     428            ),
     429            true
     430        );
     431
     432        if ( $speaker_id ) {
     433            update_post_meta( $speaker_id, '_wcb_speaker_email', $speaker[ 'Email Address' ] );
     434            update_post_meta( $speaker_id, '_wcpt_user_id',      $this->get_user_id_from_username( $speaker['WordPress.org Username'] ) );
     435        }
     436
     437        return $speaker_id;
     438    }
     439
     440    /**
     441     * Create a drafted session post
     442     *
     443     * @param array   $session
     444     * @param WP_Post $speaker
     445     *
     446     * @return int | WP_Error
     447     */
     448    protected function create_draft_session( $session, $speaker ) {
     449        $session_id = wp_insert_post(
     450            array(
     451                'post_type'    => 'wcb_session',
     452                'post_title'   => $session['Topic Title'],
     453                'post_content' => $session['Topic Description'],
     454                'post_status'  => 'draft',
     455                'post_author'  => $this->get_user_id_from_username( $session['WordPress.org Username'] ),
     456            ),
     457            true
     458        );
     459
     460        if ( $session_id ) {
     461            update_post_meta( $session_id, '_wcpt_speaker_id',      $speaker->ID );
     462            update_post_meta( $session_id, '_wcb_session_speakers', $speaker->post_title );
     463        }
     464
     465        return $session_id;
     466    }
    225467} // end WordCamp_Forms_To_Drafts
    226468
Note: See TracChangeset for help on using the changeset viewer.