| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Form\LegacyConsumer\Commands; |
| 4 |
|
| 5 |
use Give\Framework\FieldsAPI\Field; |
| 6 |
use Give\Framework\FieldsAPI\Group; |
| 7 |
use Give\Receipt\DonationReceipt; |
| 8 |
|
| 9 |
/** |
| 10 |
* @since 2.10.2 |
| 11 |
*/ |
| 12 |
class SetupFieldReceipt |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var DonationReceipt |
| 16 |
*/ |
| 17 |
protected $receipt; |
| 18 |
|
| 19 |
/** |
| 20 |
* @since 2.10.2 |
| 21 |
* |
| 22 |
* @param DonationReceipt $receipt |
| 23 |
*/ |
| 24 |
public function __construct(DonationReceipt $receipt) |
| 25 |
{ |
| 26 |
$this->receipt = $receipt; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 2.10.2 |
| 31 |
* |
| 32 |
* @param string $hook |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function __invoke($hook) |
| 37 |
{ |
| 38 |
$formID = give_get_payment_meta($this->receipt->donationId, '_give_payment_form_id'); |
| 39 |
|
| 40 |
$collection = Group::make($hook); |
| 41 |
do_action("give_fields_{$hook}", $collection, $formID); |
| 42 |
|
| 43 |
$collection->walkFields([$this, 'apply']); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @since 2.10.2 |
| 48 |
* |
| 49 |
* @param Field $field |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function apply(Field $field) |
| 54 |
{ |
| 55 |
if ( ! $field->shouldShowInReceipt()) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ($field->shouldStoreAsDonorMeta()) { |
| 60 |
$this->addDonorLineItem($field); |
| 61 |
} else { |
| 62 |
$this->addAdditionalLineItems($field); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @since 2.10.2 |
| 68 |
* |
| 69 |
* @param Field $field |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
protected function addDonorLineItem(Field $field) |
| 74 |
{ |
| 75 |
$donorID = give_get_payment_meta($this->receipt->donationId, '_give_payment_donor_id'); |
| 76 |
if ($value = Give()->donor_meta->get_meta($donorID, $field->getName(), true)) { |
| 77 |
$this->receipt |
| 78 |
->getSections()[DonationReceipt::DONORSECTIONID] |
| 79 |
->addLineItem( |
| 80 |
[ |
| 81 |
'id' => $field->getName(), |
| 82 |
'label' => $field->getLabel(), |
| 83 |
'value' => $value, |
| 84 |
] |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @since 2.10.2 |
| 91 |
* |
| 92 |
* @param Field $field |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
protected function addAdditionalLineItems(Field $field) |
| 97 |
{ |
| 98 |
if ($value = give_get_payment_meta($this->receipt->donationId, $field->getName())) { |
| 99 |
$this->receipt |
| 100 |
->getSections()[DonationReceipt::ADDITIONALINFORMATIONSECTIONID] |
| 101 |
->addLineItem( |
| 102 |
[ |
| 103 |
'id' => $field->getName(), |
| 104 |
'label' => $field->getLabel(), |
| 105 |
'value' => $value, |
| 106 |
] |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|