Default featured image - Version 1.7.2

Version Description

Download this release

Release Info

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

Code changes from version 1.7.1 to 1.7.2

.wordpress-org/banner-1544x500.jpg ADDED
Binary file
.wordpress-org/banner-772x250.jpg ADDED
Binary file
.wordpress-org/icon-128x128.jpg ADDED
Binary file
.wordpress-org/icon-256x256.jpg ADDED
Binary file
.wordpress-org/screenshot-1.png ADDED
Binary file
.wordpress-org/screenshot-2.png ADDED
Binary file
app/class-dfi.php CHANGED
@@ -25,16 +25,15 @@ final class DFI {
25
  }
26
 
27
  /**
28
- * The consturctor.
29
- *
30
- * @return self
31
  */
32
  private function __construct() {
33
- return $this;
34
  }
35
 
36
  /**
37
  * Uninstall
 
 
38
  */
39
  public static function uninstall() {
40
  delete_option( 'dfi_image_id' );
@@ -42,6 +41,8 @@ final class DFI {
42
 
43
  /**
44
  * L10n
 
 
45
  */
46
  public function load_plugin_textdomain() {
47
  load_plugin_textdomain( 'default-featured-image', false, plugin_basename( DFI_DIR ) . '/languages/' );
@@ -57,7 +58,7 @@ final class DFI {
57
  * @param bool $single Optional, default is false. If true, return only the first value of the
58
  * specified meta_key. This parameter has no effect if meta_key is not specified.
59
  *
60
- * @return string|array Single metadata value, or array of values
61
  */
62
  public function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
63
  // Only affect thumbnails on the frontend, do allow ajax calls.
@@ -70,8 +71,9 @@ final class DFI {
70
  return $null;
71
  }
72
 
 
73
  // Check if this post type supports featured images.
74
- if ( ! post_type_supports( get_post_type( $object_id ), 'thumbnail' ) ) {
75
  return $null; // post type does not support featured images.
76
  }
77
 
@@ -85,7 +87,7 @@ final class DFI {
85
  */
86
  if ( ! $meta_cache ) {
87
  $meta_cache = update_meta_cache( 'post', array( $object_id ) );
88
- if ( isset( $meta_cache[ $object_id ] ) ) {
89
  $meta_cache = $meta_cache[ $object_id ];
90
  } else {
91
  $meta_cache = array();
@@ -109,6 +111,8 @@ final class DFI {
109
 
110
  /**
111
  * Register the setting on the media settings page.
 
 
112
  */
113
  public function media_setting() {
114
  register_setting(
@@ -118,7 +122,7 @@ final class DFI {
118
  );
119
  add_settings_field(
120
  'dfi', // id.
121
- __( 'Default featured image', 'default-featured-image' ), // setting title.
122
  array( &$this, 'settings_html' ), // display callback.
123
  'media', // settings page.
124
  'default' // settings section.
@@ -127,6 +131,8 @@ final class DFI {
127
 
128
  /**
129
  * Display the buttons and a preview on the media settings page.
 
 
130
  */
131
  public function settings_html() {
132
  $value = get_option( 'dfi_image_id' );
@@ -157,11 +163,11 @@ final class DFI {
157
  *
158
  * @param string|int $thumbnail_id The saving thumbnail.
159
  *
160
- * @return string|bool
161
  */
162
  public function input_validation( $thumbnail_id ) {
163
- if ( wp_attachment_is_image( $thumbnail_id ) ) {
164
- return $thumbnail_id;
165
  }
166
 
167
  return false;
@@ -169,6 +175,8 @@ final class DFI {
169
 
170
  /**
171
  * Register the javascript
 
 
172
  */
173
  public function admin_scripts() {
174
  wp_enqueue_media(); // scripts used for uploader.
@@ -200,6 +208,8 @@ final class DFI {
200
 
201
  /**
202
  * The callback for the ajax call when the DFI changes
 
 
203
  */
204
  public function ajax_wrapper() {
205
  //phpcs:disable WordPress.Security.NonceVerification.Missing
@@ -214,9 +224,9 @@ final class DFI {
214
  /**
215
  * Add a settings link to the the plugin on the plugin page
216
  *
217
- * @param array $links An array of plugin action links.
218
  *
219
- * @return array
220
  */
221
  public function add_settings_link( $links ) {
222
  $href = admin_url( 'options-media.php#dfi-set-dfi' );
@@ -229,11 +239,11 @@ final class DFI {
229
  /**
230
  * Set a default featured image if it is missing
231
  *
232
- * @param string $html The post thumbnail HTML.
233
- * @param int $post_id The post ID.
234
- * @param int $post_thumbnail_id The post thumbnail ID.
235
- * @param string $size The post thumbnail size. Image size or array of width and height.
236
- * @param array $attr values (in that order). Default 'post-thumbnail'.
237
  *
238
  * @return string
239
  */
25
  }
26
 
27
  /**
28
+ * The constructor
 
 
29
  */
30
  private function __construct() {
 
31
  }
32
 
33
  /**
34
  * Uninstall
35
+ *
36
+ * @return void
37
  */
38
  public static function uninstall() {
39
  delete_option( 'dfi_image_id' );
41
 
42
  /**
43
  * L10n
44
+ *
45
+ * @return void
46
  */
47
  public function load_plugin_textdomain() {
48
  load_plugin_textdomain( 'default-featured-image', false, plugin_basename( DFI_DIR ) . '/languages/' );
58
  * @param bool $single Optional, default is false. If true, return only the first value of the
59
  * specified meta_key. This parameter has no effect if meta_key is not specified.
60
  *
61
+ * @return string|string[] Single metadata value, or array of values
62
  */
63
  public function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
64
  // Only affect thumbnails on the frontend, do allow ajax calls.
71
  return $null;
72
  }
73
 
74
+ $post_type = get_post_type( $object_id );
75
  // Check if this post type supports featured images.
76
+ if ( false !== $post_type && ! post_type_supports( $post_type, 'thumbnail' ) ) {
77
  return $null; // post type does not support featured images.
78
  }
79
 
87
  */
88
  if ( ! $meta_cache ) {
89
  $meta_cache = update_meta_cache( 'post', array( $object_id ) );
90
+ if ( ! empty( $meta_cache[ $object_id ] ) ) {
91
  $meta_cache = $meta_cache[ $object_id ];
92
  } else {
93
  $meta_cache = array();
111
 
112
  /**
113
  * Register the setting on the media settings page.
114
+ *
115
+ * @return void
116
  */
117
  public function media_setting() {
118
  register_setting(
122
  );
123
  add_settings_field(
124
  'dfi', // id.
125
+ _x( 'Default featured image', 'Label on the settings page.', 'default-featured-image' ), // setting title.
126
  array( &$this, 'settings_html' ), // display callback.
127
  'media', // settings page.
128
  'default' // settings section.
131
 
132
  /**
133
  * Display the buttons and a preview on the media settings page.
134
+ *
135
+ * @return void
136
  */
137
  public function settings_html() {
138
  $value = get_option( 'dfi_image_id' );
163
  *
164
  * @param string|int $thumbnail_id The saving thumbnail.
165
  *
166
+ * @return int|false
167
  */
168
  public function input_validation( $thumbnail_id ) {
169
+ if ( wp_attachment_is_image( absint( $thumbnail_id ) ) ) {
170
+ return absint( $thumbnail_id );
171
  }
172
 
173
  return false;
175
 
176
  /**
177
  * Register the javascript
178
+ *
179
+ * @return void
180
  */
181
  public function admin_scripts() {
182
  wp_enqueue_media(); // scripts used for uploader.
208
 
209
  /**
210
  * The callback for the ajax call when the DFI changes
211
+ *
212
+ * @return void It's an ajax call.
213
  */
214
  public function ajax_wrapper() {
215
  //phpcs:disable WordPress.Security.NonceVerification.Missing
224
  /**
225
  * Add a settings link to the the plugin on the plugin page
226
  *
227
+ * @param string[] $links An array of plugin action links.
228
  *
229
+ * @return string[]
230
  */
231
  public function add_settings_link( $links ) {
232
  $href = admin_url( 'options-media.php#dfi-set-dfi' );
239
  /**
240
  * Set a default featured image if it is missing
241
  *
242
+ * @param string $html The post thumbnail HTML.
243
+ * @param int $post_id The post ID.
244
+ * @param int $post_thumbnail_id The post thumbnail ID.
245
+ * @param string|int[] $size The post thumbnail size. Image size or array of width and height.
246
+ * @param string|mixed[] $attr values (in that order). Default 'post-thumbnail'.
247
  *
248
  * @return string
249
  */
phpcs.xml.dist DELETED
@@ -1,98 +0,0 @@
1
- <?xml version="1.0"?>
2
- <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
- name="DFI"
4
- xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
5
-
6
- <description>DFI phpCS</description>
7
-
8
- <!--
9
- This is a sample ruleset which can be used as a starting point for a PHPCS ruleset for a WordPress project.
10
- Whether you are reviewing other people's code or working on your own code, it is useful to
11
- have such a ruleset in place to:
12
- - Document the settings used both for your future self as well as for other contributors to the project.
13
- - Ensure that everyone uses the same settings when reviewing the code.
14
- - Make life easier as you no longer will have to type in a long range of command line parameters.
15
- Before you use this ruleset, make sure to customize the following:
16
- - The ruleset name and description above.
17
- - The supported PHP versions as set in the value for `testVersion`.
18
- For information on how to set the value for `testVersion`, please see:
19
- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions
20
- - The minimum supported WP version `minimum_supported_wp_version`.
21
- For more information about this setting, see:
22
- https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#minimum-wp-version-to-check-for-usage-of-deprecated-functions-classes-and-function-parameters
23
- - The `text-domain` used by the project.
24
- For more information about this setting, see:
25
- https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#internationalization-setting-your-text-domain
26
- - The `prefixes` used by the project.
27
- For more information about this setting, see:
28
- https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace
29
- -->
30
-
31
- <!--
32
- #############################################################################
33
- COMMAND LINE ARGUMENTS
34
- https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
35
- #############################################################################
36
- -->
37
-
38
- <file>.</file>
39
-
40
- <exclude-pattern>node_modules/*</exclude-pattern>
41
- <exclude-pattern>vendor/*</exclude-pattern>
42
-
43
- <!-- Only check PHP files. -->
44
- <arg name="extensions" value="php"/>
45
-
46
- <!-- Show progress, show the error codes for each message (source). -->
47
- <arg value="ps"/>
48
-
49
- <!-- Strip the filepaths down to the relevant bit. -->
50
- <arg name="basepath" value="./"/>
51
-
52
- <!-- Check up to 8 files simultaneously. -->
53
- <arg name="parallel" value="8"/>
54
-
55
-
56
- <!--
57
- #############################################################################
58
- SET THE RULES
59
- #############################################################################
60
- -->
61
-
62
- <rule ref="WordPress">
63
- <exclude name="Squiz.Commenting.FileComment.Missing"/>
64
- </rule>
65
-
66
- <config name="testVersion" value="5.6-"/>
67
- <rule ref="PHPCompatibilityWP"/>
68
-
69
- <!--
70
- #############################################################################
71
- SNIFF SPECIFIC CONFIGURATION
72
- #############################################################################
73
- -->
74
-
75
- <!-- Set the minimum supported WP version. This is used by several sniffs. -->
76
- <config name="minimum_supported_wp_version" value="5.2"/>
77
-
78
- <!-- Verify that all gettext calls use the correct text domain. -->
79
- <rule ref="WordPress.WP.I18n">
80
- <properties>
81
- <property name="text_domain" type="array">
82
- <element value="default-featured-image"/>
83
- </property>
84
- </properties>
85
- </rule>
86
-
87
- <!-- Verify that everything in the global namespace is prefixed with a plugin specific prefix. -->
88
- <rule ref="WordPress.NamingConventions.PrefixAllGlobals">
89
- <properties>
90
- <!-- Provide the prefixes to look for. -->
91
- <property name="prefixes" type="array">
92
- <element value="dfi"/>
93
- <element value="default_featured_image"/>
94
- </property>
95
- </properties>
96
- </rule>
97
-
98
- </ruleset>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -4,24 +4,18 @@ Tags: media, image
4
  Requires at least: 3.5
5
  Tested up to: 5.9.1
6
  Requires PHP: 5.6
7
- Stable tag: 1.7.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
- Add a default featured image to the media settings page
12
 
13
  == Description ==
14
 
15
  Add a default featured image to the media settings page. This featured image will show up if no featured image is set. Simple as that.
16
 
17
- For exceptions and to see which functions to use see the [FAQ](http://wordpress.org/extend/plugins/default-featured-image/faq/).
18
-
19
- = Suggestions are welcome =
20
- * Found a bug?
21
- * Want to help to translate it in your language?
22
- * Something to be improved?
23
-
24
- [Contact me](http://wordpress.org/support/plugin/default-featured-image)
25
 
26
  == Installation ==
27
 
@@ -33,67 +27,80 @@ For exceptions and to see which functions to use see the [FAQ](http://wordpress.
33
 
34
  = My chosen featured image doesn't show, why isn't it working? =
35
 
36
- This plugin can't guarantee that it works. That depends on the themes. Still I want to know if it fails, so [contact me](http://wordpress.org/support/plugin/default-featured-image)
37
 
38
- = Which functions can I use to display the featured image? =
39
- The plugin uses the default WordPress functions `the_post_thumbnail` or `get_the_post_thumbnail`. `has_post_thumbnail` will always return true. `get_post_thumbnail_id` will return the ID set on the post or the DFI you set.
 
 
40
 
41
- = Can I exclude a page or give it a different image? =
42
- Yes you can by using the builtin `dfi_thumbnail_id` filter. It will give you the post id which you can use to check against.
43
 
44
- **Don't use a featured image on page 23**
 
45
 
46
- function dfi_skip_page ( $dfi_id, $post_id ) {
47
- if ( $post_id == 23 ) {
48
- return 0; // invalid id
49
- }
50
- return $dfi_id; // the original featured image id
51
- }
52
- add_filter( 'dfi_thumbnail_id', 'dfi_skip_page', 10 , 2 );
53
 
54
- **Use a different image for some categories**
 
55
 
56
- The example below only works if the post has 'animals' as a category. Assigning just 'cats' won't work
57
- To do that just don't nest the `if`
 
 
 
 
 
 
 
58
 
59
- function dfi_category ( $dfi_id, $post_id ) {
60
- // all which have 'animals' as a category
61
- if ( has_category( 'animals', $post_id ) ) {
62
 
63
- //sub category
 
 
 
64
  if ( has_category( 'cats', $post_id ) ) {
65
- return 7; // cats img
66
- } else if ( has_category( 'dogs', $post_id ) ) {
67
- return 8; // dogs img
 
 
68
  }
69
 
70
- return 6; // default animals picture
71
- }
72
- return $dfi_id; // the original featured image id
73
  }
74
- add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 );
75
 
76
- **Different image for the post_type 'wiki'**
77
 
78
- function dfi_posttype_book ( $dfi_id, $post_id ) {
79
- $post = get_post($post_id);
80
- if ( 'wiki' === $post->post_type ) {
81
- return 31; // the image id
82
- }
83
- return $dfi_id; // the original featured image id
 
 
 
 
 
 
84
  }
85
- add_filter( 'dfi_thumbnail_id', 'dfi_posttype_book', 10, 2 );
86
 
87
- = Can I change the HTML of the image returned? =
88
- yes you can with the filter `dfi_thumbnail_html`.
89
 
90
- function dfi_add_class($html, $post_id, $default_thumbnail_id, $size, $attr) {
91
- // add a class to the existing class list
92
- $attr['class'] .= ' my-class';
 
 
93
 
94
- return wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
95
- }
96
- add_filter( 'dfi_thumbnail_html', 'dfi_add_class', 10, 5 );
97
 
98
  == Screenshots ==
99
 
4
  Requires at least: 3.5
5
  Tested up to: 5.9.1
6
  Requires PHP: 5.6
7
+ Stable tag: 1.7.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
+ Add a Default Featured Image for all posts & pages.
12
 
13
  == Description ==
14
 
15
  Add a default featured image to the media settings page. This featured image will show up if no featured image is set. Simple as that.
16
 
17
+ Take a look at [FAQ](http://wordpress.org/extend/plugins/default-featured-image/faq/) for the basic questions.
18
+ Feel free to contact me [on the forum](https://wordpress.org/support/plugin/default-featured-image/) or on the [github repository](https://github.com/janw-me/default-featured-image).
 
 
 
 
 
 
19
 
20
  == Installation ==
21
 
27
 
28
  = My chosen featured image doesn't show, why isn't it working? =
29
 
30
+ This plugin works out of the box for most cases, but not always. If it doesn't work you can try the following things.
31
 
32
+ - Switch themes. Most of the time the theme does something weird.
33
+ - Are you using the Core WordPress functions to get the image? (see the FAQ below this one).
34
+ - Do normal feature images work?
35
+ - Might it be hidden via css? DFI images have an extra `default-featured-img` class added to them.
36
 
37
+ Still having problems? I want to know if it fails, so [contact me](http://wordpress.org/support/plugin/default-featured-image)
 
38
 
39
+ = Which functions can I use to display the featured image? =
40
+ There are no new functions, all core WordPress functions can be used.
41
 
42
+ - [the_post_thumbnail](https://developer.wordpress.org/reference/functions/the_post_thumbnail/) / [get_the_post_thumbnail](https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/) Display the image.
43
+ - [the_post_thumbnail_url](https://developer.wordpress.org/reference/functions/the_post_thumbnail_url/) / [get_the_post_thumbnail_url](https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/) Get the url.
44
+ - [has_post_thumbnail](https://developer.wordpress.org/reference/functions/has_post_thumbnail/) If a DFI is set it will always return true.
45
+ - [get_post_thumbnail_id](https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/) will return the ID set on the post or the DFI.
 
 
 
46
 
47
+ = Can I set a different image for a custom post type?
48
+ Yes, the following code will set a different image.
49
 
50
+ add_filter( 'dfi_thumbnail_id', 'dfi_posttype_book', 10, 2 );
51
+ function dfi_posttype_book( $dfi_id, $post_id ) {
52
+ $post = get_post( $post_id );
53
+ if ( 'book' === $post->post_type ) {
54
+ return 31; // the image id for the book post type.
55
+ }
56
+
57
+ return $dfi_id; // the original featured image id.
58
+ }
59
 
60
+ = Can I set different images per category? =
61
+ Yes, the following snippet will set different images based on the category.
 
62
 
63
+ add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 );
64
+ function dfi_category( $dfi_id, $post_id ) {
65
+ // Set a different image for posts that have the 'cats' category set.
66
+ // This will trigger first, if multiple categories have been set.
67
  if ( has_category( 'cats', $post_id ) ) {
68
+ return 7; // cats img id.
69
+ }
70
+ // Set a different image for posts that have the 'cats' category set.
71
+ if ( has_category( 'dogs', $post_id ) ) {
72
+ return 8; // dogs img id.
73
  }
74
 
75
+ return $dfi_id; // the original featured image id.
 
 
76
  }
 
77
 
 
78
 
79
+ = Can I change the HTML of the default featured image? =
80
+ When a Default Featured Image is used it will already add an extra class `default-featured-img`.
81
+ This can be used for styling.
82
+
83
+ If you need more you can change the whole HTML with the filter `dfi_thumbnail_html`.
84
+
85
+ add_filter( 'dfi_thumbnail_html', 'dfi_add_class', 10, 5 );
86
+ function dfi_add_class( $html, $post_id, $default_thumbnail_id, $size, $attr ) {
87
+ // Add a class to the existing class list.
88
+ $attr['class'] .= ' my-class';
89
+
90
+ return wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
91
  }
 
92
 
93
+ = Can I exclude one page from having a Default Featured Image? =
94
+ The following code will exclude the post/page with ID 23.
95
 
96
+ add_filter( 'dfi_thumbnail_id', 'dfi_skip_page', 10, 2 );
97
+ function dfi_skip_page( $dfi_id, $post_id ) {
98
+ if ( $post_id == 23 ) {
99
+ return 0; // invalid id.
100
+ }
101
 
102
+ return $dfi_id; // the original featured image id.
103
+ }
 
104
 
105
  == Screenshots ==
106
 
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.7.1
7
  * Requires at least: 4.0
8
  * Requires PHP: 5.6
9
  * Author: Jan Willem Oostendorp
@@ -14,7 +14,7 @@
14
  * @package DFI
15
  */
16
 
17
- define( 'DFI_VERSION', '1.7.1' );
18
  define( 'DFI_DIR', plugin_dir_path( __FILE__ ) );
19
  define( 'DFI_APP_DIR', DFI_DIR . 'app' . DIRECTORY_SEPARATOR );
20
  define( 'DFI_URL', plugin_dir_url( __FILE__ ) );
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.7.2
7
  * Requires at least: 4.0
8
  * Requires PHP: 5.6
9
  * Author: Jan Willem Oostendorp
14
  * @package DFI
15
  */
16
 
17
+ define( 'DFI_VERSION', '1.7.2' );
18
  define( 'DFI_DIR', plugin_dir_path( __FILE__ ) );
19
  define( 'DFI_APP_DIR', DFI_DIR . 'app' . DIRECTORY_SEPARATOR );
20
  define( 'DFI_URL', plugin_dir_url( __FILE__ ) );