| 1 |
<?php |
| 2 |
namespace Activitypub\Transformer; |
| 3 |
|
| 4 |
use WP_Comment; |
| 5 |
use WP_Comment_Query; |
| 6 |
|
| 7 |
use Activitypub\Model\Blog_User; |
| 8 |
use Activitypub\Collection\Users; |
| 9 |
use Activitypub\Transformer\Base; |
| 10 |
|
| 11 |
use function Activitypub\is_single_user; |
| 12 |
use function Activitypub\get_rest_url_by_path; |
| 13 |
|
| 14 |
/** |
| 15 |
* WordPress Comment Transformer |
| 16 |
* |
| 17 |
* The Comment Transformer is responsible for transforming a WP_Comment object into different |
| 18 |
* Object-Types. |
| 19 |
* |
| 20 |
* Currently supported are: |
| 21 |
* |
| 22 |
* - Activitypub\Activity\Base_Object |
| 23 |
*/ |
| 24 |
class Comment extends Base { |
| 25 |
/** |
| 26 |
* Returns the User-ID of the WordPress Comment. |
| 27 |
* |
| 28 |
* @return int The User-ID of the WordPress Comment |
| 29 |
*/ |
| 30 |
public function get_wp_user_id() { |
| 31 |
return $this->wp_object->user_id; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Change the User-ID of the WordPress Comment. |
| 36 |
* |
| 37 |
* @return int The User-ID of the WordPress Comment |
| 38 |
*/ |
| 39 |
public function change_wp_user_id( $user_id ) { |
| 40 |
$this->wp_object->user_id = $user_id; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Transforms the WP_Comment object to an ActivityPub Object |
| 45 |
* |
| 46 |
* @see \Activitypub\Activity\Base_Object |
| 47 |
* |
| 48 |
* @return \Activitypub\Activity\Base_Object The ActivityPub Object |
| 49 |
*/ |
| 50 |
public function to_object() { |
| 51 |
$comment = $this->wp_object; |
| 52 |
$object = parent::to_object(); |
| 53 |
|
| 54 |
$object->set_url( \get_comment_link( $comment->comment_ID ) ); |
| 55 |
$object->set_type( 'Note' ); |
| 56 |
|
| 57 |
$published = \strtotime( $comment->comment_date_gmt ); |
| 58 |
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) ); |
| 59 |
|
| 60 |
$updated = \get_comment_meta( $comment->comment_ID, 'activitypub_last_modified', true ); |
| 61 |
if ( $updated > $published ) { |
| 62 |
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $updated ) ) ); |
| 63 |
} |
| 64 |
|
| 65 |
$object->set_content_map( |
| 66 |
array( |
| 67 |
$this->get_locale() => $this->get_content(), |
| 68 |
) |
| 69 |
); |
| 70 |
$path = sprintf( 'users/%d/followers', intval( $comment->comment_author ) ); |
| 71 |
|
| 72 |
$object->set_to( |
| 73 |
array( |
| 74 |
'https://www.w3.org/ns/activitystreams#Public', |
| 75 |
get_rest_url_by_path( $path ), |
| 76 |
) |
| 77 |
); |
| 78 |
|
| 79 |
return $object; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the User-URL of the Author of the Post. |
| 84 |
* |
| 85 |
* If `single_user` mode is enabled, the URL of the Blog-User is returned. |
| 86 |
* |
| 87 |
* @return string The User-URL. |
| 88 |
*/ |
| 89 |
protected function get_attributed_to() { |
| 90 |
if ( is_single_user() ) { |
| 91 |
$user = new Blog_User(); |
| 92 |
return $user->get_url(); |
| 93 |
} |
| 94 |
|
| 95 |
return Users::get_by_id( $this->wp_object->user_id )->get_url(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the content for the ActivityPub Item. |
| 100 |
* |
| 101 |
* The content will be generated based on the user settings. |
| 102 |
* |
| 103 |
* @return string The content. |
| 104 |
*/ |
| 105 |
protected function get_content() { |
| 106 |
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 107 |
$comment = $this->wp_object; |
| 108 |
$content = $comment->comment_content; |
| 109 |
|
| 110 |
$content = \wpautop( $content ); |
| 111 |
$content = \preg_replace( '/[\n\r\t]/', '', $content ); |
| 112 |
$content = \trim( $content ); |
| 113 |
$content = \apply_filters( 'the_content', $content, $comment ); |
| 114 |
|
| 115 |
return $content; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the in-reply-to for the ActivityPub Item. |
| 120 |
* |
| 121 |
* @return int The URL of the in-reply-to. |
| 122 |
*/ |
| 123 |
protected function get_in_reply_to() { |
| 124 |
$comment = $this->wp_object; |
| 125 |
|
| 126 |
$parent_comment = \get_comment( $comment->comment_parent ); |
| 127 |
|
| 128 |
if ( $parent_comment ) { |
| 129 |
$comment_meta = \get_comment_meta( $parent_comment->comment_ID ); |
| 130 |
|
| 131 |
if ( ! empty( $comment_meta['source_id'][0] ) ) { |
| 132 |
$in_reply_to = $comment_meta['source_id'][0]; |
| 133 |
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) { |
| 134 |
$in_reply_to = $comment_meta['source_url'][0]; |
| 135 |
} else { |
| 136 |
$in_reply_to = $this->generate_id( $parent_comment ); |
| 137 |
} |
| 138 |
} else { |
| 139 |
$in_reply_to = \get_permalink( $comment->comment_post_ID ); |
| 140 |
} |
| 141 |
|
| 142 |
return $in_reply_to; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns the ID of the ActivityPub Object. |
| 147 |
* |
| 148 |
* @see https://www.w3.org/TR/activitypub/#obj-id |
| 149 |
* @see https://github.com/tootsuite/mastodon/issues/13879 |
| 150 |
* |
| 151 |
* @return string ActivityPub URI for comment |
| 152 |
*/ |
| 153 |
protected function get_id() { |
| 154 |
$comment = $this->wp_object; |
| 155 |
return $this->generate_id( $comment ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Generates an ActivityPub URI for a comment |
| 160 |
* |
| 161 |
* @param WP_Comment|int $comment A comment object or comment ID |
| 162 |
* |
| 163 |
* @return string ActivityPub URI for comment |
| 164 |
*/ |
| 165 |
protected function generate_id( $comment ) { |
| 166 |
$comment = get_comment( $comment ); |
| 167 |
|
| 168 |
return \add_query_arg( |
| 169 |
array( |
| 170 |
'c' => $comment->comment_ID, |
| 171 |
), |
| 172 |
\trailingslashit( site_url() ) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns a list of Mentions, used in the Comment. |
| 178 |
* |
| 179 |
* @see https://docs.joinmastodon.org/spec/activitypub/#Mention |
| 180 |
* |
| 181 |
* @return array The list of Mentions. |
| 182 |
*/ |
| 183 |
protected function get_cc() { |
| 184 |
$cc = array(); |
| 185 |
|
| 186 |
$mentions = $this->get_mentions(); |
| 187 |
if ( $mentions ) { |
| 188 |
foreach ( $mentions as $mention => $url ) { |
| 189 |
$cc[] = $url; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
$comment_query = new WP_Comment_Query( |
| 194 |
array( |
| 195 |
'post_id' => $this->wp_object->comment_post_ID, |
| 196 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 197 |
'meta_query' => array( |
| 198 |
array( |
| 199 |
'key' => 'source_id', |
| 200 |
'compare' => 'EXISTS', |
| 201 |
), |
| 202 |
), |
| 203 |
) |
| 204 |
); |
| 205 |
|
| 206 |
if ( $comment_query->comments ) { |
| 207 |
foreach ( $comment_query->comments as $comment ) { |
| 208 |
if ( empty( $comment->comment_author_url ) ) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
$cc[] = \esc_url( $comment->comment_author_url ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
$cc = \array_unique( $cc ); |
| 216 |
|
| 217 |
return $cc; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns a list of Tags, used in the Comment. |
| 222 |
* |
| 223 |
* This includes Hash-Tags and Mentions. |
| 224 |
* |
| 225 |
* @return array The list of Tags. |
| 226 |
*/ |
| 227 |
protected function get_tag() { |
| 228 |
$tags = array(); |
| 229 |
|
| 230 |
$mentions = $this->get_mentions(); |
| 231 |
if ( $mentions ) { |
| 232 |
foreach ( $mentions as $mention => $url ) { |
| 233 |
$tag = array( |
| 234 |
'type' => 'Mention', |
| 235 |
'href' => \esc_url( $url ), |
| 236 |
'name' => \esc_html( $mention ), |
| 237 |
); |
| 238 |
$tags[] = $tag; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return \array_unique( $tags, SORT_REGULAR ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Helper function to get the @-Mentions from the comment content. |
| 247 |
* |
| 248 |
* @return array The list of @-Mentions. |
| 249 |
*/ |
| 250 |
protected function get_mentions() { |
| 251 |
return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->comment_content, $this->wp_object ); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Returns the locale of the post. |
| 256 |
* |
| 257 |
* @return string The locale of the post. |
| 258 |
*/ |
| 259 |
public function get_locale() { |
| 260 |
$comment_id = $this->wp_object->ID; |
| 261 |
$lang = \strtolower( \strtok( \get_locale(), '_-' ) ); |
| 262 |
|
| 263 |
/** |
| 264 |
* Filter the locale of the comment. |
| 265 |
* |
| 266 |
* @param string $lang The locale of the comment. |
| 267 |
* @param int $comment_id The comment ID. |
| 268 |
* @param WP_Post $post The comment object. |
| 269 |
* |
| 270 |
* @return string The filtered locale of the comment. |
| 271 |
*/ |
| 272 |
return apply_filters( 'activitypub_comment_locale', $lang, $comment_id, $this->wp_object ); |
| 273 |
} |
| 274 |
} |
| 275 |
|