This commit is contained in:
Peter Molnar 2016-07-22 10:47:02 +00:00
parent 71fa8a766b
commit 0fc17ec338
2 changed files with 196 additions and 185 deletions

View file

@ -4,7 +4,7 @@ Donate link: https://paypal.me/petermolnar/3
Tags: RSS, feed, featured image, attachment Tags: RSS, feed, featured image, attachment
Requires at least: 3.0 Requires at least: 3.0
Tested up to: 4.4.2 Tested up to: 4.4.2
Stable tag: 0.1.1 Stable tag: 0.2
License: GPLv3 License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html License URI: http://www.gnu.org/licenses/gpl-3.0.html
Required minimum PHP version: 5.3 Required minimum PHP version: 5.3
@ -32,6 +32,13 @@ Version numbering logic:
* every .B version indicates new features. * every .B version indicates new features.
* every ..C indicates bugfixes for A.B version. * every ..C indicates bugfixes for A.B version.
= 0.2 =
*2016-07-22*
* switched to namespace
* max 80 char long lines
= 0.1.1 = = 0.1.1 =
*2016-03-08* *2016-03-08*

View file

@ -2,8 +2,8 @@
/* /*
Plugin Name: wp-featured2rss Plugin Name: wp-featured2rss
Plugin URI: https://github.com/petermolnar/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) Description: WordPress plugin to add featured image to RSS feed as attachment
Version: 0.1.1 Version: 0.3
Author: Peter Molnar <hello@petermolnar.eu> Author: Peter Molnar <hello@petermolnar.eu>
Author URI: http://petermolnar.eu/ Author URI: http://petermolnar.eu/
License: GPLv3 License: GPLv3
@ -26,20 +26,17 @@ Required minimum PHP version: 5.3
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
if (!class_exists('WP_FEATURED2RSS')): namespace WP_FEATURED2RSS;
class WP_FEATURED2RSS { define ( 'WP_FEATURED2RSS\expire', 30000 );
const expire = 30; \register_activation_hook( __FILE__ , 'WP_FEATURED2RSS\plugin_activate' );
\add_action( 'rss2_item', 'WP_FEATURED2RSS\insert_enclosure_image');
public function __construct () {
register_activation_hook( __FILE__ , array( &$this, 'plugin_activate' ) );
add_action( 'rss2_item', array(&$this,'insert_enclosure_image') );
}
/** /**
* activate hook * activate hook
*/ */
public static function plugin_activate() { function plugin_activate() {
if ( version_compare( phpversion(), 5.3, '<' ) ) { if ( version_compare( phpversion(), 5.3, '<' ) ) {
die( 'The minimum PHP version required for this plugin is 5.3' ); die( 'The minimum PHP version required for this plugin is 5.3' );
} }
@ -48,11 +45,9 @@ class WP_FEATURED2RSS {
/** /**
* *
*/ */
public static function insert_enclosure_image ( ) { function insert_enclosure_image ( ) {
$post = static::fix_post(); $post = fix_post();
static::debug('insterting featured image to rss',7);
if ($post === false ) if ($post === false )
return false; return false;
@ -61,7 +56,8 @@ class WP_FEATURED2RSS {
if ( ! $thid ) if ( ! $thid )
return false; return false;
if ( $cached = wp_cache_get ( $thid, __CLASS__ . __FUNCTION__ ) ) debug('insterting featured image to rss',7);
if ( $cached = wp_cache_get ( $thid, __NAMESPACE__ . __FUNCTION__ ) )
return $cached; return $cached;
$asize = false; $asize = false;
@ -79,10 +75,15 @@ class WP_FEATURED2RSS {
arsort($candidates); arsort($candidates);
foreach ($candidates as $potential => $maxsize ) { foreach ($candidates as $potential => $maxsize ) {
static::debug('checking size ' . $potential . ': ' . $meta['sizes'][$potential]['file'] . ' vs ' .$meta['file'], 7 ); 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']) { if ( isset( $meta['sizes'][$potential] ) &&
static::debug( $meta['sizes'][$potential]['file'] . ' look like a resized file, using it', 7); isset( $meta['sizes'][$potential]['file'] ) &&
$meta['sizes'][$potential]['file'] != $meta['file']
) {
debug( "{$meta['sizes'][$potential]['file']} look like a resized file;".
" using it", 7 );
$asize = $potential; $asize = $potential;
$img = wp_get_attachment_image_src( $thid, $potential ); $img = wp_get_attachment_image_src( $thid, $potential );
break; break;
@ -94,7 +95,7 @@ class WP_FEATURED2RSS {
$upload_dir = wp_upload_dir(); $upload_dir = wp_upload_dir();
// check for cached version of the image, in case the plugin is used // check for cached version of the image, in case the plugin is used
// in tandem with [wp-resized2cache](https://github.com/petermolnar/wp-resized2rss) // in tandem with https://github.com/petermolnar/wp-resized2rss
$cached = WP_CONTENT_DIR . '/cache/' . $meta['sizes'][$asize]['file']; $cached = WP_CONTENT_DIR . '/cache/' . $meta['sizes'][$asize]['file'];
$file = $upload_dir['basedir'] . '/' . $meta['sizes'][$asize]['file']; $file = $upload_dir['basedir'] . '/' . $meta['sizes'][$asize]['file'];
@ -106,9 +107,14 @@ class WP_FEATURED2RSS {
return false; return false;
$mime = $meta['sizes'][$asize]['mime-type']; $mime = $meta['sizes'][$asize]['mime-type'];
$str = sprintf('<enclosure url="%s" type="%s" length="%s" />',static::fix_url($img[0]),$mime,$fsize); $str = sprintf(
'<enclosure url="%s" type="%s" length="%s" />',
\site_url( $img[0] ),
$mime,
$fsize
);
wp_cache_set ( $thid, $str, __CLASS__ . __FUNCTION__, static::expire ); wp_cache_set ( $thid, $str, __NAMESPACE__ . __FUNCTION__, expire );
echo $str; echo $str;
} }
@ -116,11 +122,11 @@ class WP_FEATURED2RSS {
/** /**
* do everything to get the Post object * do everything to get the Post object
*/ */
public static function fix_post ( &$post = null ) { function fix_post ( &$post = null ) {
if ($post === null || !static::is_post($post)) if ($post === null || !is_post($post))
global $post; global $post;
if (static::is_post($post)) if (is_post($post))
return $post; return $post;
return false; return false;
@ -129,8 +135,11 @@ class WP_FEATURED2RSS {
/** /**
* test if an object is actually a post * test if an object is actually a post
*/ */
public static function is_post ( &$post ) { function is_post ( &$post ) {
if ( !empty($post) && is_object($post) && isset($post->ID) && !empty($post->ID) ) if ( ! empty( $post ) &&
is_object( $post ) &&
isset( $post->ID ) &&
! empty( $post->ID ) )
return true; return true;
return false; return false;
@ -147,7 +156,7 @@ class WP_FEATURED2RSS {
* @output log to syslog | wp_die on high level * @output log to syslog | wp_die on high level
* @return false on not taking action, true on log sent * @return false on not taking action, true on log sent
*/ */
public static function debug( $message, $level = LOG_NOTICE ) { function debug( $message, $level = LOG_NOTICE ) {
if ( empty( $message ) ) if ( empty( $message ) )
return false; return false;
@ -195,8 +204,8 @@ class WP_FEATURED2RSS {
/** /**
* *
*/ *
public static function fix_url ( $url, $absolute = true ) { function fix_url ( $url, $absolute = true ) {
// move to generic scheme // move to generic scheme
$url = str_replace ( array('http://', 'https://'), 'https://', $url ); $url = str_replace ( array('http://', 'https://'), 'https://', $url );
@ -208,9 +217,4 @@ class WP_FEATURED2RSS {
return $url; return $url;
} }
*/
}
$WP_FEATURED2RSS = new WP_FEATURED2RSS();
endif;