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