-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultiple-featured-images.php
More file actions
373 lines (303 loc) · 12.5 KB
/
multiple-featured-images.php
File metadata and controls
373 lines (303 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/*
Plugin Name: Multiple Featured Images
Description: Enables multiple featured images for posts and pages. If you like my plugin, feel free to give me reward ;) <a href="http://www.amazon.de/registry/wishlist/16KTW9ZG027C8" title="Amazon Wishlist" target="_blank">Amazon Wishlist</a>
Version: 0.3
Author: Marcus Kober
Author URI: http://www.koeln-dialog.de/
*/
/* Copyright 2012 Marcus Kober (m.kober@koeln-dialog.de)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if( !class_exists( 'kdMultipleFeaturedImages' ) ) {
class kdMultipleFeaturedImages {
private $id = '';
private $post_type = '';
private $labels = array();
private $metabox_id = '';
private $post_meta_key = '';
private $nonce = '';
private $default_labels = array(
'name' => 'Featured Image 2',
'set' => 'Set featured image 2',
'remove' => 'Remove featured image 2',
'use' => 'Use as featured image 2',
);
private $default_args = array(
'id' => 'featured-image-2',
'post_type' => 'page',
);
/**
* Initialize the plugin
*
* @param array $args
* @return void
*/
public function __construct( $args ) {
$this->labels = wp_parse_args( $args['labels'], $this->default_labels );
unset( $args['labels'] );
$args = wp_parse_args( $args, $this->default_args );
$this->id = $args['id'];
$this->post_type = $args['post_type'];
$this->metabox_id = $this->id.'_'.$this->post_type;
$this->post_meta_key= 'kd_'.$this->id.'_'.$this->post_type.'_id';
$this->nonce = 'mfi-'.$this->args['id'].$this->args['post_type'];
if( !current_theme_supports( 'post-thumbnails' ) ) {
add_theme_support( 'post-thumbnails' );
}
add_action( 'admin_init', array( &$this, 'kd_admin_init' ) );
add_action( 'add_meta_boxes', array( &$this, 'kd_add_meta_box' ) );
add_filter( 'attachment_fields_to_edit', array( &$this, 'kd_add_attachment_field' ), 11, 2 );
add_action( 'wp_ajax_set-MuFeaImg-'.$this->id.'-'.$this->post_type, array( &$this, 'kd_ajax_set_image' ) );
add_action( 'delete_attachment', array( &$this, 'kd_delete_attachment' ) );
}
/**
* Add admin-Javascript
*
* @return void
*/
public function kd_admin_init() {
wp_enqueue_script(
'kd-multiple-featured-images',
plugins_url( 'multiple-featured-images' ).'/js/kd-admin.js',
'jquery'
);
}
/**
* Add admin metabox for choosing additional featured images
*
* @return void
*/
public function kd_add_meta_box() {
add_meta_box(
$this->metabox_id,
$this->labels['name'],
array( $this, 'kd_meta_box_content' ),
$this->post_type,
'side',
'low'
);
}
/**
* Output the metabox content
*
* @global object $post
* @return void
*/
public function kd_meta_box_content() {
global $post;
$image_id = get_post_meta(
$post->ID,
$this->post_meta_key,
true
);
echo $this->kd_meta_box_output( $image_id );
}
/**
* Generate the metabox content
*
* @global int $post_ID
* @param int $image_id
* @return string
*/
public function kd_meta_box_output( $image_id = NULL ) {
global $post_ID;
$output = '';
$setImageLink = sprintf(
'<p class="hide-if-no-js"><a title="%2$s" href="%1$s" id="kd_%3$s" class="thickbox">%%s</a></p>',
get_upload_iframe_src( 'image' ),
$this->labels['set'],
$this->id
);
if( $image_id && get_post( $image_id ) ) {
$nonce_field = wp_create_nonce( $this->nonce.$post_ID );
$thumbnail = wp_get_attachment_image( $image_id, array( 266, 266 ) );
$output.= sprintf( $setImageLink, $thumbnail );
$output.= '<p class="hide-if-no-js">';
$output.= sprintf(
'<a href="#" id="remove-%1$s-image" onclick="kdMuFeaImgRemove( \'%1$s\', \'%2$s\', \'%3$s\' ); return false;">',
$this->id,
$this->post_type,
$nonce_field
);
$output.= $this->labels['remove'];
$output.= '</a>';
$output.= '</p>';
return $output;
}
else {
return sprintf( $setImageLink, $this->labels['set'] );
}
}
/**
* Create a new field in the image upload form
*
* @param string $form_fields
* @param object $post
* @return string
*/
public function kd_add_attachment_field( $form_fields, $post ) {
$calling_id = 0;
if( isset( $_GET['post_id'] ) ) {
$calling_id = absint( $_GET['post_id'] );
}
elseif( isset( $_POST ) && count( $_POST ) ) {
$calling_id = $post->post_parent;
}
$calling_post = get_post( $calling_id );
if( is_null( $calling_post ) || $calling_post->post_type != $this->post_type ) {
return $form_fields;
}
$nonce_field = wp_create_nonce( $this->nonce.$calling_id );
$output = sprintf(
'<a href="#" id="%1$s-featuredimage" onclick="kdMuFeaImgSet( %3$s, \'%1$s\', \'%2$s\', \'%6$s\' ); return false;">%5$s</a>',
$this->id,
$this->post_type,
$post->ID,
$this->labels['name'],
$this->labels['use'],
$nonce_field
);
$form_fields['MuFeaImg-'.$this->id.'-'.$this->post_type] = array(
'label' => $this->labels['name'],
'input' => 'html',
'html' => $output
);
return $form_fields;
}
/**
* Ajax function: set and delete featured image
*
* @global int $post_ID
* @return void
*/
public function kd_ajax_set_image() {
global $post_ID;
$post_ID = intval( $_POST['post_id'] );
if( !current_user_can( 'edit_post', $post_ID ) ) {
die( '-1' );
}
$thumb_id = intval( $_POST['thumbnail_id'] );
check_ajax_referer( $this->nonce.$post_ID );
if( $thumb_id == '-1' ) {
delete_post_meta( $post_ID, $this->post_meta_key );
die( $this->kd_meta_box_output( NULL ) );
}
if( $thumb_id && get_post( $thumb_id) ) {
$thumb_html = wp_get_attachment_image( $thumb_id, 'thumbnail' );
if( !empty( $thumb_html ) ) {
update_post_meta( $post_ID, $this->post_meta_key, $thumb_id );
die( $this->kd_meta_box_output( $thumb_id ) );
}
}
die( '0' );
}
/**
* Delete custom featured image if attachmet is deleted
*
* @global object $wpdb
* @param int $post_id
* @return void
*/
public function kd_delete_attachment( $post_id ) {
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->postmeta WHERE meta_key = '%s' AND meta_value = %d",
$this->post_meta_key,
$post_id
)
);
}
/**
* Retrieve the id of the featured image
*
* @global object $post
* @param string $image_id
* @param string $post_type
* @param int $post_id
* @return int
*/
public static function get_featured_image_id( $image_id, $post_type, $post_id = NULL) {
global $post;
if( is_null( $post_id ) ) {
$post_id = get_the_ID();
}
return get_post_meta( $post_id, "kd_{$image_id}_{$post_type}_id", true);
}
/**
* Return the featured image url
*
* @param string $image_id
* @param string $post_type
* @param int $post_id
* @return string
*/
public static function get_featured_image_url( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
$id = self::get_featured_image_id( $image_id, $post_type, $post_id);
if( $size != 'full' ) {
$url = wp_get_attachment_image_src( $id, $size );
$url = $url[0];
}
else {
$url = wp_get_attachment_url( $id );
}
return $url;
}
/**
* Return the featured image html output
*
* @param string $image_id
* @param string $post_type
* @param string $size
* @param int $post_id
* @return string
*/
public static function get_the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
$id = self::get_featured_image_id( $image_id, $post_type, $post_id);
$output = '';
if( $id ) {
$output = wp_get_attachment_image(
$id,
$size,
false
);
}
return $output;
}
/**
* Output the featured image html output
*
* @param string $image_id
* @param string $post_type
* @param string $size
* @param int $post_id
* @return void
*/
public static function the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
echo self::get_the_featured_image( $image_id, $post_type, $size, $post_id );
}
}
}
function kd_mfi_get_featured_image_id( $image_id, $post_type, $post_id = NULL ) {
return kdMultipleFeaturedImages::get_featured_image_id( $image_id, $post_type, $post_id );
}
function kd_mfi_get_featured_image_url( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
return kdMultipleFeaturedImages::get_featured_image_url( $image_id, $post_type, $size, $post_id );
}
function kd_mfi_get_the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
return kdMultipleFeaturedImages::get_the_featured_image( $image_id, $post_type, $size, $post_id );
}
function kd_mfi_the_featured_image( $image_id, $post_type, $size = 'full', $post_id = NULL ) {
return kdMultipleFeaturedImages::the_featured_image( $image_id, $post_type, $size, $post_id );
}
?>