DonationDetailsView.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations\CustomFields\Views; |
| 4 | |
| 5 | use Give\Donations\Models\Donation; |
| 6 | use Give\Framework\FieldsAPI\Field; |
| 7 | use Give\Framework\FieldsAPI\Types; |
| 8 | |
| 9 | /** |
| 10 | * @since 3.0.0 |
| 11 | */ |
| 12 | class DonationDetailsView |
| 13 | { |
| 14 | /** @var Donation */ |
| 15 | protected $donation; |
| 16 | |
| 17 | /** @var Field[] */ |
| 18 | protected $fields; |
| 19 | |
| 20 | /** |
| 21 | * @since 3.0.0 |
| 22 | * |
| 23 | * @param Donation $donation |
| 24 | * @param array|Field[] $fields |
| 25 | */ |
| 26 | public function __construct(Donation $donation, array $fields) |
| 27 | { |
| 28 | $this->donation = $donation; |
| 29 | $this->fields = $fields; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @since 3.0.0 |
| 34 | * |
| 35 | * @return string |
| 36 | */ |
| 37 | public function render(): string |
| 38 | { |
| 39 | return "<div class='postbox' style='padding-bottom: 15px;'> |
| 40 | <h3 class='handle'>{$this->getTitle()}</h3> |
| 41 | <div class='inside'>{$this->getContents()}</div> |
| 42 | </div>"; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @since 3.0.0 |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | protected function getTitle(): string |
| 51 | { |
| 52 | return __('Custom Fields', 'give'); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @since 3.0.0 |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | protected function getContents(): string |
| 61 | { |
| 62 | return array_reduce($this->fields, function ($output, Field $field) { |
| 63 | $value = $this->getFieldValue($field); |
| 64 | $label = method_exists($field, 'getLabel') ? $field->getLabel() : $field->getName(); |
| 65 | |
| 66 | if (empty($value)) { |
| 67 | return $output; |
| 68 | } |
| 69 | |
| 70 | return $output . " |
| 71 | <div> |
| 72 | <strong>{$label}:</strong> |
| 73 | {$value} |
| 74 | </div> |
| 75 | "; |
| 76 | }, ''); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @since 3.0.0 |
| 81 | * |
| 82 | * @param Field $field |
| 83 | * |
| 84 | * @return mixed |
| 85 | */ |
| 86 | protected function getFieldValue(Field $field) |
| 87 | { |
| 88 | $metaValue = give()->payment_meta->get_meta($this->donation->id, $field->getName(), true); |
| 89 | |
| 90 | if (empty($metaValue)) { |
| 91 | return ''; |
| 92 | } |
| 93 | |
| 94 | if ($field->getType() === Types::FILE) { |
| 95 | return wp_get_attachment_link($metaValue); |
| 96 | } |
| 97 | |
| 98 | return $metaValue; |
| 99 | } |
| 100 | } |
| 101 |