Default featured image - Version 1.6.4

Version Description

  • get_post_meta($post_id) without specifying the meta_key didn't find the DFI. It will now even use an even deeper level and set it in the core cache.
Download this release

Release Info

Developer janw.oostendorp
Plugin Icon 128x128 Default featured image
Version 1.6.4
Comparing to
See all releases

Code changes from version 1.6.3 to 1.6.4

Files changed (2) hide show
  1. readme.txt +5 -2
  2. set-default-featured-image.php +35 -29
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: janwoostendorp
3
  Tags: media, image
4
  Requires at least: 3.5
5
- Tested up to: 5.4.1
6
  Requires PHP: 5.5
7
- Stable tag: 1.6.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -102,6 +102,9 @@ yes you can with the filter `dfi_thumbnail_html`.
102
 
103
  == Changelog ==
104
 
 
 
 
105
  = 1.6.3 =
106
  * Fixed plugin header which blocked installing it.
107
 
2
  Contributors: janwoostendorp
3
  Tags: media, image
4
  Requires at least: 3.5
5
+ Tested up to: 5.5.1
6
  Requires PHP: 5.5
7
+ Stable tag: 1.6.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
102
 
103
  == Changelog ==
104
 
105
+ = 1.6.4 =
106
+ * `get_post_meta($post_id)` without specifying the meta_key didn't find the DFI. It will now even use an even deeper level and set it in the core cache.
107
+
108
  = 1.6.3 =
109
  * Fixed plugin header which blocked installing it.
110
 
set-default-featured-image.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Default featured image
4
  * Plugin URI: http://wordpress.org/extend/plugins/default-featured-image/
5
  * Description: Allows users to select a default featured image in the media settings
6
- * Version: 1.6.3
7
  * Requires at least: 4.0
8
  * Requires PHP: 5.6
9
  * Author: Jan Willem Oostendorp
@@ -12,7 +12,7 @@
12
  * Text Domain: default-featured-image
13
  */
14
  class Default_Featured_Image {
15
- const VERSION = '1.6.3';
16
 
17
  /**
18
  * Hook everything
@@ -52,10 +52,9 @@ class Default_Featured_Image {
52
  }
53
 
54
  /**
55
- * Mostly the same as `get_metadata()` makes sure any post thumbnail function gets checked at
56
- * the deepest level possible.
57
  *
58
- * @param null|mixed $null Should be null, because we want to override the meta value.
59
  * @param int $object_id ID of the object metadata is for.
60
  * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
61
  * the specified object.
@@ -63,46 +62,53 @@ class Default_Featured_Image {
63
  * specified meta_key. This parameter has no effect if meta_key is not specified.
64
  *
65
  * @return string|array Single metadata value, or array of values
66
- * @see get_metadata() in /wp-includes/meta.php
67
  */
68
  public function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
69
- // only affect thumbnails on the frontend, do allow ajax calls.
70
- if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) || '_thumbnail_id' !== $meta_key ) {
71
  return $null;
72
  }
73
 
74
- // ignore attachments, non image attachments have icons, which get overridden otherwise.
75
- $post = get_post( $object_id );
76
- if ( ! empty( $post->post_type ) && 'attachment' === $post->post_type ) {
77
  return $null;
78
  }
79
 
80
- $meta_type = 'post';
81
- $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
82
-
83
- if ( ! $meta_cache ) {
84
- $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
85
- $meta_cache = $meta_cache[ $object_id ];
86
  }
87
 
88
- if ( ! $meta_key ) {
89
- return $meta_cache;
90
- }
91
 
92
- if ( isset( $meta_cache[ $meta_key ] ) ) {
93
- if ( $single ) {
94
- return maybe_unserialize( $meta_cache[ $meta_key ][0] );
 
 
 
 
 
 
95
  } else {
96
- return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
97
  }
98
  }
99
 
100
- if ( $single ) {
101
- // allow to set an other ID see the readme.txt for details.
102
- return apply_filters( 'dfi_thumbnail_id', get_option( 'dfi_image_id' ), $object_id ); // set the default featured img ID.
103
- } else {
104
- return array();
105
  }
 
 
 
 
 
 
 
 
 
106
  }
107
 
108
  /**
3
  * Plugin Name: Default featured image
4
  * Plugin URI: http://wordpress.org/extend/plugins/default-featured-image/
5
  * Description: Allows users to select a default featured image in the media settings
6
+ * Version: 1.6.4
7
  * Requires at least: 4.0
8
  * Requires PHP: 5.6
9
  * Author: Jan Willem Oostendorp
12
  * Text Domain: default-featured-image
13
  */
14
  class Default_Featured_Image {
15
+ const VERSION = '1.6.4';
16
 
17
  /**
18
  * Hook everything
52
  }
53
 
54
  /**
55
+ * Add the dfi_id to the meta data if needed.
 
56
  *
57
+ * @param null|mixed $null Should be null, we don't use it because we update the meta cache.
58
  * @param int $object_id ID of the object metadata is for.
59
  * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
60
  * the specified object.
62
  * specified meta_key. This parameter has no effect if meta_key is not specified.
63
  *
64
  * @return string|array Single metadata value, or array of values
 
65
  */
66
  public function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
67
+ // Only affect thumbnails on the frontend, do allow ajax calls.
68
+ if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) ) {
69
  return $null;
70
  }
71
 
72
+ // Check only empty meta_key and '_thumbnail_id'.
73
+ if ( ! empty( $meta_key ) && '_thumbnail_id' !== $meta_key ) {
 
74
  return $null;
75
  }
76
 
77
+ // Check if this post type supports featured images.
78
+ if ( ! post_type_supports( get_post_type( $object_id ), 'thumbnail' ) ) {
79
+ return $null; // post type does not support featured images.
 
 
 
80
  }
81
 
82
+ // Get current Cache.
83
+ $meta_cache = wp_cache_get( $object_id, 'post_meta' );
 
84
 
85
+ /**
86
+ * Empty objects probably need to be initiated.
87
+ *
88
+ * @see get_metadata() in /wp-includes/meta.php
89
+ */
90
+ if ( ! $meta_cache ) {
91
+ $meta_cache = update_meta_cache( 'post', array( $object_id ) );
92
+ if ( isset( $meta_cache[ $object_id ] ) ) {
93
+ $meta_cache = $meta_cache[ $object_id ];
94
  } else {
95
+ $meta_cache = array();
96
  }
97
  }
98
 
99
+ // Is the _thumbnail_id present in cache?
100
+ if ( ! empty( $meta_cache['_thumbnail_id'][0] ) ) {
101
+ return $null; // it is present, don't check anymore.
 
 
102
  }
103
+
104
+ // Get the Default Featured Image ID.
105
+ $dfi_id = get_option( 'dfi_image_id' );
106
+
107
+ // Set the dfi in cache.
108
+ $meta_cache['_thumbnail_id'][0] = apply_filters( 'dfi_thumbnail_id', $dfi_id, $object_id );
109
+ wp_cache_set( $object_id, $meta_cache, 'post_meta' );
110
+
111
+ return $null;
112
  }
113
 
114
  /**