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