| 1 |
<?php |
| 2 |
/** |
| 3 |
* Comment functions. |
| 4 |
* |
| 5 |
* Functions for working with comments in ActivityPub context. |
| 6 |
* |
| 7 |
* @package Activitypub |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Activitypub; |
| 11 |
|
| 12 |
/** |
| 13 |
* Detect a comment request. |
| 14 |
* |
| 15 |
* @deprecated 7.1.0 |
| 16 |
* |
| 17 |
* @return int|bool Comment ID or false if not found. |
| 18 |
*/ |
| 19 |
function is_comment() { |
| 20 |
\_deprecated_function( __FUNCTION__, '7.1.0' ); |
| 21 |
|
| 22 |
$comment_id = get_query_var( 'c', null ); |
| 23 |
|
| 24 |
if ( ! is_null( $comment_id ) ) { |
| 25 |
$comment = \get_comment( $comment_id ); |
| 26 |
|
| 27 |
if ( $comment ) { |
| 28 |
return $comment_id; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the comment from an ActivityPub Object ID. |
| 37 |
* |
| 38 |
* @param string $id ActivityPub object ID (usually a URL) to check. |
| 39 |
* |
| 40 |
* @return \WP_Comment|boolean Comment, or false on failure. |
| 41 |
*/ |
| 42 |
function object_id_to_comment( $id ) { |
| 43 |
return Comment::object_id_to_comment( $id ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Verify that URL is a local comment or a previously received remote comment. |
| 48 |
* (For threading comments locally) |
| 49 |
* |
| 50 |
* @param string $url The URL to check. |
| 51 |
* |
| 52 |
* @return string|null Comment ID or null if not found |
| 53 |
*/ |
| 54 |
function url_to_commentid( $url ) { |
| 55 |
return Comment::url_to_commentid( $url ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check if a comment should be federated. |
| 60 |
* |
| 61 |
* We consider a comment should be federated if it is authored by a user that is |
| 62 |
* not disabled for federation and if it is a reply directly to the post or to a |
| 63 |
* federated comment. |
| 64 |
* |
| 65 |
* @param mixed $comment Comment object or ID. |
| 66 |
* |
| 67 |
* @return boolean True if the comment should be federated, false otherwise. |
| 68 |
*/ |
| 69 |
function should_comment_be_federated( $comment ) { |
| 70 |
return Comment::should_be_federated( $comment ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Check if a comment was federated. |
| 75 |
* |
| 76 |
* This function checks if a comment was federated via ActivityPub. |
| 77 |
* |
| 78 |
* @param mixed $comment Comment object or ID. |
| 79 |
* |
| 80 |
* @return boolean True if the comment was federated, false otherwise. |
| 81 |
*/ |
| 82 |
function was_comment_sent( $comment ) { |
| 83 |
return Comment::was_sent( $comment ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Check if a comment is federated. |
| 88 |
* |
| 89 |
* We consider a comment federated if comment was received via ActivityPub. |
| 90 |
* |
| 91 |
* Use this function to check if it is comment that was received via ActivityPub. |
| 92 |
* |
| 93 |
* @param mixed $comment Comment object or ID. |
| 94 |
* |
| 95 |
* @return boolean True if the comment is federated, false otherwise. |
| 96 |
*/ |
| 97 |
function was_comment_received( $comment ) { |
| 98 |
return Comment::was_received( $comment ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if a comment is local only. |
| 103 |
* |
| 104 |
* This function checks if a comment is local only and was not sent or received via ActivityPub. |
| 105 |
* |
| 106 |
* @param mixed $comment Comment object or ID. |
| 107 |
* |
| 108 |
* @return boolean True if the comment is local only, false otherwise. |
| 109 |
*/ |
| 110 |
function is_local_comment( $comment ) { |
| 111 |
return Comment::is_local( $comment ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Retrieves the IDs of the ancestors of a comment. |
| 116 |
* |
| 117 |
* Adaption of `get_post_ancestors` from WordPress core. |
| 118 |
* |
| 119 |
* @see https://developer.wordpress.org/reference/functions/get_post_ancestors/ |
| 120 |
* |
| 121 |
* @param int|\WP_Comment $comment Comment ID or comment object. |
| 122 |
* |
| 123 |
* @return int[] Array of ancestor IDs. |
| 124 |
*/ |
| 125 |
function get_comment_ancestors( $comment ) { |
| 126 |
$comment = \get_comment( $comment ); |
| 127 |
|
| 128 |
if ( ! $comment || empty( $comment->comment_parent ) || (int) $comment->comment_parent === (int) $comment->comment_ID ) { |
| 129 |
return array(); |
| 130 |
} |
| 131 |
|
| 132 |
$ancestors = array(); |
| 133 |
|
| 134 |
$id = (int) $comment->comment_parent; |
| 135 |
$ancestors[] = $id; |
| 136 |
|
| 137 |
while ( $id > 0 ) { |
| 138 |
$ancestor = \get_comment( $id ); |
| 139 |
|
| 140 |
if ( ! $ancestor ) { |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
$parent_id = (int) $ancestor->comment_parent; |
| 145 |
|
| 146 |
// Loop detection: If the ancestor has been seen before, break. |
| 147 |
if ( empty( $parent_id ) || ( $parent_id === (int) $comment->comment_ID ) || in_array( $parent_id, $ancestors, true ) ) { |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
$id = $parent_id; |
| 152 |
$ancestors[] = $id; |
| 153 |
} |
| 154 |
|
| 155 |
return $ancestors; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Registers a ActivityPub comment type. |
| 160 |
* |
| 161 |
* @param string $comment_type Key for comment type. |
| 162 |
* @param array $args Optional. Array of arguments for registering a comment type. Default empty array. |
| 163 |
* |
| 164 |
* @return array The registered Activitypub comment type. |
| 165 |
*/ |
| 166 |
function register_comment_type( $comment_type, $args = array() ) { |
| 167 |
global $activitypub_comment_types; |
| 168 |
|
| 169 |
if ( ! is_array( $activitypub_comment_types ) ) { |
| 170 |
$activitypub_comment_types = array(); |
| 171 |
} |
| 172 |
|
| 173 |
// Sanitize comment type name. |
| 174 |
$comment_type = sanitize_key( $comment_type ); |
| 175 |
|
| 176 |
$activitypub_comment_types[ $comment_type ] = $args; |
| 177 |
|
| 178 |
/** |
| 179 |
* Fires after a ActivityPub comment type is registered. |
| 180 |
* |
| 181 |
* @param string $comment_type Comment type. |
| 182 |
* @param array $args Arguments used to register the comment type. |
| 183 |
*/ |
| 184 |
do_action( 'activitypub_registered_comment_type', $comment_type, $args ); |
| 185 |
|
| 186 |
return $args; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get the reply intent URI as a JavaScript URI. |
| 191 |
* |
| 192 |
* @return string The reply intent URI. |
| 193 |
*/ |
| 194 |
function get_reply_intent_js() { |
| 195 |
return sprintf( |
| 196 |
'javascript:(()=>{window.open(\'%s\'+encodeURIComponent(window.location.href));})();', |
| 197 |
get_reply_intent_url() |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get the reply intent URI. |
| 203 |
* |
| 204 |
* @return string The reply intent URI. |
| 205 |
*/ |
| 206 |
function get_reply_intent_url() { |
| 207 |
/** |
| 208 |
* Filters the reply intent parameters. |
| 209 |
* |
| 210 |
* @param array $params The reply intent parameters. |
| 211 |
*/ |
| 212 |
$params = \apply_filters( 'activitypub_reply_intent_params', array() ); |
| 213 |
|
| 214 |
$params += array( 'in_reply_to' => '' ); |
| 215 |
$query = \http_build_query( $params ); |
| 216 |
$path = 'post-new.php?' . $query; |
| 217 |
$url = \admin_url( $path ); |
| 218 |
|
| 219 |
/** |
| 220 |
* Filters the reply intent URL. |
| 221 |
* |
| 222 |
* @param string $url The reply intent URL. |
| 223 |
*/ |
| 224 |
$url = \apply_filters( 'activitypub_reply_intent_url', $url ); |
| 225 |
|
| 226 |
return esc_url_raw( $url ); |
| 227 |
} |
| 228 |
|