Skip to content

Commit 2517d6e

Browse files
committed
-- release v0.1.2
2 parents 4f36d3c + 356bb7f commit 2517d6e

29 files changed

Lines changed: 1283 additions & 653 deletions

.idea/workspace.xml

Lines changed: 208 additions & 155 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This library uses [WordPress REST API V2](https://developer.wordpress.org/rest-a
55
## Requirements
66
For authentication and usage of administrator level REST APIs, you need to use either of the two popular authentication plugins in your WordPress site:
77
1. [Application Passwords](https://wordpress.org/plugins/application-passwords/)
8-
2. [JWT Authentication for WP REST API](https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/)
8+
2. [JWT Authentication for WP REST API](https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/) <strong>(recommended)</strong>
99

1010
## Getting Started
1111

@@ -22,68 +22,114 @@ wp.WordPress wordPress;
2222
2323
// adminName and adminKey is needed only for admin level APIs
2424
wordPress = wp.WordPress(
25-
baseUrl: 'http://localhost',
26-
authenticator: wp.WordPressAuthenticator.ApplicationPasswords,
27-
adminName: '',
28-
adminKey: '',
25+
baseUrl: 'http://localhost',
26+
authenticator: wp.WordPressAuthenticator.JWT,
27+
adminName: '',
28+
adminKey: '',
2929
);
3030
```
3131

3232
### 3. Authenticate User
3333

3434
```dart
3535
Future<wp.User> response = wordPress.authenticateUser(
36-
username: 'username',
37-
password: 'password',
38-
);
36+
username: 'ChiefEditor',
37+
password: 'chiefeditor@123',
38+
);
3939
4040
response.then((user) {
41-
print(user.toString());
41+
createPost(user);
4242
}).catchError((err) {
43-
print(err.toString());
43+
print('Failed to fetch user: $err');
4444
});
4545
```
4646

4747
### 4. Fetch Posts
4848

4949
```dart
5050
Future<List<wp.Post>> posts = wordPress.fetchPosts(
51-
params: wp.ParamsPostList(
52-
context: wp.WordPressContext.view,
53-
pageNum: 1,
54-
perPage: 20,
55-
order: wp.Order.desc,
56-
orderBy: wp.PostsOrderBy.date,
57-
),
58-
);
51+
params: wp.ParamsPostList(
52+
context: wp.WordPressContext.view,
53+
pageNum: 1,
54+
perPage: 20,
55+
order: wp.Order.desc,
56+
orderBy: wp.PostsOrderBy.date,
57+
),
58+
);
5959
```
6060

6161
### 5. Fetch Users
6262

6363
```dart
6464
Future<List<wp.User>> users = wordPress.fetchUsers(
65-
params: wp.ParamsUserList(
66-
context: wp.WordPressContext.view,
67-
pageNum: 1,
68-
perPage: 30,
69-
order: wp.Order.asc,
70-
orderBy: wp.UsersOrderBy.name,
71-
role: wp.UserRole.subscriber,
72-
),
73-
);
65+
params: wp.ParamsUserList(
66+
context: wp.WordPressContext.view,
67+
pageNum: 1,
68+
perPage: 30,
69+
order: wp.Order.asc,
70+
orderBy: wp.UsersOrderBy.name,
71+
role: wp.UserRole.subscriber,
72+
),
73+
);
7474
```
7575

7676
### 6. Fetch Comments
7777

7878
```dart
7979
Future<List<wp.Comment>> comments = wordPress.fetchComments(
80-
params: wp.ParamsCommentList(
81-
context: wp.WordPressContext.view,
82-
pageNum: 1,
83-
perPage: 30,
84-
includePostIDs: [1],
85-
),
86-
);
80+
params: wp.ParamsCommentList(
81+
context: wp.WordPressContext.view,
82+
pageNum: 1,
83+
perPage: 30,
84+
includePostIDs: [1],
85+
),
86+
);
87+
```
88+
### 7. Create Post
89+
90+
```dart
91+
void createPost(wp.User user) {
92+
final post = wordPress.createPost(
93+
post: new wp.Post(
94+
title: 'First post as a Chief Editor',
95+
content: 'Blah! blah! blah!',
96+
excerpt: 'Discussion about blah!',
97+
author: user.id,
98+
commentStatus: wp.PostCommentStatus.open,
99+
pingStatus: wp.PostPingStatus.closed,
100+
status: wp.PostPageStatus.publish,
101+
format: wp.PostFormat.standard,
102+
sticky: true,
103+
),
104+
);
105+
106+
post.then((p) {
107+
print('Post created successfully with ID: ${p.id}');
108+
postComment(user, p);
109+
}).catchError((err) {
110+
print('Failed to create post: $err');
111+
});
112+
}
113+
```
114+
### 8. Post Comment
115+
116+
```dart
117+
void postComment(wp.User user, wp.Post post) {
118+
final comment = wordPress.createComment(
119+
comment: new wp.Comment(
120+
author: user.id,
121+
post: post.id,
122+
content: "First!",
123+
parent: 0,
124+
),
125+
);
126+
127+
comment.then((c) {
128+
print('Comment successfully posted with ID: ${c.id}');
129+
}).catchError((err) {
130+
print('Failed to comment: $err');
131+
});
132+
}
87133
```
88134

89135
## Future Work

example/lib/main.dart

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,34 @@ class PostsBuilderState extends State<PostsBuilder> {
6161
try {
6262
wordPress = wp.WordPress(
6363
baseUrl: 'http://192.168.6.165',
64-
authenticator: wp.WordPressAuthenticator.ApplicationPasswords,
65-
adminName: 'admin',
66-
adminKey: 'EOjD JsYA hKfM RHNI vufW hyUX',
64+
authenticator: wp.WordPressAuthenticator.JWT,
65+
// adminName: 'admin',
66+
// adminKey: 'EOjD JsYA hKfM RHNI vufW hyUX',
6767
);
6868
} catch (err) {
6969
print(err.toString());
7070
}
7171

72-
/* Future<wp.User> response = wordPress.authenticateUser(
73-
username: 'username',
74-
password: 'password',
72+
Future<wp.User> response = wordPress.authenticateUser(
73+
username: 'ChiefEditor',
74+
password: 'chiefeditor@123',
7575
);
7676

7777
response.then((user) {
78-
print(user.toString());
78+
createPost(user);
7979
}).catchError((err) {
80-
print(err.toString());
81-
});*/
80+
print('Failed to fetch user: $err');
81+
});
8282

83-
Future<List<wp.User>> users = wordPress.fetchUsers(
83+
fetchPosts();
84+
85+
/*Future<List<wp.User>> users = wordPress.fetchUsers(
8486
params: wp.ParamsUserList(
8587
context: wp.WordPressContext.view,
8688
pageNum: 1,
8789
perPage: 30,
8890
order: wp.Order.asc,
89-
orderBy: wp.UsersOrderBy.name,
91+
orderBy: wp.UserOrderBy.name,
9092
),
9193
);
9294
@@ -109,6 +111,73 @@ class PostsBuilderState extends State<PostsBuilder> {
109111
}).catchError((err) {
110112
print(err.toString());
111113
});
114+
115+
Future<List<wp.Page>> pages = wordPress.fetchPages(
116+
params: wp.ParamsPageList(),
117+
);
118+
pages.then((response) {
119+
print(response);
120+
}).catchError((err) {
121+
print(err.toString());
122+
});
123+
124+
Future<List<wp.Category>> categories = wordPress.fetchCategories(
125+
params: wp.ParamsCategoryList(),
126+
);
127+
categories.then((response) {
128+
print(response);
129+
}).catchError((err) {
130+
print(err.toString());
131+
});
132+
133+
Future<List<wp.Tag>> tags = wordPress.fetchTags(
134+
params: wp.ParamsTagList(),
135+
);
136+
tags.then((response) {
137+
print(response);
138+
}).catchError((err) {
139+
print(err.toString());
140+
});*/
141+
}
142+
143+
void createPost(wp.User user) {
144+
final post = wordPress.createPost(
145+
post: new wp.Post(
146+
title: 'First post as a Chief Editor',
147+
content: 'Blah! blah! blah!',
148+
excerpt: 'Discussion about blah!',
149+
author: user.id,
150+
commentStatus: wp.PostCommentStatus.open,
151+
pingStatus: wp.PostPingStatus.closed,
152+
status: wp.PostPageStatus.publish,
153+
format: wp.PostFormat.standard,
154+
sticky: true,
155+
),
156+
);
157+
158+
post.then((p) {
159+
print('Post created successfully with ID: ${p.id}');
160+
postComment(user, p);
161+
}).catchError((err) {
162+
print('Failed to create post: $err');
163+
});
164+
}
165+
166+
void postComment(wp.User user, wp.Post post) {
167+
final comment = wordPress.createComment(
168+
comment: new wp.Comment(
169+
author: user.id,
170+
post: post.id,
171+
content: "First!",
172+
parent: 0,
173+
),
174+
);
175+
176+
comment.then((c) {
177+
print('Comment successfully posted with ID: ${c.id}');
178+
}).catchError((err) {
179+
print('Failed to comment: $err');
180+
});
112181
}
113182

114183
void fetchPosts() {

lib/constants.dart

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
const URL_WP_BASE = '/wp-json/wp/v2';
21
const URL_JWT_BASE = '/wp-json/jwt-auth/v1';
2+
const URL_WP_BASE = '/wp-json/wp/v2';
33

44
const URL_JWT_TOKEN = '$URL_JWT_BASE/token';
55
const URL_JWT_TOKEN_VALIDATE = '$URL_JWT_BASE/token/validate';
66

7+
const URL_CATEGORIES = '$URL_WP_BASE/categories';
8+
const URL_COMMENTS = '$URL_WP_BASE/comments';
9+
const URL_PAGES = '$URL_WP_BASE/pages';
710
const URL_POSTS = '$URL_WP_BASE/posts';
11+
const URL_TAGS = '$URL_WP_BASE/tags';
812
const URL_USERS = '$URL_WP_BASE/users';
9-
const URL_COMMENTS = '$URL_WP_BASE/comments';
1013

1114
enum WordPressAuthenticator {
1215
JWT,
@@ -19,7 +22,7 @@ enum Order {
1922
desc,
2023
}
2124

22-
enum PostsOrderBy {
25+
enum PostOrderBy {
2326
author,
2427
date,
2528
id,
@@ -30,7 +33,7 @@ enum PostsOrderBy {
3033
slug,
3134
title,
3235
}
33-
enum PostStatus {
36+
enum PostPageStatus {
3437
publish,
3538
future,
3639
draft,
@@ -45,7 +48,7 @@ enum PostPingStatus {
4548
open,
4649
closed,
4750
}
48-
enum ObjectFormat {
51+
enum PostFormat {
4952
standard,
5053
aside,
5154
chat,
@@ -58,7 +61,7 @@ enum ObjectFormat {
5861
audio,
5962
}
6063

61-
enum UsersOrderBy {
64+
enum UserOrderBy {
6265
id,
6366
include,
6467
name,
@@ -75,7 +78,7 @@ enum UserRole {
7578
administrator,
7679
}
7780

78-
enum CommentsOrderBy {
81+
enum CommentOrderBy {
7982
date,
8083
date_gmt,
8184
id,
@@ -96,6 +99,29 @@ enum CommentType {
9699
//TODO: Add all comment types
97100
}
98101

102+
enum CategoryTagOrderBy {
103+
id,
104+
include,
105+
name,
106+
slug,
107+
term_group,
108+
description,
109+
count,
110+
}
111+
112+
enum PageOrderBy {
113+
author,
114+
date,
115+
id,
116+
include,
117+
modified,
118+
parent,
119+
relevance,
120+
slug,
121+
title,
122+
menu_order,
123+
}
124+
99125
/// Converts an enum string to enum value name.
100126
String enumStringToName(String enumString) {
101127
return enumString.split('.')[1];

0 commit comments

Comments
 (0)