Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit f186175

Browse files
committed
Add patchPostWithID
1 parent 78e3184 commit f186175

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

WordPressKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
0152100C28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0152100B28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift */; };
1616
0847B92C2A4442730044D32F /* IPLocationRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0847B92B2A4442730044D32F /* IPLocationRemote.swift */; };
1717
08C7493E2A45EA11000DA0E2 /* IPLocationRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08C7493D2A45EA11000DA0E2 /* IPLocationRemoteTests.swift */; };
18+
0C9CD7992B9A107E0045BE03 /* RemotePostUpdateParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C9CD7982B9A107E0045BE03 /* RemotePostUpdateParameters.swift */; };
1819
0CB1905E2A2A5E83004D3E80 /* BlazeCampaign.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB1905D2A2A5E83004D3E80 /* BlazeCampaign.swift */; };
1920
0CB190612A2A6A13004D3E80 /* blaze-campaigns-search.json in Resources */ = {isa = PBXBuildFile; fileRef = 0CB1905F2A2A6943004D3E80 /* blaze-campaigns-search.json */; };
2021
0CB190652A2A7569004D3E80 /* BlazeCampaignsSearchResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB190642A2A7569004D3E80 /* BlazeCampaignsSearchResponse.swift */; };
@@ -736,6 +737,7 @@
736737
0847B92B2A4442730044D32F /* IPLocationRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPLocationRemote.swift; sourceTree = "<group>"; };
737738
08C7493D2A45EA11000DA0E2 /* IPLocationRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPLocationRemoteTests.swift; sourceTree = "<group>"; };
738739
0C3A2A412A2E7BA500FD91D6 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
740+
0C9CD7982B9A107E0045BE03 /* RemotePostUpdateParameters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemotePostUpdateParameters.swift; sourceTree = "<group>"; };
739741
0CB1905D2A2A5E83004D3E80 /* BlazeCampaign.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlazeCampaign.swift; sourceTree = "<group>"; };
740742
0CB1905F2A2A6943004D3E80 /* blaze-campaigns-search.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "blaze-campaigns-search.json"; sourceTree = "<group>"; };
741743
0CB190642A2A7569004D3E80 /* BlazeCampaignsSearchResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlazeCampaignsSearchResponse.swift; sourceTree = "<group>"; };
@@ -2116,6 +2118,7 @@
21162118
E61A51A521B172A900A5F902 /* RemoteWpcomPlan.swift */,
21172119
740B23C01F17EE8000067A2A /* RemotePost.h */,
21182120
740B23C11F17EE8000067A2A /* RemotePost.m */,
2121+
0C9CD7982B9A107E0045BE03 /* RemotePostUpdateParameters.swift */,
21192122
740B23BE1F17EE8000067A2A /* RemotePostCategory.h */,
21202123
740B23BF1F17EE8000067A2A /* RemotePostCategory.m */,
21212124
93188D1C1F2262BF0028ED4D /* RemotePostTag.h */,
@@ -3502,6 +3505,7 @@
35023505
93BD27811EE73944002BB00B /* WordPressOrgXMLRPCApi.swift in Sources */,
35033506
4A57A6832B54A326008D0660 /* WordPressAPIError+NSErrorBrdige.swift in Sources */,
35043507
439A44D62107C66A00795ED7 /* JSONDecoderExtension.swift in Sources */,
3508+
0C9CD7992B9A107E0045BE03 /* RemotePostUpdateParameters.swift in Sources */,
35053509
B5A4822B20AC6C0B009D95F6 /* WPKitLogging.swift in Sources */,
35063510
B5A4822E20AC6C1A009D95F6 /* WPKitLogging.m in Sources */,
35073511
7430C9A61F1927180051B8E6 /* ReaderSiteServiceRemote.m in Sources */,

WordPressKit/NSDate+WordPressJSON.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
@interface NSDate (WordPressJSON)
44

5+
+ (NSDateFormatter *)rfc3339DateFormatter;
6+
57
/**
68
Parses a date string
79

WordPressKit/PostServiceRemote.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#import <WordPressKit/PostServiceRemoteOptions.h>
33

44
@class RemotePost;
5+
@class RemotePostUpdateParameters;
56

67
@protocol PostServiceRemote <NSObject>
78

@@ -62,6 +63,18 @@
6263
success:(void (^)(RemotePost *post))success
6364
failure:(void (^)(NSError *error))failure;
6465

66+
/**
67+
* @brief Updates a blog's post.
68+
*
69+
* @param postID The ID of the post to update. Cannot be nil.
70+
* @param success The block that will be executed on success. Can be nil.
71+
* @param failure The block that will be executed on failure. Can be nil.
72+
*/
73+
- (void)patchPostWithID:(NSNumber *)postID
74+
parameters:(RemotePostUpdateParameters *)post
75+
success:(void (^)(RemotePost *post))success
76+
failure:(void (^)(NSError *error))failure;
77+
6578
/**
6679
* @brief Deletes a post.
6780
*

WordPressKit/PostServiceRemoteREST.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,29 @@ - (void)updatePost:(RemotePost *)post
210210
}];
211211
}
212212

213+
- (void)patchPostWithID:(NSNumber *)postID
214+
parameters:(RemotePostUpdateParameters *)postUpdateParameters
215+
success:(void (^)(RemotePost *))success
216+
failure:(void (^)(NSError *))failure {
217+
NSString *path = [NSString stringWithFormat:@"sites/%@/posts/%@?context=edit", self.siteID, postID];
218+
NSString *requestUrl = [self pathForEndpoint:path withVersion:ServiceRemoteWordPressComRESTApiVersion_1_2];
219+
220+
NSDictionary *parameters = [postUpdateParameters makeRESTParameters];
221+
222+
[self.wordPressComRestApi POST:requestUrl
223+
parameters:parameters
224+
success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
225+
RemotePost *post = [self remotePostFromJSONDictionary:responseObject];
226+
if (success) {
227+
success(post);
228+
}
229+
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
230+
if (failure) {
231+
failure(error);
232+
}
233+
}];
234+
}
235+
213236
- (void)autoSave:(RemotePost *)post
214237
success:(void (^)(RemotePost *, NSString *))success
215238
failure:(void (^)(NSError *))failure
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Foundation
2+
3+
public final class RemotePostUpdateParameters: NSObject, Encodable {
4+
public var ifNotModifiedSince: Date??
5+
public var status: String??
6+
public var date: Date??
7+
public var content: String??
8+
public var slug: String??
9+
10+
@objc public func makeRESTParameters() -> [String: Any]? {
11+
let encoder = JSONEncoder()
12+
encoder.keyEncodingStrategy = .convertToSnakeCase
13+
encoder.dateEncodingStrategy = .formatted(NSDate.rfc3339DateFormatter())
14+
guard let data = try? encoder.encode(self),
15+
let object = try? JSONSerialization.jsonObject(with: data) else {
16+
return nil // Should never happen
17+
}
18+
return object as? [String: Any]
19+
}
20+
}

0 commit comments

Comments
 (0)