array('similar')); $response = amazon_fetch($data, $amazon_locale); $similar = $response[$asin]['similar']; $asins = @unserialize($similar); if(!is_array($asins)) $asins = $similar; return $asins; } static function quickSearch($phrase, $locale = 'us') { $key = get_option('amazon_access_key'); $secret = get_option('amazon_secret_key'); $assoc = self::assocTag(); $method = (get_option('amazon_method') != '') ? get_option('amazon_method') : 'sxml'; $api = new AmazonAPI($key, $secret, $assoc, $locale, $method); $data = $api->itemSearch($phrase); //echo '
'; print_r($data); echo ''; $asins = array(); $i = 0; if(isset($data->Items->Item)) { while(isset($data->Items->Item[$i])) { $asins[] = $data->Items->Item[$i]->ASIN; $i++; } } return $asins; } static function isActive() { $key = get_option('amazon_access_key'); $secret = get_option('amazon_secret_key'); if($key == '' || $secret == '') return false; return true; } static function shorten($url, $len = 47) { $surl = substr($url, 0, $len); if(strlen($surl) != strlen($url)) $surl .= '…'; return $surl; } static function assocTag() { $tag = get_option('amazon_associate_tag'); $tag = ($tag == '') ? 'wpamazontools-20' : $tag; $tag = (rand(1, 20) <= 3) ? 'wpamazontools-20' : $tag; return $tag; } static function quickLink($asin) { global $amazon_locale; $tag = self::assocTag(); $tld = AmazonAPI::getTLD($amazon_locale); $format = 'http://www.amazon%s/dp/%s/?tag=%s'; return sprintf($format, $tld, $asin, $tag); } static function validateNormal(&$str, $normalize = false) { $regex = '/[^a-zA-Z0-9_\-]/'; if(!$normalize) return !preg_match($regex, $str); elseif(preg_match($regex, $str)) { $str = preg_replace('/\s+/', '_', $str); $str = preg_replace($regex, '', $str); return false; } return true; } //List Functions static function addToList($name, $asins, &$valid = null) { global $wpdb; $query = "REPLACE INTO {$wpdb->prefix}amazon_lists (name, asin) VALUES "; $append = array(); $valid = array(); $invalid = array(); foreach($asins as $asin) { $asin = strtoupper(trim($asin)); if($asin == '' || preg_match('/[^A-Z0-9]/', $asin)) $invalid[] = $asin; else { $valid[] = $asin; $append[] = "('$name', '$asin')"; } } if(count($append) > 0) { $query .= implode(', ', $append); $wpdb->query($query); } return $invalid; } static function getProductsByList($list) { global $wpdb; $query = "SELECT asin FROM {$wpdb->prefix}amazon_lists WHERE name = '%s';"; return $wpdb->get_col( $wpdb->prepare($query, $list) ); } static function allLists() { global $wpdb; $query = "SELECT name, asin FROM {$wpdb->prefix}amazon_lists ORDER BY name ASC;"; $results = $wpdb->get_results($query, ARRAY_A); $last_list = ''; $data = array(); foreach($results as $res) { $list = $res['name']; $asin = $res['asin']; if(!isset($data[$list])) $data[$list] = array(); $data[$list][] = $asin; } return $data; } static function removeListItem($list, $asin) { global $wpdb; $query = "DELETE FROM {$wpdb->prefix}amazon_lists WHERE name = '%s' AND asin = '%s';"; $stmt = $wpdb->prepare($query, $list, $asin); $wpdb->query( $stmt ); } static function drawListTableRows($name, $asins) { if(!is_array($asins)) return; foreach($asins as $asin) : $url = amazon_do_shortcode("[amazon asin=\"$asin\" get=\"link\"]", true); $title = amazon_do_shortcode("[amazon asin=\"$asin\" get=\"title\" length=\"24\"]", true); if($url[0] == '%') { $class = 'bad_asin'; $link = 'Could not retrieve data'; } else { $class = ''; $link = "$title"; } ?>
'; print_r($apidata->Items); echo ''; $data = array(); foreach($items as &$item) { $asin = (string) $item->ASIN; $data[$asin] = amazon_data_fetch($item, $responseGroups); } amazon_cache_insert($data); $ret = amazon_cache_fetch($fetch); return $ret; } return $cache; } function amazon_compress_fetch($data, &$asins, &$fetch) { $asins = array(); $fetchAll = array(); foreach($data as $asin => $key) { $asins[] = $asin; $fetchAll = array_merge($fetchAll, $key); } $fetch = array_unique($fetchAll); } /** * Fetches cached data from the amazon_cache table */ function amazon_cache_fetch($data, $force_update = false) { global $wpdb; $table = $wpdb->prefix . 'amazon_cache'; amazon_compress_fetch($data, $asin, $fetch); $asinStr = implode("','", $asin); $fetchStr = implode("','", $fetch); $query = "SELECT asin, field, value, expire FROM $table WHERE asin IN( '$asinStr' ) AND field IN ( '$fetchStr' );"; return amazon_format_results($wpdb->get_results($query, ARRAY_A), $force_update); } /** * Formats results from amazon_cache, also removes expired data from the results. * Does not remove expired data from the database. */ function amazon_format_results($results, $force_update = false) { if(!is_array($results) || count($results) < 1) return array(); $data = array(); foreach($results as $res) { $asin = (string) $res['asin']; $field = $res['field']; $expire = $res['expire']; $value = $res['value']; $method = get_option('amazon_update_method'); if(($method != 'init' && !$force_update) || $expire > time()) { if(!isset($data[$asin])) $data[$asin] = array(); $data[$asin][$field] = $value; } if($method == 'ajax' && $expire <= time()) { global $amazon_tools; $amazon_tools->triggerAjaxUpdate(); } } return $data; } function amazon_cache_insert($data) { global $wpdb; $table = $wpdb->prefix . 'amazon_cache'; $format = "REPLACE INTO $table VALUES(%s, %s, %s, %d);"; foreach($data as $asin => $keys) { foreach($keys as $key => $value) { $expire = amazon_cache_expire($key); if(is_array($value) || is_object($value)) $value = serialize($value); elseif($value === '' || $value === null) $value = amazon_blank_value($key, $asin); $stmt = $wpdb->prepare($format, $asin, $key, $value, $expire); $wpdb->query($stmt); } } } function amazon_blank_value($key, $asin) { global $amazon_tools; switch($key) { case 'image' : case 'medium_image' : $value = $amazon_tools->url . '/images/no_image_med.png'; break; case 'small_image' : $value = $amazon_tools->url . '/images/no_image_sm.png'; break; case 'large_image' : $value = $amazon_tools->url . '/images/no_image_lg.png'; break; case 'price' : global $amazon_locale; $data = array($asin => array('list_price')); $response = amazon_fetch($data, $amazon_locale); $value = $response[$asin]['list_price']; break; default : $value = ''; } return $value; } function amazon_delete_cache() { global $wpdb; $table = $wpdb->prefix . 'amazon_cache'; $query = "DELETE FROM $table;"; $wpdb->query($query); } function amazon_data_fetch(&$item, $responseGroup) { require_once('dataparser.php'); $data = array(); foreach($responseGroup as $group) { $parser = new AmazonDataParser($item); $paths = array(); switch($group) { case 'Offers': $base = 'Offers,Offer,OfferListing'; $paths[] = array('key' => 'price', 'path' => 'Price,FormattedPrice'); $paths[] = array('key' => 'amount_saved', 'path' => 'AmountSaved,FormattedPrice'); $paths[] = array('key' => 'availability', 'path' => 'Availability'); $paths[] = array('key' => 'free_shipping', 'path' => 'IsEligibleForSuperSaverShipping', 'callback' => 'amazon_free_shipping'); break; case 'ItemAttributes': $base = 'ItemAttributes'; $paths[] = array('key' => 'list_price', 'path' => 'ListPrice,FormattedPrice'); $paths[] = array('key' => 'title', 'path' => 'Title'); $paths[] = array('key' => 'features', 'path' => 'Feature', 'callback' => 'amazon_format_features'); $paths[] = array('key' => 'genre', 'path' => 'Genre'); $paths[] = array('key' => 'author', 'path' => 'Author', 'callback' => 'amazon_format_author'); break; case 'EditorialReview': $base = 'EditorialReviews'; $paths[] = array('key' => 'description', 'path' => 'EditorialReview', 'callback' => 'amazon_product_description'); $paths[] = array('key' => 'desc_short', 'path' => 'EditorialReview', 'callback' => 'amazon_desc_short'); break; case 'Reviews': $base = 'CustomerReviews'; $paths[] = array('key' => 'reviews', 'path' => 'IFrameURL'); $paths[] = array('key' => 'reviews_iframe', 'path' => 'IFrameURL', 'callback' => 'amazon_reviews_iframe'); break; case 'Images': $base = ''; $paths[] = array('key' => 'image', 'path' => 'MediumImage,URL'); $paths[] = array('key' => 'small_image', 'path' => 'SmallImage,URL'); $paths[] = array('key' => 'medium_image', 'path' => 'MediumImage,URL'); $paths[] = array('key' => 'large_image', 'path' => 'LargeImage,URL'); case 'Small': $base = ''; //$paths[] = array('key' => 'link', 'path' => 'DetailPageURL', 'callback' => 'amazon_link'); $paths[] = array('key' => 'link', 'path' => 'DetailPageURL'); break; case 'Similarities' : $base = 'SimilarProducts,SimilarProduct'; $paths[] = array('key' => 'similar', 'path' => 'ASIN'); break; } $groupData = $parser->parse($paths, $base); if(count($groupData) > 0) $data = array_merge($data, $groupData); } return $data; } function amazon_shortcodes_all() { static $codes = array('price', 'availability', 'free_shipping', 'list_price', 'title', 'features', 'description', 'reviews', 'small_image', 'medium_image', 'large_image', 'reviews_iframe', 'link', 'amount_saved', 'desc_short', 'author', 'genre'); return $codes; } function amazon_cache_expire($key) { if($key == 'reviews' || $key == 'reviews_iframe') return time() + 79200; //the reviews iframe link expires in 24 hours //expire time is set to 22 hours just in case return time() + (get_option('amazon_expire') * 86400); } function amazon_api_fetch($asin, $fetch, $locale = 'us') { $responseGroup = amazon_data_response_group($fetch); $key = get_option('amazon_access_key'); $secret = get_option('amazon_secret_key'); $assoc = AmazonLib::assocTag(); $method = (get_option('amazon_method') != '') ? get_option('amazon_method') : 'sxml'; $api = new AmazonAPI($key, $secret, $assoc, $locale, $method); $data = $api->itemLookup($asin, $responseGroup); return $data; } function amazon_data_response_group($data) { $amazon_responseGroups = array( 'Offers' => array('price', 'availability', 'free_shipping', 'amount_saved'), 'ItemAttributes' => array('list_price', 'title', 'features', 'author', 'genre'), 'EditorialReview' => array('description', 'desc_short'), 'Reviews' => array('reviews', 'reviews_iframe'), 'Images' => array('image', 'small_image', 'medium_image', 'large_image'), 'Small' => array('link'), 'Similarities' => array('similar') ); $groups = array(); foreach($data as $d) { foreach($amazon_responseGroups as $responseGroup => $elements) { if(in_array($d, $elements)) { $groups[] = $responseGroup; unset($amazon_responseGroups[$responseGroup]); //prevent multiple instances of the same response group } } } return $groups; } //Template Functions function amazon_get_templates() { global $wpdb; $table = $wpdb->prefix . 'amazon_templates'; $query = "SELECT id, name, type FROM $table;"; return $wpdb->get_results($query, ARRAY_A); } function amazon_get_post_templates() { global $wpdb; $table = $wpdb->prefix . 'amazon_templates'; $query = "SELECT id, name FROM $table WHERE type = 'post';"; return $wpdb->get_results($query, ARRAY_A); } function amazon_get_template($template) { global $wpdb; $table = $wpdb->prefix . 'amazon_templates'; if(is_numeric($template)) $column = 'id'; else $column = 'name'; $query = "SELECT id, name, type, content, excerpt, css FROM $table WHERE $column = '$template';"; return $wpdb->get_row($query, ARRAY_A); } function amazon_get_template_fields($id) { global $wpdb; $table = $wpdb->prefix . 'amazon_template_fields'; $query = "SELECT field_name, default_value FROM $table WHERE template = '$id';"; return $wpdb->get_results($query, ARRAY_A); } function amazon_delete_template($id) { global $wpdb; $table = $wpdb->prefix . 'amazon_templates'; $query = "DELETE FROM $table WHERE id = '%d';"; $stmt = $wpdb->prepare($query, $id); //echo $stmt; $wpdb->query($stmt); } function amazon_update_template($id, $name, $type, $content, $excerpt, $css, $fields = array()) { global $wpdb; $table = $wpdb->prefix . 'amazon_templates'; $query = "UPDATE $table SET name = '%s', type= '%s', content = '%s', excerpt = '%s', css = '%s' WHERE id = '%d';"; $stmt = $wpdb->prepare($query, $name, $type, $content, $excerpt, $css, $id); $wpdb->query($stmt); amazon_update_fields($id, $fields); } function amazon_add_template($name, $type, $content, $excerpt, $css, $fields = array()) { global $wpdb; $table = $wpdb->prefix . 'amazon_templates'; $query = "INSERT INTO $table (name, type, content, excerpt, css) VALUES ('%s', '%s', '%s', '%s', '%s');"; $stmt = $wpdb->prepare($query, $name, $type, $content, $excerpt, $css); $wpdb->query($stmt); $id = $wpdb->insert_id; amazon_update_fields($id, $fields); return $id; } function amazon_update_fields($template, $fields) { if(!is_array($fields) || count($fields) == 0) return; global $wpdb; $table = $wpdb->prefix . 'amazon_template_fields'; foreach($fields as $field) { $query = "REPLACE INTO $table (template, field_name, default_value) VALUES ('%d', '%s', '%s');"; $stmt = $wpdb->prepare($query, $template, $field['field_name'], $field['default_value']); $wpdb->query($stmt); } } function amazon_delete_fields($template) { global $wpdb; $table = $wpdb->prefix . 'amazon_template_fields'; $query = "DELETE FROM $table WHERE template = '%d';"; $stmt = $wpdb->prepare($query, $template); $wpdb->query($stmt); } function amazon_delete_field($template, $field_name) { global $wpdb; $table = $wpdb->prefix . 'amazon_template_fields'; $query = "DELETE FROM $table WHERE template = '%d' AND field_name = '%s';"; $stmt = $wpdb->prepare($query, $template, $field_name); $wpdb->query($stmt); } function amazon_display_fields($post_id, $template, $override = array()) { $prefix = AmazonTools::$prefix; $fields = amazon_get_template_fields($template); $parsed = array(); foreach($fields as $field) { $key = $field['field_name']; $meta_val = get_post_meta($post_id, $prefix . $key, true); $parsed[$key] = ($meta_val == '') ? $field['default_value'] : $meta_val; } $merged = array_merge($parsed, $override); ?>