Skip to content

Commit

Permalink
Added cache refresh option and changed the default cache timeout to s…
Browse files Browse the repository at this point in the history
…omething sensible.
  • Loading branch information
Fabian Wolf committed Apr 18, 2017
1 parent da0c9fc commit 945029d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
34 changes: 31 additions & 3 deletions mastodon-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 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 4.8-nightly
* Version: 2.1
* Version: 2.2
* Author: Fabian Wolf
* License: GNU GPL v2 or later
*
Expand All @@ -17,6 +17,7 @@
* - 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')
*/

if( !class_exists( 'simple_html_dom' ) ) {
Expand Down Expand Up @@ -110,10 +111,11 @@ function shortcode( $atts = null, $content = null ) {
'width' => 700,
'height' => 200,
'css' => 'overflow: hidden',
'cache_timeout' => 0, // in seconds
'cache_timeout' => 24 * 60 * 60, // in seconds; defaults to 1 day
'no_iframe' => 0, // workaround for localhost / testing purposes
'disable_font_awesome' => 0,
'no_fa' => 0,
'flush' => 0, // intentionally flush the cache
);

$atts = shortcode_atts( $default_atts, $atts );
Expand Down Expand Up @@ -147,7 +149,9 @@ function shortcode( $atts = null, $content = null ) {
'sslverify' => false,
);

$cache_response = get_transient( $transient_name . $url_hash );
if( empty( $flush ) ) {
$cache_response = get_transient( $transient_name . $url_hash );
}

if( empty( $cache_response ) ) {
$response = wp_remote_get( $url, $args );
Expand Down Expand Up @@ -273,6 +277,30 @@ function shortcode( $atts = null, $content = null ) {

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
Expand Down
8 changes: 8 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Tested up to: 4.8
Stable tag: trunk
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Version: 2.2

Includes simple_html_dom DOM Parser (Revision 210), which is licensed under The MIT License (http://sourceforge.net/projects/simplehtmldom/)

Expand All @@ -24,6 +25,7 @@ Currently implemented features:
* 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 class instead of XPath
* Optional manual cache refresh option via shortcode attribute

= Future plans =

Expand Down Expand Up @@ -66,13 +68,19 @@ All available shortcode attributes:
* no_iframe - Disable iframe embed and use the direct content embedding instead. Automatically will load the custom CSS file, too.
* disable_font_awesome - Disable loading of Font Awesome when using the direct content embed (see above attribute), eg. if your theme is already including Font Awesome or you want to use different font icons (which have to be compatible to Font Awesome though).
* no_fa' - Alias
* flush - set this to 1 to refresh the embed cache; update post after this, give its frontend view a spin, and then remove it afterwards ;)

= Q. I have a question =
A. Chances are, someone else has asked it. Either check out the support forum at WP or take a look at the official issue tracker:
http://github.com/ginsterbusch/mastodon-embed/issues

== Changelog ==

= 2.2 =

* Fix: Corrected the cache_timeout (originally a constant was used, but I removed it from default usage and forgot to set the default attribute to something sensible); now set to 1 day (in seconds)
* Added the shortcode attribute 'flush' to enable manual cache refreshing

= 2.1 =

* Added "direct embed" option as a fallback for development and testing purposes
Expand Down

0 comments on commit 945029d

Please sign in to comment.