function widget($args, $instance)
 {
     $ExtraData = array('subscribe_type' => 'general', 'feed' => '', 'taxonomy_term_id' => '', 'cat_id' => '', 'post_type' => '');
     if (!empty($instance['subscribe_type'])) {
         $ExtraData['subscribe_type'] = $instance['subscribe_type'];
     }
     switch ($instance['subscribe_type']) {
         case 'post_type':
             if (empty($instance['subscribe_post_type'])) {
                 return;
             }
             $ExtraData['post_type'] = $instance['subscribe_post_type'];
         case 'channel':
             if (empty($instance['subscribe_feed_slug'])) {
                 return;
             }
             $ExtraData['feed'] = $instance['subscribe_feed_slug'];
             break;
         case 'ttid':
             if (empty($instance['subscribe_term_taxonomy_id']) || !is_numeric($instance['subscribe_term_taxonomy_id'])) {
                 return;
             }
             $ExtraData['taxonomy_term_id'] = $instance['subscribe_term_taxonomy_id'];
             break;
         case 'category':
             if (empty($instance['subscribe_category_id']) || !is_numeric($instance['subscribe_category_id'])) {
                 return;
             }
             $ExtraData['cat_id'] = $instance['subscribe_category_id'];
             break;
         default:
             // Doesn't matter, we'r using the default podcast channel
     }
     $Settings = powerpresssubscribe_get_settings($ExtraData);
     if (empty($Settings)) {
         return;
     }
     echo $args['before_widget'];
     if (!empty($instance['title'])) {
         echo $args['before_title'];
         echo esc_html($instance['title']);
         echo $args['after_title'];
     }
     echo powerpress_do_subscribe_sidebar_widget($Settings);
     echo $args['after_widget'];
     return;
 }
function powerpress_subscribe_shortcode($attr)
{
    if (is_feed()) {
        return '';
    }
    // Only works on pages...
    if (!is_singular()) {
        return '';
    }
    /*
    extract( shortcode_atts( array(
    	'channel'=>'', // Used for PowerPress Podcast Channels
    	'slug' => '', // Used for PowerPress (alt for 'channel')
    	'feed' => '', // Used for PowerPress (alt for 'channel')
    	'post_type' => 'post', // Used for PowerPress 
    	'category'=>'', // Used for PowerPress (specify category ID, name or slug)
    	'term_taxonomy_id'=>'', // Used for PowerPress (specify term taxonomy ID)
    	//'term_id'=>'', // Used for PowerPress (specify term ID, name or slug)
    	//'taxonomy'=>'', // Used for PowerPress (specify taxonomy name)
    	
    	'title'	=> '', // Display custom title of show/program
    	'subtitle'=>'', // Subtitle for podcast (optional)
    	'feed_url'=>'', // provide subscribe widget for specific RSS feed
    	'itunes_url'=>'', // provide subscribe widget for specific iTunes subscribe URL
    	'image_url'=>'', // provide subscribe widget for specific iTunes subscribe URL
    	'heading'=>'', // heading label for podcast
    	
    	'itunes_subtitle'=>'', // Set to 'true' to include the iTunes subtitle in subscribe widget
    	
    	// Appearance attributes
    	'itunes_button'=>'', // Set to 'true' to use only the iTunes button
    	'itunes_banner'=>'', // Set to 'true' to use only the iTunes banner
    	'style'=>'' // Set to 'true' to use only the iTunes banner
    ), $attr, 'powerpresssubscribe' ) );
    //return print_r($attr, true);
    */
    /**/
    if (empty($attr['slug']) && !empty($attr['feed'])) {
        $attr['slug'] = $attr['feed'];
    } else {
        if (empty($attr['slug']) && !empty($attr['channel'])) {
            $attr['slug'] = $attr['channel'];
        } else {
            if (empty($attr['slug'])) {
                $attr['slug'] = 'podcast';
            }
        }
    }
    // Set empty args to prevent warnings
    if (!isset($attr['term_taxonomy_id'])) {
        $attr['term_taxonomy_id'] = '';
    }
    if (!isset($attr['category_id'])) {
        $attr['category_id'] = '';
    }
    if (!isset($attr['post_type'])) {
        $attr['post_type'] = '';
    }
    $subscribe_type = '';
    $category_id = '';
    if (!empty($attr['category'])) {
        $CategoryObj = false;
        if (preg_match('/^[0-9]*$/', $attr['category'])) {
            // If it is a numeric ID, lets try finding it by ID first...
            $CategoryObj = get_term_by('id', $attr['category'], 'category');
        }
        if (empty($CategoryObj)) {
            $CategoryObj = get_term_by('name', $attr['category'], 'category');
        }
        if (empty($CategoryObj)) {
            $CategoryObj = get_term_by('slug', $attr['category'], 'category');
        }
        if (!empty($CategoryObj)) {
            $category_id = $CategoryObj->term_id;
        }
    }
    if (!empty($attr['category'])) {
        $subscribe_type = 'category';
    }
    if (!empty($attr['term_taxonomy_id'])) {
        $subscribe_type = 'ttid';
    }
    if (!empty($attr['post_type'])) {
        $subscribe_type = 'post_type';
    }
    if (!empty($attr['slug']) && $attr['slug'] != 'podcast') {
        $subscribe_type = 'channel';
    }
    $Settings = array();
    if (!empty($attr['feed_url'])) {
        $Settings['feed_url'] = $attr['feed_url'];
    } else {
        $Settings = powerpresssubscribe_get_settings(array('feed' => $attr['slug'], 'taxonomy_term_id' => $attr['term_taxonomy_id'], 'cat_id' => $category_id, 'post_type' => $attr['post_type'], 'subscribe_type' => $subscribe_type));
    }
    // Podcast title handling
    if (isset($attr['title']) && empty($attr['title']) && isset($Settings['title'])) {
        unset($Settings['title']);
    } else {
        if (!empty($attr['title'])) {
            $Settings['title'] = $attr['title'];
        } else {
            if (!isset($Settings['title'])) {
                $Settings['title'] = '';
            }
        }
    }
    // This way the title can be detected
    unset($Settings['subtitle']);
    // Make sure no subtitle passes this point
    if (!empty($attr['itunes_subtitle']) && !empty($Settings['itunes_subtitle'])) {
        $Settings['subtitle'] = $Settings['itunes_subtitle'];
    } else {
        if (!empty($attr['subtitle'])) {
            $Settings['subtitle'] = $attr['subtitle'];
        }
    }
    if (!empty($attr['itunes_url'])) {
        $Settings['itunes_url'] = $attr['itunes_url'];
    }
    if (!empty($attr['style'])) {
        $Settings['style'] = $attr['style'];
    }
    if (!empty($attr['image_url'])) {
        $Settings['image_url'] = $attr['image_url'];
    }
    if (isset($attr['heading'])) {
        // If a custom heading is set
        $Settings['heading'] = $attr['heading'];
    }
    if (empty($Settings)) {
        return '';
    }
    $Settings['itunes_url'] = powerpresssubscribe_get_itunes_url($Settings);
    wp_enqueue_style('powerpress-subscribe-style');
    if (!empty($attr['itunes_button']) && !empty($Settings['itunes_url'])) {
        $html .= '<div>';
        $html .= '';
        $html .= '<a href="';
        $html .= esc_url($Settings['itunes_url']);
        $html .= '" target="itunes_store" style="display:inline-block;overflow:hidden;background:url(https://linkmaker.itunes.apple.com/htmlResources/assets/en_us/images/web/linkmaker/badge_subscribe-lrg.png) no-repeat;width:135px;height:40px;}"></a>';
        $html .= '</div>';
        return $html;
    }
    if (!empty($attr['itunes_banner']) && !empty($Settings['itunes_url'])) {
        $apple_id = powerpress_get_apple_id($Settings['itunes_url'], true);
        if (!empty($apple_id) && $apple_id > 0) {
            $html = '';
            $html .= '<div id="ibb-widget-root-' . $apple_id . '"></div>';
            $html .= "<script>(function(t,e,i,d){var o=t.getElementById(i),n=t.createElement(e);o.style.height=250;o.style.width=300;o.style.display='inline-block';n.id='ibb-widget',n.setAttribute('src',('https:'===t.location.protocol?'https://':'http://')+d),n.setAttribute('width','300'),n.setAttribute('height','250'),n.setAttribute('frameborder','0'),n.setAttribute('scrolling','no'),o.appendChild(n)})(document,'iframe','ibb-widget-root-" . $apple_id . "'";
            $html .= ',"banners.itunes.apple.com/banner.html?partnerId=&aId=&bt=catalog&t=catalog_blur&id=' . $apple_id . '&c=us&l=en-US&w=300&h=250");</script>';
            return $html;
        }
        return '';
    }
    return powerpress_do_subscribe_widget($Settings);
}