From 945029d9c1ebb5643602b10d5d5d0c2f0a424307 Mon Sep 17 00:00:00 2001 From: Fabian Wolf Date: Tue, 18 Apr 2017 09:13:54 +0200 Subject: [PATCH] Added cache refresh option and changed the default cache timeout to something sensible. --- mastodon-embed.php | 34 +++++++++++++++++++++++++++++++--- readme.txt | 8 ++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/mastodon-embed.php b/mastodon-embed.php index f64a90c..f5ddc2c 100644 --- a/mastodon-embed.php +++ b/mastodon-embed.php @@ -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 Mastodon embed 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 * @@ -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' ) ) { @@ -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 ); @@ -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 ); @@ -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 diff --git a/readme.txt b/readme.txt index fae8a8a..0c2f203 100644 --- a/readme.txt +++ b/readme.txt @@ -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/) @@ -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 = @@ -66,6 +68,7 @@ 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: @@ -73,6 +76,11 @@ 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