-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Convert URL-related parts of Blog.m to Swift #25276
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import Foundation | ||
| import CoreData | ||
| import NSURL_IDN | ||
|
|
||
| extension Blog { | ||
|
|
||
|
|
@@ -26,4 +27,68 @@ extension Blog { | |
| return nil | ||
| } | ||
| } | ||
|
|
||
| // MARK: - URLs | ||
|
|
||
| /// User-facing display URL with protocol and trailing slash stripped, IDN decoded. | ||
| @objc public var displayURL: String? { | ||
| guard let url else { return nil } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't imagine a situation where the site URL should be But I guess that's another PR |
||
| var result = url.replacingOccurrences( | ||
| of: "^https?://", | ||
| with: "", | ||
| options: [.regularExpression, .caseInsensitive] | ||
| ) | ||
| if result.hasSuffix("/") { | ||
| result = String(result.dropLast()) | ||
| } | ||
| return NSURL.idnDecodedHostname(result) ?? result | ||
| } | ||
|
|
||
| /// The home URL for the blog. Falls back to ``url``. | ||
| @objc public var homeURL: String? { | ||
| getOptionString(name: "home_url") ?? url | ||
| } | ||
|
|
||
| /// The hostname extracted from the XML-RPC endpoint, used for reachability checks. | ||
| @objc public var hostname: String? { | ||
| var result: String? | ||
| if let xmlrpc { | ||
| result = URL(string: xmlrpc)?.host | ||
| } | ||
| if result == nil, let url { | ||
| result = url.replacingOccurrences( | ||
| of: "^.*://", | ||
| with: "", | ||
| options: [.regularExpression, .caseInsensitive] | ||
| ) | ||
| } | ||
| // NSURL doesn't recognize some TLDs like .me and .it, returning | ||
| // a full path. Strip path components to avoid breaking reachability. | ||
| return result?.components(separatedBy: "/").first | ||
| } | ||
|
|
||
| /// The login URL for the blog. | ||
| public var loginURL: URL? { | ||
| let string = getOptionString(name: "login_url") ?? url(withPath: "wp-login.php") | ||
| return string.flatMap(URL.init) | ||
| } | ||
|
|
||
| /// Builds a URL by replacing `xmlrpc.php` in the XML-RPC endpoint with the given path. | ||
| public func url(withPath path: String) -> String? { | ||
| guard let xmlrpc else { return nil } | ||
| return xmlrpc.replacingOccurrences( | ||
| of: "xmlrpc\\.php$", | ||
| with: path, | ||
| options: [.regularExpression, .caseInsensitive] | ||
| ) | ||
| } | ||
|
|
||
| /// Builds an admin URL by appending the given path to the admin base URL. | ||
| public func makeAdminURL(path: String? = nil) -> URL? { | ||
| var base = getOptionString(name: "admin_url") ?? url(withPath: "wp-admin/") ?? "" | ||
| if !base.hasSuffix("/") { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may be a better way to implement some of these, but I decided to keep direct ObjC to Swift conversion for now to reduce risk of breaking something. |
||
| base += "/" | ||
| } | ||
| return URL(string: base + (path ?? "")) | ||
| } | ||
| } | ||
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.
This comment seems obsolete. I found where the app uses
self.xmlrpc, but nothing else.