Skip to content

Commit

Permalink
Merge pull request #912 from Codeinwp/fix/mime_content_type
Browse files Browse the repository at this point in the history
Fix/mime content type
  • Loading branch information
vytisbulkevicius authored Apr 10, 2024
2 parents 07b0cad + 08df282 commit e507184
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
31 changes: 30 additions & 1 deletion includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,25 @@ public function tryReuseExistingFeaturedImage( &$result, $title_feed, $post_id =
return true;
}

/**
* Will retireve the file type of a file by its URL.
*
* @param string $url The URL of the file.
*
* @return string
*/
private function get_file_type_by_url( $url ) {
$response = wp_remote_get( $url );

// wp_remote_retrieve_header can return an array if there are multiple headers with the same name
$content_type = wp_remote_retrieve_header( $response, 'content-type' );
if ( is_array( $content_type ) ) {
$content_type = $content_type[0];
}

return $content_type;
}

/**
* Downloads and sets a post featured image if possible.
*
Expand Down Expand Up @@ -2019,7 +2038,17 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title
return false;
}

$type = mime_content_type( $local_file );
$type = '';
// try first to get the file type using the built-in function if available.
if ( function_exists( 'mime_content_type' ) ) {
$type = mime_content_type( $local_file );
}

// if the file type is not found, try to get it from the URL.
if ( empty( $type ) ) {
$type = $this->get_file_type_by_url( $img_source_url );
}

// the file is downloaded with a .tmp extension
// if the URL mentions the extension of the file, the upload succeeds
// but if the URL is like https://source.unsplash.com/random, then the upload fails
Expand Down
35 changes: 31 additions & 4 deletions tests/test-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
class Test_Feedzy_Import extends WP_UnitTestCase {

private $import_limit = 1;

/**
* Sets up the test methods.
*/
Expand All @@ -28,10 +30,10 @@ public function setUp(): void {
* @access public
* @dataProvider importDataProvider
*/
public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic_tags = '[#item_content]', $use_filter = false ) {
public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic_tags = '[#item_content]', $use_filter = false, $type = 'post' ) {
do_action( 'init' );

$num_items = 1;
$num_items = $this->import_limit;
$user_id = $this->factory->user->create(
array(
'role' => 'administrator',
Expand Down Expand Up @@ -76,7 +78,7 @@ public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic
$_POST['feedzy_post_nonce'] = wp_create_nonce( 'feedzy_post_nonce' );
$_POST['post_type'] = 'feedzy_imports';
$_POST['feedzy_meta_data']['source'] = $slug;
$_POST['feedzy_meta_data']['import_post_type'] = 'post';
$_POST['feedzy_meta_data']['import_post_type'] = $type;
$_POST['feedzy_meta_data']['import_post_term'] = 'category_' . $category_id;
$_POST['feedzy_meta_data']['import_post_status'] = 'publish';
$_POST['feedzy_meta_data']['inc_key'] = '';
Expand Down Expand Up @@ -106,7 +108,7 @@ public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic
$import_custom_fields = get_post_meta( $p->ID, 'imports_custom_fields', true );
$import_feed_limit = get_post_meta( $p->ID, 'import_feed_limit', true );

$this->assertEquals( 'post', $import_post_type );
$this->assertEquals( $type, $import_post_type );
$this->assertEquals( 'category_' . $category_id, $import_post_term );
$this->assertEquals( 'publish', $import_post_status );
$this->assertEquals( $slug, $source );
Expand Down Expand Up @@ -174,6 +176,13 @@ public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic

do_action( 'feedzy_cron', '1' );

/**
* We bail for non post types as the subsequent tests might not apply.
*/
if ( $type !== 'post' ) {
return;
}

$created = get_posts(
array(
'numberposts' => $num_items,
Expand Down Expand Up @@ -276,6 +285,24 @@ public function test_canonical_url( $post ) {

}

/**
* Test the attachment import works and the mime type is correct.
*/
public function test_attachement_import() {
$this->test_feedzy_imports( $this->get_rand_name(), $this->get_rand_name(), $this->get_two_rand_feeds(), '[#item_content]', false, 'attachment' );
$args = array(
'post_type' =>'attachment',
'numberposts' => -1,
'orderby' => 'date',
'order' => 'DESC',
);
$attachments = get_posts($args);
$this->assertEquals( $this->import_limit, count( $attachments ) );

$this->assertTrue( isset( $attachments[0]->post_mime_type ) );
$this->assertTrue( $attachments[0]->post_mime_type === 'image/jpeg' );
}


/**
* Utility method to generate a random 5 char string.
Expand Down

0 comments on commit e507184

Please sign in to comment.