-
Notifications
You must be signed in to change notification settings - Fork 1
/
mastodon-embed.php
432 lines (328 loc) · 13.6 KB
/
mastodon-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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* Plugin Name: Mastodon Embed Improved
* Plugin URI: http://f2w.de/mastodon-embed
* Description: A plugin to embed Mastodon statuses. Complete rewrite of <a href="https://github.com/DavidLibeau/mastodon-tools">Mastodon embed</a> by David Libeau. Tested up to WP 5.0-nightly
* Version: 2.5
* Author: Fabian Wolf
* Author URI: http://usability-idealist.de
* License: GNU GPL v2 or later
*
* Features:
* - class instead function
* - multiple embeds
* - working caching
* - proper shortcode initialization
* - backward compatiblity for mastodon-embed
* - directly embed toots instead of relying on the iframe method
* - fallback method: iframe (explicitely enabled via shortcode attribute 'use_iframe' (alias: 'iframe') and set it to '1'
* - direct embed with reverse-engineered CSS file (including LESS base) and override option (filter: mastodon_embed_content_style)
* - uses different shortcode ('mastodon_embed' instead of 'mastodon') if the original mastodon-embed is active as well
* - uses simple_html_dom instead of XPath
* - cache refresh via attribute ('flush')
* - improved debugging (WP_DEBUG + extended constants)
* - alias for no_iframe attribute: disable_iframe
* - force URL scheme attribute ('force_scheme') to further improve SSL only vs. unencrypted http-only sites (ie. fun with SSL enforcement in WP ;))
* - automatically picks out specific single toot; to display the complete conversation, set 'no_center' to 1; also, only works with "direct" toot embedding
*/
if( !class_exists( 'simple_html_dom' ) ) {
require_once( plugin_dir_path(__FILE__) . 'simple_html_dom.php' );
}
class __mastodon_embed_plugin {
public $pluginName = 'Mastodon Embed Improved',
$pluginPrefix = 'mastodon_embed_';
public $shortcode_name = 'mastodon',
$is_compatiblity_mode = false;
/**
* Plugin instance.
*
* @see get_instance()
* @type object
*/
protected static $instance = NULL;
/**
* Access this plugin’s working instance
*
* @wp-hook plugins_loaded
* @return object of this class
*/
public static function get_instance() {
NULL === self::$instance and self::$instance = new self( true );
return self::$instance;
}
function __construct( $plugin_init = false ) {
if( !empty( $plugin_init ) ) {
// check if the original mastodon is active and change the shortcode name accordingly
if( function_exists( 'mastodon_embed_callback' ) ) {
$this->is_compatiblity_mode = true;
$this->shortcode_name = 'mastodon_embed';
}
$this->init_assets();
add_shortcode( $this->shortcode_name, array( $this, 'shortcode' ) );
}
}
function init_assets() {
/**
* @hook mastodon_embed_content_style URL to the direct embed content CSS.
* @hook mastodon_embed_content_style_dark URL to the dark themed embed content CSS.
* @hook mastodon_embed_font_awesome_url Font Awesome CDN URL. Replace with your own or set to null to disable it from being loaded at all.
*/
$embed_content_style = apply_filters( $this->pluginPrefix . 'content_style', plugin_dir_url(__FILE__) . 'embed-content.css' );
$embed_content_style_dark = apply_filters( $this->pluginPrefix . 'content_style_dark', plugin_dir_url( __FILE__ ) . 'embed-content-dark.css' );
$embed_content_fa_url = apply_filters( $this->pluginPrefix . 'font_awesome_url', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css' );
if( !empty( $embed_content_fa_url ) ) {
wp_register_style( $this->pluginPrefix . 'font-awesome', $embed_content_fa_url );
}
if( !empty( $embed_content_style ) || !empty( $embed_content_style_dark ) ) {
$strEmbedStyleURL = ( empty( $embed_content_style ) ? $embed_content_style_dark : $embed_content_style );
wp_register_style( $this->pluginPrefix . 'content', $strEmbedStyleURL );
}
/**
* @hook mastodon_embed_js_helpers_url URL to the default Javascript helper bundle.
*/
$embed_js_helpers = apply_filters( $this->pluginPrefix . 'js_helpers_url', plugin_dir_url( __FILE__ ) . 'mastodon-embed.js' );
if( !empty( $embed_js_helpers ) ) {
wp_register_script( $this->pluginPrefix . 'helpers', $embed_js_helpers, array('jquery' ), false, true );
}
}
/**
* Debug data
* NOTE: Only output debug information if either the debug mode is enabled OR the user has admin privileges!
*/
function is_debug() {
$return = false;
if( ( defined( 'WP_DEBUG' ) && WP_DEBUG != false ) || current_user_can( 'manage_options' ) != false ) {
$return = true;
}
/**
* Enhanced mode
* NOTE: Looks a bit awkward, but this stuff is reallly complicated to handle .. o.O
*/
if( defined( 'WP_DEBUG' ) && WP_DEBUG != false ) {
if( ( defined('WP_DEBUG_LOG' ) && WP_DEBUG_LOG != false ) || ( defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY == false ) ) {
$return = false;
}
}
return $return;
}
function shortcode( $atts = null, $content = null ) {
$return = '';
$transient_name = 'mastodon_status_';
$debug = '';
/**
* NOTE: Insecure - use default attributes + @function wp_parse_args or @function shortcode_atts (do essentially the same)
*/
/**
* @hook mastodon_embed_default_attributes - The default shortcode attributes; admin settings or custom config file hooks into this, too
*/
$default_atts = apply_filters( $this->pluginPrefix . 'default_attributes', array(
'url' => '', // backward compatiblity (avoid having to increase the major version number)
'container_class' => 'mastodon-embed',
'width' => 700,
'height' => 200,
'css' => 'overflow: hidden',
'cache_timeout' => 24 * 60 * 60, // in seconds; defaults to 1 day
'use_iframe' => 0, // workaround for localhost / testing purposes
'iframe' => 0, // alias
'no_iframe' => 1, // backward compatiblity to < 2.5
'disable_iframe' => 0, // backward compatiblity mode
'disable_font_awesome' => 0,
'force_scheme' => '',
'no_fa' => 0,
'flush' => 0, // intentionally flush the cache
/*'center' => 1, // picks out only the main toot out of a conversation (entry-center class)*/
'no_center' => 0, // disable "centering" function
'enable_debug' => 0, // specific debug parameter
'show_delay' => 350, // in ms
) );
$atts = shortcode_atts( $default_atts, $atts );
extract( $atts, EXTR_SKIP );
/**
* NOTE: @function empty is much quicker and more reliable; aside of that, usage of an url attribute makes no sense. Just drop it in the content, done!
*/
if( !empty( $content ) ) {
$url = trim( strip_tags( $content ) );
}
/**
* NOTE: Sanitize URL first; if nothing is left .. dont do anything ;) Also: Backward compat. for original mastodon-embed.
*/
if( !empty( $url ) ) {
$url = esc_url( $url );
}
if( !empty( $url ) ) {
$url_hash = md5( $url ); // we dont need no "safe" encryption, just a quick way to cache stuff!
if( defined( '__MASTODON_EMBED_CACHETIME' ) && empty( $cache_timeout ) ) {
$cache_timeout = intval( __MASTODON_EMBED_CACHETIME );
}
$args = array(
'sslverify' => false,
);
if( empty( $flush ) ) {
$cache_response = get_transient( $transient_name . $url_hash );
}
if( empty( $cache_response ) ) {
$response = wp_remote_get( $url, $args );
/**
* TODO: Improve caching; maybe add the content to the cache if its a direct embed ...
*/
set_transient( $transient_name . $url_hash, $response, $cache_timeout ); //cache curl response (we only need one response to get atom url)
} else {
$response = $cache_response;
}
$http_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body( $response );
if( !empty( $iframe ) || !empty( $use_iframe ) ) {
$iframe = true;
}
/**
* NOTE: Obsolete code from @version 2.4.3
*/
/*
if( !empty( $no_iframe ) || !empty( $disable_iframe ) ) {
if( !empty( $disable_iframe ) ) {
$no_iframe = true;
}
}
*/
/**
* NOTE: Switch case is so much easier .. not to mention a proper $return.
*/
switch( $http_code ) {
case 404:
$container_class .= ' resource-not-found';
$return = '<strong>Nothing found.</strong>';
break;
case 301:
$container_class .= ' resource-access-denied';
$return = '<strong>Access denied.</strong>';
break;
default:
/**
* NOTE: using Simple HTML DOM instead of XPath
*/
$dom = new simple_html_dom();
$dom->load( $body );
if( !empty( $iframe ) ) { // use iframe
$atom_url = $dom->find( "link[type='application/atom+xml']", 0);
//$embedUrl = str_replace(".atom", "/embed", $atomUrl[0]->getAttribute("href"));
$embed_url = str_replace('.atom', '/embed', $atom_url->href );
if( !empty( $force_scheme ) && in_array( $force_scheme, array( 'http', 'https' ), true ) != false ) {
$host_scheme = parse_url( $embed_url, PHP_URL_SCHEME );
if( $host_scheme != $force_scheme ) {
$embed_url = str_replace( $host_scheme, $force_scheme, $embed_url );
}
}
$return = '<iframe src="' . $embed_url . '" style="'.$css.'" frameborder="0" width="' . $width . '" height="' . $height . '" scrolling="no"></iframe>';
} else { // use direct embed
$embed_content = $dom->find( '.activity-stream', 0 );
if( !empty( $embed_content ) ) {
//if( $this->is_empty_attr( $params['center'] ) == false ) {
if( empty( $no_center ) ) {
$centered_content = $embed_content->find( '.entry-center', 0 );
if( !empty( $centered_content ) ) {
$_embed_content = $embed_content; // backup original
// fetch complete class to wrap the centered content into (ie. avoids breaking the CSS)
$strCenteredWrap = '<div class="' . $embed_content->class . '">%s</div>';
$embed_content = $centered_content; // replace it with the "focused" toot
}
}
wp_enqueue_style( $this->pluginPrefix . 'content' );
if( empty( $disable_font_awesome ) && empty( $no_fa ) ) {
wp_enqueue_style( $this->pluginPrefix . 'font-awesome' );
}
// always load JS
wp_enqueue_script( $this->pluginPrefix . 'helpers' );
// check if nsfw / cw is enabled
/*if( strpos( $embed_content, 'class="p-summary' ) !== false || strpos( $embed_content, 'class="status__content__spoiler-link' ) !== false ) {
// if so, enqueue JS
wp_enqueue_script( $this->pluginPrefix . 'helpers' );
}*/
}
// add remote url to image sources
if( strpos( $embed_content->outertext, 'src=' ) !== false ) { // find images
// get host url
//$host_scheme = parse_url( $url, PHP_URL_SCHEME );
$host_url = parse_url( $url, PHP_URL_HOST );
if( !empty( $host_url ) ) {
foreach( $embed_content->find( 'img' ) as $image ) {
/**
* NOTE: Explicitely check for pre-existing URL scheme - to avoid display issues.
*/
if( strpos( $image->src, 'http://' ) === false && strpos( $image->src, 'https://' ) === false ) {
$image->src = esc_url( '//' .$host_url . $image->src );
}
}
}
}
$return = $embed_content->outertext;
//$debug .= "\n" . print_r( array( 'embed_content' => $embed_content->outertext ), true ) . "\n";
}
break;
}
}
if( $this->is_debug() != false || !empty( $enable_debug ) ) {
$debug = "\n\nDebug:\n" . $debug . print_r( array(
'content' => $content,
'url' => $url,
'embed_url' => $embed_url,
'attributes' => $atts,
'response_code' => $http_code,
'response_body' => $body,
), true ) . "\n";
}
/**
* NOTE: Add notification if no content was found or something else crapped up
*/
if( empty( $return ) ) {
$return = '<!-- mastodon-embed: something went wrong! ' . $debug . ' -->';
$error = true;
}
/**
* NOTE: Wrap return code with container div
*/
if( !empty( $return ) && empty( $error ) ) { // avoids adding the debug data twice!
$strWrap = '<div class="' . $container_class . '">%s</div>';
if( !empty( $strCenteredWrap ) ) {
$strWrap = str_replace( '%s', $strCenteredWrap, $strWrap ); // wrap centered toot into original div tag to avoid breaking the supplied CSS
}
$return = sprintf( $strWrap, $return );
// append debug data (if available)
if( !empty( $debug ) ) {
$return .= "\n<!-- mastodon embed: $debug -->\n\n";
}
}
return $return;
}
/**
* Own empty() implementation, because shortcode attributes might be interpreted as strings and thus conversion to integer might not work out properly
*/
function is_empty_attr( $data, $empty_strings = array('null', 'false', '0' ) ) {
$return = true;
if( is_string( $data ) && $data != '' ) {
if( in_array( strtolower( $data ), $empty_strings, true ) != false ) {
$return = false;
}
}
return $return;
}
/**
* NOTE: Added for possible future enhancements, including storing already parsed toots in a meta field (= temporary / cache)
*/
function get_post_id( $strict_mode = false ) {
global $post;
$return = 0;
if( !empty( $strict_mode ) ) {
$current_post = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
if( !empty( $current_post) && !empty( $current_post->ID ) ) {
$return = $current_post->ID;
}
} else {
if( !empty( $post ) && isset( $post->ID ) ) {
$return = $post->ID;
}
}
return $return;
}
}
// init plugin
add_action( 'plugins_loaded', array( '__mastodon_embed_plugin', 'get_instance' ) );