Dynamic Featured Image - Version 3.5.2

Version Description

  • Fix image url if image already points to CDN. PR #50
  • Internal refactorings
Download this release

Release Info

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

Code changes from version 3.5.1 to 3.5.2

dynamic-featured-image.php CHANGED
@@ -1,35 +1,35 @@
1
  <?php
2
  /***
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.5.1
7
- Author: Ankit Pokhrel
8
- Author URI: http://ankitpokhrel.com.np
9
- License: GPL2 or later
10
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
- Text Domain: dynamic-featured-image
12
- Domain Path: /languages
13
- GitHub Plugin URI: https://github.com/ankitpokhrel/Dynamic-Featured-Image
14
-
15
- Copyright (C) 2013 Ankit Pokhrel <ankitpokhrel@gmail.com, http://ankitpokhrel.com.np>,
16
-
17
- This program is free software; you can redistribute it and/or modify
18
- it under the terms of the GNU General Public License as published by
19
- the Free Software Foundation; either version 3 of the License, or
20
- (at your option) any later version.
21
-
22
- This program is distributed in the hope that it will be useful,
23
- but WITHOUT ANY WARRANTY; without even the implied warranty of
24
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
- GNU General Public License for more details.
26
-
27
- You should have received a copy of the GNU General Public License
28
- along with this program. If not, see <http://www.gnu.org/licenses/>.
29
  */
30
 
31
  // Avoid direct calls to this file
32
- if ( !defined( 'ABSPATH' ) ) {
33
  header( 'Status: 403 Forbidden' );
34
  header( 'HTTP/1.1 403 Forbidden' );
35
  exit();
@@ -42,14 +42,13 @@ if ( !defined( 'ABSPATH' ) ) {
42
  * @author Ankit Pokhrel <ankitpokhrel@gmail.com>
43
  * @version 3.0.1
44
  */
45
- class Dynamic_Featured_Image
46
- {
47
  /**
48
  * Current version of the plugin.
49
  *
50
  * @since 3.0.0
51
  */
52
- const VERSION = '3.5.0';
53
 
54
  /* Image upload directory */
55
  private $__upload_dir;
@@ -76,35 +75,34 @@ class Dynamic_Featured_Image
76
  * @access public
77
  * @global object $wpdb
78
  *
79
- * @see add_action()
80
  */
81
- public function __construct()
82
- {
83
  $this->_textDomain = 'dynamic-featured-image';
84
 
85
  //plugin update warning
86
- add_action( 'in_plugin_update_message-' . plugin_basename(__FILE__), array( $this, 'update_notice' ) );
87
 
88
  add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
89
- add_action( 'add_meta_boxes', array( $this, 'initialize_featured_box' ) );
90
  add_action( 'save_post', array( $this, 'save_meta' ) );
91
- add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
92
 
93
  //handle ajax request
94
  add_action( 'wp_ajax_dfiMetaBox_callback', array( $this, 'ajax_callback' ) );
95
 
96
  //add action links
97
- add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this, 'dfi_action_links') );
98
 
99
  //media uploader custom fields
100
- add_filter( 'attachment_fields_to_edit', array($this, 'media_attachment_custom_fields'), 10, 2 );
101
- add_filter( 'attachment_fields_to_save', array($this, 'media_attachment_custom_fields_save'), 10, 2 );
102
 
103
  //get the site protocol
104
  $protocol = $this->__get_protocol();
105
 
106
  $this->__upload_dir = wp_upload_dir();
107
- $this->__upload_url = preg_replace('#^https?://#', '', $this->__upload_dir['baseurl']);
108
 
109
  //add protocol to the upload url
110
  $this->__upload_url = $protocol . $this->__upload_url;
@@ -122,13 +120,12 @@ class Dynamic_Featured_Image
122
  *
123
  * @since 3.5.1
124
  * @access public
125
- *
126
  * @return string
127
  */
128
- private function __get_protocol()
129
- {
130
- return ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||
131
- (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ) ? "https://" : "http://";
132
  }
133
 
134
  /**
@@ -143,24 +140,23 @@ class Dynamic_Featured_Image
143
  *
144
  * @return Void
145
  */
146
- public function enqueue_admin_scripts()
147
- {
148
  //enqueue styles
149
  wp_enqueue_style( 'style-dfi', plugins_url( '/css/style-dfi.css', __FILE__ ), array(), self::VERSION );
150
  wp_enqueue_style( 'dashicons', plugins_url( '/css/dashicons.css', __FILE__ ), array(), self::VERSION );
151
 
152
  //register script
153
- wp_register_script( 'scripts-dfi', plugins_url( '/js/script-dfi.js', __FILE__), array( 'jquery' ), self::VERSION );
154
 
155
  //localize the script with required data
156
  wp_localize_script(
157
  'scripts-dfi',
158
  'WP_SPECIFIC',
159
  array(
160
- 'upload_url' => $this->__upload_url,
161
- 'metabox_title' => __($this->_metabox_title, $this->_textDomain),
162
- 'mediaSelector_title' => __('Dynamic Featured Image - Media Selector', $this->_textDomain),
163
- 'mediaSelector_buttonText' => __('Set Featured Image', $this->_textDomain)
164
  )
165
  );
166
 
@@ -179,13 +175,13 @@ class Dynamic_Featured_Image
179
  * @codeCoverageIgnore
180
  *
181
  * @param array $links Action links
 
182
  * @return array
183
  */
184
- public function dfi_action_links($links)
185
- {
186
  $upgrade_link = array(
187
- '<a href="http://ankitpokhrel.com.np/blog/downloads/dynamic-featured-image-pro/" target="_blank">Upgrade to Premium</a>'
188
- );
189
 
190
  return array_merge( $links, $upgrade_link );
191
 
@@ -205,51 +201,51 @@ class Dynamic_Featured_Image
205
  *
206
  * @return Void
207
  */
208
- public function initialize_featured_box()
209
- {
210
  global $post;
211
 
212
  //make metabox title dynamic
213
- $this->_metabox_title = apply_filters('dfi_set_metabox_title', __("Featured Image"));
214
 
215
- $featuredData = get_post_meta( $post->ID, 'dfiFeatured', true );
216
  $totalFeatured = count( $featuredData );
217
 
218
- $defaultFilter = array( 'attachment', 'revision', 'nav_menu_item' );
219
- $this->_userFilter = apply_filters('dfi_post_type_user_filter', $this->_userFilter);
220
- $filter = array_merge($defaultFilter, $this->_userFilter);
221
 
222
  $postTypes = get_post_types();
223
  $postTypes = array_diff( $postTypes, $filter );
224
 
225
- $postTypes = apply_filters('dfi_post_types', $postTypes);
226
 
227
- if ( !empty($featuredData) && $totalFeatured >= 1 ) {
228
  $i = 2;
229
- foreach ($featuredData as $featured) {
230
- self::_dfi_add_meta_box($postTypes, $featured, $i);
231
- $i++;
232
  }
233
  } else {
234
- self::_dfi_add_meta_box($postTypes);
235
  }
236
 
237
  } // END initialize_featured_box()
238
 
239
  /**
240
  * Translates more than one digit number digit by digit.
 
241
  * @param Integer $number Integer to be translated
 
242
  * @return String Translated number
243
  */
244
- protected function _get_number_translation($number)
245
- {
246
- if ($number <= 9) {
247
- return __($number, $this->_textDomain);
248
  } else {
249
- $pieces = str_split($number, 1);
250
  $buffer = '';
251
- foreach ($pieces as $piece) {
252
- $buffer .= __($piece, $this->_textDomain);
253
  }
254
 
255
  return $buffer;
@@ -258,18 +254,19 @@ class Dynamic_Featured_Image
258
 
259
  /**
260
  * adds meta boxes
261
- * @param Array $postTypes post types to show featured image box
262
- * @param Object $featured callback arguments
263
- * @param Integer $i index of the featured image
 
 
264
  * @return Void
265
  */
266
- private function _dfi_add_meta_box($postTypes, $featured = null, $i = null)
267
- {
268
- if ( !is_null($i) ) {
269
- foreach ($postTypes as $type) {
270
  add_meta_box(
271
  'dfiFeaturedMetaBox-' . $i,
272
- __($this->_metabox_title, $this->_textDomain) . " " . self::_get_number_translation($i),
273
  array( $this, 'featured_meta_box' ),
274
  $type,
275
  'side',
@@ -280,10 +277,10 @@ class Dynamic_Featured_Image
280
  }
281
 
282
  } else {
283
- foreach ($postTypes as $type) {
284
  add_meta_box(
285
  'dfiFeaturedMetaBox',
286
- __( $this->_metabox_title, $this->_textDomain ) . " " . __(2, $this->_textDomain),
287
  array( $this, 'featured_meta_box' ),
288
  $type,
289
  'side',
@@ -300,21 +297,20 @@ class Dynamic_Featured_Image
300
  * Separate thumb and full image url from given URL string
301
  *
302
  * @since 3.3.1
303
- *
304
- * @param string $urlString [description]
305
  * @param string $state Thumb or full
306
- *
307
  * @return string|null
308
  */
309
- private function _separate($urlString, $state = 'thumb')
310
- {
311
  $imagePiece = explode( ',', $urlString );
312
 
313
- if( $state == 'thumb' ) {
314
- return isset($imagePiece[0]) ? $imagePiece[0] : null;
315
  }
316
 
317
- return isset($imagePiece[1]) ? $imagePiece[1] : null;
318
  }
319
 
320
  /**
@@ -326,13 +322,13 @@ class Dynamic_Featured_Image
326
  * @see plugin_basename()
327
  *
328
  * @codeCoverageIgnore
329
- *
330
  * @param string $key Nonce key
 
331
  * @return string
332
  */
333
- protected function _nonce_field( $key )
334
- {
335
- return wp_nonce_field(plugin_basename(__FILE__), $key, true, false);
336
  }
337
 
338
  /**
@@ -346,28 +342,27 @@ class Dynamic_Featured_Image
346
  *
347
  * @return Void
348
  */
349
- public function featured_meta_box($post, $featured)
350
- {
351
  $featuredImg = $featured['args'][0];
352
- $featuredId = is_null( $featured['args'][1] ) ? 2 : --$featured['args'][1];
353
 
354
  $featuredImgTrimmed = $featuredImgFull = $featuredImg;
355
- if ( !is_null( $featuredImg ) ) {
356
- $featuredImgTrimmed = self::_separate($featuredImg);
357
- $featuredImgFull = self::_separate($featuredImg, 'full');
358
  }
359
 
360
  try {
361
 
362
  $thumbnail = $this->get_image_thumb( $this->__upload_url . $featuredImgFull, 'medium' );
363
- if ( is_null($thumbnail) ) {
364
 
365
  //medium sized thumbnail image is missing
366
- throw new Exception("Medium size image not found", 1);
367
 
368
  }
369
 
370
- } catch (Exception $e) {
371
 
372
  //since medium sized thumbnail image was not found,
373
  //let's set full image url as thumbnail
@@ -376,8 +371,8 @@ class Dynamic_Featured_Image
376
  }
377
 
378
  //Add a nonce field
379
- echo $this->_nonce_field('dfi_fimageplug-' . $featuredId);
380
- echo self::_get_featured_box($featuredImgTrimmed, $featuredImg, $featuredId, $thumbnail, $post->ID);
381
 
382
  } // END featured_meta_box()
383
 
@@ -387,23 +382,22 @@ class Dynamic_Featured_Image
387
  * @access private
388
  *
389
  * @param String $featuredImgTrimmed Medium sized image
390
- * @param String $featuredImg Full sized image
391
- * @param String $featuredId Attachment Id
392
- * @param String $thumbnail Thumb sized image
393
  *
394
  * @return String Html content
395
  */
396
- private function _get_featured_box($featuredImgTrimmed, $featuredImg, $featuredId, $thumbnail, $postId)
397
- {
398
- $hasFeaturedImage = !empty($featuredImgTrimmed) ? 'hasFeaturedImage' : '';
399
- $thumbnail = !is_null($thumbnail) ? $thumbnail : '';
400
- $dfiEmpty = is_null($featuredImgTrimmed) ? 'dfiImgEmpty' : '';
401
 
402
- return "<a href='javascript:void(0)' class='dfiFeaturedImage {$hasFeaturedImage}' title='". __('Set Featured Image', $this->_textDomain) . "' data-post-id='" . $postId . "'><span class='dashicons dashicons-camera'></span></a><br/>
403
  <img src='" . $thumbnail . "' class='dfiImg {$dfiEmpty}'/>
404
  <div class='dfiLinks'>
405
- <a href='javascript:void(0)' data-id='{$featuredId}' data-id-local='" . $this->_get_number_translation( ($featuredId + 1) ) . "' class='dfiAddNew dashicons dashicons-plus' title='" . __('Add New', $this->_textDomain) ."'></a>
406
- <a href='javascript:void(0)' class='dfiRemove dashicons dashicons-minus' title='" . __('Remove', $this->_textDomain) . "'></a>
407
  </div>
408
  <div class='dfiClearFloat'></div>
409
  <input type='hidden' name='dfiFeatured[]' value='{$featuredImg}' class='dfiImageHolder' />";
@@ -417,9 +411,8 @@ class Dynamic_Featured_Image
417
  *
418
  * @return Void
419
  */
420
- public function ajax_callback()
421
- {
422
- $featuredId = isset($_POST['id']) ? (int) strip_tags( trim( $_POST['id'] ) ) : null;
423
 
424
  if ( is_null( $featuredId ) ) {
425
  return;
@@ -427,16 +420,21 @@ class Dynamic_Featured_Image
427
 
428
  echo $this->_nonce_field( 'dfi_fimageplug-' . $featuredId );
429
  ?>
430
- <a href="javascript:void(0)" class="dfiFeaturedImage" title="<?php echo __('Set Featured Image', $this->_textDomain) ?>"><span class="dashicons dashicons-camera"></span></a><br/>
431
- <img src="" class="dfiImg dfiImgEmpty"/>
432
- <div class="dfiLinks">
433
- <a href="javascript:void(0)" data-id="<?php echo $featuredId ?>" data-id-local="<?php echo self::_get_number_translation( ($featuredId + 1) ) ?>" class="dfiAddNew dashicons dashicons-plus" title="<?php echo __('Add New', $this->_textDomain) ?>"></a>
434
- <a href="javascript:void(0)" class="dfiRemove dashicons dashicons-minus" title="<?php echo __('Remove', $this->_textDomain) ?>"></a>
435
- </div>
436
- <div class="dfiClearFloat"></div>
437
- <input type="hidden" name="dfiFeatured[]" value="" class="dfiImageHolder" />
 
 
 
 
 
438
  <?php
439
- wp_die('');
440
 
441
  } // END ajax_callback())
442
 
@@ -452,8 +450,7 @@ class Dynamic_Featured_Image
452
  *
453
  * @return string
454
  */
455
- public function add_metabox_classes($classes)
456
- {
457
  array_push( $classes, 'featured-meta-box' );
458
 
459
  return $classes;
@@ -464,16 +461,15 @@ class Dynamic_Featured_Image
464
  * Add custom fields in media uploader
465
  *
466
  * @since 3.4.0
467
- *
468
  * @param $form_fields Array Fields to include in media attachment form
469
  * @param $post Array Post data
470
- *
471
  * @return Array
472
  */
473
- public function media_attachment_custom_fields( $form_fields, $post )
474
- {
475
  $form_fields['dfi-link-to-image'] = array(
476
- 'label' => _('Link to Image'),
477
  'input' => 'text',
478
  'value' => get_post_meta( $post->ID, '_dfi_link_to_image', true )
479
  );
@@ -489,15 +485,14 @@ class Dynamic_Featured_Image
489
  *
490
  * @param $post Array The post data for database
491
  * @param $attachment Array Attachment fields from $_POST form
492
- *
493
  * @return Array
494
  */
495
- public function media_attachment_custom_fields_save( $post, $attachment )
496
- {
497
- if( isset( $attachment['dfi-link-to-image'] ) ) {
498
  update_post_meta( $post['ID'], '_dfi_link_to_image', $attachment['dfi-link-to-image'] );
499
  }
500
-
501
  return $post;
502
 
503
  } // END media_attachment_custom_fields_save()
@@ -516,14 +511,13 @@ class Dynamic_Featured_Image
516
  *
517
  * @return Void
518
  */
519
- public function save_meta($post_id)
520
- {
521
  //Check autosave
522
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
523
  return false;
524
  }
525
 
526
- if( $this->_verify_nonces() ) {
527
  //Check permission before saving data
528
  if ( current_user_can( 'edit_posts', $post_id ) && isset( $_POST['dfiFeatured'] ) ) {
529
  update_post_meta( $post_id, 'dfiFeatured', $_POST['dfiFeatured'] );
@@ -542,20 +536,19 @@ class Dynamic_Featured_Image
542
  *
543
  * @return boolean
544
  */
545
- protected function _verify_nonces()
546
- {
547
  $keys = array_keys( $_POST );
548
- foreach ($keys as $key) {
549
  if ( preg_match( '/dfi_fimageplug-\d+$/', $key ) ) {
550
  //Verify nonce
551
- if ( !wp_verify_nonce( $_POST[$key], plugin_basename(__FILE__) ) ) {
552
  return false;
553
  }
554
  }
555
  }
556
 
557
  return true;
558
-
559
  } // END _verify_nonces()
560
 
561
  /**
@@ -566,8 +559,7 @@ class Dynamic_Featured_Image
566
  *
567
  * @return Void
568
  */
569
- public function update_notice()
570
- {
571
  $info = __( 'ATTENTION! Please read the <a href="https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki" target="_blank">DOCUMENTATION</a> properly before update.', $this->_textDomain );
572
  echo '<div style="color:red; padding:7px 0;">' . strip_tags( $info, '<a><b><i><span>' ) . '</div>';
573
 
@@ -575,8 +567,7 @@ class Dynamic_Featured_Image
575
 
576
  /** Helper functions */
577
 
578
- private function execute_query($query)
579
- {
580
  return $this->__db->get_var( $query );
581
  }
582
 
@@ -591,9 +582,8 @@ class Dynamic_Featured_Image
591
  *
592
  * @return string
593
  */
594
- protected function _get_attachment_id($image_url)
595
- {
596
- return self::execute_query($this->__db->prepare( "SELECT ID FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ));
597
 
598
  } // END _get_attachment_id()
599
 
@@ -610,8 +600,7 @@ class Dynamic_Featured_Image
610
  *
611
  * @return String
612
  */
613
- public function get_image_url($attachment_id, $size = 'full')
614
- {
615
  $image_thumb = wp_get_attachment_image_src( $attachment_id, $size );
616
 
617
  return empty( $image_thumb ) ? null : $image_thumb[0];
@@ -632,10 +621,9 @@ class Dynamic_Featured_Image
632
  *
633
  * @return String
634
  */
635
- public function get_image_thumb($image_url, $size = 'thumbnail')
636
- {
637
  $attachment_id = $this->get_image_id( $image_url );
638
- $image_thumb = wp_get_attachment_image_src( $attachment_id, $size );
639
 
640
  return empty( $image_thumb ) ? null : $image_thumb[0];
641
 
@@ -643,21 +631,22 @@ class Dynamic_Featured_Image
643
 
644
  /**
645
  * Gets attachment id from given image url
 
646
  * @param String $image_url url of an image
 
647
  * @return Integer|Null attachment id of an image
648
  *
649
  * @since 2.0.0
650
  * @access public
651
  */
652
- public function get_image_id($image_url)
653
- {
654
  $attachment_id = $this->_get_attachment_id( $image_url );
655
- if ( is_null($attachment_id) ) {
656
  //check if the image is edited image
657
  //and try to get the attachment id
658
- $image_url = str_replace($this->__upload_url . "/", '', $image_url);
659
- $row = self::execute_query( $this->__db->prepare( "SELECT post_id FROM " . $this->__db->postmeta . " WHERE meta_value = %s", $image_url ) );
660
- if ( !is_null($row) ) {
661
  $attachment_id = $row;
662
  }
663
  }
@@ -675,8 +664,7 @@ class Dynamic_Featured_Image
675
  *
676
  * @return String
677
  */
678
- public function get_image_title($image_url)
679
- {
680
  return self::execute_query( $this->__db->prepare( "SELECT post_title FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
681
 
682
  } // END get_image_title()
@@ -691,8 +679,7 @@ class Dynamic_Featured_Image
691
  *
692
  * @return String
693
  */
694
- public function get_image_title_by_id($attachment_id)
695
- {
696
  return self::execute_query( $this->__db->prepare( "SELECT post_title FROM " . $this->__db->posts . " WHERE ID = %d", $attachment_id ) );
697
 
698
  } // END get_image_title_by_id()
@@ -707,9 +694,8 @@ class Dynamic_Featured_Image
707
  *
708
  * @return String
709
  */
710
- public function get_image_caption($image_url)
711
- {
712
- return self::execute_query( $this->__db->prepare("SELECT post_excerpt FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
713
 
714
  } // END get_image_caption()
715
 
@@ -723,9 +709,8 @@ class Dynamic_Featured_Image
723
  *
724
  * @return String
725
  */
726
- public function get_image_caption_by_id($attachment_id)
727
- {
728
- return self::execute_query( $this->__db->prepare("SELECT post_excerpt FROM " . $this->__db->posts . " WHERE ID = %d", $attachment_id) );
729
 
730
  } // END get_image_caption_by_id()
731
 
@@ -741,12 +726,11 @@ class Dynamic_Featured_Image
741
  *
742
  * @return String
743
  */
744
- public function get_image_alt($image_url)
745
- {
746
  $attachment = $this->__db->get_col( $this->__db->prepare( "SELECT ID FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
747
 
748
  $alt = null;
749
- if ( !empty( $attachment ) ) {
750
  $alt = get_post_meta( $attachment[0], '_wp_attachment_image_alt' );
751
  }
752
 
@@ -766,11 +750,10 @@ class Dynamic_Featured_Image
766
  *
767
  * @return String
768
  */
769
- public function get_image_alt_by_id($attachment_id)
770
- {
771
- $alt = get_post_meta($attachment_id, '_wp_attachment_image_alt');
772
 
773
- return empty($alt) ? null : $alt[0];
774
 
775
  } // END get_image_alt_by_id()
776
 
@@ -784,8 +767,7 @@ class Dynamic_Featured_Image
784
  *
785
  * @return String
786
  */
787
- public function get_image_description($image_url)
788
- {
789
  return self::execute_query( $this->__db->prepare( "SELECT post_content FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
790
 
791
  } // END get_image_description()
@@ -800,8 +782,7 @@ class Dynamic_Featured_Image
800
  *
801
  * @return String
802
  */
803
- public function get_image_description_by_id($attachment_id)
804
- {
805
  return self::execute_query( $this->__db->prepare( "SELECT post_content FROM " . $this->__db->posts . " WHERE ID = %d", $attachment_id ) );
806
 
807
  } // END get_image_description_by_id()
@@ -816,8 +797,7 @@ class Dynamic_Featured_Image
816
  *
817
  * @return string|null
818
  */
819
- public function get_link_to_image($attachment_id)
820
- {
821
  return get_post_meta( $attachment_id, '_dfi_link_to_image', true );
822
 
823
  } // END get_link_to_image()
@@ -834,15 +814,14 @@ class Dynamic_Featured_Image
834
  *
835
  * @return Array
836
  */
837
- public function get_post_attachment_ids($post_id)
838
- {
839
- $dfiImages = get_post_meta($post_id, 'dfiFeatured', true);
840
 
841
  $retVal = array();
842
- if ( !empty( $dfiImages ) && is_array( $dfiImages ) ) {
843
- foreach ($dfiImages as $dfiImage) {
844
- $dfiImageFull = self::_separate($dfiImage, 'full');
845
- $retVal[] = $this->get_image_id( $this->__upload_url . $dfiImageFull );
846
  }
847
  }
848
 
@@ -859,12 +838,11 @@ class Dynamic_Featured_Image
859
  * @see get_featured_images()
860
  *
861
  * @param Integer $position position of the featured image
862
- * @param Integer $post_id id of the current post
863
  *
864
  * @return Array if found, null otherwise
865
  */
866
- public function get_nth_featured_image($position, $post_id = null)
867
- {
868
  if ( is_null( $post_id ) ) {
869
  global $post;
870
  $post_id = $post->ID;
@@ -872,7 +850,7 @@ class Dynamic_Featured_Image
872
 
873
  $featured_images = $this->get_featured_images( $post_id );
874
 
875
- return isset($featured_images[$position - 2 ]) ? $featured_images[$position - 2] : null;
876
 
877
  } // END get_nth_featured_image()
878
 
@@ -889,12 +867,11 @@ class Dynamic_Featured_Image
889
  *
890
  * @return boolean
891
  */
892
- public function is_attached($attachment_id, $post_id)
893
- {
894
- if( empty($attachment_id) ) {
895
  return false;
896
  }
897
-
898
  $attachment_ids = $this->get_post_attachment_ids( $post_id );
899
 
900
  return in_array( $attachment_id, $attachment_ids ) ? true : false;
@@ -913,33 +890,33 @@ class Dynamic_Featured_Image
913
  *
914
  * @return Array
915
  */
916
- public function get_featured_images($post_id = null)
917
- {
918
  if ( is_null( $post_id ) ) {
919
  global $post;
920
  $post_id = $post->ID;
921
  }
922
 
923
- $dfiImages = get_post_meta($post_id, 'dfiFeatured', true);
924
 
925
  $retImages = array();
926
- if ( !empty( $dfiImages ) && is_array( $dfiImages ) ) {
927
- $dfiImages = array_filter($dfiImages);
928
 
929
  $count = 0;
930
- foreach ($dfiImages as $dfiImage) {
931
- $dfiImageTrimmed = self::_separate($dfiImage);
932
- $dfiImageFull = self::_separate($dfiImage, 'full');
933
 
934
  try {
935
 
936
- $retImages[$count]['thumb'] = $this->_get_real_upload_path( $dfiImageTrimmed );
937
- $retImages[$count]['full'] = $this->_get_real_upload_path( $dfiImageFull );
938
- $retImages[$count]['attachment_id'] = $this->get_image_id( $retImages[$count]['full'] );
939
 
940
- } catch (Exception $e) { /* Ignore the exception and continue with other featured images */ }
 
941
 
942
- $count++;
943
  }
944
  }
945
 
@@ -952,13 +929,14 @@ class Dynamic_Featured_Image
952
  *
953
  * @since 3.1.14
954
  * @access protected
955
- *
956
  * @param string $img
 
957
  * @return string
958
  */
959
  protected function _get_real_upload_path( $img ) {
960
  //check if upload path is already attached
961
- if ( strpos($img, $this->__upload_url) !== false ) {
962
  return $img;
963
  }
964
 
@@ -977,8 +955,7 @@ class Dynamic_Featured_Image
977
  *
978
  * @return Array An array of images or an empty array on failure
979
  */
980
- public function get_all_featured_images($post_id = null)
981
- {
982
  if ( is_null( $post_id ) ) {
983
  global $post;
984
  $post_id = $post->ID;
@@ -988,9 +965,9 @@ class Dynamic_Featured_Image
988
 
989
  $featured_image_array = array();
990
  if ( ! empty( $thumbnail_id ) ) {
991
- $featured_image = array(
992
- 'thumb' => wp_get_attachment_thumb_url( $thumbnail_id ),
993
- 'full' => wp_get_attachment_url( $thumbnail_id ),
994
  'attachment_id' => $thumbnail_id
995
  );
996
  $featured_image_array[] = $featured_image;
@@ -1010,16 +987,15 @@ class Dynamic_Featured_Image
1010
  * @since 1.0.0
1011
  * @access public
1012
  *
1013
- * @see load_plugin_textdomain()
1014
- * @see plugin_basename()
1015
- * @action plugins_loaded
1016
  *
1017
  * @codeCoverageIgnore
1018
  *
1019
- * @return void
1020
  */
1021
- public function load_plugin_textdomain()
1022
- {
1023
  load_plugin_textdomain(
1024
  $this->_textDomain,
1025
  false,
@@ -1037,7 +1013,7 @@ class Dynamic_Featured_Image
1037
  * @since 1.0.0
1038
  * @access public
1039
  *
1040
- * @var object $dynamic_featured_image holds the instantiated class {@uses Dynamic_Featured_Image}
1041
  */
1042
  global $dynamic_featured_image;
1043
  $dynamic_featured_image = new Dynamic_Featured_Image();
1
  <?php
2
  /***
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.5.2
7
+ * Author: Ankit Pokhrel
8
+ * Author URI: http://ankitpokhrel.com.np
9
+ * License: GPL2 or later
10
+ * License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
+ * Text Domain: dynamic-featured-image
12
+ * Domain Path: /languages
13
+ * GitHub Plugin URI: https://github.com/ankitpokhrel/Dynamic-Featured-Image
14
+ *
15
+ * Copyright (C) 2013 Ankit Pokhrel <ankitpokhrel@gmail.com, http://ankitpokhrel.com.np>,
16
+ *
17
+ * This program is free software; you can redistribute it and/or modify
18
+ * it under the terms of the GNU General Public License as published by
19
+ * the Free Software Foundation; either version 3 of the License, or
20
+ * (at your option) any later version.
21
+ *
22
+ * This program is distributed in the hope that it will be useful,
23
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
+ * GNU General Public License for more details.
26
+ *
27
+ * You should have received a copy of the GNU General Public License
28
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
  */
30
 
31
  // Avoid direct calls to this file
32
+ if ( ! defined( 'ABSPATH' ) ) {
33
  header( 'Status: 403 Forbidden' );
34
  header( 'HTTP/1.1 403 Forbidden' );
35
  exit();
42
  * @author Ankit Pokhrel <ankitpokhrel@gmail.com>
43
  * @version 3.0.1
44
  */
45
+ class Dynamic_Featured_Image {
 
46
  /**
47
  * Current version of the plugin.
48
  *
49
  * @since 3.0.0
50
  */
51
+ const VERSION = '3.5.2';
52
 
53
  /* Image upload directory */
54
  private $__upload_dir;
75
  * @access public
76
  * @global object $wpdb
77
  *
78
+ * @see add_action()
79
  */
80
+ public function __construct() {
 
81
  $this->_textDomain = 'dynamic-featured-image';
82
 
83
  //plugin update warning
84
+ add_action( 'in_plugin_update_message-' . plugin_basename( __FILE__ ), array( $this, 'update_notice' ) );
85
 
86
  add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
87
+ add_action( 'add_meta_boxes', array( $this, 'initialize_featured_box' ) );
88
  add_action( 'save_post', array( $this, 'save_meta' ) );
89
+ add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
90
 
91
  //handle ajax request
92
  add_action( 'wp_ajax_dfiMetaBox_callback', array( $this, 'ajax_callback' ) );
93
 
94
  //add action links
95
+ add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'dfi_action_links' ) );
96
 
97
  //media uploader custom fields
98
+ add_filter( 'attachment_fields_to_edit', array( $this, 'media_attachment_custom_fields' ), 10, 2 );
99
+ add_filter( 'attachment_fields_to_save', array( $this, 'media_attachment_custom_fields_save' ), 10, 2 );
100
 
101
  //get the site protocol
102
  $protocol = $this->__get_protocol();
103
 
104
  $this->__upload_dir = wp_upload_dir();
105
+ $this->__upload_url = preg_replace( '#^https?://#', '', $this->__upload_dir['baseurl'] );
106
 
107
  //add protocol to the upload url
108
  $this->__upload_url = $protocol . $this->__upload_url;
120
  *
121
  * @since 3.5.1
122
  * @access public
123
+ *
124
  * @return string
125
  */
126
+ private function __get_protocol() {
127
+ return ( ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) ||
128
+ ( ! empty( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] == 443 ) ) ? "https://" : "http://";
 
129
  }
130
 
131
  /**
140
  *
141
  * @return Void
142
  */
143
+ public function enqueue_admin_scripts() {
 
144
  //enqueue styles
145
  wp_enqueue_style( 'style-dfi', plugins_url( '/css/style-dfi.css', __FILE__ ), array(), self::VERSION );
146
  wp_enqueue_style( 'dashicons', plugins_url( '/css/dashicons.css', __FILE__ ), array(), self::VERSION );
147
 
148
  //register script
149
+ wp_register_script( 'scripts-dfi', plugins_url( '/js/script-dfi.js', __FILE__ ), array( 'jquery' ), self::VERSION );
150
 
151
  //localize the script with required data
152
  wp_localize_script(
153
  'scripts-dfi',
154
  'WP_SPECIFIC',
155
  array(
156
+ 'upload_url' => $this->__upload_url,
157
+ 'metabox_title' => __( $this->_metabox_title, $this->_textDomain ),
158
+ 'mediaSelector_title' => __( 'Dynamic Featured Image - Media Selector', $this->_textDomain ),
159
+ 'mediaSelector_buttonText' => __( 'Set Featured Image', $this->_textDomain )
160
  )
161
  );
162
 
175
  * @codeCoverageIgnore
176
  *
177
  * @param array $links Action links
178
+ *
179
  * @return array
180
  */
181
+ public function dfi_action_links( $links ) {
 
182
  $upgrade_link = array(
183
+ '<a href="http://ankitpokhrel.com.np/blog/downloads/dynamic-featured-image-pro/" target="_blank">Upgrade to Premium</a>'
184
+ );
185
 
186
  return array_merge( $links, $upgrade_link );
187
 
201
  *
202
  * @return Void
203
  */
204
+ public function initialize_featured_box() {
 
205
  global $post;
206
 
207
  //make metabox title dynamic
208
+ $this->_metabox_title = apply_filters( 'dfi_set_metabox_title', __( "Featured Image" ) );
209
 
210
+ $featuredData = get_post_meta( $post->ID, 'dfiFeatured', true );
211
  $totalFeatured = count( $featuredData );
212
 
213
+ $defaultFilter = array( 'attachment', 'revision', 'nav_menu_item' );
214
+ $this->_userFilter = apply_filters( 'dfi_post_type_user_filter', $this->_userFilter );
215
+ $filter = array_merge( $defaultFilter, $this->_userFilter );
216
 
217
  $postTypes = get_post_types();
218
  $postTypes = array_diff( $postTypes, $filter );
219
 
220
+ $postTypes = apply_filters( 'dfi_post_types', $postTypes );
221
 
222
+ if ( ! empty( $featuredData ) && $totalFeatured >= 1 ) {
223
  $i = 2;
224
+ foreach ( $featuredData as $featured ) {
225
+ self::_dfi_add_meta_box( $postTypes, $featured, $i );
226
+ $i ++;
227
  }
228
  } else {
229
+ self::_dfi_add_meta_box( $postTypes );
230
  }
231
 
232
  } // END initialize_featured_box()
233
 
234
  /**
235
  * Translates more than one digit number digit by digit.
236
+ *
237
  * @param Integer $number Integer to be translated
238
+ *
239
  * @return String Translated number
240
  */
241
+ protected function _get_number_translation( $number ) {
242
+ if ( $number <= 9 ) {
243
+ return __( $number, $this->_textDomain );
 
244
  } else {
245
+ $pieces = str_split( $number, 1 );
246
  $buffer = '';
247
+ foreach ( $pieces as $piece ) {
248
+ $buffer .= __( $piece, $this->_textDomain );
249
  }
250
 
251
  return $buffer;
254
 
255
  /**
256
  * adds meta boxes
257
+ *
258
+ * @param Array $postTypes post types to show featured image box
259
+ * @param Object $featured callback arguments
260
+ * @param Integer $i index of the featured image
261
+ *
262
  * @return Void
263
  */
264
+ private function _dfi_add_meta_box( $postTypes, $featured = null, $i = null ) {
265
+ if ( ! is_null( $i ) ) {
266
+ foreach ( $postTypes as $type ) {
 
267
  add_meta_box(
268
  'dfiFeaturedMetaBox-' . $i,
269
+ __( $this->_metabox_title, $this->_textDomain ) . " " . self::_get_number_translation( $i ),
270
  array( $this, 'featured_meta_box' ),
271
  $type,
272
  'side',
277
  }
278
 
279
  } else {
280
+ foreach ( $postTypes as $type ) {
281
  add_meta_box(
282
  'dfiFeaturedMetaBox',
283
+ __( $this->_metabox_title, $this->_textDomain ) . " " . __( 2, $this->_textDomain ),
284
  array( $this, 'featured_meta_box' ),
285
  $type,
286
  'side',
297
  * Separate thumb and full image url from given URL string
298
  *
299
  * @since 3.3.1
300
+ *
301
+ * @param string $urlString [description]
302
  * @param string $state Thumb or full
303
+ *
304
  * @return string|null
305
  */
306
+ private function _separate( $urlString, $state = 'thumb' ) {
 
307
  $imagePiece = explode( ',', $urlString );
308
 
309
+ if ( $state == 'thumb' ) {
310
+ return isset( $imagePiece[0] ) ? $imagePiece[0] : null;
311
  }
312
 
313
+ return isset( $imagePiece[1] ) ? $imagePiece[1] : null;
314
  }
315
 
316
  /**
322
  * @see plugin_basename()
323
  *
324
  * @codeCoverageIgnore
325
+ *
326
  * @param string $key Nonce key
327
+ *
328
  * @return string
329
  */
330
+ protected function _nonce_field( $key ) {
331
+ return wp_nonce_field( plugin_basename( __FILE__ ), $key, true, false );
 
332
  }
333
 
334
  /**
342
  *
343
  * @return Void
344
  */
345
+ public function featured_meta_box( $post, $featured ) {
 
346
  $featuredImg = $featured['args'][0];
347
+ $featuredId = is_null( $featured['args'][1] ) ? 2 : -- $featured['args'][1];
348
 
349
  $featuredImgTrimmed = $featuredImgFull = $featuredImg;
350
+ if ( ! is_null( $featuredImg ) ) {
351
+ $featuredImgTrimmed = self::_separate( $featuredImg );
352
+ $featuredImgFull = self::_separate( $featuredImg, 'full' );
353
  }
354
 
355
  try {
356
 
357
  $thumbnail = $this->get_image_thumb( $this->__upload_url . $featuredImgFull, 'medium' );
358
+ if ( is_null( $thumbnail ) ) {
359
 
360
  //medium sized thumbnail image is missing
361
+ throw new Exception( "Medium size image not found", 1 );
362
 
363
  }
364
 
365
+ } catch ( Exception $e ) {
366
 
367
  //since medium sized thumbnail image was not found,
368
  //let's set full image url as thumbnail
371
  }
372
 
373
  //Add a nonce field
374
+ echo $this->_nonce_field( 'dfi_fimageplug-' . $featuredId );
375
+ echo self::_get_featured_box( $featuredImgTrimmed, $featuredImg, $featuredId, $thumbnail, $post->ID );
376
 
377
  } // END featured_meta_box()
378
 
382
  * @access private
383
  *
384
  * @param String $featuredImgTrimmed Medium sized image
385
+ * @param String $featuredImg Full sized image
386
+ * @param String $featuredId Attachment Id
387
+ * @param String $thumbnail Thumb sized image
388
  *
389
  * @return String Html content
390
  */
391
+ private function _get_featured_box( $featuredImgTrimmed, $featuredImg, $featuredId, $thumbnail, $postId ) {
392
+ $hasFeaturedImage = ! empty( $featuredImgTrimmed ) ? 'hasFeaturedImage' : '';
393
+ $thumbnail = ! is_null( $thumbnail ) ? $thumbnail : '';
394
+ $dfiEmpty = is_null( $featuredImgTrimmed ) ? 'dfiImgEmpty' : '';
 
395
 
396
+ return "<a href='javascript:void(0)' class='dfiFeaturedImage {$hasFeaturedImage}' title='" . __( 'Set Featured Image', $this->_textDomain ) . "' data-post-id='" . $postId . "'><span class='dashicons dashicons-camera'></span></a><br/>
397
  <img src='" . $thumbnail . "' class='dfiImg {$dfiEmpty}'/>
398
  <div class='dfiLinks'>
399
+ <a href='javascript:void(0)' data-id='{$featuredId}' data-id-local='" . $this->_get_number_translation( ( $featuredId + 1 ) ) . "' class='dfiAddNew dashicons dashicons-plus' title='" . __( 'Add New', $this->_textDomain ) . "'></a>
400
+ <a href='javascript:void(0)' class='dfiRemove dashicons dashicons-minus' title='" . __( 'Remove', $this->_textDomain ) . "'></a>
401
  </div>
402
  <div class='dfiClearFloat'></div>
403
  <input type='hidden' name='dfiFeatured[]' value='{$featuredImg}' class='dfiImageHolder' />";
411
  *
412
  * @return Void
413
  */
414
+ public function ajax_callback() {
415
+ $featuredId = isset( $_POST['id'] ) ? (int) strip_tags( trim( $_POST['id'] ) ) : null;
 
416
 
417
  if ( is_null( $featuredId ) ) {
418
  return;
420
 
421
  echo $this->_nonce_field( 'dfi_fimageplug-' . $featuredId );
422
  ?>
423
+ <a href="javascript:void(0)" class="dfiFeaturedImage"
424
+ title="<?php echo __( 'Set Featured Image', $this->_textDomain ) ?>"><span
425
+ class="dashicons dashicons-camera"></span></a><br/>
426
+ <img src="" class="dfiImg dfiImgEmpty"/>
427
+ <div class="dfiLinks">
428
+ <a href="javascript:void(0)" data-id="<?php echo $featuredId ?>"
429
+ data-id-local="<?php echo self::_get_number_translation( ( $featuredId + 1 ) ) ?>"
430
+ class="dfiAddNew dashicons dashicons-plus" title="<?php echo __( 'Add New', $this->_textDomain ) ?>"></a>
431
+ <a href="javascript:void(0)" class="dfiRemove dashicons dashicons-minus"
432
+ title="<?php echo __( 'Remove', $this->_textDomain ) ?>"></a>
433
+ </div>
434
+ <div class="dfiClearFloat"></div>
435
+ <input type="hidden" name="dfiFeatured[]" value="" class="dfiImageHolder"/>
436
  <?php
437
+ wp_die( '' );
438
 
439
  } // END ajax_callback())
440
 
450
  *
451
  * @return string
452
  */
453
+ public function add_metabox_classes( $classes ) {
 
454
  array_push( $classes, 'featured-meta-box' );
455
 
456
  return $classes;
461
  * Add custom fields in media uploader
462
  *
463
  * @since 3.4.0
464
+ *
465
  * @param $form_fields Array Fields to include in media attachment form
466
  * @param $post Array Post data
467
+ *
468
  * @return Array
469
  */
470
+ public function media_attachment_custom_fields( $form_fields, $post ) {
 
471
  $form_fields['dfi-link-to-image'] = array(
472
+ 'label' => _( 'Link to Image' ),
473
  'input' => 'text',
474
  'value' => get_post_meta( $post->ID, '_dfi_link_to_image', true )
475
  );
485
  *
486
  * @param $post Array The post data for database
487
  * @param $attachment Array Attachment fields from $_POST form
488
+ *
489
  * @return Array
490
  */
491
+ public function media_attachment_custom_fields_save( $post, $attachment ) {
492
+ if ( isset( $attachment['dfi-link-to-image'] ) ) {
 
493
  update_post_meta( $post['ID'], '_dfi_link_to_image', $attachment['dfi-link-to-image'] );
494
  }
495
+
496
  return $post;
497
 
498
  } // END media_attachment_custom_fields_save()
511
  *
512
  * @return Void
513
  */
514
+ public function save_meta( $post_id ) {
 
515
  //Check autosave
516
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
517
  return false;
518
  }
519
 
520
+ if ( $this->_verify_nonces() ) {
521
  //Check permission before saving data
522
  if ( current_user_can( 'edit_posts', $post_id ) && isset( $_POST['dfiFeatured'] ) ) {
523
  update_post_meta( $post_id, 'dfiFeatured', $_POST['dfiFeatured'] );
536
  *
537
  * @return boolean
538
  */
539
+ protected function _verify_nonces() {
 
540
  $keys = array_keys( $_POST );
541
+ foreach ( $keys as $key ) {
542
  if ( preg_match( '/dfi_fimageplug-\d+$/', $key ) ) {
543
  //Verify nonce
544
+ if ( ! wp_verify_nonce( $_POST[ $key ], plugin_basename( __FILE__ ) ) ) {
545
  return false;
546
  }
547
  }
548
  }
549
 
550
  return true;
551
+
552
  } // END _verify_nonces()
553
 
554
  /**
559
  *
560
  * @return Void
561
  */
562
+ public function update_notice() {
 
563
  $info = __( 'ATTENTION! Please read the <a href="https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki" target="_blank">DOCUMENTATION</a> properly before update.', $this->_textDomain );
564
  echo '<div style="color:red; padding:7px 0;">' . strip_tags( $info, '<a><b><i><span>' ) . '</div>';
565
 
567
 
568
  /** Helper functions */
569
 
570
+ private function execute_query( $query ) {
 
571
  return $this->__db->get_var( $query );
572
  }
573
 
582
  *
583
  * @return string
584
  */
585
+ protected function _get_attachment_id( $image_url ) {
586
+ return self::execute_query( $this->__db->prepare( "SELECT ID FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
 
587
 
588
  } // END _get_attachment_id()
589
 
600
  *
601
  * @return String
602
  */
603
+ public function get_image_url( $attachment_id, $size = 'full' ) {
 
604
  $image_thumb = wp_get_attachment_image_src( $attachment_id, $size );
605
 
606
  return empty( $image_thumb ) ? null : $image_thumb[0];
621
  *
622
  * @return String
623
  */
624
+ public function get_image_thumb( $image_url, $size = 'thumbnail' ) {
 
625
  $attachment_id = $this->get_image_id( $image_url );
626
+ $image_thumb = wp_get_attachment_image_src( $attachment_id, $size );
627
 
628
  return empty( $image_thumb ) ? null : $image_thumb[0];
629
 
631
 
632
  /**
633
  * Gets attachment id from given image url
634
+ *
635
  * @param String $image_url url of an image
636
+ *
637
  * @return Integer|Null attachment id of an image
638
  *
639
  * @since 2.0.0
640
  * @access public
641
  */
642
+ public function get_image_id( $image_url ) {
 
643
  $attachment_id = $this->_get_attachment_id( $image_url );
644
+ if ( is_null( $attachment_id ) ) {
645
  //check if the image is edited image
646
  //and try to get the attachment id
647
+ $image_url = str_replace( $this->__upload_url . "/", '', $image_url );
648
+ $row = self::execute_query( $this->__db->prepare( "SELECT post_id FROM " . $this->__db->postmeta . " WHERE meta_value = %s", $image_url ) );
649
+ if ( ! is_null( $row ) ) {
650
  $attachment_id = $row;
651
  }
652
  }
664
  *
665
  * @return String
666
  */
667
+ public function get_image_title( $image_url ) {
 
668
  return self::execute_query( $this->__db->prepare( "SELECT post_title FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
669
 
670
  } // END get_image_title()
679
  *
680
  * @return String
681
  */
682
+ public function get_image_title_by_id( $attachment_id ) {
 
683
  return self::execute_query( $this->__db->prepare( "SELECT post_title FROM " . $this->__db->posts . " WHERE ID = %d", $attachment_id ) );
684
 
685
  } // END get_image_title_by_id()
694
  *
695
  * @return String
696
  */
697
+ public function get_image_caption( $image_url ) {
698
+ return self::execute_query( $this->__db->prepare( "SELECT post_excerpt FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
 
699
 
700
  } // END get_image_caption()
701
 
709
  *
710
  * @return String
711
  */
712
+ public function get_image_caption_by_id( $attachment_id ) {
713
+ return self::execute_query( $this->__db->prepare( "SELECT post_excerpt FROM " . $this->__db->posts . " WHERE ID = %d", $attachment_id ) );
 
714
 
715
  } // END get_image_caption_by_id()
716
 
726
  *
727
  * @return String
728
  */
729
+ public function get_image_alt( $image_url ) {
 
730
  $attachment = $this->__db->get_col( $this->__db->prepare( "SELECT ID FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
731
 
732
  $alt = null;
733
+ if ( ! empty( $attachment ) ) {
734
  $alt = get_post_meta( $attachment[0], '_wp_attachment_image_alt' );
735
  }
736
 
750
  *
751
  * @return String
752
  */
753
+ public function get_image_alt_by_id( $attachment_id ) {
754
+ $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt' );
 
755
 
756
+ return empty( $alt ) ? null : $alt[0];
757
 
758
  } // END get_image_alt_by_id()
759
 
767
  *
768
  * @return String
769
  */
770
+ public function get_image_description( $image_url ) {
 
771
  return self::execute_query( $this->__db->prepare( "SELECT post_content FROM " . $this->__db->posts . " WHERE guid = %s", $image_url ) );
772
 
773
  } // END get_image_description()
782
  *
783
  * @return String
784
  */
785
+ public function get_image_description_by_id( $attachment_id ) {
 
786
  return self::execute_query( $this->__db->prepare( "SELECT post_content FROM " . $this->__db->posts . " WHERE ID = %d", $attachment_id ) );
787
 
788
  } // END get_image_description_by_id()
797
  *
798
  * @return string|null
799
  */
800
+ public function get_link_to_image( $attachment_id ) {
 
801
  return get_post_meta( $attachment_id, '_dfi_link_to_image', true );
802
 
803
  } // END get_link_to_image()
814
  *
815
  * @return Array
816
  */
817
+ public function get_post_attachment_ids( $post_id ) {
818
+ $dfiImages = get_post_meta( $post_id, 'dfiFeatured', true );
 
819
 
820
  $retVal = array();
821
+ if ( ! empty( $dfiImages ) && is_array( $dfiImages ) ) {
822
+ foreach ( $dfiImages as $dfiImage ) {
823
+ $dfiImageFull = self::_separate( $dfiImage, 'full' );
824
+ $retVal[] = $this->get_image_id( $this->__upload_url . $dfiImageFull );
825
  }
826
  }
827
 
838
  * @see get_featured_images()
839
  *
840
  * @param Integer $position position of the featured image
841
+ * @param Integer $post_id id of the current post
842
  *
843
  * @return Array if found, null otherwise
844
  */
845
+ public function get_nth_featured_image( $position, $post_id = null ) {
 
846
  if ( is_null( $post_id ) ) {
847
  global $post;
848
  $post_id = $post->ID;
850
 
851
  $featured_images = $this->get_featured_images( $post_id );
852
 
853
+ return isset( $featured_images[ $position - 2 ] ) ? $featured_images[ $position - 2 ] : null;
854
 
855
  } // END get_nth_featured_image()
856
 
867
  *
868
  * @return boolean
869
  */
870
+ public function is_attached( $attachment_id, $post_id ) {
871
+ if ( empty( $attachment_id ) ) {
 
872
  return false;
873
  }
874
+
875
  $attachment_ids = $this->get_post_attachment_ids( $post_id );
876
 
877
  return in_array( $attachment_id, $attachment_ids ) ? true : false;
890
  *
891
  * @return Array
892
  */
893
+ public function get_featured_images( $post_id = null ) {
 
894
  if ( is_null( $post_id ) ) {
895
  global $post;
896
  $post_id = $post->ID;
897
  }
898
 
899
+ $dfiImages = get_post_meta( $post_id, 'dfiFeatured', true );
900
 
901
  $retImages = array();
902
+ if ( ! empty( $dfiImages ) && is_array( $dfiImages ) ) {
903
+ $dfiImages = array_filter( $dfiImages );
904
 
905
  $count = 0;
906
+ foreach ( $dfiImages as $dfiImage ) {
907
+ $dfiImageTrimmed = self::_separate( $dfiImage );
908
+ $dfiImageFull = self::_separate( $dfiImage, 'full' );
909
 
910
  try {
911
 
912
+ $retImages[ $count ]['thumb'] = $this->_get_real_upload_path( $dfiImageTrimmed );
913
+ $retImages[ $count ]['full'] = $this->_get_real_upload_path( $dfiImageFull );
914
+ $retImages[ $count ]['attachment_id'] = $this->get_image_id( $retImages[ $count ]['full'] );
915
 
916
+ } catch ( Exception $e ) { /* Ignore the exception and continue with other featured images */
917
+ }
918
 
919
+ $count ++;
920
  }
921
  }
922
 
929
  *
930
  * @since 3.1.14
931
  * @access protected
932
+ *
933
  * @param string $img
934
+ *
935
  * @return string
936
  */
937
  protected function _get_real_upload_path( $img ) {
938
  //check if upload path is already attached
939
+ if ( strpos( $img, $this->__upload_url ) !== false || preg_match('/https?:\/\//', $img) ) {
940
  return $img;
941
  }
942
 
955
  *
956
  * @return Array An array of images or an empty array on failure
957
  */
958
+ public function get_all_featured_images( $post_id = null ) {
 
959
  if ( is_null( $post_id ) ) {
960
  global $post;
961
  $post_id = $post->ID;
965
 
966
  $featured_image_array = array();
967
  if ( ! empty( $thumbnail_id ) ) {
968
+ $featured_image = array(
969
+ 'thumb' => wp_get_attachment_thumb_url( $thumbnail_id ),
970
+ 'full' => wp_get_attachment_url( $thumbnail_id ),
971
  'attachment_id' => $thumbnail_id
972
  );
973
  $featured_image_array[] = $featured_image;
987
  * @since 1.0.0
988
  * @access public
989
  *
990
+ * @see load_plugin_textdomain()
991
+ * @see plugin_basename()
992
+ * @action plugins_loaded
993
  *
994
  * @codeCoverageIgnore
995
  *
996
+ * @return void
997
  */
998
+ public function load_plugin_textdomain() {
 
999
  load_plugin_textdomain(
1000
  $this->_textDomain,
1001
  false,
1013
  * @since 1.0.0
1014
  * @access public
1015
  *
1016
+ * @var object $dynamic_featured_image holds the instantiated class {@uses Dynamic_Featured_Image}
1017
  */
1018
  global $dynamic_featured_image;
1019
  $dynamic_featured_image = new Dynamic_Featured_Image();
languages/dynamic-featured-image-bs_BA.mo CHANGED
File without changes
languages/dynamic-featured-image-bs_BA.po CHANGED
File without changes
languages/dynamic-featured-image-he_IL.mo CHANGED
File without changes
languages/dynamic-featured-image-he_IL.po CHANGED
File without changes
languages/dynamic-featured-image-hr_HR.mo CHANGED
File without changes
languages/dynamic-featured-image-hr_HR.po CHANGED
File without changes
languages/dynamic-featured-image-it_IT.mo CHANGED
File without changes
languages/dynamic-featured-image-it_IT.po CHANGED
File without changes
languages/dynamic-featured-image-ne_NP.mo CHANGED
File without changes
languages/dynamic-featured-image-ne_NP.po CHANGED
File without changes
languages/dynamic-featured-image-sr_RS.mo CHANGED
File without changes
languages/dynamic-featured-image-sr_RS.po CHANGED
File without changes
languages/dynamic-featured-image-sv_SE.mo CHANGED
File without changes
languages/dynamic-featured-image-sv_SE.po CHANGED
File without changes
readme.txt CHANGED
@@ -3,19 +3,27 @@ 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.5
6
- Tested up to: 4.3
7
- Stable tag: 3.5.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
  Dynamically adds multiple featured image (post thumbnail) functionality to posts, pages and custom post types.
12
 
13
  == Description ==
 
14
  Dynamically adds multiple featured image or multiple post thumbnail functionality to your page, posts and custom post types. This plugin provides you an interface to add any number of featured image as you want without writing a single line of code. These featured images can then be collected by the various theme functions.
15
 
16
- > **A NOTE ABOUT SUPPORT:** We’re here to help troubleshoot bugs, but please don't set expectations early as the support forums at WordPress.org are only checked once a week. All the support of the Dynamic Featured Image plugin will now be provided from within [this support forum](http://ankitpokhrel.com.np/blog/forums/forum/dynamic-featured-image-free-version/ "Dynamic Featured Image Support Forum").
 
 
 
 
 
 
 
17
 
18
- > Users looking for more timely/in-depth support and extended features are encouraged to check out [Dynamic Featured Image PRO](http://ankitpokhrel.com.np/blog/downloads/dynamic-featured-image-pro/ "Dynamic Featured Image PRO").
19
 
20
  > **FEATURES OF PRO VERSION**
21
  – Settings Page to control the visibility of DFI metabox.
@@ -28,12 +36,6 @@ Dynamically adds multiple featured image or multiple post thumbnail functionalit
28
 
29
  > [Learn more &raquo;](http://ankitpokhrel.com.np/blog/downloads/dynamic-featured-image-pro/ "Dynamic Featured Image PRO")
30
 
31
- **Overview**
32
- Dynamic Featured Image enables the option to have MULTIPLE featured images within a post or page.
33
- This is especially helpful when you use other plugins, post thumbnails or sliders that use featured images.
34
- Why limit yourself to only one featured image if you can do some awesome stuffs with multiple featured image?
35
- DFI allows you to add different number of featured images to each post and page that can be collected by the various theme functions.
36
-
37
  **How it works?**
38
  1. After successfull plugin activation go to `add` or `edit` page of posts or pages and you will notice a box for second featured image.
39
  2. Click `Set featured image`, select required image from "Dynamic Featured Image - Media Selector" popup and click `Set Featured Image`.
@@ -57,7 +59,7 @@ array
57
 
58
  **Resources**
59
  1. [Detail Documentation](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki "Documentation for current ver.").
60
- 2. [DFI Blog](http://ankitpokhrel.com.np/blog/category/dynamic-featured-image/ "DFI Blog").
61
  3. [StackOverflow Tag](http://stackoverflow.com/questions/tagged/dynamic-featured-image "StackOverflow Tag").
62
 
63
  **MultiSite Info**
@@ -101,12 +103,12 @@ This usually happens because of cache. Clear all your cache and try again if you
101
  Yes! It is easily possible from version 3.1.13. A filter is added in the recent version for this purpose. Refer [this thread](http://ankitpokhrel.com.np/blog/is-it-possible-to-make-dfi-work-only-for-certain-post-types/) for mor info.
102
 
103
  = 6. Other problems or questions? =
104
- Other problems? Don't forget to check the [blog](http://ankitpokhrel.com.np/blog/category/dynamic-featured-image/) and learn to create some exciting things using DFI.
105
 
106
  Please use [support forum](http://wordpress.org/support/plugin/dynamic-featured-image) first if you have any question or queries about the project.
107
  If you don't receive any help in support forum then you can directly contact me at `info [at] ankitpokhrel.com.np`. Please atleast wait for 48hrs before sending another request.
108
 
109
- Please feel free to report any bug found at https://github.com/ankitpokhrel/Dynamic-Featured-Image/ or `info [at] ankitpokhrel.com.np`.
110
 
111
  == Screenshots ==
112
  1. New featured image box.
@@ -114,6 +116,10 @@ Please feel free to report any bug found at https://github.com/ankitpokhrel/Dyna
114
  3. Add new featured image box.
115
 
116
  == Changelog ==
 
 
 
 
117
  = 3.5.1 =
118
  * Minor code refactorings.
119
 
@@ -206,6 +212,9 @@ Please feel free to report any bug found at https://github.com/ankitpokhrel/Dyna
206
  * Fixed some minor issues.
207
 
208
  == Upgrade Notice ==
 
 
 
209
  = 3.5.1 =
210
  * Minor code refactorings.
211
 
@@ -234,7 +243,7 @@ This version has translation in 6 different languages, bug fix on edited image a
234
  This version has bug fixes on edited image. View issue #25 in Github for more info.
235
 
236
  = 3.1.0 =
237
- This version has some bug fix and code quality improvement. You may need to change database value manually because the plugin finds the uplod folder
238
  automatically from now on.
239
 
240
  = 3.0.1 =
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.5
6
+ Tested up to: 4.7
7
+ Stable tag: 3.5.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
  Dynamically adds multiple featured image (post thumbnail) functionality to posts, pages and custom post types.
12
 
13
  == Description ==
14
+
15
  Dynamically adds multiple featured image or multiple post thumbnail functionality to your page, posts and custom post types. This plugin provides you an interface to add any number of featured image as you want without writing a single line of code. These featured images can then be collected by the various theme functions.
16
 
17
+ **Overview**
18
+
19
+ Dynamic Featured Image enables the option to have MULTIPLE featured images within a post or page.
20
+ This is especially helpful when you use other plugins, post thumbnails or sliders that use featured images.
21
+ Why limit yourself to only one featured image if you can do some awesome stuffs with multiple featured image?
22
+ DFI allows you to add different number of featured images to each post and page that can be collected by the various theme functions.
23
+
24
+ > **A NOTE ABOUT SUPPORT:** We’re here to help troubleshoot bugs, but please don't set expectations early as the support forums at WordPress.org are only checked once a week. All the support of the Dynamic Featured Image plugin will now be provided from within [this support forum](http://ankitpokhrel.com/explore/forums/forum/dynamic-featured-image-free-version/ "Dynamic Featured Image Support Forum").
25
 
26
+ > Users looking for more timely/in-depth support and extended features are encouraged to check out [Dynamic Featured Image PRO](http://ankitpokhrel.com/explore/dynamic-featured-image-pro/ "Dynamic Featured Image PRO").
27
 
28
  > **FEATURES OF PRO VERSION**
29
  – Settings Page to control the visibility of DFI metabox.
36
 
37
  > [Learn more &raquo;](http://ankitpokhrel.com.np/blog/downloads/dynamic-featured-image-pro/ "Dynamic Featured Image PRO")
38
 
 
 
 
 
 
 
39
  **How it works?**
40
  1. After successfull plugin activation go to `add` or `edit` page of posts or pages and you will notice a box for second featured image.
41
  2. Click `Set featured image`, select required image from "Dynamic Featured Image - Media Selector" popup and click `Set Featured Image`.
59
 
60
  **Resources**
61
  1. [Detail Documentation](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki "Documentation for current ver.").
62
+ 2. [DFI Blog](http://ankitpokhrel.com/explore/category/dynamic-featured-image/ "DFI Blog").
63
  3. [StackOverflow Tag](http://stackoverflow.com/questions/tagged/dynamic-featured-image "StackOverflow Tag").
64
 
65
  **MultiSite Info**
103
  Yes! It is easily possible from version 3.1.13. A filter is added in the recent version for this purpose. Refer [this thread](http://ankitpokhrel.com.np/blog/is-it-possible-to-make-dfi-work-only-for-certain-post-types/) for mor info.
104
 
105
  = 6. Other problems or questions? =
106
+ Other problems? Don't forget to check the [blog](http://ankitpokhrel.com/explore/category/dynamic-featured-image/) and learn to create some exciting things using DFI.
107
 
108
  Please use [support forum](http://wordpress.org/support/plugin/dynamic-featured-image) first if you have any question or queries about the project.
109
  If you don't receive any help in support forum then you can directly contact me at `info [at] ankitpokhrel.com.np`. Please atleast wait for 48hrs before sending another request.
110
 
111
+ Please feel free to report any bug found at https://github.com/ankitpokhrel/Dynamic-Featured-Image/ or `info [at] ankitpokhrel.com`.
112
 
113
  == Screenshots ==
114
  1. New featured image box.
116
  3. Add new featured image box.
117
 
118
  == Changelog ==
119
+ = 3.5.2 =
120
+ * Fix image url if image already points to CDN. PR #50
121
+ * Internal refactorings
122
+
123
  = 3.5.1 =
124
  * Minor code refactorings.
125
 
212
  * Fixed some minor issues.
213
 
214
  == Upgrade Notice ==
215
+ = 3.5.2 =
216
+ * Some refactorings and bug fixes.
217
+
218
  = 3.5.1 =
219
  * Minor code refactorings.
220
 
243
  This version has bug fixes on edited image. View issue #25 in Github for more info.
244
 
245
  = 3.1.0 =
246
+ This version has some bug fix and code quality improvement. You may need to change database value manually because the plugin finds the upload folder
247
  automatically from now on.
248
 
249
  = 3.0.1 =
uninstall.php CHANGED
File without changes