Skip to content

Commit

Permalink
Merge pull request #51 from dishitpala/issue-50-video-support
Browse files Browse the repository at this point in the history
Issue 50: Add support for videos
  • Loading branch information
junaidbhura authored Jun 26, 2024
2 parents 8f1b3c8 + b39a348 commit 9fc1ce6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
7 changes: 6 additions & 1 deletion inc/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ public function get_url( $identifier = 0, $args = array() ) {
$url = $this->get_domain() . '/' . $this->_cloud_name;

// If file name is present, add the "images" prefix.
if ( ! empty( $args['file_name'] ) ) {
if ( ! empty( $args['file_name'] ) && ( ! isset( $args['file_type'] ) || 'video' !== $args['file_type'] ) ) {
$url .= '/images';
}

// Add support for the video file type.
if ( ! empty( $args['file_type'] ) && 'video' === $args['file_type'] ) {
$url .= '/video/upload';
}

// Transformations.
if ( ! empty( $args['transform'] ) ) {
$transformations_slug = $this->build_transformation_slug( $args['transform'] );
Expand Down
Binary file added tests/data/video.mp4
Binary file not shown.
99 changes: 89 additions & 10 deletions tests/test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
*/
class JB_Test_Cloudinary_Plugin extends WP_UnitTestCase {

private static $_upload_dir = '';
private static $_image_id = 0;
private static $_attached_file = null;
private static $_upload_dir = '';
private static $_image_id = 0;
private static $_video_id = 0;
private static $_attached_file = null;
private static $_attached_video_file = null;

/**
* Setup.
*/
static function setUpBeforeClass() {
static function setUpBeforeClass(): void {
/**
* Set aspect ratio the same as the original image (1920x1080),
* so that wp_image_matches_ratio() doesn't strip them
Expand All @@ -28,9 +30,11 @@ static function setUpBeforeClass() {

add_image_size( 'different_aspect_ratio', 400, 200, true );

self::$_upload_dir = wp_upload_dir();
self::$_image_id = self::upload_image();
self::$_attached_file = get_attached_file( self::$_image_id );
self::$_upload_dir = wp_upload_dir();
self::$_image_id = self::upload_image();
self::$_video_id = self::upload_video();
self::$_attached_file = get_attached_file( self::$_image_id );
self::$_attached_video_file = get_attached_file( self::$_video_id );

update_option( 'cloudinary_default_hard_crop', 'fill' );
update_option( 'cloudinary_default_soft_crop', 'fit' );
Expand All @@ -46,8 +50,9 @@ static function setUpBeforeClass() {
/**
* Tear down.
*/
static function tearDownAfterClass() {
static function tearDownAfterClass(): void {
wp_delete_attachment( self::$_image_id, true );
wp_delete_attachment( self::$_video_id, true );
}

/**
Expand Down Expand Up @@ -78,6 +83,34 @@ static function upload_image() {
return $image_id;
}

/**
* Upload a video.
*
* @return int|WP_Error
*/
static function upload_video() {
$wp_upload_dir = self::$_upload_dir;

$file_name = $wp_upload_dir['path'] . DIRECTORY_SEPARATOR . 'video-' . rand_str( 6 ) . '.mp4';
$file_type = wp_check_filetype( basename( $file_name ), null );
copy( JB_CLOUDINARY_PATH . '/tests/data/video.mp4', $file_name );

$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $file_name ),
'post_mime_type' => $file_type['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
'post_content' => '',
'post_status' => 'inherit',
);

$video_id = wp_insert_attachment( $attachment, $file_name );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $video_id, $file_name );
wp_update_attachment_metadata( $video_id, $attach_data );

return $video_id;
}

/**
* Get the path to the uploaded image.
*
Expand All @@ -96,6 +129,24 @@ function get_image_path( $file_name = '' ) {
}
}

/**
* Get the path to the uploaded video.
*
* @param string $file_name
* @return string
*/
function get_video_path( $file_name = '' ) {
$file = self::$_attached_video_file;
$file_info = pathinfo( $file );
$wp_upload_dir = self::$_upload_dir;

if ( empty( $file_name ) ) {
return $wp_upload_dir['subdir'] . '/' . $file_info['basename'];
} else {
return $wp_upload_dir['subdir'] . '/' . $file_info['filename'] . '/' . $file_name . '.' . $file_info['extension'];
}
}

/**
* @covers \JB\Cloudinary\Core::setup()
*/
Expand Down Expand Up @@ -163,6 +214,33 @@ function test_get_url() {

$this->assertEquals( cloudinary_url( self::$_image_id, $options_3 ), 'https://res-2.cloudinary.com/test-cloud/images/w_300,h_200,c_fill,q_80,g_face/test-auto-folder' . $image_path_2, 'Incorrect URL.' );
$this->assertEquals( cloudinary_url( $original_image_url, $options_3 ), 'https://res-3.cloudinary.com/test-cloud/images/w_300,h_200,c_fill,q_80,g_face/test-auto-folder' . $image_path_2, 'Incorrect URL.' );

$video_test_file_name = 'video-test-file-name';
$video_path = $this->get_video_path();
$video_path_2 = $this->get_video_path( $video_test_file_name );
$original_video_url = cloudinary_get_original_url( self::$_video_id );
$options = array(
'transform' => array(
'crop' => 'fit',
'height' => 1080,
'width' => 1920,
),
'file_type' => 'video',
);
$options_2 = array(
'file_name' => $video_test_file_name,
'file_type' => 'video',
);
$options_3 = array_merge( $options, $options_2 );

$this->assertEquals( cloudinary_url( self::$_video_id, $options ), 'https://res-1.cloudinary.com/test-cloud/video/upload/c_fit,h_1080,w_1920/test-auto-folder' . $video_path, 'Incorrect URL.' );
$this->assertEquals( cloudinary_url( $original_video_url, $options ), 'https://res-2.cloudinary.com/test-cloud/video/upload/c_fit,h_1080,w_1920/test-auto-folder' . $video_path, 'Incorrect URL.' );

$this->assertEquals( cloudinary_url( self::$_video_id, $options_2 ), 'https://res-3.cloudinary.com/test-cloud/video/upload/test-auto-folder' . $video_path_2, 'Incorrect URL.' );
$this->assertEquals( cloudinary_url( $original_video_url, $options_2 ), 'https://res-1.cloudinary.com/test-cloud/video/upload/test-auto-folder' . $video_path_2, 'Incorrect URL.' );

$this->assertEquals( cloudinary_url( self::$_video_id, $options_3 ), 'https://res-2.cloudinary.com/test-cloud/video/upload/c_fit,h_1080,w_1920/test-auto-folder' . $video_path_2, 'Incorrect URL.' );
$this->assertEquals( cloudinary_url( $original_video_url, $options_3 ), 'https://res-3.cloudinary.com/test-cloud/video/upload/c_fit,h_1080,w_1920/test-auto-folder' . $video_path_2, 'Incorrect URL.' );
}

/**
Expand Down Expand Up @@ -236,8 +314,9 @@ function test_wp_get_attachment_image() {
$test_string = '<img width="1920" height="1080" src="https://res-3.cloudinary.com/test-cloud/test-auto-folder' . $image_path . '" class="attachment-full size-full" alt="Test Alt" decoding="async" title="Test Title" loading="lazy" srcset="https://res-1.cloudinary.com/test-cloud/w_1920,c_fit/test-auto-folder' . $image_path . ' 1920w, https://res-2.cloudinary.com/test-cloud/w_300,h_169,c_fit/test-auto-folder' . $image_path . ' 300w, https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $image_path . ' 1024w, https://res-1.cloudinary.com/test-cloud/w_768,h_432,c_fit/test-auto-folder' . $image_path . ' 768w, https://res-2.cloudinary.com/test-cloud/w_1536,h_864,c_fit/test-auto-folder' . $image_path . ' 1536w" sizes="(max-width: 1920px) 100vw, 1920px" />';
$this->assertEquals(
wp_get_attachment_image( self::$_image_id, 'full', false, array(
'alt' => 'Test Alt',
'title' => 'Test Title',
'alt' => 'Test Alt',
'decoding' => 'async',
'title' => 'Test Title',
) ),
$test_string,
'Incorrect attachment image.'
Expand Down

0 comments on commit 9fc1ce6

Please sign in to comment.