PluginProbe
ActivityPub / 9.0.1
ActivityPub v9.0.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / functions-comment.php

functions-comment.php in ActivityPub 9.0.1, at includes/functions-comment.php

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