Skip to content

Commit

Permalink
[#127] WordPress 5.6/SimplePie Compatibility: Dealing with deprecated…
Browse files Browse the repository at this point in the history
… WP_Feed_Cache with replacement FeedWordPie_Cache class. (Should resolve PHP warnings: 'Deprecated: class-wp-feed-cache.php is <strong>deprecated</strong> since version 5.6.0', 'Message: ./cache is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.')
  • Loading branch information
radgeek committed Jan 18, 2021
1 parent 4e2494a commit 2903991
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 5 deletions.
177 changes: 177 additions & 0 deletions extend/SimplePie/feedwordpie_cache.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* FeedWordPie_Cache class. Provides customized feed caching for FeedWordPress.
* This is derived (currently, just copied and combined) from WordPress Core
* wp-includes/class-wp-feed-cache.php ("Feed API: WP_Feed_Cache_Transient class")
* and from wp-includes/SimplePie/Cache.php ("SimplePie_Cache"), pursuant to the
* terms of the GPL.
*
* The wrapper class WP_Feed_Cache was deprecated in WordPress 5.6, but its intended
* replacement, WP_Feed_Cache_Transient, is currently NOT a subclass of SimplePie_Cache
* which causes problems registering it as a cache class in some versions of SimplePie.
* Solution: For now, let's copy the class over under a new name, but this time, inherit
* from SimplePie_Cache. (I am doing it this way because I might want to make some real
* changs to the implementation of caching, in order to better support typical FeedWordPress
* use cases. In the meantime, this should at least stop the PHP warnings and the failure
* to correctly cache feed contents that users are encountering with WordPress 5.6.)
*
* @version 2021.0118
*/

/**
* Core class used to implement feed cache transients.
*
* @since 2.8.0
*/
class FeedWordPie_Cache extends SimplePie_Cache {

/**
* Holds the transient name.
*
* @since 2.8.0
* @var string
*/
public $name;

/**
* Holds the transient mod name.
*
* @since 2.8.0
* @var string
*/
public $mod_name;

/**
* Holds the cache duration in seconds.
*
* Defaults to 43200 seconds (12 hours).
*
* @since 2.8.0
* @var int
*/
public $lifetime = 43200;

/**
* Constructor.
*
* @since 2.8.0
* @since 3.2.0 Updated to use a PHP5 constructor.
*
* @param string $location URL location (scheme is used to determine handler).
* @param string $filename Unique identifier for cache object.
* @param string $extension 'spi' or 'spc'.
*/
public function __construct( $location, $filename, $extension ) {
$this->name = 'feed_' . $filename;
$this->mod_name = 'feed_mod_' . $filename;

$lifetime = $this->lifetime;
/**
* Filters the transient lifetime of the feed cache.
*
* @since 2.8.0
*
* @param int $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
* @param string $filename Unique identifier for the cache object.
*/
$this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename );
}

/**
* Sets the transient.
*
* @since 2.8.0
*
* @param SimplePie $data Data to save.
* @return true Always true.
*/
public function save( $data ) {
if ( $data instanceof SimplePie ) {
$data = $data->data;
}

set_transient( $this->name, $data, $this->lifetime );
set_transient( $this->mod_name, time(), $this->lifetime );
return true;
}

/**
* Gets the transient.
*
* @since 2.8.0
*
* @return mixed Transient value.
*/
public function load() {
return get_transient( $this->name );
}

/**
* Gets mod transient.
*
* @since 2.8.0
*
* @return mixed Transient value.
*/
public function mtime() {
return get_transient( $this->mod_name );
}

/**
* Sets mod transient.
*
* @since 2.8.0
*
* @return bool False if value was not set and true if value was set.
*/
public function touch() {
return set_transient( $this->mod_name, time(), $this->lifetime );
}

/**
* Deletes transients.
*
* @since 2.8.0
*
* @return true Always true.
*/
public function unlink() {
delete_transient( $this->name );
delete_transient( $this->mod_name );
return true;
}

/**
* Create a new SimplePie_Cache object
*
* @param string $location URL location (scheme is used to determine handler)
* @param string $filename Unique identifier for cache object
* @param string $extension 'spi' or 'spc'
* @return SimplePie_Cache_Base Type of object depends on scheme of `$location`
*/
public static function get_handler($location, $filename, $extension)
{

$type = explode(':', $location, 2);
$type = $type[0];
if (!empty(self::$handlers[$type]))
{
$class = self::$handlers[$type];
return $class($location, $filename, $extension);
}

return new FeedWordPie_Cache($location, $filename, $extension);
}

/**
* Create a new SimplePie_Cache object
*
* @deprecated Use {@see get_handler} instead
*/
public function create($location, $filename, $extension)
{
trigger_error('Cache::create() has been replaced with Cache::get_handler(). Switch to the registry system to use this.', E_USER_DEPRECATED);
return self::get_handler($location, $filename, $extension);
}

}
9 changes: 4 additions & 5 deletions feedwordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: FeedWordPress
Plugin URI: http://feedwordpress.radgeek.com/
Description: simple and flexible Atom/RSS syndication for WordPress
Version: 2020.0818
Version: 2021.0118
Author: C. Johnson
Author URI: https://feedwordpress.radgeek.com/contact/
License: GPL
Expand Down Expand Up @@ -96,8 +96,6 @@
// Dependencies: modules packaged with WordPress core
$wpCoreDependencies = array(
"class:SimplePie" => "class-simplepie",
"class:WP_Feed_Cache" => "class-wp-feed-cache",
"class:WP_Feed_Cache_Transient" => "class-wp-feed-cache-transient",
"class:WP_SimplePie_File" => "class-wp-simplepie-file",
"class:WP_SimplePie_Sanitize_KSES" => "class-wp-simplepie-sanitize-kses",
"function:wp_insert_user" => "registration",
Expand Down Expand Up @@ -141,6 +139,7 @@
require_once("${dir}/inspectpostmeta.class.php");
require_once("${dir}/syndicationdataqueries.class.php");
require_once("${dir}/extend/SimplePie/feedwordpie.class.php");
require_once("${dir}/extend/SimplePie/feedwordpie_cache.class.php");
require_once("${dir}/extend/SimplePie/feedwordpie_item.class.php");
require_once("${dir}/extend/SimplePie/feedwordpie_file.class.php");
require_once("${dir}/extend/SimplePie/feedwordpie_parser.class.php");
Expand Down Expand Up @@ -1928,15 +1927,15 @@ static function fetch ($url, $params = array()) {
$timeout = intval($timeout);

$pie_class = apply_filters('feedwordpress_simplepie_class', 'FeedWordPie');
$cache_class = apply_filters('feedwordpress_cache_class', 'WP_Feed_Cache');
$cache_class = apply_filters('feedwordpress_cache_class', 'FeedWordPie_Cache');
$file_class = apply_filters('feedwordpress_file_class', 'FeedWordPie_File');
$parser_class = apply_filters('feedwordpress_parser_class', 'FeedWordPie_Parser');
$item_class = apply_filters('feedwordpress_item_class', 'FeedWordPie_Item');
$sniffer_class = apply_filters('feedwordpress_sniffer_class', 'FeedWordPie_Content_Type_Sniffer');

$feed = new $pie_class;
$feed->set_feed_url($url);
$feed->set_cache_class($cache_class);
$feed->registry->register('Cache', $cache_class);
$feed->set_timeout($timeout);

$feed->set_content_type_sniffer_class($sniffer_class);
Expand Down

1 comment on commit 2903991

@talgalili
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @radgeek - any chance you'd be releasing this to wordpress.org anytime soon?!

Please sign in to comment.