@@ -795,6 +795,20 @@ private function get_comment_status_array( \WP_Comment $comment ) {
795
795
);
796
796
}
797
797
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
+
798
812
private function get_status_array ( $ post , $ data = array () ) {
799
813
if ( ! $ post ->post_author ) {
800
814
return null ;
@@ -833,7 +847,7 @@ function( $user_id ) {
833
847
'in_reply_to_id ' => null ,
834
848
'in_reply_to_account_id ' => null ,
835
849
'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 ),
837
851
'created_at ' => mysql2date ( 'Y-m-d\TH:i:s.000P ' , $ post ->post_date , false ),
838
852
'edited_at ' => null ,
839
853
'emojis ' => array (),
@@ -867,39 +881,55 @@ function( $user_id ) {
867
881
}
868
882
$ img = substr ( $ data ['content ' ], $ p , $ e - $ p + 19 );
869
883
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
874
890
|| ( isset ( $ img_tag ['class ' ] ) && preg_match ( '#\bwp-image- ' . $ attachment_id . '\b# ' , $ img_tag ['class ' ] ) )
875
891
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
+ }
880
900
}
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
+ );
881
911
}
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
- );
892
912
}
893
913
$ data ['content ' ] = trim ( substr ( $ data ['content ' ], 0 , $ p ) . substr ( $ data ['content ' ], $ e + 19 ) );
894
914
$ p = strpos ( $ data ['content ' ], '<!-- wp:image ' );
895
915
}
896
916
897
917
foreach ( $ attachments as $ attachment_id => $ attachment ) {
898
- $ attachment_metadata = \wp_get_attachment_metadata ( $ attachment_id );
899
918
$ 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
+
900
930
$ data ['media_attachments ' ][] = array (
901
931
'id ' => $ attachment_id ,
902
- 'type ' => ' image ' ,
932
+ 'type ' => $ type ,
903
933
'url ' => $ url ,
904
934
'preview_url ' => $ url ,
905
935
'text_url ' => $ url ,
@@ -966,6 +996,9 @@ public function api_submit_post( $request ) {
966
996
}
967
997
968
998
$ status = make_clickable ( $ status );
999
+ if ( class_exists ( '\Activitypub\Mention ' ) ) {
1000
+ $ status = \Activitypub \Mention::the_content ( $ status );
1001
+ }
969
1002
970
1003
$ visibility = $ request ->get_param ( 'visibility ' );
971
1004
if ( empty ( $ visibility ) ) {
0 commit comments