-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
GB finish uploads out of editor #10954
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c5df2f3
Implement the img upload processor for Gutenberg img blocks.
SergioEstevao 4882840
Merge branch 'develop' into issue/gb_finish_uploads_out_of_editor
SergioEstevao 67a28c1
Remove width and height insertion from the processor.
SergioEstevao 8fd1d21
Restore the failed state when coming back to the post.
SergioEstevao f5ee388
Merge branch 'develop' into issue/gb_finish_uploads_out_of_editor
SergioEstevao 4493df2
Fix lint violations.
SergioEstevao 6c906e0
Check if bridge is loaded before sending events.
SergioEstevao 4a2b15a
Merge branch 'develop' into issue/gb_finish_uploads_out_of_editor
SergioEstevao d90dc78
Update Gutenberg version.
SergioEstevao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
WordPress/Classes/ViewRelated/Gutenberg/Processors/GutenbergImgUploadProcessor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Foundation | ||
import Aztec | ||
|
||
class GutenbergImgUploadProcessor: Processor { | ||
|
||
let mediaUploadID: Int32 | ||
let remoteURLString: String | ||
let serverMediaID: Int | ||
static let imgClassIDPrefixAttribute = "wp-image-" | ||
|
||
init(mediaUploadID: Int32, serverMediaID: Int, remoteURLString: String) { | ||
self.mediaUploadID = mediaUploadID | ||
self.serverMediaID = serverMediaID | ||
self.remoteURLString = remoteURLString | ||
} | ||
|
||
lazy var imgPostMediaUploadProcessor = HTMLProcessor(for: "img", replacer: { (img) in | ||
guard let imgClassAttributeValue = img.attributes["class"]?.value, | ||
case let .string(imgClass) = imgClassAttributeValue else { | ||
return nil | ||
} | ||
|
||
let classAttributes = imgClass.components(separatedBy: " ") | ||
|
||
guard let imageIDAttribute = classAttributes.filter({ (value) -> Bool in | ||
value.hasPrefix(GutenbergImgUploadProcessor.imgClassIDPrefixAttribute) | ||
}).first else { | ||
return nil | ||
} | ||
|
||
let imageIDString = String(imageIDAttribute.dropFirst(GutenbergImgUploadProcessor.imgClassIDPrefixAttribute.count)) | ||
let imgUploadID = Int32(imageIDString) | ||
|
||
guard imgUploadID == self.mediaUploadID else { | ||
return nil | ||
} | ||
|
||
let newImgClassAttributes = imgClass.replacingOccurrences(of: imageIDAttribute, with: GutenbergImgUploadProcessor.imgClassIDPrefixAttribute + String(self.serverMediaID)) | ||
|
||
var attributes = img.attributes | ||
attributes.set(.string(self.remoteURLString), forKey: "src") | ||
attributes.set(.string(newImgClassAttributes), forKey: "class") | ||
|
||
var html = "<img " | ||
let attributeSerializer = ShortcodeAttributeSerializer() | ||
html += attributeSerializer.serialize(attributes) | ||
html += ">" | ||
return html | ||
}) | ||
|
||
func process(_ text: String) -> String { | ||
var result = imgPostMediaUploadProcessor.process(text) | ||
result = result.replacingOccurrences(of: "wp:image {\"id\":\(String(mediaUploadID))}", with: "wp:image {\"id\":\(String(serverMediaID))}") | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
WordPress/WordPressTest/Gutenberg/GutenbergImgUploadProcessorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import XCTest | ||
@testable import WordPress | ||
|
||
class GutenbergImgUploadProcessorTests: XCTestCase { | ||
|
||
let postContent = """ | ||
<!-- wp:image {"id":-181231834} --> | ||
<figure class="wp-block-image"><img src="file://tmp/EC856C66-7B79-4631-9503-2FB9FF0E6C66.jpg" alt="" class="wp-image--181231834"/></figure> | ||
<!-- /wp:image --> | ||
""" | ||
|
||
let postResultContent = """ | ||
<!-- wp:image {"id":100} --> | ||
<figure class="wp-block-image"><img src="http://www.wordpress.com/logo.jpg" alt="" class="wp-image-100"></figure> | ||
<!-- /wp:image --> | ||
""" | ||
|
||
func testProcessor() { | ||
let gutenbergMediaUploadID = Int32(-181231834) | ||
let mediaID = 100 | ||
let remoteURLStr = "http://www.wordpress.com/logo.jpg" | ||
|
||
let gutenbergImgPostUploadProcessor = GutenbergImgUploadProcessor(mediaUploadID: gutenbergMediaUploadID, serverMediaID: mediaID, remoteURLString: remoteURLStr) | ||
let resultContent = gutenbergImgPostUploadProcessor.process(postContent) | ||
|
||
XCTAssertEqual(resultContent, postResultContent, "Post content should be updated correctly") | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this replacing the post content for all posts, not just those in Gutenberg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we apply both processors on all posts, but only the GB ones will be affected for the processor, because they have a different format for the media uploads ids.