forked from mfields/Taxonomy-Images
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaxonomy-images.php
1184 lines (1014 loc) · 35.6 KB
/
taxonomy-images.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: Taxonomy Images
Plugin URI: http://wordpress.mfields.org/plugins/taxonomy-images/
Description: Associate images from your media library to categories, tags and custom taxonomies.
Version: 0.8.0
Author: Michael Fields
Author URI: http://wordpress.mfields.org/
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright 2010-2011 Michael Fields [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
*/
require_once( trailingslashit( dirname( __FILE__ ) ) . 'deprecated.php' );
require_once( trailingslashit( dirname( __FILE__ ) ) . 'public-filters.php' );
/**
* Version Number.
*
* @return string The plugin's version number.
* @access private
* @since 0.7
* @alter 0.7.4
*/
function taxonomy_image_plugin_version() {
return '0.8.0';
}
/**
* Get a url to a file in this plugin.
*
* @return string
* @access private
* @since 0.7
*/
function taxonomy_image_plugin_url( $file = '' ) {
static $path = '';
if ( empty( $path ) ) {
$path = plugin_dir_url( __FILE__ );
}
return $path . $file;
}
/**
* Detail Image Size.
*
* @return array Configuration for the "detail" image size.
* @access private
* @since 0.7
*/
function taxonomy_image_plugin_detail_image_size() {
return array(
'name' => 'detail',
'size' => array( 75, 75, true )
);
}
/**
* Register custom image size with WordPress.
*
* @access private
* @since 2010-10-28
*/
function taxonomy_image_plugin_add_image_size() {
$detail = taxonomy_image_plugin_detail_image_size();
add_image_size(
$detail['name'],
$detail['size'][0],
$detail['size'][1],
$detail['size'][2]
);
}
add_action( 'init', 'taxonomy_image_plugin_add_image_size' );
/**
* Load Plugin Text Domain.
*
* @access private
* @since 0.7.3
*/
function taxonomy_image_plugin_text_domain() {
load_plugin_textdomain( 'taxonomy-images', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'init', 'taxonomy_image_plugin_text_domain' );
/**
* Modal Button.
*
* Create a button in the modal media window to associate the current image to the term.
*
* @param array Multidimensional array representing the images form.
* @param stdClass WordPress post object.
* @return array The image's form array with added button if modal window was accessed by this script.
*
* @access private
* @since 2010-10-28
* @alter 0.7
*/
function taxonomy_image_plugin_modal_button( $fields, $post ) {
if ( isset( $fields['image-size'] ) && isset( $post->ID ) ) {
$image_id = (int) $post->ID;
$o = '<div class="taxonomy-image-modal-control" id="' . esc_attr( 'taxonomy-image-modal-control-' . $image_id ) . '">';
$o.= '<span class="button create-association">' . sprintf( esc_html__( 'Associate with %1$s', 'taxonomy-images' ), '<span class="term-name">' . esc_html__( 'this term', 'taxonomy-images' ) . '</span>' ) . '</span>';
$o.= '<span class="remove-association">' . sprintf( esc_html__( 'Remove association with %1$s', 'taxonomy-images' ), '<span class="term-name">' . esc_html__( 'this term', 'taxonomy-images' ) . '</span>' ) . '</span>';
$o.= '<input class="taxonomy-image-button-image-id" name="' . esc_attr( 'taxonomy-image-button-image-id-' . $image_id ) . '" type="hidden" value="' . esc_attr( $image_id ) . '" />';
$o.= '<input class="taxonomy-image-button-nonce-create" name="' . esc_attr( 'taxonomy-image-button-nonce-create-' . $image_id ) . '" type="hidden" value="' . esc_attr( wp_create_nonce( 'taxonomy-image-plugin-create-association' ) ) . '" />';
$o.= '<input class="taxonomy-image-button-nonce-remove" name="' . esc_attr( 'taxonomy-image-button-nonce-remove-' . $image_id ) . '" type="hidden" value="' . esc_attr( wp_create_nonce( 'taxonomy-image-plugin-remove-association' ) ) . '" />';
$o.= '</div>';
$fields['image-size']['extra_rows']['taxonomy-image-plugin-button']['html'] = $o;
}
return $fields;
}
add_filter( 'attachment_fields_to_edit', 'taxonomy_image_plugin_modal_button', 20, 2 );
/**
* Get Image Source.
*
* Return a uri to a custom image size.
*
* If size doesn't exist, attempt to create a resized version.
* The output of this function should be escaped before printing to the browser.
*
* @param int Image ID.
* @return string URI of custom image on success; emtpy string otherwise.
*
* @access private.
* @since 2010-10-28
*/
function taxonomy_image_plugin_get_image_src( $id ) {
$detail = taxonomy_image_plugin_detail_image_size();
/* Return url to custom intermediate size if it exists. */
$img = image_get_intermediate_size( $id, $detail['name'] );
if ( isset( $img['url'] ) )
return $img['url'];
/* Detail image does not exist, attempt to create it. */
$wp_upload_dir = wp_upload_dir();
if ( isset( $wp_upload_dir['basedir'] ) ) {
/* Create path to original uploaded image. */
$path = trailingslashit( $wp_upload_dir['basedir'] ) . get_post_meta( $id, '_wp_attached_file', true );
if ( is_file( $path ) ) {
/* Attempt to create a new downsized version of the original image. */
$new = image_resize( $path,
$detail['size'][0],
$detail['size'][1],
$detail['size'][2]
);
/* Image creation successful. Generate and cache image metadata. Return url. */
if ( ! is_wp_error( $new ) ) {
$meta = wp_generate_attachment_metadata( $id, $path );
wp_update_attachment_metadata( $id, $meta );
$img = image_get_intermediate_size( $id, $detail['name'] );
if ( isset( $img['url'] ) ) {
return $img['url'];
}
}
}
}
/* Custom intermediate size cannot be created, try for thumbnail. */
$img = image_get_intermediate_size( $id, 'thumbnail' );
if ( isset( $img['url'] ) )
return $img['url'];
/* Thumbnail cannot be found, try fullsize. */
$url = wp_get_attachment_url( $id );
if ( ! empty( $url ) )
return $url;
/*
* No image can be found.
* This is most likely caused by a user deleting an attachment before deleting it's association with a taxonomy.
* If we are in the administration panels:
* - Delete the association.
* - Return uri to default.png.
*/
if ( is_admin() ) {
$assoc = taxonomy_image_plugin_get_associations();
foreach ( $assoc as $term => $img ) {
if ( $img === $id ) {
unset( $assoc[$term] );
}
}
update_option( 'taxonomy_image_plugin', $assoc );
return taxonomy_image_plugin_url( 'default.png' );
}
/*
* No image can be found.
* Return path to blank-image.png.
*/
return taxonomy_image_plugin_url( 'blank.png' );
}
/**
* Sanitize Associations.
*
* Ensures that all key/value pairs are positive integers.
* This filter will discard all zero and negative values.
*
* @param array An array of term_taxonomy_id/attachment_id pairs.
* @return array Sanitized version of parameter.
*
* @access private
*/
function taxonomy_image_plugin_sanitize_associations( $associations ) {
$o = array();
foreach ( (array) $associations as $tt_id => $im_id ) {
$tt_id = absint( $tt_id );
$im_id = absint( $im_id );
if ( 0 < $tt_id && 0 < $im_id )
$o[$tt_id] = $im_id;
}
return $o;
}
/**
* Sanitize Settings.
*
* This function is responsible for ensuring that
* all values within the 'taxonomy_image_plugin_settings'
* options are of the appropriate type.
*
* @param array Unknown.
* @return array Multi-dimensional array of sanitized settings.
*
* @access private
* @since 0.7
*/
function taxonomy_image_plugin_settings_sanitize( $dirty ) {
$clean = array();
if ( isset( $dirty['taxonomies'] ) ) {
$taxonomies = get_taxonomies();
foreach ( (array) $dirty['taxonomies'] as $taxonomy ) {
if ( in_array( $taxonomy, $taxonomies ) )
$clean['taxonomies'][] = $taxonomy;
}
}
/* translators: Notice displayed on the custom administration page. */
$message = __( 'Image support for taxonomies successfully updated', 'taxonomy-images' );
if ( empty( $clean ) ) {
/* translators: Notice displayed on the custom administration page. */
$message = __( 'Image support has been disabled for all taxonomies.', 'taxonomy-images' );
}
add_settings_error( 'taxonomy_image_plugin_settings', 'taxonomies_updated', esc_html( $message ), 'updated' );
return $clean;
}
/**
* Register settings with WordPress.
*
* This plugin will store to sets of settings in the
* options table. The first is named 'taxonomy_image_plugin'
* and stores the associations between terms and images. The
* keys in this array represent the term_taxonomy_id of the
* term while the value represents the ID of the image
* attachment.
*
* The second setting is used to store everything else. As of
* version 0.7 it has one key named 'taxonomies' whichi is a
* flat array consisting of taxonomy names representing a
* black-list of registered taxonomies. These taxonomies will
* NOT be given an image UI.
*
* @access private
*/
function taxonomy_image_plugin_register_settings() {
register_setting(
'taxonomy_image_plugin',
'taxonomy_image_plugin',
'taxonomy_image_plugin_sanitize_associations'
);
register_setting(
'taxonomy_image_plugin_settings',
'taxonomy_image_plugin_settings',
'taxonomy_image_plugin_settings_sanitize'
);
add_settings_section(
'taxonomy_image_plugin_settings',
esc_html__( 'Settings', 'taxonomy-images' ),
'__return_false',
'taxonomy_image_plugin_settings'
);
add_settings_field(
'taxonomy-images',
esc_html__( 'Taxonomies', 'taxonomy-images' ),
'taxonomy_image_plugin_control_taxonomies',
'taxonomy_image_plugin_settings',
'taxonomy_image_plugin_settings'
);
}
add_action( 'admin_init', 'taxonomy_image_plugin_register_settings' );
/**
* Admin Menu.
*
* Create the admin menu link for the settings page.
*
* @access private
* @since 0.7
*/
function taxonomy_images_settings_menu() {
add_options_page(
esc_html__( 'Taxonomy Images', 'taxonomy-images' ), /* HTML <title> tag. */
esc_html__( 'Taxonomy Images', 'taxonomy-images' ), /* Link text in admin menu. */
'manage_options',
'taxonomy_image_plugin_settings',
'taxonomy_image_plugin_settings_page'
);
}
add_action( 'admin_menu', 'taxonomy_images_settings_menu' );
/**
* Settings Page Template.
*
* This function in conjunction with others usei the WordPress
* Settings API to create a settings page where users can adjust
* the behaviour of this plugin. Please see the following functions
* for more insight on the output generated by this function:
*
* taxonomy_image_plugin_control_taxonomies()
*
* @access private
* @since 0.7
*/
function taxonomy_image_plugin_settings_page() {
print "\n" . '<div class="wrap">';
screen_icon();
/* translators: Heading of the custom administration page. */
print "\n" . '<h2>' . esc_html__( 'Taxonomy Images Plugin Settings', 'taxonomy-images' ) . '</h2>';
print "\n" . '<div id="taxonomy-images">';
print "\n" . '<form action="options.php" method="post">';
settings_fields( 'taxonomy_image_plugin_settings' );
do_settings_sections( 'taxonomy_image_plugin_settings' );
/* translators: Button on the custom administration page. */
print "\n" . '<div class="button-holder"><input class="button-primary" name="submit" type="submit" value="' . esc_attr__( 'Save Changes', 'taxonomy-images' ) . '" /></div>';
print "\n" . '</div></form></div>';
}
/**
* Taxonomy Checklist.
*
* @access private
*/
function taxonomy_image_plugin_control_taxonomies() {
$settings = get_option( 'taxonomy_image_plugin_settings' );
$taxonomies = get_taxonomies( array(), 'objects' );
foreach ( (array) $taxonomies as $taxonomy ) {
if ( ! isset( $taxonomy->name ) )
continue;
if ( ! isset( $taxonomy->label ) )
continue;
if ( ! isset( $taxonomy->show_ui ) || empty( $taxonomy->show_ui ) )
continue;
$id = 'taxonomy-images-' . $taxonomy->name;
$checked = '';
if ( isset( $settings['taxonomies'] ) && in_array( $taxonomy->name, (array) $settings['taxonomies'] ) )
$checked = ' checked="checked"';
print "\n" . '<p><label for="' . esc_attr( $id ) . '">';
print '<input' . $checked . ' id="' . esc_attr( $id ) . '" type="checkbox" name="taxonomy_image_plugin_settings[taxonomies][]" value="' . esc_attr( $taxonomy->name ) . '">';
print ' ' . esc_html( $taxonomy->label ) . '</label></p>';
}
}
/**
* JSON Respose.
* Terminates script execution.
*
* @param array Associative array of values to be encoded in JSON.
*
* @access private
*/
function taxonomy_image_plugin_json_response( $args ) {
/* translators: An ajax request has failed for an unknown reason. */
$response = wp_parse_args( $args, array(
'status' => 'bad',
'why' => esc_html__( 'Unknown error encountered', 'taxonomy-images' )
) );
header( 'Content-type: application/jsonrequest' );
print json_encode( $response );
exit;
}
/**
* Get Term Info.
*
* Returns term info by term_taxonomy_id.
*
* @param int term_taxonomy_id
* @return array Keys: term_id (int) and taxonomy (string).
*
* @access private
*/
function taxonomy_image_plugin_get_term_info( $tt_id ) {
static $cache = array();
if ( isset( $cache[$tt_id] ) ) {
return $cache[$tt_id];
}
global $wpdb;
$data = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d LIMIT 1", $tt_id ) );
if ( isset( $data[0]->term_id ) )
$cache[$tt_id]['term_id'] = absint( $data[0]->term_id );
if ( isset( $data[0]->taxonomy ) )
$cache[$tt_id]['taxonomy'] = sanitize_title_with_dashes( $data[0]->taxonomy );
if ( isset( $cache[$tt_id] ) )
return $cache[$tt_id];
return array();
}
/**
* Check Taxonomy Permissions.
*
* Allows a permission check to be performed on a term
* when all you know is the term_taxonomy_id.
*
* @param int term_taxonomy_id
* @return bool True if user can edit terms, False if not.
*
* @access private
*/
function taxonomy_image_plugin_check_permissions( $tt_id ) {
$data = taxonomy_image_plugin_get_term_info( $tt_id );
if ( ! isset( $data['taxonomy'] ) )
return false;
$taxonomy = get_taxonomy( $data['taxonomy'] );
if ( ! isset( $taxonomy->cap->edit_terms ) )
return false;
return current_user_can( $taxonomy->cap->edit_terms );
}
/**
* Create an association.
*
* Callback for the wp_ajax_{$_GET['action']} hook.
*
* @access private
*/
function taxonomy_image_plugin_create_association() {
if ( ! isset( $_POST['tt_id'] ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'tt_id not sent', 'taxonomy-images' ),
) );
}
$tt_id = absint( $_POST['tt_id'] );
if ( empty( $tt_id ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'tt_id is empty', 'taxonomy-images' ),
) );
}
if ( ! taxonomy_image_plugin_check_permissions( $tt_id ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'You do not have the correct capability to manage this term', 'taxonomy-images' ),
) );
}
if ( ! isset( $_POST['wp_nonce'] ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'No nonce included.', 'taxonomy-images' ),
) );
}
if ( ! wp_verify_nonce( $_POST['wp_nonce'], 'taxonomy-image-plugin-create-association' ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'Nonce did not match', 'taxonomy-images' ),
) );
}
if ( ! isset( $_POST['attachment_id'] ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'Image id not sent', 'taxonomy-images' )
) );
}
$image_id = absint( $_POST['attachment_id'] );
if ( empty( $image_id ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'Image id is not a positive integer', 'taxonomy-images' )
) );
}
$assoc = taxonomy_image_plugin_get_associations();
$assoc[$tt_id] = $image_id;
if ( update_option( 'taxonomy_image_plugin', taxonomy_image_plugin_sanitize_associations( $assoc ) ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'good',
'why' => esc_html__( 'Image successfully associated', 'taxonomy-images' ),
'attachment_thumb_src' => taxonomy_image_plugin_get_image_src( $image_id )
) );
}
else {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'Association could not be created', 'taxonomy-images' )
) );
}
/* Don't know why, but something didn't work. */
taxonomy_image_plugin_json_response();
}
add_action( 'wp_ajax_taxonomy_image_create_association', 'taxonomy_image_plugin_create_association' );
/**
* Remove an association.
*
* Removes an association from the setting stored in the database.
* Print json encoded message and terminates script execution.
*
* @access private
*/
function taxonomy_image_plugin_remove_association() {
if ( ! isset( $_POST['tt_id'] ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'tt_id not sent', 'taxonomy-images' ),
) );
}
$tt_id = absint( $_POST['tt_id'] );
if ( empty( $tt_id ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'tt_id is empty', 'taxonomy-images' ),
) );
}
if ( ! taxonomy_image_plugin_check_permissions( $tt_id ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'You do not have the correct capability to manage this term', 'taxonomy-images' ),
) );
}
if ( ! isset( $_POST['wp_nonce'] ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'No nonce included', 'taxonomy-images' ),
) );
}
if ( ! wp_verify_nonce( $_POST['wp_nonce'], 'taxonomy-image-plugin-remove-association') ) {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'Nonce did not match', 'taxonomy-images' ),
) );
}
$assoc = taxonomy_image_plugin_get_associations();
if ( ! isset( $assoc[$tt_id] ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'good',
'why' => esc_html__( 'Nothing to remove', 'taxonomy-images' )
) );
}
unset( $assoc[$tt_id] );
if ( update_option( 'taxonomy_image_plugin', $assoc ) ) {
taxonomy_image_plugin_json_response( array(
'status' => 'good',
'why' => esc_html__( 'Association successfully removed', 'taxonomy-images' )
) );
}
else {
taxonomy_image_plugin_json_response( array(
'status' => 'bad',
'why' => esc_html__( 'Association could not be removed', 'taxonomy-images' )
) );
}
/* Don't know why, but something didn't work. */
taxonomy_image_plugin_json_response();
}
add_action( 'wp_ajax_taxonomy_image_plugin_remove_association', 'taxonomy_image_plugin_remove_association' );
/**
* Get a list of user-defined associations.
* Associations are stored in the WordPress options table.
*
* @param bool Should WordPress query the database for the results
* @return array List of associations. Key => taxonomy_term_id; Value => image_id
*
* @access private
*/
function taxonomy_image_plugin_get_associations( $refresh = false ) {
static $associations = array();
if ( empty( $associations ) || $refresh )
$associations = taxonomy_image_plugin_sanitize_associations( get_option( 'taxonomy_image_plugin' ) );
return $associations;
}
add_action( 'init', 'taxonomy_image_plugin_get_associations' );
/**
* Dynamically create hooks for each taxonomy.
*
* Adds hooks for each taxonomy that the user has given
* an image interface to via settings page. These hooks
* enable the image interface on wp-admin/edit-tags.php.
*
* @access private
* @since 0.4.3
* @alter 0.7
*/
function taxonomy_image_plugin_add_dynamic_hooks() {
$settings = get_option( 'taxonomy_image_plugin_settings' );
if ( ! isset( $settings['taxonomies'] ) )
return;
foreach ( $settings['taxonomies'] as $taxonomy ) {
add_filter( 'manage_' . $taxonomy . '_custom_column', 'taxonomy_image_plugin_taxonomy_rows', 15, 3 );
add_filter( 'manage_edit-' . $taxonomy . '_columns', 'taxonomy_image_plugin_taxonomy_columns' );
add_action( $taxonomy . '_edit_form_fields', 'taxonomy_image_plugin_edit_tag_form', 10, 2 );
}
}
add_action( 'admin_init', 'taxonomy_image_plugin_add_dynamic_hooks' );
/**
* Edit Term Columns.
*
* Insert a new column on wp-admin/edit-tags.php.
*
* @see taxonomy_image_plugin_add_dynamic_hooks()
*
* @param array A list of columns.
* @return array List of columns with "Images" inserted after the checkbox.
*
* @access private
* @since 0.4.3
*/
function taxonomy_image_plugin_taxonomy_columns( $original_columns ) {
$new_columns = $original_columns;
array_splice( $new_columns, 1 );
$new_columns['taxonomy_image_plugin'] = esc_html__( 'Image', 'taxonomy-images' );
return array_merge( $new_columns, $original_columns );
}
/**
* Edit Term Rows.
*
* Create image control for each term row of wp-admin/edit-tags.php.
*
* @see taxonomy_image_plugin_add_dynamic_hooks()
*
* @param string Row.
* @param string Name of the current column.
* @param int Term ID.
* @return string @see taxonomy_image_plugin_control_image()
*
* @access private
* @since 2010-11-08
*/
function taxonomy_image_plugin_taxonomy_rows( $row, $column_name, $term_id ) {
if ( 'taxonomy_image_plugin' === $column_name ) {
global $taxonomy;
return $row . taxonomy_image_plugin_control_image( $term_id, $taxonomy );
}
return $row;
}
/**
* Edit Term Control.
*
* Create image control for wp-admin/edit-tag-form.php.
* Hooked into the '{$taxonomy}_edit_form_fields' action.
*
* @param stdClass Term object.
* @param string Taxonomy slug.
*
* @access private
* @since 2010-11-08
*/
function taxonomy_image_plugin_edit_tag_form( $term, $taxonomy ) {
$taxonomy = get_taxonomy( $taxonomy );
$name = __( 'term', 'taxonomy-images' );
if ( isset( $taxonomy->labels->singular_name ) )
$name = strtolower( $taxonomy->labels->singular_name );
?>
<tr class="form-field hide-if-no-js">
<th scope="row" valign="top"><label for="description"><?php print esc_html__( 'Image', 'taxonomy-images' ) ?></label></th>
<td>
<?php print taxonomy_image_plugin_control_image( $term->term_id, $taxonomy->name ); ?>
<div class="clear"></div>
<span class="description"><?php printf( esc_html__( 'Associate an image from your media library to this %1$s.', 'taxonomy-images' ), esc_html( $name ) ); ?></span>
</td>
</tr>
<?php
}
/**
* Image Control.
*
* Creates all image controls on edit-tags.php.
*
* @todo Remove rel tag from link... will need to adjust js to accomodate.
* @since 0.7
* @access private
*/
function taxonomy_image_plugin_control_image( $term_id, $taxonomy ) {
$term = get_term( $term_id, $taxonomy );
$tt_id = 0;
if ( isset( $term->term_taxonomy_id ) )
$tt_id = (int) $term->term_taxonomy_id;
$taxonomy = get_taxonomy( $taxonomy );
$name = esc_html__( 'term', 'taxonomy-images' );
if ( isset( $taxonomy->labels->singular_name ) )
$name = strtolower( $taxonomy->labels->singular_name );
$hide = ' hide';
$attachment_id = 0;
$associations = taxonomy_image_plugin_get_associations();
if ( isset( $associations[ $tt_id ] ) ) {
$attachment_id = (int) $associations[ $tt_id ];
$hide = '';
}
$img = taxonomy_image_plugin_get_image_src( $attachment_id );
$term = get_term( $term_id, $taxonomy->name );
$o = "\n" . '<div id="' . esc_attr( 'taxonomy-image-control-' . $tt_id ) . '" class="taxonomy-image-control hide-if-no-js">';
$o.= "\n" . '<a class="thickbox taxonomy-image-thumbnail" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=library&post_id=0&TB_iframe=true' ) . '" title="' . esc_attr( sprintf( __( 'Associate an image with the %1$s named “%2$s”.', 'taxonomy-images' ), $name, $term->name ) ) . '"><img id="' . esc_attr( 'taxonomy_image_plugin_' . $tt_id ) . '" src="' . esc_url( $img ) . '" alt="" /></a>';
$o.= "\n" . '<a class="control upload thickbox" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=type&post_id=0&TB_iframe=true' ) . '" title="' . esc_attr( sprintf( __( 'Upload a new image for this %s.', 'taxonomy-images' ), $name ) ) . '">' . esc_html__( 'Upload.', 'taxonomy-images' ) . '</a>';
$o.= "\n" . '<a class="control remove' . $hide . '" href="#" id="' . esc_attr( 'remove-' . $tt_id ) . '" rel="' . esc_attr( $tt_id ) . '" title="' . esc_attr( sprintf( __( 'Remove image from this %s.', 'taxonomy-images' ), $name ) ) . '">' . esc_html__( 'Delete', 'taxonomy-images' ) . '</a>';
$o.= "\n" . '<input type="hidden" class="tt_id" name="' . esc_attr( 'tt_id-' . $tt_id ) . '" value="' . esc_attr( $tt_id ) . '" />';
$o.= "\n" . '<input type="hidden" class="image_id" name="' . esc_attr( 'image_id-' . $tt_id ) . '" value="' . esc_attr( $attachment_id ) . '" />';
if ( isset( $term->name ) && isset( $term->slug ) )
$o.= "\n" . '<input type="hidden" class="term_name" name="' . esc_attr( 'term_name-' . $term->slug ) . '" value="' . esc_attr( $term->name ) . '" />';
$o.= "\n" . '</div>';
return $o;
}
/**
* Custom javascript for modal media box.
*
* This script need to be added to all instance of the media upload box.
*
* @access private
*/
function taxonomy_image_plugin_media_upload_popup_js() {
wp_enqueue_script(
'taxonomy-images-media-upload-popup',
taxonomy_image_plugin_url( 'media-upload-popup.js' ),
array( 'jquery' ),
taxonomy_image_plugin_version()
);
wp_localize_script( 'taxonomy-images-media-upload-popup', 'TaxonomyImagesModal', array (
'termBefore' => esc_html__( '“', 'taxonomy-images' ),
'termAfter' => esc_html__( '”', 'taxonomy-images' ),
'associating' => esc_html__( 'Associating …', 'taxonomy-images' ),
'success' => esc_html__( 'Successfully Associated', 'taxonomy-images' ),
'removing' => esc_html__( 'Removing …', 'taxonomy-images' ),
'removed' => esc_html__( 'Successfully Removed', 'taxonomy-images' )
) );
}
add_action( 'admin_print_scripts-media-upload-popup', 'taxonomy_image_plugin_media_upload_popup_js' );
/**
* Custom javascript for wp-admin/edit-tags.php.
*
* @access private
*/
function taxonomy_image_plugin_edit_tags_js() {
if ( false == taxonomy_image_plugin_is_screen_active() )
return;
wp_enqueue_script(
'taxonomy-image-plugin-edit-tags',
taxonomy_image_plugin_url( 'edit-tags.js' ),
array( 'jquery', 'thickbox' ),
taxonomy_image_plugin_version()
);
wp_localize_script( 'taxonomy-image-plugin-edit-tags', 'taxonomyImagesPlugin', array (
'nonce' => wp_create_nonce( 'taxonomy-image-plugin-remove-association' ),
'img_src' => taxonomy_image_plugin_url( 'default.png' ),
'tt_id' => 0,
'image_id' => 0,
) );
}
add_action( 'admin_print_scripts-edit-tags.php', 'taxonomy_image_plugin_edit_tags_js' );
/**
* Custom styles.
*
* @since 0.7
* @access private
*/
function taxonomy_image_plugin_css_admin() {
if ( false == taxonomy_image_plugin_is_screen_active() && 'admin_print_styles-media-upload-popup' != current_filter() )
return;
wp_enqueue_style(
'taxonomy-image-plugin-edit-tags',
taxonomy_image_plugin_url( 'admin.css' ),
array(),
taxonomy_image_plugin_version(),
'screen'
);
}
add_action( 'admin_print_styles-edit-tags.php', 'taxonomy_image_plugin_css_admin' );
add_action( 'admin_print_styles-media-upload-popup', 'taxonomy_image_plugin_css_admin' );
/**
* Thickbox styles.
*
* @since 0.7
* @access private
*/
function taxonomy_image_plugin_css_thickbox() {
if ( false == taxonomy_image_plugin_is_screen_active() )
return;
wp_enqueue_style( 'thickbox' );
}
add_action( 'admin_print_styles-edit-tags.php', 'taxonomy_image_plugin_css_thickbox' );
/**
* Public Styles.
*
* Prints custom css to all public pages. If you do not
* wish to have these styles included for you, please
* insert the following code into your theme's functions.php
* file:
*
* add_filter( 'taxonomy-images-disable-public-css', '__return_true' );
*
* @since 0.7
* @access private
*/
function taxonomy_image_plugin_css_public() {
if ( apply_filters( 'taxonomy-images-disable-public-css', false ) )
return;
wp_enqueue_style(
'taxonomy-image-plugin-public',
taxonomy_image_plugin_url( 'style.css' ),
array(),
taxonomy_image_plugin_version(),
'screen'
);
}
add_action( 'wp_print_styles', 'taxonomy_image_plugin_css_public' );
/**
* Activation.
*
* Two entries in the options table will created when this
* plugin is activated in the event that they do not exist.
*
* 'taxonomy_image_plugin' (array) A flat list of all assocaitions
* made by this plugin. Keys are integers representing the
* term_taxonomy_id of terms. Values are integers representing the
* ID property of an image attachment.
*
* 'taxonomy_image_plugin_settings' (array) A multi-dimensional array
* of user-defined settings. As of version 0.7, only one key is used:
* 'taxonomies' which is a whitelist of registered taxonomies having ui
* that support the custom image ui provided by this plugin.
*
* @access private
* @alter 0.7
*/
function taxonomy_image_plugin_activate() {
$associations = get_option( 'taxonomy_image_plugin' );
if ( false === $associations )
add_option( 'taxonomy_image_plugin', array() );
$settings = get_option( 'taxonomy_image_plugin_settings' );
if ( false === $settings ) {
add_option( 'taxonomy_image_plugin_settings', array(
'taxonomies' => array()
) );
}
}
register_activation_hook( __FILE__, 'taxonomy_image_plugin_activate' );
/**
* Is Screen Active?
*
* @return bool
*
* @access private
* @since 0.7
*/
function taxonomy_image_plugin_is_screen_active() {
$screen = get_current_screen();
if ( ! isset( $screen->taxonomy ) )
return false;
$settings = get_option( 'taxonomy_image_plugin_settings' );
if ( ! isset( $settings['taxonomies'] ) )
return false;
if ( in_array( $screen->taxonomy, $settings['taxonomies'] ) )
return true;
return false;
}
/**
* Cache Images
*
* Sets the WordPress object cache for all term images
* associated to the posts in the provided array. This
* function has been created to minimize queries when
* using this plugins get_the_terms() style function.
*
* @param array Post objects.
*