| 1 |
jQuery(document).ready(function ($) { |
| 2 |
//Refresh the profile image thumbnail |
| 3 |
function mt_ajax_thumbnail_refresh() { |
| 4 |
var post_id = jQuery("#metronet_post_id").val(); |
| 5 |
jQuery("#metronet-profile-image").html( |
| 6 |
'<img class="mpp-loading" alt="Loading" width="150" height="150" src="' + |
| 7 |
metronet_profile_image.loading_gif + |
| 8 |
'" />', |
| 9 |
); |
| 10 |
$.post( |
| 11 |
metronet_profile_image.ajax_url, |
| 12 |
{ |
| 13 |
action: "metronet_get_thumbnail", |
| 14 |
post_id: post_id, |
| 15 |
_wpnonce: metronet_profile_image.nonce, |
| 16 |
user_id: jQuery("#metronet_profile_id").val(), |
| 17 |
}, |
| 18 |
function (response) { |
| 19 |
jQuery("#metronet-profile-image").html( |
| 20 |
mt_display_block(response.thumb_html), |
| 21 |
); |
| 22 |
jQuery(".user-profile-picture img ").replaceWith( |
| 23 |
response.avatar, |
| 24 |
); |
| 25 |
if (response.user_id === response.logged_in_user_id) { |
| 26 |
jQuery( |
| 27 |
"#wp-admin-bar-my-account img.avatar-26", |
| 28 |
).replaceWith(response.avatar_admin_small); |
| 29 |
jQuery( |
| 30 |
"#wp-admin-bar-my-account img.avatar-64", |
| 31 |
).replaceWith(response.avatar_admin_medium); |
| 32 |
} |
| 33 |
}, |
| 34 |
"json", |
| 35 |
); |
| 36 |
} |
| 37 |
//Remove the profile image |
| 38 |
function mt_remove_profile_image() { |
| 39 |
jQuery("#metronet-profile-image").html( |
| 40 |
'<img class="mpp-loading" alt="Loading" width="150" height="150" src="' + |
| 41 |
metronet_profile_image.loading_gif + |
| 42 |
'" />', |
| 43 |
); |
| 44 |
$.post( |
| 45 |
metronet_profile_image.ajax_url, |
| 46 |
{ |
| 47 |
action: "metronet_remove_thumbnail", |
| 48 |
post_id: metronet_profile_image.user_post_id, |
| 49 |
user_id: jQuery("#metronet_profile_id").val(), |
| 50 |
_wpnonce: metronet_profile_image.nonce, |
| 51 |
}, |
| 52 |
function (response) { |
| 53 |
jQuery("#metronet-profile-image").html( |
| 54 |
mt_display_block(response.thumb_html), |
| 55 |
); |
| 56 |
jQuery(".user-profile-picture img ").replaceWith( |
| 57 |
response.avatar, |
| 58 |
); |
| 59 |
if (response.user_id === response.logged_in_user_id) { |
| 60 |
jQuery( |
| 61 |
"#wp-admin-bar-my-account img.avatar-26", |
| 62 |
).replaceWith(response.avatar_admin_small); |
| 63 |
jQuery( |
| 64 |
"#wp-admin-bar-my-account img.avatar-64", |
| 65 |
).replaceWith(response.avatar_admin_medium); |
| 66 |
} |
| 67 |
}, |
| 68 |
"json", |
| 69 |
); |
| 70 |
} |
| 71 |
// Set thumbnail img and wrapping a to display:block to fix visual bug |
| 72 |
function mt_display_block(htmlString) { |
| 73 |
var temp = document.createElement("div"); |
| 74 |
temp.innerHTML = htmlString; |
| 75 |
temp.firstElementChild.style.display = "block"; |
| 76 |
temp.firstElementChild.firstElementChild.style.display = "block"; |
| 77 |
return temp.innerHTML; |
| 78 |
} |
| 79 |
|
| 80 |
$("#mpp").on("click", ".mpp_add_media", function (e) { |
| 81 |
//Assign the default view for the media uploader |
| 82 |
|
| 83 |
var uploader = wp.media({ |
| 84 |
state: "featured-image", |
| 85 |
states: [new wp.media.controller.FeaturedImage()], |
| 86 |
title: metronet_profile_image.set_profile_text, |
| 87 |
button: { |
| 88 |
text: metronet_profile_image.remove_profile_text, |
| 89 |
}, |
| 90 |
multiple: false, |
| 91 |
}); |
| 92 |
|
| 93 |
// CUSTOM TOOLBAR ON BOTTOM OF MEDIA MANAGER. SETS UP THE TWO ACTION BUTTONS |
| 94 |
|
| 95 |
uploader.on( |
| 96 |
"toolbar:create", |
| 97 |
function (toolbar) { |
| 98 |
var options = {}; |
| 99 |
options.items = {}; |
| 100 |
options.items.select = { |
| 101 |
text: metronet_profile_image.set_profile_text, |
| 102 |
style: "primary", |
| 103 |
click: wp.media.view.Toolbar.Select.prototype.clickSelect, |
| 104 |
requires: { selection: true }, |
| 105 |
event: "select", |
| 106 |
reset: false, |
| 107 |
close: true, |
| 108 |
state: false, |
| 109 |
syncSelection: true, |
| 110 |
}; |
| 111 |
if (!$("#metronet-profile-image a").hasClass("default-image")) { |
| 112 |
options.items.remove = { |
| 113 |
text: metronet_profile_image.remove_profile_text, |
| 114 |
style: "secondary", |
| 115 |
requires: { selection: false }, |
| 116 |
click: wp.media.view.Toolbar.Select.prototype |
| 117 |
.clickSelect, |
| 118 |
event: "remove", |
| 119 |
reset: true, |
| 120 |
close: true, |
| 121 |
state: false, |
| 122 |
syncSelection: true, |
| 123 |
}; |
| 124 |
} |
| 125 |
this.createSelectToolbar(toolbar, options); |
| 126 |
}, |
| 127 |
uploader, |
| 128 |
); |
| 129 |
|
| 130 |
//For when the featured thumbnail is set |
| 131 |
uploader.mt_featured_set = function (id) { |
| 132 |
jQuery("#metronet-profile-image").html( |
| 133 |
'<img class="mpp-loading" alt="Loading" width="150" height="150" src="' + |
| 134 |
metronet_profile_image.loading_gif + |
| 135 |
'" />', |
| 136 |
); |
| 137 |
$.post( |
| 138 |
metronet_profile_image.ajax_url, |
| 139 |
{ |
| 140 |
action: "metronet_add_thumbnail", |
| 141 |
post_id: metronet_profile_image.user_post_id, |
| 142 |
user_id: jQuery("#metronet_profile_id").val(), |
| 143 |
thumbnail_id: id, |
| 144 |
_wpnonce: metronet_profile_image.nonce, |
| 145 |
}, |
| 146 |
function (response) { |
| 147 |
jQuery("#metronet-profile-image").html( |
| 148 |
mt_display_block(response.thumb_html), |
| 149 |
); |
| 150 |
jQuery(".user-profile-picture img ").replaceWith( |
| 151 |
response.avatar, |
| 152 |
); |
| 153 |
if (response.user_id === response.logged_in_user_id) { |
| 154 |
jQuery( |
| 155 |
"#wp-admin-bar-my-account img.avatar-26", |
| 156 |
).replaceWith(response.avatar_admin_small); |
| 157 |
jQuery( |
| 158 |
"#wp-admin-bar-my-account img.avatar-64", |
| 159 |
).replaceWith(response.avatar_admin_medium); |
| 160 |
} |
| 161 |
}, |
| 162 |
"json", |
| 163 |
); |
| 164 |
}; |
| 165 |
|
| 166 |
//For when the Add Profile Image is clicked |
| 167 |
uploader.on("select", function () { |
| 168 |
var featured = uploader.state().get("selection").single(); |
| 169 |
wp.media.featuredImage.set(featured ? featured.id : -1); |
| 170 |
if (!featured.id) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
uploader.mt_featured_set(featured.id); |
| 175 |
}); |
| 176 |
|
| 177 |
//When the remove buttons is clicked |
| 178 |
uploader.on("remove", function () { |
| 179 |
wp.media.featuredImage.set(-1); |
| 180 |
mt_remove_profile_image(); |
| 181 |
}); |
| 182 |
|
| 183 |
//For when the window is closed (update the thumbnail) |
| 184 |
uploader.on("escape", function () { |
| 185 |
mt_ajax_thumbnail_refresh(); |
| 186 |
}); |
| 187 |
|
| 188 |
//Open the media uploader |
| 189 |
uploader.open(); |
| 190 |
return false; |
| 191 |
}); |
| 192 |
$("#mpp").on("click", "a#metronet-remove", function (e) { |
| 193 |
e.preventDefault(); |
| 194 |
wp.media.featuredImage.set(-1); |
| 195 |
mt_remove_profile_image(); |
| 196 |
}); |
| 197 |
}); |
| 198 |
|