From 2ca33fdacdd0b233a2bb2f045a3a8f2d31bba53d Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 15 Oct 2024 11:01:41 +0200 Subject: [PATCH] Make Intent-URL filterable (#936) * Make Intent-URL filterable Allow plugins to extend the Intent-URL with a custom-post type for example. This PR also adds `post_type` to the "Query parameters" section. See: https://github.com/Automattic/jetpack/pull/39738 * ensure that `in_reply_to` is the last param --- includes/functions.php | 38 +++++++++++++++++++++++++++++++++----- templates/toolbox.php | 8 ++++++-- templates/welcome.php | 4 ++-- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 8aad9766..799af97f 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1135,16 +1135,44 @@ function normalize_host( $host ) { return \str_replace( 'www.', '', $host ); } +/** + * Get the reply intent URI as a JavaScript URI. + * + * @return string The reply intent URI. + */ +function get_reply_intent_js() { + return sprintf( + 'javascript:(()=>{window.open(\'%s\'+encodeURIComponent(window.location.href));})();', + get_reply_intent_url() + ); +} + /** * Get the reply intent URI. * * @return string The reply intent URI. */ -function get_reply_intent_uri() { - return sprintf( - 'javascript:(()=>{window.open(\'%s\'+encodeURIComponent(window.location.href));})();', - esc_url( \admin_url( 'post-new.php?in_reply_to=' ) ) - ); +function get_reply_intent_url() { + /** + * Filters the reply intent parameters. + * + * @param array $params The reply intent parameters. + */ + $params = \apply_filters( 'activitypub_reply_intent_params', array() ); + + $params += array( 'in_reply_to' => '' ); + $query = \http_build_query( $params ); + $path = 'post-new.php?' . $query; + $url = \admin_url( $path ); + + /** + * Filters the reply intent URL. + * + * @param string $url The reply intent URL. + */ + $url = \apply_filters( 'activitypub_reply_intent_url', $url ); + + return esc_url_raw( $url ); } /** diff --git a/templates/toolbox.php b/templates/toolbox.php index adc0b80b..21d786e3 100644 --- a/templates/toolbox.php +++ b/templates/toolbox.php @@ -16,7 +16,7 @@

- + @@ -26,7 +26,7 @@

- +

@@ -68,6 +68,10 @@ in_reply_to + + post_type + +

diff --git a/templates/welcome.php b/templates/welcome.php index 7a27f141..71169da9 100644 --- a/templates/welcome.php +++ b/templates/welcome.php @@ -27,13 +27,13 @@

%s', - esc_url( $bookmarklet_url ), // Need to escape quotes for the bookmarklet. + esc_attr( $bookmarklet_js ), // Need to escape quotes for the bookmarklet. sprintf( $reply_from_template, \wp_parse_url( \home_url(), PHP_URL_HOST ) ) ); /* translators: %s is where the button HTML will be rendered. */