PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 All 260 releases
propertyhive / includes / class-ph-sale.php

class-ph-sale.php in Property Hive 2.3.0, at includes/class-ph-sale.php

263 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 /**
6 * Sale
7 *
8 * The Property Hive sale class handles sale data.
9 *
10 * @class PH_Sale
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_Sale; preserving the existing PH_* class name is required for plugin and extension compatibility.
17 class PH_Sale {
18
19 /** @public int Sale (post) ID */
20 public $id;
21
22 /**
23 * Get the sale if ID is passed, otherwise the sale is new and empty.
24 *
25 * @access public
26 * @param string $id (default: '')
27 * @return void
28 */
29 public function __construct( $id = '' ) {
30 if ( $id > 0 ) {
31 $this->get_sale( $id );
32 }
33 }
34
35 /**
36 * Gets a sale from the database.
37 *
38 * @access public
39 * @param int $id (default: 0)
40 * @return bool
41 */
42 public function get_sale( $id = 0 ) {
43 if ( ! $id ) {
44 return false;
45 }
46 if ( $result = get_post( $id ) ) {
47 $this->populate( $result );
48 return true;
49 }
50 return false;
51 }
52
53 /**
54 * __isset function.
55 *
56 * @access public
57 * @param mixed $key
58 * @return bool
59 */
60 public function __isset( $key ) {
61 if ( ! $this->id ) {
62 return false;
63 }
64 return metadata_exists( 'post', $this->id, '_' . $key );
65 }
66
67 /**
68 * __get function.
69 *
70 * @access public
71 * @param mixed $key
72 * @return mixed
73 */
74 public function __get( $key ) {
75 // Get values or default if not set
76 $value = get_post_meta( $this->id, $key, true );
77 if ($value == '')
78 {
79 $value = get_post_meta( $this->id, '_' . $key, true );
80 }
81 return $value;
82 }
83
84 /**
85 * Populates a sale from the loaded post data.
86 *
87 * @access public
88 * @param mixed $result
89 * @return void
90 */
91 public function populate( $result ) {
92 // Standard post data
93 $this->id = $result->ID;
94 $this->post_title = $result->post_title;
95 $this->post_status = $result->post_status;
96 }
97
98 /**
99 * Get the formatted sale amount
100 *
101 * @access public
102 * @return string
103 */
104 public function get_formatted_amount( ) {
105
106 $amount = $this->_amount;
107 $prefix = '&pound;';
108 return ( ( $amount != '' ) ? $prefix . ph_display_price_field($amount) : '-' );
109
110 }
111
112 /**
113 * Get a list of applicants on the sale
114 *
115 * @access public
116 * @param bool $add_hyperlinks
117 * @param bool $additional_contact_details
118 * @param bool $contact_details_visible
119 * @return array
120 */
121 public function get_applicants( $add_hyperlinks = false, $additional_contact_details = false, $contact_details_visible = true )
122 {
123 $applicant_contact_ids = get_post_meta( $this->id, '_applicant_contact_id' );
124 if ( is_array($applicant_contact_ids) && !empty($applicant_contact_ids) )
125 {
126 $applicants = array();
127 foreach ( $applicant_contact_ids as $applicant_contact_id )
128 {
129 $applicant_name = esc_html( get_the_title( $applicant_contact_id ) );
130 if ( $add_hyperlinks )
131 {
132 $edit_link = get_edit_post_link( $applicant_contact_id );
133 $applicant_name = '<a href="' . esc_url( $edit_link ) . '">' . $applicant_name . '</a>';
134 }
135 if ( $additional_contact_details )
136 {
137 $contact_details = array();
138 $telephone_number = get_post_meta( $applicant_contact_id, '_telephone_number', true );
139 if( !empty($telephone_number) )
140 {
141 $contact_details[] = 'T: ' . esc_html( $telephone_number );
142 }
143
144 $email_address = get_post_meta( $applicant_contact_id, '_email_address', true );
145 if( !empty($email_address) )
146 {
147 $contact_details[] = 'E: ' . esc_html( $email_address );
148 }
149
150 $contact_details = apply_filters( 'propertyhive_sale_applicant_contact_details', $contact_details, $applicant_contact_id );
151
152 if ( !empty($contact_details) )
153 {
154 $applicant_name .= '<div class="row-actions' . ($contact_details_visible ? ' visible' : '') . '">' . implode( "<br>", $contact_details ) . '</div>';
155 }
156 }
157 $applicants[] = $applicant_name;
158 }
159 return implode("<br>", $applicants);
160 }
161 else
162 {
163 return '-';
164 }
165 }
166
167 /**
168 * Get the full address of the property attached to the sale
169 *
170 * @access public
171 * @return string
172 */
173 public function get_property_address()
174 {
175 $property_id = (int)$this->_property_id;
176
177 if ( !empty($property_id) )
178 {
179 $property = new PH_Property( $property_id );
180 return '<a href="' . esc_url( get_edit_post_link( $property_id, '' ) ) . '" target="' . esc_attr( apply_filters('propertyhive_subgrid_link_target', '') ) . '">' . esc_html( $property->get_formatted_full_address() ) . '</a>';
181 }
182 else
183 {
184 return '-';
185 }
186 }
187
188 /**
189 * Get the contact details of the owner(s) of the property attached to the sale
190 *
191 * @access public
192 * @return string
193 */
194 public function get_property_owners()
195 {
196 $property_owners = '-';
197
198 $property_id = (int)$this->_property_id;
199
200 if ( !empty($property_id) )
201 {
202 $property = new PH_Property( $property_id );
203 $owner_contact_ids = $property->_owner_contact_id;
204 if (
205 ( !is_array($owner_contact_ids) && $owner_contact_ids != '' && $owner_contact_ids != 0 )
206 ||
207 ( is_array($owner_contact_ids) && !empty($owner_contact_ids) )
208 )
209 {
210 $property_owners = '';
211
212 if ( !is_array($owner_contact_ids) )
213 {
214 $owner_contact_ids = array($owner_contact_ids);
215 }
216
217 foreach ( $owner_contact_ids as $owner_contact_id )
218 {
219 $property_owners .= $this->formatted_contact_meta_box_data($owner_contact_id, false);
220 }
221 }
222 }
223
224 return $property_owners;
225 }
226
227 /**
228 * Format contact data for display in a meta box list
229 */
230 private function formatted_contact_meta_box_data( $contact_post_id, $add_edit_link = true )
231 {
232 if ( $add_edit_link )
233 {
234 $contact_text = '<a href="' . esc_url( get_edit_post_link( $contact_post_id, '' ) ) . '" target="' . esc_attr( apply_filters('propertyhive_subgrid_link_target', '') ) . '">' . esc_html( get_the_title($contact_post_id) ) . '</a>';
235 }
236 else
237 {
238 $contact_text = esc_html( get_the_title($contact_post_id) );
239 }
240
241 $contact_details = '';
242 $telephone_number = get_post_meta( $contact_post_id, '_telephone_number', true );
243 if( !empty($telephone_number) )
244 {
245 $contact_details = 'T: ' . esc_html($telephone_number);
246 }
247
248 $email_address = get_post_meta( $contact_post_id, '_email_address', true );
249 if( !empty($email_address) )
250 {
251 $contact_details .= ( $contact_details != '' ) ? '<br>' : '';
252 $contact_details .= 'E: ' . esc_html($email_address);
253 }
254
255 if ( $contact_details != '' )
256 {
257 $contact_text .= '<div class="row-actions visible">' . $contact_details . '</div>';
258 }
259
260 return $contact_text;
261 }
262 }
263