Skip to content
Merged
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
5 changes: 5 additions & 0 deletions WordPress/Classes/Services/PostCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ class PostCoordinator: NSObject {
let gutenbergMediaFilesUploadProcessor = GutenbergMediaFilesUploadProcessor(mediaUploadID: gutenbergMediaUploadID, serverMediaID: mediaID, remoteURLString: remoteURLStr)
gutenbergProcessors.append(gutenbergMediaFilesUploadProcessor)

if let videoPressGUID = media.videopressGUID {
let gutenbergVideoPressUploadProcessor = GutenbergVideoPressUploadProcessor(mediaUploadID: gutenbergMediaUploadID, serverMediaID: mediaID, videoPressGUID: videoPressGUID)
gutenbergProcessors.append(gutenbergVideoPressUploadProcessor)
}

} else if media.mediaType == .audio {
let gutenbergAudioProcessor = GutenbergAudioUploadProcessor(mediaUploadID: gutenbergMediaUploadID, serverMediaID: mediaID, remoteURLString: remoteURLStr)
gutenbergProcessors.append(gutenbergAudioProcessor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import Foundation
import Aztec

class GutenbergVideoPressUploadProcessor: Processor {

let mediaUploadID: Int32
let serverMediaID: Int
let videoPressGUID: String
private var videoPressURL: VideoPressURL?

private enum VideoPressBlockKeys: String {
case name = "wp:videopress/video"
case id
case guid
case resizeToParent
case cover
case autoplay
case controls
case loop
case muted
case playsinline
case poster
case preload
case seekbarColor
case seekbarPlayedColor
case seekbarLoadingColor
case useAverageColor
}

private enum VideoPressURLQueryParams: String {
case resizeToParent
case cover
case autoPlay
case controls
case loop
case muted
case persistVolume
case playsinline
case posterUrl
case preloadContent
case sbc
case sbpc
case sblc
case useAverageColor
}

init(mediaUploadID: Int32, serverMediaID: Int, videoPressGUID: String) {
self.mediaUploadID = mediaUploadID
self.serverMediaID = serverMediaID
self.videoPressGUID = videoPressGUID
}

lazy var videoPressHtmlProcessor = HTMLProcessor(for: "figure", replacer: { (figure) in
let url = self.videoPressURL?.getURLString() ?? ""
var attributes = figure.attributes
var html = "<figure "
let attributeSerializer = ShortcodeAttributeSerializer()
html += attributeSerializer.serialize(figure.attributes)
html += "><div class=\"jetpack-videopress-player__wrapper\">"
html += "\n\(url.escapeHtmlNamedEntities())\n"
html += "</div></figure>"
return html
})

lazy var videoPressBlockProcessor = GutenbergBlockProcessor(for: VideoPressBlockKeys.name.rawValue, replacer: { videoPressBlock in
guard let mediaID = videoPressBlock.attributes[VideoPressBlockKeys.id.rawValue] as? Int, mediaID == self.mediaUploadID else {
return nil
}
var block = "<!-- \(VideoPressBlockKeys.name.rawValue) "
var attributes = videoPressBlock.attributes
attributes[VideoPressBlockKeys.id.rawValue] = self.serverMediaID
attributes[VideoPressBlockKeys.guid.rawValue] = self.videoPressGUID
if let jsonData = try? JSONSerialization.data(withJSONObject: attributes, options: .sortedKeys),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that attributes are sorted alphabetically using the option .sortedKeys. This differs from the order of the attributes defined in the block settings. I thought this might imply that, after the processor updates the post content, opening the post again would make the content be modified. However, seems it that's not the case and the order change doesn't imply a modification.

let jsonString = String(data: jsonData, encoding: .utf8) {
block += jsonString
}
block += " -->"

self.videoPressURL = VideoPressURL(attributes: attributes, guid: self.videoPressGUID)
block += self.videoPressHtmlProcessor.process(videoPressBlock.content)

block += "<!-- /\(VideoPressBlockKeys.name.rawValue) -->"
return block
})

/// The VideoPress URL is built using the same logic we have in Jetpack:
/// https://github.com/Automattic/jetpack/blob/b1b826ab38690c5fad18789301ac81297a458878/projects/packages/videopress/src/client/lib/url/index.ts#L19-L67
private struct VideoPressURL {
let attributes: [String: Any]
let guid: String
private var options: [URLQueryItem]

init(attributes: [String: Any], guid: String) {
self.attributes = attributes
self.guid = guid
self.options = [URLQueryItem]()

// Populate options
addDefaultOptions()
addAutoPlayOption()
addControlsOption()
addLoopOption()
addVolumeOptions()
addPlaysInlineOption()
addPosterOption()
addPreloadOption()
addSeekbarOptions()
addUseAverageColorOption()
}

/// Returns the string of the VideoPress URL.
func getURLString() -> String {
guard let url = URL(string: "https://videopress.com/v/\(guid)"), var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return ""
}
urlComponents.queryItems = options
guard
let query = urlComponents.query,
/// In web, the query parameters are encoded using `encodeURIComponent` that percent encodes reserved characters (like `/` and `:`) that are not strictly necessary to be encoded based on RFC 3986.
/// In the spirit of generating an URL with the same encoding, we encode using `urlHostAllowed` which percent encodes all reserved characters.
///
/// References:
/// - https://en.wikipedia.org/wiki/URL_encoding#Reserved_characters
/// - https://www.ietf.org/rfc/rfc3986.txt
/// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
/// - https://github.com/WordPress/gutenberg/blob/1dfec0ab5f0977dcce2722bdfbe823926903e2a6/packages/url/src/build-query-string.js#L53
let encodedQuery = query.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else {
return url.absoluteString
}
return "\(url.absoluteString)?\(encodedQuery)"
}

/// Adds default options.
private mutating func addDefaultOptions() {
options += [
URLQueryItem(name: VideoPressURLQueryParams.resizeToParent.rawValue, value: true.stringLiteral),
URLQueryItem(name: VideoPressURLQueryParams.cover.rawValue, value: true.stringLiteral),
]
}

/// Adds AutoPlay option.
///
/// Turned OFF by default.
private mutating func addAutoPlayOption() {
if let autoplay = attributes[VideoPressBlockKeys.autoplay.rawValue] as? Bool, autoplay == true {
options.append(URLQueryItem(name: VideoPressURLQueryParams.autoPlay.rawValue, value: autoplay.stringLiteral))
}
}

/// Adds Controls option.
///
/// Turned ON by default.
private mutating func addControlsOption() {
if let controls = attributes[VideoPressBlockKeys.controls.rawValue] as? Bool, controls == false {
options.append(URLQueryItem(name: VideoPressURLQueryParams.controls.rawValue, value: controls.stringLiteral))
}
}

/// Adds Loop option.
///
/// Turned OFF by default.
private mutating func addLoopOption() {
if let loop = attributes[VideoPressBlockKeys.loop.rawValue] as? Bool, loop == true {
options.append(URLQueryItem(name: VideoPressURLQueryParams.loop.rawValue, value: loop.stringLiteral))
}
}

/// Adds Volume-related options.
///
/// - Muted: Turned OFF by default.
private mutating func addVolumeOptions() {
if let muted = attributes[VideoPressBlockKeys.muted.rawValue] as? Bool, muted == true {
options.append(URLQueryItem(name: VideoPressURLQueryParams.muted.rawValue, value: muted.stringLiteral))
options.append(URLQueryItem(name: VideoPressURLQueryParams.persistVolume.rawValue, value: false.stringLiteral))
}
}

/// Adds PlaysInline option.
///
/// Turned OFF by default.
private mutating func addPlaysInlineOption() {
if let playinline = attributes[VideoPressBlockKeys.playsinline.rawValue] as? Bool, playinline == true {
options.append(URLQueryItem(name: VideoPressURLQueryParams.playsinline.rawValue, value: playinline.stringLiteral))
}
}

/// Adds Poster option.
///
/// No image by default.
private mutating func addPosterOption() {
if let poster = attributes[VideoPressBlockKeys.poster.rawValue] as? String, !poster.isEmpty {
options.append(URLQueryItem(name: VideoPressURLQueryParams.posterUrl.rawValue, value: poster))
}
}

/// Adds Preload option.
///
/// Metadata by default.
private mutating func addPreloadOption() {
if let preload = attributes[VideoPressBlockKeys.preload.rawValue] as? String, !preload.isEmpty {
options.append(URLQueryItem(name: VideoPressURLQueryParams.preloadContent.rawValue, value: preload))
}
else {
options.append(URLQueryItem(name: VideoPressURLQueryParams.preloadContent.rawValue, value: "metadata"))
}
}

/// Adds Seekbar options.
///
/// - SeekbarColor: No color by default.
/// - SeekbarPlayerColor: No color by default.
/// - SeekbarLoadingColor: No color by default.
private mutating func addSeekbarOptions() {
if let seekbarColor = attributes[VideoPressBlockKeys.seekbarColor.rawValue] as? String, !seekbarColor.isEmpty {
options.append(URLQueryItem(name: VideoPressURLQueryParams.sbc.rawValue, value: seekbarColor))
}
if let seekbarPlayedColor = attributes[VideoPressBlockKeys.seekbarPlayedColor.rawValue] as? String, !seekbarPlayedColor.isEmpty {
options.append(URLQueryItem(name: VideoPressURLQueryParams.sbpc.rawValue, value: seekbarPlayedColor))
}
if let seekbarLoadingColor = attributes[VideoPressBlockKeys.seekbarLoadingColor.rawValue] as? String, !seekbarLoadingColor.isEmpty {
options.append(URLQueryItem(name: VideoPressURLQueryParams.sblc.rawValue, value: seekbarLoadingColor))
}
}

/// Adds UseAverageColor option.
///
/// Turned ON by default.
private mutating func addUseAverageColorOption() {
if let useAverageColor = attributes[VideoPressBlockKeys.useAverageColor.rawValue] as? Bool {
if useAverageColor == true {
options.append(URLQueryItem(name: VideoPressURLQueryParams.useAverageColor.rawValue, value: useAverageColor.stringLiteral))
}
}
else {
options.append(URLQueryItem(name: VideoPressURLQueryParams.useAverageColor.rawValue, value: true.stringLiteral))
}
}
}

func process(_ text: String) -> String {
return videoPressBlockProcessor.process(text)
}
}
10 changes: 10 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@
17FCA6811FD84B4600DBA9C8 /* NoticeStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17FCA6801FD84B4600DBA9C8 /* NoticeStore.swift */; };
1A433B1D2254CBEE00AE7910 /* WordPressComRestApi+Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A433B1C2254CBEE00AE7910 /* WordPressComRestApi+Defaults.swift */; };
1ABA150822AE5F870039311A /* WordPressUIBundleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ABA150722AE5F870039311A /* WordPressUIBundleTests.swift */; };
1D19C56329C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D19C56229C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift */; };
1D19C56429C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D19C56229C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift */; };
1D19C56629C9DB0A00FB0087 /* GutenbergVideoPressUploadProcessorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D19C56529C9DB0A00FB0087 /* GutenbergVideoPressUploadProcessorTests.swift */; };
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
1E0462162566938300EB98EF /* GutenbergFileUploadProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E0462152566938300EB98EF /* GutenbergFileUploadProcessor.swift */; };
1E0FF01E242BC572008DA898 /* GutenbergWebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E0FF01D242BC572008DA898 /* GutenbergWebViewController.swift */; };
Expand Down Expand Up @@ -6091,6 +6094,8 @@
1A433B1C2254CBEE00AE7910 /* WordPressComRestApi+Defaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressComRestApi+Defaults.swift"; sourceTree = "<group>"; };
1ABA150722AE5F870039311A /* WordPressUIBundleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressUIBundleTests.swift; sourceTree = "<group>"; };
1BC96E982E9B1A6DD86AF491 /* Pods-WordPressShareExtension.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressShareExtension.release-alpha.xcconfig"; path = "../Pods/Target Support Files/Pods-WordPressShareExtension/Pods-WordPressShareExtension.release-alpha.xcconfig"; sourceTree = "<group>"; };
1D19C56229C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GutenbergVideoPressUploadProcessor.swift; sourceTree = "<group>"; };
1D19C56529C9DB0A00FB0087 /* GutenbergVideoPressUploadProcessorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GutenbergVideoPressUploadProcessorTests.swift; sourceTree = "<group>"; };
1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1D6058910D05DD3D006BFB54 /* WordPress.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WordPress.app; sourceTree = BUILT_PRODUCTS_DIR; };
1E0462152566938300EB98EF /* GutenbergFileUploadProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GutenbergFileUploadProcessor.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -17750,6 +17755,7 @@
91138454228373EB00FB02B7 /* GutenbergVideoUploadProcessor.swift */,
4629E4202440C5B20002E15C /* GutenbergCoverUploadProcessor.swift */,
F5E1577E25DE04E200EEEDFB /* GutenbergMediaFilesUploadProcessor.swift */,
1D19C56229C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift */,
46638DF5244904A3006E8439 /* GutenbergBlockProcessor.swift */,
);
path = Processors;
Expand Down Expand Up @@ -17791,6 +17797,7 @@
FF2EC3C12209AC19006176E1 /* GutenbergImgUploadProcessorTests.swift */,
FF1B11E6238FE27A0038B93E /* GutenbergGalleryUploadProcessorTests.swift */,
9123471A221449E200BD9F97 /* GutenbergInformativeDialogTests.swift */,
1D19C56529C9DB0A00FB0087 /* GutenbergVideoPressUploadProcessorTests.swift */,
FF0B2566237A023C004E255F /* GutenbergVideoUploadProcessorTests.swift */,
4629E4222440C8160002E15C /* GutenbergCoverUploadProcessorTests.swift */,
AEE082892681C23C00DCF54B /* GutenbergRefactoredGalleryUploadProcessorTests.swift */,
Expand Down Expand Up @@ -21124,6 +21131,7 @@
40EE947F2213213F00CD264F /* PublicizeConnectionStatsRecordValue+CoreDataClass.swift in Sources */,
1702BBE01CF3034E00766A33 /* DomainsService.swift in Sources */,
8B1CF00F2433902700578582 /* PasswordAlertController.swift in Sources */,
1D19C56329C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift in Sources */,
1770BD0D267A368100D5F8C0 /* BloggingRemindersPushPromptViewController.swift in Sources */,
FA7F92B825E61C7E00502D2A /* ReaderTagsFooter.swift in Sources */,
80535DB82946C79700873161 /* JetpackBrandingMenuCardCell.swift in Sources */,
Expand Down Expand Up @@ -23143,6 +23151,7 @@
0A9687BC28B40771009DCD2F /* FullScreenCommentReplyViewModelMock.swift in Sources */,
400199AB222590E100EB0906 /* AllTimeStatsRecordValueTests.swift in Sources */,
C8567498243F41CA001A995E /* MockTenorService.swift in Sources */,
1D19C56629C9DB0A00FB0087 /* GutenbergVideoPressUploadProcessorTests.swift in Sources */,
4A9314DC297790C300360232 /* PeopleServiceTests.swift in Sources */,
4054F4642214F94D00D261AB /* StreakStatsRecordValueTests.swift in Sources */,
85B125411B028E34008A3D95 /* PushAuthenticationManagerTests.swift in Sources */,
Expand Down Expand Up @@ -24740,6 +24749,7 @@
FABB25352602FC2C00C8785C /* ReaderListTopic.swift in Sources */,
FABB25362602FC2C00C8785C /* PublicizeService.swift in Sources */,
FABB25372602FC2C00C8785C /* ActivityContentFactory.swift in Sources */,
1D19C56429C9D9A700FB0087 /* GutenbergVideoPressUploadProcessor.swift in Sources */,
FABB25382602FC2C00C8785C /* TenorDataLoader.swift in Sources */,
FABB25392602FC2C00C8785C /* OverviewCell.swift in Sources */,
F4D829662931046F00038726 /* UIButton+Dismiss.swift in Sources */,
Expand Down
Loading