-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrequire-featured-image-on-edit.js
95 lines (81 loc) · 2.94 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
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
jQuery(document).ready(function($) {
function isGutenberg() {
return ($('.block-editor-writing-flow').length > 0);
}
function checkImageReturnWarningMessageOrEmpty() {
if (isGutenberg()) {
var $img = $('.editor-post-featured-image').find('img');
} else {
var $img = $('#postimagediv').find('img');
}
if ($img.length === 0) {
return passedFromServer.jsWarningHtml;
}
if (featuredImageIsTooSmall()) {
return passedFromServer.jsSmallHtml;
}
return '';
}
// Contains three test "failures" at page load
var isTooSmallTrials = [ true, true, true ];
function featuredImageIsTooSmall() {
// A weird polling issue in Chrome made this necessary
if (isGutenberg()) {
var $img = $('.editor-post-featured-image').find('img');
} else {
var $img = $('#postimagediv').find('img');
}
// pop one off, if needed
if( isTooSmallTrials.length > 2 ) {
isTooSmallTrials.shift();
}
isTooSmallTrials.push( passedImageIsTooSmall($img) );
var imageIsTooSmallCount = isTooSmallTrials.reduce(function (a, b) {
return a + b;
}, 0);
return (imageIsTooSmallCount > 2);
}
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>');
if (isGutenberg()) {
$('.editor-post-publish-panel__toggle').attr('disabled', 'disabled');
} else {
$('#publish').attr('disabled','disabled');
}
}
function clearWarningAndEnablePublish() {
$('#nofeature-message').remove();
if (isGutenberg()) {
$('.editor-post-publish-panel__toggle').removeAttr('disabled');
} else {
$('#publish').removeAttr('disabled');
}
}
function createMessageAreaIfNeeded() {
if ($('body').find("#nofeature-message").length === 0) {
if (isGutenberg()) {
$('.components-notice-list').append('<div id="nofeature-message"></div>');
} else {
$('#post').before('<div id="nofeature-message"></div>');
}
}
}
function detectWarnFeaturedImage() {
if (checkImageReturnWarningMessageOrEmpty()) {
disablePublishAndWarn(checkImageReturnWarningMessageOrEmpty());
} else {
clearWarningAndEnablePublish();
}
}
detectWarnFeaturedImage();
setInterval(detectWarnFeaturedImage, 800);
});