initial release
Peter Molnar hello@petermolnar.eu
Mon, 14 Dec 2015 13:39:18 +0000
3 files changed,
235 insertions(+),
0 deletions(-)
A
composer.json
@@ -0,0 +1,15 @@
+{ + "name": "petermolnar/wp-featured2rss", + "description": "WordPress plugin to add a resized featured image to RSS feed as enclosure (which WordPress doesn't do by default)", + "require": { + "php": ">=5.3.0", + }, + "license": "GPLv3", + "authors": [ + { + "name": "Peter Molnar", + "email": "hello@petermolnar.eu", + "homepage": "https://petermolnar.eu" + } + ] +}
A
readme.txt
@@ -0,0 +1,38 @@
+=== wp-featured2rss === +Contributors: cadeyrn +Donate link: https://paypal.me/petermolnar/3 +Tags: RSS, feed, featured image, attachment +Requires at least: 3.0 +Tested up to: 4.4 +Stable tag: 0.1 +License: GPLv3 +License URI: http://www.gnu.org/licenses/gpl-3.0.html +Required minimum PHP version: 5.3 + +WordPress plugin to add a resized featured image to RSS feed as enclosure (which WordPress doesn't do by default) + +== Description == + +For an unknown reason, WordPress doesn't adding featured images as enclosures to my RSS2 feed; this plugin is supposed to take care of that instead. + +It's strictly only using resized images to maintain compatibility in case access to the full size image is blocked for various reasons (like not wanting people to be able to access it). + +== Installation == + +1. Upload contents of `wp-featured2rss` to the `/wp-content/plugins/` directory +2. Activate the plugin through the `Plugins` menu in WordPress + +== Frequently Asked Questions == + +== Changelog == + +Version numbering logic: + +* every A. indicates BIG changes. +* every .B version indicates new features. +* every ..C indicates bugfixes for A.B version. + += 0.1 = +*2015-12-14* + +* initial public release
A
wp-featured2rss.php
@@ -0,0 +1,182 @@
+<?php +/* +Plugin Name: wp-featured2rss +Plugin URI: https://github.com/petermolnar/wp-featured2rss +Description: WordPress plugin to add featured image to RSS feed as attachment (which WordPress doesn't do by default) +Version: 0.1 +Author: Peter Molnar <hello@petermolnar.eu> +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 () { + + add_action( 'rss2_item', array(&$this,'insert_enclosure_image') ); + } + + /** + * + */ + public static function insert_enclosure_image ( ) { + + $post = static::fix_post(); + + static::debug('insterting featured image to rss'); + + 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; + + $sizes = array ( + 0 => 'large', + 1 => 'medium', + 2 => 'thumbnail' + ); + + $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'] ); + + 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'); + $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('<enclosure url="%s" type="%s" length="%s" />',static::fix_url($img[0]),$mime,$fsize); + + wp_cache_set ( $thid, $str, __CLASS__ . __FUNCTION__, static::expire ); + + echo $str; + } + + /** + * do everything to get the Post object + */ + public static function fix_post ( &$post = null ) { + if ($post === null || !static::is_post($post)) + global $post; + + if (static::is_post($post)) + return $post; + + return false; + } + + /** + * test if an object is actually a post + */ + public static function is_post ( &$post ) { + if ( !empty($post) && is_object($post) && isset($post->ID) && !empty($post->ID) ) + return true; + + return false; + } + + /** + * + * debug messages; will only work if WP_DEBUG is on + * or if the level is LOG_ERR, but that will kill the process + * + * @param string $message + * @param int $level + */ + static function debug( $message, $level = LOG_NOTICE ) { + if ( @is_array( $message ) || @is_object ( $message ) ) + $message = json_encode($message); + + + switch ( $level ) { + case LOG_ERR : + wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' ); + exit; + default: + if ( !defined( 'WP_DEBUG' ) || WP_DEBUG != true ) + return; + break; + } + + error_log( __CLASS__ . " => " . $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;