Adds a custom image url field in RSS2 output of Amazon Auto Links (WordPress plugin) product RSS2 feeds.
<?php | |
/** | |
* Plugin Name: Amazon Auto Links - Custom RSS ImageURL Field | |
* Plugin URI: http://en.michaeluno.jp/amazon-auto-links | |
* Description: Adds a custom image url field in RSS2 output of Amazon Auto Links product RSS2 feeds. Requires Amazon Auto Links 3.5.4 or above. | |
* Author URI: http://michaeluno.jp | |
* Version: 0.0.1 | |
* Author: Michael Uno | |
*/ | |
new AmazonAutoLinks_CustomRSS2Field( | |
"imageurl", // tag name | |
"", // the name space url e.g. http://my-namespace.sample/imageurl | |
500 // image size in pixel. default: 500, set 0 to use the unit's image size | |
); | |
class AmazonAutoLinks_CustomRSS2Field { | |
private $___sTagName = ''; | |
private $___sNamespaceURL = ''; | |
private $___iImageSize = 500; | |
public function __construct( $sTagName, $sNamespaceURL, $iImageSize=500 ) { | |
$this->___sTagName = $sTagName; | |
$this->___sNamespaceURL = $sNamespaceURL; | |
$this->___iImageSize = $iImageSize; | |
add_action( 'aal_action_rss2_namespace', array( $this, '_doOnAALAction_RSS2Namespace' ) ); | |
add_action( 'aal_action_rss2_item', array( $this, '_doOnAALAction_RSS2Item' ) ); | |
} | |
public function _doOnAALAction_RSS2Namespace() { | |
if ( ! $this->___sNamespaceURL || ! $this->___sTagName ) { | |
return; | |
} | |
echo 'xmlns:' . $this->___sTagName . '="' . $this->___sNamespaceURL. '"'; | |
} | |
public function _doOnAALAction_RSS2Item( $aProduct ) { | |
echo '<' . $this->___sTagName . '>' | |
. $this->___setImageSize( $aProduct[ "thumbnail_url" ], $this->___iImageSize ) | |
. '</' . $this->___sTagName . '>'; | |
} | |
/** | |
* Returns the resized image url. | |
* | |
* Examples | |
* _SL160_ -> _SL500_ | |
* _SS160_ -> _SS500_ | |
*/ | |
private function ___setImageSize( $strImgURL, $numImageSize ) { | |
if ( ! $numImageSize ) { | |
return $strImgURL; | |
} | |
return preg_replace( '/(?<=_S)([LS])(\d+){3}(?=_)/i', 'S${2}'. $numImageSize . '', $strImgURL ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment