forked from pressupinc/require-featured-image
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequire-featured-image-on-edit.js
51 lines (43 loc) · 1.36 KB
/
require-featured-image-on-edit.js
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
jQuery(document).ready(function($) {
function checkImageReturnWarningMessageOrEmpty() {
var $img = $('#postimagediv').find('img');
if ($img.length === 0) {
return passedFromServer.jsWarningHtml;
}
if (passedImageIsTooSmall($img)) {
return passedFromServer.jsSmallHtml;
}
return '';
}
function passedImageIsTooSmall($img) {
var input = $img[0].src;
var pathToImage = input.replace(/-\d+[Xx]\d+\./g, ".");
var featuredImage = new Image();
featuredImage.src = pathToImage;
return featuredImage.width < passedFromServer.width || featuredImage.height < passedFromServer.height;
}
function disablePublishAndWarn(message) {
createMessageAreaIfNeeded();
$('#nofeature-message').addClass("error")
.html('<p>'+message+'</p>');
$('#publish').attr('disabled','disabled');
}
function clearWarningAndEnablePublish() {
$('#nofeature-message').remove();
$('#publish').removeAttr('disabled');
}
function createMessageAreaIfNeeded() {
if ($('body').find("#nofeature-message").length === 0) {
$('#post').before('<div id="nofeature-message"></div>');
}
}
function detectWarnFeaturedImage() {
if (checkImageReturnWarningMessageOrEmpty()) {
disablePublishAndWarn(checkImageReturnWarningMessageOrEmpty());
} else {
clearWarningAndEnablePublish();
}
}
detectWarnFeaturedImage();
setInterval(detectWarnFeaturedImage, 800);
});