Skip to content

Commit 1e0f113

Browse files
committed
v3.5.0
1 parent 1a43178 commit 1e0f113

15 files changed

+1222
-179
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
## [3.5.0] - 2024-03-18
2+
3+
* Breaking change `WPJsonAPI.initWith` is now `WPJsonAPI.init`
4+
* Refactor project
5+
* Ability to save user token for future requests
6+
* New WpUser model for user data
7+
* New wcRegister method for networking class
8+
* Add more data to `WpUserLoginResponse` and `WpUserInfoResponse`
9+
* Added `version` to `WpJsonAPI` class
10+
* New `WPJsonAPI.wpLogin` method to login a user
11+
* New `WPJsonAPI.wpLogout` method to logout a user
12+
* New `WPJsonAPI.wpUserLoggedIn` method to check if a user is logged in
13+
* New `WPJsonAPI.wpUser` method to get the current user
14+
* New `WPJsonAPI.wpUserId` method to get the current user's ID
15+
* New `WPJsonAPI.wpUserToken` method to get the current user's token
16+
* New `WPJsonAPI.wpAuth` method to authenticate the previously logged in user
17+
* Added Storage key to `WPJsonAPI` class
18+
* New docs added to Readme
19+
* Dependency updates
20+
121
## [3.4.0] - 2024-03-15
222

323
* Added new networking methods for WooCommerce Points and Rewards

README.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ In your flutter project add the dependency:
2828
``` dart
2929
dependencies:
3030
...
31-
wp_json_api: ^3.4.0
31+
wp_json_api: ^3.5.0
3232
```
3333

3434
### Usage example #
@@ -45,7 +45,7 @@ import 'package:wp_json_api/wp_json_api.dart';
4545
4646
void main() {
4747
48-
WPJsonAPI.instance.initWith(baseUrl: "https://mysite.com");
48+
WPJsonAPI.instance.init(baseUrl: "https://mysite.com");
4949
5050
...
5151
```
@@ -115,29 +115,26 @@ WPUserRegisterResponse wpUserRegisterResponse = await WPJsonAPI.instance
115115
.api((request) => request.wpRegister(
116116
email: email,
117117
password: password,
118-
username: username
118+
// username: username // optional - the library will automatically generate a username if not provided
119119
));
120120
```
121121

122122
#### WordPress - Get Users Info
123123
- Used to get a WordPress users info
124-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
124+
- After you login/register, you can all this method to get the users info
125125

126126
``` dart
127127
WPUserInfoResponse wpUserInfoResponse = await WPJsonAPI.instance
128-
.api((request) => request.wpGetUserInfo(
129-
userToken
130-
));
128+
.api((request) => request.wpGetUserInfo());
131129
```
132130

133131
#### WordPress - Update Users Info
134132
- Used to update a WordPress users info
135-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
133+
- After you login/register, you can all this method to update the users info
136134

137135
``` dart
138136
WPUserInfoUpdatedResponse wpUserInfoUpdatedResponse = await WPJsonAPI.instance
139137
.api((request) => request.wpUpdateUserInfo(
140-
userToken,
141138
firstName: firstName,
142139
lastName: lastName,
143140
displayName: displayName
@@ -146,55 +143,61 @@ WPUserInfoUpdatedResponse wpUserInfoUpdatedResponse = await WPJsonAPI.instance
146143

147144
#### WordPress - Update users password
148145
- Used to update a users password
149-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
146+
- After you login/register, you can all this method to update the users password
150147

151148
``` dart
152149
WPUserResetPasswordResponse wpUserResetPasswordResponse = await WPJsonAPI.instance
153150
.api((request) => request.wpResetPassword(
154-
userToken,
155151
password: password
156152
));
157153
```
158154

159155
#### WordPress - Add a role to a user
160156
- Used to add a role to a user in WordPress
161-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
157+
- After you login/register, you can all this method to add a role to the user
162158

163159
``` dart
164160
WPUserAddRoleResponse wpUserAddRoleResponse = await WPJsonAPI.instance
165161
.api((request) => request.wpUserAddRole(
166-
userToken,
167162
role: "customer" // e.g. customer, subscriber
168163
));
169164
```
170165

171166
#### WordPress - Remove a role from a user
172167
- Used to remove a role from a user in WordPress
173-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
168+
- After you login/register, you can all this method to remove a role from the user
174169

175170
``` dart
176171
WPUserRemoveRoleResponse wpUserRemoveRoleResponse = await WPJsonAPI.instance
177172
.api((request) => request.wpUserRemoveRole(
178-
userToken,
179173
role: "customer" // e.g. customer, subscriber
180174
));
181175
```
182176

183177
#### WordPress - Delete a user
184178
- Used to delete a user in WordPress
185-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
179+
- After you login/register, you can all this method to delete the user
186180
- You can pass an optional argument 'reassign' to reassign posts and links to new User ID.
187181

188182
``` dart
189183
WPUserDeleteResponse wpUserDeleteResponse = await WPJsonAPI.instance
190-
.api((request) => request.wpUserDelete(
191-
userToken
192-
));
184+
.api((request) => request.wpUserDelete());
185+
```
186+
187+
#### WooCommerce - Register
188+
- Used to register a user
189+
190+
``` dart
191+
WPUserRegisterResponse wpUserRegisterResponse = await WPJsonAPI.instance
192+
.api((request) => request.wpRegister(
193+
email: email,
194+
password: password
195+
));
193196
```
194197

195198
#### WooCommerce - Get users info in WooCommerce
196199
- Used to get WooCommerce info for a given user
197-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
200+
- After you login/register, you can all this method to get the users WooCommerce info
198201

199202
``` dart
200203
WCCustomerInfoResponse wcCustomerInfoResponse = await WPJsonAPI.instance
@@ -206,12 +209,11 @@ WCCustomerInfoResponse wcCustomerInfoResponse = await WPJsonAPI.instance
206209
#### WooCommerce - Update users info in WooCommerce
207210
- Used to update a users WooCommerce details
208211
- All the parameter are optional so if you wanted to just update the name, you could just add first_name and last_name
209-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
212+
- After you login/register, you can all this method to update the users WooCommerce info
210213

211214
``` dart
212215
WCCustomerUpdatedResponse wcCustomerUpdatedResponse = await WPJsonAPI.instance
213216
.api((request) => request.wcUpdateCustomerInfo(
214-
userToken,
215217
firstName: firstName,
216218
lastName: lastName,
217219
displayName: displayName,
@@ -242,23 +244,20 @@ WCCustomerUpdatedResponse wcCustomerUpdatedResponse = await WPJsonAPI.instance
242244

243245
#### WooCommerce Points and Rewards - Get user's points
244246
- This is used to get the user's current points in the [WooCommerce Points and Rewards](https://woo.com/products/woocommerce-points-and-rewards/) plugin
245-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
247+
- After you login/register, you can all this method to get the users points
246248

247249
``` dart
248250
WcPointsAndRewardUser wcPointsAndRewardUser = await WPJsonAPI.instance
249-
.api((request) => request.wcPointsAndRewardsUser(
250-
userToken
251-
));
251+
.api((request) => request.wcPointsAndRewardsUser());
252252
```
253253

254254
#### WooCommerce Points and Rewards - Calculate the value of points
255255
- This is used to calculate the value of points in the [WooCommerce Points and Rewards](https://woo.com/products/woocommerce-points-and-rewards/) plugin
256-
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
256+
- After you login/register, you can all this method to calculate the value of points
257257

258258
``` dart
259259
WcPointsAndRewardCalculatePoints wcPointsAndRewardsCalculatePoints = await WPJsonAPI.instance
260260
.api((request) => request.wcPointsAndRewardsCalculatePoints(
261-
userToken,
262261
points: 100
263262
));
264263
```
@@ -270,6 +269,6 @@ For help getting started with WooSignal, view our
270269
To use this plugin, add `wp_json_api` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).
271270

272271
## Note
273-
Install our WordPress plugin "[WP JSON API](https://woosignal.com/plugins/wordpress/wp-json-api)" v3.3.2 to use this flutter plugin.
272+
Install our WordPress plugin "[WP JSON API](https://woosignal.com/plugins/wordpress/wp-json-api)" v3.4.0 to use this flutter plugin.
274273

275274
Disclaimer: This plugin is not affiliated with or supported by Automattic, Inc. All logos and trademarks are the property of their respective owners.

example/main.dart

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class MyHomePage extends StatefulWidget {
2929
}
3030

3131
class _MyHomePageState extends State<MyHomePage> {
32-
late TextEditingController _tfEmailController;
33-
late TextEditingController _tfPasswordController;
32+
TextEditingController _tfEmailController = TextEditingController();
33+
TextEditingController _tfPasswordController = TextEditingController();
3434

3535
@override
3636
void initState() {
@@ -40,10 +40,7 @@ class _MyHomePageState extends State<MyHomePage> {
4040
// FIRST ON YOUR WORDPRESS STORE
4141
// LINK https://woosignal.com/plugins/wordpress/wp-json-api
4242

43-
WPJsonAPI.instance.initWith(baseUrl: "http://mysite.com");
44-
45-
_tfEmailController = TextEditingController();
46-
_tfPasswordController = TextEditingController();
43+
WPJsonAPI.instance.init(baseUrl: "http://mysite.com");
4744
}
4845

4946
_login() async {
@@ -60,24 +57,23 @@ class _MyHomePageState extends State<MyHomePage> {
6057
print(e);
6158
}
6259

63-
if (wpUserLoginResponse != null) {
64-
print(wpUserLoginResponse.data?.userToken);
65-
print(wpUserLoginResponse.data?.userId);
66-
67-
// GET USER INFO
68-
WPUserInfoResponse? wpUserInfoResponse =
69-
await WPJsonAPI.instance.api((request) {
70-
return request.wpGetUserInfo(wpUserLoginResponse!.data!.userToken!);
71-
});
72-
73-
if (wpUserInfoResponse != null) {
74-
print(wpUserInfoResponse.data?.firstName);
75-
print(wpUserInfoResponse.data?.lastName);
76-
} else {
77-
print("something went wrong");
78-
}
79-
} else {
60+
if (wpUserLoginResponse == null) {
8061
print("invalid login details");
62+
return;
63+
}
64+
65+
print(wpUserLoginResponse.data?.userToken);
66+
print(wpUserLoginResponse.data?.userId);
67+
68+
// GET USER INFO
69+
WPUserInfoResponse? wpUserInfoResponse =
70+
await WPJsonAPI.instance.api((request) => request.wpGetUserInfo());
71+
72+
if (wpUserInfoResponse != null) {
73+
print(wpUserInfoResponse.data?.firstName);
74+
print(wpUserInfoResponse.data?.lastName);
75+
} else {
76+
print("something went wrong");
8177
}
8278
}
8379

0 commit comments

Comments
 (0)