Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wp create user #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line states this file shouldn't be version controlled.

export "FLUTTER_ROOT=/Users/sachin/Documents/devtools/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/sachin/Projects/2019/Flutter/Plugins/flutter_wordpress/example"
export "FLUTTER_TARGET=/Users/sachin/Projects/2019/Flutter/Plugins/flutter_wordpress/example/lib/main.dart"
export "FLUTTER_ROOT=/Users/afiqhamdan/Development/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/afiqhamdan/Desktop/flutter_wordpress/example"
export "FLUTTER_TARGET=/Users/afiqhamdan/Desktop/flutter_wordpress/example/lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
export "FLUTTER_FRAMEWORK_DIR=/Users/sachin/Documents/devtools/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_FRAMEWORK_DIR=/Users/afiqhamdan/Development/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "TRACK_WIDGET_CREATION=true"
21 changes: 18 additions & 3 deletions example/lib/display_posts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,41 @@ class PostsBuilderState extends State<PostsBuilder> {

// yahya

Future<void> createUser({@required String email, @required String username, @required String password, @required List<String> roles}) async {
Future<void> createUser({@required String email, @required String username, @required String password, List<String> roles}) async {

await widget.wordPress.createUser(
user: wp.User(
email: email,
password: password,
username: username,
roles: roles
// roles: roles
)
).then((p) {
print('User created successfully ${p}');
}).catchError((err) {
print('Failed to create user: $err');
});

// await widget.wordPress.createUser2(
// user: wp.User(
// email: email,
// password: password,
// username: username,
// roles: roles
// )
// ).then((p) {
// print('User created successfully ${p}');
// }).catchError((err) {
// print('Failed to create user: $err');
// });
}

// =====================
// UPDATE START
// =====================

Future<void> updatePost({@required int id, @required int userId}) async {

await widget.wordPress.updatePost(
post: new wp.Post(
title: 'First post as a Chief Editor',
Expand Down Expand Up @@ -361,7 +376,7 @@ class PostsBuilderState extends State<PostsBuilder> {
RaisedButton.icon(
color: Colors.blueAccent,
onPressed: () {
createUser(roles: ["subscriber"], username: "myUserName", password: "123", email: "[email protected]");
createUser(username: "myUserName", password: "123", email: "[email protected]");
},
icon: Icon(Icons.add_circle, color: Colors.white,),
label: Text(
Expand Down
2 changes: 1 addition & 1 deletion example/lib/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class LoginFieldsState extends State<LoginFields> {
});

wp.WordPress wordPress = new wp.WordPress(
baseUrl: 'YOUR WEBSITE URL',
baseUrl: '',
authenticator: wp.WordPressAuthenticator.JWT,
adminName: '',
adminKey: '',
Expand Down
43 changes: 19 additions & 24 deletions lib/flutter_wordpress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ class WordPress {
bool fetchAttachments = false,
String postType = "posts",
}) async {
final StringBuffer url = new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType);
final StringBuffer url =
new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType);

url.write(postParams.toString());

Expand Down Expand Up @@ -651,8 +652,8 @@ class WordPress {
}
}

// yahya - @mymakarim

// uploadMedia function added by: @GarvMaggu
async.Future<dynamic> uploadMedia(File image) async {
final StringBuffer url = new StringBuffer(_baseUrl + URL_MEDIA);
var file = image.readAsBytesSync();
Expand All @@ -679,33 +680,27 @@ class WordPress {
}
}

// uploadMedia function added by: @GarvMaggu

async.Future<bool> createUser({@required User user}) async {
// createUser fix function by: @afiq90
async.Future<User> createUser({@required User user}) async {
final StringBuffer url = new StringBuffer(_baseUrl + URL_USERS);

HttpClient httpClient = new HttpClient();
HttpClientRequest request =
await httpClient.postUrl(Uri.parse(url.toString()));
request.headers
.set(HttpHeaders.contentTypeHeader, "application/json; charset=UTF-8");
request.headers.set(HttpHeaders.acceptHeader, "application/json");
request.headers.set('Authorization', "${_urlHeader['Authorization']}");

request.add(utf8.encode(json.encode(user.toJson())));
HttpClientResponse response = await request.close();
final response = await http.post(
url.toString(),
headers: _urlHeader,
body: user.toJson(),
);

if (response.statusCode >= 200 && response.statusCode < 300) {
return true;
return User.fromJson(json.decode(response.body));
} else {
response.transform(utf8.decoder).listen((contents) {
try {
WordPressError err = WordPressError.fromJson(json.decode(contents));
throw err;
} catch (e) {
throw new WordPressError(message: contents);
}
});
try {
WordPressError err =
WordPressError.fromJson(json.decode(response.body));
throw err;
} catch (e) {
throw new WordPressError(message: response.body);
}
}
}

Expand Down
50 changes: 31 additions & 19 deletions lib/schemas/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,27 @@ class User {

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['username'] = this.username;
data['name'] = this.name;
data['first_name'] = this.firstName;
data['last_name'] = this.lastName;
data['email'] = this.email;
data['url'] = this.url;
data['description'] = this.description;
data['link'] = this.link;
data['locale'] = this.locale;
data['nickname'] = this.nickname;
data['slug'] = this.slug;
data['roles'] = this.roles;
data['registered_date'] = this.registeredDate;

// afiq @afiqnymous
if (this.id != null) data['id'] = this.id;
if (this.username != null) data['username'] = this.username;
if (this.password != null) data['password'] = this.password;
if (this.name != null) data['name'] = this.name;
if (this.firstName != null) data['first_name'] = this.firstName;
if (this.lastName != null) data['last_name'] = this.lastName;
if (this.email != null) data['email'] = this.email;
if (this.url != null) data['url'] = this.url;
if (this.description != null) data['description'] = this.description;
if (this.link != null) data['link'] = this.link;
if (this.locale != null) data['locale'] = this.locale;
if (this.nickname != null) data['nickname'] = this.nickname;
if (this.link != null) data['link'] = this.link;
if (this.slug != null) data['slug'] = this.slug;
// if (this.roles != null) data['roles'] = this.roles;
// if (json['roles'] != null) roles = json['roles'].cast<String>();

if (this.registeredDate != null) data['registered_date'] = this.registeredDate;

if (this.capabilities != null) {
data['capabilities'] = this.capabilities.toJson();
}
Expand All @@ -113,14 +120,19 @@ class User {
if (this.lLinks != null) {
data['_links'] = this.lLinks.toJson();
}
// yahya - @mymakarim
if (this.password != null) {
data['password'] = this.password;
}
// end yahya - @mymakarim

return data;
}

// Map<String, dynamic> toJson() {
// final Map<String, dynamic> data = new Map<String, dynamic>();
// if (this.username != null) data['username'] = this.username;
// if (this.password != null) data['password'] = this.password;
// if (this.email != null) data['email'] = this.email;

// return data;
// }

@override
String toString() {
return 'id: $id, name: $name';
Expand Down