Prépare une sortie de post unique pour la réponse.

Paramètres

$post

(WP_Post)(Obligatoire)Objet du post.

$requête

(WP_REST_Request)(Obligatoire)Objet de la requête.

Retour

(WP_REST_Response) Objet de la réponse.

Source

Fichier : wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

publicfunctionprepare_item_for_response($post,$request){$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('0000-00-00 00:00:00'===$post->post_date_gmt){$post_date_gmt=get_gmt_from_date($post->post_date);}else{$post_date_gmt=$post->post_date_gmt;}$data['date_gmt']=$this->prepare_date_response($post_date_gmt);}if(rest_is_field_included('guid',$fields)){$data['guid']=array(/** This filter is documented in wp-includes/post-template.php */'rendered'=>apply_filters('get_the_guid',$post->guid,$post->ID),'raw'=>$post->guid,);}if(rest_is_field_included('modified',$fields)){$data['modified']=$this->prepare_date_response($post->post_modified_gmt,$post->post_modified);}if(rest_is_field_included('modified_gmt',$fields)){/*
			 * For drafts, `post_modified_gmt` may not be set (see `post_date_gmt` comments
			 * above). In this case, shim the value based on the `post_modified` field
			 * with the site's timezone offset applied.
			 */if('0000-00-00 00:00:00'===$post->post_modified_gmt){$post_modified_gmt=gmdate('Y-m-d H:i:s',strtotime($post->post_modified)-(get_option('gmt_offset')*3600));}else{$post_modified_gmt=$post->post_modified_gmt;}$data['modified_gmt']=$this->prepare_date_response($post_modified_gmt);}if(rest_is_field_included('password',$fields)){$data['password']=$post->post_password;}if(rest_is_field_included('slug',$fields)){$data['slug']=$post->post_name;}if(rest_is_field_included('status',$fields)){$data['status']=$post->post_status;}if(rest_is_field_included('type',$fields)){$data['type']=$post->post_type;}if(rest_is_field_included('link',$fields)){$data['link']=get_permalink($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'));$data['title']['rendered']=get_the_title($post->ID);remove_filter('protected_title_format',array($this,'protected_title_format'));}$has_password_filter=false;if($this->can_access_password_content($post,$request)){// Allow access to the post, permissions already checked before.add_filter('post_password_required','__return_false');$has_password_filter=true;}if(rest_is_field_included('content',$fields)){$data['content']=array();}if(rest_is_field_included('content.raw',$fields)){$data['content']['raw']=$post->post_content;}if(rest_is_field_included('content.rendered',$fields)){/** This filter is documented in wp-includes/post-template.php */$data['content']['rendered']=post_password_required($post)?'':apply_filters('the_content',$post->post_content);}if(rest_is_field_included('content.protected',$fields)){$data['content']['protected']=(bool)$post->post_password;}if(rest_is_field_included('content.block_version',$fields)){$data['content']['block_version']=block_version($post->post_content);}if(rest_is_field_included('excerpt',$fields)){/** This filter is documented in wp-includes/post-template.php */$excerpt=apply_filters('get_the_excerpt',$post->post_excerpt,$post);/** This filter is documented in wp-includes/post-template.php */$excerpt=apply_filters('the_excerpt',$excerpt);$data['excerpt']=array('raw'=>$post->post_excerpt,'rendered'=>post_password_required($post)?'':$excerpt,'protected'=>(bool)$post->post_password,);}if($has_password_filter){// Reset filter.remove_filter('post_password_required','__return_false');}if(rest_is_field_included('author',$fields)){$data['author']=(int)$post->post_author;}if(rest_is_field_included('featured_media',$fields)){$data['featured_media']=(int)get_post_thumbnail_id($post->ID);}if(rest_is_field_included('parent',$fields)){$data['parent']=(int)$post->post_parent;}if(rest_is_field_included('menu_order',$fields)){$data['menu_order']=(int)$post->menu_order;}if(rest_is_field_included('comment_status',$fields)){$data['comment_status']=$post->comment_status;}if(rest_is_field_included('ping_status',$fields)){$data['ping_status']=$post->ping_status;}if(rest_is_field_included('sticky',$fields)){$data['sticky']=is_sticky($post->ID);}if(rest_is_field_included('template',$fields)){$template=get_page_template_slug($post->ID);if($template){$data['template']=$template;}else{$data['template']='';}}if(rest_is_field_included('format',$fields)){$data['format']=get_post_format($post->ID);// Fill in blank post format.if(empty($data['format'])){$data['format']='standard';}}if(rest_is_field_included('meta',$fields)){$data['meta']=$this->meta->get_value($post->ID,$request);}$taxonomies=wp_list_filter(get_object_taxonomies($this->post_type,'objects'),array('show_in_rest'=>true));foreach($taxonomiesas$taxonomy){$base=!empty($taxonomy->rest_base)?$taxonomy->rest_base:$taxonomy->name;if(rest_is_field_included($base,$fields)){$terms=get_the_terms($post,$taxonomy->name);$data[$base]=$terms?array_values(wp_list_pluck($terms,'term_id')):array();}}$post_type_obj=get_post_type_object($post->post_type);if(is_post_type_viewable($post_type_obj)&&$post_type_obj->public){$permalink_template_requested=rest_is_field_included('permalink_template',$fields);$generated_slug_requested=rest_is_field_included('generated_slug',$fields);if($permalink_template_requested||$generated_slug_requested){if(!function_exists('get_sample_permalink')){require_onceABSPATH.'wp-admin/includes/post.php';}$sample_permalink=get_sample_permalink($post->ID,$post->post_title,'');if($permalink_template_requested){$data['permalink_template']=$sample_permalink[0];}if($generated_slug_requested){$data['generated_slug']=$sample_permalink[1];}}}$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);$links=$this->prepare_links($post);$response->add_links($links);if(!empty($links['self']['href'])){$actions=$this->get_available_actions($post,$request);$self=$links['self']['href'];foreach($actionsas$rel){$response->add_link($rel,$self);}}/**
		 * Filters the post data for a REST API response.
		 *
		 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
		 *
		 * Possible filter names include:
		 *
		 *  - `rest_prepare_post`
		 *  - `rest_prepare_page`
		 *  - `rest_prepare_attachment`
		 *
		 * @since 4.7.0
		 *
		 * @param WP_REST_Response $response The response object.
		 * @param WP_Post          $post     Post object.
		 * @param WP_REST_Request  $request  Request object.
		 */returnapply_filters("rest_prepare_{$this->post_type}",$response,$post,$request);}

Related

Utilise

Utilisations Description
wp-includes/rest-api.php : rest_is_field_included()

Étant donné un tableau de champs à inclure dans une réponse, dont certains peuvent être... nested.fields, détermine si le champ fourni doit être inclus dans le corps de la réponse.

wp-includes/blocks.php : block_version()

Renvoie la version actuelle du format de bloc que la chaîne de contenu utilise.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::get_available_actions()

Obtenir les relations de liens disponibles pour le post et l'utilisateur actuel.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::prepare_links()

Prépare les liens pour la requête.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : rest_prepare_{$this->post_type}

Filtre les données du post pour une réponse à l'API REST.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::prepare_date_response()

Vérifie le post_date_gmt ou modified_gmt et prépare toute date de post ou de modification pour une sortie de post unique.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::can_access_password_content()

Vérifie si l'utilisateur peut accéder au contenu protégé par un mot de passe.

wp-includes/rest-api.php : rest_ensure_response()

Assure qu'une réponse REST est un objet de réponse (pour la cohérence).

wp-includes/post.php : is_post_type_viewable()

Détermine si un type de post est considéré comme "visualisable".

wp-admin/includes/post.php : get_sample_permalink()

Obtenir un exemple de permalien basé sur le nom du post.

wp-includes/category-template.php : get_the_terms()

Récupère les termes de la taxonomie qui sont attachés au post.

wp-includes/formatting.php : get_gmt_from_date()

Étant donné une date dans le fuseau horaire du site, renvoie cette date en UTC.

wp-includes/query.php : setup_postdata()

Configurer les données globales du post.

wp-includes/functions.php : wp_list_filter()

Filtre une liste d'objets, sur la base d'un ensemble d'arguments clé => valeur.

wp-includes/functions.php : wp_list_pluck()

Pluck un certain champ de chaque objet dans une liste.

wp-includes/taxonomy.php : get_object_taxonomies()

Renvoie les noms ou les objets des taxonomies qui sont enregistrés pour l'objet ou le type d'objet demandé, comme un objet post ou un nom de type post.

wp-includes/link-template.php : get_permalink()

Récupère le permalien complet pour le post ou l'ID de post actuel.

wp-includes/plugin.php : apply_filters()

Appelle les fonctions de rappel qui ont été ajoutées à un crochet de filtre.

wp-includes/plugin.php : add_filter()

Accroche une fonction ou une méthode à une action de filtre spécifique.

wp-includes/plugin.php : remove_filter()

Supprime une fonction d'un crochet de filtre spécifié.

wp-includes/option.php : get_option()

Récupère une valeur d'option basée sur un nom d'option.

wp-includes/post-thumbnail-template.php : get_post_thumbnail_id()

Récupérer l'identifiant de la vignette du post.

wp-includes/post-template.php : get_page_template_slug()

Obtenir le nom de fichier du modèle spécifique pour un post donné.

wp-includes/post-template.php : post_password_required()

Si le post nécessite un mot de passe et si le mot de passe correct a été fourni.

wp-includes/post-template.php : get_the_guid

Filtre l'identifiant unique global (guid) du post.

wp-includes/post-template.php : le_contenu

Filtre le contenu du post.

wp-includes/post-template.php : get_the_excerpt

Filtre l'extrait de post récupéré.

wp-includes/post-template.php : l'extrait_de_poste

Filtre l'extrait de post affiché.

wp-includes/post-template.php : get_the_title()

Récupérer le titre du post.

wp-includes/post.php : is_sticky()

Détermine si un message est collant.

wp-includes/post.php : get_post_type_object()

Récupère un objet de type de post par son nom.

wp-includes/post-formats.php : get_post_format()

Récupérer le slug de format pour un post.

Utilisé par

Utilisé par Description
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php : WP_REST_Attachments_Controller::prepare_item_for_response()

Prépare une seule sortie de pièce jointe pour la réponse.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::create_item()

Crée un article unique.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::update_item()

Met à jour un seul post.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::delete_item()

Supprime un seul post.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::get_item()

Récupère un seul post.

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php : WP_REST_Posts_Controller::get_items()

Récupère une collection de posts.

Changelog

Version Description
4.7.0 Introduit.