0.2
This commit is contained in:
parent
71fa8a766b
commit
0fc17ec338
2 changed files with 196 additions and 185 deletions
|
@ -4,7 +4,7 @@ Donate link: https://paypal.me/petermolnar/3
|
|||
Tags: RSS, feed, featured image, attachment
|
||||
Requires at least: 3.0
|
||||
Tested up to: 4.4.2
|
||||
Stable tag: 0.1.1
|
||||
Stable tag: 0.2
|
||||
License: GPLv3
|
||||
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
Required minimum PHP version: 5.3
|
||||
|
@ -32,6 +32,13 @@ Version numbering logic:
|
|||
* every .B version indicates new features.
|
||||
* every ..C indicates bugfixes for A.B version.
|
||||
|
||||
= 0.2 =
|
||||
*2016-07-22*
|
||||
|
||||
* switched to namespace
|
||||
* max 80 char long lines
|
||||
|
||||
|
||||
= 0.1.1 =
|
||||
*2016-03-08*
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
/*
|
||||
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.1
|
||||
Description: WordPress plugin to add featured image to RSS feed as attachment
|
||||
Version: 0.3
|
||||
Author: Peter Molnar <hello@petermolnar.eu>
|
||||
Author URI: http://petermolnar.eu/
|
||||
License: GPLv3
|
||||
|
@ -26,33 +26,28 @@ Required minimum PHP version: 5.3
|
|||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
if (!class_exists('WP_FEATURED2RSS')):
|
||||
namespace WP_FEATURED2RSS;
|
||||
|
||||
class WP_FEATURED2RSS {
|
||||
const expire = 30;
|
||||
define ( 'WP_FEATURED2RSS\expire', 30000 );
|
||||
\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
|
||||
*/
|
||||
public static function plugin_activate() {
|
||||
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 ( ) {
|
||||
function insert_enclosure_image ( ) {
|
||||
|
||||
$post = static::fix_post();
|
||||
|
||||
static::debug('insterting featured image to rss',7);
|
||||
$post = fix_post();
|
||||
|
||||
if ($post === false )
|
||||
return false;
|
||||
|
@ -61,7 +56,8 @@ class WP_FEATURED2RSS {
|
|||
if ( ! $thid )
|
||||
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;
|
||||
|
||||
$asize = false;
|
||||
|
@ -79,10 +75,15 @@ class WP_FEATURED2RSS {
|
|||
arsort($candidates);
|
||||
|
||||
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']) {
|
||||
static::debug( $meta['sizes'][$potential]['file'] . ' look like a resized file, using it', 7);
|
||||
if ( isset( $meta['sizes'][$potential] ) &&
|
||||
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;
|
||||
$img = wp_get_attachment_image_src( $thid, $potential );
|
||||
break;
|
||||
|
@ -94,7 +95,7 @@ class WP_FEATURED2RSS {
|
|||
|
||||
$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)
|
||||
// in tandem with https://github.com/petermolnar/wp-resized2rss
|
||||
$cached = WP_CONTENT_DIR . '/cache/' . $meta['sizes'][$asize]['file'];
|
||||
$file = $upload_dir['basedir'] . '/' . $meta['sizes'][$asize]['file'];
|
||||
|
||||
|
@ -106,37 +107,45 @@ class WP_FEATURED2RSS {
|
|||
return false;
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* do everything to get the Post object
|
||||
*/
|
||||
public static function fix_post ( &$post = null ) {
|
||||
if ($post === null || !static::is_post($post))
|
||||
function fix_post ( &$post = null ) {
|
||||
if ($post === null || !is_post($post))
|
||||
global $post;
|
||||
|
||||
if (static::is_post($post))
|
||||
if (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) )
|
||||
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
|
||||
|
@ -147,7 +156,7 @@ class WP_FEATURED2RSS {
|
|||
* @output log to syslog | wp_die on high level
|
||||
* @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 ) )
|
||||
return false;
|
||||
|
||||
|
@ -191,12 +200,12 @@ class WP_FEATURED2RSS {
|
|||
$parent = $caller['class'] . '::' . $parent;
|
||||
|
||||
return error_log( "{$parent}: {$message}" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function fix_url ( $url, $absolute = true ) {
|
||||
*
|
||||
function fix_url ( $url, $absolute = true ) {
|
||||
// move to generic scheme
|
||||
$url = str_replace ( array('http://', 'https://'), 'https://', $url );
|
||||
|
||||
|
@ -207,10 +216,5 @@ class WP_FEATURED2RSS {
|
|||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$WP_FEATURED2RSS = new WP_FEATURED2RSS();
|
||||
|
||||
endif;
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue