Version Description
Download this release
Release Info
Developer | janw.oostendorp |
Plugin | Default featured image |
Version | 1.6.2 |
Comparing to | |
See all releases |
Code changes from version 1.6.1 to 1.6.2
- phpcs.xml.dist +99 -0
- readme.txt +3 -2
- set-default-featured-image.php +143 -112
- trunk/languages/default-featured-image-nl_NL.mo +0 -0
- trunk/languages/default-featured-image-nl_NL.po +39 -0
- trunk/phpcs.xml.dist +99 -0
- trunk/readme.txt +140 -0
- trunk/set-default-featured-image.js +106 -0
- trunk/set-default-featured-image.php +260 -0
phpcs.xml.dist
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="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-"/>
|
69 |
+
<rule ref="PHPCompatibilityWP"/>
|
70 |
+
|
71 |
+
<!--
|
72 |
+
#############################################################################
|
73 |
+
SNIFF SPECIFIC CONFIGURATION
|
74 |
+
#############################################################################
|
75 |
+
-->
|
76 |
+
|
77 |
+
<!-- Set the minimum supported WP version. This is used by several sniffs. -->
|
78 |
+
<config name="minimum_supported_wp_version" value="5.2"/>
|
79 |
+
|
80 |
+
<!-- Verify that all gettext calls use the correct text domain. -->
|
81 |
+
<rule ref="WordPress.WP.I18n">
|
82 |
+
<properties>
|
83 |
+
<property name="text_domain" type="array">
|
84 |
+
<element value="default-featured-image"/>
|
85 |
+
</property>
|
86 |
+
</properties>
|
87 |
+
</rule>
|
88 |
+
|
89 |
+
<!-- Verify that everything in the global namespace is prefixed with a plugin specific prefix. -->
|
90 |
+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
|
91 |
+
<properties>
|
92 |
+
<!-- Provide the prefixes to look for. -->
|
93 |
+
<property name="prefixes" type="array">
|
94 |
+
<element value="dfi"/>
|
95 |
+
</property>
|
96 |
+
</properties>
|
97 |
+
</rule>
|
98 |
+
|
99 |
+
</ruleset>
|
readme.txt
CHANGED
@@ -2,8 +2,9 @@
|
|
2 |
Contributors: janwoostendorp
|
3 |
Tags: media, image
|
4 |
Requires at least: 3.5
|
5 |
-
Tested up to: 4.
|
6 |
-
|
|
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
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.2
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
set-default-featured-image.php
CHANGED
@@ -1,222 +1,252 @@
|
|
1 |
-
<?php
|
2 |
-
|
3 |
/**
|
4 |
-
*
|
5 |
* Plugin URI: http://wordpress.org/extend/plugins/default-featured-image/
|
6 |
* Description: Allows users to select a default featured image in the media settings
|
7 |
-
* Version: 1.6.
|
8 |
-
* Requires PHP: 5.5
|
9 |
* Author: Jan Willem Oostendorp
|
10 |
* License: GPLv2 or later
|
11 |
* Text Domain: default-featured-image
|
12 |
*/
|
13 |
-
|
14 |
-
|
15 |
-
{
|
16 |
-
const L10n = 'default-featured-image';
|
17 |
|
18 |
/**
|
19 |
* Hook everything
|
20 |
*/
|
21 |
-
function __construct() {
|
22 |
-
// add the settings field to the media page
|
23 |
add_action( 'admin_init', array( &$this, 'media_setting' ) );
|
24 |
-
// enqueue the js
|
25 |
add_action( 'admin_print_scripts-options-media.php', array( &$this, 'admin_scripts' ) );
|
26 |
-
// get the preview image ajax call
|
27 |
add_action( 'wp_ajax_dfi_change_preview', array( &$this, 'ajax_wrapper' ) );
|
28 |
-
// set dfi meta key on every occasion
|
29 |
-
add_filter( 'get_post_metadata', array(&$this, 'set_dfi_meta_key'), 10, 4 );
|
30 |
-
// display a default featured image
|
31 |
add_filter( 'post_thumbnail_html', array( &$this, 'show_dfi' ), 20, 5 );
|
32 |
-
// add a link on the plugin page to the setting
|
33 |
-
add_filter('
|
34 |
-
// add L10n
|
35 |
-
add_action( 'init', array(&$this, '
|
36 |
-
// remove setting on removal
|
37 |
-
register_uninstall_hook(__FILE__, array('default_featured_image', 'uninstall'));
|
38 |
|
39 |
}
|
40 |
|
41 |
-
|
|
|
|
|
|
|
42 |
delete_option( 'dfi_image_id' );
|
43 |
}
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
}
|
48 |
|
49 |
/**
|
50 |
* Mostly the same as `get_metadata()` makes sure any post thumbnail function gets checked at
|
51 |
* the deepest level possible.
|
52 |
*
|
53 |
-
* @
|
|
|
|
|
|
|
|
|
|
|
54 |
*
|
55 |
-
* @param null $null
|
56 |
-
* @param int $object_id ID of the object metadata is for
|
57 |
-
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
|
58 |
-
* the specified object.
|
59 |
-
* @param bool $single Optional, default is false. If true, return only the first value of the
|
60 |
-
* specified meta_key. This parameter has no effect if meta_key is not specified.
|
61 |
* @return string|array Single metadata value, or array of values
|
|
|
62 |
*/
|
63 |
-
function set_dfi_meta_key( $null
|
64 |
-
// only affect thumbnails on the frontend, do allow ajax calls
|
65 |
-
if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) || '_thumbnail_id'
|
66 |
return $null;
|
|
|
67 |
|
68 |
-
|
69 |
-
$
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
if (
|
72 |
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
|
73 |
-
$meta_cache = $meta_cache[$object_id];
|
74 |
}
|
75 |
|
76 |
-
if (
|
77 |
return $meta_cache;
|
|
|
78 |
|
79 |
-
if ( isset($meta_cache[$meta_key]) ) {
|
80 |
-
if ( $single )
|
81 |
-
return maybe_unserialize( $meta_cache[$meta_key][0] );
|
82 |
-
else
|
83 |
-
return array_map('maybe_unserialize', $meta_cache[$meta_key]);
|
|
|
84 |
}
|
85 |
|
86 |
-
if ($single)
|
87 |
-
// allow to set an other ID see the readme.txt for details
|
88 |
-
return apply_filters( 'dfi_thumbnail_id', get_option( 'dfi_image_id' ), $object_id ); // set the default featured img ID
|
89 |
-
else
|
90 |
return array();
|
|
|
91 |
}
|
92 |
|
93 |
/**
|
94 |
-
*
|
95 |
*/
|
96 |
-
function media_setting() {
|
97 |
register_setting(
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
);
|
102 |
add_settings_field(
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
);
|
109 |
}
|
110 |
|
111 |
/**
|
112 |
-
*
|
113 |
*/
|
114 |
-
function settings_html() {
|
115 |
$value = get_option( 'dfi_image_id' );
|
116 |
|
117 |
$rm_btn_class = 'button button-disabled';
|
118 |
-
if ( !empty( $value ) ) {
|
119 |
-
echo $this->preview_image( $value );
|
120 |
$rm_btn_class = 'button';
|
121 |
}
|
122 |
?>
|
123 |
<input id="dfi_id" type="hidden" value="<?php echo esc_attr( $value ); ?>" name="dfi_image_id"/>
|
124 |
|
125 |
-
<a id="dfi-set-dfi" class="button" title="<?php
|
126 |
<span style="margin-top: 3px;" class="dashicons dashicons-format-image"></span>
|
127 |
-
<?php
|
128 |
</a>
|
129 |
<div style="margin-top:5px;">
|
130 |
-
<a id="dfi-no-fdi" class="<?php echo $rm_btn_class
|
131 |
-
|
|
|
132 |
</a>
|
133 |
</div>
|
134 |
<?php
|
135 |
}
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
}
|
|
|
142 |
return false;
|
143 |
}
|
144 |
|
145 |
/**
|
146 |
* Register the javascript
|
147 |
*/
|
148 |
-
function admin_scripts() {
|
149 |
-
wp_enqueue_media(); // scripts used for uploader
|
150 |
-
wp_enqueue_script( 'dfi-script', plugin_dir_url( __FILE__ ) . 'set-default-featured-image.js' );
|
151 |
-
wp_localize_script(
|
152 |
-
'
|
153 |
-
'
|
154 |
-
|
|
|
|
|
|
|
|
|
155 |
}
|
156 |
|
157 |
/**
|
158 |
-
*
|
159 |
-
*
|
|
|
|
|
160 |
* @return string
|
161 |
*/
|
162 |
-
function preview_image( $image_id ) {
|
163 |
-
$output
|
164 |
$output .= wp_get_attachment_image( $image_id, array( 80, 60 ), true );
|
165 |
$output .= '</div>';
|
|
|
166 |
return $output;
|
167 |
}
|
168 |
|
169 |
/**
|
170 |
* The callback for the ajax call when the DFI changes
|
171 |
*/
|
172 |
-
function ajax_wrapper() {
|
173 |
-
if (
|
174 |
-
|
|
|
175 |
}
|
176 |
-
die(); // ajax call
|
177 |
}
|
178 |
|
179 |
/**
|
180 |
-
*
|
181 |
-
*
|
182 |
-
* @param
|
|
|
183 |
* @return array
|
184 |
*/
|
185 |
-
function add_settings_link( $links
|
|
|
|
|
|
|
186 |
|
187 |
-
if ( $file == plugin_basename( __FILE__ ) ) {
|
188 |
-
$href = admin_url('options-media.php#dfi-set-dfi');
|
189 |
-
$settings_link = '<a href="' . $href . '">' . __( 'Settings' )/*get this from WP core*/ . '</a>';
|
190 |
-
array_unshift( $links, $settings_link );
|
191 |
-
}
|
192 |
return $links;
|
193 |
}
|
194 |
|
195 |
/**
|
196 |
* Set a default featured image if it is missing
|
197 |
-
*
|
198 |
-
* @param
|
199 |
-
* @param int
|
200 |
-
* @param
|
201 |
-
* @param array
|
|
|
|
|
202 |
* @return string
|
203 |
*/
|
204 |
-
function show_dfi( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
|
205 |
-
$default_thumbnail_id = get_option( 'dfi_image_id' ); //select the default thumb
|
206 |
|
207 |
-
// if an image is set return that image
|
208 |
-
if ( $default_thumbnail_id
|
209 |
return $html;
|
|
|
210 |
|
211 |
-
if (isset($attr['class']) ) {
|
212 |
-
$attr['class'] .=
|
213 |
} else {
|
214 |
$size_class = $size;
|
215 |
-
if ( is_array( $size_class )) {
|
216 |
-
$size_class = 'size-' . implode( 'x', $size_class);
|
217 |
}
|
218 |
-
//attachment-$size is a default class `wp_get_attachment_image` would otherwise add. It won't add it if there are classes already there
|
219 |
-
$attr = array
|
220 |
}
|
221 |
|
222 |
$html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
|
@@ -225,5 +255,6 @@ class default_featured_image
|
|
225 |
return $html;
|
226 |
}
|
227 |
}
|
228 |
-
|
229 |
-
|
|
1 |
+
<?php
|
|
|
2 |
/**
|
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.2
|
|
|
7 |
* Author: Jan Willem Oostendorp
|
8 |
* License: GPLv2 or later
|
9 |
* Text Domain: default-featured-image
|
10 |
*/
|
11 |
+
class Default_Featured_Image {
|
12 |
+
const VERSION = '1.6.2';
|
|
|
|
|
13 |
|
14 |
/**
|
15 |
* Hook everything
|
16 |
*/
|
17 |
+
public function __construct() {
|
18 |
+
// add the settings field to the media page.
|
19 |
add_action( 'admin_init', array( &$this, 'media_setting' ) );
|
20 |
+
// enqueue the js.
|
21 |
add_action( 'admin_print_scripts-options-media.php', array( &$this, 'admin_scripts' ) );
|
22 |
+
// get the preview image ajax call.
|
23 |
add_action( 'wp_ajax_dfi_change_preview', array( &$this, 'ajax_wrapper' ) );
|
24 |
+
// set dfi meta key on every occasion.
|
25 |
+
add_filter( 'get_post_metadata', array( &$this, 'set_dfi_meta_key' ), 10, 4 );
|
26 |
+
// display a default featured image.
|
27 |
add_filter( 'post_thumbnail_html', array( &$this, 'show_dfi' ), 20, 5 );
|
28 |
+
// add a link on the plugin page to the setting.
|
29 |
+
add_filter( 'plugin_action_links_default-featured-image/set-default-featured-image.php', array( &$this, 'add_settings_link' ), 10, 1 );
|
30 |
+
// add L10n.
|
31 |
+
add_action( 'init', array( &$this, 'load_plugin_textdomain' ) );
|
32 |
+
// remove setting on removal.
|
33 |
+
register_uninstall_hook( __FILE__, array( 'default_featured_image', 'uninstall' ) );
|
34 |
|
35 |
}
|
36 |
|
37 |
+
/**
|
38 |
+
* Uninstall
|
39 |
+
*/
|
40 |
+
public static function uninstall() {
|
41 |
delete_option( 'dfi_image_id' );
|
42 |
}
|
43 |
|
44 |
+
/**
|
45 |
+
* L10n
|
46 |
+
*/
|
47 |
+
public function load_plugin_textdomain() {
|
48 |
+
load_plugin_textdomain( 'default-featured-image', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
|
49 |
}
|
50 |
|
51 |
/**
|
52 |
* Mostly the same as `get_metadata()` makes sure any post thumbnail function gets checked at
|
53 |
* the deepest level possible.
|
54 |
*
|
55 |
+
* @param null|mixed $null Should be null, because we want to override the meta value.
|
56 |
+
* @param int $object_id ID of the object metadata is for.
|
57 |
+
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
|
58 |
+
* the specified object.
|
59 |
+
* @param bool $single Optional, default is false. If true, return only the first value of the
|
60 |
+
* specified meta_key. This parameter has no effect if meta_key is not specified.
|
61 |
*
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
* @return string|array Single metadata value, or array of values
|
63 |
+
* @see get_metadata() in /wp-includes/meta.php
|
64 |
*/
|
65 |
+
public function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
|
66 |
+
// only affect thumbnails on the frontend, do allow ajax calls.
|
67 |
+
if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) || '_thumbnail_id' !== $meta_key ) {
|
68 |
return $null;
|
69 |
+
}
|
70 |
|
71 |
+
// ignore attachments, non image attachments have icons, which get overridden otherwise.
|
72 |
+
$post = get_post( $object_id );
|
73 |
+
if ( ! empty( $post->post_type ) && 'attachment' === $post->post_type ) {
|
74 |
+
return $null;
|
75 |
+
}
|
76 |
+
|
77 |
+
$meta_type = 'post';
|
78 |
+
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
|
79 |
|
80 |
+
if ( ! $meta_cache ) {
|
81 |
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
|
82 |
+
$meta_cache = $meta_cache[ $object_id ];
|
83 |
}
|
84 |
|
85 |
+
if ( ! $meta_key ) {
|
86 |
return $meta_cache;
|
87 |
+
}
|
88 |
|
89 |
+
if ( isset( $meta_cache[ $meta_key ] ) ) {
|
90 |
+
if ( $single ) {
|
91 |
+
return maybe_unserialize( $meta_cache[ $meta_key ][0] );
|
92 |
+
} else {
|
93 |
+
return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
|
94 |
+
}
|
95 |
}
|
96 |
|
97 |
+
if ( $single ) {
|
98 |
+
// allow to set an other ID see the readme.txt for details.
|
99 |
+
return apply_filters( 'dfi_thumbnail_id', get_option( 'dfi_image_id' ), $object_id ); // set the default featured img ID.
|
100 |
+
} else {
|
101 |
return array();
|
102 |
+
}
|
103 |
}
|
104 |
|
105 |
/**
|
106 |
+
* Register the setting on the media settings page.
|
107 |
*/
|
108 |
+
public function media_setting() {
|
109 |
register_setting(
|
110 |
+
'media', // settings page.
|
111 |
+
'dfi_image_id', // option name.
|
112 |
+
array( &$this, 'input_validation' ) // validation callback.
|
113 |
);
|
114 |
add_settings_field(
|
115 |
+
'dfi', // id.
|
116 |
+
__( 'Default featured image', 'default-featured-image' ), // setting title.
|
117 |
+
array( &$this, 'settings_html' ), // display callback.
|
118 |
+
'media', // settings page.
|
119 |
+
'default' // settings section.
|
120 |
);
|
121 |
}
|
122 |
|
123 |
/**
|
124 |
+
* Display the buttons and a preview on the media settings page.
|
125 |
*/
|
126 |
+
public function settings_html() {
|
127 |
$value = get_option( 'dfi_image_id' );
|
128 |
|
129 |
$rm_btn_class = 'button button-disabled';
|
130 |
+
if ( ! empty( $value ) ) {
|
131 |
+
echo $this->preview_image( $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
132 |
$rm_btn_class = 'button';
|
133 |
}
|
134 |
?>
|
135 |
<input id="dfi_id" type="hidden" value="<?php echo esc_attr( $value ); ?>" name="dfi_image_id"/>
|
136 |
|
137 |
+
<a id="dfi-set-dfi" class="button" title="<?php esc_attr_e( 'Select default featured image', 'default-featured-image' ); ?>" href="#">
|
138 |
<span style="margin-top: 3px;" class="dashicons dashicons-format-image"></span>
|
139 |
+
<?php esc_html_e( 'Select default featured image', 'default-featured-image' ); ?>
|
140 |
</a>
|
141 |
<div style="margin-top:5px;">
|
142 |
+
<a id="dfi-no-fdi" class="<?php echo esc_attr( $rm_btn_class ); ?>"
|
143 |
+
title="<?php esc_attr_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>" href="#">
|
144 |
+
<?php esc_html_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>
|
145 |
</a>
|
146 |
</div>
|
147 |
<?php
|
148 |
}
|
149 |
|
150 |
+
/**
|
151 |
+
* Is the given input a valid image.
|
152 |
+
*
|
153 |
+
* @param string|int $thumbnail_id The saving thumbnail.
|
154 |
+
*
|
155 |
+
* @return string|bool
|
156 |
+
*/
|
157 |
+
public function input_validation( $thumbnail_id ) {
|
158 |
+
if ( wp_attachment_is_image( $thumbnail_id ) ) {
|
159 |
+
return $thumbnail_id;
|
160 |
}
|
161 |
+
|
162 |
return false;
|
163 |
}
|
164 |
|
165 |
/**
|
166 |
* Register the javascript
|
167 |
*/
|
168 |
+
public function admin_scripts() {
|
169 |
+
wp_enqueue_media(); // scripts used for uploader.
|
170 |
+
wp_enqueue_script( 'dfi-script', plugin_dir_url( __FILE__ ) . 'set-default-featured-image.js', array(), self::VERSION, true );
|
171 |
+
wp_localize_script(
|
172 |
+
'dfi-script',
|
173 |
+
'dfi_L10n',
|
174 |
+
array(
|
175 |
+
'manager_title' => __( 'Select default featured image', 'default-featured-image' ),
|
176 |
+
'manager_button' => __( 'Set default featured image', 'default-featured-image' ),
|
177 |
+
)
|
178 |
+
);
|
179 |
}
|
180 |
|
181 |
/**
|
182 |
+
* Get an image and wrap it in a div
|
183 |
+
*
|
184 |
+
* @param int $image_id A valid attachment image ID.
|
185 |
+
*
|
186 |
* @return string
|
187 |
*/
|
188 |
+
public function preview_image( $image_id ) {
|
189 |
+
$output = '<div id="preview-image" style="float:left; padding: 0 5px 0 0;">';
|
190 |
$output .= wp_get_attachment_image( $image_id, array( 80, 60 ), true );
|
191 |
$output .= '</div>';
|
192 |
+
|
193 |
return $output;
|
194 |
}
|
195 |
|
196 |
/**
|
197 |
* The callback for the ajax call when the DFI changes
|
198 |
*/
|
199 |
+
public function ajax_wrapper() {
|
200 |
+
if ( ! empty( $_POST['image_id'] ) && absint( $_POST['image_id'] ) ) {
|
201 |
+
$img_id = absint( $_POST['image_id'] );
|
202 |
+
echo $this->preview_image( $img_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
203 |
}
|
204 |
+
die(); // ajax call..
|
205 |
}
|
206 |
|
207 |
/**
|
208 |
+
* Add a settings link to the the plugin on the plugin page
|
209 |
+
*
|
210 |
+
* @param array $links An array of plugin action links.
|
211 |
+
*
|
212 |
* @return array
|
213 |
*/
|
214 |
+
public function add_settings_link( $links ) {
|
215 |
+
$href = admin_url( 'options-media.php#dfi-set-dfi' );
|
216 |
+
$settings_link = '<a href="' . $href . '">' . __( 'Settings' ) . '</a>'; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
|
217 |
+
array_unshift( $links, $settings_link );
|
218 |
|
|
|
|
|
|
|
|
|
|
|
219 |
return $links;
|
220 |
}
|
221 |
|
222 |
/**
|
223 |
* Set a default featured image if it is missing
|
224 |
+
*
|
225 |
+
* @param string $html The post thumbnail HTML.
|
226 |
+
* @param int $post_id The post ID.
|
227 |
+
* @param int $post_thumbnail_id The post thumbnail ID.
|
228 |
+
* @param string $size The post thumbnail size. Image size or array of width and height.
|
229 |
+
* @param array $attr values (in that order). Default 'post-thumbnail'.
|
230 |
+
*
|
231 |
* @return string
|
232 |
*/
|
233 |
+
public function show_dfi( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
|
234 |
+
$default_thumbnail_id = get_option( 'dfi_image_id' ); // select the default thumb.
|
235 |
|
236 |
+
// if an image is set return that image.
|
237 |
+
if ( (int) $default_thumbnail_id !== (int) $post_thumbnail_id ) {
|
238 |
return $html;
|
239 |
+
}
|
240 |
|
241 |
+
if ( isset( $attr['class'] ) ) {
|
242 |
+
$attr['class'] .= ' default-featured-img';
|
243 |
} else {
|
244 |
$size_class = $size;
|
245 |
+
if ( is_array( $size_class ) ) {
|
246 |
+
$size_class = 'size-' . implode( 'x', $size_class );
|
247 |
}
|
248 |
+
// attachment-$size is a default class `wp_get_attachment_image` would otherwise add. It won't add it if there are classes already there.
|
249 |
+
$attr = array( 'class' => "attachment-{$size_class} default-featured-img" );
|
250 |
}
|
251 |
|
252 |
$html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
|
255 |
return $html;
|
256 |
}
|
257 |
}
|
258 |
+
|
259 |
+
// run the plugin class.
|
260 |
+
new Default_Featured_Image();
|
trunk/languages/default-featured-image-nl_NL.mo
ADDED
Binary file
|
trunk/languages/default-featured-image-nl_NL.po
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
msgid ""
|
2 |
+
msgstr ""
|
3 |
+
"Project-Id-Version: Default featured Image\n"
|
4 |
+
"POT-Creation-Date: 2012-12-24 16:42+0100\n"
|
5 |
+
"PO-Revision-Date: 2012-12-24 16:42+0100\n"
|
6 |
+
"Last-Translator: Jan Willem Oostendorp <janw.oostendorp@gmail.com>\n"
|
7 |
+
"Language-Team: Jan Willem Oostendorp <janw.oostendorp@gmail.com>\n"
|
8 |
+
"Language: nl_NL\n"
|
9 |
+
"MIME-Version: 1.0\n"
|
10 |
+
"Content-Type: text/plain; charset=UTF-8\n"
|
11 |
+
"Content-Transfer-Encoding: 8bit\n"
|
12 |
+
"X-Generator: Poedit 1.5.3\n"
|
13 |
+
"X-Poedit-KeywordsList: __;_e\n"
|
14 |
+
"X-Poedit-Basepath: ../\n"
|
15 |
+
"X-Poedit-SearchPath-0: .\n"
|
16 |
+
|
17 |
+
#: set-default-featured-image.php:44
|
18 |
+
msgid "Default featured image"
|
19 |
+
msgstr "Standaard uitgelichte afbeelding"
|
20 |
+
|
21 |
+
#: set-default-featured-image.php:65 set-default-featured-image.php:67
|
22 |
+
#: set-default-featured-image.php:90
|
23 |
+
msgid "Select default featured image"
|
24 |
+
msgstr "Selecteer een standaard uitgelichte afbeelding"
|
25 |
+
|
26 |
+
#: set-default-featured-image.php:70 set-default-featured-image.php:71
|
27 |
+
msgid "Don't use a default featured image"
|
28 |
+
msgstr "Geen standaard uitgelichte afbeelding"
|
29 |
+
|
30 |
+
#: set-default-featured-image.php:91
|
31 |
+
msgid "Set default featured image"
|
32 |
+
msgstr "Zet standaard uitgelichte afbeelding"
|
33 |
+
|
34 |
+
#: set-default-featured-image.php:126
|
35 |
+
msgid "Settings"
|
36 |
+
msgstr "Instellingen"
|
37 |
+
|
38 |
+
#~ msgid "Select a default featured image"
|
39 |
+
#~ msgstr "Selecteer een standaard uitgelichte afbeelding"
|
trunk/phpcs.xml.dist
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="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-"/>
|
69 |
+
<rule ref="PHPCompatibilityWP"/>
|
70 |
+
|
71 |
+
<!--
|
72 |
+
#############################################################################
|
73 |
+
SNIFF SPECIFIC CONFIGURATION
|
74 |
+
#############################################################################
|
75 |
+
-->
|
76 |
+
|
77 |
+
<!-- Set the minimum supported WP version. This is used by several sniffs. -->
|
78 |
+
<config name="minimum_supported_wp_version" value="5.2"/>
|
79 |
+
|
80 |
+
<!-- Verify that all gettext calls use the correct text domain. -->
|
81 |
+
<rule ref="WordPress.WP.I18n">
|
82 |
+
<properties>
|
83 |
+
<property name="text_domain" type="array">
|
84 |
+
<element value="default-featured-image"/>
|
85 |
+
</property>
|
86 |
+
</properties>
|
87 |
+
</rule>
|
88 |
+
|
89 |
+
<!-- Verify that everything in the global namespace is prefixed with a plugin specific prefix. -->
|
90 |
+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
|
91 |
+
<properties>
|
92 |
+
<!-- Provide the prefixes to look for. -->
|
93 |
+
<property name="prefixes" type="array">
|
94 |
+
<element value="dfi"/>
|
95 |
+
</property>
|
96 |
+
</properties>
|
97 |
+
</rule>
|
98 |
+
|
99 |
+
</ruleset>
|
trunk/readme.txt
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Plugin Name ===
|
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.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 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 |
+
|
28 |
+
1. Unzip the folder to the `/wp-content/plugins/` directory
|
29 |
+
2. Activate the plugin through the 'Plugins' menu in WordPress
|
30 |
+
3. Go to the settings->media page and select an image.
|
31 |
+
|
32 |
+
== Frequently Asked Questions ==
|
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 |
+
|
100 |
+
1. The setting on the media page
|
101 |
+
2. The media manager will start with the current selected image
|
102 |
+
|
103 |
+
== Changelog ==
|
104 |
+
|
105 |
+
= 1.6.1 =
|
106 |
+
* Small readme.txt update.
|
107 |
+
|
108 |
+
= 1.6 =
|
109 |
+
* On of the last fixes didn't account for all situations.
|
110 |
+
|
111 |
+
= 1.5 =
|
112 |
+
* Fixed two small (and rare) warnings
|
113 |
+
* Added translation domain
|
114 |
+
|
115 |
+
= 1.4 =
|
116 |
+
* Added plugin images both the plugin header as the thumbnail. Based on the boat WP.org uses in it's theme previews
|
117 |
+
* Fixed a bug where the ajax calls didn't return the DFI [forum thread](https://wordpress.org/support/topic/dfi-woocommerce-facetwp?replies=10)
|
118 |
+
|
119 |
+
= 1.3 =
|
120 |
+
* Filter `dfi_thumbnail_id` now also returns the post ID of the post (or any postype) that is being called. See the FAQ for new examples
|
121 |
+
|
122 |
+
= 1.2 =
|
123 |
+
* Filter `dfi_thumbnail_id` is now called in an earlier stage.
|
124 |
+
|
125 |
+
= 1.1 =
|
126 |
+
* Fixed inheriting classes of the image
|
127 |
+
|
128 |
+
= 1.0 =
|
129 |
+
* Plugin will now remove it's setting on plugin removal
|
130 |
+
* added a default class to the `<img>` tag, if it shows a default featured image
|
131 |
+
* The default featured image will now also return with `get_post_thumbnail_id`, making the chance that it fail far far smaller.
|
132 |
+
* The image given in the media page is now validated
|
133 |
+
|
134 |
+
= 0.9 =
|
135 |
+
* Launch
|
136 |
+
|
137 |
+
== Upgrade Notice ==
|
138 |
+
|
139 |
+
= 1.0 =
|
140 |
+
Update makes sure that the set image will show. Everywhere.
|
trunk/set-default-featured-image.js
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
jQuery(document).ready(function ($) {
|
2 |
+
'use strict';
|
3 |
+
|
4 |
+
var $set_button = $('#dfi-set-dfi'),
|
5 |
+
$td = $set_button.parent(),
|
6 |
+
$hidden_input = $td.find('#dfi_id'),
|
7 |
+
$del_button = $td.find('#dfi-no-fdi');
|
8 |
+
|
9 |
+
/**
|
10 |
+
* @param html the preview html
|
11 |
+
*/
|
12 |
+
function set_preview_html(html) {
|
13 |
+
var $cur_preview = $td.find('#preview-image');
|
14 |
+
// remove old
|
15 |
+
$cur_preview.remove();
|
16 |
+
// prepend new
|
17 |
+
$td.prepend(html);
|
18 |
+
//disable button
|
19 |
+
$del_button.removeClass('button-disabled');
|
20 |
+
}
|
21 |
+
|
22 |
+
/**
|
23 |
+
* @param image_id int
|
24 |
+
* @return html string with the image
|
25 |
+
*/
|
26 |
+
function set_preview_image(image_id) {
|
27 |
+
var data = {
|
28 |
+
action: 'dfi_change_preview',
|
29 |
+
image_id: image_id
|
30 |
+
};
|
31 |
+
|
32 |
+
$.post(ajaxurl, data, function (response) {
|
33 |
+
set_preview_html(response);
|
34 |
+
});
|
35 |
+
|
36 |
+
// return responseText;
|
37 |
+
}
|
38 |
+
|
39 |
+
/**
|
40 |
+
* set a loading image until the ajax is ready
|
41 |
+
*/
|
42 |
+
function set_loading_image() {
|
43 |
+
var $cur_preview = $td.find('#preview-image'),
|
44 |
+
html = '<div id="preview-image" style="float:left; padding: 0 5px 0 0; height: 60px;"><img src="images/loading.gif"/></div>';
|
45 |
+
|
46 |
+
$cur_preview.remove();
|
47 |
+
$td.prepend(html);
|
48 |
+
}
|
49 |
+
|
50 |
+
/**
|
51 |
+
* @param selected_id the selected image id
|
52 |
+
*/
|
53 |
+
function set_dfi(selected_id) {
|
54 |
+
$hidden_input.val(selected_id);
|
55 |
+
// set preview
|
56 |
+
set_loading_image();
|
57 |
+
set_preview_image(selected_id);
|
58 |
+
}
|
59 |
+
|
60 |
+
// remove featured image
|
61 |
+
$del_button.click(function (e) {
|
62 |
+
e.preventDefault();
|
63 |
+
var $cur_preview = $td.find('#preview-image');
|
64 |
+
$cur_preview.remove();
|
65 |
+
$hidden_input.val('');
|
66 |
+
$(this).addClass('button-disabled');
|
67 |
+
});
|
68 |
+
|
69 |
+
/**
|
70 |
+
* open the media manager
|
71 |
+
*/
|
72 |
+
$set_button.click(function (e) {
|
73 |
+
e.preventDefault();
|
74 |
+
var frame = wp.media({
|
75 |
+
title : dfi_L10n.manager_title,
|
76 |
+
multiple : false,
|
77 |
+
library : { type : 'image' },
|
78 |
+
button : { text : dfi_L10n.manager_button }
|
79 |
+
});
|
80 |
+
// close event media manager
|
81 |
+
frame.on('close', function () {
|
82 |
+
var images = frame.state().get('selection');
|
83 |
+
// set the images
|
84 |
+
images.each(function (image) {
|
85 |
+
set_dfi(image.id);
|
86 |
+
});
|
87 |
+
});
|
88 |
+
|
89 |
+
// open event media manager
|
90 |
+
frame.on('open', function () {
|
91 |
+
var attachment,
|
92 |
+
selection = frame.state().get('selection'),
|
93 |
+
id = $hidden_input.val();
|
94 |
+
|
95 |
+
attachment = wp.media.attachment(id);
|
96 |
+
attachment.fetch();
|
97 |
+
|
98 |
+
selection.add(attachment ? [ attachment ] : []);
|
99 |
+
});
|
100 |
+
|
101 |
+
// everything is set open the media manager
|
102 |
+
frame.open();
|
103 |
+
});
|
104 |
+
|
105 |
+
|
106 |
+
}); // doc rdy
|
trunk/set-default-featured-image.php
ADDED
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
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.2
|
7 |
+
* Author: Jan Willem Oostendorp
|
8 |
+
* License: GPLv2 or later
|
9 |
+
* Text Domain: default-featured-image
|
10 |
+
*/
|
11 |
+
class Default_Featured_Image {
|
12 |
+
const VERSION = '1.6.2';
|
13 |
+
|
14 |
+
/**
|
15 |
+
* Hook everything
|
16 |
+
*/
|
17 |
+
public function __construct() {
|
18 |
+
// add the settings field to the media page.
|
19 |
+
add_action( 'admin_init', array( &$this, 'media_setting' ) );
|
20 |
+
// enqueue the js.
|
21 |
+
add_action( 'admin_print_scripts-options-media.php', array( &$this, 'admin_scripts' ) );
|
22 |
+
// get the preview image ajax call.
|
23 |
+
add_action( 'wp_ajax_dfi_change_preview', array( &$this, 'ajax_wrapper' ) );
|
24 |
+
// set dfi meta key on every occasion.
|
25 |
+
add_filter( 'get_post_metadata', array( &$this, 'set_dfi_meta_key' ), 10, 4 );
|
26 |
+
// display a default featured image.
|
27 |
+
add_filter( 'post_thumbnail_html', array( &$this, 'show_dfi' ), 20, 5 );
|
28 |
+
// add a link on the plugin page to the setting.
|
29 |
+
add_filter( 'plugin_action_links_default-featured-image/set-default-featured-image.php', array( &$this, 'add_settings_link' ), 10, 1 );
|
30 |
+
// add L10n.
|
31 |
+
add_action( 'init', array( &$this, 'load_plugin_textdomain' ) );
|
32 |
+
// remove setting on removal.
|
33 |
+
register_uninstall_hook( __FILE__, array( 'default_featured_image', 'uninstall' ) );
|
34 |
+
|
35 |
+
}
|
36 |
+
|
37 |
+
/**
|
38 |
+
* Uninstall
|
39 |
+
*/
|
40 |
+
public static function uninstall() {
|
41 |
+
delete_option( 'dfi_image_id' );
|
42 |
+
}
|
43 |
+
|
44 |
+
/**
|
45 |
+
* L10n
|
46 |
+
*/
|
47 |
+
public function load_plugin_textdomain() {
|
48 |
+
load_plugin_textdomain( 'default-featured-image', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
|
49 |
+
}
|
50 |
+
|
51 |
+
/**
|
52 |
+
* Mostly the same as `get_metadata()` makes sure any post thumbnail function gets checked at
|
53 |
+
* the deepest level possible.
|
54 |
+
*
|
55 |
+
* @param null|mixed $null Should be null, because we want to override the meta value.
|
56 |
+
* @param int $object_id ID of the object metadata is for.
|
57 |
+
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
|
58 |
+
* the specified object.
|
59 |
+
* @param bool $single Optional, default is false. If true, return only the first value of the
|
60 |
+
* specified meta_key. This parameter has no effect if meta_key is not specified.
|
61 |
+
*
|
62 |
+
* @return string|array Single metadata value, or array of values
|
63 |
+
* @see get_metadata() in /wp-includes/meta.php
|
64 |
+
*/
|
65 |
+
public function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
|
66 |
+
// only affect thumbnails on the frontend, do allow ajax calls.
|
67 |
+
if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) || '_thumbnail_id' !== $meta_key ) {
|
68 |
+
return $null;
|
69 |
+
}
|
70 |
+
|
71 |
+
// ignore attachments, non image attachments have icons, which get overridden otherwise.
|
72 |
+
$post = get_post( $object_id );
|
73 |
+
if ( ! empty( $post->post_type ) && 'attachment' === $post->post_type ) {
|
74 |
+
return $null;
|
75 |
+
}
|
76 |
+
|
77 |
+
$meta_type = 'post';
|
78 |
+
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
|
79 |
+
|
80 |
+
if ( ! $meta_cache ) {
|
81 |
+
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
|
82 |
+
$meta_cache = $meta_cache[ $object_id ];
|
83 |
+
}
|
84 |
+
|
85 |
+
if ( ! $meta_key ) {
|
86 |
+
return $meta_cache;
|
87 |
+
}
|
88 |
+
|
89 |
+
if ( isset( $meta_cache[ $meta_key ] ) ) {
|
90 |
+
if ( $single ) {
|
91 |
+
return maybe_unserialize( $meta_cache[ $meta_key ][0] );
|
92 |
+
} else {
|
93 |
+
return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
|
94 |
+
}
|
95 |
+
}
|
96 |
+
|
97 |
+
if ( $single ) {
|
98 |
+
// allow to set an other ID see the readme.txt for details.
|
99 |
+
return apply_filters( 'dfi_thumbnail_id', get_option( 'dfi_image_id' ), $object_id ); // set the default featured img ID.
|
100 |
+
} else {
|
101 |
+
return array();
|
102 |
+
}
|
103 |
+
}
|
104 |
+
|
105 |
+
/**
|
106 |
+
* Register the setting on the media settings page.
|
107 |
+
*/
|
108 |
+
public function media_setting() {
|
109 |
+
register_setting(
|
110 |
+
'media', // settings page.
|
111 |
+
'dfi_image_id', // option name.
|
112 |
+
array( &$this, 'input_validation' ) // validation callback.
|
113 |
+
);
|
114 |
+
add_settings_field(
|
115 |
+
'dfi', // id.
|
116 |
+
__( 'Default featured image', 'default-featured-image' ), // setting title.
|
117 |
+
array( &$this, 'settings_html' ), // display callback.
|
118 |
+
'media', // settings page.
|
119 |
+
'default' // settings section.
|
120 |
+
);
|
121 |
+
}
|
122 |
+
|
123 |
+
/**
|
124 |
+
* Display the buttons and a preview on the media settings page.
|
125 |
+
*/
|
126 |
+
public function settings_html() {
|
127 |
+
$value = get_option( 'dfi_image_id' );
|
128 |
+
|
129 |
+
$rm_btn_class = 'button button-disabled';
|
130 |
+
if ( ! empty( $value ) ) {
|
131 |
+
echo $this->preview_image( $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
132 |
+
$rm_btn_class = 'button';
|
133 |
+
}
|
134 |
+
?>
|
135 |
+
<input id="dfi_id" type="hidden" value="<?php echo esc_attr( $value ); ?>" name="dfi_image_id"/>
|
136 |
+
|
137 |
+
<a id="dfi-set-dfi" class="button" title="<?php esc_attr_e( 'Select default featured image', 'default-featured-image' ); ?>" href="#">
|
138 |
+
<span style="margin-top: 3px;" class="dashicons dashicons-format-image"></span>
|
139 |
+
<?php esc_html_e( 'Select default featured image', 'default-featured-image' ); ?>
|
140 |
+
</a>
|
141 |
+
<div style="margin-top:5px;">
|
142 |
+
<a id="dfi-no-fdi" class="<?php echo esc_attr( $rm_btn_class ); ?>"
|
143 |
+
title="<?php esc_attr_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>" href="#">
|
144 |
+
<?php esc_html_e( 'Don\'t use a default featured image', 'default-featured-image' ); ?>
|
145 |
+
</a>
|
146 |
+
</div>
|
147 |
+
<?php
|
148 |
+
}
|
149 |
+
|
150 |
+
/**
|
151 |
+
* Is the given input a valid image.
|
152 |
+
*
|
153 |
+
* @param string|int $thumbnail_id The saving thumbnail.
|
154 |
+
*
|
155 |
+
* @return string|bool
|
156 |
+
*/
|
157 |
+
public function input_validation( $thumbnail_id ) {
|
158 |
+
if ( wp_attachment_is_image( $thumbnail_id ) ) {
|
159 |
+
return $thumbnail_id;
|
160 |
+
}
|
161 |
+
|
162 |
+
return false;
|
163 |
+
}
|
164 |
+
|
165 |
+
/**
|
166 |
+
* Register the javascript
|
167 |
+
*/
|
168 |
+
public function admin_scripts() {
|
169 |
+
wp_enqueue_media(); // scripts used for uploader.
|
170 |
+
wp_enqueue_script( 'dfi-script', plugin_dir_url( __FILE__ ) . 'set-default-featured-image.js', array(), self::VERSION, true );
|
171 |
+
wp_localize_script(
|
172 |
+
'dfi-script',
|
173 |
+
'dfi_L10n',
|
174 |
+
array(
|
175 |
+
'manager_title' => __( 'Select default featured image', 'default-featured-image' ),
|
176 |
+
'manager_button' => __( 'Set default featured image', 'default-featured-image' ),
|
177 |
+
)
|
178 |
+
);
|
179 |
+
}
|
180 |
+
|
181 |
+
/**
|
182 |
+
* Get an image and wrap it in a div
|
183 |
+
*
|
184 |
+
* @param int $image_id A valid attachment image ID.
|
185 |
+
*
|
186 |
+
* @return string
|
187 |
+
*/
|
188 |
+
public function preview_image( $image_id ) {
|
189 |
+
$output = '<div id="preview-image" style="float:left; padding: 0 5px 0 0;">';
|
190 |
+
$output .= wp_get_attachment_image( $image_id, array( 80, 60 ), true );
|
191 |
+
$output .= '</div>';
|
192 |
+
|
193 |
+
return $output;
|
194 |
+
}
|
195 |
+
|
196 |
+
/**
|
197 |
+
* The callback for the ajax call when the DFI changes
|
198 |
+
*/
|
199 |
+
public function ajax_wrapper() {
|
200 |
+
if ( ! empty( $_POST['image_id'] ) && absint( $_POST['image_id'] ) ) {
|
201 |
+
$img_id = absint( $_POST['image_id'] );
|
202 |
+
echo $this->preview_image( $img_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
203 |
+
}
|
204 |
+
die(); // ajax call..
|
205 |
+
}
|
206 |
+
|
207 |
+
/**
|
208 |
+
* Add a settings link to the the plugin on the plugin page
|
209 |
+
*
|
210 |
+
* @param array $links An array of plugin action links.
|
211 |
+
*
|
212 |
+
* @return array
|
213 |
+
*/
|
214 |
+
public function add_settings_link( $links ) {
|
215 |
+
$href = admin_url( 'options-media.php#dfi-set-dfi' );
|
216 |
+
$settings_link = '<a href="' . $href . '">' . __( 'Settings' ) . '</a>'; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
|
217 |
+
array_unshift( $links, $settings_link );
|
218 |
+
|
219 |
+
return $links;
|
220 |
+
}
|
221 |
+
|
222 |
+
/**
|
223 |
+
* Set a default featured image if it is missing
|
224 |
+
*
|
225 |
+
* @param string $html The post thumbnail HTML.
|
226 |
+
* @param int $post_id The post ID.
|
227 |
+
* @param int $post_thumbnail_id The post thumbnail ID.
|
228 |
+
* @param string $size The post thumbnail size. Image size or array of width and height.
|
229 |
+
* @param array $attr values (in that order). Default 'post-thumbnail'.
|
230 |
+
*
|
231 |
+
* @return string
|
232 |
+
*/
|
233 |
+
public function show_dfi( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
|
234 |
+
$default_thumbnail_id = get_option( 'dfi_image_id' ); // select the default thumb.
|
235 |
+
|
236 |
+
// if an image is set return that image.
|
237 |
+
if ( (int) $default_thumbnail_id !== (int) $post_thumbnail_id ) {
|
238 |
+
return $html;
|
239 |
+
}
|
240 |
+
|
241 |
+
if ( isset( $attr['class'] ) ) {
|
242 |
+
$attr['class'] .= ' default-featured-img';
|
243 |
+
} else {
|
244 |
+
$size_class = $size;
|
245 |
+
if ( is_array( $size_class ) ) {
|
246 |
+
$size_class = 'size-' . implode( 'x', $size_class );
|
247 |
+
}
|
248 |
+
// attachment-$size is a default class `wp_get_attachment_image` would otherwise add. It won't add it if there are classes already there.
|
249 |
+
$attr = array( 'class' => "attachment-{$size_class} default-featured-img" );
|
250 |
+
}
|
251 |
+
|
252 |
+
$html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
|
253 |
+
$html = apply_filters( 'dfi_thumbnail_html', $html, $post_id, $default_thumbnail_id, $size, $attr );
|
254 |
+
|
255 |
+
return $html;
|
256 |
+
}
|
257 |
+
}
|
258 |
+
|
259 |
+
// run the plugin class.
|
260 |
+
new Default_Featured_Image();
|