Skip to content

Commit

Permalink
-- release v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
SachinGanesh committed Feb 16, 2019
2 parents 4f36d3c + 356bb7f commit 2517d6e
Show file tree
Hide file tree
Showing 29 changed files with 1,283 additions and 653 deletions.
363 changes: 208 additions & 155 deletions .idea/workspace.xml

Large diffs are not rendered by default.

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

## Getting Started

Expand All @@ -22,68 +22,114 @@ wp.WordPress wordPress;
// adminName and adminKey is needed only for admin level APIs
wordPress = wp.WordPress(
baseUrl: 'http://localhost',
authenticator: wp.WordPressAuthenticator.ApplicationPasswords,
adminName: '',
adminKey: '',
baseUrl: 'http://localhost',
authenticator: wp.WordPressAuthenticator.JWT,
adminName: '',
adminKey: '',
);
```

### 3. Authenticate User

```dart
Future<wp.User> response = wordPress.authenticateUser(
username: 'username',
password: 'password',
);
username: 'ChiefEditor',
password: 'chiefeditor@123',
);
response.then((user) {
print(user.toString());
createPost(user);
}).catchError((err) {
print(err.toString());
print('Failed to fetch user: $err');
});
```

### 4. Fetch Posts

```dart
Future<List<wp.Post>> posts = wordPress.fetchPosts(
params: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 20,
order: wp.Order.desc,
orderBy: wp.PostsOrderBy.date,
),
);
params: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 20,
order: wp.Order.desc,
orderBy: wp.PostsOrderBy.date,
),
);
```

### 5. Fetch Users

```dart
Future<List<wp.User>> users = wordPress.fetchUsers(
params: wp.ParamsUserList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
order: wp.Order.asc,
orderBy: wp.UsersOrderBy.name,
role: wp.UserRole.subscriber,
),
);
params: wp.ParamsUserList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
order: wp.Order.asc,
orderBy: wp.UsersOrderBy.name,
role: wp.UserRole.subscriber,
),
);
```

### 6. Fetch Comments

```dart
Future<List<wp.Comment>> comments = wordPress.fetchComments(
params: wp.ParamsCommentList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
includePostIDs: [1],
),
);
params: wp.ParamsCommentList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
includePostIDs: [1],
),
);
```
### 7. Create Post

```dart
void createPost(wp.User user) {
final post = wordPress.createPost(
post: new wp.Post(
title: 'First post as a Chief Editor',
content: 'Blah! blah! blah!',
excerpt: 'Discussion about blah!',
author: user.id,
commentStatus: wp.PostCommentStatus.open,
pingStatus: wp.PostPingStatus.closed,
status: wp.PostPageStatus.publish,
format: wp.PostFormat.standard,
sticky: true,
),
);
post.then((p) {
print('Post created successfully with ID: ${p.id}');
postComment(user, p);
}).catchError((err) {
print('Failed to create post: $err');
});
}
```
### 8. Post Comment

```dart
void postComment(wp.User user, wp.Post post) {
final comment = wordPress.createComment(
comment: new wp.Comment(
author: user.id,
post: post.id,
content: "First!",
parent: 0,
),
);
comment.then((c) {
print('Comment successfully posted with ID: ${c.id}');
}).catchError((err) {
print('Failed to comment: $err');
});
}
```

## Future Work
Expand Down
91 changes: 80 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,34 @@ class PostsBuilderState extends State<PostsBuilder> {
try {
wordPress = wp.WordPress(
baseUrl: 'http://192.168.6.165',
authenticator: wp.WordPressAuthenticator.ApplicationPasswords,
adminName: 'admin',
adminKey: 'EOjD JsYA hKfM RHNI vufW hyUX',
authenticator: wp.WordPressAuthenticator.JWT,
// adminName: 'admin',
// adminKey: 'EOjD JsYA hKfM RHNI vufW hyUX',
);
} catch (err) {
print(err.toString());
}

/* Future<wp.User> response = wordPress.authenticateUser(
username: 'username',
password: 'password',
Future<wp.User> response = wordPress.authenticateUser(
username: 'ChiefEditor',
password: 'chiefeditor@123',
);

response.then((user) {
print(user.toString());
createPost(user);
}).catchError((err) {
print(err.toString());
});*/
print('Failed to fetch user: $err');
});

Future<List<wp.User>> users = wordPress.fetchUsers(
fetchPosts();

/*Future<List<wp.User>> users = wordPress.fetchUsers(
params: wp.ParamsUserList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
order: wp.Order.asc,
orderBy: wp.UsersOrderBy.name,
orderBy: wp.UserOrderBy.name,
),
);
Expand All @@ -109,6 +111,73 @@ class PostsBuilderState extends State<PostsBuilder> {
}).catchError((err) {
print(err.toString());
});
Future<List<wp.Page>> pages = wordPress.fetchPages(
params: wp.ParamsPageList(),
);
pages.then((response) {
print(response);
}).catchError((err) {
print(err.toString());
});
Future<List<wp.Category>> categories = wordPress.fetchCategories(
params: wp.ParamsCategoryList(),
);
categories.then((response) {
print(response);
}).catchError((err) {
print(err.toString());
});
Future<List<wp.Tag>> tags = wordPress.fetchTags(
params: wp.ParamsTagList(),
);
tags.then((response) {
print(response);
}).catchError((err) {
print(err.toString());
});*/
}

void createPost(wp.User user) {
final post = wordPress.createPost(
post: new wp.Post(
title: 'First post as a Chief Editor',
content: 'Blah! blah! blah!',
excerpt: 'Discussion about blah!',
author: user.id,
commentStatus: wp.PostCommentStatus.open,
pingStatus: wp.PostPingStatus.closed,
status: wp.PostPageStatus.publish,
format: wp.PostFormat.standard,
sticky: true,
),
);

post.then((p) {
print('Post created successfully with ID: ${p.id}');
postComment(user, p);
}).catchError((err) {
print('Failed to create post: $err');
});
}

void postComment(wp.User user, wp.Post post) {
final comment = wordPress.createComment(
comment: new wp.Comment(
author: user.id,
post: post.id,
content: "First!",
parent: 0,
),
);

comment.then((c) {
print('Comment successfully posted with ID: ${c.id}');
}).catchError((err) {
print('Failed to comment: $err');
});
}

void fetchPosts() {
Expand Down
40 changes: 33 additions & 7 deletions lib/constants.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const URL_WP_BASE = '/wp-json/wp/v2';
const URL_JWT_BASE = '/wp-json/jwt-auth/v1';
const URL_WP_BASE = '/wp-json/wp/v2';

const URL_JWT_TOKEN = '$URL_JWT_BASE/token';
const URL_JWT_TOKEN_VALIDATE = '$URL_JWT_BASE/token/validate';

const URL_CATEGORIES = '$URL_WP_BASE/categories';
const URL_COMMENTS = '$URL_WP_BASE/comments';
const URL_PAGES = '$URL_WP_BASE/pages';
const URL_POSTS = '$URL_WP_BASE/posts';
const URL_TAGS = '$URL_WP_BASE/tags';
const URL_USERS = '$URL_WP_BASE/users';
const URL_COMMENTS = '$URL_WP_BASE/comments';

enum WordPressAuthenticator {
JWT,
Expand All @@ -19,7 +22,7 @@ enum Order {
desc,
}

enum PostsOrderBy {
enum PostOrderBy {
author,
date,
id,
Expand All @@ -30,7 +33,7 @@ enum PostsOrderBy {
slug,
title,
}
enum PostStatus {
enum PostPageStatus {
publish,
future,
draft,
Expand All @@ -45,7 +48,7 @@ enum PostPingStatus {
open,
closed,
}
enum ObjectFormat {
enum PostFormat {
standard,
aside,
chat,
Expand All @@ -58,7 +61,7 @@ enum ObjectFormat {
audio,
}

enum UsersOrderBy {
enum UserOrderBy {
id,
include,
name,
Expand All @@ -75,7 +78,7 @@ enum UserRole {
administrator,
}

enum CommentsOrderBy {
enum CommentOrderBy {
date,
date_gmt,
id,
Expand All @@ -96,6 +99,29 @@ enum CommentType {
//TODO: Add all comment types
}

enum CategoryTagOrderBy {
id,
include,
name,
slug,
term_group,
description,
count,
}

enum PageOrderBy {
author,
date,
id,
include,
modified,
parent,
relevance,
slug,
title,
menu_order,
}

/// Converts an enum string to enum value name.
String enumStringToName(String enumString) {
return enumString.split('.')[1];
Expand Down
Loading

0 comments on commit 2517d6e

Please sign in to comment.