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
121 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 | * @param GatewayPaymentData $paymentData |
| 21 | */ |
| 22 | public function __construct( GatewayPaymentData $paymentData ) |
| 23 | { |
| 24 | $this->paymentData = $paymentData; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @since 2.19.0 |
| 29 | * @param $length |
| 30 | */ |
| 31 | public function setLength( $length ) |
| 32 | { |
| 33 | $this->length = $length; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @since 2.19.0 |
| 38 | * @return string |
| 39 | */ |
| 40 | public function getSummaryWithDonor() |
| 41 | { |
| 42 | return $this->trimAndFilter(implode(' - ', [ |
| 43 | $this->getSummary(), |
| 44 | $this->getDonorLabel(), |
| 45 | ])); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @since 2.19.0 |
| 50 | * @return string |
| 51 | */ |
| 52 | public function getSummary() |
| 53 | { |
| 54 | return $this->trimAndFilter(implode( ': ', array_filter([ |
| 55 | $this->getLabel(), |
| 56 | $this->getPriceLabel(), |
| 57 | ]))); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @since 2.19.0 |
| 62 | * @param $property |
| 63 | * @return mixed|void |
| 64 | */ |
| 65 | protected function get( $property ) |
| 66 | { |
| 67 | if( property_exists( $this->paymentData, $property ) ) { |
| 68 | return $this->paymentData->$property; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @since 2.19.0 |
| 74 | * @return string |
| 75 | */ |
| 76 | protected function getLabel() |
| 77 | { |
| 78 | $formId = give_get_payment_form_id( $this->get( 'donationId' ) ); |
| 79 | return wp_sprintf( __( 'Donation Form ID: %d', 'give' ), $formId ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @since 2.19.0 |
| 84 | * @return string |
| 85 | */ |
| 86 | protected function getPriceLabel() |
| 87 | { |
| 88 | $formId = give_get_payment_form_id( $this->get( 'donationId' ) ); |
| 89 | return $this->get( 'priceId' ) |
| 90 | ? give_get_price_option_name( $formId, $this->get( 'priceId' ) ) |
| 91 | : ''; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @since 2.19.0 |
| 96 | * @return string |
| 97 | */ |
| 98 | protected function getDonorLabel() |
| 99 | { |
| 100 | return sprintf( |
| 101 | '%s %s (%s)', |
| 102 | $this->get( 'donorInfo' )->firstName, |
| 103 | $this->get( 'donorInfo' )->lastName, |
| 104 | $this->get( 'donorInfo' )->email |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @since 2.19.0 |
| 110 | * @param string $text |
| 111 | * @return string |
| 112 | */ |
| 113 | protected function trimAndFilter( $text ) |
| 114 | { |
| 115 | /** |
| 116 | * @since 1.8.12 |
| 117 | */ |
| 118 | return apply_filters( 'give_payment_gateway_donation_summary', substr( $text, 0, $this->length ) ); |
| 119 | } |
| 120 | } |
| 121 |