Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ install_db() {
fi

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
MYSQL_PWD=$DB_PASS mysqladmin create $DB_NAME --user="$DB_USER" $EXTRA
}

install_wp
Expand Down
57 changes: 44 additions & 13 deletions dynamic-featured-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,23 @@ public function featured_meta_box( $post, $featured ) {
$featured_img_full = $this->separate( $featured_img, 'full' );
}

$thumbnail = $this->get_image_thumb( $this->upload_url . $featured_img_full, 'medium' );
if ( empty( $thumbnail ) ) {
// since medium sized thumbnail image is missing,
// let's set full image url as thumbnail.
$thumbnail = $featured_img_full;
$thumbnail = null;
$attachment_id = null;
if ( ! empty( $featured_img_full ) ) {
$attachment_id = $this->get_image_id( $this->upload_url . $featured_img_full );

$thumbnail = $this->get_image_thumb_by_attachment_id( $attachment_id, 'medium' );

if ( empty( $thumbnail ) ) {
// since medium sized thumbnail image is missing,
// let's set full image url as thumbnail.
$thumbnail = $featured_img_full;
}
}

// Add a nonce field.
echo $this->nonce_field( 'dfi_fimageplug-' . $featured_id ); // WPCS: XSS ok.
echo $this->get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post->ID ); // WPCS: XSS ok.
echo $this->get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post->ID, $attachment_id ); // WPCS: XSS ok.
}

/**
Expand All @@ -404,19 +411,20 @@ public function featured_meta_box( $post, $featured ) {
* @access private
*
* @param string $featured_img_trimmed Medium sized image.
* @param string $featured_img Full sized image.
* @param string $featured_id Attachment Id.
* @param string $thumbnail Thumb sized image.
* @param int $post_id Post id.
* @param string $featured_img Full sized image.
* @param string $featured_id Featured id number for translation.
* @param string $thumbnail Thumb sized image.
* @param int $post_id Post id.
* @param int $attachment_id Attachment id.
*
* @return string Html content
*/
private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) {
$has_featured_image = ! empty( $featured_img_trimmed ) ? 'hasFeaturedImage' : '';
private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id, $attachment_id ) {
$has_featured_image = ! empty( $featured_img_trimmed ) ? ' hasFeaturedImage' : '';
$thumbnail = ! is_null( $thumbnail ) ? $thumbnail : '';
$dfi_empty = is_null( $featured_img_trimmed ) ? 'dfiImgEmpty' : '';

return "<a href='javascript:void(0)' class='dfiFeaturedImage {$has_featured_image}' title='" . __( 'Set Featured Image', self::TEXT_DOMAIN ) . "' data-post-id='" . $post_id . "'><span class='dashicons dashicons-camera'></span></a><br/>
return "<a href='javascript:void(0)' class='dfiFeaturedImage{$has_featured_image}' title='" . __( 'Set Featured Image', self::TEXT_DOMAIN ) . "' data-post-id='" . $post_id . "' data-attachment-id='" . $attachment_id . "'><span class='dashicons dashicons-camera'></span></a><br/>
<img src='" . $thumbnail . "' class='dfiImg {$dfi_empty}'/>
<div class='dfiLinks'>
<a href='javascript:void(0)' data-id='{$featured_id}' data-id-local='" . $this->get_number_translation( $featured_id + 1 ) . "' class='dfiAddNew dashicons dashicons-plus' title='" . __( 'Add New', self::TEXT_DOMAIN ) . "'></a>
Expand Down Expand Up @@ -659,6 +667,29 @@ public function get_image_url( $attachment_id, $size = 'full' ) {
return empty( $image_thumb ) ? null : $image_thumb[0];
}

/**
* Get image thumbnail url of specific size by attachment id.
*
* @since 3.7.0
* @access public
*
* @see wp_get_attachment_image_src()
*
* @param int $attachment_id attachment id of an image.
* @param string $size size of the image to fetch (thumbnail, medium, full).
*
* @return string|null
*/
public function get_image_thumb_by_attachment_id( $attachment_id, $size = 'thumbnail' ) {
if ( empty( $attachment_id ) ) {
return null;
}

$image_thumb = wp_get_attachment_image_src( $attachment_id, $size );

return empty( $image_thumb ) ? null : $image_thumb[0];
}

/**
* Get image thumbnail url of specific size by image url.
*
Expand Down
14 changes: 13 additions & 1 deletion js/script-dfi.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,19 @@ jQuery( document ).ready( function ( $ ) {

featuredBox.find( 'img' ).attr( 'src', medium ).fadeIn( 200 );
featuredBox.find( 'input.dfiImageHolder' ).val( dfiFeaturedImages );
} ).open();
} );

dfi_uploader.on('open', function () {
var attached = current.data('attachment-id');
var selection = dfi_uploader.state().get('selection');

if ( attached ) {
selection.add(wp.media.attachment(attached));
}
});

// Open media dialog.
dfi_uploader.open();
} // End if().

return false;
Expand Down
Loading