| 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 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Legacy public global class PH_Contact; preserving the existing PH_* class name is required for plugin and extension compatibility. |
| 17 |
class PH_Contact { |
| 18 |
|
| 19 |
/** @public int Contact (post) ID */ |
| 20 |
public $id; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get the contact if ID is passed, otherwise the contact is new and empty. |
| 24 |
* |
| 25 |
* @access public |
| 26 |
* @param string $id (default: '') |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function __construct( $id = '', $user_id = '' ) { |
| 30 |
|
| 31 |
if ( $user_id > 0 ) |
| 32 |
{ |
| 33 |
// We've been passed a user ID. Need to get contact ID based on user_id |
| 34 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- PH_Contact maps one WordPress user ID to one contact with fields=ids and posts_per_page=1. Both meta key and scalar value are fixed/typed; one-row result. |
| 35 |
$contact_query = new WP_Query( array( 'post_type' => 'contact', 'meta_key' => '_user_id', 'meta_value' => $user_id, 'fields' => 'ids', 'posts_per_page' => 1 ) ); |
| 36 |
|
| 37 |
if ( $contact_query->have_posts() ) |
| 38 |
{ |
| 39 |
while ( $contact_query->have_posts() ) |
| 40 |
{ |
| 41 |
$contact_query->the_post(); |
| 42 |
|
| 43 |
$this->get_contact( get_the_ID() ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
wp_reset_postdata(); |
| 48 |
} |
| 49 |
else |
| 50 |
{ |
| 51 |
if ( $id > 0 ) { |
| 52 |
$this->get_contact( $id ); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Gets a contact from the database. |
| 59 |
* |
| 60 |
* @access public |
| 61 |
* @param int $id (default: 0) |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
public function get_contact( $id = 0 ) { |
| 65 |
if ( ! $id ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
if ( $result = get_post( $id ) ) { |
| 69 |
$this->populate( $result ); |
| 70 |
return true; |
| 71 |
} |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* __isset function. |
| 77 |
* |
| 78 |
* @access public |
| 79 |
* @param mixed $key |
| 80 |
* @return bool |
| 81 |
*/ |
| 82 |
public function __isset( $key ) { |
| 83 |
if ( ! $this->id ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
return metadata_exists( 'post', $this->id, '_' . $key ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* __get function. |
| 91 |
* |
| 92 |
* @access public |
| 93 |
* @param mixed $key |
| 94 |
* @return mixed |
| 95 |
*/ |
| 96 |
public function __get( $key ) { |
| 97 |
// Get values or default if not set |
| 98 |
$value = get_post_meta( $this->id, $key, true ); |
| 99 |
if ($value == '') |
| 100 |
{ |
| 101 |
$value = get_post_meta( $this->id, '_' . $key, true ); |
| 102 |
} |
| 103 |
return $value; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Populates a contact from the loaded post data. |
| 108 |
* |
| 109 |
* @access public |
| 110 |
* @param mixed $result |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function populate( $result ) { |
| 114 |
// Standard post data |
| 115 |
$this->id = $result->ID; |
| 116 |
$this->post_title = $result->post_title; |
| 117 |
$this->post_status = $result->post_status; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the full formatted address |
| 122 |
* |
| 123 |
* @access public |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
public function get_formatted_full_address( $separator = ', ' ) { |
| 127 |
// Standard post data |
| 128 |
|
| 129 |
$return = ''; |
| 130 |
|
| 131 |
$company_name = $this->_company_name; |
| 132 |
if ($company_name != '') |
| 133 |
{ |
| 134 |
$return .= $company_name; |
| 135 |
} |
| 136 |
$address_name_number = $this->_address_name_number; |
| 137 |
if ($address_name_number != '') |
| 138 |
{ |
| 139 |
if ($return != '') { $return .= $separator; } |
| 140 |
$return .= $address_name_number; |
| 141 |
} |
| 142 |
$address_street = $this->_address_street; |
| 143 |
if ($address_street != '') |
| 144 |
{ |
| 145 |
if ($return != '') { $return .= ' '; } |
| 146 |
$return .= $address_street; |
| 147 |
} |
| 148 |
$address_two = $this->_address_two; |
| 149 |
if ($address_two != '') |
| 150 |
{ |
| 151 |
if ($return != '') { $return .= $separator; } |
| 152 |
$return .= $address_two; |
| 153 |
} |
| 154 |
$address_three = $this->_address_three; |
| 155 |
if ($address_three != '') |
| 156 |
{ |
| 157 |
if ($return != '') { $return .= $separator; } |
| 158 |
$return .= $address_three; |
| 159 |
} |
| 160 |
$address_four = $this->_address_four; |
| 161 |
if ($address_four != '') |
| 162 |
{ |
| 163 |
if ($return != '') { $return .= $separator; } |
| 164 |
$return .= $address_four; |
| 165 |
} |
| 166 |
$address_postcode = $this->_address_postcode; |
| 167 |
if ($address_postcode != '') |
| 168 |
{ |
| 169 |
if ($return != '') { $return .= $separator; } |
| 170 |
$return .= $address_postcode; |
| 171 |
} |
| 172 |
|
| 173 |
return $return; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Get the dear field if populated, fallback to the full name if not |
| 178 |
* |
| 179 |
* @access public |
| 180 |
* @return string |
| 181 |
*/ |
| 182 |
public function dear() |
| 183 |
{ |
| 184 |
$dear = $this->_dear; |
| 185 |
return !empty($dear) ? $dear : $this->post_title; |
| 186 |
} |
| 187 |
} |
| 188 |
|