rest_is_field_included example


    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $location  = $item;
        $locations = get_nav_menu_locations();
        $menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;

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

        if ( rest_is_field_included( 'name', $fields ) ) {
            $data['name'] = $location->name;
        }

        if ( rest_is_field_included( 'description', $fields ) ) {
            $data['description'] = $location->description;
        }

        if ( rest_is_field_included( 'menu', $fields ) ) {
            $data['menu'] = (int) $menu;
        }

        

    public function prepare_item_for_response( $item$request ) {
        $data = array();

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

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = (int) $item->ID;
        }

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

        $response = rest_ensure_response( $data );

        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $links = $this->prepare_links( $item );
            

    public function prepare_item_for_response( $item$request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable         // Restores the more descriptive, specific name for use within this method.         $template = $item;

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

        // Base fields for every template.         $data = array();

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = $template->id;
        }

        if ( rest_is_field_included( 'theme', $fields ) ) {
            $data['theme'] = $template->theme;
        }

        if ( rest_is_field_included( 'content', $fields ) ) {
            $data['content'] = array();
        }
        if ( rest_is_field_included( 'content.raw', $fields ) ) {
            

    public function prepare_item_for_response( $term$request ) {
        $nav_menu = wp_get_nav_menu_object( $term );
        $response = parent::prepare_item_for_response( $nav_menu$request );

        $fields = $this->get_fields_for_response( $request );
        $data   = $response->get_data();

        if ( rest_is_field_included( 'locations', $fields ) ) {
            $data['locations'] = $this->get_menu_locations( $nav_menu->term_id );
        }

        if ( rest_is_field_included( 'auto_add', $fields ) ) {
            $data['auto_add'] = $this->get_menu_auto_add( $nav_menu->term_id );
        }

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

        
        $raw_config                       = json_decode( $post->post_content, true );
        $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON'];
        $config                           = array();
        if ( $is_global_styles_user_theme_json ) {
            $config = ( new WP_Theme_JSON( $raw_config, 'custom' ) )->get_raw_data();
        }

        // Base fields for every post.         $data   = array();
        $fields = $this->get_fields_for_response( $request );

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = $post->ID;
        }

        if ( rest_is_field_included( 'title', $fields ) ) {
            $data['title'] = array();
        }
        if ( rest_is_field_included( 'title.raw', $fields ) ) {
            $data['title']['raw'] = $post->post_title;
        }
        if ( rest_is_field_included( 'title.rendered', $fields ) ) {
            add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );

            

    public function prepare_item_for_response( $item$request ) {
        // Base fields for every post.         $fields    = $this->get_fields_for_response( $request );
        $menu_item = $this->get_nav_menu_item( $item->ID );
        $data      = array();

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = $menu_item->ID;
        }

        if ( rest_is_field_included( 'title', $fields ) ) {
            $data['title'] = array();
        }

        if ( rest_is_field_included( 'title.raw', $fields ) ) {
            $data['title']['raw'] = $menu_item->title;
        }

        
// Restores the more descriptive, specific name for use within this method.         $post            = $item;
        $GLOBALS['post'] = $post;

        setup_postdata( $post );

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

        // Base fields for every post.         $data = array();

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = $post->ID;
        }

        if ( rest_is_field_included( 'date', $fields ) ) {
            $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
        }

        if ( rest_is_field_included( 'date_gmt', $fields ) ) {
            /* * For drafts, `post_date_gmt` may not be set, indicating that the date * of the draft should be updated each time it is saved (see #38883). * In this case, shim the value based on the `post_date` field * with the site's timezone offset applied. */
if ( in_array( 'meta', $fields, true ) ) {
            $data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
        }

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

        // Wrap the data in a response object.         $response = rest_ensure_response( $data );

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

        /** * Filters a comment returned from the REST API. * * Allows modification of the comment right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Comment $comment The original comment object. * @param WP_REST_Request $request Request used to generate the response. */
$data['meta'] = $this->meta->get_value( $user->ID, $request );
        }

        $context = ! empty( $request['context'] ) ? $request['context'] : 'embed';

        $data = $this->add_additional_fields_to_object( $data$request );
        $data = $this->filter_response_by_context( $data$context );

        // Wrap the data in a response object.         $response = rest_ensure_response( $data );

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

        /** * Filters user data returned from the REST API. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_User $user User object used to create response. * @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.         $block_type = $item;
        $fields     = $this->get_fields_for_response( $request );
        $data       = array();

        if ( rest_is_field_included( 'attributes', $fields ) ) {
            $data['attributes'] = $block_type->get_attributes();
        }

        if ( rest_is_field_included( 'is_dynamic', $fields ) ) {
            $data['is_dynamic'] = $block_type->is_dynamic();
        }

        $schema = $this->get_item_schema();
        // Fields deprecated in WordPress 6.1, but left in the schema for backwards compatibility.         $deprecated_fields = array(
            'editor_script',
            
'viewportWidth' => 'viewport_width',
            'inserter'      => 'inserter',
            'categories'    => 'categories',
            'keywords'      => 'keywords',
            'blockTypes'    => 'block_types',
            'postTypes'     => 'post_types',
            'templateTypes' => 'template_types',
            'source'        => 'source',
        );
        $data   = array();
        foreach ( $keys as $item_key => $rest_key ) {
            if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key$fields ) ) {
                $data[ $rest_key ] = $item[ $item_key ];
            }
        }

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

    /** * Retrieves the block pattern schema, conforming to JSON Schema. * * @since 6.0.0 * @since 6.3.0 Added `source` property. * * @return array Item schema data. */
$prepared = array(
            'id'            => $widget_id,
            'id_base'       => $parsed_id['id_base'],
            'sidebar'       => $sidebar_id,
            'rendered'      => '',
            'rendered_form' => null,
            'instance'      => null,
        );

        if (
            rest_is_field_included( 'rendered', $fields ) &&
            'wp_inactive_widgets' !== $sidebar_id
        ) {
            $prepared['rendered'] = trim( wp_render_widget( $widget_id$sidebar_id ) );
        }

        if ( rest_is_field_included( 'rendered_form', $fields ) ) {
            $rendered_form = wp_render_widget_control( $widget_id );
            if ( ! is_null( $rendered_form ) ) {
                $prepared['rendered_form'] = trim( $rendered_form );
            }
        }

        
$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 );

        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $links               = $handler->prepare_item_links( $item_id );
            $links['collection'] = array(
                'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
            );
            $response->add_links( $links );
        }

        return $response;
    }

    /** * Retrieves the item schema, conforming to JSON Schema. * * @since 5.0.0 * * @return array Item schema data. */
$global_styles_config = $this->get_decoded_global_styles_json( $post->post_content );

        if ( is_wp_error( $global_styles_config ) ) {
            return $global_styles_config;
        }

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

        if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) {
            $global_styles_config = ( new WP_Theme_JSON( $global_styles_config, 'custom' ) )->get_raw_data();
            if ( rest_is_field_included( 'settings', $fields ) ) {
                $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass();
            }
            if ( rest_is_field_included( 'styles', $fields ) ) {
                $data['styles'] = ! empty( $global_styles_config['styles'] ) ? $global_styles_config['styles'] : new stdClass();
            }
        }

        if ( rest_is_field_included( 'author', $fields ) ) {
            $data['author'] = (int) $post->post_author;
        }

        

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $theme  = $item;
        $data   = array();
        $fields = $this->get_fields_for_response( $request );

        if ( rest_is_field_included( 'stylesheet', $fields ) ) {
            $data['stylesheet'] = $theme->get_stylesheet();
        }

        if ( rest_is_field_included( 'template', $fields ) ) {
            /** * Use the get_template() method, not the 'Template' header, for finding the template. * The 'Template' header is only good for what was written in the style.css, while * get_template() takes into account where WordPress actually located the theme and * whether it is actually valid. */
            $data['template'] = $theme->get_template();
        }
Home | Imprint | This part of the site doesn't use cookies.