Affiliation.php
| 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\TimeDate; |
| 10 | |
| 11 | /** |
| 12 | * Holds the data of the current Affiliation. |
| 13 | */ |
| 14 | class Affiliation extends Model { |
| 15 | use HasReferrals, HasPayouts, HasCommissionStructure, HasDates; |
| 16 | |
| 17 | /** |
| 18 | * Rest API endpoint |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $endpoint = 'affiliations'; |
| 23 | |
| 24 | /** |
| 25 | * Object name |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $object_name = 'affiliation'; |
| 30 | |
| 31 | /** |
| 32 | * Activate an affiliation |
| 33 | * |
| 34 | * @param string $id Affiliation ID. |
| 35 | * |
| 36 | * @return $this|\WP_Error |
| 37 | */ |
| 38 | protected function activate( $id = null ) { |
| 39 | if ( $id ) { |
| 40 | $this->setAttribute( 'id', $id ); |
| 41 | } |
| 42 | |
| 43 | if ( $this->fireModelEvent( 'activating' ) === false ) { |
| 44 | return $this; |
| 45 | } |
| 46 | |
| 47 | if ( empty( $this->attributes['id'] ) ) { |
| 48 | return new \WP_Error( 'not_saved', 'Please create the affiliation.' ); |
| 49 | } |
| 50 | |
| 51 | $activated = \SureCart::request( |
| 52 | $this->endpoint . '/' . $this->attributes['id'] . '/activate', |
| 53 | [ |
| 54 | 'method' => 'PATCH', |
| 55 | 'query' => $this->query, |
| 56 | ] |
| 57 | ); |
| 58 | |
| 59 | if ( is_wp_error( $activated ) ) { |
| 60 | return $activated; |
| 61 | } |
| 62 | |
| 63 | $this->resetAttributes(); |
| 64 | $this->fill( $activated ); |
| 65 | $this->fireModelEvent( 'activated' ); |
| 66 | |
| 67 | return $this; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Deactivate an affiliation |
| 72 | * |
| 73 | * @param string $id Affiliation ID. |
| 74 | * |
| 75 | * @return $this|\WP_Error |
| 76 | */ |
| 77 | protected function deactivate( $id = null ) { |
| 78 | if ( $id ) { |
| 79 | $this->setAttribute( 'id', $id ); |
| 80 | } |
| 81 | |
| 82 | if ( $this->fireModelEvent( 'deactivating' ) === false ) { |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | if ( empty( $this->attributes['id'] ) ) { |
| 87 | return new \WP_Error( 'not_saved', 'Please create the affiliation.' ); |
| 88 | } |
| 89 | |
| 90 | $deactivated = \SureCart::request( |
| 91 | $this->endpoint . '/' . $this->attributes['id'] . '/deactivate', |
| 92 | [ |
| 93 | 'method' => 'PATCH', |
| 94 | 'query' => $this->query, |
| 95 | ] |
| 96 | ); |
| 97 | |
| 98 | if ( is_wp_error( $deactivated ) ) { |
| 99 | return $deactivated; |
| 100 | } |
| 101 | |
| 102 | $this->resetAttributes(); |
| 103 | $this->fill( $deactivated ); |
| 104 | $this->fireModelEvent( 'deactivated' ); |
| 105 | |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Set the clicks attribute |
| 111 | * |
| 112 | * @param object $value Array of click objects. |
| 113 | * |
| 114 | * @return void |
| 115 | */ |
| 116 | public function setClicksAttribute( $value ) { |
| 117 | $this->setCollection( 'clicks', $value, Click::class ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the display name |
| 122 | * |
| 123 | * @return string |
| 124 | */ |
| 125 | protected function getDisplayNameAttribute() { |
| 126 | if ( ! empty( $this->first_name . $this->last_name ) ) { |
| 127 | return $this->first_name . ' ' . $this->last_name; |
| 128 | } |
| 129 | return $this->email; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the display status attribute. |
| 134 | * |
| 135 | * @return string |
| 136 | */ |
| 137 | public function getStatusDisplayTextAttribute() { |
| 138 | return $this->active ? __( 'Active', 'surecart' ) : __( 'Inactive', 'surecart' ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the display status attribute. |
| 143 | * |
| 144 | * @return string |
| 145 | */ |
| 146 | public function getStatusTypeAttribute() { |
| 147 | return $this->active ? 'success' : 'default'; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Get the expires at date. |
| 153 | * |
| 154 | * @return string |
| 155 | */ |
| 156 | public function getExpiresAtDateAttribute() { |
| 157 | return ! empty( $this->expires_at ) ? TimeDate::formatDate( $this->expires_at ) : ''; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get the expires at date and time. |
| 162 | * |
| 163 | * @return string |
| 164 | */ |
| 165 | public function getExpiresAtDateTimeAttribute() { |
| 166 | return ! empty( $this->expires_at ) ? TimeDate::formatDateAndTime( $this->expires_at ) : ''; |
| 167 | } |
| 168 | } |
| 169 |