DeprecateOldTemplateHook.php
4 years ago
HookCommandInterface.php
4 years ago
SetupFieldConfirmation.php
3 years ago
SetupFieldEmailTag.php
4 years ago
SetupFieldPersistence.php
4 years ago
SetupFieldReceipt.php
3 years ago
SetupFieldValidation.php
4 years ago
SetupNewTemplateHook.php
4 years ago
SetupFieldPersistence.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Form\LegacyConsumer\Commands; |
| 4 | |
| 5 | use Give\Form\LegacyConsumer\Actions\UploadFilesAction; |
| 6 | use Give\Framework\FieldsAPI\Concenrs\StoreAsMeta; |
| 7 | use Give\Framework\FieldsAPI\Field; |
| 8 | use Give\Framework\FieldsAPI\File; |
| 9 | use Give\Framework\FieldsAPI\Group; |
| 10 | use Give\Framework\FieldsAPI\Text; |
| 11 | use Give\Framework\FieldsAPI\Types; |
| 12 | |
| 13 | /** |
| 14 | * Persist custom field values as donation meta. |
| 15 | * |
| 16 | * @since 2.10.2 |
| 17 | */ |
| 18 | class SetupFieldPersistence implements HookCommandInterface |
| 19 | { |
| 20 | |
| 21 | /** @var int */ |
| 22 | private $donationId; |
| 23 | /** @var array */ |
| 24 | private $donationData; |
| 25 | |
| 26 | /** |
| 27 | * @since 2.10.2 |
| 28 | * |
| 29 | * @param int $donationId |
| 30 | * @param array $donationData |
| 31 | */ |
| 32 | public function __construct($donationId, $donationData) |
| 33 | { |
| 34 | $this->donationId = $donationId; |
| 35 | $this->donationData = $donationData; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @since 2.10.2 |
| 40 | * |
| 41 | * @param string $hook |
| 42 | * |
| 43 | * @void |
| 44 | */ |
| 45 | public function __invoke($hook) |
| 46 | { |
| 47 | $collection = Group::make($hook); |
| 48 | do_action("give_fields_$hook", $collection, $this->donationData['give_form_id']); |
| 49 | $collection->walkFields([$this, 'process']); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Process the given field. |
| 54 | * |
| 55 | * @since 2.10.2 |
| 56 | * @since 2.14.0 Handle File field type and custom field type separately. Use add meta function to persist field value. |
| 57 | * |
| 58 | * @param Field $field |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function process(Field $field) |
| 63 | { |
| 64 | if ($field->getType() === Types::FILE) { |
| 65 | /** @var File $field */ |
| 66 | if (isset($_FILES[$field->getName()])) { |
| 67 | $fileUploader = new UploadFilesAction($field); |
| 68 | $fileIds = $fileUploader(); |
| 69 | |
| 70 | foreach ($fileIds as $fileId) { |
| 71 | if ($field->shouldStoreAsDonorMeta()) { |
| 72 | $donorID = give_get_payment_meta($this->donationId, '_give_payment_donor_id'); |
| 73 | Give()->donor_meta->add_meta($donorID, $field->getName(), $fileId); |
| 74 | } else { |
| 75 | // Store as Donation Meta - default behavior. |
| 76 | give()->payment_meta->add_meta($this->donationId, $field->getName(), $fileId); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } elseif (in_array($field->getType(), Types::all(), true)) { |
| 81 | if (isset($_POST[$field->getName()])) { |
| 82 | $data = give_clean($_POST[$field->getName()]); |
| 83 | $value = is_array($data) ? |
| 84 | implode('| ', array_values(array_filter($data))) : |
| 85 | $data; |
| 86 | |
| 87 | /** @var Text $field */ |
| 88 | if ($field->shouldStoreAsDonorMeta()) { |
| 89 | $donorID = give_get_payment_meta($this->donationId, '_give_payment_donor_id'); |
| 90 | Give()->donor_meta->add_meta($donorID, $field->getName(), $value); |
| 91 | } else { |
| 92 | // Store as Donation Meta - default behavior. |
| 93 | give()->payment_meta->add_meta($this->donationId, $field->getName(), $value); |
| 94 | } |
| 95 | } |
| 96 | } else { |
| 97 | /** |
| 98 | * Use this action to save custom field which does not exist in fields API. |
| 99 | * |
| 100 | * @since 2.14.0 |
| 101 | * |
| 102 | * @param Field $field |
| 103 | * @param int $donationId |
| 104 | * @param array $donationData |
| 105 | */ |
| 106 | do_action('give_fields_api_save_field', $field, $this->donationId, $this->donationData); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 |