11<?php
22
3- class ResolverCommonAccount extends Resolver {
4- public function login ( $ args ) {
5- $ creds = array ();
6-
7- $ creds ['user_login ' ] = $ args ["email " ];
8- $ creds ['user_password ' ] = $ args ["password " ];
9- $ creds ['remember ' ] = true ;
10- $ user = wp_signon ( $ creds );
11-
12- if ( ! is_wp_error ( $ user ) ) {
13- global $ current_user ;
14-
15- $ current_user = $ user ;
16-
17- return $ this ->get ($ current_user ->ID );
18- } else {
19- $ error = reset ( $ user ->errors );
20- throw new Exception ( $ error [0 ] );
21- }
22- }
23-
24- public function logout ( $ args ) {
25- wp_logout ();
26-
27- return array (
28- 'status ' => is_user_logged_in ()
29- );
30- }
31-
32- public function register ( $ args ) {
33- $ customer = $ args ['customer ' ];
34-
35- $ userdata = array (
36- 'user_pass ' => $ customer ['password ' ],
37- 'user_login ' => $ customer ['email ' ],
38- 'user_email ' => $ customer ['email ' ],
39- 'first_name ' => $ customer ['firstName ' ],
40- 'last_name ' => $ customer ['lastName ' ]
41- );
42-
43- $ user_id = wp_insert_user ( $ userdata );
44- if ( ! is_wp_error ( $ user_id ) ) {
45- return $ this ->get ($ user_id );
46- } else {
47- $ error = reset ( $ user_id ->errors );
48- throw new Exception ( $ error [0 ] );
49- }
3+ class ResolverCommonAccount extends Resolver
4+ {
5+ public function login ($ args )
6+ {
7+ $ creds = array ();
8+
9+ $ creds ['user_login ' ] = $ args ["email " ];
10+ $ creds ['user_password ' ] = $ args ["password " ];
11+ $ creds ['remember ' ] = true ;
12+ $ user = wp_signon ($ creds );
13+
14+ if (!is_wp_error ($ user )) {
15+ global $ current_user ;
16+
17+ $ current_user = $ user ;
18+
19+ return $ this ->get ($ current_user ->ID );
20+ } else {
21+ $ error = reset ($ user ->errors );
22+ throw new Exception ($ error [0 ]);
23+ }
5024 }
51-
52- public function edit ($ args ) {
25+
26+ public function logout ($ args )
27+ {
28+ wp_logout ();
29+
30+ return array (
31+ 'status ' => is_user_logged_in ()
32+ );
33+ }
34+
35+ public function register ($ args )
36+ {
37+ $ customer = $ args ['customer ' ];
38+
39+ $ userdata = array (
40+ 'user_pass ' => $ customer ['password ' ],
41+ 'user_login ' => $ customer ['email ' ],
42+ 'user_email ' => $ customer ['email ' ],
43+ 'first_name ' => $ customer ['firstName ' ],
44+ 'last_name ' => $ customer ['lastName ' ]
45+ );
46+
47+ $ user_id = wp_insert_user ($ userdata );
48+ if (!is_wp_error ($ user_id )) {
49+ return $ this ->get ($ user_id );
50+ } else {
51+ $ error = reset ($ user_id ->errors );
52+ throw new Exception ($ error [0 ]);
53+ }
54+ }
55+
56+ public function edit ($ args )
57+ {
5358 global $ current_user ;
5459
5560 return $ this ->get ($ current_user ->ID );
5661 }
5762
58- public function editPassword ($ args ) {
63+ public function editPassword ($ args )
64+ {
5965 global $ current_user ;
6066
6167 return $ this ->get ($ current_user ->ID );
6268 }
6369
64- public function get ($ user_id ) {
65- $ user = get_user_by ( 'ID ' , $ user_id );
70+ public function get ($ user_id )
71+ {
72+ $ user = get_user_by ('ID ' , $ user_id );
73+
74+ return array (
75+ 'id ' => $ user ->ID ,
76+ 'email ' => $ user ->user_email ,
77+ 'firstName ' => get_user_meta ($ user ->ID , 'first_name ' , true ),
78+ 'lastName ' => get_user_meta ($ user ->ID , 'last_name ' , true ),
79+ );
80+ }
81+
82+ public function isLogged ($ args )
83+ {
84+ $ customer = array ();
85+
86+ if (is_user_logged_in ()) {
87+ $ user = wp_get_current_user ();
88+
89+ $ customer = array (
90+ 'id ' => $ user ->ID ,
91+ 'email ' => $ user ->user_email ,
92+ 'firstName ' => $ user ->user_firstname ,
93+ 'lastName ' => $ user ->user_lasttname
94+ );
95+ }
6696
6797 return array (
68- 'id ' => $ user ->ID ,
69- 'email ' => $ user ->user_email ,
70- 'firstName ' => get_user_meta ( $ user ->ID , 'first_name ' , true ),
71- 'lastName ' => get_user_meta ( $ user ->ID , 'last_name ' , true ),
98+ 'status ' => is_user_logged_in (),
99+ 'customer ' => $ customer
72100 );
73101 }
74102
75- public function isLogged ( $ args ) {
76- $ customer = array ();
77-
78- if ( is_user_logged_in () ) {
79- $ user = wp_get_current_user ();
80-
81- $ customer = array (
82- 'id ' => $ user ->ID ,
83- 'email ' => $ user ->user_email ,
84- 'firstName ' => $ user ->user_firstname ,
85- 'lastName ' => $ user ->user_lasttname
86- );
87- }
88-
89- return array (
90- 'status ' => is_user_logged_in (),
91- 'customer ' => $ customer
92- );
93- }
103+ public function address ($ args )
104+ {
105+ $ address = array ();
106+
107+ global $ current_user ;
108+
109+ switch ($ args ['id ' ]) {
110+ case 'billing ' :
111+ $ address = array (
112+ 'id ' => 'billing ' ,
113+ 'firstName ' => get_user_meta ($ current_user ->ID , 'billing_first_name ' , true ),
114+ 'lastName ' => get_user_meta ($ current_user ->ID , 'billing_last_name ' , true ),
115+ 'company ' => get_user_meta ($ current_user ->ID , 'billing_company ' , true ),
116+ 'address1 ' => get_user_meta ($ current_user ->ID , 'billing_address_1 ' , true ),
117+ 'address2 ' => get_user_meta ($ current_user ->ID , 'billing_address_2 ' , true ),
118+ 'zoneId ' => '' ,
119+ 'zone ' => array (
120+ 'id ' => '' ,
121+ 'name ' => ''
122+ ),
123+ 'country ' => $ this ->load ->resolver ('common/country/get ' , array (
124+ 'id ' => get_user_meta ($ current_user ->ID , 'billing_country ' , true )
125+ )),
126+ 'countryId ' => get_user_meta ($ current_user ->ID , 'billing_country ' , true ),
127+ 'city ' => get_user_meta ($ current_user ->ID , 'billing_city ' , true ),
128+ 'zipcode ' => get_user_meta ($ current_user ->ID , 'billing_postcode ' , true )
129+ );
130+ break ;
131+ case 'shipping ' :
132+ $ address = array (
133+ 'id ' => 'shipping ' ,
134+ 'firstName ' => get_user_meta ($ current_user ->ID , 'shipping_first_name ' , true ),
135+ 'lastName ' => get_user_meta ($ current_user ->ID , 'shipping_last_name ' , true ),
136+ 'company ' => get_user_meta ($ current_user ->ID , 'shipping_company ' , true ),
137+ 'address1 ' => get_user_meta ($ current_user ->ID , 'shipping_address_1 ' , true ),
138+ 'address2 ' => get_user_meta ($ current_user ->ID , 'shipping_address_2 ' , true ),
139+ 'zoneId ' => '' ,
140+ 'zone ' => array (
141+ 'id ' => '' ,
142+ 'name ' => ''
143+ ),
144+ 'country ' => $ this ->load ->resolver ('common/country/get ' , array (
145+ 'id ' => get_user_meta ($ current_user ->ID , 'shipping_country ' , true )
146+ )),
147+ 'countryId ' => get_user_meta ($ current_user ->ID , 'shipping_country ' , true ),
148+ 'city ' => get_user_meta ($ current_user ->ID , 'shipping_city ' , true ),
149+ 'zipcode ' => get_user_meta ($ current_user ->ID , 'shipping_postcode ' , true )
150+ );
151+ break ;
152+ }
153+
154+ return $ address ;
155+ }
156+
157+ public function addressList ($ args )
158+ {
159+
160+ $ ids = array ('billing ' , 'shipping ' );
161+
162+ $ address = array ();
163+
164+ foreach ($ ids as $ value ) {
165+ $ address [] = $ this ->address (array ('id ' => $ value ));
166+ }
167+
168+ return $ address ;
169+ }
170+
171+ public function editAddress ($ args )
172+ {
173+ global $ current_user ;
174+
175+ $ prefix = $ args ['id ' ];
176+
177+ $ data = array (
178+ 'firstName ' => '_first_name ' ,
179+ 'lastName ' => '_last_name ' ,
180+ 'company ' => '_company ' ,
181+ 'address1 ' => '_address_1 ' ,
182+ 'address2 ' => '_address_2 ' ,
183+ 'countryId ' => '_country ' ,
184+ 'city ' => '_city ' ,
185+ 'zipcode ' => '_postcode '
186+ );
187+
188+ foreach ($ data as $ key => $ value ) {
189+ update_user_meta ($ current_user ->ID , $ prefix .$ value , $ args ['address ' ][$ key ]);
190+ }
191+
192+ return $ this ->address ($ args );
193+ }
194+
195+ public function addAddress ($ args ) {
196+ throw new Exception ('Adding an address is not possible in Wordpress ' );
197+ }
198+
199+ public function removeAddress ($ args ) {
200+ throw new Exception ('Removing an address is not possible in Wordpress ' );
201+ }
94202}
0 commit comments