rest_get_server example


function _wp_rest_api_autosave_meta( $autosave ) {
    // Ensure it's a REST API request.     if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
        return;
    }

    $body = rest_get_server()->get_raw_data();
    $body = json_decode( $body, true );

    if ( ! isset( $body['meta']['footnotes'] ) ) {
        return;
    }

    // `wp_creating_autosave` passes the array,     // `_wp_put_post_revision` passes the ID.     $id = is_int( $autosave ) ? $autosave : $autosave['ID'];

    if ( ! $id ) {
        
'<code>$args</code>',
                        '<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>'
                    ),
                    '6.1.0'
                );
                break; // Leave the foreach loop once a non-array argument was found.             }
        }
    }

    $full_route = '/' . $clean_namespace . '/' . trim( $route, '/' );
    rest_get_server()->register_route( $clean_namespace$full_route$args$override );
    return true;
}

/** * Registers a new field on an existing WordPress object type. * * @since 4.7.0 * * @global array $wp_rest_additional_fields Holds registered fields, organized * by object type. * * @param string|array $object_type Object(s) the field is being registered to, * "post"|"term"|"comment" etc. * @param string $attribute The attribute name. * @param array $args { * Optional. An array of arguments used to handle the registered field. * * @type callable|null $get_callback Optional. The callback function used to retrieve the field value. Default is * 'null', the field will not be returned in the response. The function will * be passed the prepared object data. * @type callable|null $update_callback Optional. The callback function used to set and update the field value. Default * is 'null', the value cannot be set or updated. The function will be passed * the model object, like WP_Post. * @type array|null $schema Optional. The schema for this field. * Default is 'null', no schema entry will be returned. * } */

    public function prepare_response_for_collection( $response ) {
        if ( ! ( $response instanceof WP_REST_Response ) ) {
            return $response;
        }

        $data   = (array) $response->get_data();
        $server = rest_get_server();
        $links  = $server::get_compact_response_links( $response );

        if ( ! empty( $links ) ) {
            $data['_links'] = $links;
        }

        return $data;
    }

    /** * Filters a response based on the context defined in the schema. * * @since 4.7.0 * * @param array $response_data Response data to filter. * @param string $context Context defined in the schema. * @return array Filtered response. */
Home | Imprint | This part of the site doesn't use cookies.