Return header image and moderators

This commit is contained in:
weryques 2018-04-25 14:35:54 -03:00
parent 6a220eb16a
commit 2a0f17af26
3 changed files with 85 additions and 46 deletions

View File

@ -148,9 +148,28 @@ class REST_Collections_Controller extends REST_Controller {
$item_arr = $item->__toArray();
if ( $request['context'] === 'edit' ) {
$moderators_ids = $item_arr['moderators_ids'];
$moderators = [];
foreach ($moderators_ids as $id){
$user_data = get_userdata($id);
if($user_data){
$user['name'] = $user_data->display_name;
//$user['roles'] = $user_data->roles;
$user['id'] = $user_data->ID;
$moderators[] = $user;
}
}
$item_arr['moderators'] = $moderators;
$item_arr['current_user_can_edit'] = $item->can_edit();
}
unset($item_arr['moderators_ids']);
} else {
$attributes_to_filter = $request['fetch_only'];

View File

@ -31,6 +31,7 @@ class Collection extends Entity {
$enable_cover_page,
$cover_page_id,
$header_image_id,
$header_image,
$moderators_ids;
/**
@ -68,7 +69,7 @@ class Collection extends Entity {
$array_collection = parent::__toArray();
$array_collection['featured_image'] = $this->get_featured_image();
$array_collection['featured_img_id'] = $this->get_featured_img_id();
$array_collection['header_image'] = $this->get_header_image();
$array_collection['author_name'] = $this->get_author_name();
return $array_collection;
@ -207,6 +208,13 @@ class Collection extends Entity {
return get_the_post_thumbnail_url( $this->get_id(), 'full' );
}
/**
* @return false|string
*/
function get_header_image(){
return wp_get_attachment_url( $this->get_header_image_id() );
}
/**
* @param $id
*/

View File

@ -117,7 +117,6 @@ abstract class Repository {
}
$post_t = $collection->get_db_identifier();
$obj->WP_Post->post_type = $post_t;
}
@ -134,50 +133,9 @@ abstract class Repository {
}
}
if ( method_exists( $obj, 'get_featured_img_id' ) ) {
if ( ! get_post_thumbnail_id( $obj->WP_Post->ID ) ) {
// was added a thumbnail
$diffs = $this->insert_thumbnail( $obj, $diffs );
$settled = set_post_thumbnail( $obj->WP_Post, (int) $obj->get_featured_img_id() );
if ( $settled ) {
$thumbnail_url = get_the_post_thumbnail_url( $obj->WP_Post->ID );
$diffs['featured_image'] = [
'new' => $thumbnail_url,
'old' => '',
'diff_with_index' => 0,
];
}
} else {
// was update a thumbnail
$old_thumbnail = get_the_post_thumbnail_url( $obj->WP_Post->ID );
$fid = $obj->get_featured_img_id();
if(!$fid){
$settled = delete_post_thumbnail($obj->WP_Post);
} else {
$settled = set_post_thumbnail( $obj->WP_Post, (int) $fid );
}
if ( $settled ) {
$thumbnail_url = get_the_post_thumbnail_url( $obj->WP_Post->ID );
$diffs['featured_image'] = [
'new' => $thumbnail_url,
'old' => $old_thumbnail,
'diff_with_index' => 0,
];
}
}
}
// TODO: Logs for header image insert and update
do_action( 'tainacan-insert', $obj, $diffs, $is_update );
do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj );
@ -741,12 +699,66 @@ abstract class Repository {
}
}
unset($diff['id'], $diff['collection_id'], $diff['author_id'], $diff['creation_date']);
unset($diff['id'], $diff['collection_id'], $diff['author_id'], $diff['creation_date'], $diff['featured_img_id']);
$diff = apply_filters( 'tainacan-entity-diff', $diff, $new, $old );
return $diff;
}
/**
* @param $obj
* @param $diffs
*
* @return mixed
*/
protected function insert_thumbnail( $obj, $diffs ) {
if ( method_exists( $obj, 'get_featured_img_id' ) ) {
if ( ! get_post_thumbnail_id( $obj->WP_Post->ID ) ) {
// was added a thumbnail
$settled = set_post_thumbnail( $obj->WP_Post, (int) $obj->get_featured_img_id() );
if ( $settled ) {
$thumbnail_url = get_the_post_thumbnail_url( $obj->WP_Post->ID );
$diffs['featured_image'] = [
'new' => $thumbnail_url,
'old' => '',
'diff_with_index' => 0,
];
}
} else {
// was update a thumbnail
$old_thumbnail = get_the_post_thumbnail_url( $obj->WP_Post->ID );
$fid = $obj->get_featured_img_id();
if ( ! $fid ) {
$settled = delete_post_thumbnail( $obj->WP_Post );
} else {
$settled = set_post_thumbnail( $obj->WP_Post, (int) $fid );
}
if ( $settled ) {
$thumbnail_url = get_the_post_thumbnail_url( $obj->WP_Post->ID );
$diffs['featured_image'] = [
'new' => $thumbnail_url,
'old' => $old_thumbnail,
'diff_with_index' => 0,
];
}
}
}
return $diffs;
}
}
?>