| 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 |
/** |
| 119 |
* Get the full formatted address |
| 120 |
* |
| 121 |
* @access public |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public function get_formatted_full_address( $separator = ', ' ) { |
| 125 |
// Standard post data |
| 126 |
|
| 127 |
$return = ''; |
| 128 |
|
| 129 |
$company_name = $this->_company_name; |
| 130 |
if ($company_name != '') |
| 131 |
{ |
| 132 |
$return .= $company_name; |
| 133 |
} |
| 134 |
$address_name_number = $this->_address_name_number; |
| 135 |
if ($address_name_number != '') |
| 136 |
{ |
| 137 |
if ($return != '') { $return .= $separator; } |
| 138 |
$return .= $address_name_number; |
| 139 |
} |
| 140 |
$address_street = $this->_address_street; |
| 141 |
if ($address_street != '') |
| 142 |
{ |
| 143 |
if ($return != '') { $return .= ' '; } |
| 144 |
$return .= $address_street; |
| 145 |
} |
| 146 |
$address_two = $this->_address_two; |
| 147 |
if ($address_two != '') |
| 148 |
{ |
| 149 |
if ($return != '') { $return .= $separator; } |
| 150 |
$return .= $address_two; |
| 151 |
} |
| 152 |
$address_three = $this->_address_three; |
| 153 |
if ($address_three != '') |
| 154 |
{ |
| 155 |
if ($return != '') { $return .= $separator; } |
| 156 |
$return .= $address_three; |
| 157 |
} |
| 158 |
$address_four = $this->_address_four; |
| 159 |
if ($address_four != '') |
| 160 |
{ |
| 161 |
if ($return != '') { $return .= $separator; } |
| 162 |
$return .= $address_four; |
| 163 |
} |
| 164 |
$address_postcode = $this->_address_postcode; |
| 165 |
if ($address_postcode != '') |
| 166 |
{ |
| 167 |
if ($return != '') { $return .= $separator; } |
| 168 |
$return .= $address_postcode; |
| 169 |
} |
| 170 |
|
| 171 |
return $return; |
| 172 |
} |
| 173 |
} |
| 174 |
|