Update API

This commit is contained in:
Hoang Huu 2019-10-01 15:57:58 +07:00
parent ff39207b86
commit d5bb6abe51
5 changed files with 41 additions and 18 deletions

View File

@ -115,7 +115,7 @@ class OpalEstate_Agency {
* Get url of user avatar by agency id * Get url of user avatar by agency id
*/ */
public static function get_avatar_url( $userID ) { public static function get_avatar_url( $userID ) {
return get_post_meta( $userID, OPALESTATE_AGENCY_PREFIX . "avatar", true ); return get_post_meta( $userID, OPALESTATE_AGENCY_PREFIX . 'avatar', true );
} }
/** /**
@ -223,11 +223,19 @@ class OpalEstate_Agency {
*/ */
public static function get_link( $agency_id ) { public static function get_link( $agency_id ) {
$agency = get_post( $agency_id ); $agency = get_post( $agency_id );
if ( ! $agency ) {
return [
'name' => '',
'avatar' => '',
'link' => '',
];
}
$url = self::get_avatar_url( $agency_id ); $url = self::get_avatar_url( $agency_id );
return [ return [
'name' => $agency->post_title, 'name' => $agency->post_title,
'avatar' => $url, 'avatar' => $url ? $url : '',
'link' => get_permalink( $agency->ID ), 'link' => get_permalink( $agency->ID ),
]; ];
} }

View File

@ -134,6 +134,15 @@ class OpalEstate_Agent {
public static function get_link( $agent_id ) { public static function get_link( $agent_id ) {
$agent = get_post( $agent_id ); $agent = get_post( $agent_id );
if ( ! $agent ) {
return [
'name' => '',
'avatar' => '',
'link' => '',
];
}
$url = self::get_avatar_url( $agent_id ); $url = self::get_avatar_url( $agent_id );
return [ return [
@ -141,7 +150,6 @@ class OpalEstate_Agent {
'avatar' => $url, 'avatar' => $url,
'link' => get_permalink( $agent->ID ), 'link' => get_permalink( $agent->ID ),
]; ];
} }
public static function metaboxes_fields() { public static function metaboxes_fields() {

View File

@ -227,8 +227,7 @@ class Property_Api extends Base_Api {
$property['amenities'] = $data->get_amenities(); $property['amenities'] = $data->get_amenities();
$property['types'] = $data->get_types_tax(); $property['types'] = $data->get_types_tax();
$property['author_type'] = $data->get_author_type(); $property['author_type'] = $data->get_author_type();
$property['author_data'] = $data->get_author_link(); $property['author_data'] = $data->get_author_link_data();
return apply_filters( 'opalestate_api_properties_property', $property ); return apply_filters( 'opalestate_api_properties_property', $property );
} }

View File

@ -427,11 +427,22 @@ class Opalestate_Property {
* *
*/ */
public function render_author_link() { public function render_author_link() {
$data = $this->get_author_link_data();
if ( ! $data ) {
return;
}
$avatar = $data['avatar'] ? $data['avatar'] : opalestate_get_image_avatar_placehold();
$avatar = '<img class="avatar" src="' . esc_url( $avatar ) . '" alt="' . $data['name'] . '" />';
return '<a href="' . $data['link'] . '" aria-label="' . $data['name'] . '" class="author-link"><span aria-label="' . $data['name'] . '" class="author-avatar hint--top">' . $avatar . '</span><span class="author-name">' . $data['name'] . '</span></a>';
}
public function get_author_link_data() {
$data = [];
switch ( $this->get_author_type() ) { switch ( $this->get_author_type() ) {
case 'hide': case 'hide':
$data = [];
return;
break; break;
case 'agent': case 'agent':
@ -448,28 +459,25 @@ class Opalestate_Property {
break; break;
} }
$avatar = $data['avatar'] ? $data['avatar'] : opalestate_get_image_avatar_placehold(); return $data;
$avatar = '<img class="avatar" src="' . esc_url( $avatar ) . '" alt="' . $data['name'] . '" />';
return '<a href="' . $data['link'] . '" aria-label="' . $data['name'] . '" class="author-link"><span aria-label="' . $data['name'] . '" class="author-avatar hint--top">' . $avatar . '</span><span class="author-name">' . $data['name'] . '</span></a>';
} }
public function get_author_link() { public function get_author_link() {
$user_id = get_post_field( 'post_author', $this->get_id() );
$image_id = get_user_meta( get_the_author_meta( 'ID' ), OPALESTATE_USER_PROFILE_PREFIX . 'avatar_id', true ); $image_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'avatar_id', true );
$related_id = get_user_meta( get_the_author_meta( 'ID' ), OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true ); $related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
if ( $image_id ) { if ( $image_id ) {
$url = wp_get_attachment_url( $image_id ); $url = wp_get_attachment_url( $image_id );
} else { } else {
$url = get_avatar_url( get_the_author_meta( 'email' ) ); $url = get_avatar_url( get_the_author_meta( 'email', $user_id ) );
} }
if ( $related_id ) { if ( $related_id ) {
$authorlink = get_permalink( $related_id ); $authorlink = get_permalink( $related_id );
$author = get_the_title( $related_id ); $author = get_the_title( $related_id );
} else { } else {
$authorlink = get_author_posts_url( get_the_author_meta( 'ID' ) ); $authorlink = get_author_posts_url( $user_id );
$author = get_the_author(); $author = get_the_author();
} }