| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\PaymentGateways; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
|
| 7 |
/** |
| 8 |
* @since 2.19.0 |
| 9 |
*/ |
| 10 |
class DonationSummary |
| 11 |
{ |
| 12 |
/** @var int */ |
| 13 |
protected $length = 255; |
| 14 |
|
| 15 |
/** @var Donation */ |
| 16 |
protected $donation; |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 2.19.0 |
| 20 |
*/ |
| 21 |
public function __construct(Donation $donation) |
| 22 |
{ |
| 23 |
$this->donation = $donation; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @since 2.19.0 |
| 28 |
* |
| 29 |
* @param int $length |
| 30 |
*/ |
| 31 |
public function setLength(int $length) |
| 32 |
{ |
| 33 |
$this->length = $length; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @since 2.19.0 |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function getSummaryWithDonor(): string |
| 42 |
{ |
| 43 |
return $this->trim( |
| 44 |
implode(' - ', [ |
| 45 |
$this->getSummary(), |
| 46 |
$this->getDonorLabel(), |
| 47 |
]) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @since 2.19.0 |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function getSummary(): string |
| 57 |
{ |
| 58 |
return $this->trimAndFilter( |
| 59 |
implode( |
| 60 |
': ', |
| 61 |
array_filter([ |
| 62 |
$this->getLabel(), |
| 63 |
$this->getPriceLabel(), |
| 64 |
]) |
| 65 |
) |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @since 2.19.0 |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
protected function getLabel(): string |
| 75 |
{ |
| 76 |
$formId = give_get_payment_form_id($this->donation->id); |
| 77 |
$formTitle = get_the_title($formId); |
| 78 |
return $formTitle ?: sprintf(__('Donation Form ID: %d', 'give'), $formId); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @since 2.19.0 |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
protected function getPriceLabel(): string |
| 86 |
{ |
| 87 |
$priceId = $this->donation->levelId; |
| 88 |
|
| 89 |
return is_numeric($priceId) |
| 90 |
? give_get_price_option_name($this->donation->formId, $this->donation->levelId) |
| 91 |
: ''; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @since 2.19.0 |
| 96 |
*/ |
| 97 |
protected function getDonorLabel(): string |
| 98 |
{ |
| 99 |
return sprintf( |
| 100 |
'%s %s (%s)', |
| 101 |
$this->donation->firstName, |
| 102 |
$this->donation->lastName, |
| 103 |
$this->donation->email |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @since 2.19.0 |
| 109 |
* |
| 110 |
* @param string $text |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
protected function trimAndFilter(string $text): string |
| 115 |
{ |
| 116 |
/** |
| 117 |
* @since 2.25.0 Re-use trim method for text. |
| 118 |
* @since 1.8.12 |
| 119 |
*/ |
| 120 |
return apply_filters('give_payment_gateway_donation_summary', $this->trim($text)); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @since 2.25.0 |
| 125 |
* |
| 126 |
* @param string $text |
| 127 |
* |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
protected function trim(string $text): string |
| 131 |
{ |
| 132 |
return substr($text, 0, $this->length); |
| 133 |
} |
| 134 |
} |
| 135 |
|