Skip to content

Commit 9929571

Browse files
mattwiebepfefferle
andauthored
Enable Mastodon Apps: support profile editing, blog user (#788)
--------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 9c39c46 commit 9929571

File tree

7 files changed

+343
-72
lines changed

7 files changed

+343
-72
lines changed

includes/class-admin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public static function admin_notices() {
113113
if ( 'edit' === $current_screen->base && Extra_Fields::is_extra_fields_post_type( $current_screen->post_type ) ) {
114114
?>
115115
<div class="notice" style="margin: 0; background: none; border: none; box-shadow: none; padding: 15px 0 0 0; font-size: 14px;">
116-
<?php esc_html_e( 'These are extra fields that are used for your ActivityPub profile. You can use your homepage, social profiles, pronouns, age, anything you want.', 'activitypub' ); ?>
116+
<?php
117+
esc_html_e( 'These are extra fields that are used for your ActivityPub profile. You can use your homepage, social profiles, pronouns, age, anything you want.', 'activitypub' );
118+
?>
117119
</div>
118120
<?php
119121
}

includes/collection/class-extra-fields.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use WP_Query;
77
use Activitypub\Collection\Users;
88

9+
use function Activitypub\site_supports_blocks;
10+
911
class Extra_Fields {
1012

1113
const USER_POST_TYPE = 'ap_extrafield';
@@ -37,6 +39,23 @@ public static function get_actor_fields( $user_id ) {
3739
return apply_filters( 'activitypub_get_actor_extra_fields', $fields, $user_id );
3840
}
3941

42+
public static function get_formatted_content( $post ) {
43+
$content = \get_the_content( null, false, $post );
44+
$content = Link::the_content( $content, true );
45+
if ( site_supports_blocks() ) {
46+
$content = \do_blocks( $content );
47+
}
48+
$content = \wptexturize( $content );
49+
$content = \wp_filter_content_tags( $content );
50+
// replace script and style elements
51+
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
52+
$content = \strip_shortcodes( $content );
53+
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
54+
$content = \apply_filters( 'activitypub_extra_field_content', $content, $post );
55+
56+
return $content;
57+
}
58+
4059
/**
4160
* Transforms the Extra Fields (Cutom Post Types) to ActivityPub Actor-Attachments.
4261
*
@@ -56,16 +75,7 @@ function( $rel ) {
5675
);
5776

5877
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-
78+
$content = self::get_formatted_content( $post );
6979
$attachments[] = array(
7080
'type' => 'PropertyValue',
7181
'name' => \get_the_title( $post ),
@@ -211,10 +221,7 @@ function( $rel ) {
211221
'post_title' => $title,
212222
'post_status' => 'publish',
213223
'post_author' => $user_id,
214-
'post_content' => sprintf(
215-
'<!-- wp:paragraph --><p>%s</p><!-- /wp:paragraph -->',
216-
Link::the_content( $url )
217-
),
224+
'post_content' => self::make_paragraph_block( Link::the_content( $url ) ),
218225
'comment_status' => 'closed',
219226
'menu_order' => $menu_order,
220227
);
@@ -231,6 +238,13 @@ function( $rel ) {
231238
return $extra_fields;
232239
}
233240

241+
public static function make_paragraph_block( $content ) {
242+
if ( ! site_supports_blocks() ) {
243+
return $content;
244+
}
245+
return '<!-- wp:paragraph --><p>' . $content . '</p><!-- /wp:paragraph -->';
246+
}
247+
234248
/**
235249
* Checks if the user is the blog user.
236250
* @param int $user_id The user ID.

includes/model/class-blog.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ public function get_preferred_username() {
206206
* @return array The User-Icon.
207207
*/
208208
public function get_icon() {
209-
// try site icon first
210-
$icon_id = get_option( 'site_icon' );
209+
// try site_logo, falling back to site_icon, first
210+
$icon_id = get_option( 'site_logo', get_option( 'site_icon' ) );
211211

212212
// try custom logo second
213213
if ( ! $icon_id ) {
@@ -390,11 +390,57 @@ public function get_indexable() {
390390
}
391391

392392
/**
393-
* Get the User-Hashtags.
393+
* Update the User-Name.
394+
*
395+
* @param mixed $value The new value.
396+
* @return bool True if the attribute was updated, false otherwise.
397+
*/
398+
public function update_name( $value ) {
399+
return \update_option( 'blogname', $value );
400+
}
401+
402+
/**
403+
* Update the User-Description.
404+
*
405+
* @param mixed $value The new value.
406+
* @return bool True if the attribute was updated, false otherwise.
407+
*/
408+
public function update_summary( $value ) {
409+
return \update_option( 'blogdescription', $value );
410+
}
411+
412+
/**
413+
* Update the User-Icon.
414+
*
415+
* @param mixed $value The new value.
416+
* @return bool True if the attribute was updated, false otherwise.
417+
*/
418+
public function update_icon( $value ) {
419+
if ( ! wp_attachment_is_image( $value ) ) {
420+
return false;
421+
}
422+
return \update_option( 'site_logo', $value ) && \update_option( 'site_icon', $value );
423+
}
424+
425+
/**
426+
* Update the User-Header-Image.
427+
*
428+
* @param mixed $value The new value.
429+
* @return bool True if the attribute was updated, false otherwise.
430+
*/
431+
public function update_header( $value ) {
432+
if ( ! wp_attachment_is_image( $value ) ) {
433+
return false;
434+
}
435+
return \update_option( 'activitypub_header_image', $value );
436+
}
437+
438+
/**
439+
* Get the User - Hashtags .
394440
*
395441
* @see https://docs.joinmastodon.org/spec/activitypub/#Hashtag
396442
*
397-
* @return array The User-Hashtags.
443+
* @return array The User - Hashtags .
398444
*/
399445
public function get_tag() {
400446
$hashtags = array();

includes/model/class-user.php

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ public function get_preferred_username() {
136136
}
137137

138138
public function get_icon() {
139+
$icon = \get_user_option( 'activitypub_icon', $this->_id );
140+
if ( wp_attachment_is_image( $icon ) ) {
141+
return array(
142+
'type' => 'Image',
143+
'url' => esc_url( wp_get_attachment_url( $icon ) ),
144+
);
145+
}
146+
139147
$icon = \esc_url(
140148
\get_avatar_url(
141149
$this->_id,
@@ -153,12 +161,12 @@ public function get_image() {
153161
$header_image = get_user_option( 'activitypub_header_image', $this->_id );
154162
$image_url = null;
155163

156-
if ( $header_image ) {
157-
$image_url = \wp_get_attachment_url( $header_image );
164+
if ( ! $header_image && \has_header_image() ) {
165+
$image_url = \get_header_image();
158166
}
159167

160-
if ( ! $image_url && \has_header_image() ) {
161-
$image_url = \get_header_image();
168+
if ( $header_image ) {
169+
$image_url = \wp_get_attachment_url( $header_image );
162170
}
163171

164172
if ( $image_url ) {
@@ -278,4 +286,52 @@ public function get_indexable() {
278286
return false;
279287
}
280288
}
289+
290+
291+
/**
292+
* Update the User-Name.
293+
*
294+
* @param mixed $value The new value.
295+
* @return bool True if the attribute was updated, false otherwise.
296+
*/
297+
public function update_name( $value ) {
298+
$userdata = [ 'ID' => $this->_id, 'display_name' => $value ];
299+
return \wp_update_user( $userdata );
300+
}
301+
302+
/**
303+
* Update the User-Description.
304+
*
305+
* @param mixed $value The new value.
306+
* @return bool True if the attribute was updated, false otherwise.
307+
*/
308+
public function update_summary( $value ) {
309+
return \update_user_option( $this->_id, 'activitypub_description', $value );
310+
}
311+
312+
/**
313+
* Update the User-Icon.
314+
*
315+
* @param mixed $value The new value. Should be an attachment ID.
316+
* @return bool True if the attribute was updated, false otherwise.
317+
*/
318+
public function update_icon( $value ) {
319+
if ( ! wp_attachment_is_image( $value ) ) {
320+
return false;
321+
}
322+
return update_user_option( $this->_id, 'activitypub_icon', $value );
323+
}
324+
325+
/**
326+
* Update the User-Header-Image.
327+
*
328+
* @param mixed $value The new value. Should be an attachment ID.
329+
* @return bool True if the attribute was updated, false otherwise.
330+
*/
331+
public function update_header( $value ) {
332+
if ( ! wp_attachment_is_image( $value ) ) {
333+
return false;
334+
}
335+
return \update_user_option( $this->_id, 'activitypub_header_image', $value );
336+
}
281337
}

0 commit comments

Comments
 (0)