FYI * Etsy Shop Plugin
TimeMachine - Slowly exploring the future » Blog Archive » Wordpress Etsy Widget
I've just installed it on my two blogs and it's perfect:
Bareket 57
http://astrologysignssite.com/
P.S.
If you want the links to your Etsy Store to open in a new window then add: target="_blank" to your href code in the "Edit Plugins" section.
<?php
/*
Plugin Name: Etsy Widget
Plugin URI: TimeMachine - Slowly exploring the future » Blog Archive » Wordpress Etsy Widget
Description: This plugin adds a widget similar to etsy mini but is not based on an iframe or flash and allows you to control the styling in your theme.
Version: 1.0.8
Author: Joshua Coxwell
Author URI: TimeMachine - Slowly exploring the future
*/
function widget_etsywp_register() {
//If you modify this source in any way please obtain your own
//etsy api key from: developer.etsy.com
define('ETSY_API_KEY', 'wtdqy35a5ek4pkhsxc6rqgn3');
define('ETSY_CACHE_LIFE', 21600);//6 hours in seconds
function widget_etsywp($args) {
extract($args);
$options = get_option('widget_etsywp');
$title = $options['title'];
$etsyuserid = $options['etsyuserid'];
$cacheloc = $options['cacheloc'];
$etsyitems = widget_etsywp_getEtsyItems( $etsyuserid, $cacheloc );
echo $before_widget;
if (!empty($options['title'])) echo $before_title . $title . $after_title;
if ($etsyuserid) {
widget_etsywp_createTable( $etsyitems, $options );
}
echo $after_widget;
}
function widget_etsywp_control() {
$options = get_option('widget_etsywp');
if ( isset($_POST["etsywp-submit"]) ) {
$newoptions['title'] = strip_tags(stripslashes($_POST["etsywp-title"]));
$newoptions['etsyuserid'] = $_POST["etsywp-etsyuserid"];
$newoptions['rowcount'] = $_POST["etsywp-rowcount"];
$newoptions['colcount'] = $_POST["etsywp-colcount"];
$newoptions['order'] = $_POST["etsywp-order"];
$newoptions['imgsize'] = $_POST["etsywp-imgsize"];
$newoptions['showstorelink'] = $_POST["etsywp-showstorelink"];
$newoptions['showetsylogo'] = $_POST["etsywp-showetsylogo"];
$newoptions['linktext'] = $_POST["etsywp-linktext"];
$newoptions['cacheloc'] = $_POST["etsywp-cacheloc"];
$options = $newoptions;
//note: update_option automatically escapes for sql insertion.
update_option('widget_etsywp', $options);
}
if ( $options != $newoptions ) {
}
$title = attribute_escape($options['title']);
$etsyuserid = attribute_escape($options['etsyuserid']);
$showetsylogo = $options['showetsylogo'];
$showstorelink = $options['showstorelink'];
$linktext = attribute_escape($options['linktext']);
$cacheloc = attribute_escape($options['cacheloc']);
if(empty($cacheloc)) $cacheloc = sys_get_temp_dir();
$orderoptions = array("Most Recent", "Random");
if(empty($options['imgsize'])) $options['imgsize'] = "75x75";
$imgsizeoptions = array("75x75", "170x135");
?>
<p><label for="etsywp-title"><?php _e('Title:'); ?> <input class="widefat" id="etsywp-title" name="etsywp-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="etsywp-etsyuserid"><?php _e('Etsy Shop Name:'); ?> <input class="widefat" id="etsywp-etsyuserid" name="etsywp-etsyuserid" type="text" value="<?php echo $etsyuserid; ?>" /></label></p>
<table>
<tr><td> <label for="etsywp-rowcount"><?php _e('Rows:'); ?></label></td>
<td><select id="etsywp-rowcount" name="etsywp-rowcount">
<?php widget_etsywp_createOptions( array_keys(array_fill(1, 5, 0)) , $options['rowcount']); ?>
</td></tr>
<tr><td><label for="etsywp-colcount"><?php _e('Columns:'); ?></label></td>
<td><select id="etsywp-colcount" name="etsywp-colcount">
<?php widget_etsywp_createOptions( array_keys(array_fill(1, 5, 0)), $options['colcount']); ?>
</td></td>
<tr><td><label for="etsywp-order"><?php _e('Image Size:'); ?></label></td>
<td><select id="etsywp-imgsize" name="etsywp-imgsize">
<?php widget_etsywp_createOptions( $imgsizeoptions, $options['imgsize']); ?>
</td></td>
<tr><td><label for="etsywp-order"><?php _e('Items:'); ?></label></td>
<td><select id="etsywp-order" name="etsywp-order">
<?php widget_etsywp_createOptions( $orderoptions, $options['order']); ?>
</td></td></table><br/>
<p><label for="etsywp-showetsylogo"><input id="etsywp-showetsylogo" name="etsywp-showetsylogo" type="checkbox" value="1"<?php if ($showetsylogo) { echo ' checked="checked"'; } ?> /> Show Etsy Logo</label></p>
<p><label for="etsywp-showstorelink"><input id="etsywp-showstorelink" name="etsywp-showstorelink" type="checkbox" value="1"<?php if ($showstorelink) { echo ' checked="checked"'; } ?> /> Show Store Link</label></p>
<p><label for="etsywp-linktext"><?php _e('Link Text:'); ?> <input class="widefat" id="etsywp-linktext" name="etsywp-linktext" type="text" value="<?php echo $linktext; ?>" /></label></p>
<p><label for="etsywp-cacheloc"><?php _e('Cache Location:'); ?> <input class="widefat" id="etsywp-cacheloc" name="etsywp-cacheloc" type="text" value="<?php echo $cacheloc; ?>" /></label></p>
<input type="hidden" id="etsywp-submit" name="etsywp-submit" value="1" />
<?php
}
function widget_etsywp_createOptions( &$options, $so ){
$r = '';
foreach ($options as $o){
$r .= '<option value="'.$o.'"';
if($o == $so){
$r .= ' selected="selected"';
}
$r .= '> '.$o.' </option>';
}
echo $r;
}
function widget_etsywp_createTable( &$items, &$options ){
if($options['order'] == 'Random') shuffle($items);
$rows = $options['rowcount'];
$cols = $options['colcount'];
$count = count($items);
$r = '<div style="height: 5px;"></div><div class="etsy"><table>';
for ($i=0; $i<$rows; $i++) {
$r .= '<tr>';
for ($j=0; $j<$cols; $j++){
$r .= '<td>';
$ij = $i * $cols + $j;
if($ij < $count){
$item = $items[$ij];
$imgurl = widget_etsywp_getImgURL($item, $options['imgsize']);
$r .= '<a href="'.$item->url.'" title="'.$item->title.'" target="_blank"><img src="'.$imgurl.'"/></a>' ;
}
$r .= '</td>';
}
$r .= '</tr>';
}
if($options['showstorelink'] && $count > 0){
$shopid = attribute_escape($options['etsyuserid']);
$r .= '<tr><td colspan="'.$cols.'"><div style="text-align: center;">';
if ($options['showetsylogo']) $r .= widget_etsywp_etsylogo();
$r .= '<a title="Etsy Store" href="http://'.$shopid.'.etsy.com" target="_blank">'.$options['linktext'].'</a>';
$r .= '</div></td></tr>';
}
$r .= '</table></div>';
echo $r;
}
function widget_etsywp_getImgURL(&$item, $size){
$result = '';
switch($size){
case "170x135":
$result = $item->Images[0]->url_170x135;
break;
case "75x75":
default:
$result = $item->Images[0]->url_75x75;
break;
}
return $result;
}
function widget_etsywp_etsylogo(){
$r = '<div style="font-family:Arial,Helvetica,sans-serif; text-align: center; color:#D35701; font-size:14px; font-weight:bold;">';
$r .= '<a href="http:etsy.com" target="_blank">Etsy</a>';
$r .= '<div style="font-size:10px;"><a href="http:etsy.com" target="_blank">Buy Handmade</a></div></div>';
return $r;
}
function widget_etsywp_queryEtsy( $uid, $cache_file ) {
$url = 'http://openapi.etsy.com/v2/public/shops/' . $uid . '/listings/active?detail_level=low&includes=Images&limit=100& api_key=' . ETSY_API_KEY;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (intval($status) != 200) die("Error: $response_body");
$tmp_file = $cache_file.rand().'.tmp';
file_put_contents($tmp_file, $response_body);
rename($tmp_file, $cache_file);
return $response_body;
}
function widget_etsywp_getEtsyItems( $uid, $cacheloc ){
$parsed = parse_url(get_bloginfo('home'));
if(empty($cacheloc)) $cacheloc = sys_get_temp_dir();
if(!file_exists($cacheloc)) mkdir($cacheloc, 0777, true);
$cache_file = $cacheloc.'/etsy'.$uid.'_cache_'.$parsed['host'].'.json';
$data;
if (!file_exists($cache_file) or (time() - filemtime($cache_file) >= ETSY_CACHE_LIFE)){
$data = widget_etsywp_queryEtsy( $uid, $cache_file );
}else{
$data = file_get_contents($cache_file);
}
$items = json_decode($data);
return $items->results;
}
$ops = array('classname' => 'widget_etsywp', 'description' => "Show Etsy items in sidebar");
wp_register_sidebar_widget('etsywp', 'Etsy', 'widget_etsywp', $widget_ops);
wp_register_widget_control('etsywp', 'Etsy WP', 'widget_etsywp_control' );
}
function widget_etsywp_on_activate(){
//clear the cache whenever the plugin is activated
$options = get_option('widget_etsywp');
$cacheloc = $options['cacheloc'];
if(empty($cacheloc)) $cacheloc = sys_get_temp_dir();
if(file_exists($cacheloc)){
$uid = $options['etsyuserid'];
$parsed = parse_url(get_bloginfo('home'));
$cache_file = $cacheloc.'/etsy'.$uid.'_cache_'.$parsed['host'].'.json';
if(file_exists($cache_file)){
unlink($cache_file);
}
}
}
add_action('widgets_init', 'widget_etsywp_register');
register_activation_hook( __FILE__, 'widget_etsywp_on_activate' );
?>
Love & Light