Geen omschrijving

admin-thumbnail.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * This function:
  3. *
  4. * Generates a custom image uploader / selector tied to a post where the click action originated
  5. * Upon clicking "Use as thumbnail" the image selected is set to be the post thumbnail
  6. * A thumbnail image is then shown in the All Posts / All Pages / All Custom Post types Admin Dashboard view
  7. *
  8. * @since 1.0.0
  9. *
  10. * global ajaxurl, fiat_thumb
  11. */
  12. (function($){
  13. //uploading files variable
  14. var thumbnail_upload_frame;
  15. // inspired from:
  16. // mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/
  17. jQuery(document).on('click', '.fiat_thickbox', function(event) {
  18. event.preventDefault();
  19. var that = this.href;
  20. var thumb_id = this.dataset.thumbnailId;
  21. // get post id to assign thumbnail to
  22. var post_id = FIATparseUrl(that).params.post_id;
  23. //If the frame already exists, reopen it
  24. if (typeof(thumbnail_upload_frame)!=="undefined") {
  25. thumbnail_upload_frame.close();
  26. }
  27. // Set the title and expected images to use in the dialog
  28. thumbnail_upload_frame = wp.media.frames.customHeader = wp.media({
  29. //Title of media manager frame
  30. title: "Featured Image Admin Thumb",
  31. library: {
  32. type: 'image'
  33. },
  34. button: {
  35. //Button text
  36. text: fiat_thumb.button_text
  37. },
  38. states: [
  39. new wp.media.controller.Library({
  40. library: wp.media.query({ type: 'image' }),
  41. multiple: false, // do not allow multiple files, if you want multiple, set true
  42. filterable: 'all' // turn on filters
  43. })
  44. ]
  45. });
  46. // Set the post id we would like the thumbnail assigned to
  47. wp.media.model.settings.post.id = post_id;
  48. thumbnail_upload_frame.on('open', function() {
  49. var selection = thumbnail_upload_frame.state().get('selection');
  50. var selected_thumb = wp.media.attachment(thumb_id);
  51. selected_thumb.fetch();
  52. selection.add([selected_thumb]);
  53. });
  54. //callback for selected image when the "Use as thumbnail" is clicked
  55. thumbnail_upload_frame.on('select', function() {
  56. var attachment = thumbnail_upload_frame.state().get('selection').first().toJSON();
  57. var nonce = FIATparseUrl(that).params._wpnonce;
  58. // Use WP AJAX function to set the selected image as post thumbnail.
  59. jQuery.post ( ajaxurl, {
  60. action: 'set-post-thumbnail',
  61. post_id: post_id,
  62. thumbnail_id: attachment.id,
  63. _ajax_nonce: nonce,
  64. cookie: encodeURIComponent( document.cookie )
  65. }).done( function( html ) {
  66. // Inject thumbnail image onto related post in All Posts/All Pages view
  67. jQuery.post( ajaxurl, {
  68. action: 'fiat_get_thumbnail',
  69. thumbnail_id: attachment.id,
  70. post_id: post_id,
  71. _ajax_nonce: nonce
  72. }).done ( function( thumb_url ) {
  73. // This is the column location to place the img
  74. var pre_html = '<a title="' + fiat_thumb.change_featured_image + '" href="' + '/wp-admin/media-upload.php?post_id=' + post_id + '&amp;type=image&amp;TB_iframe=1&_wpnonce=' + nonce + '" class="fiat_thickbox" >';
  75. var post_html = '</a>';
  76. $( '.thumb', '#post-' + post_id ).html( pre_html + thumb_url + post_html );
  77. $( '.thumb', '#post-' + post_id ).hide().fadeIn();
  78. })
  79. });
  80. });
  81. //Open custom image modal
  82. thumbnail_upload_frame.open();
  83. });
  84. /**
  85. * Parse supplied url and return parameters found as object
  86. *
  87. * @param url
  88. * @returns {{params}}
  89. * @constructor
  90. */
  91. function FIATparseUrl( url ) {
  92. // props: http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
  93. var a = document.createElement('a');
  94. a.href = url;
  95. return {
  96. params: (function(){
  97. var ret = {},
  98. seg = a.search.replace(/^\?/,'').split('&'),
  99. len = seg.length, i = 0, s;
  100. for (;i<len;i++) {
  101. if (!seg[i]) { continue; }
  102. s = seg[i].split('=');
  103. ret[s[0]] = s[1];
  104. }
  105. return ret;
  106. })()
  107. }
  108. }
  109. }(jQuery));