Skip to:
Content

BuddyPress.org

Changeset 9947


Ignore:
Timestamp:
06/15/2015 11:54:42 PM (11 years ago)
Author:
johnjamesjacoby
Message:

Messages: Separate actions from screen functions

This change extracts action-specific functionality from 2 message specific screen functions (per the component coding standards) by introducing two new functions in bp-messages-actions.php and hooking them to bp_actions. These new functions handle the creation of new private messages and notices, and the editing & deletion of existing notices.

A few additional improvements were also made to these functionalities:

  • String improvements to provide additional clarity to site members performing message related actions
  • Code scrutinization and documentation improvements to better understand what these functions are for and when they were introduced
  • Updates to new helper functions introduced since these functions were first introduced and last updated

Fixes #6505 (in trunk, for 2.4)

Location:
trunk/src/bp-messages
Files:
2 edited

Legend:

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

    r9862 r9947  
    1414// Exit if accessed directly
    1515defined( 'ABSPATH' ) || exit;
     16
     17/**
     18 * Handle creating of private messages or sitewide notices
     19 *
     20 * @since BuddyPress (2.4.0)
     21 *
     22 * @return boolean
     23 */
     24function bp_messages_action_create_message() {
     25
     26        // Bail if not posting to the compose message screen
     27        if ( ! bp_is_post_request() || ! bp_is_messages_component() || ! bp_is_current_action( 'compose' ) ) {
     28                return false;
     29        }
     30
     31        // Check the nonce
     32        check_admin_referer( 'messages_send_message' );
     33
     34        // Define local variables
     35        $redirect_to = '';
     36        $feedback    = '';
     37        $success     = false;
     38
     39        // Missing subject or content
     40        if ( empty( $_POST['subject'] ) || empty( $_POST['content'] ) ) {
     41                $success  = false;
     42                $feedback = __( 'Message was not sent. Check the contents and try again.', 'buddypress' );
     43
     44        // Subject and content present
     45        } else {
     46
     47                // Setup the link to the logged-in user's messages
     48                $member_messages = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
     49
     50                // Site-wide notice
     51                if ( isset( $_POST['send-notice'] ) ) {
     52
     53                        // Attempt to save the notice and redirect to notices
     54                        if ( messages_send_notice( $_POST['subject'], $_POST['content'] ) ) {
     55                                $success     = true;
     56                                $feedback    = __( 'Notice successfully created.', 'buddypress' );
     57                                $redirect_to = trailingslashit( $member_messages . 'notices' );
     58
     59                        // Notice could not be sent
     60                        } else {
     61                                $success  = false;
     62                                $feedback = __( 'Notice was not created. Please try again.', 'buddypress' );
     63                        }
     64
     65                // Private conversation
     66                } else {
     67
     68                        // Filter recipients into the format we need - array( 'username/userid', 'username/userid' )
     69                        $autocomplete_recipients = (array) explode( ',', $_POST['send-to-input']     );
     70                        $typed_recipients        = (array) explode( ' ', $_POST['send_to_usernames'] );
     71                        $recipients              = array_merge( $autocomplete_recipients, $typed_recipients );
     72
     73                        /**
     74                         * Filters the array of recipients to receive the composed message.
     75                         *
     76                         * @since BuddyPress (1.2.10)
     77                         *
     78                         * @param array $recipients Array of recipients to receive message.
     79                         */
     80                        $recipients = apply_filters( 'bp_messages_recipients', $recipients );
     81
     82                        // Attempt to send the message
     83                        $thread_id  = messages_new_message( array(
     84                                'recipients' => $recipients,
     85                                'subject'    => $_POST['subject'],
     86                                'content'    => $_POST['content']
     87                        ) );
     88
     89                        // Send the message and redirect to it
     90                        if ( ! empty( $thread_id ) ) {
     91                                $success     = true;
     92                                $feedback    = __( 'Message successfully sent.', 'buddypress' );
     93                                $view        = trailingslashit( $member_messages . 'view' );
     94                                $redirect_to = trailingslashit( $view . $thread_id );
     95
     96                        // Message could not be sent
     97                        } else {
     98                                $success  = false;
     99                                $feedback = __( 'Message was not sent. Please try again.', 'buddypress' );
     100                        }
     101                }
     102        }
     103
     104        // Feedback
     105        if ( ! empty( $feedback ) ) {
     106
     107                // Determine message type
     108                $type = ( true === $success )
     109                        ? 'success'
     110                        : 'error';
     111
     112                // Add feedback message
     113                bp_core_add_message( $feedback, $type );
     114        }
     115
     116        // Maybe redirect
     117        if ( ! empty( $redirect_to ) ) {
     118                bp_core_redirect( $redirect_to );
     119        }
     120}
     121add_action( 'bp_actions', 'bp_messages_action_create_message' );
     122
     123/**
     124 * Handle editing of sitewide notices
     125 *
     126 * @since BuddyPress (2.4.0)
     127 *
     128 * @global int $notice_id
     129 *
     130 * @return boolean
     131 */
     132function bp_messages_action_edit_notice() {
     133        global $notice_id;
     134
     135        // Bail if not viewing a single notice URL
     136        if ( ! bp_is_messages_component() || ! bp_is_current_action( 'notices' ) || ! bp_action_variable( 1 ) ) {
     137                return false;
     138        }
     139
     140        // Get action variables
     141        $action    = bp_action_variable( 0 ); // deactivate|activate|delete
     142        $notice_id = bp_action_variable( 1 ); // 1|2|3|etc...
     143
     144        // Bail if notice ID is not numeric
     145        if ( ! is_numeric( $notice_id ) ) {
     146                return;
     147        }
     148
     149        // Define local variables
     150        $redirect_to = '';
     151        $feedback    = '';
     152        $success     = false;
     153
     154        // Get the notice from database
     155        $notice = new BP_Messages_Notice( $notice_id );
     156
     157        // Take action
     158        switch ( $action ) {
     159
     160                // Deactivate
     161                case 'deactivate' :
     162                        $success  = $notice->deactivate();
     163                        $feedback = true === $success
     164                                ? __( 'Notice deactivated successfully.',              'buddypress' )
     165                                : __( 'There was a problem deactivating that notice.', 'buddypress' );
     166                        break;
     167
     168                // Activate
     169                case 'activate' :
     170                        $success  = $notice->activate();
     171                        $feedback = true === $success
     172                                ? __( 'Notice activated successfully.',              'buddypress' )
     173                                : __( 'There was a problem activating that notice.', 'buddypress' );
     174                        break;
     175
     176                // Delete
     177                case 'delete' :
     178                        $success  = $notice->delete();
     179                        $feedback = true === $success
     180                                ? __( 'Notice deleted successfully.',              'buddypress' )
     181                                : __( 'There was a problem deleting that notice.', 'buddypress' );
     182                        break;
     183        }
     184
     185        // Feedback
     186        if ( ! empty( $feedback ) ) {
     187
     188                // Determine message type
     189                $type = ( true === $success )
     190                        ? 'success'
     191                        : 'error';
     192
     193                // Add feedback message
     194                bp_core_add_message( $feedback, $type );
     195        }
     196
     197        // Redirect
     198        $member_notices = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
     199        $redirect_to    = trailingslashit( $member_notices . 'notices' );
     200
     201        bp_core_redirect( $redirect_to );
     202}
     203add_action( 'bp_actions', 'bp_messages_action_edit_notice' );
    16204
    17205/**
  • trunk/src/bp-messages/bp-messages-screens.php

    r9946 r9947  
    1717/**
    1818 * Load the Messages > Inbox screen.
     19 *
     20 * @since BuddyPress (1.0.0)
    1921 */
    2022function messages_screen_inbox() {
     23
    2124        if ( bp_action_variables() ) {
    2225                bp_do_404();
    … …  
    4346/**
    4447 * Load the Messages > Sent screen.
     48 *
     49 * @since BuddyPress (1.0.0)
    4550 */
    4651function messages_screen_sentbox() {
     52
    4753        if ( bp_action_variables() ) {
    4854                bp_do_404();
    … …  
    6975/**
    7076 * Load the Messages > Compose screen.
     77 *
     78 * @since BuddyPress (1.0.0)
    7179 */
    7280function messages_screen_compose() {
    … …  
    8088        messages_remove_callback_values();
    8189
    82         // Check if the message form has been submitted
    83         if ( isset( $_POST['send'] ) ) {
    84 
    85                 // Check the nonce
    86                 check_admin_referer( 'messages_send_message' );
    87 
    88                 // Check we have what we need
    89                 if ( empty( $_POST['subject'] ) || empty( $_POST['content'] ) ) {
    90                         bp_core_add_message( __( 'There was an error sending that message. Please try again.', 'buddypress' ), 'error' );
    91                 } else {
    92                         // If this is a notice, send it
    93                         if ( isset( $_POST['send-notice'] ) ) {
    94                                 if ( messages_send_notice( $_POST['subject'], $_POST['content'] ) ) {
    95                                         bp_core_add_message( __( 'Notice sent successfully!', 'buddypress' ) );
    96                                         bp_core_redirect( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox/' );
    97                                 } else {
    98                                         bp_core_add_message( __( 'There was an error sending that notice. Please try again.', 'buddypress' ), 'error' );
    99                                 }
    100                         } else {
    101                                 // Filter recipients into the format we need - array( 'username/userid', 'username/userid' )
    102                                 $autocomplete_recipients = explode( ',', $_POST['send-to-input'] );
    103                                 $typed_recipients        = explode( ' ', $_POST['send_to_usernames'] );
    104                                 $recipients              = array_merge( (array) $autocomplete_recipients, (array) $typed_recipients );
    105 
    106                                 /**
    107                                  * Filters the array of recipients to receive the composed message.
    108                                  *
    109                                  * @since BuddyPress (1.2.10)
    110                                  *
    111                                  * @param array $recipients Array of recipients to receive message.
    112                                  */
    113                                 $recipients              = apply_filters( 'bp_messages_recipients', $recipients );
    114                                 $thread_id               = messages_new_message( array(
    115                                         'recipients' => $recipients,
    116                                         'subject'    => $_POST['subject'],
    117                                         'content'    => $_POST['content']
    118                                 ) );
    119 
    120                                 // Send the message
    121                                 if ( ! empty( $thread_id ) ) {
    122                                         bp_core_add_message( __( 'Message sent successfully!', 'buddypress' ) );
    123                                         bp_core_redirect( bp_loggedin_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id . '/' );
    124                                 } else {
    125                                         bp_core_add_message( __( 'There was an error sending that message. Please try again.', 'buddypress' ), 'error' );
    126                                 }
    127                         }
    128                 }
    129         }
    130 
    13190        /**
    13291         * Fires right before the loading of the Messages compose screen template file.
    … …  
    149108 * Load an individual conversation screen.
    150109 *
     110 * @since BuddyPress (1.0.0)
     111 *
    151112 * @return bool|null False on failure.
    152113 */
    … …  
    154115
    155116        // Bail if not viewing a single message
    156         if ( !bp_is_messages_component() || !bp_is_current_action( 'view' ) ) {
     117        if ( ! bp_is_messages_component() || ! bp_is_current_action( 'view' ) ) {
    157118                return false;
    158119        }
    … …  
    160121        $thread_id = (int) bp_action_variable( 0 );
    161122
    162         if ( empty( $thread_id ) || !messages_is_valid_thread( $thread_id ) || ( !messages_check_thread_access( $thread_id ) && !bp_current_user_can( 'bp_moderate' ) ) ) {
     123        if ( empty( $thread_id ) || ! messages_is_valid_thread( $thread_id ) || ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) ) {
    163124                bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_messages_slug() ) );
    164125        }
    … …  
    191152 * Load the Messages > Notices screen.
    192153 *
     154 * @since BuddyPress (1.0.0)
     155 *
    193156 * @return false|null False on failure.
    194157 */
    195158function messages_screen_notices() {
    196         global $notice_id;
    197 
    198         $notice_id = (int) bp_action_variable( 1 );
    199 
    200         if ( !empty( $notice_id ) && is_numeric( $notice_id ) ) {
    201                 $notice = new BP_Messages_Notice( $notice_id );
    202 
    203                 if ( bp_is_action_variable( 'deactivate', 0 ) ) {
    204                         if ( !$notice->deactivate() ) {
    205                                 bp_core_add_message( __('There was a problem deactivating that notice.', 'buddypress'), 'error' );
    206                         } else {
    207                                 bp_core_add_message( __('Notice deactivated.', 'buddypress') );
    208                         }
    209                 } elseif ( bp_is_action_variable( 'activate', 0 ) ) {
    210                         if ( !$notice->activate() ) {
    211                                 bp_core_add_message( __('There was a problem activating that notice.', 'buddypress'), 'error' );
    212                         } else {
    213                                 bp_core_add_message( __('Notice activated.', 'buddypress') );
    214                         }
    215                 } elseif ( bp_is_action_variable( 'delete' ) ) {
    216                         if ( !$notice->delete() ) {
    217                                 bp_core_add_message( __('There was a problem deleting that notice.', 'buddypress'), 'buddypress' );
    218                         } else {
    219                                 bp_core_add_message( __('Notice deleted.', 'buddypress') );
    220                         }
    221                 }
    222                 bp_core_redirect( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices' ) );
    223         }
    224159
    225160        if ( bp_action_variables() ) {
    … …  
    247182/**
    248183 * Render the markup for the Messages section of Settings > Notifications.
     184 *
     185 * @since BuddyPress (1.0.0)
    249186 */
    250187function messages_screen_notification_settings() {
     188
    251189        if ( bp_action_variables() ) {
    252190                bp_do_404();
Note: See TracChangeset for help on using the changeset viewer.