Prepara l'output di un singolo post per la risposta.

Parametri

$post

(WP_Post)(Richiesto)Oggetto del post.

$Richiesta

(WP_REST_Request)(Richiesto)Oggetto della richiesta.

Ritorno

(WP_REST_Response) Oggetto della risposta.

Fonte

File: 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);}

Correlato

Utilizzi

Usi Descrizione
wp-includes/rest-api.php: rest_is_field_included()

Dato un array di campi da includere in una risposta, alcuni dei quali possono essere nested.fields, determina se il campo fornito deve essere incluso nel corpo della risposta.

wp-includes/blocks.php: block_version()

Restituisce la versione corrente del formato del blocco che la stringa di contenuto sta usando.

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

Ottiene le relazioni di link disponibili per il post e l'utente corrente.

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

Prepara i collegamenti per la richiesta.

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

Filtra i dati del post per una risposta API REST.

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

Controlla il post_date_gmt o modified_gmt e prepara qualsiasi data di pubblicazione o modifica per l'output di un singolo post.

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

Verifica se l'utente può accedere a contenuti protetti da password.

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

Assicura che una risposta REST sia un oggetto risposta (per coerenza).

wp-includes/post.php: is_post_type_viewable()

Determina se un tipo di post è considerato "visualizzabile".

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

Ottiene un permalink di esempio basato sul nome del post.

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

Recupera i termini della tassonomia che sono allegati al post.

wp-includes/formatting.php: get_gmt_from_date()

Data una data nel fuso orario del sito, restituisce quella data in UTC.

wp-includes/query.php: setup_postdata()

Imposta i dati globali dei post.

wp-includes/functions.php: wp_list_filter()

Filtra un elenco di oggetti, in base a un insieme di argomenti chiave => valore.

wp-includes/functions.php: wp_list_pluck()

Estrae un certo campo da ogni oggetto di una lista.

wp-includes/taxonomy.php: get_object_taxonomies()

Restituisce i nomi o gli oggetti delle tassonomie registrate per l'oggetto o il tipo di oggetto richiesto, come il nome di un oggetto post o di un tipo di post.

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

Recupera il permalink completo per il post o l'ID del post corrente.

wp-includes/plugin.php: applica_filtri()

Richiama le funzioni di callback che sono state aggiunte a un gancio per filtri.

wp-includes/plugin.php: add_filter()

Aggancia una funzione o un metodo a un'azione di filtro specifica.

wp-includes/plugin.php: rimuovi_filtro()

Rimuove una funzione da un gancio di filtro specificato.

wp-includes/option.php: get_option()

Recupera il valore di un'opzione in base a un nome di opzione.

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

Recupera l'ID della miniatura del post.

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

Ottiene il nome del file del template specifico per un determinato post.

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

Se il post richiede una password e se è stata fornita la password corretta.

wp-includes/post-template.php: get_the_guid

Filtra il Global Unique Identifier (guid) del post.

wp-includes/post-template.php: il_contenuto

Filtra il contenuto del post.

wp-includes/post-template.php: get_the_excerpt

Filtra l'estratto del post recuperato.

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

Filtra l'estratto del post visualizzato.

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

Recupera il titolo del post.

wp-includes/post.php: is_sticky()

Determina se un post è appiccicoso.

wp-includes/post.php: get_post_type_object()

Recupera un oggetto di tipo post per nome.

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

Recupera lo slug del formato per un post

Usato da

Usato da Descrizione
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::prepare_item_for_response()

Prepara un singolo allegato in uscita per la risposta.

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

Crea un singolo post.

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

Aggiorna un singolo post.

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

Cancella un singolo post.

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

Recupera un singolo post.

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

Recupera un insieme di post.

Changelog

Versione Descrizione
4.7.0 Introdotto.