-
Notifications
You must be signed in to change notification settings - Fork 953
Expand file tree
/
Copy pathfeatured-image.js
More file actions
209 lines (174 loc) · 5.57 KB
/
featured-image.js
File metadata and controls
209 lines (174 loc) · 5.57 KB
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
/* global wp */
/* global YoastSEO */
/* jshint -W097 */
/* jshint -W003 */
import { select, subscribe } from "@wordpress/data";
import a11ySpeak from "a11y-speak";
import isBlockEditor from "../helpers/isBlockEditor";
import { __ } from "@wordpress/i18n";
/**
* @summary Initializes the featured image integration.
* @param {object} $ jQuery
* @returns {void}
*/
export default function initFeaturedImageIntegration( $ ) {
var featuredImagePlugin;
var $featuredImageElement;
var $postImageDiv;
var $postImageDivHeading;
/**
* A content analysis plugin for the featured image.
*
* @param {object} app The Yoast SEO app.
*
* @constructor
*/
var FeaturedImagePlugin = function( app ) {
this._app = app;
this.featuredImage = null;
this.pluginName = "addFeaturedImagePlugin";
this.registerPlugin();
this.registerModifications();
};
/**
* Sets the featured image to use in the analysis
*
* @param {String} featuredImage The featured image to use.
*
* @returns {void}
*/
FeaturedImagePlugin.prototype.setFeaturedImage = function( featuredImage ) {
this.featuredImage = featuredImage;
this._app.pluginReloaded( this.pluginName );
};
/**
* Removes featured image and reloads analyzer
*
* @returns {void}
*/
FeaturedImagePlugin.prototype.removeFeaturedImage = function() {
this.setFeaturedImage( null );
};
/**
* Registers this plugin to YoastSEO
*
* @returns {void}
*/
FeaturedImagePlugin.prototype.registerPlugin = function() {
this._app.registerPlugin( this.pluginName, { status: "ready" } );
};
/**
* Registers modifications to YoastSEO
*
* @returns {void}
*/
FeaturedImagePlugin.prototype.registerModifications = function() {
this._app.registerModification( "content", this.addImageToContent.bind( this ), this.pluginName, 10 );
};
/**
* Adds featured image to sort so it can be analyzed
*
* @param {String} content The content to alter.
*
* @returns {String} Returns the possible altered content.
*/
FeaturedImagePlugin.prototype.addImageToContent = function( content ) {
if ( null !== this.featuredImage ) {
content += this.featuredImage;
}
return content;
};
/**
* Remove opengraph warning frame and borders
*
* @returns {void}
*/
function removeOpengraphWarning() {
$( "#yst_opengraph_image_warning" ).remove();
$postImageDiv.removeClass( "yoast-opengraph-image-notice" );
}
/**
* Check if image is smaller than 200x200 pixels. If this is the case, show a warning
*
* @param {object} featuredImage The featured image object.
*
* @returns {void}
*/
function checkFeaturedImage( featuredImage ) {
var attachment = featuredImage.state().get( "selection" ).first().toJSON();
const featuredImageNotice = __( "SEO issue: The featured image should be at least 200 by 200 pixels to be picked up by Facebook and other social media sites.", "wordpress-seo" );
if ( attachment.width < 200 || attachment.height < 200 ) {
// Show warning to user and do not add image to OG
if ( 0 === $( "#yst_opengraph_image_warning" ).length ) {
// Create a warning using native WordPress notices styling.
$( '<div id="yst_opengraph_image_warning" class="notice notice-error notice-alt"><p>' +
featuredImageNotice +
"</p></div>" )
.insertAfter( $postImageDivHeading );
$postImageDiv.addClass( "yoast-opengraph-image-notice" );
a11ySpeak( featuredImageNotice, "assertive" );
}
} else {
// Force reset warning
removeOpengraphWarning();
}
}
/**
* Returns whether the featured image ID is a valid media ID.
*
* @param {*} featuredImageId The candidate featured image ID.
* @returns {boolean} Whether the given ID is a valid ID.
*/
function isValidMediaId( featuredImageId ) {
return typeof featuredImageId === "number" && featuredImageId > 0;
}
var featuredImage = wp.media.featuredImage.frame();
if ( typeof YoastSEO === "undefined" ) {
return;
}
featuredImagePlugin = new FeaturedImagePlugin( YoastSEO.app );
$postImageDiv = $( "#postimagediv" );
$postImageDivHeading = $postImageDiv.find( ".hndle" );
featuredImage.on( "select", function() {
var selectedImageHTML, selectedImage, alt;
checkFeaturedImage( featuredImage );
selectedImage = featuredImage.state().get( "selection" ).first();
alt = selectedImage.get( "alt" );
selectedImageHTML = "<img" +
' src="' + selectedImage.get( "url" ) + '"' +
' width="' + selectedImage.get( "width" ) + '"' +
' height="' + selectedImage.get( "height" ) + '"' +
' alt="' + alt +
'"/>';
featuredImagePlugin.setFeaturedImage( selectedImageHTML );
} );
$postImageDiv.on( "click", "#remove-post-thumbnail", function() {
featuredImagePlugin.removeFeaturedImage();
removeOpengraphWarning();
} );
$featuredImageElement = $( "#set-post-thumbnail > img" );
if ( "undefined" !== typeof $featuredImageElement.prop( "src" ) ) {
featuredImagePlugin.setFeaturedImage( $( "#set-post-thumbnail " ).html() );
}
// Fallback for Gutenberg, as the featured image id does not exist there.
if ( ! isBlockEditor() ) {
return;
}
let imageData;
let previousImageData;
subscribe( () => {
const featuredImageId = select( "core/editor" ).getEditedPostAttribute( "featured_media" );
if ( ! isValidMediaId( featuredImageId ) ) {
return;
}
imageData = select( "core" ).getMedia( featuredImageId );
if ( typeof imageData === "undefined" ) {
return;
}
if ( imageData !== previousImageData ) {
previousImageData = imageData;
const featuredImageHTML = `<img src="${imageData.source_url}" alt="${imageData.alt_text}" >`;
featuredImagePlugin.setFeaturedImage( featuredImageHTML );
}
} );
}