-
Notifications
You must be signed in to change notification settings - Fork 800
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
Comments: Redirect front-end comment edit links to Calypso. #8069
Conversation
class.jetpack.php
Outdated
add_filter( 'get_edit_post_link', array( $this, 'point_edit_links_to_calypso' ), 1, 2 ); | ||
if ( Jetpack::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) { | ||
add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 ); | ||
add_filter( 'edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1, 3 ); |
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 think it would be more appropriate to use get_edit_comment_link
filter here instead, the same way as we do for edit post links.
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... could have sworn I looked for that, and didn't find it 🙃 Yep, for sure, much easier.
class.jetpack.php
Outdated
@@ -669,6 +670,15 @@ function point_edit_links_to_calypso( $default_url, $post_id ) { | |||
return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); | |||
} | |||
|
|||
function point_edit_comment_links_to_calypso( $link, $comment_id, $text ) { | |||
$url = wp_parse_url( get_home_url() ); |
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'm pretty sure this is going to break on Jetpack sites with subfolder installs. I suggest following the existing pattern in point_edit_post_links_to_calypso
to avoid that:
$site_slug = Jetpack::build_raw_urls( get_home_url() );
class.jetpack.php
Outdated
@@ -669,6 +670,15 @@ function point_edit_links_to_calypso( $default_url, $post_id ) { | |||
return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); | |||
} | |||
|
|||
function point_edit_comment_links_to_calypso( $link, $comment_id, $text ) { | |||
$url = wp_parse_url( get_home_url() ); | |||
$html = sprintf( '<a class="comment-edit-link" href="%s">%s</a>', |
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.
Once we switch to get_edit_comment_link
filter, we can simplify this part - we'll only need to return the href
.
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.
class.jetpack.php
Outdated
@@ -669,6 +670,14 @@ function point_edit_links_to_calypso( $default_url, $post_id ) { | |||
return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); | |||
} | |||
|
|||
function point_edit_comment_links_to_calypso( $url ) { | |||
wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args ); |
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.
Could you walk me through what's happening here? Does wp_parse_url
not return an array of the url parts?
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.
Sure. PHP_URL_QUERY
says, "just return the query
key value of that array as a string" (it's the only part we're interested in). wp_parse_str
(which uses parse_str
) parses the query params it finds in that string and assigns them to the $query_args
array. The one we want that contains the comment id is the c
param (which is awkwardly assigned as amp;c
).
I could just use basic regex (\d+$
?) to strip whatever number off the end of the string, but that feels a little more fragile over time.
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.
Does the caller not have access to the comment id here?
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'd at the least add a quick comment since amp;c
is a bit unexpected
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.
Sure, good idea; added in 948498f.
@@ -669,6 +670,14 @@ function point_edit_links_to_calypso( $default_url, $post_id ) { | |||
return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); | |||
} | |||
|
|||
function point_edit_comment_links_to_calypso( $url ) { |
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.
Any reason why the signature is different for this function versus point_edit_post_links_to_calypso
? I think it's clearer to pass through a fallback and a comment id explicitly.
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.
point_edit_post_links_to_calypso
is hooked to get_edit_post_link
, which passes in the original link, the post ID (very convenient for our needs), and a context (not needed in our situation, so we only grab the first two ).
The get_edit_comment_link
filter, however, only gives us the link we'll be filtering, hence all the parsing madness.
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.
👍 Got it thanks for clarifying!
`point_edit_comment_links_to_calypso`.
class.jetpack.php
Outdated
function point_edit_comment_links_to_calypso( $url ) { | ||
// Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID. | ||
wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args ); | ||
return esc_url( sprintf( 'https://wordpress.com/comments/all/%s/?commentId=%d&action=edit', |
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.
@kwight the hotlink for edit will be https://wordpress.com/comment/{site_slug}/{comment_id}?action=edit
This is Calypso's PR Automattic/wp-calypso#20777
Urls look correct here. @rodrigoi could you update the related wpcom diff to match? |
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.
LGTM!
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.
Tests well and code looks good.
Needs Automattic/wp-calypso#20777.
Just as we've added the ability to redirect front-end post and page edit links to Calypso in #7732 , this PR gives the same treatment to front-end comment edit links. The same Jetpack option is used for activating (
edit_links_calypso_redirect
).When this option is enabled, comment edit links will be filtered with an
href
pointing to Calypso, in the format/comments/all/:site?commentId=:comment-id&action=edit
.See Automattic/wp-calypso#17221.
Testing
class.jetpack.php
L619 and replace the first checkJetpack::get_option( 'edit_links_calypso_redirect' )
with justtrue
.Proposed changelog entry for your changes:
Edit links for comments in the frontend can redirect to calypso if the Jetpack option
edit_links_calypso_redirect
is enabled