Skip to:
Content

BuddyPress.org

Changeset 13584


Ignore:
Timestamp:
09/20/2023 01:00:54 AM (19 months ago)
Author:
imath
Message:

BP Legacy: improve private messages user feedback management

  • Use a redirect to be sure to clear template notices when a required field is missing at next page load.
  • Preserve submitted texts and contacts in case of missing required fields (using cookies, see messages_add_callback_values()).
  • Improve the template so that it directly informs about all missing required fields at once.

Props niftythree, amitgrhr

Fixes #8578
Closes https://github.com/buddypress/buddypress/pull/167

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/actions/compose.php

    r13503 r13584  
    2929    $feedback    = '';
    3030    $success     = false;
     31    $is_notice   = isset( $_POST['send-notice'] );
     32    $path_chunks = array( bp_get_messages_slug() );
    3133
    32     // Missing subject or content.
    33     if ( empty( $_POST['subject'] ) || empty( $_POST['content'] ) ) {
    34         $success  = false;
     34    // List required vars and error messages.
     35    $required_vars = array(
     36        'subject' => __( 'Please enter a subject line.', 'buddypress' ),
     37        'content' => __( 'Please enter some content.', 'buddypress' ),
     38    );
    3539
    36         if ( empty( $_POST['subject'] ) ) {
    37             $feedback = __( 'Your message was not sent. Please enter a subject line.', 'buddypress' );
    38         } else {
    39             $feedback = __( 'Your message was not sent. Please enter some content.', 'buddypress' );
     40    // The username is only needed for private messages.
     41    if ( ! $is_notice ) {
     42        $required_vars = array_merge(
     43            array(
     44                'send_to_usernames' => __( 'Please enter a username or Friend\'s name.', 'buddypress' ),
     45            ),
     46            $required_vars
     47        );
     48    }
     49
     50    // Calculate whether some required vars are missing.
     51    $needed_keys    = array_intersect_key( $_POST, $required_vars );
     52    $needs_feedback = count( array_filter( $needed_keys ) ) !== count( $required_vars );
     53
     54    // Prevent notice errors.
     55    $required_args = array_map(
     56        'wp_unslash',
     57        bp_parse_args(
     58            $needed_keys,
     59            array(
     60                'send_to_usernames' => '',
     61                'subject'           => '',
     62                'content'           => ''
     63            )
     64        )
     65    );
     66
     67    // Use cookies to preserve existing values.
     68    messages_add_callback_values(
     69        implode( ' ', wp_parse_list( $required_args['send_to_usernames'] ) ),
     70        esc_html( $required_args['subject'] ),
     71        esc_textarea( $required_args['content'] )
     72    );
     73
     74    // Handle required vars.
     75    if ( $needs_feedback ) {
     76        $success       = false;
     77        $path_chunks[] = 'compose';
     78        $feedbacks     = array( __( 'Your message was not sent.', 'buddypress' ) );
     79
     80        foreach ( $required_vars as $required_key => $required_var_feedback ) {
     81            if ( ! $required_args[ $required_key ] ) {
     82                $feedbacks[] = $required_var_feedback;
     83            }
    4084        }
    4185
    42     // Subject and content present.
     86        // Set the feedback message.
     87        $feedback    = implode( "\n", $feedbacks );
     88        $redirect_to = bp_loggedin_user_url( bp_members_get_path_chunks( $path_chunks ) );
    4389    } else {
    4490
    45         // Setup the link to the logged-in user's messages.
    46         $path_chunks = array( bp_get_messages_slug() );
    47 
    4891        // Site-wide notice.
    49         if ( isset( $_POST['send-notice'] ) ) {
     92        if ( $is_notice ) {
    5093
    5194            // Attempt to save the notice and redirect to notices.
    52             if ( messages_send_notice( $_POST['subject'], $_POST['content'] ) ) {
     95            if ( messages_send_notice( $required_args['subject'], $required_args['content'] ) ) {
    5396                $success       = true;
    5497                $feedback      = __( 'Notice successfully created.', 'buddypress' );
    5598                $path_chunks[] = 'notices';
    56                 $redirect_to   = bp_loggedin_user_url( bp_members_get_path_chunks( $path_chunks ) );
    5799
    58             // Notice could not be sent.
     100                // Notice could not be sent.
    59101            } else {
    60                 $success  = false;
    61                 $feedback = __( 'Notice was not created. Please try again.', 'buddypress' );
     102                $success       = false;
     103                $path_chunks[] = 'compose';
     104                $feedback      = __( 'Notice was not created. Please try again.', 'buddypress' );
    62105            }
    63106
    64         // Private conversation.
     107            $redirect_to = bp_loggedin_user_url( bp_members_get_path_chunks( $path_chunks ) );
     108
     109            // Private conversation.
    65110        } else {
    66111
    67112            // Filter recipients into the format we need - array( 'username/userid', 'username/userid' ).
    68             $autocomplete_recipients = (array) explode( ',', $_POST['send-to-input'] );
    69             $typed_recipients        = (array) explode( ' ', $_POST['send_to_usernames'] );
    70             $recipients              = array_merge( $autocomplete_recipients, $typed_recipients );
     113            $autocomplete_recipients = array();
     114            if ( isset( $_POST['send-to-input'] ) && $_POST['send-to-input'] ) {
     115                $autocomplete_recipients = (array) explode( ',', $_POST['send-to-input'] );
     116            }
     117
     118            $typed_recipients = (array) explode( ' ', $required_args['send_to_usernames'] );
     119            $recipients       = array_merge( $autocomplete_recipients, $typed_recipients );
    71120
    72121            /**
     
    80129
    81130            // Attempt to send the message.
    82             $send = messages_new_message( array(
    83                 'recipients' => $recipients,
    84                 'subject'    => $_POST['subject'],
    85                 'content'    => $_POST['content'],
    86                 'error_type' => 'wp_error',
    87             ) );
     131            $send = messages_new_message(
     132                array(
     133                    'recipients' => $recipients,
     134                    'subject'    => $required_args['subject'],
     135                    'content'    => $required_args['content'],
     136                    'error_type' => 'wp_error',
     137                )
     138            );
    88139
    89140            // Send the message and redirect to it.
     
    93144                $path_chunks[] = 'view';
    94145                $path_chunks[] = array( $send );
    95                 $redirect_to   = bp_loggedin_user_url( bp_members_get_path_chunks( $path_chunks ) );
    96146
    97             // Message could not be sent.
     147                // Message could not be sent.
    98148            } else {
    99                 $success  = false;
    100                 $feedback = $send->get_error_message();
     149                $success       = false;
     150                $path_chunks[] = 'compose';
     151                $feedback      = $send->get_error_message();
    101152            }
     153
     154            $redirect_to = bp_loggedin_user_url( bp_members_get_path_chunks( $path_chunks ) );
    102155        }
    103156    }
  • trunk/src/bp-messages/bp-messages-template.php

    r13503 r13584  
    10041004
    10051005        // Sanitized in bp-messages-filters.php.
    1006         $subject = ! empty( $_POST['subject'] )
    1007             ? $_POST['subject']
    1008             : '';
     1006        $subject = '';
     1007        if ( isset( $_POST['subject'] ) && $_POST['subject'] ) {
     1008            $subject = $_POST['subject'];
     1009        } elseif ( isset( $_COOKIE['bp_messages_subject'] ) && $_COOKIE['bp_messages_subject'] ) {
     1010            $subject = $_COOKIE['bp_messages_subject'];
     1011        }
    10091012
    10101013        /**
     
    10351038
    10361039        // Sanitized in bp-messages-filters.php.
    1037         $content = ! empty( $_POST['content'] )
    1038             ? $_POST['content']
    1039             : '';
     1040        $content = '';
     1041        if ( isset( $_POST['content'] ) && $_POST['content'] ) {
     1042            $content = $_POST['content'];
     1043        } elseif ( isset( $_COOKIE['bp_messages_content'] ) && $_COOKIE['bp_messages_content'] ) {
     1044            $content = $_COOKIE['bp_messages_content'];
     1045        }
    10401046
    10411047        /**
     
    16801686
    16811687        // Sanitized in bp-messages-filters.php.
    1682         $recipients = isset( $_GET['r'] )
    1683             ? $_GET['r']
    1684             : '';
     1688        $recipients = '';
     1689        if ( isset( $_GET['r'] ) && $_GET['r'] ) {
     1690            $recipients = $_GET['r'];
     1691        } elseif ( isset( $_COOKIE['bp_messages_send_to'] ) && $_COOKIE['bp_messages_send_to'] ) {
     1692            $recipients = $_COOKIE['bp_messages_send_to'];
     1693        }
    16851694
    16861695        /**
  • trunk/src/bp-templates/bp-legacy/buddypress/members/single/messages/compose.php

    r13295 r13584  
    1111<h2 class="bp-screen-reader-text"><?php
    1212    /* translators: accessibility text */
    13     _e( 'Compose Message', 'buddypress' );
     13    esc_html_e( 'Compose Message', 'buddypress' );
    1414?></h2>
    1515
     
    2525    do_action( 'bp_before_messages_compose_content' ); ?>
    2626
    27     <label for="send-to-input"><?php _e("Send To (Username or Friend's Name)", 'buddypress' ); ?></label>
     27    <label for="send-to-input"><?php esc_html_e( 'Send To (Username or Friend\'s Name)', 'buddypress' ); ?></label>
    2828    <ul class="first acfb-holder">
    2929        <li>
     
    3434
    3535    <?php if ( bp_current_user_can( 'bp_moderate' ) ) : ?>
    36         <p><label for="send-notice"><input type="checkbox" id="send-notice" name="send-notice" value="1" /> <?php _e( "This is a notice to all users.", 'buddypress' ); ?></label></p>
     36        <p><label for="send-notice"><input type="checkbox" id="send-notice" name="send-notice" value="1" /> <?php esc_html_e( "This is a notice to all users.", 'buddypress' ); ?></label></p>
    3737    <?php endif; ?>
    3838
    39     <label for="subject"><?php _e( 'Subject', 'buddypress' ); ?></label>
     39    <label for="subject"><?php esc_html_e( 'Subject', 'buddypress' ); ?></label>
    4040    <input type="text" name="subject" id="subject" value="<?php bp_messages_subject_value(); ?>" />
    4141
    42     <label for="message_content"><?php _e( 'Message', 'buddypress' ); ?></label>
     42    <label for="message_content"><?php esc_html_e( 'Message', 'buddypress' ); ?></label>
    4343    <textarea name="content" id="message_content" rows="15" cols="40"><?php bp_messages_content_value(); ?></textarea>
    4444
Note: See TracChangeset for help on using the changeset viewer.