| 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 | /** |
| 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 | |