PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.2.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.2.0
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 / Customer.php
Customer.php
127 lines 2.4 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 /**
6 * Price model
7 */
8 class Customer extends Model {
9 /**
10 * Rest API endpoint
11 *
12 * @var string
13 */
14 protected $endpoint = 'customers';
15
16 /**
17 * Object name
18 *
19 * @var string
20 */
21 protected $object_name = 'customer';
22
23 /**
24 * Create a new model
25 *
26 * @param array $attributes Attributes to create.
27 * @param boolean $create_user Whether to create a corresponding WordPress user.
28 *
29 * @return $this|\WP_Error|false
30 */
31 protected function create( $attributes = [], $create_user = true ) {
32 parent::create( $attributes );
33
34 // maybe create a WordPress user.
35 if ( $create_user ) {
36 $user = User::create(
37 [
38 'user_name' => $this->attributes['name'] ?? null,
39 'user_email' => $this->attributes['email'],
40 ]
41 );
42
43 // handle error creating user.
44 if ( is_wp_error( $user ) ) {
45 return $user;
46 }
47
48 $linked = $user->setCustomerId( $this->attributes['id'], $this->live_mode ? 'live' : 'test' );
49
50 if ( is_wp_error( $linked ) ) {
51 return $linked;
52 }
53 }
54
55 return $this;
56 }
57
58 /**
59 * Expose media for a customer
60 *
61 * @param string $media_id The media id.
62 *
63 * @return \SureCart\Models\Media|false;
64 */
65 protected function exposeMedia( $media_id ) {
66 if ( empty( $this->attributes['id'] ) || empty( $media_id ) ) {
67 return false;
68 }
69
70 if ( $this->fireModelEvent( 'exposing_media' ) === false ) {
71 return false;
72 }
73
74 $media = \SureCart::request(
75 $this->endpoint . '/' . $this->attributes['id'] . '/expose/' . $media_id,
76 [
77 'method' => 'GET',
78 'query' => $this->query,
79 ]
80 );
81
82 if ( $this->isError( $media ) ) {
83 return $media;
84 }
85
86 $this->fireModelEvent( 'exposed_media' );
87
88 return new Media( $media );
89 }
90
91 /**
92 * Get a customer by their email address
93 *
94 * @param string $email Email address.
95 * @return this
96 */
97 protected function byEmail( $email ) {
98 return $this->where(
99 [
100 'email' => $email,
101 ]
102 )->first();
103 }
104
105 /**
106 * Get the customer's user.
107 *
108 * @return string|null
109 */
110 public function getUser() {
111 return User::findByCustomerId( $this->id );
112 }
113
114 /**
115 * Maybe also return the user when the id is set.
116 *
117 * @param string $value The user id.
118 * @return void
119 */
120 public function setIdAttribute( $value ) {
121 $this->attributes['id'] = $value;
122 if ( ! empty( $this->query['expand'] ) && in_array( 'user', $this->query['expand'] ) ) {
123 $this->attributes['user'] = $this->getUser();
124 }
125 }
126 }
127