From faf2d8f69abe1670cde2ea84d32a99cf21dc3bc7 Mon Sep 17 00:00:00 2001 From: Yehuda Ringler Date: Sun, 26 Jul 2020 17:52:26 -0400 Subject: [PATCH 1/2] add support for custom fields on posts --- lib/flutter_wordpress.dart | 15 +++++++++++++-- lib/schemas/post.dart | 11 +++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/flutter_wordpress.dart b/lib/flutter_wordpress.dart index 74161ce..eb24c6e 100644 --- a/lib/flutter_wordpress.dart +++ b/lib/flutter_wordpress.dart @@ -257,6 +257,9 @@ class WordPress { /// /// [fetchAll] will make as many API requests as is needed to get all posts. /// This may take a while. + /// + /// Specify any custom fields in [customFieldNames]. They will be loaded into + /// [Post.customFields] /// /// In case of an error, a [WordPressError] object is thrown. Future> fetchPosts({ @@ -271,6 +274,7 @@ class WordPress { bool fetchAttachments = false, String postType = "posts", bool fetchAll = false, + Set? customFieldNames = null }) async { if (fetchAll) { postParams = postParams.copyWith(perPage: 100); @@ -288,9 +292,16 @@ class WordPress { List posts = []; final list = json.decode(response.body); - for (final post in list) { + for (final Map post in list) { + Map? customFields; + + if (customFieldNames != null && customFieldNames.isNotEmpty) { + customFields = Map.fromEntries( + customFieldNames.map((key) => MapEntry(key, post[key]))); + } + posts.add(await _postBuilder( - post: Post.fromJson(post), + post: Post.fromJson(post)..customFields = customFields ?? {}, setAuthor: fetchAuthor, setComments: fetchComments, orderComments: orderComments, diff --git a/lib/schemas/post.dart b/lib/schemas/post.dart index 0ecd0b2..bbd81de 100644 --- a/lib/schemas/post.dart +++ b/lib/schemas/post.dart @@ -99,6 +99,9 @@ class Post { /// The featured Media of the post. Media? featuredMedia; + /// Custom fields on a post. + Map? customFields; + Post({ this.date, this.dateGmt, @@ -118,6 +121,7 @@ class Post { this.format = PostFormat.standard, this.categoryIDs, this.tagIDs, + this.customFields, }) : this.title = new Title(rendered: title), this.featuredMedia = new Media(sourceUrl: featuredMedia), this.content = new Content(rendered: content), @@ -204,6 +208,13 @@ class Post { data['categories'] = listToUrlString(this.categoryIDs ?? []); data['tags'] = listToUrlString(this.tagIDs ?? []); + if (customFields != null) { + for (final key in customFields!.keys) { + data[key] = customFields![key]; + } + } + + return data; } From b7827495f4ccf0d5ab555018ae4849c09034989a Mon Sep 17 00:00:00 2001 From: Yehuda Ringler Date: Fri, 31 Jul 2020 08:35:28 -0400 Subject: [PATCH 2/2] pass in custom field names to query all --- lib/flutter_wordpress.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/flutter_wordpress.dart b/lib/flutter_wordpress.dart index eb24c6e..2086e05 100644 --- a/lib/flutter_wordpress.dart +++ b/lib/flutter_wordpress.dart @@ -327,6 +327,8 @@ class WordPress { fetchTags: fetchTags, fetchFeaturedMedia: fetchFeaturedMedia, fetchAttachments: fetchAttachments, + customFieldNames: customFieldNames, + postType: postType )); } }