(.*?)<\/Width>/", $imageMatch[2], $matches);
$width = $matches[1];
$images[strtolower($imageMatch[1])] = array(
'url' => $imageUrl,
'height' => $height,
'width' => $width
);
}
}
}
$items[strtolower($asin)] = array(
'title' => $title,
'author' => $authors,
'url' => $url,
'images' => $images
);
}
return $items;
}
}
class AMZSCShowcase {
var $_identifier = '';
var $_name = 'New Showcase';
var $_locale = 'us';
var $_associateId = '';
var $_displayNum = 0;
var $_sortMethod = 'normal';
var $_items = array();
var $_defaultItemTemplate = '';
var $_defaultShowcaseTemplate = '[showcaseItems]
';
/**
* Generates a new item and adds it to the showcase
*
*/
function AddItem() {
$item = array(
'identifier' => $this->GenerateIdentifier(),
'asin' => '',
'imageSize' => 'small',
'template' => '',
);
$this->_items[$item['identifier']] = $item;
return $item['identifier'];
}
/**
* Initializes a Showcase object
*
* @param string $showcaseIdentifier Identifier of the Showcase to be loaded
*/
function AMZSCShowcase($showcaseIdentifier = null) {
if (!$showcaseIdentifier) {
$this->_identifier = $this->GenerateIdentifier();
} else {
$this->_identifier = $showcaseIdentifier;
$this->LoadOptions();
}
}
/**
* Cleans up an ASIN in preparation for an API call
*
* @param string $asin ASIN to be cleaned
* @return string Cleaned ASIN
*/
function CleanAsin($asin) {
$asin = str_replace('-', '', $asin);
$asin = str_replace(' ', '', $asin);
$asin = strtolower($asin);
return $asin;
}
/**
* Generates a unique identifier
*
* @return string
*/
function GenerateIdentifier() {
return md5(uniqid(rand(), true));
}
/**
* Return associate id
*
* @return string
*/
function GetAssociateId() {
return $this->_associateId;
}
/**
* Return display num
*
* @return integer
*/
function GetDisplayNum() {
return $this->_displayNum;
}
/**
* Return identifier
*
* @return string
*/
function GetIdentifier() {
return $this->_identifier;
}
/**
* Return items
*
* @return array
*/
function GetItems() {
return $this->_items;
}
/**
* Return locale
*
* @return string
*/
function GetLocale() {
return $this->_locale;
}
/**
* Return name
*
* @return string
*/
function GetName() {
return $this->_name;
}
/**
* Creates an array of all showcase options
*
*/
function GetOptionsArray() {
$options = array(
'associateid' => $this->_associateId,
'displaynum' => $this->_displayNum,
'identifier' => $this->_identifier,
'items' => $this->_items,
'locale' => $this->_locale,
'name' => $this->_name,
'sortmethod' => $this->_sortMethod
);
return $options;
}
/**
* Utility function for determining the URL for posting forms to
*/
function GetPostUrl() {
$page = basename(__FILE__);
if (isset($_GET['page']) && !empty($_GET['page'])) {
$page = preg_replace('[^a-zA-Z0-9\.\_\-]', '', $_GET['page']);
}
return $_SERVER['PHP_SELF'] . "?page=" . $page;
}
/**
* Return sort method
*
* @return string
*/
function GetSortMethod() {
return $this->_sortMethod;
}
/**
* Generates options HTML for an individual showcase item
*
* @return string Generated HTML
*/
function ItemOptionsFormHtml($itemIdentifier = null) {
if (isset($this->_items[$itemIdentifier])) {
$item = $this->_items[$itemIdentifier];
$selectedImageSize = array('swatch'=>'', 'small'=>'', 'tiny'=>'', 'medium'=>'', 'large'=>'');
$selectedImageSize[$item['imageSize']] = 'selected';
$template = !empty($item['template']) ? stripslashes($item['template']) : $this->_defaultItemTemplate;
$preview = '';
if (isset($item['amazonCache']['data']['images']['thumbnail']['url']) && !empty($item['amazonCache']['data']['images']['thumbnail']['url'])) {
$preview = ' ';
}
$html = <<
Swatch (30px)
Small (75px)
Tiny (110px)
Medium (160px)
Large (450px+)
{$preview}
HTML;
return $html;
}
return '';
}
/**
* Removes a specific item from the showcase
*
* @param string $itemIdentifier Identifier of item to be removed
*/
function RemoveItem($itemIdentifier = null) {
if (isset($this->_items[$itemIdentifier])) {
unset($this->_items[$itemIdentifier]);
}
}
/**
* Assign all private variable values by load the showcase options from the DB
*
*/
function LoadOptions() {
$options = get_option('amzshcs');
if (isset($options['showcases'][$this->_identifier])) {
$showcaseOptions = $options['showcases'][$this->_identifier];
if (isset($showcaseOptions['associateid'])) {
$this->_associateId = $showcaseOptions['associateid'];
}
if (isset($showcaseOptions['displaynum'])) {
$this->_displayNum = $showcaseOptions['displaynum'];
}
if (isset($showcaseOptions['items'])) {
$this->_items = $showcaseOptions['items'];
}
if (isset($showcaseOptions['locale'])) {
$this->_locale = $showcaseOptions['locale'];
}
if (isset($showcaseOptions['name'])) {
$this->_name = $showcaseOptions['name'];
}
if (isset($showcaseOptions['sortmethod'])) {
$this->_sortMethod = $showcaseOptions['sortmethod'];
}
}
}
/**
* Set associate id
* @param string $associateId
* @return bool
*/
function SetAssociateId($associateId) {
return $this->_associateId = $associateId;
}
/**
* Set display num
* @param integer $displayNum
* @return bool
*/
function SetDisplayNum($displayNum) {
return $this->_displayNum = $displayNum;
}
/**
* Set identifier
* @param string $identifier
* @return bool
*/
function SetIdentifier($identifier) {
return $this->_identifier = $identifier;
}
/**
* Set items
* @param array $items
* @return bool
*/
function SetItems($items) {
return $this->_items = $items;
}
/**
* Set locale
* @param string $locale
* @return bool
*/
function SetLocale($locale) {
return $this->_locale = $locale;
}
/**
* Set name
* @param string $name
* @return bool
*/
function SetName($name) {
return $this->_name = $name;
}
/**
* Set multiple object options at once by passing an array of option values
*
* @param array $options
*/
function SetOptions($options = array()) {
foreach ($options as $option => $value) {
switch (strtolower($option)) {
case 'associateid':
$this->_associateId = trim($value);
break;
case 'displaynum':
$this->_displayNum = $value;
break;
case 'identifier':
$this->_identifier = $value;
break;
case 'items':
if (is_array($value)) {
foreach ($value as $identifier => $item) {
if (isset($item['asin'])) {
$value[$identifier]['asin'] = $this->CleanAsin($item['asin']);
}
}
$this->_items = $value;
} else {
$this->_items = array();
}
break;
case 'locale':
$this->_locale = $value;
break;
case 'name':
$this->_name = $value;
break;
case 'sortmethod':
$this->_sortMethod = $value;
break;
}
}
}
/**
* Set sort method
* @param string $sortMethod
* @return bool
*/
function SetSortMethod($sortMethod) {
return $this->_sortMethod = $sortMethod;
}
/**
* Returns the HTML for a showcase
*
* @param bool $encodeEntities If true, applies htmlspecialchars to data. Default false.
*/
function ShowcaseHtml($encodeEntities = false) {
$items = $this->_items;
if ($this->_displayNum > 0) {
shuffle($items);
$items = array_slice($items, 0, $this->_displayNum);
}
$itemHtml = '';
foreach ($items as $item) {
$itemIdentifier = $item['identifier'];
if (!empty($item['template'])) {
$itemHtml[$itemIdentifier] = stripslashes($item['template']);
} else {
$itemHtml[$itemIdentifier] = $this->_defaultItemTemplate;
}
if (is_array($item['amazonCache']['data']['author'])) {
$authors = implode(', ', $item['amazonCache']['data']['author']);
} else {
$authors = $item['amazonCache']['data']['author'];
}
if ($encodeEntities) {
$authors = htmlspecialchars($authors);
$item['amazonCache']['data']['title'] = htmlspecialchars($item['amazonCache']['data']['title']);
}
$itemHtml[$itemIdentifier] = str_replace('[itemIdentifier]', $itemIdentifier, $itemHtml[$itemIdentifier]);
$itemHtml[$itemIdentifier] = str_replace('[author]', $authors, $itemHtml[$itemIdentifier]);
$itemHtml[$itemIdentifier] = str_replace('[title]', $item['amazonCache']['data']['title'], $itemHtml[$itemIdentifier]);
$itemHtml[$itemIdentifier] = str_replace('[url]', $item['amazonCache']['data']['url'], $itemHtml[$itemIdentifier]);
$image = '';
$image_url = '';
$image_width = '';
$image_height = '';
if (!empty($item['amazonCache']['data']['images'][$item['imageSize']])) {
$image_url = $item['amazonCache']['data']['images'][$item['imageSize']]['url'];
$image_width = $item['amazonCache']['data']['images'][$item['imageSize']]['width'];
$image_height = $item['amazonCache']['data']['images'][$item['imageSize']]['height'];
$image = ' ';
}
$itemHtml[$itemIdentifier] = str_replace('[image]', $image, $itemHtml[$itemIdentifier]);
$itemHtml[$itemIdentifier] = str_replace('[image_url]', $image_url, $itemHtml[$itemIdentifier]);
$itemHtml[$itemIdentifier] = str_replace('[image_width]', $image_width, $itemHtml[$itemIdentifier]);
$itemHtml[$itemIdentifier] = str_replace('[image_height]', $image_height, $itemHtml[$itemIdentifier]);
}
$html = $this->_defaultShowcaseTemplate;
$html = str_replace('[showcaseIdentifier]', $this->_identifier, $html);
$html = str_replace('[showcaseItems]', implode('', $itemHtml), $html);
return $html;
}
/**
* Generates options HTML for the showcase
*
* @return string Generated HTML
*/
function ShowcaseOptionsFormHtml($postUrl = null) {
if (!$postUrl) {
$postUrl = $this->GetPostUrl();
}
$selectedLocale = array('us'=>'', 'uk'=>'', 'de'=>'', 'jp'=>'', 'fr'=>'', 'ca'=>'');
$selectedLocale[$this->_locale] = 'selected';
$selectedDisplayNum = array(0=>'', 1=>'', 2=>'', 3=>'', 4=>'', 5=>'', 6=>'', 7=>'', 8=>'', 9=>'', 10=>'');
$selectedDisplayNum[$this->_displayNum] = 'selected';
$selectedSortMethod = array('normal'=>'', 'random'=>'');
$selectedSortMethod[$this->_sortMethod] = 'selected';
$html = <<
{$this->_name} click to toggle options
HTML;
return $html;
}
/**
* Update Amazon data for all showcase items
*
*/
function UpdateShowcaseAmazonData() {
// Extact all of the ASINs from the items array
$asins = array();
foreach ($this->_items as $item) {
$asins[] = $item['asin'];
}
if (!empty($asins)) {
// Query Amazon for data
$amazonData = $GLOBALS['AMZSCAmazon']->GetItems($asins, $this->_associateId, $this->_locale);
foreach ($this->_items as $identifier => $item) {
if (!empty($amazonData[$item['asin']])) {
$this->_items[$identifier]['amazonCache'] = array(
'lastUpdated' => date('Y-m-d H:i:s'),
'data' => $amazonData[$item['asin']]
);
}
}
}
}
}
class AMZSCWidget extends WP_Widget {
/** constructor */
function AMZSCWidget() {
parent::WP_Widget(false, $name = 'Amazon Showcase');
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
extract($args);
if (!empty($instance['showcase_id'])) {
$title = apply_filters('widget_title', $GLOBALS['AmazonShowcase']->_showcases[$instance['showcase_id']]->_name);
?>
_showcases;
if (!empty($showcases)) {
?>
Choose a Showcase:
$showcase) { ?>
>_name; ?>
You have not created any showcases yet!
You can create showcases in Settings > Amazon Showcase .
DisplayShowcase($showcaseIdentifier);
}
if (!ini_get('allow_url_fopen') && !function_exists('curl_init')) {
echo 'Amazon Showcase is unable to function within your current server settings. Please either set "allow_url_fopen" to 1, or enable cURL.
';
} else if (!function_exists('http_build_query')) {
echo 'Amazon Showcase is unable to function with your current version of PHP. Please upgrade to PHP 5 or greater.
';
} else if (!function_exists('hash_hmac') || base64_encode(hash_hmac("sha256", 'hashCheck', 'hashCheck', true)) != 'bCrWmap2WxEyY8MdKLibMiZoi8U/jZ1RftMm1ju0jxU=') {
echo 'Amazon Showcase requires access to hash_hmac()/sha256.
';
} else {
// Handle all ajax requests
if (isset($_REQUEST["amzshcs_ajax_action"])) {
AmazonShowcase::Enable();
switch ($_REQUEST['amzshcs_ajax_action']) {
case 'ajax_preview':
$GLOBALS['AmazonShowcase']->AjaxGenerateItemPreview($_REQUEST["amzshcs_asin"]);
break;
case 'addshowcase':
$GLOBALS['AmazonShowcase']->AjaxAddShowcase();
break;
case 'removeshowcase':
$GLOBALS['AmazonShowcase']->AjaxRemoveShowcase($_REQUEST["amzshcs_identifier"]);
break;
case 'addshowcaseitem':
$GLOBALS['AmazonShowcase']->AjaxAddShowcaseItem($_REQUEST["amzshcs_showcase_identifier"]);
break;
case 'removeshowcaseitem':
$GLOBALS['AmazonShowcase']->AjaxRemoveShowcaseItem($_REQUEST["amzshcs_showcase_identifier"], $_REQUEST["amzshcs_item_identifier"]);
break;
}
} else {
// If Wordpress was initialized correctly, go ahead and enable the plugin
if (defined('ABSPATH') && defined('WPINC')) {
add_action('init', array('AmazonShowcase', 'Enable'), 1000, 0);
add_action('widgets_init', create_function('', 'return register_widget("AMZSCWidget");'));
}
}
}
?>