项目作者: dreamsoftin

项目描述 :
Flutter WordPress API
高级语言: Dart
项目地址: git://github.com/dreamsoftin/flutter_wordpress.git
创建时间: 2019-02-11T05:30:18Z
项目社区:https://github.com/dreamsoftin/flutter_wordpress

开源协议:MIT License

下载


Flutter Wordpress

pub.dev

This library uses WordPress REST API V2 to provide a way for your application to interact with your WordPress website.

Tutorial - by Ritesh Sharma

Screenshots

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
  2. JWT Authentication for WP REST API (recommended)

Getting Started

1. Import library

First:

Find your pubspec.yaml in the root of your project and add flutter_wordpress: ^0.2.0 under dependencies:

Second:

  1. import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;

2. Instantiate WordPress class

  1. wp.WordPress wordPress;
  2. // adminName and adminKey is needed only for admin level APIs
  3. wordPress = wp.WordPress(
  4. baseUrl: 'http://localhost',
  5. authenticator: wp.WordPressAuthenticator.JWT,
  6. adminName: '',
  7. adminKey: '',
  8. );

3. Authenticate User

  1. Future<wp.User> response = wordPress.authenticateUser(
  2. username: 'ChiefEditor',
  3. password: 'chiefeditor@123',
  4. );
  5. response.then((user) {
  6. createPost(user);
  7. }).catchError((err) {
  8. print('Failed to fetch user: $err');
  9. });

4. Fetch Posts

  1. Future<List<wp.Post>> posts = wordPress.fetchPosts(
  2. postParams: wp.ParamsPostList(
  3. context: wp.WordPressContext.view,
  4. pageNum: 1,
  5. perPage: 20,
  6. order: wp.Order.desc,
  7. orderBy: wp.PostOrderBy.date,
  8. ),
  9. fetchAuthor: true,
  10. fetchFeaturedMedia: true,
  11. fetchComments: true,
  12. postType: 'post'
  13. );

5. Fetch Users

  1. Future<List<wp.User>> users = wordPress.fetchUsers(
  2. params: wp.ParamsUserList(
  3. context: wp.WordPressContext.view,
  4. pageNum: 1,
  5. perPage: 30,
  6. order: wp.Order.asc,
  7. orderBy: wp.UsersOrderBy.name,
  8. roles: ['subscriber'],
  9. ),
  10. );

6. Fetch Comments

  1. Future<List<wp.Comment>> comments = wordPress.fetchComments(
  2. params: wp.ParamsCommentList(
  3. context: wp.WordPressContext.view,
  4. pageNum: 1,
  5. perPage: 30,
  6. includePostIDs: [1],
  7. ),
  8. );

7. Create User

  1. Future<void> createUser({@required String email, @required String username, @required String password, @required List<String> roles}) async {
  2. await widget.wordPress.createUser(
  3. user: wp.User(
  4. email: email,
  5. password: password,
  6. username: username,
  7. roles: roles
  8. )
  9. ).then((p) {
  10. print('User created successfully ${p}');
  11. }).catchError((err) {
  12. print('Failed to create user: $err');
  13. });
  14. }

8. Create Post

  1. void createPost({@required wp.User user}) {
  2. final post = widget.wordPress.createPost(
  3. post: new wp.Post(
  4. title: 'First post as a Chief Editor',
  5. content: 'Blah! blah! blah!',
  6. excerpt: 'Discussion about blah!',
  7. authorID: user.id,
  8. commentStatus: wp.PostCommentStatus.open,
  9. pingStatus: wp.PostPingStatus.closed,
  10. status: wp.PostPageStatus.publish,
  11. format: wp.PostFormat.standard,
  12. sticky: true,
  13. ),
  14. );
  15. post.then((p) {
  16. print('Post created successfully with ID: ${p.id}');
  17. }).catchError((err) {
  18. print('Failed to create post: $err');
  19. });
  20. }

9. create Comment

  1. void createComment({@required int userId, @required int postId}) {
  2. final comment = widget.wordPress.createComment(
  3. comment: new wp.Comment(
  4. author: userId,
  5. post: postId,
  6. content: "First!",
  7. parent: 0,
  8. ),
  9. );
  10. comment.then((c) {
  11. print('Comment successfully posted with ID: ${c.id}');
  12. }).catchError((err) {
  13. print('Failed to comment: $err');
  14. });
  15. }

10. Update Comment

  1. Future<void> updateComment({@required int id, @required int postId, @required wp.User user}) async {
  2. await widget.wordPress.updateComment(
  3. comment: new wp.Comment(
  4. content: "Comment Updated2!",
  5. author: user.id,
  6. post: postId,
  7. ),
  8. id: id,
  9. ).then((c) {
  10. print('Comment updated successfully "$c"');
  11. }).catchError((err) {
  12. print('Failed to update Comment: $err');
  13. });
  14. }

11. Update Post

  1. Future<void> updatePost({@required int id, @required int userId}) async {
  2. await widget.wordPress.updatePost(
  3. post: new wp.Post(
  4. title: 'First post as a Chief Editor',
  5. content: 'Blah! blah! blah!',
  6. excerpt: 'Discussion about blah!',
  7. authorID: userId,
  8. commentStatus: wp.PostCommentStatus.open,
  9. pingStatus: wp.PostPingStatus.closed,
  10. status: wp.PostPageStatus.publish,
  11. format: wp.PostFormat.standard,
  12. sticky: true,
  13. ),
  14. id: id, //
  15. ).then((p) {
  16. print('Post updated successfully with ID ${p}');
  17. }).catchError((err) {
  18. print('Failed to update post: $err');
  19. });
  20. }

12. Update User

  1. Future<void> updateUser({@required int id, @required String username, @required String email}) async {
  2. await widget.wordPress.updateUser(
  3. user: new wp.User(
  4. description: "This is description for this user",
  5. username: username,
  6. id: id,
  7. email: email
  8. ),
  9. id: id,
  10. ).then((u) {
  11. print('User updated successfully $u');
  12. }).catchError((err) {
  13. print('Failed to update User: $err');
  14. });
  15. }

13. Delete Comment

  1. Future<void> deleteComment({@required int id}) async {
  2. await widget.wordPress.deleteComment(id: id).then((c) {
  3. print('Comment Deleted successfully: $c');
  4. }).catchError((err) {
  5. print('Failed to Delete comment: $err');
  6. });
  7. }

14. Delete Post

  1. Future<void> deletePost({@required int id}) async {
  2. await widget.wordPress.deletePost(id: id).then((p) {
  3. print('Post Deleted successfully: $p');
  4. }).catchError((err) {
  5. print('Failed to Delete post: $err');
  6. });
  7. }

15. Delete User

  1. Future<void> deleteUser({@required int id, @required int reassign}) async {
  2. await widget.wordPress.deleteUser(id: id, reassign: reassign).then((u) {
  3. print('User Deleted successfully: $u');
  4. }).catchError((err) {
  5. print('Failed to Delete user: $err');
  6. });
  7. }

16. Upload Media

  1. uploadMedia(File image) async {
  2. var media = await wordPress.uploadMedia(image).then((m) {
  3. print('Media uploaded successfully: $m');
  4. }).catchError((err) {
  5. print('Failed to upload Media: $err');
  6. });
  7. int mediaID = media['id'];
  8. }

Future Work

  1. Implementing OAuth 2.0 authentication.

Contributors