PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Models / Affiliation.php
Affiliation.php
191 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Models;
4
5 use SureCart\Models\Traits\HasCommissionStructure;
6 use SureCart\Models\Traits\HasDates;
7 use SureCart\Models\Traits\HasPayouts;
8 use SureCart\Models\Traits\HasReferrals;
9 use SureCart\Support\Currency;
10 use SureCart\Support\TimeDate;
11
12 /**
13 * Holds the data of the current Affiliation.
14 */
15 class Affiliation extends Model {
16 use HasReferrals;
17 use HasPayouts;
18 use HasCommissionStructure;
19 use HasDates;
20
21 /**
22 * Rest API endpoint
23 *
24 * @var string
25 */
26 protected $endpoint = 'affiliations';
27
28 /**
29 * Object name
30 *
31 * @var string
32 */
33 protected $object_name = 'affiliation';
34
35 /**
36 * Activate an affiliation
37 *
38 * @param string $id Affiliation ID.
39 *
40 * @return $this|\WP_Error
41 */
42 protected function activate( $id = null ) {
43 if ( $id ) {
44 $this->setAttribute( 'id', $id );
45 }
46
47 if ( $this->fireModelEvent( 'activating' ) === false ) {
48 return $this;
49 }
50
51 if ( empty( $this->attributes['id'] ) ) {
52 return new \WP_Error( 'not_saved', 'Please create the affiliation.' );
53 }
54
55 $activated = \SureCart::request(
56 $this->endpoint . '/' . $this->attributes['id'] . '/activate',
57 [
58 'method' => 'PATCH',
59 'query' => $this->query,
60 ]
61 );
62
63 if ( is_wp_error( $activated ) ) {
64 return $activated;
65 }
66
67 $this->resetAttributes();
68 $this->fill( $activated );
69 $this->fireModelEvent( 'activated' );
70
71 return $this;
72 }
73
74 /**
75 * Deactivate an affiliation
76 *
77 * @param string $id Affiliation ID.
78 *
79 * @return $this|\WP_Error
80 */
81 protected function deactivate( $id = null ) {
82 if ( $id ) {
83 $this->setAttribute( 'id', $id );
84 }
85
86 if ( $this->fireModelEvent( 'deactivating' ) === false ) {
87 return $this;
88 }
89
90 if ( empty( $this->attributes['id'] ) ) {
91 return new \WP_Error( 'not_saved', 'Please create the affiliation.' );
92 }
93
94 $deactivated = \SureCart::request(
95 $this->endpoint . '/' . $this->attributes['id'] . '/deactivate',
96 [
97 'method' => 'PATCH',
98 'query' => $this->query,
99 ]
100 );
101
102 if ( is_wp_error( $deactivated ) ) {
103 return $deactivated;
104 }
105
106 $this->resetAttributes();
107 $this->fill( $deactivated );
108 $this->fireModelEvent( 'deactivated' );
109
110 return $this;
111 }
112
113 /**
114 * Set the clicks attribute
115 *
116 * @param object $value Array of click objects.
117 *
118 * @return void
119 */
120 public function setClicksAttribute( $value ) {
121 $this->setCollection( 'clicks', $value, Click::class );
122 }
123
124 /**
125 * Get the display name
126 *
127 * @return string
128 */
129 protected function getDisplayNameAttribute() {
130 if ( ! empty( $this->first_name . $this->last_name ) ) {
131 return $this->first_name . ' ' . $this->last_name;
132 }
133 return $this->email;
134 }
135
136 /**
137 * Get the display status attribute.
138 *
139 * @return string
140 */
141 public function getStatusDisplayTextAttribute() {
142 return $this->active ? __( 'Active', 'surecart' ) : __( 'Inactive', 'surecart' );
143 }
144
145 /**
146 * Get the display status attribute.
147 *
148 * @return string
149 */
150 public function getStatusTypeAttribute() {
151 return $this->active ? 'success' : 'default';
152 }
153
154
155 /**
156 * Get the expires at date.
157 *
158 * @return string
159 */
160 public function getExpiresAtDateAttribute() {
161 return ! empty( $this->expires_at ) ? TimeDate::formatDate( $this->expires_at ) : '';
162 }
163
164 /**
165 * Get the expires at date and time.
166 *
167 * @return string
168 */
169 public function getExpiresAtDateTimeAttribute() {
170 return ! empty( $this->expires_at ) ? TimeDate::formatDateAndTime( $this->expires_at ) : '';
171 }
172
173 /**
174 * Get the total commission amount display.
175 *
176 * @return string
177 */
178 public function getTotalCommissionDisplayAmountAttribute() {
179 return Currency::format( $this->total_commission_amount );
180 }
181
182 /**
183 * Get the total commission amount display.
184 *
185 * @return string
186 */
187 public function getTotalNotPaidCommissionDisplayAmountAttribute() {
188 return Currency::format( $this->total_not_paid_commission_amount );
189 }
190 }
191