-
Notifications
You must be signed in to change notification settings - Fork 1
/
gecka-terms-thumbnails.php
1379 lines (1098 loc) · 35.9 KB
/
gecka-terms-thumbnails.php
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* Plugin Name: Gecka Terms Thumbnails
* Plugin URI: http://gecka-apps.com
* Description: Add thumbnails support to categories and any chosen taxonomies.
* Version: 1.1
* Author: Gecka Apps, Gecka
* Author URI: http://gecka.nc
* Text Domain: gecka-terms-thumbnails
* Domain Path: /languages
* Licence: GPL
* Requires at least: 3.0
* Tested up to: 4.4
*/
/*
* Copyright 2011 Gecka (email : [email protected])
*
* 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
*/
// Load the textdomain
load_plugin_textdomain( 'gecka-terms-thumbnails', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
require_once dirname( __FILE__ ) . '/settings.php';
$gecka_terms_thumbnails = Gecka_Terms_Thumbnails::instance();
class Gecka_Terms_Thumbnails {
/**
* Plugin settings
* @var Gecka_Terms_Thumbnails_Settings
*/
public static $settings;
/**
* Allowed mime types
* @var array
*/
public static $mimes = array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
);
/**
* Singleton intance
* @var Gecka_Terms_Thumbnails
*/
private static $instance;
/**
* Uri to the plugin folder
* @var string
*/
private static $plugin_url;
/**
* Absolute path to the plugin folder
* @var string
*/
private static $plugin_path;
private static $taxonomies = array( 'category' );
/**
* Thumbnails sizes
* @var array
*/
private static $thumbnails_sizes = array();
private $error;
/**
* Private constructor (singleton)
*/
private function __construct() {
self::$plugin_url = plugins_url( '', __FILE__ );
self::$plugin_path = dirname( __FILE__ );
// init settings
self::$settings = Gecka_Terms_Thumbnails_Settings::instance();
// add default thumbnails sizes
self::add_image_size( 'admin-thumbnail', 50, 50, true );
self::add_image_size( 'thumbnail', self::$settings->term_thumbnail_size_w, self::$settings->term_thumbnail_size_h, self::$settings->term_thumbnail_crop );
self::add_image_size( 'medium', self::$settings->term_medium_size_w, self::$settings->term_medium_size_h, self::$settings->term_medium_crop );
register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 5 );
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ), 5 );
add_action( 'init', array( $this, 'metadata_wpdbfix' ) );
add_action( 'switch_blog', array( $this, 'metadata_wpdbfix' ) );
add_filter( 'widget_categories_args', array( $this, 'widget_categories_args' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
/**
* Registers a new term thumbnail size
*
* @param string $name
* @param int $width
* @param int $height
* @param bool $crop
*/
public static function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
self::$thumbnails_sizes[ $name ] = array(
'width' => absint( $width ),
'height' => absint( $height ),
'crop' => (bool) $crop
);
}
/* =Manages taxonomies terms image support
----------------------------------------------- */
/**
* Returns the singleton instance
* @return Gecka_Terms_Thumbnails
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
/**
* Adds thumbnail support for the specified taxonomy
*
* @param string $taxonomy
*/
public static function add_taxonomy_support( $taxonomy ) {
$taxonomies = (array) $taxonomy;
self::$taxonomies = array_merge( self::$taxonomies, $taxonomies );
}
/**
* Removes thumbnail support for the specified taxonomy
*
* @param string $taxonomy
*/
public static function remove_taxonomy_support( $taxonomy ) {
$key = array_search( $taxonomy, self::$taxonomies );
if ( false !== $key ) {
unset( self::$taxonomies[ $key ] );
}
}
/* =Manages terms thumbnails sizes
----------------------------------------------- */
/**
* Sets the default thumbnail size
*
* @param int $width
* @param int $height
* @param bool $crop
*/
public static function set_thumbnail( $width = 0, $height = 0, $crop = false ) {
self::add_image_size( 'thumbnail', $width, $height, $crop );
}
/**
* Returns true if the specified term has a thumbnail image for the specified category and size
*
* @param int $term_id
* @param string $size
*/
public static function has_term_thumbnail( $term_id, $taxonomy, $size = null ) {
$image_infos = self::get_term_image_infos( $term_id, $taxonomy );
if ( empty( $image_infos ) ) {
return false;
} elseif ( ! $size ) {
return true;
}
if ( isset ( $image_infos['thumbnails'][ $size ] ) ) {
return true;
}
return false;
}
/* =Static functions to display terms thumbnails
----------------------------------------------- */
/**
* Return a term's thumbnail meta data for the specified taxonomy
*
* @param int $term_id
* @param string $taxonomy
*
* @return array
*/
public static function get_term_image_infos( $term_id, $taxonomy ) {
$meta_data = false;
if ( $taxonomy ) {
$meta_data = get_metadata( 'term', $term_id, 'image-' . $taxonomy, true );
}
// compatibility with beta1
if ( ! $meta_data ) {
$meta_data = get_metadata( 'term', $term_id, 'image', true );
}
return $meta_data;
}
/**
* Returns the specified term's thumbnail's HTML code for the specified taxonomy and size
*
* @param int $term_id
* @param string $taxonomy
* @param string $size
* @param array $attr
*/
public static function get_the_term_thumbnail( $term_id, $taxonomy, $size = 'thumbnail', $attr = '' ) {
$size = apply_filters( 'term_thumbnail_size', $size, $term_id, $taxonomy );
$image = self::get_term_thumbnail( $term_id, $taxonomy, $size );
$term = get_term( $term_id, $taxonomy );
if ( $image ) {
do_action( 'begin_fetch_term_thumbnail_html', $term_id, $taxonomy, $image, $size );
list( $src, $width, $height ) = $image;
$hwstring = image_hwstring( $width, $height );
if ( is_array( $size ) ) {
$size = $width . 'x' . $height;
}
$default_attr = array(
'src' => $src,
'class' => "attachment-$size $taxonomy-thumbnail",
'alt' => trim( strip_tags( $term->name ) ), // Use Alt field first
'title' => trim( strip_tags( $term->name ) ),
);
$attr = wp_parse_args( $attr, $default_attr );
$attr = apply_filters( 'get_the_term_thumbnail_attributes', $attr, $term_id, $taxonomy, $image, $size );
$attr = array_map( 'esc_attr', $attr );
$html = rtrim( "<img $hwstring" );
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
do_action( 'end_fetch_term_thumbnail_html', $term_id, $taxonomy, $image, $size );
} else {
$html = '';
}
return apply_filters( 'term_thumbnail_html', $html, $term_id, $taxonomy, $image, $size, $attr );
}
/**
* Returns the specified term's thumbnail for the specified taxonomy and size
*
* @param int $term_id
* @param string $taxonomy
* @param string $size
*/
public static function get_term_thumbnail( $term_id, $taxonomy, $size = null ) {
$infos = self::get_term_image_infos( $term_id, $taxonomy );
if ( ! $infos ) {
return false;
}
if ( ! $size ) {
return array( $infos['url'], $infos['infos'][0], $infos['infos'][1] );
}
if ( is_array( $size ) ) {
/*
* @TODO here we need to fing the thumbnail nearest to the asked size
*/
}
if ( ! isset( $infos['thumbnails'][ $size ] ) ) {
return false;
}
$infos = $infos['thumbnails'][ $size ];
return array( $infos['url'], $infos['infos'][0], $infos['infos'][1] );
}
/**
* Manage terms image meta
*/
/**
* Checks PHP version and create the needed database table on plugin activation
*/
public function activation_hook() {
// checks the PHP version
if ( version_compare( PHP_VERSION, '5.0.0', '<' ) ) {
deactivate_plugins( basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ ) ); // Deactivate ourself
wp_die( "Sorry, the Gecka Terms Ordering plugin requires PHP 5 or higher." );
}
// creates the needed database table
global $wpdb;
/**
* Create the termmeta database table, for WordPress < version 4.4.
*
* The max index length is required since 4.2, because of the move to utf8mb4 collation.
*
* @see wp_get_db_schema()
*/
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "termmeta";
$max_index_length = 191;
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
meta_id bigint(20) unsigned NOT NULL auto_increment,
term_id bigint(20) unsigned NOT NULL default '0',
meta_key varchar(255) default NULL,
meta_value longtext,
PRIMARY KEY (meta_id),
KEY term_id (term_id),
KEY meta_key (meta_key($max_index_length))
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/* =Manages the terms images metadata
----------------------------------------------- */
/**
* Filters default thumbnails sizes and supported taxonomies
* Runs on the plugins_loaded action hook
*/
public function plugins_loaded() {
self::$taxonomies = apply_filters( 'terms-thumbnails-default-sizes', self::$taxonomies );
self::$thumbnails_sizes = apply_filters( 'terms-thumbnails-default-sizes', self::$thumbnails_sizes );
}
/**
* Filters default thumbnails sizes and supported taxonomies
* Runs on the after_setup_theme action hook
*/
public function after_setup_theme() {
self::$taxonomies = apply_filters( 'terms-thumbnails-taxonomies', self::$taxonomies );
self::$thumbnails_sizes = apply_filters( 'terms-thumbnails-sizes', self::$thumbnails_sizes );
}
/* =Terms images and thumbnails path and url
----------------------------------------------- */
/**
* Sets our table name into wpdb
* Runs on the init and switch_blog action hooks
*/
public function metadata_wpdbfix() {
global $wpdb;
if ( ! isset( $wpdb->termmeta ) ) {
$wpdb->termmeta = "{$wpdb->prefix}termmeta";
}
}
/**
* Filters the default categories widget args
* Run on the widget_categories_args filter hook
*
* @param array $args
*
* @return array
*/
public function widget_categories_args( $args ) {
// default taxonomy
$taxonomy = empty( $args->taxonomy ) ? 'category' : $args->taxonomy;
// the taxonomy hasn't thumbnail support, so we ignore it
if ( ! self::has_support( $taxonomy ) ) {
return $args;
}
// default thumbnail size
if ( ! isset( $args['show_thumbnail'] ) ) {
$args['show_thumbnail'] = 'thumbnail';
}
// our custom walker to add thumbnails
$args['walker'] = new Walker_Term();
return $args;
}
/* =Static functions to manage terms images
----------------------------------------------- */
/**
* Return true if the specified taxonomy has terms thumbnails support
*
* @param string $taxonomy
*
* @return bool
*/
public static function has_support( $taxonomy ) {
if ( in_array( $taxonomy, self::$taxonomies ) ) {
return true;
}
return false;
}
/**
* Init the admin
* Runs on the admin_init action hook
*/
public function admin_init() {
// adds scripts and css
add_action( 'admin_head-edit-tags.php', array( $this, 'admin_head' ) );
// show our admin notices
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
// adds/removes our errors var to url on redirect
add_filter( 'wp_redirect', array( $this, 'wp_redirect' ) );
foreach ( self::$taxonomies as $taxonomy ) {
// add a file field to terms add and edit form
add_action( $taxonomy . '_add_form_fields', array( $this, 'add_field' ), 10, 2 );
add_action( $taxonomy . '_edit_form_fields', array( $this, 'edit_field' ), 10, 2 );
// save image on term save
add_action( "edited_$taxonomy", array( $this, 'process_upload' ), 10, 2 );
// generate thumbnails after a term is saved
add_action( "edited_$taxonomy", array( $this, 'generate_thumbnails_action' ), 15, 2 );
// delete image and thumbnails of deleted terms
add_action( "delete_term", array( $this, 'delete_term' ), 5, 3 );
// add images column to terms list-table
add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'edit_columns' ) );
add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'columns' ), 10, 3 );
}
add_action( 'wp_ajax_delete_term_image', array( $this, 'ajax_delete_term_image' ) );
}
/**
* Css and script on the admin terms forms
* Runs on the admin_head-edit-tags.php action hook
*/
public function admin_head() {
if ( empty( $_GET['taxonomy'] ) || ! self::has_support( $_GET['taxonomy'] ) ) {
return;
}
if ( isset( $_GET['tag_ID'] ) && $term_id = (int) $_GET['tag_ID'] ) :
?>
<script type="text/javascript">
<!--
jQuery(document).ready(function ($) {
var nonce = '<?php echo wp_create_nonce( 'delete_term_image' ) ?>';
$('#delete-thumb-button').click(
function () {
$.post(ajaxurl, {
term_id: <?php echo esc_js($term_id) ?>,
taxonomy: '<?php echo esc_js($_GET['taxonomy']) ?>',
action: 'delete_term_image',
_nonce: nonce
}, function (data) {
if (data == '1') $('#term_thumbnail').hide('slow');
});
}
);
});
//-->
</script><?php
endif;
?>
<style type="text/css">
<!--
th#image {
width: 55px
}
.attachment-admin-thumbnail {
border: 1px solid #ccc;
padding: 3px;
}
-->
</style><?php
}
/**
* Shows errors in admin
* Runs on the admin_notices action hook
*/
public function admin_notice() {
if ( empty( $_GET['term_image_error'] ) ) {
return;
}
$error = unserialize( base64_decode( $_GET['term_image_error'] ) );
if ( ! is_wp_error( $error ) ) {
return;
}
echo '<div class="error">
<p><strong>' . __( 'Image upload error: ', 'gecka_terms_ordering' ) . '</strong>' . $error->get_error_message() . '</p>
</div>';
}
/**
* On wp_redirect, we add/remove our errors var as needed
*
* @param string $location
* Runs on the wp_redirect filter hook
*/
public function wp_redirect( $location ) {
$location = remove_query_arg( 'term_image_error', $location );
if ( ! $this->error ) {
return $location;
}
$location = add_query_arg( 'term_image_error', urlencode( base64_encode( serialize( $this->error ) ) ), $location );
return $location;
}
/* =Misc static functions
----------------------------------------------- */
/**
* Adds a field to the "Add term" form
*
* @param string $taxonomy
* Runs on the {$taxonomy}_add_form_fields action hook
*/
public function add_field( $taxonomy ) {
?>
<div class="form-field">
<label for="tag-description"><?php _e( 'Image', 'Gecka_Terms_Thumbnails' ) ?></label>
<p><?php _e( 'Once you have added this new term, edit it to set its image.', 'gecka-terms-thumbnails' ); ?></p>
</div>
<?php
}
/* =Action and filter hooks
----------------------------------------------- */
/**
* Adds a field to the "Edit term" form
*
* @param string $taxonomy
* Runs on the {$taxonomy}_edit_form_fields action hook
*/
public function edit_field( $term, $taxonomy ) {
$term_id = $term->term_id;
$current = self::get_term_image_infos( $term_id, $taxonomy );
$upload_size_unit = $max_upload_size = wp_max_upload_size();
$sizes = array( 'KB', 'MB', 'GB' );
for ( $u = - 1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u ++ ) {
$upload_size_unit /= 1024;
}
if ( $u < 0 ) {
$upload_size_unit = 0;
$u = 0;
} else {
$upload_size_unit = (int) $upload_size_unit;
}
?>
<tr class="form-field">
<th scope="row" valign="top"><label
for="image"><?php _ex( 'Image', 'Taxonomy Image', 'gecka-terms-thumbnails' ); ?></label></th>
<td>
<?php if ( has_term_thumbnail( $term_id, $taxonomy ) ) : ?>
<div id="term_thumbnail">
<p class="description"><?php _e( 'You already have an image defined. You can delete it or replace. To keep it, ignore the following fields.', 'gecka-terms-thumbnails' ); ?></p>
<br>
<?php the_term_thumbnail( $term_id, $taxonomy, 'admin-thumbnail', array( 'style' => 'float:left; border: 1px solid #ccc; margin-right: 10px; padding: 3px; ' ) ); ?>
<input type="button" id="delete-thumb-button"
value="<?php _e( 'Delete the current image', 'gecka-terms-thumbnails' ) ?>"
class="button-secondary action" style="width: auto">
<br><br>
</div>
<?php endif; ?>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_upload_size ?>"/>
<input type="file" id="image" name="image" style="width: 97%;" class="button-secondary action"><br/>
<span
class="description"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[ $u ] ); ?></span>
</tr>
<script type="text/javascript">
<!--
jQuery('#edittag').attr('enctype', 'multipart/form-data').attr('encoding', 'multipart/form-data');
//-->
</script>
<?php
}
/**
* Process the thumbnial upload
* Runs on the edited_$taxonomy action hook
*
* @param int $term_id
* @param int $tt_id
*
* @return stdClass|boolean:
*/
public function process_upload( $term_id, $tt_id ) {
// get the taxonomy and check that it supports images
if ( empty( $_POST['taxonomy'] ) || ! self::has_support( $_POST['taxonomy'] ) ) {
return;
}
$taxonomy = $_POST['taxonomy'];
if ( ! self::has_support( $taxonomy ) ) {
return $term;
}
$file = isset( $_FILES['image'] ) ? $_FILES['image'] : null;
if ( ! $file ) {
return $term;
}
/* create the taxonomy directory if needed */
if ( ! $dir = self::images_mkdir( $taxonomy ) ) {
return $this->upload_error( $file, __( "Permission error creating the terms-images/{taxonomy} folder.", 'gecka-terms-thumbnails' ) );
}
// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
$upload_error_strings = array(
false,
__( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
__( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
__( "The uploaded file was only partially uploaded." ),
__( "No file was uploaded.", 'gecka-terms-thumbnails' ),
'',
__( "Missing a temporary folder." ),
__( "Failed to write file to disk." ),
__( "File upload stopped by extension." )
);
if ( $file['error'] > 0 && $file['error'] !== 4 ) {
return $this->upload_error( $file, $upload_error_strings[ $file['error'] ] );
}
if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] && $file['error'] !== 4 ) {
return $this->upload_error( $file, $file['error'] );
}
// A non-empty file will pass this test.
if ( ! ( $file['size'] > 0 ) ) {
return $term;
}
// A properly uploaded file will pass this test.
if ( ! @ is_uploaded_file( $file['tmp_name'] ) ) {
return $this->upload_error( $file, __( 'Specified file failed upload test.' ) );
}
// mime check
$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], self::$mimes );
extract( $wp_filetype );
if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
return $this->upload_error( $file, __( 'Sorry, this file type is not permitted for security reasons.' ) );
}
/* delete old image if it exists */
if ( false === self::remove_term_image( $term_id, $taxonomy ) ) {
@ unlink( $new_file );
return $this->upload_error( $file, __( 'An error occured when trying to remove the old image.', 'gecka-terms-thumbnails' ) );
}
if ( $proper_filename ) {
$file['name'] = $proper_filename;
}
if ( ! $ext ) {
$ext = ltrim( strrchr( $file['name'], '.' ), '.' );
}
if ( ! $type ) {
$type = $file['type'];
}
/* get a unique filename */
$filename = wp_unique_filename( $dir, $file['name'] );
// Move the file to the uploads dir
$new_file = $dir . "/$filename";
/* moves uploaded file */
if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) {
return
$file_infos = array();
}
$file_infos ['name'] = $filename;
$file_infos ['size'] = $file['size'];
$file_infos ['path'] = $new_file;
$file_infos ['url'] = self::images_url() . '/' . $taxonomy . '/' . $filename;
$file_infos ['type'] = $type;
$file_infos ['ext'] = $ext;
$file_infos ['infos'] = getimagesize( $new_file );
$file_infos ['thumbnails'] = array();
self::update_term_image_infos( $term_id, $taxonomy, $file_infos );
}
/**
* Make a directory
*
* @param string $taxonomy optional category name to create a taxnonomy directory
*/
public static function images_mkdir( $taxonomy = '' ) {
global $wp_filesystem;
WP_Filesystem();
$dir = self::images_dir() . ( $taxonomy ? '/' . $taxonomy : '' );
if ( ! wp_mkdir_p( $dir ) && ! is_dir( $dir ) ) // Only check to see if the Dir exists upon creation failure. Less I/O this way.
{
wp_die( __( 'Could not create directory.' ) );
}
return $dir;
}
/**
* Returns the absolute path to the thumbnails folder
* @return string
*/
public static function images_dir() {
$upload_dir_infos = wp_upload_dir();
return $upload_dir_infos['basedir'] . '/terms-images';
}
/**
* Handles upload errors
*
* @param array $file
* @param $message $message
*/
private function upload_error( &$file, $message ) {
$this->error = new WP_Error( 'invalid_upload', $message, $file );
return false;
}
/**
* Remove a term's image (and its thumbnails)
*
* @param int $term_id
* @param string $taxonomy
*/
public static function remove_term_image( $term_id, $taxonomy ) {
$infos = self::get_term_image_infos( $term_id, $taxonomy );
if ( ! $infos ) {
return;
}
if ( ! empty( $infos ) && isset( $infos['path'] ) ) {
if ( false === self::remove_term_thumbnails( $term_id, $taxonomy ) ) {
return false;
}
if ( ! @ unlink( $infos['path'] ) && file_exists( $infos['path'] ) ) {
return false;
}
}
self::delete_term_image_infos( $term_id, $taxonomy );
return true;
}
/**
* Removes the generated thumbnails of a term
*
* @param int $term_id
* @param string $taxonomy
*
* @return bool
*/
public static function remove_term_thumbnails( $term_id, $taxonomy ) {
$infos = self::get_term_image_infos( $term_id, $taxonomy );
if ( ! $infos ) {
return;
}
if ( empty( $infos['thumbnails'] ) ) {
return true;
}
foreach ( $infos['thumbnails'] as $name => $thumbnail ) {
if ( false === self::remove_term_thumbnail( $name, $term_id, $taxonomy ) ) {
return false;
}
}
return true;
}
/**
* Removes aterm's thumbnail
* @pram string $thumbnail_name
*
* @param int $term_id
* @param string $taxonomy
*
* @return bool
*/
public static function remove_term_thumbnail( $thumbnail_name, $term_id, $taxonomy ) {
if ( $thumbnail_name == 'admin_thumbnail' ) {
return;
}
$infos = self::get_term_image_infos( $term_id, $taxonomy );
if ( ! $infos ) {
return;
}
if ( empty( $infos['thumbnails'] ) || empty( $infos['thumbnails'][ $thumbnail_name ] ) ) {
return true;
}
$thumbnail = $infos['thumbnails'][ $thumbnail_name ];
if ( file_exists( $thumbnail['path'] ) && ! @ unlink( $thumbnail['path'] ) ) {
return false;
}
unset( $infos['thumbnails'][ $thumbnail_name ] );
self::update_term_image_infos( $term_id, $taxonomy, $infos );
return true;
}
/**
* Updates a term thumbnail metadata
*
* @param int $term_id
* @param sttring $taxonomy
* @param array $infos
*
* @return boolean
*/
public static function update_term_image_infos( $term_id, $taxonomy, $infos ) {
// compatibility with beta1
if ( get_metadata( 'term', $term_id, 'image', true ) ) {
delete_metadata( 'term', $term_id, 'image' );
}
return update_metadata( 'term', $term_id, 'image-' . $taxonomy, $infos );
}
/**
* Deletes a term's thumbnail metadata
*
* @param int $term_id
* @param string $taxonomy
*
* @return boolean
*/
public static function delete_term_image_infos( $term_id, $taxonomy ) {
// compatibility with beta1
if ( get_metadata( 'term', $term_id, 'image-' . $taxonomy, true ) ) {
return delete_metadata( 'term', $term_id, 'image-' . $taxonomy );
}
// compatibility with beta1
if ( get_metadata( 'term', $term_id, 'image', true ) ) {
return delete_metadata( 'term', $term_id, 'image' );
}
return delete_metadata( 'term', $term_id, 'image-' . $taxonomy );
}
/**
* Returns the URI to the thumbnails folder
* @return string
*/
public static function images_url() {
$upload_dir_infos = wp_upload_dir();
return $upload_dir_infos['baseurl'] . '/terms-images';
}
/**
* Denerates the thumbnails of a saved term
* Runs on the edited_$taxonomy action hook
*
* @param unknown_type $term_id
* @param unknown_type $tt_id
*/
public function generate_thumbnails_action( $term_id, $tt_id ) {
$taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'category';
if ( ! self::has_support( $taxonomy ) ) {
return;
}
self::generate_thumbnails( $term_id, $taxonomy );
}
/**
* Generate a term's thumbnails
*
* @param int $term_id
* @param string $taxonomy
*/