BP_REST_Messages_Actions_Endpoint
Messages Actions endpoints.
Description
Source
File: bp-messages/classes/class-bp-rest-messages-actions-endpoint.php
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | class BP_REST_Messages_Actions_Endpoint extends WP_REST_Controller { /** * Reuse some parts of the BP_REST_Messages_Endpoint class. * * @since 0.1.0 * * @var BP_REST_Messages_Endpoint */ protected $message_endppoint ; /** * Constructor. * * @since 0.1.0 */ public function __construct() { $this -> namespace = bp_rest_namespace() . '/' . bp_rest_version(); $this ->rest_base = buddypress()->messages->id; $this ->message_endppoint = new BP_REST_Messages_Endpoint(); } /** * Register the component routes. * * @since 0.1.0 */ public function register_routes() { register_rest_route( $this -> namespace , '/' . $this ->rest_base . '/action/(?P<id>[\d]+)' , array ( 'args' => array ( 'id' => array ( 'description' => __( 'ID of the Messages Thread.' , 'buddyboss' ), 'type' => 'integer' , 'required' => true, ), 'action' => array ( 'description' => __( 'Action name to perform on the message thread.' , 'buddyboss' ), 'type' => 'string' , 'required' => true, 'enum' => array ( 'delete_messages' , 'hide_thread' , 'unread' , ), 'sanitize_callback' => 'sanitize_key' , 'validate_callback' => 'rest_validate_request_arg' , ), 'value' => array ( 'description' => __( 'Value for the action on message thread.' , 'buddyboss' ), 'type' => 'boolean' , 'sanitize_callback' => 'rest_sanitize_boolean' , 'validate_callback' => 'rest_validate_request_arg' , ), ), array ( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array ( $this , 'action_items' ), 'permission_callback' => array ( $this , 'action_items_permissions_check' ), 'args' => $this ->get_collection_params(), ), 'schema' => array ( $this ->message_endppoint, 'get_item_schema' ), ) ); } /** * Perform Action on the Message Thread. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {GET} /wp-json/buddyboss/v1/messages/action/:id Thread Action * @apiName GetBBThreadsAction * @apiGroup Messages * @apiDescription Perform Action on the Message Thread. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} id ID of the Messages Thread. * @apiParam {String=delete_messages,hide_thread,unread} action Action name to perform on the message thread. * @apiParam {Boolean} value Value for the action on message thread. */ public function action_items( $request ) { $action = $request ->get_param( 'action' ); $value = $request ->get_param( 'value' ); $thread_id = $request ->get_param( 'id' ); switch ( $action ) { case 'delete_messages' : $retval = $this ->rest_delete_messages( $thread_id , $value ); break ; case 'hide_thread' : $retval = $this ->rest_hide_thread( $thread_id , $value ); break ; case 'unread' : $retval = $this ->rest_unread_thread( $thread_id , $value ); break ; } if ( is_wp_error( $retval ) ) { return $retval ; } // Clear recipients cache after update hidden property. wp_cache_delete( 'thread_recipients_' . $thread_id , 'bp_messages' ); $thread = new BP_Messages_Thread( $thread_id ); $retval = $this ->prepare_response_for_collection( $this ->message_endppoint->prepare_item_for_response( $thread , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a thread is fetched via the REST API. * * @param BP_Messages_Thread $thread Thread object. * @param WP_REST_Response $retval The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_messages_action_items' , $thread , $response , $request ); return $response ; } /** * Check if a given request has access to thread items. * * @param WP_REST_Request $request Full data about the request. * * @return WP_Error|bool * @since 0.1.0 */ public function action_items_permissions_check( $request ) { $retval = true; if ( ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to perform action on messages.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } $thread = $this ->message_endppoint->get_thread_object( $request [ 'id' ] ); if ( true === $retval && empty ( $thread ->thread_id ) ) { $retval = new WP_Error( 'bp_rest_invalid_id' , __( 'Sorry, this thread does not exist.' , 'buddyboss' ), array ( 'status' => 404, ) ); } if ( true === $retval && bp_current_user_can( 'bp_moderate' ) ) { $retval = true; } else { $id = messages_check_thread_access( $thread ->thread_id ); if ( true === $retval && is_null ( $id ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not allowed to see this thread.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval ) { $retval = true; } } /** * Filter the messages `action_items` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_messages_action_items_permissions_check' , $retval , $request ); } /** * Delete thread messages by logged in users. * * @param integer $thread_id ID of the Messages Thread. * @param boolen $value Action value. * * @return bool|void */ protected function rest_delete_messages( $thread_id , $value ) { if ( empty ( $value ) ) { return ; } return messages_delete_thread( $thread_id ); } /** * Hide unhide message thread based on the logged in user. * - from bp_nouveau_ajax_hide_thread(); * * @param integer $thread_id ID of the Messages Thread. * @param boolen $value Action value. * * @return bool|void */ protected function rest_hide_thread( $thread_id , $value ) { global $bp , $wpdb ; if ( empty ( $value ) ) { // phpcs:ignore $wpdb ->query( $wpdb ->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_hidden = %d WHERE thread_id = %d AND user_id = %d" , 0, (int) $thread_id , bp_loggedin_user_id() ) ); return true; } elseif ( ! empty ( $value ) ) { // phpcs:ignore $wpdb ->query( $wpdb ->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_hidden = %d WHERE thread_id = %d AND user_id = %d" , 1, (int) $thread_id , bp_loggedin_user_id() ) ); return true; } return false; } /** * Read/Unread message thread based on the logged in user. * - from bp_nouveau_ajax_readunread_thread_messages(); * * @param integer $thread_id ID of the Messages Thread. * @param boolen $value Action value. * * @return bool|void */ protected function rest_unread_thread( $thread_id , $value ) { if ( empty ( $value ) ) { messages_mark_thread_read( $thread_id ); return true; } elseif ( ! empty ( $value ) ) { messages_mark_thread_unread( $thread_id ); } return false; } } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Methods
- __construct — Constructor.
- action_items — Perform Action on the Message Thread.
- action_items_permissions_check — Check if a given request has access to thread items.
- register_routes — Register the component routes.
- rest_delete_messages — Delete thread messages by logged in users.
- rest_hide_thread — Hide unhide message thread based on the logged in user.
- rest_unread_thread — Read/Unread message thread based on the logged in user.
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.