-
Notifications
You must be signed in to change notification settings - Fork 3
/
wpdotorg-embed.php
316 lines (230 loc) · 9.49 KB
/
wpdotorg-embed.php
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
Plugin Name: WP.org Embed
Plugin URI: https://www.ademti-software.co.uk/
Description: Paste the URL to a WordPress.org plugin into your posts or pages, and have the plugin information pulled in and displayed automatically
Version: 1.4
Author: Ademti Software
Author URI: https://www.ademti-software.co.uk/
*/
/**
* Copyright (c) 2013-2015 Lee Willis. All rights reserved.
* Copyright (c) 2015-2024 Ademti Software. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
/**
* This class handles being the oEmbed provider in terms of registering the URLs that
* we can embed, and handling the actual oEmbed calls. It relies on the WP.org API
* class to retrieve the information from WordPress.org
* @uses class wpdotorg_api
*/
class wpdotorg_embed {
private $api;
/**
* Constructor. Registers hooks and filters
* @param class $api An instance of the wpdotorg_api classs
*/
public function __construct( $api ) {
$this->api = $api;
add_action( 'init', array( $this, 'register_oembed_handler' ) );
add_action( 'init', array( $this, 'maybe_handle_oembed' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_action( 'admin_init', array( $this, 'schedule_expiry' ) );
add_action( 'wpdotorg_embed_cron', array( $this, 'cron' ) );
// @TODO i18n
}
/**
* Make sure we have a scheduled event set to clear down the oEmbed cache until
* WordPress supports cache_age in oEmbed responses.
*/
function schedule_expiry() {
if ( ! wp_next_scheduled( 'wpdotorg_embed_cron' ) ) {
$frequency = apply_filters( 'wpdotorg_embed_cache_frequency', 'daily' );
wp_schedule_event( time(), $frequency, 'wpdotorg_embed_cron' );
}
}
/**
* Expire old oEmbeds.
* Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it
* daily. Ideally WP should honour cache_age in oEmbed responses properly
*/
function cron() {
global $wpdb, $table_prefix;
$sql = "DELETE
FROM {$table_prefix}postmeta
WHERE meta_key LIKE '_oembed_%'";
$results = $wpdb->get_results( $sql );
}
/**
* Enqueue the frontend CSS
* @return void
*/
function enqueue_styles() {
wp_register_style( 'wpdotorg-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/wpdotorg-embed.css' ) );
wp_enqueue_style( 'wpdotorg-embed' );
}
/**
* Register the oEmbed provider, and point it at a local endpoint since wpdotorg
* doesn't directly support oEmbed yet. Our local endpoint will use the wpdotorg
* API to fulfil the request.
* @param array $providers The current list of providers
* @return array The list, with our new provider added
*/
public function register_oembed_handler() {
$oembed_url = home_url();
$key = $this->get_key();
$oembed_url = add_query_arg( array( 'wpdotorg_oembed' => $key ), $oembed_url );
wp_oembed_add_provider( '#https?://wordpress.org/extend/plugins/.*/?#i', $oembed_url, true );
wp_oembed_add_provider( '#https?://wordpress.org/extend/themes/.*/?#i', $oembed_url, true );
wp_oembed_add_provider( '#https?://wordpress.org/plugins/.*/?#i', $oembed_url, true );
wp_oembed_add_provider( '#https?://wordpress.org/themes/.*/?#i', $oembed_url, true );
}
/**
* Generate a unique key that can be used on our requests to stop others
* hijacking our internal oEmbed API
* @return string The site key
*/
private function get_key() {
$key = get_option( 'wpdotorg_oembed_key' );
if ( ! $key ) {
$key = md5( time() . rand( 0,65535 ) );
add_option( 'wpdotorg_oembed_key', $key, '', 'yes' );
}
return $key;
}
/**
* Check whether this is an oembed request, handle if it is.
* Ignore it if not.
* Insert rant here about WP's lack of a front-end AJAX handler.
*/
public function maybe_handle_oembed() {
if ( isset( $_GET['wpdotorg_oembed'] ) ) {
return $this->handle_oembed();
}
}
/**
* Handle an oembed request
*/
public function handle_oembed() {
// Check this request is valid
if ( $_GET['wpdotorg_oembed'] != $this->get_key() ) {
header( 'HTTP/1.0 403 Forbidden' );
die( 'Matt Mullenweg is sad.' );
}
// Check we have the required information
$url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
$format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
if ( ! empty( $format ) && $format != 'json' ) {
header( 'HTTP/1.0 501 Not implemented' );
die( 'Only JSON here, probably #blamenacin' );
}
if ( preg_match( '#https?://wordpress.org/extend/plugins/([^/]*)/?$#i', $url, $matches ) ) {
$this->oembed_wpdotorg_plugin( $matches[1] );
} elseif ( preg_match( '#https?://wordpress.org/plugins/([^/]*)/?$#i', $url, $matches ) ) {
$this->oembed_wpdotorg_plugin( $matches[1] );
} elseif ( preg_match( '#https?://wordpress.org/extend/themes/([^/]*)/?$#i', $url, $matches ) ) {
$this->oembed_wpdotorg_theme( $matches[1] );
} elseif ( preg_match( '#https?://wordpress.org/themes/([^/]*)/?$#i', $url, $matches ) ) {
$this->oembed_wpdotorg_theme( $matches[1] );
} else {
header( 'HTTP/1.0 404 Not Found' );
die( 'Mike Little is lost, and afraid' );
}
}
/**
* Retrieve the information from wpdotorg for a plugin, and
* output it as an oembed response
*/
private function oembed_wpdotorg_plugin( $slug ) {
$plugin = $this->api->get_plugin( $slug );
$response = new stdClass();
$response->type = 'rich';
$response->width = '10';
$response->height = '10';
$response->version = '1.0';
$response->title = $plugin->sections['description'];
$response->html = '<div class="wpdotorg-embed wpdotorg-embed-plugin">';
// @TODO This should all be templated
$response->html .= '<p><a href="http://wordpress.org/extend/plugins/' . esc_attr( $slug ) . '" target="_blank"><strong>' . esc_html( $plugin->name ) . '</strong></a><br/>';
if ( ! empty( $plugin->author ) ) {
$response->html .= 'by <span class="wpdotorg-embed-plugin-author">' . wp_kses_post( $plugin->author ) . '</span></p>';
}
if ( ! empty( $plugin->sections['description'] ) ) {
$response->html .= '<p class="wpdotorg-embed-plugin-description">' . wp_kses_post( $plugin->sections['description'] ) . '</p>';
}
$stats = '';
if ( ! empty( $plugin->version) ) {
$stats .= '<li>Current version: ' . esc_html( $plugin->version ) . '</li>';
}
if ( ! empty( $plugin->rating ) ) {
$stats .= '<li>Rating: ' . esc_html( $plugin->rating ) . '(' . esc_html( $plugin->num_ratings ) . ' ratings)</li>';
}
if ( ! empty( $plugin->downloaded ) ) {
$stats .= '<li>Downloaded ' . esc_html( number_format_i18n( $plugin->downloaded ) ) . ' times</li>';
}
if ( ! empty( $stats ) ) {
$response->html .= '<p><strong>Stats:</strong></p><ul class="wpdotorg-embed-stats-list">'.$stats.'</ul>';
}
$response->html .= '</div>';
header( 'Content-Type: application/json' );
echo json_encode( $response );
die();
}
/**
* Retrieve the information from wpdotorg for a theme, and
* output it as an oembed response
*/
private function oembed_wpdotorg_theme( $slug ) {
$theme = $this->api->get_theme( $slug );
$response = new stdClass();
$response->type = 'rich';
$response->width = '10';
$response->height = '10';
$response->version = '1.0';
$response->title = $theme->sections['description'];
$response->html = '<div class="wpdotorg-embed wpdotorg-embed-theme">';
// @TODO This should all be templated
$response->html .= '<p><a href="http://wordpress.org/extend/themes/' . esc_attr( $slug ) . '" target="_blank"><strong>' . esc_html( $theme->name ) . '</strong></a><br/>';
if ( ! empty( $theme->author ) )
$response->html .= 'by <span class="wpdotorg-embed-theme-author">' . wp_kses_post( $theme->author ) . '</span></p>';
if ( ! empty( $theme->sections['description'] ) )
$response->html .= '<p class="wpdotorg-embed-theme-description">' . wp_kses_post( $theme->sections['description'] ) . '</p>';
$stats = '';
if ( ! empty( $theme->version) ) {
$stats .= '<li>Current version: ' . esc_html( $theme->version ) . '</li>';
}
if ( ! empty( $theme->rating ) ) {
$stats .= '<li>Rating: ' . esc_html( $theme->rating ) . '(' . esc_html( $theme->num_ratings ) . ' ratings)</li>';
}
if ( ! empty( $theme->downloaded ) ) {
$stats .= '<li>Downloaded ' . esc_html( number_format_i18n( $plugin->downloaded ) ) . ' times</li>';
}
if ( ! empty( $stats ) ) {
$response->html .= '<p><strong>Stats:</strong></p><ul class="wpdotorg-embed-stats-list">'.$stats.'</ul>';
}
$response->html .= '</div>';
header( 'Content-Type: application/json' );
echo json_encode( $response );
die();
}
}
require_once( 'wpdotorg-api.php' );
$wpdotorg_api = new wpdotorg_api();
$wpdotorg_embed = new wpdotorg_embed( $wpdotorg_api );