BP_REST_Account_Settings_Endpoint
Account Settings endpoints.
Description
Source
File: bp-settings/classes/class-bp-rest-account-settings-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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | class BP_REST_Account_Settings_Endpoint extends WP_REST_Controller { /** * Constructor. * * @since 0.1.0 */ public function __construct() { $this -> namespace = bp_rest_namespace() . '/' . bp_rest_version(); $this ->rest_base = 'account-settings' ; } /** * Register the component routes. * * @since 0.1.0 */ public function register_routes() { register_rest_route( $this -> namespace , '/' . $this ->rest_base, array ( array ( 'methods' => WP_REST_Server::READABLE, 'callback' => array ( $this , 'get_items' ), 'permission_callback' => array ( $this , 'get_items_permissions_check' ), 'args' => $this ->get_collection_params(), ), 'schema' => array ( $this , 'get_item_schema' ), ) ); } /** * Retrieve Account Settings. * * @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/account-settings Account Settings * @apiName GetBBAccountSettings * @apiGroup Account Settings * @apiDescription Retrieve account settings tabs. * @apiVersion 1.0.0 * @apiPermission LoggedInUser */ public function get_items( $request ) { $retval = array (); $navs = array (); global $bp ; $tmp_bp = $bp ; // Setup Navigation for non admin users. add_filter( 'bp_displayed_user_id' , array ( $this , 'bp_rest_get_displayed_user' ), 999 ); bp_setup_nav(); remove_filter( 'bp_displayed_user_id' , array ( $this , 'bp_rest_get_displayed_user' ), 999 ); $user_nav = buddypress()->members->nav; if ( ! empty ( $user_nav ) ) { $navs = $user_nav ->get_secondary( array ( 'parent_slug' => 'settings' , 'user_has_access' => true, ) ); } // if it's nouveau then let it order the tabs. if ( function_exists( 'bp_nouveau_set_nav_item_order' ) ) { bp_nouveau_set_nav_item_order( $navs , bp_nouveau_get_appearance_settings( 'user_nav_order' ) ); } if ( ! empty ( $navs ) ) { foreach ( $navs as $nav ) { $retval [] = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $nav , $request ) ); } } $bp = $tmp_bp ; $response = rest_ensure_response( $retval ); /** * Fires after account settings are fetched via the REST API. * * @param array $navs Fetched Navigations. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_account_settings_get_items' , $navs , $response , $request ); return $response ; } /** * Check if a given request has access to account settings. * * @param WP_REST_Request $request Full data about the request. * * @return WP_Error|bool * @since 0.1.0 */ public function get_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 see the account settings.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the account settings `get_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_account_settings_get_items_permissions_check' , $retval , $request ); } /** * Prepares account settings data for return as an object. * * @param object $nav Navigation object. * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response * @since 0.1.0 */ public function prepare_item_for_response( $nav , $request ) { $data = array ( 'name' => $nav ->name, 'slug' => $nav ->slug, 'position' => $nav ->position, 'link' => $nav ->link, ); $context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ; $data = $this ->add_additional_fields_to_object( $data , $request ); $data = $this ->filter_response_by_context( $data , $context ); // @todo add prepare_links $response = rest_ensure_response( $data ); $response ->add_links( $this ->prepare_links( $data [ 'slug' ] ) ); /** * Filter a notification value returned from the API. * * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request Request used to generate the response. * @param object $nav Navigation object. * * @since 0.1.0 */ return apply_filters( 'bp_rest_account_setting_prepare_value' , $response , $request , $nav ); } /** * Prepare links for the request. * * @param string $nav_slug Navigation slug. * @return array Links for the given group. */ protected function prepare_links( $nav_slug ) { $base = '/' . $this -> namespace . '/' . $this ->rest_base; $links = array ( 'options' => array ( 'embeddable' => true, 'href' => rest_url( trailingslashit( $base ) . $nav_slug ), ), ); return $links ; } /** * Get the Account Settings schema, conforming to JSON Schema. * * @return array * @since 0.1.0 */ public function get_item_schema() { $schema = array ( 'title' => 'bp_account_settings' , 'type' => 'object' , 'properties' => array ( 'name' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'A unique name for the setting navigation.' , 'buddyboss' ), 'readonly' => true, 'type' => 'string' , ), 'slug' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'The URL-friendly name for the navigation' , 'buddyboss' ), 'readonly' => true, 'type' => 'string' , ), 'position' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'The position of the current navigation item.' , 'buddyboss' ), 'readonly' => true, 'type' => 'integer' , ), 'link' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'A unique link for the navigation.' , 'buddyboss' ), 'readonly' => true, 'type' => 'string' , ), ), ); /** * Filters the Account Settings schema. * * @param array $schema The endpoint schema. */ return apply_filters( 'bp_rest_account_settings_schema' , $this ->add_additional_fields_schema( $schema ) ); } /** * Get the query params for the Account Settings collections. * * @return array * @since 0.1.0 */ public function get_collection_params() { $params = parent::get_collection_params(); $params [ 'context' ][ 'default' ] = 'view' ; // Remove the search argument. unset( $params [ 'search' ] ); unset( $params [ 'page' ] ); unset( $params [ 'per_page' ] ); /** * Filters the collection query params. * * @param array $params Query params. */ return apply_filters( 'bp_rest_account_settings_collection_params' , $params ); } /** * Set current and display user with current user. * * @param int $user_id The user id. * * @return int */ public function bp_rest_get_displayed_user( $user_id ) { return get_current_user_id(); } } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Methods
- __construct — Constructor.
- bp_rest_get_displayed_user — Set current and display user with current user.
- bp_rest_is_current_component — Unset group component while using this.
- get_collection_params — Get the query params for the Account Settings collections.
- get_item_schema — Get the Account Settings schema, conforming to JSON Schema.
- get_items — Retrieve Account Settings.
- get_items_permissions_check — Check if a given request has access to account settings.
- prepare_item_for_response — Prepares account settings data for return as an object.
- prepare_links — Prepare links for the request.
- register_routes — Register the component routes.
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.