PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.2.0
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.2.0
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Repositories / ClientRepository.php

ClientRepository.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.2.0, at includes/Repositories/ClientRepository.php

452 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Client Repository Class
4 *
5 * @package Easy_Invoice
6 * @subpackage Repositories
7 */
8
9 namespace EasyInvoice\Repositories;
10
11 use EasyInvoice\Constants\ClientFields;
12 use EasyInvoice\Interfaces\ClientRepositoryInterface;
13 use EasyInvoice\Models\Client;
14 use WP_User;
15 use WP_User_Query;
16 use WP_Query;
17
18 /**
19 * ClientRepository Class
20 *
21 * Handles data access for client objects using WordPress users.
22 */
23 class ClientRepository implements ClientRepositoryInterface {
24
25 /**
26 * Find a client by ID
27 *
28 * @param int $id The client ID
29 * @return Client|null The client model or null if not found
30 */
31 public function find($id) {
32 // Regular client (WordPress user)
33 $user = get_user_by('id', $id);
34
35 if (!$user) {
36 return null;
37 }
38
39 return $this->createClientFromUser($user);
40 }
41
42 /**
43 * Get all clients
44 *
45 * @param array $args Optional arguments to filter the results
46 * @return array Array of Client models
47 */
48 public function all($args = []) {
49 $clients = [];
50
51 // Get all users to include administrators and other users
52 $user_query = new WP_User_Query([
53 'number' => -1,
54 'orderby' => 'ID',
55 'order' => 'DESC',
56 'role__not_in' => ['Administrator'], // exclude admins
57 ]);
58
59 foreach ($user_query->get_results() as $user) {
60 $client = $this->createClientFromUser($user);
61 if ($client) {
62 $clients[] = $client;
63 }
64 }
65
66 return $clients;
67 }
68
69 /**
70 * Create a new client
71 *
72 * @param array $data The client data
73 * @return Client The created client model
74 */
75 public function create($data) {
76 // Create regular client as WordPress user
77 return $this->createRegularClient($data);
78 }
79
80 /**
81 * Create a regular client (WordPress user)
82 *
83 * @param array $data The client data
84 * @return Client|null The created client model
85 */
86 protected function createRegularClient($data) {
87 // Create a new user if email is provided
88 if (empty($data[ClientFields::EMAIL])) {
89 return null;
90 }
91
92 $user_data = [
93 'user_email' => $data[ClientFields::EMAIL],
94 'user_login' => $data[ClientFields::USERNAME],
95 'user_pass' => !empty($data[ClientFields::PASSWORD]) ? $data[ClientFields::PASSWORD] : wp_generate_password(),
96 'display_name' => $data[ClientFields::FIRST_NAME] . ' ' . $data[ClientFields::LAST_NAME],
97 'first_name' => $data[ClientFields::FIRST_NAME],
98 'last_name' => $data[ClientFields::LAST_NAME],
99 'role' => 'customer',
100 ];
101
102 $user_id = wp_insert_user($user_data);
103
104 if (is_wp_error($user_id)) {
105 return null;
106 }
107
108 // Create client model
109 $client = new Client($user_id);
110
111 // Set the client data
112 $this->setClientData($client, $data);
113
114 return $client;
115 }
116
117 /**
118 * Update an existing client
119 *
120 * @param int $id The client ID
121 * @param array $data The client data
122 * @return Client|null The updated client model or null if not found
123 */
124 public function update($id, $data) {
125 $client = $this->find($id);
126
127 if (!$client) {
128 return null;
129 }
130
131 // Regular client - update user data if email is provided
132 $user_data = ['ID' => $id];
133 if (!empty($data[ClientFields::EMAIL])) {
134 $user_data['user_email'] = $data[ClientFields::EMAIL];
135 }
136 if (!empty($data[ClientFields::USERNAME])) {
137 $user_data['user_login'] = $data[ClientFields::USERNAME];
138 }
139 if (!empty($data[ClientFields::PASSWORD])) {
140 $user_data['user_pass'] = $data[ClientFields::PASSWORD];
141 }
142 if (!empty($data[ClientFields::FIRST_NAME]) && !empty($data[ClientFields::LAST_NAME])) {
143 $user_data['display_name'] = $data[ClientFields::FIRST_NAME] . ' ' . $data[ClientFields::LAST_NAME];
144 }
145 if (!empty($data[ClientFields::FIRST_NAME])) {
146 $user_data['first_name'] = $data[ClientFields::FIRST_NAME];
147 }
148 if (!empty($data[ClientFields::LAST_NAME])) {
149 $user_data['last_name'] = $data[ClientFields::LAST_NAME];
150 }
151
152 // Only update user data if we have more than just the ID
153 if(count($user_data) > 1) {
154 $result = wp_update_user($user_data);
155 if (is_wp_error($result)) {
156 // Log the error but continue with client data update
157 }
158 }
159
160 // Set the client data
161 $this->setClientData($client, $data);
162
163 return $client;
164 }
165
166 /**
167 * Delete a client
168 *
169 * @param int $id The client ID
170 * @return bool True if successful, false otherwise
171 */
172 public function delete($id) {
173 // Check if client exists
174 $client = $this->find($id);
175
176 if (!$client) {
177 return false;
178 }
179
180 // Check if user exists and is not an administrator
181 $user = get_user_by('ID', $id);
182 if (!$user || in_array('administrator', $user->roles)) {
183 return false;
184 }
185
186 // Delete all invoices and quotes associated with this client
187 global $wpdb;
188
189 // Get all invoices and quotes for this client
190 $posts = $wpdb->get_results($wpdb->prepare(
191 "SELECT ID, post_type FROM {$wpdb->posts} WHERE post_type IN ('easy_invoice', 'easy_invoice_quote') AND ID IN (
192 SELECT post_id FROM {$wpdb->postmeta}
193 WHERE (meta_key = '_easy_invoice_client_id' OR meta_key = '_easy_invoice_quote_client_id')
194 AND meta_value = %d
195 )",
196 $id
197 ));
198
199 // Delete each post and its meta
200 foreach ($posts as $post) {
201 wp_delete_post($post->ID, true);
202 }
203
204 // Delete all payments associated with this client
205 $payments = $wpdb->get_col($wpdb->prepare(
206 "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'easy_payment' AND ID IN (
207 SELECT post_id FROM {$wpdb->postmeta}
208 WHERE meta_key = '_easy_payment_client_id' AND meta_value = %d
209 )",
210 $id
211 ));
212
213 foreach ($payments as $payment_id) {
214 wp_delete_post($payment_id, true);
215 }
216
217 // Delete the WordPress user (this will also delete all user meta)
218 $result = wp_delete_user($id);
219
220 // Return the result
221 return $result;
222
223 return $result;
224 }
225
226 /**
227 * Find clients by email
228 *
229 * @param string $email The client email
230 * @return array Array of Client models
231 */
232 public function findByEmail($email) {
233 $clients = [];
234
235 // Find regular clients by email
236 $user = get_user_by('email', $email);
237
238 if ($user) {
239 $clients[] = $this->createClientFromUser($user);
240 }
241
242 return $clients;
243 }
244
245 /**
246 * Find clients by business/client name
247 *
248 * @param string $business_client_name The business/client name
249 * @return array Array of Client models
250 */
251 public function findByBusinessClientName($business_client_name) {
252 $args = [
253 'meta_query' => [
254 [
255 'key' => ClientFields::BUSINESS_CLIENT_NAME,
256 'value' => $business_client_name,
257 'compare' => 'LIKE',
258 ],
259 ],
260 ];
261
262 return $this->all($args);
263 }
264
265 /**
266 * Search clients by name, email, or company
267 *
268 * @param string $query The search query
269 * @return array Array of Client models
270 */
271 public function search($query) {
272 $clients = [];
273 $query = trim($query);
274
275 if (empty($query)) {
276 return $this->all();
277 }
278
279 // Search by display name, first name, last name, or email
280 $name_args = [
281 'search' => '*' . $query . '*',
282 'search_columns' => ['display_name', 'first_name', 'last_name', 'user_email'],
283 'meta_query' => [
284 'relation' => 'OR',
285 [
286 'key' => ClientFields::BUSINESS_CLIENT_NAME,
287 'compare' => 'EXISTS',
288 ],
289 [
290 'key' => ClientFields::EMAIL,
291 'compare' => 'EXISTS',
292 ],
293 ],
294 ];
295
296 $name_query = new WP_User_Query($name_args);
297 foreach ($name_query->get_results() as $user) {
298 $clients[] = $this->createClientFromUser($user);
299 }
300
301 // Search by business client name (meta field)
302 $business_args = [
303 'meta_query' => [
304 'relation' => 'AND',
305 [
306 'key' => ClientFields::BUSINESS_CLIENT_NAME,
307 'value' => $query,
308 'compare' => 'LIKE',
309 ],
310 [
311 'relation' => 'OR',
312 [
313 'key' => ClientFields::BUSINESS_CLIENT_NAME,
314 'compare' => 'EXISTS',
315 ],
316 [
317 'key' => ClientFields::EMAIL,
318 'compare' => 'EXISTS',
319 ],
320 ],
321 ],
322 ];
323
324 $business_query = new WP_User_Query($business_args);
325 foreach ($business_query->get_results() as $user) {
326 // Check if this client is already in the results
327 $exists = false;
328 foreach ($clients as $existing_client) {
329 if ($existing_client->getId() === $user->ID) {
330 $exists = true;
331 break;
332 }
333 }
334
335 if (!$exists) {
336 $clients[] = $this->createClientFromUser($user);
337 }
338 }
339
340 // Search by email (meta field)
341 $email_args = [
342 'meta_query' => [
343 'relation' => 'AND',
344 [
345 'key' => ClientFields::EMAIL,
346 'value' => $query,
347 'compare' => 'LIKE',
348 ],
349 [
350 'relation' => 'OR',
351 [
352 'key' => ClientFields::BUSINESS_CLIENT_NAME,
353 'compare' => 'EXISTS',
354 ],
355 [
356 'key' => ClientFields::EMAIL,
357 'compare' => 'EXISTS',
358 ],
359 ],
360 ],
361 ];
362
363 $email_query = new WP_User_Query($email_args);
364 foreach ($email_query->get_results() as $user) {
365 // Check if this client is already in the results
366 $exists = false;
367 foreach ($clients as $existing_client) {
368 if ($existing_client->getId() === $user->ID) {
369 $exists = true;
370 break;
371 }
372 }
373
374 if (!$exists) {
375 $clients[] = $this->createClientFromUser($user);
376 }
377 }
378
379 // Limit results to 10 to avoid performance issues
380 return array_slice($clients, 0, 10);
381 }
382
383 /**
384 * Create a client model from a WordPress user
385 *
386 * @param WP_User $user The WordPress user
387 * @return Client The client model
388 */
389 protected function createClientFromUser(WP_User $user) {
390 try {
391 $client = new Client($user->ID);
392 return $client;
393 } catch (\Exception $e) {
394 return null;
395 }
396 }
397
398 /**
399 * Set the client data
400 *
401 * @param Client $client The client model
402 * @param array $data The client data
403 */
404 protected function setClientData(Client $client, array $data) {
405 if (isset($data[ClientFields::BUSINESS_CLIENT_NAME])) {
406 $client->business_client_name = $data[ClientFields::BUSINESS_CLIENT_NAME];
407 update_user_meta($client->getId(), ClientFields::BUSINESS_CLIENT_NAME, $data[ClientFields::BUSINESS_CLIENT_NAME]);
408 }
409
410 if (isset($data[ClientFields::EMAIL])) {
411 $client->email = $data[ClientFields::EMAIL];
412 update_user_meta($client->getId(), ClientFields::EMAIL, $data[ClientFields::EMAIL]);
413 }
414
415 if (isset($data[ClientFields::USERNAME])) {
416 $client->username = $data[ClientFields::USERNAME];
417 update_user_meta($client->getId(), ClientFields::USERNAME, $data[ClientFields::USERNAME]);
418 }
419
420 if (isset($data[ClientFields::ADDRESS])) {
421 $client->address = $data[ClientFields::ADDRESS];
422 update_user_meta($client->getId(), ClientFields::ADDRESS, $data[ClientFields::ADDRESS]);
423 }
424
425 if (isset($data[ClientFields::EXTRA_INFO])) {
426 $client->extra_info = $data[ClientFields::EXTRA_INFO];
427 update_user_meta($client->getId(), ClientFields::EXTRA_INFO, $data[ClientFields::EXTRA_INFO]);
428 }
429
430 if (isset($data[ClientFields::FIRST_NAME])) {
431 $client->first_name = $data[ClientFields::FIRST_NAME];
432 update_user_meta($client->getId(), ClientFields::FIRST_NAME, $data[ClientFields::FIRST_NAME]);
433 }
434
435 if (isset($data[ClientFields::LAST_NAME])) {
436 $client->last_name = $data[ClientFields::LAST_NAME];
437 update_user_meta($client->getId(), ClientFields::LAST_NAME, $data[ClientFields::LAST_NAME]);
438 }
439
440 if (isset($data[ClientFields::WEBSITE])) {
441 $client->website = $data[ClientFields::WEBSITE];
442 update_user_meta($client->getId(), ClientFields::WEBSITE, $data[ClientFields::WEBSITE]);
443 }
444 if (isset($data[ClientFields::PHONE])) {
445 $client->phone = $data[ClientFields::PHONE];
446 update_user_meta($client->getId(), ClientFields::PHONE, $data[ClientFields::PHONE]);
447 }
448
449 // Reset the dirty flag after saving
450 $client->resetDirty();
451 }
452 }