BP_REST_XProfile_Search_Form_Fields_Endpoint

XProfile Search Form Fields endpoints.

Description

Use /xprofile/search

Source

File: bp-xprofile/classes/class-bp-rest-xprofile-search-form-fields-endpoint.php

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
class BP_REST_XProfile_Search_Form_Fields_Endpoint extends WP_REST_Controller {
 
    /**
     * Constructor.
     *
     * @since 0.1.0
     */
    public function __construct() {
        $this->namespace = bp_rest_namespace() . '/' . bp_rest_version();
        $this->rest_base = buddypress()->profile->id . '/search';
    }
 
    /**
     * 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 XProfile search form fields.
     *
     * @param WP_REST_Request $request Full data about the request.
     *
     * @return WP_REST_Response
     * @since 0.1.0
     *
     * @api            {GET} /wp-json/buddyboss/v1/xprofile/search Get Search Form
     * @apiName        GetBBxProfileSearchForm
     * @apiGroup       Profile Fields
     * @apiDescription Retrieve Advanced Search Form fields for Members Directory.
     * @apiVersion     1.0.0
     * @apiParam {Number} [form_id] ID of the profile search form.
     */
    public function get_items( $request ) {
 
        if ( empty( $request['form_id'] ) ) {
            $args['form_id'] = bp_profile_search_main_form();
        }
 
        /**
         * Filter the query arguments for the request.
         *
         * @param array $args Key value array of query var to query value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        $args = apply_filters( 'bp_rest_xprofile_search_form_fields_get_items_query_args', $args, $request );
 
        // Actually, query it.
        $f = bp_profile_search_escaped_form_data( $args['form_id'] );
 
        $response = rest_ensure_response( $f );
 
        /**
         * Fires after a list of field are fetched via the REST API.
         *
         * @param array $field_groups Fetched field groups.
         * @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_xprofile_search_form_fields_get_items', $response, $request );
 
        return $response;
    }
 
    /**
     * Check if a given request has access to XProfile search form fields.
     *
     * @param WP_REST_Request $request Full data about the request.
     *
     * @return bool
     * @since 0.1.0
     */
    public function get_items_permissions_check( $request ) {
 
        /**
         * Filter the XProfile fields `get_items` permissions check.
         *
         * @param bool $retval Returned value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_profile_search_main_form', ( function_exists( 'bp_disable_advanced_profile_search' ) ? ! bp_disable_advanced_profile_search() : false ), $request );
    }
 
    /**
     * Get the XProfile field schema, conforming to JSON Schema.
     *
     * @return array
     * @since 0.1.0
     */
    public function get_item_schema() {
        $schema = array(
            '$schema'    => 'http://json-schema.org/draft-04/schema#',
            'title'      => 'bp_xprofile_search_form_field',
            'type'       => 'object',
            'properties' => array(),
        );
 
        /**
         * Filters the xprofile search form field schema.
         *
         * @param array $schema The endpoint schema.
         */
        return apply_filters( 'bp_rest_xprofile_search_form_field_schema', $this->add_additional_fields_schema( $schema ) );
    }
 
    /**
     * Get the query params for the XProfile search form fields.
     *
     * @return array
     * @since 0.1.0
     */
    public function get_collection_params() {
        $params = array(
            'form_id' => array(
                'description'       => __( 'ID of the profile search form.', 'buddyboss' ),
                'type'              => 'string',
                'validate_callback' => 'rest_validate_request_arg',
            ),
        );
 
        /**
         * Filters the collection query params.
         *
         * @param array $params Query params.
         */
        return apply_filters( 'bp_rest_xprofile_search_form_fields_collection_params', $params );
    }
}

Changelog

Changelog
Version Description
0.1.0 Introduced.

Methods

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.