diff --git a/includes/class-shortcodes.php b/includes/class-shortcodes.php index 6ac73c03c..10018dd47 100644 --- a/includes/class-shortcodes.php +++ b/includes/class-shortcodes.php @@ -166,24 +166,26 @@ public static function content( $atts, $content, $tag ) { if ( empty( $content ) ) { $content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true ); } - } else { - $content = \get_post_field( 'post_content', $item ); + } - if ( 'yes' === $atts['apply_filters'] ) { - /** This filter is documented in wp-includes/post-template.php */ - $content = \apply_filters( 'the_content', $content ); - } else { - $content = do_blocks( $content ); - $content = wptexturize( $content ); - $content = wp_filter_content_tags( $content ); - } + if ( empty( $content ) ) { + $content = \get_post_field( 'post_content', $item ); + } - // Replace script and style elements. - $content = \preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $content ); - $content = strip_shortcodes( $content ); - $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); + if ( 'yes' === $atts['apply_filters'] ) { + /** This filter is documented in wp-includes/post-template.php */ + $content = \apply_filters( 'the_content', $content ); + } else { + $content = do_blocks( $content ); + $content = wptexturize( $content ); + $content = wp_filter_content_tags( $content ); } + // Replace script and style elements. + $content = \preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $content ); + $content = strip_shortcodes( $content ); + $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); + add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) ); return $content; diff --git a/includes/transformer/class-attachment.php b/includes/transformer/class-attachment.php index ef3e1d1fd..65f500ca8 100644 --- a/includes/transformer/class-attachment.php +++ b/includes/transformer/class-attachment.php @@ -24,11 +24,11 @@ class Attachment extends Post { * @return array The Attachments. */ protected function get_attachment() { - $mime_type = get_post_mime_type( $this->item->ID ); - $media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type ); - $type = ''; + $mime_type = \get_post_mime_type( $this->item->ID ); + $mime_type_parts = \explode( '/', $mime_type ); + $type = ''; - switch ( $media_type ) { + switch ( $mime_type_parts[0] ) { case 'audio': case 'video': $type = 'Document'; diff --git a/tests/includes/transformer/class-test-attachment.php b/tests/includes/transformer/class-test-attachment.php new file mode 100644 index 000000000..6887da527 --- /dev/null +++ b/tests/includes/transformer/class-test-attachment.php @@ -0,0 +1,167 @@ +attachment->create_object( + array( + 'post_type' => 'attachment', + 'post_mime_type' => 'image/jpeg', + 'post_title' => 'Test Image', + 'post_content' => 'Test Image Description', + ) + ); + } + + /** + * Clean up after tests. + */ + public static function wpTearDownAfterClass() { + wp_delete_post( self::$attachment_id, true ); + } + + /** + * Test get_type method. + * + * @covers ::get_type + */ + public function test_get_type() { + $attachment = get_post( self::$attachment_id ); + $transformer = new Attachment( $attachment ); + $type = $this->get_protected_method( $transformer, 'get_type' ); + + $this->assertEquals( 'Note', $type ); + } + + /** + * Test get_attachment method with different mime types. + * + * @covers ::get_attachment + * @dataProvider provide_mime_types + */ + public function test_get_attachment( $mime_type, $expected_type ) { + $attachment_id = self::factory()->attachment->create_object( + array( + 'post_type' => 'attachment', + 'post_mime_type' => $mime_type, + ) + ); + + $attachment = get_post( $attachment_id ); + $transformer = new Attachment( $attachment ); + $result = $this->get_protected_method( $transformer, 'get_attachment' ); + + $this->assertIsArray( $result ); + $this->assertEquals( $expected_type, $result['type'] ); + $this->assertEquals( $mime_type, $result['mediaType'] ); + $this->assertArrayHasKey( 'url', $result ); + + wp_delete_post( $attachment_id, true ); + } + + /** + * Test get_attachment method with alt text. + * + * @covers ::get_attachment + */ + public function test_get_attachment_with_alt() { + $alt_text = 'Test Alt Text'; + update_post_meta( self::$attachment_id, '_wp_attachment_image_alt', $alt_text ); + + $attachment = get_post( self::$attachment_id ); + $transformer = new Attachment( $attachment ); + $result = $this->get_protected_method( $transformer, 'get_attachment' ); + + $this->assertArrayHasKey( 'name', $result ); + $this->assertEquals( $alt_text, $result['name'] ); + } + + /** + * Test to_object method. + * + * @covers ::to_object + */ + public function test_to_object() { + $attachment = get_post( self::$attachment_id ); + $transformer = new Attachment( $attachment ); + $object = $transformer->to_object(); + + $this->assertEquals( 'Note', $object->get_type() ); + $this->assertEquals( home_url( '?p=' . self::$attachment_id ), $object->get_id() ); + $this->assertNull( $object->get_name() ); + } + + /** + * Data provider for mime types. + * + * @return array Test data. + */ + public function provide_mime_types() { + return array( + 'image' => array( + 'image/jpeg', + 'Image', + ), + 'audio' => array( + 'audio/mpeg', + 'Document', + ), + 'video' => array( + 'video/mp4', + 'Document', + ), + 'pdf' => array( + 'application/pdf', + '', + ), + 'text' => array( + 'text/plain', + '', + ), + ); + } + + /** + * Helper method to access protected methods. + * + * @param object $object Object instance. + * @param string $method_name Method name. + * @param array $parameters Optional parameters. + * + * @return mixed Method result. + */ + protected function get_protected_method( $object, $method_name, $parameters = array() ) { + $reflection = new \ReflectionClass( get_class( $object ) ); + $method = $reflection->getMethod( $method_name ); + $method->setAccessible( true ); + + return $method->invokeArgs( $object, $parameters ); + } +}