WP_REST_Response example



            foreach ( $widget_ids as $widget_id ) {
                $response = $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' )$request );

                if ( ! is_wp_error( $response ) ) {
                    $prepared[] = $this->prepare_response_for_collection( $response );
                }
            }
        }

        return new WP_REST_Response( $prepared );
    }

    /** * Checks if a given request has access to get a widget. * * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */
    public function get_item_permissions_check( $request ) {
        
$retval = wp_delete_term( $term->term_id, $term->taxonomy );

        if ( ! $retval ) {
            return new WP_Error(
                'rest_cannot_delete',
                __( 'The term cannot be deleted.' ),
                array( 'status' => 500 )
            );
        }

        $response = new WP_REST_Response();
        $response->set_data(
            array(
                'deleted'  => true,
                'previous' => $previous->get_data(),
            )
        );

        /** * Fires after a single term is deleted via the REST API. * * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. * * Possible hook names include: * * - `rest_delete_category` * - `rest_delete_post_tag` * * @since 4.7.0 * * @param WP_Term $term The deleted term. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. */


        $passwords = WP_Application_Passwords::get_user_application_passwords( $user->ID );
        $response  = array();

        foreach ( $passwords as $password ) {
            $response[] = $this->prepare_response_for_collection(
                $this->prepare_item_for_response( $password$request )
            );
        }

        return new WP_REST_Response( $response );
    }

    /** * Checks if a given request has access to get a specific application password. * * @since 5.6.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */
    public function get_item_permissions_check( $request ) {
        


        $id    = $template->wp_id;
        $force = (bool) $request['force'];

        $request->set_param( 'context', 'edit' );

        // If we're forcing, then delete permanently.         if ( $force ) {
            $previous = $this->prepare_item_for_response( $template$request );
            $result   = wp_delete_post( $id, true );
            $response = new WP_REST_Response();
            $response->set_data(
                array(
                    'deleted'  => true,
                    'previous' => $previous->get_data(),
                )
            );
        } else {
            // Otherwise, only trash if we haven't already.             if ( 'trash' === $template->status ) {
                return new WP_Error(
                    'rest_template_already_trashed',
                    


            $data['_file'] = $file;

            if ( ! $this->does_plugin_match_request( $request$data ) ) {
                continue;
            }

            $plugins[] = $this->prepare_response_for_collection( $this->prepare_item_for_response( $data$request ) );
        }

        return new WP_REST_Response( $plugins );
    }

    /** * Checks if a given request has access to get a specific plugin. * * @since 5.5.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */
    public function get_item_permissions_check( $request ) {
        


    if ( $response instanceof WP_REST_Response ) {
        return $response;
    }

    /* * While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide * all the required methods used in WP_REST_Server::dispatch(). */
    if ( $response instanceof WP_HTTP_Response ) {
        return new WP_REST_Response(
            $response->get_data(),
            $response->get_status(),
            $response->get_headers()
        );
    }

    return new WP_REST_Response( $response );
}

/** * Handles _deprecated_function() errors. * * @since 4.4.0 * * @param string $function_name The function that was called. * @param string $replacement The function that should have been called. * @param string $version Version. */
return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menu items do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
        }

        $previous = $this->prepare_item_for_response( get_post( $request['id'] )$request );

        $result = wp_delete_post( $request['id'], true );

        if ( ! $result ) {
            return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
        }

        $response = new WP_REST_Response();
        $response->set_data(
            array(
                'deleted'  => true,
                'previous' => $previous->get_data(),
            )
        );

        /** * Fires immediately after a single menu item is deleted via the REST API. * * @since 5.9.0 * * @param object $nav_menu_item Inserted or updated menu item object. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request Request object. */

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $item_id = $item;
        $handler = $this->get_search_handler( $request );
        if ( is_wp_error( $handler ) ) {
            return new WP_REST_Response();
        }

        $fields = $this->get_fields_for_response( $request );

        $data = $handler->prepare_item( $item_id$fields );
        $data = $this->add_additional_fields_to_object( $data$request );

        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $data    = $this->filter_response_by_context( $data$context );

        $response = rest_ensure_response( $data );

        
__( 'Sorry, you are not allowed to delete this post.' ),
                array( 'status' => rest_authorization_required_code() )
            );
        }

        $request->set_param( 'context', 'edit' );

        // If we're forcing, then delete permanently.         if ( $force ) {
            $previous = $this->prepare_item_for_response( $post$request );
            $result   = wp_delete_post( $id, true );
            $response = new WP_REST_Response();
            $response->set_data(
                array(
                    'deleted'  => true,
                    'previous' => $previous->get_data(),
                )
            );
        } else {
            // If we don't support trashing for this type, error out.             if ( ! $supports_trash ) {
                return new WP_Error(
                    'rest_trash_not_supported',
                    
'name'            => get_option( 'blogname' ),
            'description'     => get_option( 'blogdescription' ),
            'url'             => get_option( 'siteurl' ),
            'home'            => home_url(),
            'gmt_offset'      => get_option( 'gmt_offset' ),
            'timezone_string' => get_option( 'timezone_string' ),
            'namespaces'      => array_keys( $this->namespaces ),
            'authentication'  => array(),
            'routes'          => $this->get_data_for_routes( $this->get_routes()$request['context'] ),
        );

        $response = new WP_REST_Response( $available );
        $response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' );
        $this->add_active_theme_link_to_index( $response );
        $this->add_site_logo_to_index( $response );
        $this->add_site_icon_to_index( $response );

        /** * Filters the REST API root index data. * * This contains the data describing the API. This includes information * about supported authentication schemes, supported namespaces, routes * available on the API, and a small amount of data about the site. * * @since 4.4.0 * @since 6.0.0 Added `$request` parameter. * * @param WP_REST_Response $response Response data. * @param WP_REST_Request $request Request data. */

        $supports_trash = apply_filters( 'rest_comment_trashable', ( EMPTY_TRASH_DAYS > 0 )$comment );

        $request->set_param( 'context', 'edit' );

        if ( $force ) {
            $previous = $this->prepare_item_for_response( $comment$request );
            $result   = wp_delete_comment( $comment->comment_ID, true );
            $response = new WP_REST_Response();
            $response->set_data(
                array(
                    'deleted'  => true,
                    'previous' => $previous->get_data(),
                )
            );
        } else {
            // If this type doesn't support trashing, error out.             if ( ! $supports_trash ) {
                return new WP_Error(
                    'rest_trash_not_supported',
                    
$response = array();

        if ( $raw_patterns ) {
            foreach ( $raw_patterns as $pattern ) {
                $response[] = $this->prepare_response_for_collection(
                    $this->prepare_item_for_response( $pattern$request )
                );
            }
        }

        return new WP_REST_Response( $response );
    }

    /** * Prepare a raw block pattern before it gets output in a REST API response. * * @since 5.8.0 * @since 5.9.0 Renamed `$raw_pattern` to `$item` to match parent class for PHP 8 named parameter support. * * @param object $item Raw pattern from api.wordpress.org, before any changes. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */

        do_action( 'rest_delete_revision', $result$request );

        if ( ! $result ) {
            return new WP_Error(
                'rest_cannot_delete',
                __( 'The post cannot be deleted.' ),
                array( 'status' => 500 )
            );
        }

        $response = new WP_REST_Response();
        $response->set_data(
            array(
                'deleted'  => true,
                'previous' => $previous->get_data(),
            )
        );
        return $response;
    }

    /** * Determines the allowed query_vars for a get_items() response and prepares * them for WP_Query. * * @since 5.0.0 * * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array. * @param WP_REST_Request $request Optional. Full details about the request. * @return array Items query arguments. */
'icon'                => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ),
            'last_updated'        => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),
            'humanized_updated'   => sprintf(
                /* translators: %s: Human-readable time difference. */
                __( '%s ago' ),
                human_time_diff( strtotime( $plugin['last_updated'] ) )
            ),
        );

        $this->add_additional_fields_to_object( $block$request );

        $response = new WP_REST_Response( $block );

        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $response->add_links( $this->prepare_links( $plugin ) );
        }

        return $response;
    }

    /** * Generates a list of links to include in the response for the plugin. * * @since 5.5.0 * * @param array $plugin The plugin data from WordPress.org. * @return array */
$result = wp_delete_user( $id$reassign );

        if ( ! $result ) {
            return new WP_Error(
                'rest_cannot_delete',
                __( 'The user cannot be deleted.' ),
                array( 'status' => 500 )
            );
        }

        $response = new WP_REST_Response();
        $response->set_data(
            array(
                'deleted'  => true,
                'previous' => $previous->get_data(),
            )
        );

        /** * Fires immediately after a user is deleted via the REST API. * * @since 4.7.0 * * @param WP_User $user The user data. * @param WP_REST_Response $response The response returned from the API. * @param WP_REST_Request $request The request sent to the API. */
Home | Imprint | This part of the site doesn't use cookies.