Author URI: http://petermolnar.eu/
License: GPLv3
Required minimum PHP version: 5.3
*/
/* Copyright 2015 Peter Molnar ( hello@petermolnar.eu )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 3, as
published by the Free Software Foundation.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if (!class_exists('WP_FEATURED2RSS')):
class WP_FEATURED2RSS {
const expire = 30;
public function __construct () {
register_activation_hook( __FILE__ , array( &$this, 'plugin_activate' ) );
add_action( 'rss2_item', array(&$this,'insert_enclosure_image') );
}
/**
* activate hook
*/
public static function plugin_activate() {
if ( version_compare( phpversion(), 5.3, '<' ) ) {
die( 'The minimum PHP version required for this plugin is 5.3' );
}
}
/**
*
*/
public static function insert_enclosure_image ( ) {
$post = static::fix_post();
static::debug('insterting featured image to rss',7);
if ($post === false )
return false;
$thid = get_post_thumbnail_id( $post->ID );
if ( ! $thid )
return false;
if ( $cached = wp_cache_get ( $thid, __CLASS__ . __FUNCTION__ ) )
return $cached;
$asize = false;
$meta = wp_get_attachment_metadata($thid);
// creating sorted candidates array from available sizes,
// sorted by width*height pixels
$candidates = array();
foreach( $meta['sizes'] as $potential => $details ) {
if ( isset($details['width']) && isset($details['height']) ) {
$max = ((int)$details['width']) * ((int)$details['height']);
$candidates[ $potential ] = $max;
}
}
arsort($candidates);
foreach ($candidates as $potential => $maxsize ) {
static::debug('checking size ' . $potential . ': ' . $meta['sizes'][$potential]['file'] . ' vs ' .$meta['file'], 7 );
if (isset($meta['sizes'][$potential]) && isset($meta['sizes'][$potential]['file']) && $meta['sizes'][$potential]['file'] != $meta['file']) {
static::debug( $meta['sizes'][$potential]['file'] . ' look like a resized file, using it', 7);
$asize = $potential;
$img = wp_get_attachment_image_src( $thid, $potential );
break;
}
}
if ( $asize == false )
return false;
$upload_dir = wp_upload_dir();
// check for cached version of the image, in case the plugin is used
// in tandem with [wp-resized2cache](https://github.com/petermolnar/wp-resized2rss)
$cached = WP_CONTENT_DIR . '/cache/' . $meta['sizes'][$asize]['file'];
$file = $upload_dir['basedir'] . '/' . $meta['sizes'][$asize]['file'];
if ( file_exists($cached))
$fsize = filesize($cached);
elseif ( file_exists($file) )
$fsize = filesize($file);
else
return false;
$mime = $meta['sizes'][$asize]['mime-type'];
$str = sprintf('
' . $message . '
' ); exit; } $trace = debug_backtrace(); $caller = $trace[1]; $parent = $caller['function']; if (isset($caller['class'])) $parent = $caller['class'] . '::' . $parent; return error_log( "{$parent}: {$message}" ); } /** * */ public static function fix_url ( $url, $absolute = true ) { // move to generic scheme $url = str_replace ( array('http://', 'https://'), 'https://', $url ); $domain = parse_url(get_bloginfo('url'), PHP_URL_HOST); // relative to absolute if ($absolute && !stristr($url, $domain)) { $url = 'https://' . $domain . '/' . ltrim($url, '/'); } return $url; } } $WP_FEATURED2RSS = new WP_FEATURED2RSS(); endif;