PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / Addons / RequestAQuote / Models / QuoteRequest.php

QuoteRequest.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/RequestAQuote/Models/QuoteRequest.php

315 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\RequestAQuote\Models;
2
3 use TierPricingTable\Addons\RequestAQuote\CPT\QuoteRequestCPT;
4
5 class QuoteRequest {
6
7 protected int $id = 0;
8
9 protected array $data = array(
10 'status' => 'unread',
11 'title' => '',
12 'content' => '',
13 'date' => '',
14 'productId' => 0,
15 'quantity' => 1,
16 'price' => '',
17 'userId' => 0,
18 'customerEmail' => '',
19 'customFields' => array(),
20 'convertedOrderId' => 0,
21 );
22
23 protected array $metaData = array();
24
25 protected array $changes = array();
26
27 public function __construct( $quote = 0 ) {
28 if ( is_numeric( $quote ) && $quote > 0 ) {
29 $this->id = absint( $quote );
30 $this->read();
31 } elseif ( $quote instanceof \WP_Post ) {
32 $this->id = absint( $quote->ID );
33 $this->read();
34 } elseif ( is_array( $quote ) ) {
35 $this->setProps( $quote );
36 }
37 }
38
39 public function getId() {
40 return $this->id;
41 }
42
43 public function setId( $id ) {
44 $this->id = absint( $id );
45 }
46
47 public function read() {
48 if ( ! $this->getId() ) {
49 return;
50 }
51
52 $post = get_post( $this->getId() );
53 if ( ! $post || $post->post_type !== QuoteRequestCPT::POST_TYPE ) {
54 $this->setId( 0 );
55
56 return;
57 }
58
59 $this->setProps( array(
60 'status' => $post->post_status,
61 'title' => $post->post_title,
62 'content' => $post->post_content,
63 'date' => $post->post_date,
64 ) );
65
66 $meta = get_post_meta( $this->getId() );
67 if ( $meta && is_array( $meta ) ) {
68 foreach ( $meta as $key => $value ) {
69 $val = $value[0];
70 if ( is_string( $val ) && is_serialized( $val ) ) {
71 $val = maybe_unserialize( $val );
72 }
73 $this->metaData[ $key ] = $val;
74 }
75 }
76
77 $customFields = $this->metaData['_custom_fields'] ?? array();
78 if ( ! is_array( $customFields ) ) {
79 $customFields = array();
80 }
81
82 // Hydrate known properties
83 $this->setProps( array(
84 'productId' => $this->metaData['_product_id'] ?? 0,
85 'quantity' => $this->metaData['_quantity'] ?? 1,
86 'price' => $this->metaData['_price'] ?? '',
87 'userId' => $this->metaData['_user_id'] ?? 0,
88 'customerEmail' => $this->metaData['_email'] ?? '',
89 'customFields' => $customFields,
90 'convertedOrderId' => $this->metaData['_converted_order_id'] ?? 0,
91 ) );
92
93 $this->applyChanges();
94 }
95
96 public function save() {
97 $post_data = array();
98
99 if ( array_key_exists( 'status', $this->changes ) ) {
100 $post_data['post_status'] = $this->changes['status'];
101 }
102 if ( array_key_exists( 'title', $this->changes ) ) {
103 $post_data['post_title'] = $this->changes['title'];
104 }
105 if ( array_key_exists( 'content', $this->changes ) ) {
106 $post_data['post_content'] = $this->changes['content'];
107 }
108 if ( array_key_exists( 'date', $this->changes ) ) {
109 $post_data['post_date'] = $this->changes['date'];
110 }
111
112 if ( ! $this->getId() ) {
113 $post_data['post_type'] = QuoteRequestCPT::POST_TYPE;
114 if ( empty( $post_data['post_title'] ) ) {
115 $post_data['post_title'] = __( 'Quote Request', 'tier-pricing-table' );
116 }
117 if ( empty( $post_data['post_status'] ) ) {
118 $post_data['post_status'] = 'unread';
119 }
120
121 $post_id = wp_insert_post( $post_data );
122 if ( ! is_wp_error( $post_id ) ) {
123 $this->setId( $post_id );
124 } else {
125 return false;
126 }
127 } elseif ( ! empty( $post_data ) ) {
128 $post_data['ID'] = $this->getId();
129 wp_update_post( $post_data );
130 }
131
132 // Save mapped properties to meta
133 $meta_mapping = array(
134 'productId' => '_product_id',
135 'quantity' => '_quantity',
136 'price' => '_price',
137 'userId' => '_user_id',
138 'customerEmail' => '_email',
139 'customFields' => '_custom_fields',
140 'convertedOrderId' => '_converted_order_id',
141 );
142
143 foreach ( $meta_mapping as $prop => $meta_key ) {
144 if ( array_key_exists( $prop, $this->changes ) ) {
145 update_post_meta( $this->getId(), $meta_key, $this->changes[ $prop ] );
146 }
147 }
148
149 // Save direct meta changes
150 foreach ( $this->metaData as $key => $value ) {
151 update_post_meta( $this->getId(), $key, $value );
152 }
153
154 $this->applyChanges();
155
156 return $this->getId();
157 }
158
159 public function getProp( $prop, $context = 'view' ) {
160 return array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : ( $this->data[ $prop ] ?? null );
161 }
162
163 public function setProp( $prop, $value ) {
164 if ( array_key_exists( $prop, $this->data ) && $this->data[ $prop ] !== $value ) {
165 $this->changes[ $prop ] = $value;
166 }
167 }
168
169 public function setProps( $props ) {
170 foreach ( $props as $prop => $value ) {
171 $this->setProp( $prop, $value );
172 }
173 }
174
175 public function getStatus() {
176 return $this->getProp( 'status' );
177 }
178
179 public function setStatus( $value ) {
180 $this->setProp( 'status', $value );
181 }
182
183 public function getTitle() {
184 return $this->getProp( 'title' );
185 }
186
187 public function setTitle( $value ) {
188 $this->setProp( 'title', $value );
189 }
190
191 public function getContent() {
192 return $this->getProp( 'content' );
193 }
194
195 public function setContent( $value ) {
196 $this->setProp( 'content', $value );
197 }
198
199 public function getDate() {
200 return $this->getProp( 'date' );
201 }
202
203 public function setDate( $value ) {
204 $this->setProp( 'date', $value );
205 }
206
207 public function getProductId(): int {
208 return (int) $this->getProp( 'productId' );
209 }
210
211 public function setProductId( $value ) {
212 $this->setProp( 'productId', (int) $value );
213 }
214
215 public function getQuantity(): int {
216 return (int) $this->getProp( 'quantity' );
217 }
218
219 public function setQuantity( $value ) {
220 $this->setProp( 'quantity', (int) $value );
221 }
222
223 public function getPrice() {
224 return $this->getProp( 'price' );
225 }
226
227 public function setPrice( $value ) {
228 $this->setProp( 'price', $value );
229 }
230
231 public function getUserId(): int {
232 return (int) $this->getProp( 'userId' );
233 }
234
235 public function setUserId( $value ) {
236 $this->setProp( 'userId', (int) $value );
237 }
238
239 public function applyChanges() {
240 if ( ! empty( $this->changes ) ) {
241 $this->data = array_merge( $this->data, $this->changes );
242 $this->changes = array();
243 }
244 }
245
246 public function getCustomerEmail() {
247 return $this->getProp( 'customerEmail' );
248 }
249
250 public function setCustomerEmail( $value ) {
251 $this->setProp( 'customerEmail', $value );
252 }
253
254 public function getCustomFields(): array {
255 return (array) $this->getProp( 'customFields' );
256 }
257
258 public function setCustomFields( $value ) {
259 $this->setProp( 'customFields', (array) $value );
260 }
261
262 public function getConvertedOrderId(): int {
263 return (int) $this->getProp( 'convertedOrderId' );
264 }
265
266 public function setConvertedOrderId( $value ) {
267 $this->setProp( 'convertedOrderId', (int) $value );
268 }
269
270 public function getMeta( $key ) {
271 return $this->metaData[ $key ] ?? '';
272 }
273
274 public function addMetaData( $key, $value, $unique = false ) {
275 if ( $unique && isset( $this->metaData[ $key ] ) ) {
276 return;
277 }
278 $this->metaData[ $key ] = $value;
279 }
280
281 public function updateMetaData( $key, $value ) {
282 $this->metaData[ $key ] = $value;
283 }
284
285 public function deleteMetaData( $key ) {
286 unset( $this->metaData[ $key ] );
287 if ( $this->getId() ) {
288 delete_post_meta( $this->getId(), $key );
289 }
290 }
291
292 public function getMetaData(): array {
293 return $this->metaData;
294 }
295
296 public function getCustomerName() {
297 $fields = $this->getCustomFields();
298
299 // Helper to extract value if it's an array
300 $getValue = function($field) {
301 if ( is_array( $field ) && isset( $field['value'] ) ) {
302 return $field['value'];
303 }
304 return $field;
305 };
306
307 if ( isset( $fields['name'] ) ) {
308 return $getValue( $fields['name'] );
309 }
310 if ( isset( $fields['first_name'] ) ) {
311 return $getValue( $fields['first_name'] );
312 }
313 return $this->getMeta( '_name' ) ?: $this->getMeta( '_first_name' );
314 }
315 }