| 1 |
<?php |
| 2 |
/** |
| 3 |
* Client Display Helper |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Helpers |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace EasyInvoice\Helpers; |
| 11 |
|
| 12 |
/** |
| 13 |
* Helper class for displaying client information with fallback logic |
| 14 |
*/ |
| 15 |
class ClientDisplayHelper { |
| 16 |
|
| 17 |
/** |
| 18 |
* Get display name for client with fallback logic |
| 19 |
* |
| 20 |
* Priority: business_client_name > first_name + last_name > display_name |
| 21 |
* |
| 22 |
* @param int $client_id WordPress user ID |
| 23 |
* @return string Client display name |
| 24 |
*/ |
| 25 |
public static function getClientDisplayName(int $client_id): string { |
| 26 |
if ($client_id <= 0) { |
| 27 |
return ''; |
| 28 |
} |
| 29 |
|
| 30 |
$user = get_user_by('id', $client_id); |
| 31 |
if (!$user) { |
| 32 |
return ''; |
| 33 |
} |
| 34 |
|
| 35 |
// First priority: Business/Client Name |
| 36 |
$business_name = get_user_meta($client_id, 'easy_invoice_business_client_name', true); |
| 37 |
if (!empty($business_name)) { |
| 38 |
return $business_name; |
| 39 |
} |
| 40 |
|
| 41 |
// Second priority: First Name + Last Name |
| 42 |
$first_name = get_user_meta($client_id, 'first_name', true); |
| 43 |
$last_name = get_user_meta($client_id, 'last_name', true); |
| 44 |
|
| 45 |
if (!empty($first_name) || !empty($last_name)) { |
| 46 |
$full_name = trim($first_name . ' ' . $last_name); |
| 47 |
if (!empty($full_name)) { |
| 48 |
return $full_name; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// Third priority: WordPress display name |
| 53 |
if (!empty($user->display_name)) { |
| 54 |
return $user->display_name; |
| 55 |
} |
| 56 |
|
| 57 |
// Last resort: Username |
| 58 |
return $user->user_login; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get client display name for invoice or quote |
| 63 |
* |
| 64 |
* @param \EasyInvoice\Models\Invoice|\EasyInvoice\Models\Quote $document |
| 65 |
* @return string Client display name |
| 66 |
*/ |
| 67 |
public static function getDocumentClientDisplayName($document): string { |
| 68 |
if (!$document) { |
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
// Try to get customer name directly from document |
| 73 |
if (method_exists($document, 'getCustomerName')) { |
| 74 |
$customer_name = $document->getCustomerName(); |
| 75 |
if (!empty($customer_name)) { |
| 76 |
return $customer_name; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Fallback to client ID lookup |
| 81 |
if (method_exists($document, 'getClientId')) { |
| 82 |
$client_id = $document->getClientId(); |
| 83 |
if ($client_id > 0) { |
| 84 |
return self::getClientDisplayName($client_id); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return ''; |
| 89 |
} |
| 90 |
} |
| 91 |
|