Browse files

Use the Universal Pictures

  • Loading branch information...
1 parent b1fe3a9 commit 54c5e4bc399933f065cf2459b08a79d2d9f992c4 @petervanderdoes committed May 11, 2010
Showing with 125 additions and 18 deletions.
  1. +52 −2 2.8/avh-amazon.client.php
  2. +58 −2 2.8/inc/avh-amazon.admin.php
  3. +13 −12 2.8/inc/avh-amazon.shortcode.php
  4. +2 −2 2.8/inc/avh-amazon.widgets.php
View
54 2.8/avh-amazon.client.php
@@ -91,6 +91,7 @@ class AVHAmazonCore
var $db_options_name_widget_wishlist;
var $db_options_name_cached_wishlist;
var $db_options_name_cached_item;
+ var $db_options_name_uli_pics;
/**
* Are we running PHP5
@@ -143,7 +144,7 @@ function __construct ()
$this->db_options_name_widget_wishlist = 'widget_avhamazon_wishlist';
$this->db_options_name_cached_wishlist = 'avhamazon_cached_wishlist';
$this->db_options_name_cached_item = 'avhamazon_cached_items';
-
+ $this->db_options_name_uli_pics='avhamazon_uli_pics';
/**
* Default options - General Purpose
*
@@ -205,7 +206,7 @@ function __construct ()
add_filter( 'avhamazon_text', 'wptexturize' );
add_filter( 'avhamazon_text', 'convert_chars' );
add_filter( 'avhamazon_text', 'esc_html' );
-
+ add_action('init',array(&$this,'setupUniversalListImagesSizes'));
return;
}
@@ -921,6 +922,36 @@ function getImageInfo ( $imagesize, $item_result = null )
return ($img);
}
+ function getUniversalListImageInfo ( $imagesize, $item_id )
+ {
+ $picture_attachment_id = null;
+ $imageurl = $this->info['graphics_url'];
+
+ $pictures = get_option( $this->db_options_name_uli_pics );
+ $picture_size = 'avh-amazon-uli-' . strtolower( $imagesize );
+ if ( array_key_exists( $item_id, $pictures ) ) {
+ $picture_attachment_id = $pictures[$item_id];
+ $img_src = wp_get_attachment_image_src( $picture_attachment_id, $picture_size );
+ } else {
+ switch ( strtolower( $imagesize ) )
+ {
+ case 'medium' :
+ $img_src = array ($imageurl . '/no-image-160.gif', 160, 160 );
+ break;
+ case 'small' :
+ $img_src = array ($imageurl . '/no-image-75.gif', 75, 75 );
+ break;
+ case 'large' :
+ $img_src = array ($imageurl . '/no-image-500.gif', 500, 500 );
+ break;
+ default :
+ $img_src = array ($imageurl . '/no-image-160.gif', 160, 160 );
+ break;
+ }
+ }
+
+ return ($img_src);
+ }
/**
* Get the options for the widget
*
@@ -1057,6 +1088,25 @@ function handleCssFile ( $handle, $cssfile )
}
}
+ function setupUniversalListImagesSizes(){
+ $thumbnails = array (
+ 'avh-amazon-uli-medium' => array(
+ 'height' => 160,
+ 'width' => 160,
+ 'crop' => false),
+ 'avh-amazon-uli-small' => array(
+ 'height' => 75,
+ 'width' => 75,
+ 'crop' => false),
+ 'avh-amazon-uli-large' => array(
+ 'height' => 500,
+ 'width' => 500,
+ 'crop' => false));
+ foreach ( $thumbnails as $name => $value ) {
+ add_image_size ( $name, $value['width'],$value['height'],$value['crop'] );
+ }
+
+ }
} //End Class avh_amazon
View
60 2.8/inc/avh-amazon.admin.php
@@ -269,9 +269,57 @@ function pageAVHAmazonTools ()
echo '<input class="button-secondary" type="submit" value="Clear" name="submitButton" autocomplete="on" /></p>';
echo '</td></tr>';
echo '</tbody></table>';
- echo '<div id="avhamazonwishlistoutputclear">';
- echo '</div>';
+ echo '</form>';
+
+ echo '<h3>Get Pictures for Universal List items in the cache</h3>';
+ $pictures = get_option( $this->core->db_options_name_uli_pics );
+ if ( isset( $action ) && 'uli-pictures-update' == $action ) {
+ $wishlist = get_option( $this->core->db_options_name_cached_wishlist );
+ if ( 0 <= count( $wishlist ) ) {
+ foreach ( $wishlist as $wishlist_id => $value ) {
+ foreach ( $value['list']['Lists']['List']['ListItem'] as $key => $item ) {
+ if ( isset( $item['UniversalListItem'] ) ) {
+ $item_id = $item['ListItemId'];
+ if ( ! isset( $pictures[$item_id] ) ) {
+
+ // Download the image
+ $tmp_file = download_url( $item['UniversalListItem']['ImageUrl'] );
+ if ( ! is_wp_error( $tmp_file ) ) {
+ // Determine the mimetype and set the approriate extension
+ $imgstats = @getimagesize( $tmp_file );
+ $ext = image_type_to_extension( $imgstats[2] );
+
+ // Upload the picture into the media Library
+ $file_array['name'] = strtolower( $item['ListItemId'] . $ext );
+ $file_array['tmp_name'] = $tmp_file;
+ $desc = 'AVH Amazon - Universal List Item - ' . $file_array['name'];
+ $picture_src = media_handle_sideload( $file_array, 0, $desc );
+ $picture_attachment_id = $this->getIdByGuid( $picture_src );
+ if ( ! is_wp_error( $picture_attachment_id ) ) {
+ // Associate the item_id with the attachement.
+ $pictures[$item_id] = $picture_attachment_id;
+ update_option( $this->core->db_options_name_uli_pics, $pictures );
+
+ } else {
+ @unlink( $file_array['tmp_name'] );
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ echo '<form id="uli-pictures" action=' . $this->core->getBackLink() . ' method="post">';
+ wp_nonce_field( 'avhamazon-tools' );
+ echo '<table class="form-table"><tbody><tr><td>';
+ $uli_pictures = is_array( $pictures ) ? count( $pictures ) : 0;
+ echo '<p>Number of pictures: ' . $uli_pictures . '</p>';
+ echo '<p><input name="action" value="uli-pictures-update" type="hidden" />';
+ echo '<input class="button-secondary" type="submit" value="Get Pictures" name="submitButton" autocomplete="on" /></p>';
+ echo '</td></tr>';
+ echo '</tbody></table>';
echo '</form>';
@@ -741,5 +789,13 @@ function getNiceTitleOptions ( $id = '' )
}
return 'Unknown';
}
+
+ function getIdByGuid($picture_src) {
+ global $wpdb;
+
+ $mysql = 'SELECT ID from '.$wpdb->posts.' where guid="'.$picture_src.'"';
+ $result = $wpdb->get_row($mysql);
+ return $result->ID;
+ }
}
?>
View
25 2.8/inc/avh-amazon.shortcode.php
@@ -109,7 +109,7 @@ function handleShortcode ( $atts, $content = null )
foreach ( $Item_keys as $value ) {
$Item = $list_result['Lists']['List']['ListItem'][$value];
}
- if (!isset($Item['UniversalListIten'])) {
+ if (!isset($Item['UniversalListItem'])) {
$attrs['asin'] = $Item['Item']['ASIN'];
} else {
$attr['asin']='UniversalListItem';
@@ -122,12 +122,12 @@ function handleShortcode ( $atts, $content = null )
for ($x=0; $x<=count ( $list_result['Lists']['List']['ListItem'])-1; $x+=$attrs['col'] ) {
$return .= '<tr>';
for ($i=1; $i<=$attrs['col']; $i++) {
- $value=$list_result['Lists']['List']['ListItem'][$x+$i-1];
- if (!isset($value['UniversalListItem'])) {
- $attrs['asin'] = $value['Item']['ASIN'];
+ $item=$list_result['Lists']['List']['ListItem'][$x+$i-1];
+ if (!isset($item['UniversalListItem'])) {
+ $attrs['asin'] = $item['Item']['ASIN'];
list ( $oneresult, $error ) = $this->shortcodeAsin( $attrs, $content, $associatedid, false );
} else {
- $oneresult = $this->shortcodeUniversalListItem($attrs,$value['UniversalListItem'], $content, false);
+ $oneresult = $this->shortcodeUniversalListItem($attrs,$item, $content, false);
}
$return .= '<td>'.$oneresult .'</td>';
}
@@ -140,7 +140,7 @@ function handleShortcode ( $atts, $content = null )
if ( $attrs['asin'] != 'UniversalListItem') {
list ( $result, $error ) = $this->shortcodeAsin( $attrs, $content, $associatedid );
} else {
- $result = $this->shortcodeUniversalListItem($attrs,$Item['UniversalListItem'], $content, false);
+ $result = $this->shortcodeUniversalListItem($attrs,$Item, $content, false);
}
if ( $error ) {
@@ -212,8 +212,9 @@ function shortcodeAsin ( $attrs, $content, $associatedid, $single=true )
return array ($return, $error );
}
- function shortcodeUniversalListItem ( $attrs, $universalitem, $content, $single = true )
+ function shortcodeUniversalListItem ( $attrs, $item, $content, $single = true )
{
+ $universalitem=$item['UniversalListItem'];
$myurl = $universalitem['ProductUrl'];
// If no content is given we use the Title from Amazon.
@@ -225,15 +226,15 @@ function shortcodeUniversalListItem ( $attrs, $universalitem, $content, $single
$return = '<a title="' . $content . '" href="' . $myurl . '">' . $content . '</a>';
break;
case 'pic' :
- $imginfo = $this->core->getImageInfo( $attrs['picsize'] );
- $return = '<div class="wp-caption alignleft"><a title="' . $content . '" href="' . $myurl . '"><img width="' . $imginfo['w'] . '" height="' . $imginfo['h'] . '" src="' . $imginfo['url'] . '" alt="' . $content . '"/></a></div>';
+ $imginfo = $this->core->getUniversalListImageInfo( $attrs['picsize'],$item['ListItemId'] );
+ $return = '<div class="wp-caption alignleft"><a title="' . $content . '" href="' . $myurl . '"><img width="' . $imginfo[1] . '" height="' . $imginfo[2] . '" src="' . $imginfo[0] . '" alt="' . $content . '"/></a></div>';
break;
case 'pic-text' :
- $imginfo = $this->core->getImageInfo( $attrs['picsize'] );
+ $imginfo = $this->core->getUniversalListImageInfo( $attrs['picsize'],$item['ListItemId'] );
if ( $single ) {
- $return = '<table style=" border: none; cellpadding: 2px; align: left"><tr><td><a title="' . $content . '" href="' . $myurl . '"><img class="alignleft" width="' . $imginfo['w'] . '" height="' . $imginfo['h'] . '" src="' . $imginfo['url'] . '" alt="' . $content . '"/></a></td><td><a title="' . $content . '" href="' . $myurl . '">' . $content . '</a></td></tr></table>';
+ $return = '<table style=" border: none; cellpadding: 2px; align: left"><tr><td><a title="' . $content . '" href="' . $myurl . '"><img class="alignleft" width="' . $imginfo[1] . '" height="' . $imginfo[2] . '" src="' . $imginfo[0] . '" alt="' . $content . '"/></a></td><td><a title="' . $content . '" href="' . $myurl . '">' . $content . '</a></td></tr></table>';
} else {
- $return = '<a title="' . $content . '" href="' . $myurl . '"><img class="alignleft" width="' . $imginfo['w'] . '" height="' . $imginfo['h'] . '" src="' . $imginfo['url'] . '" alt="' . $content . '"/></a><a title="' . $content . '" href="' . $myurl . '">' . $content . '</a>';
+ $return = '<a title="' . $content . '" href="' . $myurl . '"><img class="alignleft" width="' . $imginfo[1] . '" height="' . $imginfo[2] . '" src="' . $imginfo[0] . '" alt="' . $content . '"/></a><a title="' . $content . '" href="' . $myurl . '">' . $content . '</a>';
}
break;
default :
View
4 2.8/inc/avh-amazon.widgets.php
@@ -216,8 +216,8 @@ function widget ( $args, $instance )
}
} else {
$myurl = $Item['UniversalListItem']['ProductUrl'];
- $imginfo = $this->core->getImageInfo( $imagesize);
- echo '<a ' . $target . ' title="' . $Item['UniversalListItem']['Title'] . '" href="' . $myurl . '"><img class="wishlistimage" width="' . $imginfo['w'] . '" height="' . $imginfo['h'] . '" src="' . $imginfo['url'] . '" alt="' . $Item['UniversalListItem']['Title'] . '"/></a>';
+ $imginfo = $this->core->getUniversalListImageInfo( $imagesize, $Item['ListItemId']);
+ echo '<a ' . $target . ' title="' . $Item['UniversalListItem']['Title'] . '" href="' . $myurl . '"><img class="wishlistimage" width="' . $imginfo[1] . '" height="' . $imginfo[2] . '" src="' . $imginfo[0] . '" alt="' . $Item['UniversalListItem']['Title'] . '"/></a>';
echo '<div class="wishlistcaption">' . $Item['UniversalListItem']['Title'] . '</div>';
}
}

0 comments on commit 54c5e4b

Please sign in to comment.