-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Gutenberg] Add VideoPress upload processor #20380
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
125452f
Add VideoPress upload processor
fluiddot 056378f
Add tests for VideoPress upload processor
fluiddot 7cc5d89
Add VideoPress upload processor in `PostCoordinator`
fluiddot 5e92801
Merge branch 'trunk' into gutenberg/videopress-upload-processor
fluiddot dacbf53
Use enum for VideoPress block and query param keys
fluiddot 0ec182b
Divide VideoPress URL build logic
fluiddot 2ddbacb
Fix VideoPress block processor
fluiddot 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 hidden or 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
243 changes: 243 additions & 0 deletions
243
WordPress/Classes/ViewRelated/Gutenberg/Processors/GutenbergVideoPressUploadProcessor.swift
This file contains hidden or 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,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), | ||
| 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) | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
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.