From d5bb6abe51367305f21816af0214540f97681fcc Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Tue, 1 Oct 2019 15:57:58 +0700 Subject: [PATCH] Update API --- inc/agency/class-opalestate-agency.php | 12 ++++++-- inc/agent/class-opalestate-agent.php | 10 ++++++- inc/api/class-base-api.php | 2 +- inc/api/v1/property.php | 3 +- inc/property/class-opalestate-property.php | 32 ++++++++++++++-------- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/inc/agency/class-opalestate-agency.php b/inc/agency/class-opalestate-agency.php index bea6bc37..55150b02 100755 --- a/inc/agency/class-opalestate-agency.php +++ b/inc/agency/class-opalestate-agency.php @@ -115,7 +115,7 @@ class OpalEstate_Agency { * Get url of user avatar by agency id */ 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 ) { $agency = get_post( $agency_id ); + if ( ! $agency ) { + return [ + 'name' => '', + 'avatar' => '', + 'link' => '', + ]; + } + $url = self::get_avatar_url( $agency_id ); return [ 'name' => $agency->post_title, - 'avatar' => $url, + 'avatar' => $url ? $url : '', 'link' => get_permalink( $agency->ID ), ]; } diff --git a/inc/agent/class-opalestate-agent.php b/inc/agent/class-opalestate-agent.php index 2ba759bd..e86329b8 100755 --- a/inc/agent/class-opalestate-agent.php +++ b/inc/agent/class-opalestate-agent.php @@ -134,6 +134,15 @@ class OpalEstate_Agent { public static function get_link( $agent_id ) { $agent = get_post( $agent_id ); + + if ( ! $agent ) { + return [ + 'name' => '', + 'avatar' => '', + 'link' => '', + ]; + } + $url = self::get_avatar_url( $agent_id ); return [ @@ -141,7 +150,6 @@ class OpalEstate_Agent { 'avatar' => $url, 'link' => get_permalink( $agent->ID ), ]; - } public static function metaboxes_fields() { diff --git a/inc/api/class-base-api.php b/inc/api/class-base-api.php index 94191c9a..5eee0f8c 100755 --- a/inc/api/class-base-api.php +++ b/inc/api/class-base-api.php @@ -191,4 +191,4 @@ abstract class Base_API { private function invalid_key() { return new WP_Error( 'rest_forbidden', esc_html__( 'Invalid API key!' ), array( 'status' => rest_authorization_required_code() ) ); } -} \ No newline at end of file +} diff --git a/inc/api/v1/property.php b/inc/api/v1/property.php index 047b6a2b..020ab999 100644 --- a/inc/api/v1/property.php +++ b/inc/api/v1/property.php @@ -227,8 +227,7 @@ class Property_Api extends Base_Api { $property['amenities'] = $data->get_amenities(); $property['types'] = $data->get_types_tax(); $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 ); } diff --git a/inc/property/class-opalestate-property.php b/inc/property/class-opalestate-property.php index 7c75601b..c6cdb8c8 100755 --- a/inc/property/class-opalestate-property.php +++ b/inc/property/class-opalestate-property.php @@ -427,11 +427,22 @@ class Opalestate_Property { * */ 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 = '' . $data['name'] . ''; + + return '' . $avatar . '' . $data['name'] . ''; + } + + public function get_author_link_data() { + $data = []; switch ( $this->get_author_type() ) { - case 'hide': - - return; + $data = []; break; case 'agent': @@ -448,28 +459,25 @@ class Opalestate_Property { break; } - $avatar = $data['avatar'] ? $data['avatar'] : opalestate_get_image_avatar_placehold(); - $avatar = '' . $data['name'] . ''; - - return '' . $avatar . '' . $data['name'] . ''; + return $data; } public function get_author_link() { - - $image_id = get_user_meta( get_the_author_meta( '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 ); + $user_id = get_post_field( 'post_author', $this->get_id() ); + $image_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'avatar_id', true ); + $related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true ); if ( $image_id ) { $url = wp_get_attachment_url( $image_id ); } else { - $url = get_avatar_url( get_the_author_meta( 'email' ) ); + $url = get_avatar_url( get_the_author_meta( 'email', $user_id ) ); } if ( $related_id ) { $authorlink = get_permalink( $related_id ); $author = get_the_title( $related_id ); } else { - $authorlink = get_author_posts_url( get_the_author_meta( 'ID' ) ); + $authorlink = get_author_posts_url( $user_id ); $author = get_the_author(); }