Skip to content
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
135 changes: 0 additions & 135 deletions Sources/WordPressData/Objective-C/AbstractPost.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ @implementation AbstractPost
@dynamic permalinkTemplateURL;
@synthesize voiceContent;

#pragma mark - Life Cycle Methods

- (void)save
{
[[ContextManager sharedInstance] saveContext:self.managedObjectContext];
}

#pragma mark -
#pragma mark Revision management

Expand All @@ -58,45 +51,6 @@ - (AbstractPost *)original

#pragma mark - Helpers

- (BOOL)dateCreatedIsNilOrEqualToDateModified
{
return self.date_created_gmt == nil || [self.date_created_gmt isEqualToDate:self.dateModified];
}

- (BOOL)hasPhoto
{
if ([self.media count] == 0) {
return NO;
}

if (self.featuredImage != nil) {
return YES;
}

for (Media *media in self.media) {
if (media.mediaType == MediaTypeImage) {
return YES;
}
}

return NO;
}

- (BOOL)hasVideo
{
if ([self.media count] == 0) {
return NO;
}

for (Media *media in self.media) {
if (media.mediaType == MediaTypeVideo) {
return YES;
}
}

return NO;
}

- (BOOL)hasCategories
{
return NO;
Expand All @@ -107,93 +61,4 @@ - (BOOL)hasTags
return NO;
}

- (BOOL)hasRemote
{
return ((self.postID != nil) && ([self.postID longLongValue] > 0));
}

#pragma mark - Convenience methods

// If the post has a scheduled status.
- (BOOL)isScheduled
{
return ([self.status isEqualToString:PostStatusScheduled]);
}

- (BOOL)isDraft
{
return [self.status isEqualToString:PostStatusDraft];
}

- (BOOL)isPublished
{
return [self.status isEqualToString:PostStatusPublish];
}

- (BOOL)originalIsDraft
{
if ([self.status isEqualToString:PostStatusDraft]) {
return YES;
} else if (self.isRevision && [self.original.status isEqualToString:PostStatusDraft]) {
return YES;
}
return NO;
}

- (BOOL)shouldPublishImmediately
{
/// - warning: Yes, this is WordPress logic and it matches the behavior on
/// the web. If `dateCreated` is the same as `dateModified`, the system
/// uses it to represent a "no publish date selected" scenario.
return [self originalIsDraft] && [self dateCreatedIsNilOrEqualToDateModified];
}

- (NSURL *)blogURL
{
return [NSURL URLWithString:self.blog.url];
}

- (BOOL)isPrivateAtWPCom
{
return self.blog.isPrivateAtWPCom;
}

#pragma mark - Post

- (void)updatePathForDisplayImageBasedOnContent
{
// First lets check the post content for a suitable image
NSString *result = [DisplayableImageHelper searchPostContentForImageToDisplay:self.content];
if (result.length > 0) {
self.pathForDisplayImage = result;
}
// If none found let's see if some galleries are available
NSSet *mediaIDs = [DisplayableImageHelper searchPostContentForAttachmentIdsInGalleries:self.content];
for (Media *media in self.blog.media) {
NSNumber *mediaID = media.mediaID;
if (mediaID && [mediaIDs containsObject:mediaID]) {
result = media.remoteURL;
}
}
self.pathForDisplayImage = result;
}

- (void)setParsedOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)data
{
if (data == nil) {
self.rawOtherTerms = nil;
} else {
self.rawOtherTerms = [NSJSONSerialization dataWithJSONObject:data options:0 error:nil];
}
}

- (NSDictionary<NSString *, NSArray<NSString *> *> *)parseOtherTerms
{
if (self.rawOtherTerms == nil) {
return [NSDictionary dictionary];
}

return [NSJSONSerialization JSONObjectWithData:self.rawOtherTerms options:0 error:nil] ?: [NSDictionary dictionary];
}

@end
45 changes: 0 additions & 45 deletions Sources/WordPressData/Objective-C/include/AbstractPost.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,54 +62,9 @@ typedef NS_ENUM(NSUInteger, AbstractPostRemoteStatus) {

@property (nonatomic, strong, nullable) NSString *voiceContent;

- (BOOL)hasPhoto;
- (BOOL)hasVideo;
- (BOOL)hasCategories;
- (BOOL)hasTags;


#pragma mark - Conveniece Methods
- (BOOL)shouldPublishImmediately;
- (BOOL)isPrivateAtWPCom;


#pragma mark - Unsaved Changes

/**
Returns YES if the post is has a `future` post status
*/
- (BOOL)isScheduled;

/**
Returns YES if the post is a draft
*/
- (BOOL)isDraft;

/**
Returns YES if the post is a published.
*/
- (BOOL)isPublished;

/**
Returns YES if the original post is a draft
*/
- (BOOL)originalIsDraft;

// Does the post exist on the blog?
- (BOOL)hasRemote;

// Save changes to disk
- (void)save;

/**
* Updates the path for the display image by looking at the post content and trying to find an good image to use.
* If no appropiated image is found the path is set to nil.
*/
- (void)updatePathForDisplayImageBasedOnContent;

- (void)setParsedOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)data;
- (NSDictionary<NSString *, NSArray<NSString *> *> *)parseOtherTerms;

@end

@interface AbstractPost (CoreDataGeneratedAccessors)
Expand Down
110 changes: 110 additions & 0 deletions Sources/WordPressData/Swift/AbstractPost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,114 @@
func hasRevision() -> Bool {
revision != nil
}

/// Returns YES if the post is has a `future` post status
@objc
func isScheduled() -> Bool {
status == .scheduled
}

/// Returns YES if the post is a draft
@objc
func isDraft() -> Bool {
status == .draft
}

/// Returns YES if the post is a published.
@objc
func isPublished() -> Bool {
status == .publish
}

/// Returns YES if the original post is a draft
@objc
func originalIsDraft() -> Bool {
if status == .draft {
return true
} else if isRevision(), original?.status == .draft {
return true
}

Check warning on line 357 in Sources/WordPressData/Swift/AbstractPost.swift

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either merge this branch with the identical one on line 353 or change one of the implementations.

See more on https://sonarcloud.io/project/issues?id=wordpress-mobile_WordPress-iOS&issues=AZrXu8zDMjhEhvu4rqLv&open=AZrXu8zDMjhEhvu4rqLv&pullRequest=25040
return false
}

@objc
func shouldPublishImmediately() -> Bool {
// - warning: Yes, this is WordPress logic and it matches the behavior on
// the web. If `dateCreated` is the same as `dateModified`, the system
// uses it to represent a "no publish date selected" scenario.
originalIsDraft() && (date_created_gmt == nil || date_created_gmt == dateModified)
}

@objc
func hasPhoto() -> Bool {
if media.isEmpty {
return false
}

if featuredImage != nil {
return true
}

return media.contains { $0.mediaType == .image }
}

@objc
func hasVideo() -> Bool {
if media.isEmpty {
return false
}

return media.contains { $0.mediaType == .video }
}

/// Does the post exist on the blog?
@objc
func hasRemote() -> Bool {
(postID?.int64Value ?? 0) > 0
}

@objc
var parsedOtherTerms: [String: [String]] {
get {
guard let rawOtherTerms else {
return [:]
}

return (try? JSONSerialization.jsonObject(with: rawOtherTerms) as? [String: [String]]) ?? [:]
}
set {
rawOtherTerms = try? JSONSerialization.data(withJSONObject: newValue)
}
}

/// Updates the path for the display image by looking at the post content and trying to find an good image to use.
/// If no appropiated image is found the path is set to nil.
@objc
func updatePathForDisplayImageBasedOnContent() {
guard let content else {
return
}

if let result = DisplayableImageHelper.searchPostContentForImage(toDisplay: content), !result.isEmpty {
pathForDisplayImage = result
return
}

guard let allMedia = blog.media, !allMedia.isEmpty else { return }

let mediaIDs = DisplayableImageHelper.searchPostContentForAttachmentIds(inGalleries: content) as? Set<NSNumber> ?? []
for media in allMedia {
guard let media = media as? Media else { continue }

guard let mediaID = media.mediaID,
mediaIDs.contains(mediaID) else {
continue
}

if let remoteURL = media.remoteURL {
pathForDisplayImage = remoteURL
break
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension RemotePostCreateParameters {
excerpt = post.mt_excerpt
slug = post.wp_slug
featuredImageID = post.featuredImage?.mediaID?.intValue
otherTerms = post.parseOtherTerms()
otherTerms = post.parsedOtherTerms
switch post {
case let page as Page:
parentPageID = page.parentID?.intValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class EditorMediaUtility {
let requestURL: URL
if url.isFileURL {
requestURL = url
} else if post.isPrivateAtWPCom() && url.isHostedAtWPCom {
} else if post.blog.isPrivateAtWPCom() && url.isHostedAtWPCom {
// private wpcom image needs special handling.
// the size that WPImageHelper expects is pixel size
size.width = size.width * scale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct PostSettings: Hashable {
}

featuredImageID = post.featuredImage?.mediaID?.intValue
otherTerms = post.parseOtherTerms()
otherTerms = post.parsedOtherTerms

metadata = PostMetadata(post)

Expand Down Expand Up @@ -112,8 +112,8 @@ struct PostSettings: Hashable {
post.featuredImage = nil
}

if !RemotePost.compare(otherTerms: post.parseOtherTerms(), withAnother: otherTerms) {
post.setParsedOtherTerms(otherTerms)
if !RemotePost.compare(otherTerms: post.parsedOtherTerms, withAnother: otherTerms) {
post.parsedOtherTerms = otherTerms
}

var postMetadataContainer = PostMetadataContainer(post)
Expand Down