Skip to content

Commit a71312a

Browse files
Add fetching categories, tags. Add creating posts, comments.
2 parents 0056a12 + fb5cbc4 commit a71312a

15 files changed

Lines changed: 719 additions & 428 deletions

.idea/workspace.xml

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

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [0.1.1] - Added Models
2+
3+
* Update README.md. Add fetch users list, fetch comments list.
4+
* Implemented authorization function and fetching of Posts with parameters
5+
16
## [0.1.0] - Added Models
27

38
* Created model classes for Wordpress REST API and implemented user authentication function using JWT authentication system.

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: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,28 @@ 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: 'ChiefEditor',
74+
password: 'chiefeditor@123',
75+
);
76+
77+
response.then((user) {
78+
createPost(user);
79+
}).catchError((err) {
80+
print('Failed to fetch user: $err');
81+
});
82+
7283
fetchPosts();
7384

74-
Future<List<wp.User>> users = wordPress.fetchUsers(
85+
/*Future<List<wp.User>> users = wordPress.fetchUsers(
7586
params: wp.ParamsUserList(
7687
context: wp.WordPressContext.view,
7788
pageNum: 1,
@@ -126,6 +137,46 @@ class PostsBuilderState extends State<PostsBuilder> {
126137
print(response);
127138
}).catchError((err) {
128139
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');
129180
});
130181
}
131182

@@ -149,8 +200,6 @@ class PostsBuilderState extends State<PostsBuilder> {
149200
if (snapshot.hasData) {
150201
return ListView.separated(
151202
itemBuilder: (context, i) {
152-
print(
153-
"Post Status: ${snapshot.data[i].status} ${snapshot.data[i].format} ${snapshot.data[i].commentStatus} ${snapshot.data[i].pingStatus}");
154203
return Column(
155204
crossAxisAlignment: CrossAxisAlignment.start,
156205
children: <Widget>[

0 commit comments

Comments
 (0)