| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
/** |
| 6 |
* Contact |
| 7 |
* |
| 8 |
* The Property Hive contact class handles contact data. |
| 9 |
* |
| 10 |
* @class PH_Contact |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Contact { |
| 17 |
|
| 18 |
/** @public int Contact (post) ID */ |
| 19 |
public $id; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the contact if ID is passed, otherwise the contact is new and empty. |
| 23 |
* |
| 24 |
* @access public |
| 25 |
* @param string $id (default: '') |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function __construct( $id = '', $user_id = '' ) { |
| 29 |
|
| 30 |
if ( $user_id > 0 ) |
| 31 |
{ |
| 32 |
// We've been passed a user ID. Need to get contact ID based on user_id |
| 33 |
$contact_query = new WP_Query( array( 'post_type' => 'contact', 'meta_key' => '_user_id', 'meta_value' => $user_id, 'fields' => 'ids', 'posts_per_page' => 1 ) ); |
| 34 |
|
| 35 |
if ( $contact_query->have_posts() ) |
| 36 |
{ |
| 37 |
while ( $contact_query->have_posts() ) |
| 38 |
{ |
| 39 |
$contact_query->the_post(); |
| 40 |
|
| 41 |
$this->get_contact( get_the_ID() ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
wp_reset_postdata(); |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
if ( $id > 0 ) { |
| 50 |
$this->get_contact( $id ); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Gets a contact from the database. |
| 57 |
* |
| 58 |
* @access public |
| 59 |
* @param int $id (default: 0) |
| 60 |
* @return bool |
| 61 |
*/ |
| 62 |
public function get_contact( $id = 0 ) { |
| 63 |
if ( ! $id ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
if ( $result = get_post( $id ) ) { |
| 67 |
$this->populate( $result ); |
| 68 |
return true; |
| 69 |
} |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* __isset function. |
| 75 |
* |
| 76 |
* @access public |
| 77 |
* @param mixed $key |
| 78 |
* @return bool |
| 79 |
*/ |
| 80 |
public function __isset( $key ) { |
| 81 |
if ( ! $this->id ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
return metadata_exists( 'post', $this->id, '_' . $key ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* __get function. |
| 89 |
* |
| 90 |
* @access public |
| 91 |
* @param mixed $key |
| 92 |
* @return mixed |
| 93 |
*/ |
| 94 |
public function __get( $key ) { |
| 95 |
// Get values or default if not set |
| 96 |
$value = get_post_meta( $this->id, $key, true ); |
| 97 |
if ($value == '') |
| 98 |
{ |
| 99 |
$value = get_post_meta( $this->id, '_' . $key, true ); |
| 100 |
} |
| 101 |
return $value; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Populates a contact from the loaded post data. |
| 106 |
* |
| 107 |
* @access public |
| 108 |
* @param mixed $result |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function populate( $result ) { |
| 112 |
// Standard post data |
| 113 |
$this->id = $result->ID; |
| 114 |
$this->post_title = $result->post_title; |
| 115 |
$this->post_status = $result->post_status; |
| 116 |
} |
| 117 |
} |
| 118 |
|