Opal-Estate-Pro/inc/agency/class-opalestate-agency.php

395 lines
8.7 KiB
PHP
Raw Normal View History

2019-09-10 06:27:33 +02:00
<?php
/**
2019-09-25 03:44:27 +02:00
* OpalEstate_Agency
2019-09-10 06:27:33 +02:00
*
* @package opalestate
* @author Opal Team <info@wpopal.com >
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* @class OpalEstate_Agency
*
* @version 1.0
*/
class OpalEstate_Agency {
/**
* @var String $author_name
*
* @access protected
*/
protected $author_name;
2019-09-28 11:39:55 +02:00
/**
* @var String $author_name
*
* @access protected
*/
public $post_id;
/**
* @var String $author_name
*
* @access protected
*/
public $author;
2019-09-10 06:27:33 +02:00
/**
* @var Boolean $is_featured
*
* @access protected
*/
protected $is_featured;
/**
* Get A Instance Of Opalestate_Property
*/
public static function get_instance( $post_id = null ) {
static $_instance;
if ( ! $_instance ) {
$_instance = new OpalEstate_Agency( $post_id );
}
return $_instance;
}
/**
* Constructor
*/
public function __construct( $post_id = null ) {
2019-09-28 11:39:55 +02:00
global $post ;
2019-09-10 06:27:33 +02:00
2019-09-28 11:39:55 +02:00
if( $post == null ) {
$post = WP_Post::get_instance( $post_id );
}
$this->post = $post;
2019-09-10 06:27:33 +02:00
$this->post_id = $post_id ? $post_id : get_the_ID();
$this->author = get_userdata( $post->post_author );
$this->author_name = ! empty( $this->author ) ? sprintf( '%s %s', $this->author->first_name, $this->author->last_name ) : null;
$this->is_featured = $this->get_meta( 'featured' );
$this->is_trusted = $this->get_meta( 'trusted' );
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public function get_id() {
return $this->post_id;
}
/**
* Get Collection Of soicals with theirs values
*/
public function get_socials() {
$socials = [
'facebook' => '',
'twitter' => '',
'pinterest' => '',
'google' => '',
'instagram' => '',
'linkedIn' => '',
];
$output = [];
foreach ( $socials as $social => $k ) {
$data = $this->get_meta( $social );
if ( $data && $data != "#" && ! empty( $data ) ) {
$output[ $social ] = $data;
}
}
return $output;
}
/**
* Get url of user avatar by agency id
*/
2019-10-03 05:10:43 +02:00
public static function get_avatar_url( $userID, $size='thumbnail' ) {
2019-10-02 09:15:17 +02:00
2019-10-03 06:31:02 +02:00
$id = get_post_meta( $userID, OPALESTATE_AGENCY_PREFIX . 'avatar_id', true );
$url = wp_get_attachment_image_url( $id, $size );
2019-10-02 09:15:17 +02:00
if( $url ) {
return $url;
}
2019-10-01 10:57:58 +02:00
return get_post_meta( $userID, OPALESTATE_AGENCY_PREFIX . 'avatar', true );
2019-09-10 06:27:33 +02:00
}
/**
* Render list of levels of agency
*/
public function render_level() {
$levels = wp_get_post_terms( $this->post_id, 'opalestate_agency_cat' );
if ( empty( $levels ) ) {
return;
}
$output = '<span class="agency-levels">';
foreach ( $levels as $key => $value ) {
$output .= '<span class="agency-label"><span>' . $value->name . '</span></span>';
}
$output .= '</span>';
echo $output;
}
/**
* get meta data value of key without prefix
*/
public function get_meta( $key ) {
return get_post_meta( $this->get_id(), OPALESTATE_AGENCY_PREFIX . $key, true );
}
/**
* return true if this agency is featured
*/
public function is_featured() {
return $this->is_featured;
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public function render_avatar() {
}
/**
* render block information by id
*/
public static function render_box_info( $post_id ) {
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public function get_gallery() {
return $this->get_meta( 'gallery' );
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public function get_trusted() {
return $this->is_trusted;
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public function get_members() {
$team = [];
$ids = get_post_meta( $this->post_id, OPALESTATE_AGENCY_PREFIX . 'team', true );
2019-09-25 03:44:27 +02:00
if ( $ids ) {
foreach ( $ids as $id ) {
$user = get_user_by( 'id', $id );
$team[] = [
'id' => $user->ID,
'name' => $user->display_name,
'avatar_url' => OpalEstate_User::get_author_picture( $user->ID ),
'username' => $user->user_login,
'description' => 'okokok',
];
}
2019-09-10 06:27:33 +02:00
}
return $team;
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public static function get_link( $agency_id ) {
$agency = get_post( $agency_id );
2019-10-01 10:57:58 +02:00
if ( ! $agency ) {
return [
'name' => '',
'avatar' => '',
'link' => '',
];
}
2019-09-10 06:27:33 +02:00
$url = self::get_avatar_url( $agency_id );
return [
'name' => $agency->post_title,
2019-10-01 10:57:58 +02:00
'avatar' => $url ? $url : '',
2019-09-10 06:27:33 +02:00
'link' => get_permalink( $agency->ID ),
];
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public static function metaboxes_fields() {
$metabox = new Opalestate_Agency_MetaBox();
$fields = $metabox->metaboxes_admin_fields();
return array_merge_recursive( $fields, $metabox->get_social_fields( OPALESTATE_AGENCY_PREFIX ) );
}
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
public function get_rating_counts() {
return $this->get_meta( 'rating_count' ) ? $this->get_meta( 'rating_count' ) : 0;
}
/**
* Get average rating.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return float
*/
public function get_average_rating() {
return $this->get_meta( 'average_rating' ) ? $this->get_meta( 'average_rating' ) : 0;
}
/**
* Get review count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
public function get_review_count() {
return $this->get_meta( 'review_count' ) ? $this->get_meta( 'review_count' ) : 0;
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public function get_rating_count_stats() {
return $this->get_meta( 'rating_count_stats' ) ? $this->get_meta( 'rating_count_stats' ) : [
5 => 0,
4 => 0,
3 => 0,
2 => 0,
1 => 0,
];
}
2019-09-28 11:39:55 +02:00
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
public function get_category_tax ( ) {
return wp_get_post_terms( $this->post_id, 'opalestate_agency_cat' );
}
/**
* Get rating count.
*
* @param string $context What the value is for. Valid values are view and edit.
* @return int
*/
2019-09-10 06:27:33 +02:00
public function get_rating_average_stats() {
return $this->get_meta( 'rating_average_stats' );
}
/**
2019-09-25 03:44:27 +02:00
* Update user data.
2019-09-10 06:27:33 +02:00
*
2019-09-25 03:44:27 +02:00
* @param $user_id User ID.
2019-09-10 06:27:33 +02:00
*/
public static function update_user_data( $user_id ) {
$fields = self::metaboxes_fields();
$others = [
'avatar_id' => '',
'map' => '',
];
foreach ( $fields as $key => $field ) {
$kpos = $field['id'];
2019-09-25 03:44:27 +02:00
$tmp = str_replace( OPALESTATE_AGENCY_PREFIX, '', $field['id'] );
2019-09-10 06:27:33 +02:00
if ( isset( $_POST[ $kpos ] ) && $tmp ) {
$data = is_string( $_POST[ $kpos ] ) ? sanitize_text_field( $_POST[ $kpos ] ) : $_POST[ $kpos ];
update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . $tmp, $data );
}
}
// update for others
foreach ( $others as $key => $value ) {
$kpos = OPALESTATE_AGENCY_PREFIX . $key;
if ( isset( $_POST[ $kpos ] ) ) {
$data = is_string( $_POST[ $kpos ] ) ? sanitize_text_field( $_POST[ $kpos ] ) : $_POST[ $kpos ];
update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . $key, $data );
}
}
}
/**
2019-09-25 03:44:27 +02:00
* Update data from user.
2019-09-10 06:27:33 +02:00
*
2019-09-25 03:44:27 +02:00
* @param $related_id Post ID
2019-09-10 06:27:33 +02:00
*/
public static function update_data_from_user( $related_id ) {
$fields = self::metaboxes_fields();
$others = [
'avatar_id' => '',
'map' => '',
];
2019-09-25 03:44:27 +02:00
foreach ( $fields as $key => $field ) {
$tmp = str_replace( OPALESTATE_AGENCY_PREFIX, '', $field['id'] );
2019-09-10 06:27:33 +02:00
$kpos = OPALESTATE_USER_PROFILE_PREFIX . $tmp;
if ( isset( $_POST[ $kpos ] ) && $tmp ) {
$data = is_string( $_POST[ $kpos ] ) ? sanitize_text_field( $_POST[ $kpos ] ) : $_POST[ $kpos ];
update_post_meta( $related_id, OPALESTATE_AGENCY_PREFIX . $tmp, $data );
}
}
// update for others
foreach ( $others as $key => $value ) {
$kpos = OPALESTATE_USER_PROFILE_PREFIX . $key;
if ( isset( $_POST[ $kpos ] ) ) {
$data = is_string( $_POST[ $kpos ] ) ? sanitize_text_field( $_POST[ $kpos ] ) : $_POST[ $kpos ];
update_post_meta( $related_id, OPALESTATE_AGENCY_PREFIX . $key, $data );
}
}
}
}