forked from Yoast/wordpress-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-seo-dashboard-widget.js
192 lines (164 loc) · 4.78 KB
/
wp-seo-dashboard-widget.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
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
/* global wpseoDashboardWidgetL10n, wpseoApi, jQuery */
import React from "react";
import ReactDOM from "react-dom";
import SeoAssessment from "yoast-components/composites/Plugin/DashboardWidget/components/SeoAssessment";
import ScoreAssessments from "yoast-components/composites/Plugin/Shared/components/ScoreAssessments";
import getFeed from "yoast-components/utils/getFeed";
import WordpressFeed from "yoast-components/composites/Plugin/DashboardWidget/components/WordpressFeed";
import colors from "yoast-components/style-guide/colors.json";
class DashboardWidget extends React.Component {
/**
* Creates the components and initializes its state.
*/
constructor() {
super();
this.state = {
statistics: null,
ryte: null,
feed: null,
};
this.getStatistics();
this.getRyte();
this.getFeed();
}
/**
* Returns a color to be used for a given score.
*
* @param {string} score The score, expected to be 'na', 'bad', 'ok', 'good'.
*
* @returns {string} The color to use for this score. Defaults to grey if no such color exists.
*/
static getColorFromScore( score ) {
return colors[ `$color_${ score }` ] || colors.$color_grey;
}
/**
* Fetches data from the statistics endpoint, parses it and sets it to the state.
*
* @returns {void}
*/
getStatistics() {
wpseoApi.get( "statistics", ( response ) => {
let statistics = {};
statistics.seoScores = response.seo_scores.map( ( score ) => ( {
value: parseInt( score.count, 10 ),
color: DashboardWidget.getColorFromScore( score.seo_rank ),
html: `<a href="${ score.link }">${ score.label }</a>`,
} ) );
// Wrap in a div and get the text to HTML decode.
statistics.header = jQuery( `<div>${ response.header }</div>` ).text();
this.setState( { statistics } );
} );
}
/**
* Fetches data from the Ryte endpoint, parses it and sets it to the state.
*
* @returns {void}
*/
getRyte() {
wpseoApi.get( "ryte", ( response ) => {
if ( ! response.ryte ) {
return;
}
let ryte = {
scores: [ {
color: DashboardWidget.getColorFromScore( response.ryte.score ),
html: response.ryte.label,
} ],
canFetch: response.ryte.can_fetch,
};
this.setState( { ryte } );
} );
}
/**
* Fetches data from the yoast.com feed, parses it and sets it to the state.
*
* @returns {void}
*/
getFeed() {
getFeed( "https://yoast.com/feed/widget/", 2 )
.then( ( feed ) => {
feed.items = feed.items.map( ( item ) => {
item.description = jQuery( `<div>${ item.description }</div>` ).text();
item.description = item.description.replace( `The post ${ item.title } appeared first on Yoast.`, "" ).trim();
item.content = jQuery( `<div>${ item.content }</div>` ).text();
return item;
} );
this.setState( { feed } );
} )
.catch( error => console.log( error ) );
}
/**
* Returns the SEO Assessment sub-component.
*
* @returns {ReactElement} The SEO Assessment component.
*/
getSeoAssessment() {
if ( this.state.statistics === null ) {
return null;
}
return <SeoAssessment key="yoast-seo-posts-assessment"
seoAssessmentText={ this.state.statistics.header }
seoAssessmentItems={ this.state.statistics.seoScores }/>;
}
/**
* Returns the Ryte Assessment sub-component.
*
* @returns {ReactElement} The Ryte Assessment component.
*/
getRyteAssessment() {
if ( this.state.ryte === null ) {
return null;
}
return (
<div id="yoast-seo-ryte-assessment" key="yoast-seo-ryte-assessment">
<h3>{ wpseoDashboardWidgetL10n.ryte_header }</h3>
<ScoreAssessments items={ this.state.ryte.scores }/>
<div>
{ this.state.ryte.canFetch &&
<a className="fetch-status button" href={ wpseoDashboardWidgetL10n.ryte_fetch_url }>
{ wpseoDashboardWidgetL10n.ryte_fetch }
</a>
}
<a className="landing-page button" href={ wpseoDashboardWidgetL10n.ryte_landing_url } target="_blank">
{ wpseoDashboardWidgetL10n.ryte_analyze }
</a>
</div>
</div>
);
}
/**
* Returns the yoast.com feed sub-component.
*
* @returns {ReactElement} The yoast.com feed component.
*/
getYoastFeed() {
if ( this.state.feed === null ) {
return null;
}
return <WordpressFeed
key="yoast-seo-blog-feed"
title={ wpseoDashboardWidgetL10n.feed_header }
feed={ this.state.feed }
footerHtml={ wpseoDashboardWidgetL10n.feed_footer } />;
}
/**
* Renders the component.
*
* @returns {ReactElement} The component.
*/
render() {
let contents = [
this.getSeoAssessment(),
this.getRyteAssessment(),
this.getYoastFeed(),
].filter( item => item !== null );
if ( contents.length === 0 ) {
return null;
}
return <div>{ contents }</div>;
}
}
const element = document.getElementById( "yoast-seo-dashboard-widget" );
if( element ) {
ReactDOM.render( <DashboardWidget/>, element );
}