PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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 / collection / class-extra-fields.php

class-extra-fields.php in ActivityPub 3.2.4, at includes/collection/class-extra-fields.php

243 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Activitypub\Collection;
4
5 use Activitypub\Link;
6 use WP_Query;
7 use Activitypub\Collection\Users;
8
9 class Extra_Fields {
10
11 const USER_POST_TYPE = 'ap_extrafield';
12 const BLOG_POST_TYPE = 'ap_extrafield_blog';
13
14 /**
15 * Get the extra fields for a user.
16 *
17 * @param int $user_id The user ID.
18 *
19 * @return \WP_Post[] The extra fields.
20 */
21 public static function get_actor_fields( $user_id ) {
22 $is_blog = self::is_blog( $user_id );
23 $post_type = $is_blog ? self::BLOG_POST_TYPE : self::USER_POST_TYPE;
24 $args = array(
25 'post_type' => $post_type,
26 'nopaging' => true,
27 'orderby' => 'menu_order',
28 'order' => 'ASC',
29 );
30 if ( ! $is_blog ) {
31 $args['author'] = $user_id;
32 }
33
34 $query = new \WP_Query( $args );
35 $fields = $query->posts ?? array();
36
37 return apply_filters( 'activitypub_get_actor_extra_fields', $fields, $user_id );
38 }
39
40 /**
41 * Transforms the Extra Fields (Cutom Post Types) to ActivityPub Actor-Attachments.
42 *
43 * @param \WP_Post[] $fields The extra fields.
44 *
45 * @return array ActivityPub attachments.
46 */
47 public static function fields_to_attachments( $fields ) {
48 $attachments = array();
49 \add_filter(
50 'activitypub_link_rel',
51 function( $rel ) {
52 $rel .= ' me';
53
54 return $rel;
55 }
56 );
57
58 foreach ( $fields as $post ) {
59 $content = \get_the_content( null, false, $post );
60 $content = \do_blocks( $content );
61 $content = \wptexturize( $content );
62 $content = \wp_filter_content_tags( $content );
63 // replace script and style elements
64 $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
65 $content = \strip_shortcodes( $content );
66 $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
67 $content = \apply_filters( 'activitypub_extra_field_content', $content, $post );
68
69 $attachments[] = array(
70 'type' => 'PropertyValue',
71 'name' => \get_the_title( $post ),
72 'value' => \html_entity_decode(
73 $content,
74 \ENT_QUOTES,
75 'UTF-8'
76 ),
77 );
78
79 $link_added = false;
80
81 // Add support for FEP-fb2a, for more information see FEDERATION.md
82 $link_content = \trim( \strip_tags( $content, '<a>' ) );
83 if (
84 \stripos( $link_content, '<a' ) === 0 &&
85 \stripos( $link_content, '<a', 3 ) === false &&
86 \stripos( $link_content, '</a>', \strlen( $link_content ) - 4 ) !== false &&
87 \class_exists( '\WP_HTML_Tag_Processor' )
88 ) {
89 $tags = new \WP_HTML_Tag_Processor( $link_content );
90 $tags->next_tag( 'A' );
91
92 if ( 'A' === $tags->get_tag() ) {
93 $attachment = array(
94 'type' => 'Link',
95 'name' => \get_the_title( $post ),
96 'href' => \esc_url( $tags->get_attribute( 'href' ) ),
97 'rel' => explode( ' ', $tags->get_attribute( 'rel' ) ),
98 );
99
100 $link_added = true;
101 }
102 }
103
104 if ( ! $link_added ) {
105 $attachment = array(
106 'type' => 'Note',
107 'name' => \get_the_title( $post ),
108 'content' => \html_entity_decode(
109 $content,
110 \ENT_QUOTES,
111 'UTF-8'
112 ),
113 );
114 }
115
116 $attachments[] = $attachment;
117 }
118
119 return $attachments;
120 }
121
122 /**
123 * Check if a post type is an extra fields post type.
124 *
125 * @param string $post_type The post type.
126 *
127 * @return bool True if the post type is an extra fields post type, otherwise false.
128 */
129 public static function is_extra_fields_post_type( $post_type ) {
130 return \in_array( $post_type, array( self::USER_POST_TYPE, self::BLOG_POST_TYPE ), true );
131 }
132
133 /**
134 * Check if a post type is the `ap_extrafield` post type.
135 *
136 * @param string $post_type The post type.
137 *
138 * @return bool True if the post type is `ap_extrafield`, otherwise false.
139 */
140 public static function is_extra_field_post_type( $post_type ) {
141 return self::USER_POST_TYPE === $post_type;
142 }
143
144 /**
145 * Check if a post type is the `ap_extrafield_blog` post type.
146 *
147 * @param string $post_type The post type.
148 *
149 * @return bool True if the post type is `ap_extrafield_blog`, otherwise false.
150 */
151 public static function is_extra_field_blog_post_type( $post_type ) {
152 return self::BLOG_POST_TYPE === $post_type;
153 }
154
155 /**
156 * Add default extra fields to an actor.
157 *
158 * @param array $extra_fields The extra fields.
159 * @param int $user_id The User-ID.
160 *
161 * @return array The extra fields.
162 */
163 public static function default_actor_extra_fields( $extra_fields, $user_id ) {
164 // We'll only take action when there are none yet.
165 if ( ! empty( $extra_fields ) ) {
166 return $extra_fields;
167 }
168
169 $is_blog = self::is_blog( $user_id );
170 $already_migrated = $is_blog
171 ? \get_option( 'activitypub_default_extra_fields' )
172 : \get_user_meta( $user_id, 'activitypub_default_extra_fields', true );
173
174 if ( $already_migrated ) {
175 return $extra_fields;
176 }
177
178 \add_filter(
179 'activitypub_link_rel',
180 function( $rel ) {
181 $rel .= ' me';
182
183 return $rel;
184 }
185 );
186
187 $defaults = array(
188 \__( 'Blog', 'activitypub' ) => \home_url( '/' ),
189 );
190
191 if ( ! $is_blog ) {
192 $author_url = \get_the_author_meta( 'user_url', $user_id );
193 $author_posts_url = \get_author_posts_url( $user_id );
194
195 $defaults[ \__( 'Profile', 'activitypub' ) ] = $author_posts_url;
196 if ( $author_url !== $author_posts_url ) {
197 $defaults[ \__( 'Homepage', 'activitypub' ) ] = $author_url;
198 }
199 }
200
201 $post_type = $is_blog ? self::BLOG_POST_TYPE : self::USER_POST_TYPE;
202 $menu_order = 10;
203
204 foreach ( $defaults as $title => $url ) {
205 if ( ! $url ) {
206 continue;
207 }
208
209 $extra_field = array(
210 'post_type' => $post_type,
211 'post_title' => $title,
212 'post_status' => 'publish',
213 'post_author' => $user_id,
214 'post_content' => sprintf(
215 '<!-- wp:paragraph --><p>%s</p><!-- /wp:paragraph -->',
216 Link::the_content( $url )
217 ),
218 'comment_status' => 'closed',
219 'menu_order' => $menu_order,
220 );
221
222 $menu_order += 10;
223 $extra_field_id = wp_insert_post( $extra_field );
224 $extra_fields[] = get_post( $extra_field_id );
225 }
226
227 $is_blog
228 ? \update_option( 'activitypub_default_extra_fields', true )
229 : \update_user_meta( $user_id, 'activitypub_default_extra_fields', true );
230
231 return $extra_fields;
232 }
233
234 /**
235 * Checks if the user is the blog user.
236 * @param int $user_id The user ID.
237 * @return bool True if the user is the blog user, otherwise false.
238 */
239 private static function is_blog( $user_id ) {
240 return Users::BLOG_USER_ID === $user_id;
241 }
242 }
243