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