PluginProbe
ActivityPub / 8.3.0
ActivityPub v8.3.0
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 8.3.0, at includes/functions-comment.php

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