Skip to content

Commit 883faed

Browse files
committed
Normalize whitespace
1 parent df9605e commit 883faed

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

includes/class-mastodon-api.php

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,20 @@ private function get_comment_status_array( \WP_Comment $comment ) {
795795
);
796796
}
797797

798+
/**
799+
* Strip empty whitespace
800+
*
801+
* @param string $post_content The post content.
802+
*
803+
* @return string The normalized content.
804+
*/
805+
private function normalize_whitespace( $post_content ) {
806+
$post_content = preg_replace( '#<!-- /?wp:paragraph -->\s*<!-- /?wp:paragraph -->#', PHP_EOL, $post_content );
807+
$post_content = preg_replace( '#\n\s*\n+#', PHP_EOL, $post_content );
808+
809+
return trim( $post_content );
810+
}
811+
798812
private function get_status_array( $post, $data = array() ) {
799813
if ( ! $post->post_author ) {
800814
return null;
@@ -833,7 +847,7 @@ function( $user_id ) {
833847
'in_reply_to_id' => null,
834848
'in_reply_to_account_id' => null,
835849
'reblog' => null,
836-
'content' => trim( $post->post_title . PHP_EOL . $post->post_content ),
850+
'content' => $this->normalize_whitespace( $post->post_title . PHP_EOL . $post->post_content ),
837851
'created_at' => mysql2date( 'Y-m-d\TH:i:s.000P', $post->post_date, false ),
838852
'edited_at' => null,
839853
'emojis' => array(),
@@ -867,39 +881,55 @@ function( $user_id ) {
867881
}
868882
$img = substr( $data['content'], $p, $e - $p + 19 );
869883
if ( preg_match( '#<img(?:\s+src="(?P<url>[^"]+)"|\s+width="(?P<width>\d+)"|\s+height="(?P<height>\d+)"|\s+class="(?P<class>[^"]+)|\s+.*="[^"]+)+"#i', $img, $img_tag ) ) {
870-
$media_id = crc32( $img_tag['url'] );
871-
foreach ( $attachments as $attachment_id => $attachment ) {
872-
if (
873-
wp_get_attachment_url( $attachment_id ) === $img_tag['url']
884+
if ( ! empty( $img_tag['url'] ) ) {
885+
$url = $img_tag['url'];
886+
$media_id = crc32( $url );
887+
foreach ( $attachments as $attachment_id => $attachment ) {
888+
if (
889+
wp_get_attachment_url( $attachment_id ) === $url
874890
|| ( isset( $img_tag['class'] ) && preg_match( '#\bwp-image-' . $attachment_id . '\b#', $img_tag['class'] ) )
875891

876-
) {
877-
$media_id = $attachment_id;
878-
unset( $attachments[ $attachment_id ] );
879-
break;
892+
) {
893+
$media_id = $attachment_id;
894+
$attachment_metadata = \wp_get_attachment_metadata( $attachment_id );
895+
$img_tag['width'] = $attachment_metadata['width'];
896+
$img_tag['height'] = $attachment_metadata['height'];
897+
unset( $attachments[ $attachment_id ] );
898+
break;
899+
}
880900
}
901+
$data['media_attachments'][] = array(
902+
'id' => strval( $media_id ),
903+
'type' => 'image',
904+
'url' => $url,
905+
'preview_url' => $url,
906+
'text_url' => $url,
907+
'width' => $img_tag['width'],
908+
'height' => $img_tag['height'],
909+
'description' => $attachment->post_excerpt,
910+
);
881911
}
882-
$data['media_attachments'][] = array(
883-
'id' => strval( $media_id ),
884-
'type' => 'image',
885-
'url' => $img_tag['url'],
886-
'preview_url' => $img_tag['url'],
887-
'text_url' => $img_tag['url'],
888-
'width' => $img_tag['width'],
889-
'height' => $img_tag['height'],
890-
'description' => $attachment->post_excerpt,
891-
);
892912
}
893913
$data['content'] = trim( substr( $data['content'], 0, $p ) . substr( $data['content'], $e + 19 ) );
894914
$p = strpos( $data['content'], '<!-- wp:image' );
895915
}
896916

897917
foreach ( $attachments as $attachment_id => $attachment ) {
898-
$attachment_metadata = \wp_get_attachment_metadata( $attachment_id );
899918
$url = wp_get_attachment_url( $attachment_id );
919+
$attachment_metadata = wp_get_attachment_metadata( $attachment_id );
920+
921+
$type = 'image';
922+
if ( preg_match( '#^image/#', $attachment_metadata['mime-type'] ) || preg_match( '#\.(gif|png|jpe?g)$#i', $url ) ) {
923+
$type = 'image';
924+
} elseif ( preg_match( '#^audio/#', $attachment_metadata['mime-type'] ) || preg_match( '#\.(mp3|m4a|wav|aiff)$#i', $url ) ) {
925+
$type = 'audio';
926+
} elseif ( preg_match( '#^video/#', $attachment_metadata['mime-type'] ) || preg_match( '#\.(mov|mkv|mp4)$#i', $url ) ) {
927+
$type = 'video';
928+
}
929+
900930
$data['media_attachments'][] = array(
901931
'id' => $attachment_id,
902-
'type' => 'image',
932+
'type' => $type,
903933
'url' => $url,
904934
'preview_url' => $url,
905935
'text_url' => $url,
@@ -966,6 +996,9 @@ public function api_submit_post( $request ) {
966996
}
967997

968998
$status = make_clickable( $status );
999+
if ( class_exists( '\Activitypub\Mention' ) ) {
1000+
$status = \Activitypub\Mention::the_content( $status );
1001+
}
9691002

9701003
$visibility = $request->get_param( 'visibility' );
9711004
if ( empty( $visibility ) ) {

0 commit comments

Comments
 (0)