Skip to content

Commit c7f4f49

Browse files
authored
Merge pull request #50 from yringler/add-fetch-all
Add fetch all
2 parents fdee207 + 1fb982b commit c7f4f49

6 files changed

Lines changed: 96 additions & 16 deletions

File tree

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
.buildlog/
1010
.history
1111
.svn/
12+
*flutter_export_environment*
1213

1314
# IntelliJ related
1415
*.iml

example/ios/Flutter/flutter_export_environment.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/flutter_wordpress.dart

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ class WordPress {
243243
///
244244
/// (**Note:** *Set only those fetch boolean parameters which you need because
245245
/// the more information to fetch, the longer it will take to return all Posts*)
246+
///
247+
/// [fetchAll] will make as many API requests as is needed to get all posts.
248+
/// This may take a while.
246249
///
247250
/// In case of an error, a [WordPressError] object is thrown.
248251
async.Future<List<Post>> fetchPosts({
@@ -256,8 +259,14 @@ class WordPress {
256259
bool fetchFeaturedMedia = false,
257260
bool fetchAttachments = false,
258261
String postType = "posts",
262+
bool fetchAll = false
259263
}) async {
260-
final StringBuffer url = new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType);
264+
if (fetchAll) {
265+
postParams = postParams.copyWith(perPage: 100);
266+
}
267+
268+
final StringBuffer url =
269+
new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType);
261270

262271
url.write(postParams.toString());
263272

@@ -280,6 +289,25 @@ class WordPress {
280289
setAttachments: fetchAttachments,
281290
));
282291
}
292+
293+
if (fetchAll && response.headers["x-wp-totalpages"] != null) {
294+
final totalPages = int.parse(response.headers["x-wp-totalpages"]);
295+
296+
for (int i = postParams.pageNum + 1; i <= totalPages; ++i) {
297+
posts.addAll(await fetchPosts(
298+
postParams: postParams.copyWith(pageNum: i),
299+
fetchAuthor: fetchAuthor,
300+
fetchComments: fetchComments,
301+
orderComments: orderComments,
302+
orderCommentsBy: orderCommentsBy,
303+
fetchCategories: fetchCategories,
304+
fetchTags: fetchTags,
305+
fetchFeaturedMedia: fetchFeaturedMedia,
306+
fetchAttachments: fetchAttachments,
307+
));
308+
}
309+
}
310+
283311
return posts;
284312
} else {
285313
try {
@@ -534,7 +562,11 @@ class WordPress {
534562
///
535563
/// In case of an error, a [WordPressError] object is thrown.
536564
async.Future<List<Category>> fetchCategories(
537-
{@required ParamsCategoryList params}) async {
565+
{@required ParamsCategoryList params, bool fetchAll = false}) async {
566+
if (fetchAll) {
567+
params = params.copyWith(perPage: 100);
568+
}
569+
538570
final StringBuffer url = new StringBuffer(_baseUrl + URL_CATEGORIES);
539571

540572
url.write(params.toString());
@@ -547,6 +579,16 @@ class WordPress {
547579
list.forEach((category) {
548580
categories.add(Category.fromJson(category));
549581
});
582+
583+
if (fetchAll && response.headers["x-wp-totalpages"] != null) {
584+
final totalPages = int.parse(response.headers["x-wp-totalpages"]);
585+
586+
for (int i = params.pageNum + 1; i <= totalPages; ++i) {
587+
categories.addAll(await fetchCategories(
588+
params: params.copyWith(pageNum: i)));
589+
}
590+
}
591+
550592
return categories;
551593
} else {
552594
try {
@@ -652,7 +694,7 @@ class WordPress {
652694
}
653695

654696
// yahya - @mymakarim
655-
697+
656698
async.Future<dynamic> uploadMedia(File image) async {
657699
final StringBuffer url = new StringBuffer(_baseUrl + URL_MEDIA);
658700
var file = image.readAsBytesSync();
@@ -678,7 +720,7 @@ class WordPress {
678720
}
679721
}
680722
}
681-
723+
682724
// uploadMedia function added by: @GarvMaggu
683725

684726
async.Future<bool> createUser({@required User user}) async {

lib/requests/params_category_list.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ class ParamsCategoryList {
5050
};
5151
}
5252

53+
ParamsCategoryList copyWith({
54+
int pageNum,
55+
int perPage
56+
}) {
57+
return ParamsCategoryList(
58+
context: context,
59+
order: order,
60+
orderBy: orderBy,
61+
pageNum: pageNum ?? this.pageNum,
62+
perPage: perPage ?? this.perPage,
63+
searchQuery: searchQuery,
64+
slug: slug,
65+
excludeCategoryIDs: excludeCategoryIDs,
66+
hideEmpty: hideEmpty,
67+
includeCategoryIDs: includeCategoryIDs,
68+
parent: parent,
69+
post: post
70+
);
71+
}
72+
5373
@override
5474
String toString() {
5575
return constructUrlParams(toMap());

lib/requests/params_post_list.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ class ParamsPostList {
7474
};
7575
}
7676

77+
ParamsPostList copyWith({
78+
int pageNum ,
79+
int perPage,
80+
}) {
81+
return ParamsPostList(
82+
afterDate: afterDate,
83+
beforeDate: beforeDate,
84+
context: context,
85+
excludeAuthorIDs: excludeAuthorIDs,
86+
excludeCategories: excludeCategories,
87+
excludePostIDs: excludePostIDs,
88+
excludeTags: excludeTags,
89+
includeAuthorIDs: includeAuthorIDs,
90+
includeCategories: includeCategories,
91+
includePostIDs: includePostIDs,
92+
includeTags: includeTags,
93+
offset: offset,
94+
order: order,
95+
orderBy: orderBy,
96+
pageNum: pageNum ?? this.pageNum,
97+
perPage: perPage ?? this.perPage,
98+
postStatus: postStatus,
99+
searchQuery: searchQuery,
100+
slug: slug,
101+
sticky: sticky
102+
);
103+
}
104+
77105
@override
78106
String toString() {
79107
return constructUrlParams(toMap());

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: flutter_wordpress
22
description: This library uses WordPress REST-API-V2 to provide a way for your application to
33
interact with your WordPress website.
4-
version: 0.2.1
4+
version: 0.2.2
55
authors:
66
- Sachin Ganesh <sachin@dreamsoftin.com>
77
- Suraj Shettigar <suraj@dreamsoftin.com>

0 commit comments

Comments
 (0)