Default featured image - Version 1.7.1

Version Description

  • Fixed weird SVN deployment bug.
Download this release

Release Info

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

Code changes from version 1.6.4 to 1.7.1

app/class-dfi-exceptions.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * This class holds exceptions for plugins that need exceptions for DFI.
5
+ *
6
+ * Class DFI_Exceptions
7
+ */
8
+ class DFI_Exceptions {
9
+
10
+ /**
11
+ * Exclude dfi from shortcode: wpuf_edit
12
+ *
13
+ * @param mixed $false unused, just pass along.
14
+ * @param string $tag The shortcode.
15
+ *
16
+ * @return mixed
17
+ */
18
+ public static function wp_user_frontend_pre( $false, $tag ) {
19
+ if ( 'wpuf_edit' === $tag ) {
20
+ add_filter( 'dfi_thumbnail_id', '__return_null' );
21
+ }
22
+
23
+ return $false;
24
+ }
25
+
26
+ /**
27
+ * Exclude dfi from shortcode: wpuf_edit
28
+ *
29
+ * @param mixed $output unused, just pass along.
30
+ * @param string $tag The shortcode.
31
+ *
32
+ * @return mixed
33
+ */
34
+ public static function wp_user_frontend_after( $output, $tag ) {
35
+ if ( 'wpuf_edit' === $tag ) {
36
+ remove_filter( 'dfi_thumbnail_id', '__return_null' );
37
+ }
38
+
39
+ return $output;
40
+ }
41
+
42
+ /**
43
+ * Exclude wp all import, the DFI during the import.
44
+ *
45
+ * @param int $dfi_id The DFI id.
46
+ *
47
+ * @return null|int
48
+ */
49
+ public static function wp_all_import_dfi_workaround( $dfi_id ) {
50
+ if ( function_exists( 'wp_all_import_get_import_id' ) && is_numeric( wp_all_import_get_import_id() ) ) {
51
+ return null; // If a post is imported with WP All Import, set DFI id to null.
52
+ } else {
53
+ return $dfi_id;
54
+ }
55
+ }
56
+ }
app/class-dfi.php ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Class DFI
5
+ */
6
+ final class DFI {
7
+
8
+ /**
9
+ * Holds the instance.
10
+ *
11
+ * @var self
12
+ */
13
+ protected static $inst = null;
14
+
15
+ /**
16
+ * Create instance of this class.
17
+ *
18
+ * @return self
19
+ */
20
+ public static function instance() {
21
+ if ( null === static::$inst ) {
22
+ static::$inst = new self();
23
+ }
24
+ return static::$inst;
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' );
41
+ }
42
+
43
+ /**
44
+ * L10n
45
+ */
46
+ public function load_plugin_textdomain() {
47
+ load_plugin_textdomain( 'default-featured-image', false, plugin_basename( DFI_DIR ) . '/languages/' );
48
+ }
49
+
50
+ /**
51
+ * Add the dfi_id to the meta data if needed.
52
+ *
53
+ * @param null|mixed $null Should be null, we don't use it because we update the meta cache.
54
+ * @param int $object_id ID of the object metadata is for.
55
+ * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
56
+ * the specified object.
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.
64
+ if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) ) {
65
+ return $null;
66
+ }
67
+
68
+ // Check only empty meta_key and '_thumbnail_id'.
69
+ if ( ! empty( $meta_key ) && '_thumbnail_id' !== $meta_key ) {
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
+
78
+ // Get current Cache.
79
+ $meta_cache = wp_cache_get( $object_id, 'post_meta' );
80
+
81
+ /**
82
+ * Empty objects probably need to be initiated.
83
+ *
84
+ * @see get_metadata() in /wp-includes/meta.php
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();
92
+ }
93
+ }
94
+
95
+ // Is the _thumbnail_id present in cache?
96
+ if ( ! empty( $meta_cache['_thumbnail_id'][0] ) ) {
97
+ return $null; // it is present, don't check anymore.
98
+ }
99
+
100
+ // Get the Default Featured Image ID.
101
+ $dfi_id = get_option( 'dfi_image_id' );
102
+
103
+ // Set the dfi in cache.
104
+ $meta_cache['_thumbnail_id'][0] = apply_filters( 'dfi_thumbnail_id', $dfi_id, $object_id );
105
+ wp_cache_set( $object_id, $meta_cache, 'post_meta' );
106
+
107
+ return $null;
108
+ }
109
+
110
+ /**
111
+ * Register the setting on the media settings page.
112
+ */
113
+ public function media_setting() {
114
+ register_setting(
115
+ 'media', // settings page.
116
+ 'dfi_image_id', // option name.
117
+ array( &$this, 'input_validation' ) // validation callback.
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.
125
+ );
126
+ }
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' );
133
+
134
+ $rm_btn_class = 'button button-disabled';
135
+ if ( ! empty( $value ) ) {
136
+ echo $this->preview_image( $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
137
+ $rm_btn_class = 'button';
138
+ }
139
+ ?>
140
+ <input id="dfi_id" type="hidden" value="<?php echo esc_attr( $value ); ?>" name="dfi_image_id"/>
141
+
142
+ <a id="dfi-set-dfi" class="button" title="<?php esc_attr_e( 'Select default featured image', 'default-featured-image' ); ?>" href="#">
143
+ <span style="margin-top: 3px;" class="dashicons dashicons-format-image"></span>
144
+ <?php esc_html_e( 'Select default featured image', 'default-featured-image' ); ?>
145
+ </a>
146
+ <div style="margin-top:5px;">
147
+ <a id="dfi-no-fdi" class="<?php echo esc_attr( $rm_btn_class ); ?>"
148
+ title="<?php esc_attr_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>" href="#">
149
+ <?php esc_html_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>
150
+ </a>
151
+ </div>
152
+ <?php
153
+ }
154
+
155
+ /**
156
+ * Is the given input a valid image.
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;
168
+ }
169
+
170
+ /**
171
+ * Register the javascript
172
+ */
173
+ public function admin_scripts() {
174
+ wp_enqueue_media(); // scripts used for uploader.
175
+ wp_enqueue_script( 'dfi-script', DFI_URL . 'set-default-featured-image.js', array(), DFI_VERSION, true );
176
+ wp_localize_script(
177
+ 'dfi-script',
178
+ 'dfi_L10n',
179
+ array(
180
+ 'manager_title' => __( 'Select default featured image', 'default-featured-image' ),
181
+ 'manager_button' => __( 'Set default featured image', 'default-featured-image' ),
182
+ )
183
+ );
184
+ }
185
+
186
+ /**
187
+ * Get an image and wrap it in a div
188
+ *
189
+ * @param int $image_id A valid attachment image ID.
190
+ *
191
+ * @return string
192
+ */
193
+ public function preview_image( $image_id ) {
194
+ $output = '<div id="preview-image" style="float:left; padding: 0 5px 0 0;">';
195
+ $output .= wp_get_attachment_image( $image_id, array( 80, 60 ), true );
196
+ $output .= '</div>';
197
+
198
+ return $output;
199
+ }
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
206
+ // This is only a preview, don't bother verifying.
207
+ if ( ! empty( $_POST['image_id'] ) && absint( $_POST['image_id'] ) ) {
208
+ $img_id = absint( $_POST['image_id'] );
209
+ echo $this->preview_image( $img_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
210
+ }
211
+ die(); // ajax call..
212
+ }
213
+
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' );
223
+ $settings_link = '<a href="' . $href . '">' . __( 'Settings' ) . '</a>'; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
224
+ array_unshift( $links, $settings_link );
225
+
226
+ return $links;
227
+ }
228
+
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
+ */
240
+ public function show_dfi( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
241
+ $default_thumbnail_id = get_option( 'dfi_image_id' ); // select the default thumb.
242
+
243
+ // if an image is set return that image.
244
+ if ( (int) $default_thumbnail_id !== (int) $post_thumbnail_id ) {
245
+ return $html;
246
+ }
247
+
248
+ if ( isset( $attr['class'] ) ) {
249
+ $attr['class'] .= ' default-featured-img';
250
+ } else {
251
+ $size_class = $size;
252
+ if ( is_array( $size_class ) ) {
253
+ $size_class = 'size-' . implode( 'x', $size_class );
254
+ }
255
+ // attachment-$size is a default class `wp_get_attachment_image` would otherwise add. It won't add it if there are classes already there.
256
+ $attr = array( 'class' => "attachment-{$size_class} default-featured-img" );
257
+ }
258
+
259
+ $html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
260
+ $html = apply_filters( 'dfi_thumbnail_html', $html, $post_id, $default_thumbnail_id, $size, $attr );
261
+
262
+ return $html;
263
+ }
264
+ }
phpcs.xml.dist CHANGED
@@ -60,9 +60,7 @@
60
  -->
61
 
62
  <rule ref="WordPress">
63
- <exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
64
  <exclude name="Squiz.Commenting.FileComment.Missing"/>
65
- <exclude name="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound"/>
66
  </rule>
67
 
68
  <config name="testVersion" value="5.6-"/>
@@ -92,6 +90,7 @@
92
  <!-- Provide the prefixes to look for. -->
93
  <property name="prefixes" type="array">
94
  <element value="dfi"/>
 
95
  </property>
96
  </properties>
97
  </rule>
60
  -->
61
 
62
  <rule ref="WordPress">
 
63
  <exclude name="Squiz.Commenting.FileComment.Missing"/>
 
64
  </rule>
65
 
66
  <config name="testVersion" value="5.6-"/>
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>
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.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
 
@@ -63,7 +63,7 @@ To do that just don't nest the `if`
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
 
@@ -101,6 +101,14 @@ yes you can with the filter `dfi_thumbnail_html`.
101
  2. The media manager will start with the current selected image
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.
2
  Contributors: janwoostendorp
3
  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
 
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
 
101
  2. The media manager will start with the current selected image
102
 
103
  == Changelog ==
104
+ = 1.7.1 =
105
+ * Fixed weird SVN deployment bug.
106
+
107
+ = 1.7.0 =
108
+ * moved main class to it's own file.
109
+ * Added second class that can hold exceptions with other plugins
110
+ * The first exception is for WP User Frontend
111
+ * The second exception is for WP All Import.
112
 
113
  = 1.6.4 =
114
  * `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.
set-default-featured-image.php CHANGED
@@ -3,267 +3,56 @@
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
10
  * Author URI: https://janw.me/
11
  * License: GPLv2 or later
12
  * Text Domain: default-featured-image
 
 
13
  */
14
- class Default_Featured_Image {
15
- const VERSION = '1.6.4';
16
 
17
- /**
18
- * Hook everything
19
- */
20
- public function __construct() {
21
- // add the settings field to the media page.
22
- add_action( 'admin_init', array( &$this, 'media_setting' ) );
23
- // enqueue the js.
24
- add_action( 'admin_print_scripts-options-media.php', array( &$this, 'admin_scripts' ) );
25
- // get the preview image ajax call.
26
- add_action( 'wp_ajax_dfi_change_preview', array( &$this, 'ajax_wrapper' ) );
27
- // set dfi meta key on every occasion.
28
- add_filter( 'get_post_metadata', array( &$this, 'set_dfi_meta_key' ), 10, 4 );
29
- // display a default featured image.
30
- add_filter( 'post_thumbnail_html', array( &$this, 'show_dfi' ), 20, 5 );
31
- // add a link on the plugin page to the setting.
32
- add_filter( 'plugin_action_links_default-featured-image/set-default-featured-image.php', array( &$this, 'add_settings_link' ), 10, 1 );
33
- // add L10n.
34
- add_action( 'init', array( &$this, 'load_plugin_textdomain' ) );
35
- // remove setting on removal.
36
- register_uninstall_hook( __FILE__, array( 'default_featured_image', 'uninstall' ) );
 
 
 
 
 
 
 
37
 
38
- }
39
-
40
- /**
41
- * Uninstall
42
- */
43
- public static function uninstall() {
44
- delete_option( 'dfi_image_id' );
45
- }
46
-
47
- /**
48
- * L10n
49
- */
50
- public function load_plugin_textdomain() {
51
- load_plugin_textdomain( 'default-featured-image', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
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.
61
- * @param bool $single Optional, default is false. If true, return only the first value of the
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
- /**
115
- * Register the setting on the media settings page.
116
- */
117
- public function media_setting() {
118
- register_setting(
119
- 'media', // settings page.
120
- 'dfi_image_id', // option name.
121
- array( &$this, 'input_validation' ) // validation callback.
122
- );
123
- add_settings_field(
124
- 'dfi', // id.
125
- __( 'Default featured image', 'default-featured-image' ), // setting title.
126
- array( &$this, 'settings_html' ), // display callback.
127
- 'media', // settings page.
128
- 'default' // settings section.
129
- );
130
- }
131
-
132
- /**
133
- * Display the buttons and a preview on the media settings page.
134
- */
135
- public function settings_html() {
136
- $value = get_option( 'dfi_image_id' );
137
-
138
- $rm_btn_class = 'button button-disabled';
139
- if ( ! empty( $value ) ) {
140
- echo $this->preview_image( $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
141
- $rm_btn_class = 'button';
142
- }
143
- ?>
144
- <input id="dfi_id" type="hidden" value="<?php echo esc_attr( $value ); ?>" name="dfi_image_id"/>
145
-
146
- <a id="dfi-set-dfi" class="button" title="<?php esc_attr_e( 'Select default featured image', 'default-featured-image' ); ?>" href="#">
147
- <span style="margin-top: 3px;" class="dashicons dashicons-format-image"></span>
148
- <?php esc_html_e( 'Select default featured image', 'default-featured-image' ); ?>
149
- </a>
150
- <div style="margin-top:5px;">
151
- <a id="dfi-no-fdi" class="<?php echo esc_attr( $rm_btn_class ); ?>"
152
- title="<?php esc_attr_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>" href="#">
153
- <?php esc_html_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>
154
- </a>
155
- </div>
156
- <?php
157
- }
158
-
159
- /**
160
- * Is the given input a valid image.
161
- *
162
- * @param string|int $thumbnail_id The saving thumbnail.
163
- *
164
- * @return string|bool
165
- */
166
- public function input_validation( $thumbnail_id ) {
167
- if ( wp_attachment_is_image( $thumbnail_id ) ) {
168
- return $thumbnail_id;
169
- }
170
-
171
- return false;
172
- }
173
-
174
- /**
175
- * Register the javascript
176
- */
177
- public function admin_scripts() {
178
- wp_enqueue_media(); // scripts used for uploader.
179
- wp_enqueue_script( 'dfi-script', plugin_dir_url( __FILE__ ) . 'set-default-featured-image.js', array(), self::VERSION, true );
180
- wp_localize_script(
181
- 'dfi-script',
182
- 'dfi_L10n',
183
- array(
184
- 'manager_title' => __( 'Select default featured image', 'default-featured-image' ),
185
- 'manager_button' => __( 'Set default featured image', 'default-featured-image' ),
186
- )
187
- );
188
- }
189
-
190
- /**
191
- * Get an image and wrap it in a div
192
- *
193
- * @param int $image_id A valid attachment image ID.
194
- *
195
- * @return string
196
- */
197
- public function preview_image( $image_id ) {
198
- $output = '<div id="preview-image" style="float:left; padding: 0 5px 0 0;">';
199
- $output .= wp_get_attachment_image( $image_id, array( 80, 60 ), true );
200
- $output .= '</div>';
201
-
202
- return $output;
203
- }
204
-
205
- /**
206
- * The callback for the ajax call when the DFI changes
207
- */
208
- public function ajax_wrapper() {
209
- if ( ! empty( $_POST['image_id'] ) && absint( $_POST['image_id'] ) ) {
210
- $img_id = absint( $_POST['image_id'] );
211
- echo $this->preview_image( $img_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
212
- }
213
- die(); // ajax call..
214
- }
215
-
216
- /**
217
- * Add a settings link to the the plugin on the plugin page
218
- *
219
- * @param array $links An array of plugin action links.
220
- *
221
- * @return array
222
- */
223
- public function add_settings_link( $links ) {
224
- $href = admin_url( 'options-media.php#dfi-set-dfi' );
225
- $settings_link = '<a href="' . $href . '">' . __( 'Settings' ) . '</a>'; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
226
- array_unshift( $links, $settings_link );
227
-
228
- return $links;
229
- }
230
-
231
- /**
232
- * Set a default featured image if it is missing
233
- *
234
- * @param string $html The post thumbnail HTML.
235
- * @param int $post_id The post ID.
236
- * @param int $post_thumbnail_id The post thumbnail ID.
237
- * @param string $size The post thumbnail size. Image size or array of width and height.
238
- * @param array $attr values (in that order). Default 'post-thumbnail'.
239
- *
240
- * @return string
241
- */
242
- public function show_dfi( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
243
- $default_thumbnail_id = get_option( 'dfi_image_id' ); // select the default thumb.
244
-
245
- // if an image is set return that image.
246
- if ( (int) $default_thumbnail_id !== (int) $post_thumbnail_id ) {
247
- return $html;
248
- }
249
-
250
- if ( isset( $attr['class'] ) ) {
251
- $attr['class'] .= ' default-featured-img';
252
- } else {
253
- $size_class = $size;
254
- if ( is_array( $size_class ) ) {
255
- $size_class = 'size-' . implode( 'x', $size_class );
256
- }
257
- // attachment-$size is a default class `wp_get_attachment_image` would otherwise add. It won't add it if there are classes already there.
258
- $attr = array( 'class' => "attachment-{$size_class} default-featured-img" );
259
- }
260
-
261
- $html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
262
- $html = apply_filters( 'dfi_thumbnail_html', $html, $post_id, $default_thumbnail_id, $size, $attr );
263
-
264
- return $html;
265
- }
266
- }
267
 
268
- // run the plugin class.
269
- new Default_Featured_Image();
 
 
 
 
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
10
  * Author URI: https://janw.me/
11
  * License: GPLv2 or later
12
  * Text Domain: default-featured-image
13
+ *
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__ ) );
21
+ define( 'DFI_NAME', basename( __DIR__ ) . DIRECTORY_SEPARATOR . basename( __FILE__ ) );
22
+
23
+ require_once DFI_APP_DIR . 'class-dfi.php';
24
+ require_once DFI_APP_DIR . 'class-dfi-exceptions.php';
25
+
26
+ $dfi = DFI::instance();
27
+
28
+ // add the settings field to the media page.
29
+ add_action( 'admin_init', array( $dfi, 'media_setting' ) );
30
+ // enqueue the js.
31
+ add_action( 'admin_print_scripts-options-media.php', array( $dfi, 'admin_scripts' ) );
32
+ // get the preview image ajax call.
33
+ add_action( 'wp_ajax_dfi_change_preview', array( $dfi, 'ajax_wrapper' ) );
34
+ // set dfi meta key on every occasion.
35
+ add_filter( 'get_post_metadata', array( $dfi, 'set_dfi_meta_key' ), 10, 4 );
36
+ // display a default featured image.
37
+ add_filter( 'post_thumbnail_html', array( $dfi, 'show_dfi' ), 20, 5 );
38
+ // add a link on the plugin page to the setting.
39
+ add_filter( 'plugin_action_links_default-featured-image/set-default-featured-image.php', array( $dfi, 'add_settings_link' ), 10, 1 );
40
+ // add L10n.
41
+ add_action( 'init', array( $dfi, 'load_plugin_textdomain' ) );
42
+ // remove setting on removal.
43
+ register_uninstall_hook( __FILE__, array( 'DFI', 'uninstall' ) );
44
 
45
+ /**
46
+ * Exception: https://wordpress.org/plugins/wp-user-frontend/
47
+ *
48
+ * @see https://wordpress.org/support/topic/couldnt-able-to-edit-default-featured-image-from-post/
49
+ */
50
+ add_filter( 'pre_do_shortcode_tag', array( 'DFI_Exceptions', 'wp_user_frontend_pre' ), 9, 2 );
51
+ add_filter( 'do_shortcode_tag', array( 'DFI_Exceptions', 'wp_user_frontend_after' ), 9, 2 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ /**
54
+ * Exception: https://www.wpallimport.com/
55
+ *
56
+ * @see https://wordpress.org/support/topic/importing-images-into-woocommerce-using-cron/
57
+ */
58
+ add_filter( 'dfi_thumbnail_id', array( 'DFI_Exceptions', 'wp_all_import_dfi_workaround' ), 9, 2 );