Skip to content

Commit

Permalink
Merge pull request #12 from junaidbhura/default-crop-admin
Browse files Browse the repository at this point in the history
Default crop options in WP admin
  • Loading branch information
junaidbhura authored Oct 26, 2018
2 parents 2129ac5 + aa68b9b commit f5619a7
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 25 deletions.
30 changes: 30 additions & 0 deletions admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@
<input name="cloudinary_auto_mapping_folder" id="cloudinary_auto_mapping_folder" value="<?php echo esc_html( get_option( 'cloudinary_auto_mapping_folder' ) ); ?>" class="regular-text" type="text">
</td>
</tr>
<tr>
<th scope="row"><label for="cloudinary_default_hard_crop"><?php esc_html_e( 'Default Hard Crop', 'cloudinary' ); ?></label></th>
<td>
<select name="cloudinary_default_hard_crop" id="cloudinary_default_hard_crop">
<option value="fill"
<?php if ( 'fill' === get_option( 'cloudinary_default_hard_crop' ) ) : ?>
selected="selected"
<?php endif; ?>>fill</option>
<option value="fit"
<?php if ( 'fit' === get_option( 'cloudinary_default_hard_crop' ) ) : ?>
selected="selected"
<?php endif; ?>>fit</option>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="cloudinary_default_soft_crop"><?php esc_html_e( 'Default Soft Crop', 'cloudinary' ); ?></label></th>
<td>
<select name="cloudinary_default_soft_crop" id="cloudinary_default_soft_crop">
<option value="fit"
<?php if ( 'fit' === get_option( 'cloudinary_default_soft_crop' ) ) : ?>
selected="selected"
<?php endif; ?>>fit</option>
<option value="fill"
<?php if ( 'fill' === get_option( 'cloudinary_default_soft_crop' ) ) : ?>
selected="selected"
<?php endif; ?>>fill</option>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="cloudinary_urls"><?php esc_html_e( 'URLs', 'cloudinary' ); ?></label></th>
<td>
Expand Down
2 changes: 1 addition & 1 deletion auto-cloudinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: Auto Cloudinary
Description: Super simple Cloudinary auto-upload implementation for WordPress.
Version: 1.0.3
Version: 1.1.0
Author: Junaid Bhura
Author URI: https://junaidbhura.com
Text Domain: cloudinary
Expand Down
2 changes: 2 additions & 0 deletions inc/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function options_page() {
) {
update_option( 'cloudinary_cloud_name', sanitize_text_field( $_POST['cloudinary_cloud_name'] ) );
update_option( 'cloudinary_auto_mapping_folder', sanitize_text_field( $_POST['cloudinary_auto_mapping_folder'] ) );
update_option( 'cloudinary_default_hard_crop', sanitize_text_field( $_POST['cloudinary_default_hard_crop'] ) );
update_option( 'cloudinary_default_soft_crop', sanitize_text_field( $_POST['cloudinary_default_soft_crop'] ) );
$urls = trim( sanitize_textarea_field( $_POST['cloudinary_urls'] ) );
if ( empty( $urls ) ) {
$urls = 'https://res.cloudinary.com';
Expand Down
15 changes: 15 additions & 0 deletions inc/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Core {
public $_setup = false;
public $_cloud_name = '';
public $_auto_mapping_folder = '';
public $_default_hard_crop = '';
public $_default_soft_crop = '';
public $_options = array();
public $_urls = array();
private $_url_counter = 0;
Expand All @@ -32,13 +34,26 @@ public static function get_instance() {
public function setup() {
$this->_cloud_name = apply_filters( 'cloudinary_cloud_name', get_option( 'cloudinary_cloud_name' ) );
$this->_auto_mapping_folder = apply_filters( 'cloudinary_auto_mapping_folder', get_option( 'cloudinary_auto_mapping_folder' ) );
$this->_default_hard_crop = get_option( 'cloudinary_default_hard_crop' );
$this->_default_soft_crop = get_option( 'cloudinary_default_soft_crop' );
$this->_options['urls'] = get_option( 'cloudinary_urls' );
$upload_dir = wp_upload_dir();
$this->_options['upload_url'] = apply_filters( 'cloudinary_upload_url', $upload_dir['baseurl'] );

if ( ! empty( $this->_cloud_name ) && ! empty( $this->_auto_mapping_folder ) ) {
$this->_setup = true;
}

/**
* Users before 1.1.0 might not have updated their options.
* This is to support them.
*/
if ( empty( $this->_default_hard_crop ) ) {
$this->_default_hard_crop = apply_filters( 'cloudinary_default_hard_crop', apply_filters( 'cloudinary_default_crop', 'fill' ) );
}
if ( empty( $this->_default_soft_crop ) ) {
$this->_default_soft_crop = apply_filters( 'cloudinary_default_hard_crop', apply_filters( 'cloudinary_default_crop', 'fill' ) );
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions inc/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ function cloudinary_get_original_url( $id = 0 ) {
* @return string
*/
function cloudinary_default_crop( $hard_crop = false ) {
$core = JB\Cloudinary\Core::get_instance();
if ( $hard_crop ) {
return apply_filters( 'cloudinary_default_hard_crop', apply_filters( 'cloudinary_default_crop', 'fill' ) );
return apply_filters( 'cloudinary_default_hard_crop', apply_filters( 'cloudinary_default_crop', $core->_default_hard_crop ) );
} else {
return apply_filters( 'cloudinary_default_soft_crop', apply_filters( 'cloudinary_default_crop', 'fill' ) );
return apply_filters( 'cloudinary_default_soft_crop', apply_filters( 'cloudinary_default_crop', $core->_default_soft_crop ) );
}
}
}
7 changes: 5 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Contributors: junaidbhura
Tags: cloudinary, dynamic-images, cdn, image-optimization, image-manipulation
Requires at least: 4.4
Tested up to: 4.9
Tested up to: 5.0
Requires PHP: 5.6
Stable tag: 1.0.3
Stable tag: 1.1.0

Super simple Cloudinary auto-upload implementation for WordPress.

Expand Down Expand Up @@ -140,6 +140,9 @@ Activate the plugin through the 'Plugins' menu in WordPress.

== Changelog ==

= 1.1.0 =
* Added default crop options to the WP Admin [#10](https://github.com/junaidbhura/auto-cloudinary/issues/10)

= 1.0.3 =
* New filters for default hard and soft crops [#2](https://github.com/junaidbhura/auto-cloudinary/issues/2). Props [@petersplugins](https://github.com/petersplugins)
* Performance improvements
Expand Down
Binary file modified screenshot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 33 additions & 20 deletions tests/test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ static function setUpBeforeClass() {
self::$_image_id = self::upload_image();
self::$_attached_file = get_attached_file( self::$_image_id );

update_option( 'cloudinary_default_hard_crop', 'fill' );
update_option( 'cloudinary_default_soft_crop', 'fit' );
update_option( 'cloudinary_content_images', '1' );
JB\Cloudinary\bootstrap();
}
Expand Down Expand Up @@ -161,7 +163,7 @@ function test_get_url() {
function test_update_content_images() {
cloudinary_ignore_start();
$content = 'Test content. ' . get_image_tag( self::$_image_id, '', '', 'none', 'full' );
$updated_content = str_replace( self::$_upload_dir['baseurl'], 'https://res-1.cloudinary.com/test-cloud/w_1920,h_1080,c_fill/test-auto-folder', $content );
$updated_content = str_replace( self::$_upload_dir['baseurl'], 'https://res-1.cloudinary.com/test-cloud/w_1920,h_1080,c_fit/test-auto-folder', $content );
cloudinary_ignore_end();

$this->assertEquals( cloudinary_update_content_images( $content ), $updated_content, 'Content image incorrect.' );
Expand All @@ -174,22 +176,22 @@ function test_update_content_images() {
function test_image_srcset() {
$image_path = $this->get_image_path();
$srcset_full = array(
'https://res-3.cloudinary.com/test-cloud/w_1920,c_fill/test-auto-folder' . $image_path . ' 1920w',
'https://res-1.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $image_path . ' 300w',
'https://res-2.cloudinary.com/test-cloud/w_768,h_432,c_fill/test-auto-folder' . $image_path . ' 768w',
'https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $image_path . ' 1024w',
'https://res-3.cloudinary.com/test-cloud/w_1920,c_fit/test-auto-folder' . $image_path . ' 1920w',
'https://res-1.cloudinary.com/test-cloud/w_300,h_169,c_fit/test-auto-folder' . $image_path . ' 300w',
'https://res-2.cloudinary.com/test-cloud/w_768,h_432,c_fit/test-auto-folder' . $image_path . ' 768w',
'https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $image_path . ' 1024w',
);
$srcset_large = array(
'https://res-2.cloudinary.com/test-cloud/w_1920,c_fill/test-auto-folder' . $image_path . ' 1920w',
'https://res-3.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $image_path . ' 300w',
'https://res-1.cloudinary.com/test-cloud/w_768,h_432,c_fill/test-auto-folder' . $image_path . ' 768w',
'https://res-2.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $image_path . ' 1024w',
'https://res-2.cloudinary.com/test-cloud/w_1920,c_fit/test-auto-folder' . $image_path . ' 1920w',
'https://res-3.cloudinary.com/test-cloud/w_300,h_169,c_fit/test-auto-folder' . $image_path . ' 300w',
'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_1024,h_576,c_fit/test-auto-folder' . $image_path . ' 1024w',
);
$srcset_medium = array(
'https://res-1.cloudinary.com/test-cloud/w_1920,c_fill/test-auto-folder' . $image_path . ' 1920w',
'https://res-2.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $image_path . ' 300w',
'https://res-3.cloudinary.com/test-cloud/w_768,h_432,c_fill/test-auto-folder' . $image_path . ' 768w',
'https://res-1.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $image_path . ' 1024w',
'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_768,h_432,c_fit/test-auto-folder' . $image_path . ' 768w',
'https://res-1.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $image_path . ' 1024w',
);

$this->assertEquals( wp_get_attachment_image_srcset( self::$_image_id, 'full' ), implode( ', ', $srcset_full ) );
Expand All @@ -205,13 +207,13 @@ function test_wp_get_attachment_image_src() {
$this->assertEquals( $src[0], 'https://res-2.cloudinary.com/test-cloud/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );

$src = wp_get_attachment_image_src( self::$_image_id, 'large' );
$this->assertEquals( $src[0], 'https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );
$this->assertEquals( $src[0], 'https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );

$src = wp_get_attachment_image_src( self::$_image_id, 'medium' );
$this->assertEquals( $src[0], 'https://res-1.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );
$this->assertEquals( $src[0], 'https://res-1.cloudinary.com/test-cloud/w_300,h_169,c_fit/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );

$src = wp_get_attachment_image_src( self::$_image_id, array( 100, 100 ) );
$this->assertEquals( $src[0], 'https://res-2.cloudinary.com/test-cloud/w_100,h_100,c_fill/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );
$this->assertEquals( $src[0], 'https://res-2.cloudinary.com/test-cloud/w_100,h_100,c_fit/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );
}

/**
Expand All @@ -220,7 +222,7 @@ function test_wp_get_attachment_image_src() {
function test_wp_get_attachment_image() {
$image_path = $this->get_image_path();

$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" title="Test Title" srcset="https://res-1.cloudinary.com/test-cloud/w_1920,c_fill/test-auto-folder' . $image_path . ' 1920w, https://res-2.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $image_path . ' 300w, https://res-3.cloudinary.com/test-cloud/w_768,h_432,c_fill/test-auto-folder' . $image_path . ' 768w, https://res-1.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $image_path . ' 1024w" sizes="(max-width: 1920px) 100vw, 1920px" />';
$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" title="Test Title" 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_768,h_432,c_fit/test-auto-folder' . $image_path . ' 768w, https://res-1.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $image_path . ' 1024w" sizes="(max-width: 1920px) 100vw, 1920px" />';
$this->assertEquals(
wp_get_attachment_image( self::$_image_id, 'full', false, array(
'alt' => 'Test Alt',
Expand All @@ -230,27 +232,38 @@ function test_wp_get_attachment_image() {
'Incorrect attachment image.'
);

$test_string = '<img width="300" height="169" src="https://res-2.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $image_path . '" class="attachment-medium size-medium" alt="" srcset="https://res-3.cloudinary.com/test-cloud/w_1920,c_fill/test-auto-folder' . $image_path . ' 1920w, https://res-1.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $image_path . ' 300w, https://res-2.cloudinary.com/test-cloud/w_768,h_432,c_fill/test-auto-folder' . $image_path . ' 768w, https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $image_path . ' 1024w" sizes="(max-width: 300px) 100vw, 300px" />';
$test_string = '<img width="300" height="169" src="https://res-2.cloudinary.com/test-cloud/w_300,h_169,c_fit/test-auto-folder' . $image_path . '" class="attachment-medium size-medium" alt="" srcset="https://res-3.cloudinary.com/test-cloud/w_1920,c_fit/test-auto-folder' . $image_path . ' 1920w, https://res-1.cloudinary.com/test-cloud/w_300,h_169,c_fit/test-auto-folder' . $image_path . ' 300w, https://res-2.cloudinary.com/test-cloud/w_768,h_432,c_fit/test-auto-folder' . $image_path . ' 768w, https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $image_path . ' 1024w" sizes="(max-width: 300px) 100vw, 300px" />';
$this->assertEquals(
wp_get_attachment_image( self::$_image_id, 'medium' ),
$test_string,
'Incorrect attachment image.'
);

$test_string = '<img width="1024" height="576" src="https://res-1.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $image_path . '" class="attachment-large size-large" alt="" srcset="https://res-2.cloudinary.com/test-cloud/w_1920,c_fill/test-auto-folder' . $image_path . ' 1920w, https://res-3.cloudinary.com/test-cloud/w_300,h_169,c_fill/test-auto-folder' . $image_path . ' 300w, https://res-1.cloudinary.com/test-cloud/w_768,h_432,c_fill/test-auto-folder' . $image_path . ' 768w, https://res-2.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $image_path . ' 1024w" sizes="(max-width: 1024px) 100vw, 1024px" />';
$test_string = '<img width="1024" height="576" src="https://res-1.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $image_path . '" class="attachment-large size-large" alt="" srcset="https://res-2.cloudinary.com/test-cloud/w_1920,c_fit/test-auto-folder' . $image_path . ' 1920w, https://res-3.cloudinary.com/test-cloud/w_300,h_169,c_fit/test-auto-folder' . $image_path . ' 300w, 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_1024,h_576,c_fit/test-auto-folder' . $image_path . ' 1024w" sizes="(max-width: 1024px) 100vw, 1024px" />';
$this->assertEquals(
wp_get_attachment_image( self::$_image_id, 'large' ),
$test_string,
'Incorrect attachment image.'
);
}

/**
* Test default crop.
*
* @covers cloudinary_default_crop()
*/
function test_default_crop() {
$this->assertEquals( cloudinary_default_crop(), 'fit', 'Incorrect default crop.' );
$this->assertEquals( cloudinary_default_crop( false ), 'fit', 'Incorrect default crop.' );
$this->assertEquals( cloudinary_default_crop( true ), 'fill', 'Incorrect default crop.' );
}

/**
* Test filters.
*/
function test_filters() {
$src = wp_get_attachment_image_src( self::$_image_id, 'large' );
$this->assertEquals( $src[0], 'https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fill/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );
$this->assertEquals( $src[0], 'https://res-3.cloudinary.com/test-cloud/w_1024,h_576,c_fit/test-auto-folder' . $this->get_image_path(), 'Incorrect SRC.' );

add_filter( 'cloudinary_default_args', function( $args ) {
return array(
Expand Down

0 comments on commit f5619a7

Please sign in to comment.