-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediatags_admin.php
850 lines (714 loc) · 29 KB
/
mediatags_admin.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
<?php
function mediatags_admin_init()
{
global $mediatags, $wp_version;
add_action( 'admin_head', 'mediatags_admin_head_proc' );
add_action( 'admin_footer', 'mediatags_admin_footer' );
add_action( 'plugins_loaded', 'mediatag_thirdparty_support' );
add_action( 'wp_ajax_media_tags_bulk_action', 'media_tags_bulk_action_callback' );
add_action( 'delete_attachment', 'mediatags_delete_attachment_proc' );
// http://wordpress.org/support/topic/plugin-media-tags-row-count-fix?replies=3
add_action('parent_file', 'mediatags_edit_tags_fixes');
// Add dropdowns above Media > Library listing
add_action( 'restrict_manage_posts', 'mediatags_filter_posts' );
add_action( 'wp_ajax_get_mediatags_ajax', 'mediatags_get_mediatags_ajax' );
// These hook into the Media Upload popup tabs
add_filter( 'media_upload_tabs', 'mediatag_upload_tab' );
add_action( 'media_upload_mediatags', 'media_upload_mediatags' );
// Handle Export/Import interaction
add_action('export_wp', 'mediatags_wp_export_metadata');
add_action('import_post_meta', 'mediatags_wp_import_metadata', 10, 3);
add_action( 'add_meta_boxes', 'mediatags_metaboxes' );
$mediatag_admin_bulk_library = get_option('mediatag_admin_bulk_library', 'yes');
$mediatag_admin_bulk_inline = get_option('mediatag_admin_bulk_inline', 'yes');
if ($mediatag_admin_bulk_inline == "yes")
{
add_filter('media_upload_gallery', 'media_upload_gallery_tab', 10, 1); // The Gallery Tab
add_filter('media_upload_library', 'media_upload_gallery_tab', 10, 1); // The Media Library Tab
}
// If we are viewing the Media > Library page. This needs hooks to display the jQuery-UI popup for the Bulk admin
// if enabled via the Media-Tags > Settings page.
if (mediataga_check_url('wp-admin/upload.php'))
{
wp_enqueue_style( 'mediatags-stylesheet', plugins_url('/css/mediatags_style_admin.css', __FILE__),
false, $mediatags->plugin_version);
if (($mediatag_admin_bulk_library == "yes") && (current_user_can( MEDIATAGS_ASSIGN_TERMS_CAP )))
{
if (MEDIA_TAGS_POPUP == 'JQUERY-UI')
{
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-dialog');
wp_enqueue_style( 'mediatags-jquery-ui',
plugins_url('/js/jquery-ui/css/flick/jquery-ui-1.7.3.custom.css', __FILE__),
array('mediatags-stylesheet'), $mediatags->plugin_version );
}
wp_enqueue_script('mediatags-bulk-common', plugins_url('/js/mediatags_bulk_common.js', __FILE__),
array('jquery', 'jquery-ui-core', 'jquery-ui-dialog'), $mediatags->plugin_version);
wp_enqueue_script('mediatags-bulk-library', plugins_url('/js/mediatags_bulk_library.js', __FILE__),
array('jquery', 'mediatags-bulk-common'), $mediatags->plugin_version);
wp_enqueue_script('mediatags', plugins_url('/js/mediatags.js', __FILE__),
array('jquery'), $mediatags->plugin_version);
}
}
// Else If we are viewing the Media popup via the Post/Page editor. if enabled via the Media-Tags > Settings page.
else if (mediataga_check_url('wp-admin/media-upload.php'))
{
wp_enqueue_style( 'mediatags-stylesheet', plugins_url('/css/mediatags_style_admin.css', __FILE__),
false, $mediatags->plugin_version);
if (($mediatag_admin_bulk_inline == "yes") && (current_user_can( MEDIATAGS_ASSIGN_TERMS_CAP ))
&& ((isset($_GET['tab'])) && (($_GET['tab'] == "gallery") || ($_GET['tab'] == "library"))) )
{
wp_enqueue_script('mediatags-bulk-common', plugins_url('/js/mediatags_bulk_common.js', __FILE__),
array('jquery'), $mediatags->plugin_version);
wp_enqueue_script('mediatags-bulk-inline', plugins_url('/js/mediatags_bulk_inline.js', __FILE__),
array('jquery', 'mediatags-bulk-common'), $mediatags->plugin_version);
wp_enqueue_script('mediatags', plugins_url('/js/mediatags.js', __FILE__),
array('jquery'), $mediatags->plugin_version);
}
}
else if (mediataga_check_url('wp-admin/admin.php'))
{
if ((isset($_GET['page']))
&& ( ($_GET['page'] == "mediatags_settings_panel")
|| ($_GET['page'] == "mediatags_roles_panel")
|| ($_GET['page'] == "mediatags_help_panel")
|| ($_GET['page'] == "mediatags_thirdparty_panel") ))
{
wp_enqueue_style( 'mediatags-stylesheet', plugins_url('/css/mediatags_style_admin.css', __FILE__),
false, $mediatags->plugin_version);
wp_enqueue_script('mediatags', plugins_url('/js/mediatags.js', __FILE__),
array('jquery'), $mediatags->plugin_version);
}
}
else if ((mediataga_check_url('wp-admin/media.php')) || mediataga_check_url('wp-admin/post.php'))
{
wp_enqueue_style( 'mediatags-stylesheet', plugins_url('/css/mediatags_style_admin.css', __FILE__),
false, $mediatags->plugin_version);
wp_enqueue_script('mediatags', plugins_url('/js/mediatags.js', __FILE__),
array('jquery'), $mediatags->plugin_version);
}
if (function_exists('mediatags_settings_api_init'))
mediatags_settings_api_init();
add_filter( 'manage_upload_columns', 'mediatags_library_column_header' );
add_filter( 'manage_edit-media-tags_columns', 'mediatags_terms_column_header' );
add_action( 'manage_media_custom_column', 'mediatags_library_column_row', 10, 2 );
add_filter( 'manage_media-tags_custom_column', 'mediatags_terms_column_row', 10, 3 );
add_filter( 'manage_upload_sortable_columns', 'mediatags_admin_media_sort_columns' );
add_filter( 'manage_edit-media-tags_sortable_columns', 'mediatags_admin_terms_sort_columns' );
add_filter( 'get_terms_args', 'mediatags_admin_terms_args_filter', 10, 2);
}
function mediataga_check_url($url='')
{
if (!$url) return;
$_REQUEST_URI = explode('?', $_SERVER['REQUEST_URI']);
$url_len = strlen($url);
$url_offset = $url_len * -1;
// If out test string ($url) is longer than the page URL. skip
if (strlen($_REQUEST_URI[0]) < $url_len) return;
if ($url == substr($_REQUEST_URI[0], $url_offset, $url_len))
return true;
}
function mediatags_admin_media_sort_columns($cols)
{
$cols[MEDIA_TAGS_TAXONOMY] = MEDIA_TAGS_TAXONOMY;
return $cols;
}
function mediatags_admin_terms_sort_columns($cols)
{
$cols['mediatags_used'] = 'mediatags_used';
return $cols;
}
function mediatags_wp_head()
{
add_mediatags_alternate_link();
}
function mediatags_admin_head_proc()
{
// All header output moved to the admin_init function.
}
function mediatags_admin_footer()
{
global $mediatags, $wp_version;
if (function_exists('get_current_screen'))
$current_screen = get_current_screen();
else
{
global $current_screen;
}
if ((isset($current_screen->id))
&& (($current_screen->id == "upload") || ($current_screen->id == "media-upload")) )
{
$mediatag_admin_bulk_library = get_option('mediatag_admin_bulk_library', 'yes');
$mediatag_admin_bulk_inline = get_option('mediatag_admin_bulk_inline', 'yes');
if ($mediatag_admin_bulk_library == "yes")
{
mediatags_bulk_admin_panel();
?>
<div id="media-tags-bulk-selection-error" title="Media Tags Selection Error" style="display:none"><?php echo __('<p>You must first select which Media Items to change.</p><p>Please close this dialog window and make your selection.</p>', MEDIA_TAGS_I18N_DOMAIN); ?>
</div>
<?php show_mediataga_admin_buttons_text(); ?>
<script type='text/javascript'>
jQuery(document).ready(function() {
jQuery('form#posts-filter select[name=action]').append('<option value="media-tags"><?php
echo _x('Media-Tags', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></option>');
jQuery('form#posts-filter select[name=action2]').append('<option value="media-tags"><?php
echo _x('Media-Tags', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></option>');
jQuery('select#media-tags').change(function(){
var media_tags_filter = jQuery(this).val();
if (media_tags_filter !== "")
{
if (jQuery('form.search-form input#media-tags-search').length)
{
jQuery('form.search-form input#media-tags-search').val(media_tags_filter);
}
else
{
jQuery('form.search-form').append('<input type="hidden" id="media-tags-search" name="media-tags" value="'+media_tags_filter+'"/>');
}
}
else
{
jQuery('form.search-form input#media-tags-search').remove();
}
});
<?php
if ((isset($_GET['media-tags'])) && (strlen($_GET['media-tags'])))
{
?>
jQuery('form.search-form').append('<input type="hidden" id="media-tags-search" name="media-tags" value="<?php echo esc_attr( $_GET['media-tags'] ); ?>"/>');
<?php } ?>
});
</script>
<?php
}
}
}
function show_mediataga_admin_buttons_text()
{
// My way of setting some text that can be pulled in via JS. This is much better than hard-coded inside the JS code. Plus I can provided language support for these different buttons labels.
?>
<div id="media-tags-bulk-content-buttons" style="display: none">
<div class="close"><?php echo _x('Close', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></div>
<div class="submit"><?php echo _x('Submit', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></div>
<div class="cancel"><?php echo _x('Cancel', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></div>
<div class="media-tags"><?php echo _x('Media-Tags', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></div>
<div class="show"><?php echo _x('Show', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></div>
<div class="hide"><?php echo _x('Hide', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></div>
<div class="all"><?php echo _x('All Media-Tags', 'bulk admin label', MEDIA_TAGS_I18N_DOMAIN); ?></div>
</div>
<?php
}
function mediatags_admin_panels()
{
// Adds the 'Media-Tags' submenu option under the Media nav
add_media_page( _x("Media-Tags", 'menu label', MEDIA_TAGS_I18N_DOMAIN),
_x("Media-Tags", 'page label', MEDIA_TAGS_I18N_DOMAIN),
MEDIATAGS_MANAGE_TERMS_CAP,
"edit-tags.php?taxonomy=media-tags&post_type=attachment" );
// Adds the 'Media-Tags' top-level menu panel
add_menu_page( _x("Media-Tags Settings", 'page label', MEDIA_TAGS_I18N_DOMAIN),
_x("Media-Tags", 'menu label', MEDIA_TAGS_I18N_DOMAIN),
MEDIATAGS_SETTINGS_CAP,
'mediatags_settings_panel',
'mediatags_settings_panel');
add_submenu_page( 'mediatags_settings_panel',
_x('Media-Tags Settings', 'page label', MEDIA_TAGS_I18N_DOMAIN),
_x('Settings', 'menu label', 'menu label', MEDIA_TAGS_I18N_DOMAIN),
MEDIATAGS_SETTINGS_CAP,
'mediatags_settings_panel',
'mediatags_settings_panel');
$mediatag_use_roles = get_option('mediatag_use_roles', 'yes');
if (!$mediatag_use_roles)
$mediatag_use_roles = "yes";
if ($mediatag_use_roles == 'yes') {
add_submenu_page( 'mediatags_settings_panel',
_x('Roles Management','page label', MEDIA_TAGS_I18N_DOMAIN),
_x('Roles Management', 'menu label', MEDIA_TAGS_I18N_DOMAIN),
MEDIATAGS_MANAGE_ROLE_CAP,
'mediatags_roles_panel',
'mediatags_roles_panel');
}
$mediatag_use_thirdparty = get_option('mediatag_use_thirdparty', 'yes');
if (!$mediatag_use_thirdparty)
$mediatag_use_thirdparty = "yes";
if ($mediatag_use_thirdparty == 'yes') {
add_submenu_page( 'mediatags_settings_panel',
_x('Third Party Settings','page label', MEDIA_TAGS_I18N_DOMAIN),
_x('Third Party Settings', 'menu label', MEDIA_TAGS_I18N_DOMAIN),
MEDIATAGS_SETTINGS_CAP,
'mediatags_thirdparty_panel',
'mediatags_thirdparty_panel');
}
add_submenu_page( 'mediatags_settings_panel',
_x('Help', 'page label', MEDIA_TAGS_I18N_DOMAIN),
_x('Help', 'menu label', MEDIA_TAGS_I18N_DOMAIN),
MEDIATAGS_SETTINGS_CAP,
'mediatags_help_panel',
'mediatags_help_panel');
}
function mediatags_add_default_capabilities()
{
$role = get_role('contributor');
if ($role)
$role->add_cap(MEDIATAGS_ASSIGN_TERMS_CAP);
$role = get_role('author');
if ($role)
$role->add_cap(MEDIATAGS_ASSIGN_TERMS_CAP);
$role = get_role('editor');
if ($role) {
$role->add_cap(MEDIATAGS_MANAGE_TERMS_CAP);
$role->add_cap(MEDIATAGS_ASSIGN_TERMS_CAP);
$role->add_cap(MEDIATAGS_EDIT_TERMS_CAP);
$role->add_cap(MEDIATAGS_DELETE_TERMS_CAP);
}
$role = get_role('administrator');
if ($role) {
$role->add_cap(MEDIATAGS_SETTINGS_CAP);
$role->add_cap(MEDIATAGS_MANAGE_TERMS_CAP);
$role->add_cap(MEDIATAGS_ASSIGN_TERMS_CAP);
$role->add_cap(MEDIATAGS_EDIT_TERMS_CAP);
$role->add_cap(MEDIATAGS_DELETE_TERMS_CAP);
$role->add_cap(MEDIATAGS_MANAGE_ROLE_CAP);
}
}
function mediatags_filter_posts()
{
// First need to check the screen. We only want to show the Media-Tags dropdown on the Media > Library page
if (function_exists('get_current_screen'))
$current_screen = get_current_screen();
else
{
global $current_screen;
}
if ((isset($current_screen->id)) && ($current_screen->id == "upload"))
{
mediatags_show_library_select(MEDIA_TAGS_TAXONOMY, '', MEDIA_TAGS_TAXONOMY, __('All Media-Tags'));
}
}
// Called from mediatags_filter_posts to display and handle the actual Media-tags dropdown.
function mediatags_show_library_select($term_slug='', $select_label='', $select_key='', $select_default_option='')
{
if (!$term_slug) return;
if (strlen($select_label) == 0)
$select_label = $term_slug;
if (strlen($select_key) == 0)
$select_key = $term_slug;
if (isset($_REQUEST[$term_slug]))
$filter_term = $_REQUEST[$term_slug];
else
$filter_term = "";
$posttype_terms = (array) get_terms( $term_slug, array('get' => 'all') );
if ($posttype_terms)
{
?>
<select id="<?php echo esc_attr( $select_key ); ?>" name="<?php echo esc_attr( $select_key ); ?>">
<option selected="selected" value=""><?php echo esc_attr( $select_default_option ); ?></option><?php
foreach($posttype_terms as $_idx => $_item)
{
$is_selected = "";
if ($filter_term === $_item->slug)
$is_selected = ' selected="selected" ';
?><option value="<?php echo esc_attr( $_item->slug ); ?>" <?php echo $is_selected; ?>><?php echo esc_attr( $_item->name ); ?></option><?php
}
?>
</select>
<?php
}
}
// Builds the Media > Library column cell which displays the Media-Tags used for the specific Media item
function mediatags_terms_column_row( $something, $column_name, $term_id )
{
$row_content = "";
if ($column_name == "mediatags_used")
{
$media_tag = get_term( $term_id, MEDIA_TAGS_TAXONOMY );
if ($media_tag)
{
if ($media_tag->count > 0)
{
$row_content = '<a href="'.
get_mediatag_admin_library_link( $term_id). '">'. $media_tag->count. '</a>';
}
else
{
$row_content = $media_tag->count;
}
}
}
return $row_content;
}
// Build the output displayed when a user edits a Media item
function mediatags_show_fields_to_edit($form_fields, $post)
{
if (current_user_can( MEDIATAGS_ASSIGN_TERMS_CAP ))
{
if (mediataga_check_url('/wp-admin/async-upload.php'))
$post_media_tags_fields = mediatags_get_fields($post->ID, true);
else
$post_media_tags_fields = mediatags_get_fields($post->ID, false);
if (strlen($post_media_tags_fields))
$post_media_tags_fields = "<br />". __('Enter media tags in the space above. Enter multiple tags
separated with comma. Or select from the tag(s) below', MEDIA_TAGS_I18N_DOMAIN) . $post_media_tags_fields;
else
$post_media_tags_fields = "<br />". __('Enter media tags in the space above.
Enter multiple tags separated with comma.', MEDIA_TAGS_I18N_DOMAIN);
$form_fields['media-meta'] = array(
'label' => __('Media-Tags:', MEDIA_TAGS_I18N_DOMAIN),
'input' => 'html',
'html' => "<input type='text' name='attachments[$post->ID][media_tags_input]'
id='attachments[$post->ID][media_tags_input]'
size='50' value='' />
$post_media_tags_fields "
);
}
return $form_fields;
}
function mediatags_get_fields($post_id, $force_load_tags = false)
{
if (!is_object(get_post($post_id))) return '';
$master_media_tag_fields = "";
$post_media_tags = mediatags_get_post_mediatags($post_id);
$used_tags_array = array();
foreach ($post_media_tags as $tslug => $ttag) {
$used_tags_array[] = '<li><input type="checkbox" id="label-'.$post_id.'-'.$tslug.'" '
.'name="attachments['.$post_id.'][media_tags_checkbox]['.$tslug.']" checked="checked" />'
.'<label for="label-'.$post_id.'-'.$tslug.'">'.__($ttag->name).'</label></li>';
}
if ($force_load_tags == true)
$click_event = 'onclick="return false"';
else
$click_event = '';
if (count($used_tags_array))
$master_media_tag_fields .= '<a id="media-tags-used-'. $post_id .'" '. $click_event .' class="media-tags-show-hide-used"
post-id="'.$post_id.'" href="#">Media Tags for this attachment</a>'
.'<div id="media-tags-list-used-'. $post_id .'" class="media-tags-list-used"><ul
class="media-tags-list">'.implode('', $used_tags_array).'</ul></div>';
$master_media_tag_fields .= '<a id="media-tags-common-'. $post_id .'" '. $click_event .' class="media-tags-show-hide-common"
post-id="'.$post_id.'" href="#">Show Common Media Tags</a>'
.'<div id="media-tags-list-common-'. $post_id .'" class="media-tags-list-common"><ul
class="media-tags-list">';
if ($force_load_tags == true)
{
$master_media_tag_fields .= mediatags_load_post_mediatags_type($post_id, 'common');
}
$master_media_tag_fields .= '</ul></div>';
$master_media_tag_fields .= '<a id="media-tags-uncommon-'.$post_id.'" '. $click_event .' class="media-tags-show-hide-uncommon"
post-id="'.$post_id.'" href="#">Show Uncommon Media Tags</a>'
.'<div id="media-tags-list-uncommon-'. $post_id .'" class="media-tags-list-uncommon"><ul
class="media-tags-list">';
if ($force_load_tags == true)
{
$master_media_tag_fields .= mediatags_load_post_mediatags_type($post_id, 'uncommon');
}
$master_media_tag_fields .= '</ul></div>';
return $master_media_tag_fields;
}
function mediatags_get_mediatags_ajax()
{
if (!isset($_POST['post_id']) || empty($_POST['post_id'])) return '';
$post_id = $_POST['post_id'];
if (!is_object(get_post($post_id))) return '';
if (!isset($_POST['mediatags_type']) || empty($_POST['mediatags_type'])) return '';
$mediatags_type = $_POST['mediatags_type'];
die(mediatags_load_post_mediatags_type($post_id, $mediatags_type));
}
function mediatags_load_post_mediatags_type($post_id, $mediatags_type)
{
$post_media_tags = mediatags_get_post_mediatags($post_id);
$master_list = mediatags_load_master();
$meditags_items = array();
if ( (is_array($master_list)) && (count($master_list)) )
{
foreach ($master_list as $master_slug => $master_tag)
{
// If the Media-Tag term is already assocated with the post item. Skip it.
if (in_array($master_slug, array_keys($post_media_tags))) continue;
if ($mediatags_type == "common")
{
if ($master_tag->count > 0)
{
$meditags_items[] = '<li><input type="checkbox" id="label-'. $post_id .'-'. $master_slug .'" '
.'name="attachments['. $post_id .'][media_tags_checkbox]['. $master_slug .']" />'
.'<label for="label-'. $post_id .'-'. $master_slug .'">'. __($master_tag->name) .'</label>';
}
}
else if ($mediatags_type == "uncommon")
{
if ($master_tag->count == 0)
{
$meditags_items[] = '<li><input type="checkbox" id="label-'. $post_id .'-'. $master_slug .'" '
.'name="attachments['. $post_id .'][media_tags_checkbox]['. $master_slug .']" />'
.'<label for="label-'. $post_id .'-'. $master_slug .'">'. __($master_tag->name) .'</label>';
}
}
}
}
if (count($meditags_items))
return implode('', $meditags_items);
else
{
if ($mediatags_type == "common")
return 'No Common Media-Tags found.';
else if ($mediatags_type == "uncommon")
return 'No Uncommon Media-Tags found.';
}
}
function meditags_process_attachment_fields_to_save($post, $attachment)
{
/*
$media_tags_action = "assign";
$select_media_tags = array();
if (isset($attachment['media_tags_checkbox']))
{
foreach($attachment['media_tags_checkbox'] as $tag_idx => $tag_val)
{
$select_media_tags[] = $tag_idx;
}
}
if (isset($attachment['media_tags_input']))
$media_tags_input = $attachment['media_tags_input'];
else
$media_tags_input = "";
$select_media_items = array();
$select_media_items[] = $post['ID'];
// mediatag_process_admin_forms($media_tags_action, $select_media_items, $select_media_tags, $media_tags_input);
*/
$taxonomy = get_taxonomy( MEDIA_TAGS_TAXONOMY );
if (!current_user_can($taxonomy->cap->assign_terms))
return $post;
$media_tags_array = array();
if (isset($attachment['media_tags_checkbox']))
{
foreach($attachment['media_tags_checkbox'] as $tag_idx => $tag_val)
{
$media_tags_array[] = $tag_idx;
}
}
if (strlen($attachment['media_tags_input']))
{
$tags_tmp_array = explode(',', $attachment['media_tags_input']);
if ($tags_tmp_array)
{
foreach($tags_tmp_array as $idx => $tag_val)
{
//$tag_slug = sanitize_title_with_dashes($tag_val);
$tag_slug = sanitize_title($tag_val);
if ( ! ($id = term_exists( $tag_slug, MEDIA_TAGS_TAXONOMY ) ) )
wp_insert_term($tag_val, MEDIA_TAGS_TAXONOMY, array('slug' => $tag_slug));
$media_tags_array[] = $tag_slug;
}
}
}
$media_tags_slugs = array();
foreach($media_tags_array as $media_tags_item)
$media_tags_slugs[$media_tags_item] = $media_tags_item;
if ($media_tags_array)
{
$media_tags_slugs = array();
foreach($media_tags_array as $media_tags_item)
$media_tags_slugs[$media_tags_item] = sprintf("%s", $media_tags_item) ;
wp_set_object_terms($post['ID'], $media_tags_slugs, MEDIA_TAGS_TAXONOMY);
}
else
{
wp_set_object_terms($post['ID'], "", MEDIA_TAGS_TAXONOMY);
}
return $post;
}
function mediatags_load_master()
{
$media_tags_tmp = (array) get_terms(MEDIA_TAGS_TAXONOMY, 'hide_empty=0');
if ($media_tags_tmp)
{
$master_media_tags_array = array();
foreach($media_tags_tmp as $m_media_tag)
{
$master_media_tags_array[$m_media_tag->slug] = $m_media_tag;
}
return $master_media_tags_array;
}
}
function mediatags_delete_attachment_proc($postid = '')
{
if (!$postid) return;
wp_delete_object_term_relationships($postid, array(MEDIA_TAGS_TAXONOMY));
}
function media_upload_gallery_tab($tab_content='')
{
mediatags_bulk_admin_panel();
show_mediataga_admin_buttons_text();
}
function mediatag_upload_tab($tabs='')
{
$tabs['mediatags'] = _x('Media Tags', 'tab label', MEDIA_TAGS_I18N_DOMAIN);
return $tabs;
}
function media_upload_mediatags()
{
if ( isset($_POST['send']) ) {
// Return it to TinyMCE
return media_send_to_editor();
}
$errors = null;
return wp_iframe( 'media_upload_mediatags_form', $errors );
}
function media_upload_mediatags_form()
{
global $type;
media_upload_header();
$post_id = intval($_REQUEST['post_id']);
if ((isset($_REQUEST['type'])) && (strlen($_REQUEST['type'])))
$type = "type=". esc_attr( $_REQUEST['type'] )."&";
$form_action_url = "media-upload.php?".$type."tab=library&tab=library&post_id=". esc_attr( $post_id );
?>
<div style="clear:both"></div>
<?php $mediatag_items = get_mediatags(); ?>
<form action="">
<div id="media-items">
<?php
if ($mediatag_items)
{
foreach($mediatag_items as $mediatag_item)
{
?>
<div id="mediatag-item-<?php echo esc_attr( $mediatag_item->term_id ); ?>" class="media-item">
<div class="filename" style="display: block; float: left; width: 70%"><?php
echo esc_attr( $mediatag_item->name ); ?></div>
<div class="mediatag-item-count"
style="display: block; float: right; width: 10%; line-height:36px;overflow:hidden;padding:0 10px;">
<?php
$mediatags_library_link = MEDIA_TAGS_TAXONOMY ."=". $mediatag_item->slug;
$mediatag_count = ( $mediatag_item->count > 0 ) ? '<a href="'.
$form_action_url .'&'. $mediatags_library_link .'">'. $mediatag_item->count .'</a>' : '0';
echo esc_attr( $mediatag_count );
?>
</div>
</div>
<?php
}
}
?>
</div>
</form>
<?php
}
/* Adds column header to the Media Library panel where we show the linked Media-Tags per row */
function mediatags_library_column_header( $cols ) {
$cols[MEDIA_TAGS_TAXONOMY] = _x('Media Tags', 'column name', MEDIA_TAGS_I18N_DOMAIN);
return $cols;
}
function mediatags_terms_column_header($cols)
{
if (isset($cols['posts']))
unset($cols['posts']);
$cols['mediatags_used'] = _x('Media Used', 'column name', MEDIA_TAGS_I18N_DOMAIN);
return $cols;
}
function mediatags_admin_terms_args_filter($args, $taxonomies)
{
if (((isset($_REQUEST['action'])) && ($_REQUEST['action'] == "fetch-list"))
&& ((isset($_REQUEST['taxonomy'])) && ($_REQUEST['taxonomy'] == MEDIA_TAGS_TAXONOMY)))
{
if ((isset($args['orderby'])) && ($args['orderby'] == "mediatags_used"))
$args['orderby'] = "count";
}
return $args;
}
/* Adds the row content, link Media-Tags, for each Library item row */
function mediatags_library_column_row( $column_name, $id ) {
if ( $column_name == MEDIA_TAGS_TAXONOMY )
{
if (function_exists('wp_get_object_terms'))
$media_attachments = wp_get_object_terms( $id, MEDIA_TAGS_TAXONOMY );
else
$media_attachments = get_the_terms( $id, MEDIA_TAGS_TAXONOMY );
if ($media_attachments)
{
$media_tag_list_items = "";
foreach($media_attachments as $media_idx => $media_attachment)
{
if (strlen($media_tag_list_items)) $media_tag_list_items .= ", ";
$media_tag_list_items .= '<a href="'.
esc_url( get_mediatag_admin_library_link( $media_attachment->term_id) ). '">'. esc_attr( $media_attachment->name ) . '</a>';
}
echo $media_tag_list_items;
}
}
}
function get_mediatag_admin_edit_link( $mediatag_id ) {
$base_url = get_option('siteurl')."/wp-admin/upload.php?page=" . esc_attr( MEDIA_TAGS_ADMIN_MENU_KEY );
$media_tag = &get_term( $mediatag_id, MEDIA_TAGS_TAXONOMY );
if ( is_wp_error( $media_tag ) )
return $media_tag;
$edit_href = $base_url ."&action=editmediatag&mediatag_ID=". esc_attr( $mediatag_id );
return esc_url( $edit_href );
}
function get_mediatag_admin_search_link( $mediatag_id ) {
$base_url = get_option('siteurl')."/wp-admin/upload.php?page=". MEDIA_TAGS_ADMIN_MENU_KEY;
$media_tag = &get_term( $mediatag_id, MEDIA_TAGS_TAXONOMY );
if ( is_wp_error( $media_tag ) )
return $media_tag;
$edit_href = $base_url ."&action=searchmediatag&s=".$media_tag->slug;
return esc_url( $edit_href );
}
function get_mediatag_admin_library_link( $mediatag_id ) {
$base_url = "upload.php?";
$media_tag = get_term( $mediatag_id, MEDIA_TAGS_TAXONOMY );
if ( is_wp_error( $media_tag ) )
return;
$edit_href = $base_url . MEDIA_TAGS_TAXONOMY ."=".$media_tag->slug;
return esc_url( $edit_href );
}
function mediatags_get_taxonomy_labels()
{
$labels_default = array(
'name' => _x( 'Media-Tags', 'taxonomy general name', MEDIA_TAGS_I18N_DOMAIN ),
'singular_name' => _x( 'Media-Tag', 'taxonomy singular name', MEDIA_TAGS_I18N_DOMAIN ),
'search_items' => _x( 'Search Media-Tags', 'taxonomy search items', MEDIA_TAGS_I18N_DOMAIN ),
'popular_items' => _x( 'Popular Media-Tags', 'taxonomy popular item', MEDIA_TAGS_I18N_DOMAIN),
'all_items' => _x( 'All Media-Tags', 'taxonomy all items', MEDIA_TAGS_I18N_DOMAIN ),
'parent_item' => _x( 'Parent Media-Tag', 'taxonomy parent item', MEDIA_TAGS_I18N_DOMAIN ),
'parent_item_colon' => _x( 'Parent Media-Tag:', 'taxonomy parent item colon', MEDIA_TAGS_I18N_DOMAIN ),
'edit_item' => _x( 'Edit Media-Tag', 'taxonomy edit item', MEDIA_TAGS_I18N_DOMAIN ),
'update_item' => _x( 'Update Media-Tag', 'taxonomy update item', MEDIA_TAGS_I18N_DOMAIN ),
'add_new_item' => _x( 'Add New Media-Tag', 'taxonomy add new item', MEDIA_TAGS_I18N_DOMAIN ),
'new_item_name' => _x( 'New Media-Tag Name', 'taxonomy new item name', MEDIA_TAGS_I18N_DOMAIN ),
);
$mediatag_labels = get_option('mediatags_labels');
if ($mediatag_labels)
return array_merge($labels_default, $mediatag_labels);
else
return $labels_default;
}
// http://wordpress.org/support/topic/plugin-media-tags-row-count-fix?replies=3
function mediatags_edit_tags_fixes($parent_file)
{
if ( 'edit.php?post_type=attachment' == $parent_file )
{
// back-reference to Media menu, to fix proper side-menu focus
$parent_file = 'upload.php';
// Give 'Show on screen' form other names, to avoid conflict with some WP filters (e.g. do not use - )
add_screen_option( 'per_page', array('label' => 'Media-Tags', 'default' => 20,
'option' => 'edit_mediatags_per_page',
'value' => 'edit_mediatags_per_page') );
// Fix for 'Show on screen' form processing, to make applied rows work
// (code extracted from set_screen_options(), as I don't know yet how to hook this)
if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) )
{
check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
if ( !$user = wp_get_current_user() ) return;
$option = $_POST['wp_screen_options']['option'];
$value = $_POST['wp_screen_options']['value'];
// persist applied values in wp_usermeta table
update_user_meta($user->ID, $option, $value);
// redirect to set cookie
wp_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
}
return $parent_file;
}
}
function mediatags_metaboxes() {
add_meta_box('tagsdiv-' . MEDIA_TAGS_TAXONOMY,
__( 'Media-Tags', MEDIA_TAGS_I18N_DOMAIN ), 'post_tags_meta_box', 'attachment', 'side', 'core', array( 'taxonomy' => MEDIA_TAGS_TAXONOMY ));
}