PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Framework / FieldsAPI / Concerns / HasPersistence.php

HasPersistence.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/Framework/FieldsAPI/Concerns/HasPersistence.php

125 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\FieldsAPI\Concerns;
4
5 use Closure;
6 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
7 use Give\Framework\FieldsAPI\ValueObjects\PersistenceScope;
8
9 /**
10 * This provides the ability to set a scope and meta key for a field. The scope is used to determine if and where the
11 * field should be stored. The meta key is used to store the field in the database.
12 *
13 * @since 2.32.0
14 */
15 trait HasPersistence
16 {
17 /**
18 * @var Closure
19 */
20 private $scopeCallback;
21
22 /**
23 * @since 2.32.0
24 *
25 * @var PersistenceScope|null
26 */
27 protected $scope;
28
29 /**
30 * @since 2.32.0
31 *
32 * @var string|null
33 */
34 protected $metaKey = null;
35
36 /**
37 * @since 2.32.0
38 *
39 * @param string|PersistenceScope|Closure $scope
40 */
41 public function scope($scope): self
42 {
43 if ($scope instanceof Closure) {
44 $this->scopeCallback = $scope;
45 $scope = PersistenceScope::callback();
46 } elseif (is_string($scope)) {
47 $scope = new PersistenceScope($scope);
48 } elseif (!$scope instanceof PersistenceScope) {
49 throw new InvalidArgumentException('Scope must be a string, Closure, or PersistenceScope');
50 }
51
52 $this->scope = $scope;
53
54 return $this;
55 }
56
57 /**
58 * @since 2.32.0
59 */
60 public function getScope(): PersistenceScope
61 {
62 return $this->scope;
63 }
64
65 /**
66 * @since 2.32.0
67 */
68 public function getScopeValue(): string
69 {
70 return (string)$this->scope;
71 }
72
73 /**
74 * @return Closure|null
75 */
76 public function getScopeCallback()
77 {
78 return $this->scopeCallback;
79 }
80
81 /**
82 * @since 2.32.0
83 */
84 public function metaKey(string $metaKey): self
85 {
86 $this->metaKey = $metaKey;
87
88 return $this;
89 }
90
91 /**
92 * @since 2.32.0
93 *
94 * @return string|null
95 */
96 public function getMetaKey()
97 {
98 return $this->metaKey;
99 }
100
101 /**
102 * @since 2.32.0 updated to use scoping under the hood, no signature change
103 * @since 2.28.0 added types
104 * @since 2.10.2
105 */
106 public function storeAsDonorMeta(bool $storeAsDonorMeta = true)
107 {
108 $this->scope = $storeAsDonorMeta
109 ? PersistenceScope::donor()
110 : PersistenceScope::donation();
111
112 return $this;
113 }
114
115 /**
116 * @since 2.32.0 updated to use scoping under the hood, no signature change
117 * @since 2.28.0 added types
118 * @since 2.10.2
119 */
120 public function shouldStoreAsDonorMeta(): bool
121 {
122 return $this->scope->isDonor();
123 }
124 }
125