Dynamic Featured Image - Version 3.7.0

Version Description

  • Feature: Auto-select media on open (#78)
  • Fix: Attachment ID issue for edited image (#77, #80)
Download this release

Release Info

Developer ankitpokhrel
Plugin Icon wp plugin Dynamic Featured Image
Version 3.7.0
Comparing to
See all releases

Code changes from version 3.6.8 to 3.7.0

Files changed (3) hide show
  1. dynamic-featured-image.php +60 -19
  2. js/script-dfi.js +13 -1
  3. readme.txt +10 -3
dynamic-featured-image.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Dynamic Featured Image
4
  * Plugin URI: http://wordpress.org/plugins/dynamic-featured-image/
5
  * Description: Dynamically adds multiple featured image or post thumbnail functionality to your posts, pages and custom post types.
6
- * Version: 3.6.8
7
  * Author: Ankit Pokhrel
8
  * Author URI: https://ankitpokhrel.com
9
  * License: GPL2 or later
@@ -41,7 +41,7 @@ if ( ! defined( 'ABSPATH' ) ) {
41
  * Dynamic Featured Image plugin main class.
42
  *
43
  * @author Ankit Pokhrel <info@ankitpokhrel.com>
44
- * @version 3.6.8
45
  */
46
  class Dynamic_Featured_Image {
47
  /**
@@ -49,7 +49,7 @@ class Dynamic_Featured_Image {
49
  *
50
  * @since 3.0.0
51
  */
52
- const VERSION = '3.6.8';
53
 
54
  /**
55
  * Text domain.
@@ -385,16 +385,23 @@ class Dynamic_Featured_Image {
385
  $featured_img_full = $this->separate( $featured_img, 'full' );
386
  }
387
 
388
- $thumbnail = $this->get_image_thumb( $this->upload_url . $featured_img_full, 'medium' );
389
- if ( empty( $thumbnail ) ) {
390
- // since medium sized thumbnail image is missing,
391
- // let's set full image url as thumbnail.
392
- $thumbnail = $featured_img_full;
 
 
 
 
 
 
 
393
  }
394
 
395
  // Add a nonce field.
396
  echo $this->nonce_field( 'dfi_fimageplug-' . $featured_id ); // WPCS: XSS ok.
397
- echo $this->get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post->ID ); // WPCS: XSS ok.
398
  }
399
 
400
  /**
@@ -404,19 +411,20 @@ class Dynamic_Featured_Image {
404
  * @access private
405
  *
406
  * @param string $featured_img_trimmed Medium sized image.
407
- * @param string $featured_img Full sized image.
408
- * @param string $featured_id Attachment Id.
409
- * @param string $thumbnail Thumb sized image.
410
- * @param int $post_id Post id.
 
411
  *
412
  * @return string Html content
413
  */
414
- private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) {
415
- $has_featured_image = ! empty( $featured_img_trimmed ) ? 'hasFeaturedImage' : '';
416
  $thumbnail = ! is_null( $thumbnail ) ? $thumbnail : '';
417
  $dfi_empty = is_null( $featured_img_trimmed ) ? 'dfiImgEmpty' : '';
418
 
419
- 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/>
420
  <img src='" . $thumbnail . "' class='dfiImg {$dfi_empty}'/>
421
  <div class='dfiLinks'>
422
  <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>
@@ -659,6 +667,29 @@ class Dynamic_Featured_Image {
659
  return empty( $image_thumb ) ? null : $image_thumb[0];
660
  }
661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  /**
663
  * Get image thumbnail url of specific size by image url.
664
  *
@@ -694,11 +725,21 @@ class Dynamic_Featured_Image {
694
  $attachment_id = $this->get_attachment_id( $image_url );
695
 
696
  if ( is_null( $attachment_id ) ) {
697
- // check if the image is edited image.
698
- // and try to get the attachment id.
 
 
 
 
 
 
 
 
 
 
699
  $image_url = str_replace( $this->upload_url . '/', '', $image_url );
700
- $row = $this->execute_query( $this->db->prepare( 'SELECT post_id FROM ' . $this->db->postmeta . ' WHERE meta_key = %s AND meta_value = %s', '_wp_attached_file', $image_url ) );
701
 
 
702
  if ( ! is_null( $row ) ) {
703
  $attachment_id = $row;
704
  }
3
  * Plugin Name: Dynamic Featured Image
4
  * Plugin URI: http://wordpress.org/plugins/dynamic-featured-image/
5
  * Description: Dynamically adds multiple featured image or post thumbnail functionality to your posts, pages and custom post types.
6
+ * Version: 3.7.0
7
  * Author: Ankit Pokhrel
8
  * Author URI: https://ankitpokhrel.com
9
  * License: GPL2 or later
41
  * Dynamic Featured Image plugin main class.
42
  *
43
  * @author Ankit Pokhrel <info@ankitpokhrel.com>
44
+ * @version 3.7.0
45
  */
46
  class Dynamic_Featured_Image {
47
  /**
49
  *
50
  * @since 3.0.0
51
  */
52
+ const VERSION = '3.7.0';
53
 
54
  /**
55
  * Text domain.
385
  $featured_img_full = $this->separate( $featured_img, 'full' );
386
  }
387
 
388
+ $thumbnail = null;
389
+ $attachment_id = null;
390
+ if ( ! empty( $featured_img_full ) ) {
391
+ $attachment_id = $this->get_image_id( $this->upload_url . $featured_img_full );
392
+
393
+ $thumbnail = $this->get_image_thumb_by_attachment_id( $attachment_id, 'medium' );
394
+
395
+ if ( empty( $thumbnail ) ) {
396
+ // since medium sized thumbnail image is missing,
397
+ // let's set full image url as thumbnail.
398
+ $thumbnail = $featured_img_full;
399
+ }
400
  }
401
 
402
  // Add a nonce field.
403
  echo $this->nonce_field( 'dfi_fimageplug-' . $featured_id ); // WPCS: XSS ok.
404
+ echo $this->get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post->ID, $attachment_id ); // WPCS: XSS ok.
405
  }
406
 
407
  /**
411
  * @access private
412
  *
413
  * @param string $featured_img_trimmed Medium sized image.
414
+ * @param string $featured_img Full sized image.
415
+ * @param string $featured_id Featured id number for translation.
416
+ * @param string $thumbnail Thumb sized image.
417
+ * @param int $post_id Post id.
418
+ * @param int $attachment_id Attachment id.
419
  *
420
  * @return string Html content
421
  */
422
+ private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id, $attachment_id ) {
423
+ $has_featured_image = ! empty( $featured_img_trimmed ) ? ' hasFeaturedImage' : '';
424
  $thumbnail = ! is_null( $thumbnail ) ? $thumbnail : '';
425
  $dfi_empty = is_null( $featured_img_trimmed ) ? 'dfiImgEmpty' : '';
426
 
427
+ 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/>
428
  <img src='" . $thumbnail . "' class='dfiImg {$dfi_empty}'/>
429
  <div class='dfiLinks'>
430
  <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>
667
  return empty( $image_thumb ) ? null : $image_thumb[0];
668
  }
669
 
670
+ /**
671
+ * Get image thumbnail url of specific size by attachment id.
672
+ *
673
+ * @since 3.7.0
674
+ * @access public
675
+ *
676
+ * @see wp_get_attachment_image_src()
677
+ *
678
+ * @param int $attachment_id attachment id of an image.
679
+ * @param string $size size of the image to fetch (thumbnail, medium, full).
680
+ *
681
+ * @return string|null
682
+ */
683
+ public function get_image_thumb_by_attachment_id( $attachment_id, $size = 'thumbnail' ) {
684
+ if ( empty( $attachment_id ) ) {
685
+ return null;
686
+ }
687
+
688
+ $image_thumb = wp_get_attachment_image_src( $attachment_id, $size );
689
+
690
+ return empty( $image_thumb ) ? null : $image_thumb[0];
691
+ }
692
+
693
  /**
694
  * Get image thumbnail url of specific size by image url.
695
  *
725
  $attachment_id = $this->get_attachment_id( $image_url );
726
 
727
  if ( is_null( $attachment_id ) ) {
728
+ /*
729
+ * Check if the image is an edited image.
730
+ * and try to get the attachment id.
731
+ */
732
+
733
+ global $wp_version;
734
+
735
+ if ( intval( $wp_version ) >= 4 ) {
736
+ return attachment_url_to_postid( $image_url );
737
+ }
738
+
739
+ // Fallback.
740
  $image_url = str_replace( $this->upload_url . '/', '', $image_url );
 
741
 
742
+ $row = $this->execute_query( $this->db->prepare( 'SELECT post_id FROM ' . $this->db->postmeta . ' WHERE meta_key = %s AND meta_value = %s', '_wp_attached_file', $image_url ) );
743
  if ( ! is_null( $row ) ) {
744
  $attachment_id = $row;
745
  }
js/script-dfi.js CHANGED
@@ -128,7 +128,19 @@ jQuery( document ).ready( function ( $ ) {
128
 
129
  featuredBox.find( 'img' ).attr( 'src', medium ).fadeIn( 200 );
130
  featuredBox.find( 'input.dfiImageHolder' ).val( dfiFeaturedImages );
131
- } ).open();
 
 
 
 
 
 
 
 
 
 
 
 
132
  } // End if().
133
 
134
  return false;
128
 
129
  featuredBox.find( 'img' ).attr( 'src', medium ).fadeIn( 200 );
130
  featuredBox.find( 'input.dfiImageHolder' ).val( dfiFeaturedImages );
131
+ } );
132
+
133
+ dfi_uploader.on('open', function () {
134
+ var attached = current.data('attachment-id');
135
+ var selection = dfi_uploader.state().get('selection');
136
+
137
+ if ( attached ) {
138
+ selection.add(wp.media.attachment(attached));
139
+ }
140
+ });
141
+
142
+ // Open media dialog.
143
+ dfi_uploader.open();
144
  } // End if().
145
 
146
  return false;
readme.txt CHANGED
@@ -3,9 +3,9 @@ Contributors: ankitpokhrel, cfoellmann
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=J9FVY3ESPPD58
4
  Tags: dynamic featured image, featured image, post thumbnail, dynamic post thumbnail, multiple featured image, multiple post thumbnail
5
  Requires at least: 3.8
6
- Tested up to: 5.0
7
- Stable tag: 3.6.8
8
- Requires PHP: 5.3
9
  License: GPLv2 or later
10
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -117,6 +117,10 @@ Please feel free to report any bug found at https://github.com/ankitpokhrel/Dyna
117
  3. Add new featured image box.
118
 
119
  == Changelog ==
 
 
 
 
120
  = 3.6.8 =
121
  * Fix slow query issue (#74, Thanks @tobros91)
122
  * Add German translations (#69, Thanks @swiffer)
@@ -234,6 +238,9 @@ Please feel free to report any bug found at https://github.com/ankitpokhrel/Dyna
234
  * Fixed some minor issues.
235
 
236
  == Upgrade Notice ==
 
 
 
237
  = 3.6.8 =
238
  * Some feature and bug fixes.
239
 
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=J9FVY3ESPPD58
4
  Tags: dynamic featured image, featured image, post thumbnail, dynamic post thumbnail, multiple featured image, multiple post thumbnail
5
  Requires at least: 3.8
6
+ Tested up to: 5.1
7
+ Stable tag: 3.7.0
8
+ Requires PHP: 5.4
9
  License: GPLv2 or later
10
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
11
 
117
  3. Add new featured image box.
118
 
119
  == Changelog ==
120
+ = 3.7.0 =
121
+ * Feature: Auto-select media on open (#78)
122
+ * Fix: Attachment ID issue for edited image (#77, #80)
123
+
124
  = 3.6.8 =
125
  * Fix slow query issue (#74, Thanks @tobros91)
126
  * Add German translations (#69, Thanks @swiffer)
238
  * Fixed some minor issues.
239
 
240
  == Upgrade Notice ==
241
+ = 3.7.0 =
242
+ * Autoselect feature and attachment id bug fix.
243
+
244
  = 3.6.8 =
245
  * Some feature and bug fixes.
246