Making WordPress.org

Ticket #567: 567.2.diff

File 567.2.diff, 10.8 KB (added by iandunn, 11 years ago)
  • plugins/wcpt/wcpt-wordcamp/wordcamp-new-site.php

     
    88         */
    99        public function __construct() {
    1010                $this->new_site_id = false;
    11                
     11
    1212                add_action( 'wcpt_metabox_value', array( $this, 'render_site_url_field' ), 10, 3 );
    1313                add_action( 'wcpt_metabox_save',  array( $this, 'save_site_url_field' ), 10, 3 );
    1414        }
     
    209209                        ) );
    210210
    211211                        if ( $page_id ) {
     212                                // Save post meta
     213                                if ( ! empty( $page['meta'] ) ) {
     214                                        foreach ( $page['meta'] as $key => $value ) {
     215                                                update_post_meta( $page_id, $key, $value );
     216                                        }
     217                                }
     218
    212219                                // Set featured image
    213220                                if ( isset( $page['featured_image'] ) ) {
    214221                                        $results = media_sideload_image( $page['featured_image'], $page_id );
     
    367374
    368375                        array(
    369376                                'title'   => 'Call for Speakers',
    370                                 'content' => "<p><em>Organizers note:</em> Make sure you update the 'to' address and other fields before publishing this page!</p> <p>Blurb with information for potential speakers.</p> <p>[contact-form to='enter-your-address-here@example.net' subject='WordCamp Speaker Request'][contact-field label='Name' type='text' required='1'/][contact-field label='Email' type='email' required='1'/][contact-field label='Topic(s) You%26#039;d Like to Present On' type='textarea' required='1'/][contact-field label='Intended Audience' type='text' required='1'/][contact-field label='Past Speaking Experience (not necessary to apply)' type='textarea'/][/contact-form]</p>",
     377                                'content' => "<p><em>Organizers note:</em> Submissions to this form will automatically create draft posts for the Speaker and Session post types. Feel free to customize the form, but deleting or renaming the following fields will break the automation: Name, Email, WordPress.org Username, Your Bio, Session Title, Session Description.</p> <p>If you'd like to propose multiple topics, please submit the form multiple times, once for each topic. [Other speaker instructions/info goes here.]</p> <p>[contact-form subject='WordCamp Speaker Request'][contact-field label='Name' type='name' required='1'/][contact-field label='Email Address' type='email' required='1'/][contact-field label='WordPress.org Username' type='text' required='1'/][contact-field label='Your Bio' type='textarea' required='1'/][contact-field label='Topic Title' type='text' required='1'/][contact-field label='Topic Description' type='textarea' required='1'/][contact-field label='Intended Audience' type='text' required='1'/][contact-field label='Past Speaking Experience (not necessary to apply)' type='textarea'/][/contact-form]</p>",
    371378                                'status'  => 'draft',
    372379                                'type'    => 'post',
     380                                'meta'    => array(
     381                                        'wcfd-key' => 'call-for-speakers',
     382                                ),
    373383                        ),
    374384
    375385                        array(
  • plugins/wordcamp-forms-to-drafts/wordcamp-forms-to-drafts.php

     
    2424                add_action( 'grunion_pre_message_sent', array( $this, 'returning_organizer_application' ), 10, 3 );
    2525                add_action( 'grunion_pre_message_sent', array( $this, 'new_organizer_application' ), 10, 3 );
    2626                add_action( 'grunion_pre_message_sent', array( $this, 'call_for_sponsors' ), 10, 3 );
     27                add_action( 'grunion_pre_message_sent', array( $this, 'call_for_speakers' ), 10, 3 );
     28                add_filter( 'the_content',              array( $this, 'force_login_to_view_form' ), 9 );
     29                add_action( 'template_redirect',        array( $this, 'populate_form_based_on_user' ), 9 );
    2730        }
    2831
    2932        /**
     33         * Force user to login to view certain forms.
     34         *
     35         * @param string $content
     36         * @return string
     37         */
     38        public function force_login_to_view_form( $content ) {
     39                global $post;
     40                $form_id                  = get_post_meta( $post->ID, 'wcfd-key', true );
     41                $form_pattern             = '/\[contact-form[\D\S]+\[\/contact-form\]/';
     42                $forms_that_require_login = array( 'call-for-speakers' );
     43
     44                if ( in_array( $form_id, $forms_that_require_login ) && ! is_user_logged_in() ) {
     45                        switch ( $form_id ) {
     46                                case 'call-for-speakers':
     47                                        $please_login_message = sprintf(
     48                                                __( 'Before submitting your speaker proposal, please <a href="%s">log into your WordPress.org account</a>*.', 'wordcamporg' ),
     49                                                wp_login_url( get_permalink() )
     50                                        );
     51                                        break;
     52                        }
     53
     54                        if ( ! empty( $please_login_message ) ) {
     55                                $please_login_message = str_replace(
     56                                        __( 'Please use your <strong>WordPress.org</strong>* account to login.', 'wordcamporg' ),
     57                                        $please_login_message,
     58                                        wcorg_login_message( '', get_permalink() )  // todo enqueue message styles for this
     59                                );
     60
     61                                $content = preg_replace( $form_pattern, $please_login_message, $content );
     62                        }
     63                }
     64
     65                return $content;
     66        }
     67
     68        /**
     69         * Populate certain form fields based on the current user.
     70         */
     71        public function populate_form_based_on_user() {
     72                global $current_user, $post;
     73
     74                if ( ! is_user_logged_in() ) {
     75                        return;
     76                }
     77
     78                get_currentuserinfo();
     79                $form_id = get_post_meta( $post->ID, 'wcfd-key', true );
     80
     81                switch ( $form_id ) {
     82                        case 'call-for-speakers':
     83                                $_POST[ $this->get_grunion_field_id( $post->ID, 'WordPress.org Username' ) ] = $current_user->user_login;
     84                                break;
     85                }
     86        }
     87
     88        /**
     89         * Get the Grunion field ID
     90         *
     91         * This is a simplified version of what happens in Grunion_Contact_Form_Field::__construct()
     92         *
     93         * @todo submit Jetpack PR to modularize that logic so we can just call it directly instead of duplicating it
     94         *
     95         * @param int $page_id
     96         * @param string $label
     97         * @return string
     98         */
     99        protected function get_grunion_field_id( $page_id, $label ) {
     100                $id = sprintf(
     101                        'g%s-%s',
     102                        $page_id,
     103                        sanitize_title_with_dashes( preg_replace( '/[^a-zA-Z0-9.-_:]/', '', $label ) )
     104                );
     105
     106                return $id;
     107        }
     108
     109        /**
    30110         * Identify the form that the submission is associated with.
    31111         *
    32112         * This requires that the post containing the [contact-form] shortcode has a meta field named 'wcfd-key' added
     
    38118        protected function get_form_key( $submission_id ) {
    39119                $key = false;
    40120                $submission = get_post( $submission_id );
    41 
    42121                if ( ! empty( $submission->post_parent ) ) {
    43122                        $key = get_post_meta( $submission->post_parent, 'wcfd-key', true );
    44123                }
     
    58137        }
    59138
    60139        /**
     140         * Get speaker post ID based on WP.org user name
     141         *
     142         * @param int $user_id
     143         * @return WP_Post | 0
     144         */
     145        protected function get_speaker_from_userid( $user_id ) {
     146
     147                $speaker_check_args = array(
     148                        'post_type' => 'wcb_speaker',
     149                        'posts_per_page' => 1,
     150                        'post_status' => 'all',
     151                        'meta_query' => array(
     152                                array(
     153                                        'key' => '_wcpt_user_id',
     154                                        'value' => $user_id,
     155                                        'compare' => '=',
     156                                ),
     157                        ),
     158                );
     159
     160                $speaker_query = new WP_Query( $speaker_check_args );
     161                wp_reset_postdata();
     162                return empty( $speaker_query->post ) ? 0 : $speaker_query->post;
     163
     164        }
     165
     166        /**
    61167         * Simulate the existence of a post type.
    62168         *
    63169         * 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
     170         * on the current site. If we don't do this, PHP notices will be generated and will break the post/redirect/get
    65171         * flow because of the early headers.
    66172         *
    67173         * Yes, this is an ugly hack.
     
    222328                        }
    223329                }
    224330        }
     331
     332        /**
     333         * Create draft Speaker and Session posts from a Call for Speakers form submission.
     334         *
     335         * @todo
     336         * - Add jetpack form field for Track, where options are automatically pulled from wcb_track
     337         *   taxonomy and the selected term(s) is applied to the drafted post.
     338         * - Put form behind required login and pre-populate the username field - works but hacky
     339         * - Make the speaker the author of their session. May have to add the speaker as an author
     340         *   if their session is approved.
     341         *
     342         * @param int   $submission_id
     343         * @param array $all_values
     344         * @param array $extra_values
     345         */
     346        public function call_for_speakers( $submission_id, $all_values, $extra_values ) {
     347
     348                global $current_user;
     349                get_currentuserinfo();
     350
     351                if ( 'call-for-speakers' != $this->get_form_key( $submission_id ) ) {
     352                        return;
     353                }
     354
     355                // process the speaker part of the form
     356
     357                // check for existing speaker entry
     358                $all_values['UserID'] = $this->get_user_id_from_username( $current_user->user_login );
     359                $speaker_obj = $this->get_speaker_from_userid( $all_values['UserID'] );
     360
     361                $this->simulate_post_type( 'wordcamp' );
     362
     363                if ( empty( $speaker_obj ) ) {
     364
     365                        $speaker_to_form_key_map = array(
     366                                '_wcpt_speaker_website' => 'Website',
     367                                '_wcpt_speaker_twitter_handle' => 'Twitter Handle',
     368                                '_wcb_speaker_email' => 'Email',
     369                                '_wcpt_user_id' => 'UserID',
     370                        );
     371
     372                        // Create the speaker draft post
     373                        $speaker_draft_id = wp_insert_post( array(
     374                                'post_type'    => 'wcb_speaker',
     375                                'post_title'   => $all_values['Name'],
     376                                'post_content' => $all_values['Your Bio'],
     377                                'post_status'  => 'draft',
     378                                'post_author'  => $this->get_user_id_from_username( 'wordcamp' ),
     379                        ) );
     380
     381                        // Create the speaker post meta
     382                        if ( $speaker_draft_id ) {
     383                                foreach ( $speaker_to_form_key_map as $speaker_key => $form_key ) {
     384                                        if ( ! empty( $all_values[ $form_key ] ) ) {
     385                                                update_post_meta( $speaker_draft_id, $speaker_key, $all_values[ $form_key ] );
     386                                        }
     387                                }
     388                        }
     389                        $speaker_draft_title = $all_values['Name'];
     390
     391                } else {
     392                        $speaker_draft_id = $speaker_obj->ID;
     393                        $speaker_draft_title = $speaker_obj->post_title;
     394                }
     395
     396
     397                // process the session part of the form
     398
     399                // put the speaker draft id in all_values to allow saving to meta
     400                $all_values['session_speaker_id'] = empty( $speaker_draft_id ) ? 0 : $speaker_draft_id;
     401                $all_values['session_speaker_name'] = $speaker_draft_title . ',';
     402                $session_to_form_key_map = array(
     403                        '_wcpt_speaker_id' => 'session_speaker_id',
     404                        '_wcb_session_speakers' => 'session_speaker_name',
     405                );
     406
     407                // Create the session draft post
     408                $session_draft_id = wp_insert_post( array(
     409                        'post_type'    => 'wcb_session',
     410                        'post_title'   => $all_values['Topic Title'],
     411                        'post_content' => $all_values["Topic(s) You'd Like to Present On"],
     412                        'post_status'  => 'draft',
     413                        'post_author'  => $this->get_user_id_from_username( 'wordcamp' ),
     414                ) );
     415
     416                // Create the session post meta
     417                if ( $session_draft_id ) {
     418                        foreach ( $session_to_form_key_map as $session_key => $form_key ) {
     419                                if ( ! empty( $all_values[ $form_key ] ) ) {
     420                                        update_post_meta( $session_draft_id, $session_key, $all_values[ $form_key ] );
     421                                }
     422                        }
     423                }
     424
     425        }
     426
    225427} // end WordCamp_Forms_To_Drafts
    226428
    227429$GLOBALS['wordcamp_forms_to_drafts'] = new WordCamp_Forms_To_Drafts();