Skip to content

Instantly share code, notes, and snippets.

@jamesstout
Created May 18, 2013 14:45
Show Gist options
  • Save jamesstout/5604637 to your computer and use it in GitHub Desktop.
Save jamesstout/5604637 to your computer and use it in GitHub Desktop.
Patch to Last.fm-Tabs to add the option to add rel="nofollow"' to links [http://wordpress.org/extend/plugins/lastfm-tabs/]
Index: lastfm_options.php
===================================================================
--- lastfm_options.php (revision 714873)
+++ lastfm_options.php (working copy)
@@ -346,6 +346,15 @@
'type' => 'checkbox',
'section' => 'general'
);
+
+ $this->settings['nofollow'] = array(
+ 'title' => __('Use nofollow?','lastfm_tabs'),
+ 'desc' => __('Add nofollow to links.','lastfm_tabs'),
+ 'std' => 1,
+ 'type' => 'checkbox',
+ 'section' => 'general'
+ );
+
$this->settings['enable_tabs'] = array(
'title' => __('Enable tabbed navigation?','lastfm_tabs'),
'desc' => __('jQuery tabs for your Last.fm widget','lastfm_tabs'),
Index: lastfm_tabs.php
===================================================================
--- lastfm_tabs.php (revision 714873)
+++ lastfm_tabs.php (working copy)
@@ -232,6 +232,10 @@
return $this->options['effect'];
}
+ private function getNoFollow(){
+ return ($this->options['nofollow'] == 1) ? 'rel="nofollow"' : '';
+ }
+
private function getEffectXY(){
return $this->options['effect_xy'];
}
@@ -405,6 +409,7 @@
$effect = $this->getEffect();
$lovedtracks = new DOMDocument();
$title = $this->options['loved_title'];
+ $noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
@@ -433,13 +438,13 @@
else
$lovedcover = $nocoverurl;
$op .= '<div class="lastfm_record">
- <a class="mosaic-overlay '.$effect.' '.$effect_xy.'" href="'.$lovedurl.'" target="_blank" title="'.$lovedtrack.'">
+ <a class="mosaic-overlay '.$effect.' '.$effect_xy.'" href="'.$lovedurl.'" target="_blank" ' . $noFollow . ' title="'.$lovedtrack.'">
<div class="details">
<p>'.$artistname.'</p>
<p>'.$lovedtrack.'</p>
</div>
</a>
- <a href="'.$lovedurl.'" target="_blank">
+ <a href="'.$lovedurl.'" target="_blank" ' . $noFollow . ' title="'.$lovedtrack . '">
<img src="'.$lovedcover.'" alt="'.$lovedtrack.'" />
</a>
</div>'."\n";
@@ -582,6 +587,7 @@
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
$charts = new DOMDocument();
+ $noFollow = $this->getNoFollow();
$title = $this->options['charts_title'];
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
@@ -613,13 +619,13 @@
$chartcover = $nocoverurl;
$op .= '<div class="lastfm_record">
- <a class="mosaic-overlay '.$effect.' '. $effect_xy .'" href="'.$url.'" target="_blank" title="'.$artistname.'">
+ <a class="mosaic-overlay '.$effect.' '. $effect_xy .'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$artistname.'">
<div class="details">
<p>'.$artistname.'</p>
<p>'.$playcount.'</p>
</div>
</a>
- <a href="'.$url.'" target="_blank" title="'.$artistname.'">
+ <a href="'.$url.'" target="_blank" title="'.$artistname.'" ' . $noFollow . '>
<img src="'.$chartcover.'" alt="'.$artistname.'" title="'.$artistname.'" />
</a>
</div>'."\n";
@@ -658,6 +664,7 @@
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
$title = $this->options['top_albums_title'];
+ $noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
@@ -705,14 +712,14 @@
else
$cover = $nocoverurl;
$op .= '<div class="lastfm_record">
- <a class="mosaic-overlay '.$effect . ' '. $effect_xy .'" href="'.$url.'" target="_blank" title="'.$albumname.'">
+ <a class="mosaic-overlay '.$effect . ' '. $effect_xy .'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$albumname.'">
<div class="details">
<p>'.$albumname.'</p>
<p>'.__('by ','lastfm_tabs') . $artistname.'</p>
<p>'.sprintf(__("Played %d times.",'lastfm_tabs'), $playcount) .'</p>
</div>
</a>
- <a href="'.$url.'" target="_blank" title="'.$albumname.'">
+ <a href="'.$url.'" target="_blank" title="'.$albumname.'" ' . $noFollow . '>
<img src="'.$cover.'" alt="'.$albumname.'" title="'.$albumname.'" />
</a>
</div>'."\n";
@@ -752,6 +759,7 @@
$period = $this->options['top_artists_period'];
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
+ $noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
@@ -795,13 +803,13 @@
else
$cover = $nocoverurl;
$op .= '<div class="lastfm_record">
- <a class="mosaic-overlay '.$effect.' '.$effect_xy.'" href="'.$url.'" target="_blank" title="'.$artistname.'">
+ <a class="mosaic-overlay '.$effect.' '.$effect_xy.'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$artistname.'">
<div class="details">
<p>'.$artistname.'</p>
<p>'.sprintf(__("Played %d times.",'lastfm_tabs'), $playcount) .'</p>
</div>
</a>
- <a href="'.$url.'" target="_blank" title="'.$artistname.'">
+ <a href="'.$url.'" target="_blank" title="'.$artistname.'" ' . $noFollow . '>
<img src="'.$cover.'" alt="'.$artistname.'" title="'.$artistname.'" />
</a>
</div>'."\n";
@@ -840,6 +848,7 @@
$effect_xy = $this->getEffectXY();
$coverinfo = new DOMDocument();
$title = $this->options['recently_title'];
+ $noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
@@ -865,13 +874,13 @@
else
$cover = $nocoverurl;
$op .= '<div class="lastfm_record">
- <a class="mosaic-overlay '.$effect . ' '. $effect_xy .'" href="'.$url.'" target="_blank" title="'.$artist.'">
+ <a class="mosaic-overlay '.$effect . ' '. $effect_xy .'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$artist.'">
<div class="details">
<p>'.$song.'</p>
<p>'.$artist.'</p>
</div>
</a>
- <a href="'.$url.'" target="_blank">
+ <a href="'.$url.'" target="_blank" ' . $noFollow . '>
<img src="'.$cover.'" alt="'.$song.'" title="'.$artist.' - '.$song.'"/>
</a>
</div>'."\n";
<?php
/**
* Settings class
*
*/
class lastfm_options {
private $sections;
private $checkboxes;
private $settings;
private $lastfm_tabs;
/**
* Construct
*/
public function __construct($lastfm_tabs){
$this->lastfm_tabs = $lastfm_tabs;
$this->checkboxes = array();
$this->settings = array();
$this->get_settings();
load_plugin_textdomain('lastfm_tabs', '',LASTFM_DIRNAME . '/languages');
$this->sections['general'] = __('General settings','lastfm_tabs');
$this->sections['tabs'] = __('Widget tabs','lastfm_tabs');
$this->sections['reset'] = __('Reset to defaults','lastfm_tabs');
$this->sections['about'] = __('About/Help','lastfm_tabs');
add_action('admin_menu', array($this, 'add_pages'));
add_action('admin_init', array($this, 'register_settings'));
if (! get_option('lastfm_tabs_options'))
$this->initialize_settings();
}
/**
* Add options page
*/
public function add_pages(){
$admin_page = add_options_page(__('Last.fm options','lastfm_tabs'), __('Last.fm options','lastfm_tabs'), 'manage_options', 'lastfm-options', array($this, 'display_page'));
add_action('admin_print_scripts-' . $admin_page, array($this, 'scripts'));
add_action('admin_print_styles-' . $admin_page, array($this, 'styles'));
}
/**
* Create settings field
*/
public function create_setting($args = array()){
$defaults = array(
'id' => 'default_field',
'title' => __('Default Field','lastfm_tabs'),
'desc' => __('This is a default description.','lastfm_tabs'),
'std' => '',
'type' => 'text',
'section' => 'general',
'choices' => array(),
'size' => '',
'class' => ''
);
extract(wp_parse_args($args, $defaults ));
$field_args = array(
'type' => $type,
'id' => $id,
'desc' => $desc,
'std' => $std,
'choices' => $choices,
'label_for' => $id,
'size' => $size,
'class' => $class
);
if ($type == 'checkbox')
$this->checkboxes[] = $id;
add_settings_field($id, $title, array($this, 'display_setting'), 'lastfm-options', $section, $field_args );
}
/**
* Display options page
*
*/
public function display_page(){
echo '<div class="wrap">';
if (function_exists('screen_icon'))
screen_icon();
else
echo '<div class="icon32" id="icon-options-general"></div>';
echo '<h2>' . __('Last.fm options','lastfm_tabs') . '</h2>';
if ((isset($_GET['settings-updated'] ) && $_GET['settings-updated'] == true) || (isset($_POST['wp_delete_cache'])) )
$this->lastfm_tabs->clear_transients();
echo '<form action="options.php" method="post">';
settings_fields('lastfm_tabs_options');
echo '<div class="ui-tabs"> <ul class="ui-tabs-nav">';
foreach ($this->sections as $section_slug => $section )
echo '<li><a href="#' . $section_slug . '">' . $section . '</a></li>';
echo '</ul>';
do_settings_sections($_GET['page'] );
echo '</div>';
if (function_exists('submit_button'))
submit_button();
else
echo '<p class="submit"><input name="Submit" type="submit" class="button-primary" value="' . __('Save Changes','lastfm_tabs') . '" /></p>';
echo '</form>';
echo '<script type="text/javascript">
jQuery(document).ready(function($){
var sections = [];';
foreach ($this->sections as $section_slug => $section )
echo "sections['$section'] = '$section_slug';";
echo 'var wrapped = $(".wrap h3").wrap("<div class=\"ui-tabs-panel\">");
wrapped.each(function(){
$(this).parent().append($(this).parent().nextUntil("div.ui-tabs-panel"));
});
$(".ui-tabs-panel").each(function(index){
$(this).attr("id", sections[$(this).children("h3").text()]);
if (index > 0)
$(this).addClass("ui-tabs-hide");
});
$(".ui-tabs").tabs({
fx: { opacity: "toggle", duration: "fast" }
});
$("input[type=text], textarea").each(function(){
if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "")
$(this).css("color", "#999");
});
$("input[type=text], textarea").focus(function(){
if ($(this).val() == $(this).attr("placeholder") || $(this).val() == ""){
$(this).val("");
$(this).css("color", "#000");
}
}).blur(function(){
if ($(this).val() == "" || $(this).val() == $(this).attr("placeholder")){
$(this).val($(this).attr("placeholder"));
$(this).css("color", "#999");
}
});
$(".wrap h3, .wrap table").show();
// This will make the "warning" checkbox class really stand out when checked.
// I use it here for the Reset checkbox.
$(".warning").change(function(){
if ($(this).is(":checked"))
$(this).parent().css("background", "#c00").css("color", "#fff").css("fontWeight", "bold");
else
$(this).parent().css("background", "none").css("color", "inherit").css("fontWeight", "normal");
});
// Browser compatibility
if ($.browser.mozilla)
$("form").attr("autocomplete", "off");
});
</script>
</div>';
echo '<form name="wp_cache_content_delete" method="post">';
echo '<input type="hidden" name="wp_delete_cache" />';
if (function_exists('submit_button'))
submit_button(__('Clear caches','lastfm_tabs'),'delete');
else
echo '<p class="submit"><input type="submit" value="Clear caches" class="button-secondary delete" id="submit" name="submit"></p>';
wp_nonce_field('lfmt');
echo '</form>';
}
/**
* Description for section
*/
public function display_section(){
// code
}
/**
* Description for About/Help section
*
*/
public function display_about_section(){
_e('<h4>Shortcode</h4>','lastfm_tabs');
_e('<p>You can use the following shortcode:</p>','lastfm_tabs');
echo '<p><span class="code">[lastfm tab="&lt;function&gt;" count="&lt;count&gt;" title="&lt;true/false&gt;"]</span>';
_e('<p>The following functions are available: albums, friends, charts, artists, info, recent, loved and shouts</p>','lastfm_tabs');
_e('<p>You may combine two or more of these functions with commas.</p>','lastfm_tabs');
_e('<p>The title-parameter enables/disables output of the corresponding title for each function. Title and count are optional. If not provided, the plugin options will be used.</p>','lastfm_tabs');
_e('<p>An example:</p>','lastfm_tabs');
echo '<p><span class="code">[lastfm tab="albums, friends, charts, artists, info, recent, loved, shouts" count="3" titles="true"]</span></p>';
_e('<p>Would display all functions in that order with max. 3 entries each and print the titles.</p>','lastfm_tabs');
_e('<h4>Template tag</h4>','lastfm_tabs');
_e('<p>In your templates you can use the function <span class="code">lastfm_tabs_render($functions=array(), $count=false, $echotitle=false, $return=false)</span></p>','lastfm_tabs');
_e('<p>So the following would output a list of 5 of your friends and the 5 most listened to albums without titles:</p>','lastfm_tabs');
echo '<p><span class="code">&lt;?php if(function_exists(\'lastfm_tabs_render\')){<br />
&nbsp;&nbsp;&nbsp;&nbsp;lastfm_tabs_render(array(\'friends\',\'albums\'),5,false);<br />}?&gt;</span></p>';
_e('<p>If you do not want to echo the function\'s return, add the last parameter and set it to true.</p>','lastfm_tabs');
_e('<h4>Note</h4>','lastfm_tabs');
_e('<p>Changes in a template tag will not immediately take effect since all the data is cached. If you change options in a template tag you have to manually clean the plugin cache.</p>','lastfm_tabs');
_e('<p>If you have any questions or ideas to improve this plugins, write to [email protected] or drop a line at ','lastfm_tabs');
echo '<a href="http://kreativkonzentrat.de/kontakt" title="Kreativkonzentrat">kreativkonzentrat.de</a></p>';
}
/**
* HTML output for text field
*/
public function display_setting($args = array()){
extract($args );
$options = get_option('lastfm_tabs_options');
if (! isset($options[$id] ) && $type != 'checkbox')
$options[$id] = $std;
elseif (! isset($options[$id] ))
$options[$id] = 0;
$field_class = '';
if ($class != '')
$field_class = ' ' . $class;
switch ($type ){
case 'heading':
echo '</td></tr><tr valign="top"><td colspan="2"><h4>' . $desc . '</h4>';
break;
case 'checkbox':
echo '<input class="checkbox' . $field_class . '" type="checkbox" id="' . $id . '" name="lastfm_tabs_options[' . $id . ']" value="1" ' . checked($options[$id], 1, false ) . ' /> <span class="description">' . $desc . '</span>';
break;
case 'select':
echo '<select class="select' . $field_class . '" name="lastfm_tabs_options[' . $id . ']">';
foreach ($choices as $value => $label )
echo '<option value="' . esc_attr($value ) . '"' . selected($options[$id], $value, false ) . '>' . $label . '</option>';
echo '</select>';
if ($desc != '')
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'radio':
$i = 0;
foreach ($choices as $value => $label ){
echo '<input class="radio' . $field_class . '" type="radio" name="lastfm_tabs_options[' . $id . ']" id="' . $id . $i . '" value="' . esc_attr($value ) . '" ' . checked($options[$id], $value, false ) . '> <label for="' . $id . $i . '">' . $label . '</label>';
if ($i < count($options ) - 1 )
echo '<br />';
$i++;
}
if ($desc != '')
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'textarea':
echo '<textarea class="' . $field_class . '" id="' . $id . '" name="lastfm_tabs_options[' . $id . ']" placeholder="' . $std . '" rows="5" cols="30">' . wp_htmledit_pre($options[$id] ) . '</textarea>';
if ($desc != '')
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'password':
echo '<input class="regular-text' . $field_class . '" type="password" id="' . $id . '" name="lastfm_tabs_options[' . $id . ']" value="' . esc_attr($options[$id] ) . '" />';
if ($desc != '')
echo '<br /><span class="description">' . $desc . '</span>';
break;
case 'text':
default:
if (isset($size) && $size > 0)
$size = ' size="'.$size.'" class="text"';
else
$size = 'class="regular-text';
echo '<input '.$size. $field_class . '" type="text" id="' . $id . '" name="lastfm_tabs_options[' . $id . ']" placeholder="' . $std . '" value="' . esc_attr($options[$id] ) . '" />';
if ($desc != '')
echo '<br /><span class="description">' . $desc . '</span>';
break;
}
}
/**
* Settings and defaults
*/
public function get_settings(){
/* General Settings
*
*/
$this->settings['widget_title'] = array(
'title' => __('Widget title','lastfm_tabs'),
'desc' => __('Default widget title','lastfm_tabs'),
'std' => 'Last.fm',
'type' => 'text',
'section' => 'general'
);
$this->settings['username'] = array(
'title' => __('Last.fm username','lastfm_tabs'),
'desc' => __('Your last.fm username','lastfm_tabs'),
'std' => '',
'type' => 'text',
'section' => 'general'
);
$this->settings['cover_size'] = array(
'section' => 'general',
'title' => __('Cover size','lastfm_tabs'),
'desc' => __('Size of displayed images','lastfm_tabs'),
'type' => 'select',
'std' => '2',
'choices' => array(
'0' => __('small','lastfm_tabs'),
'1' => __('medium','lastfm_tabs'),
'2' => __('large','lastfm_tabs'),
'3' => __('extra large','lastfm_tabs')
)
);
$this->settings['expiration'] = array(
'title' => __('Cache expiration','lastfm_tabs'),
'desc' => __('Time in minutes','lastfm_tabs'),
'std' => '300',
'size' => '5',
'type' => 'text',
'section' => 'general'
);
$this->settings['nocoverurl'] = array(
'title' => __('Fallback cover','lastfm_tabs'),
'desc' => __('URL of fallback cover when no image is found','lastfm_tabs'),
'std' => LASTFM_URI . '/img/nocover_160.jpg',
'type' => 'text',
'section' => 'general'
);
$this->settings['fancyness'] = array(
'section' => 'general',
'title' => __('Fancy mouseover effect','lastfm_tabs'),
'desc' => __('Sliding/fading caption on covers','lastfm_tabs'),
'type' => 'checkbox',
'std' => 1 // Set to 1 to be checked by default, 0 to be unchecked by default.
);
$this->settings['effect'] = array(
'section' => 'general',
'title' => __('Effect','lastfm_tabs'),
'desc' => '',
'type' => 'select',
'std' => 'slide',
'choices' => array(
'fade' => __('fade','lastfm_tabs'),
'slide' => __('slide','lastfm_tabs')
)
);
$this->settings['effect_duration'] = array(
'title' => __('Effect duration','lastfm_tabs'),
'desc' => __('in ms','lastfm_tabs'),
'std' => '150',
'type' => 'text',
'size' => '5',
'section' => 'general'
);
$this->settings['effect_xy'] = array(
'section' => 'general',
'title' => __('Effect origin','lastfm_tabs'),
'desc' => '',
'type' => 'select',
'std' => 'bottom',
'choices' => array(
'bottom' => __('bottom','lastfm_tabs'),
'top' => __('top','lastfm_tabs'),
'left' => __('left','lastfm_tabs'),
'right' => __('right','lastfm_tabs')
)
);
$this->settings['use_stylesheet'] = array(
'title' => __('Use stylesheet?','lastfm_tabs'),
'desc' => __('Needed for sliding/fading captions. Fixes image width and height to 126px.<br /> You can place your own lastfm.css in your template folder to overwrite the default stylesheet.','lastfm_tabs'),
'std' => 1,
'type' => 'checkbox',
'section' => 'general'
);
$this->settings['nofollow'] = array(
'title' => __('Use nofollow?','lastfm_tabs'),
'desc' => __('Add nofollow to links.','lastfm_tabs'),
'std' => 1,
'type' => 'checkbox',
'section' => 'general'
);
$this->settings['enable_tabs'] = array(
'title' => __('Enable tabbed navigation?','lastfm_tabs'),
'desc' => __('jQuery tabs for your Last.fm widget','lastfm_tabs'),
'std' => 1,
'type' => 'checkbox',
'section' => 'general'
);
$this->settings['enable_artistimage'] = array(
'title' => __('Show artist image when no album cover is found instead of default cover?','lastfm_tabs'),
'desc' => __('May increase loading time','lastfm_tabs'),
'std' => 1,
'type' => 'checkbox',
'section' => 'general'
);
$this->settings['debugmode'] = array(
'title' => __('Enable debugmode?','lastfm_tabs'),
'desc' => __('Shows some caching information','lastfm_tabs'),
'std' => 0,
'type' => 'checkbox',
'section' => 'general'
);
/*
* Tabs
*/
$this->settings['enable_recently'] = array(
'section' => 'tabs',
'title' => __('Enable recently played tracks tab?','lastfm_tabs'),
'desc' => '',
'type' => 'checkbox',
'std' => 1
);
$this->settings['recently_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Recently played'
);
$this->settings['trackcount'] = array(
'section' => 'tabs',
'title' => __('Number of tracks to display','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'size' => '3',
'std' => '4'
);
$this->settings['enable_info'] = array(
'section' => 'tabs',
'title' => __('Enable user info tab?','lastfm_tabs'),
'desc' => '',
'type' => 'checkbox',
'std' => 0
);
$this->settings['info_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Profile'
);
$this->settings['enable_friends'] = array(
'section' => 'tabs',
'title' => __('Enable friends tab?','lastfm_tabs'),
'desc' => '',
'type' => 'checkbox',
'std' => 0
);
$this->settings['friends_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Friends'
);
$this->settings['friendscount'] = array(
'section' => 'tabs',
'title' => __('Number of friends to display','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'size' => '3',
'std' => '4'
);
$this->settings['enable_lovedtracks'] = array(
'section' => 'tabs',
'title' => __('Enable loved tracks tab?','lastfm_tabs'),
'desc' => '',
'type' => 'checkbox',
'std' => 1
);
$this->settings['loved_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Favorites'
);
$this->settings['lovedcount'] = array(
'section' => 'tabs',
'title' => __('Number of loved tracks to display','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'size' => '3',
'std' => '4'
);
$this->settings['enable_shouts'] = array(
'section' => 'tabs',
'title' => __('Enable shouts tab?','lastfm_tabs'),
'desc' => '',
'type' => 'checkbox',
'std' => 0
);
$this->settings['shouts_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Shouts'
);
$this->settings['shoutcount'] = array(
'section' => 'tabs',
'title' => __('Number of shouts to display','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'size' => '3',
'std' => '4'
);
$this->settings['enable_charts'] = array(
'section' => 'tabs',
'title' => __('Enable weekly album charts tab?','lastfm_tabs'),
'desc' => __('May increase loading time on big lists when not cached yet','lastfm_tabs'),
'type' => 'checkbox',
'std' => 0
);
$this->settings['charts_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Charts'
);
$this->settings['chartscount'] = array(
'section' => 'tabs',
'title' => __('Number of chart entries to display','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'size' => '3',
'std' => '4'
);
$this->settings['enable_top_albums'] = array(
'section' => 'tabs',
'title' => __('Enable top albums tab?','lastfm_tabs'),
'desc' => __('May increase loading time on big lists when not cached yet','lastfm_tabs'),
'type' => 'checkbox',
'std' => 0
);
$this->settings['top_albums_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Top albums'
);
$this->settings['top_albums_count'] = array(
'section' => 'tabs',
'title' => __('Number of top album entries to display','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'size' => '3',
'std' => '4'
);
$this->settings['top_albums_period'] = array(
'section' => 'tabs',
'title' => __('Period','lastfm_tabs'),
'desc' => '',
'type' => 'select',
'std' => 'bottom',
'choices' => array(
'0' => 'overall',
'1' => '7 days',
'2' => '3 months',
'3' => '6 months',
'4' => '12 months'
)
);
$this->settings['enable_top_artists'] = array(
'section' => 'tabs',
'title' => __('Enable top artists tab?','lastfm_tabs'),
'desc' => __('May increase loading time on big lists when not cached yet','lastfm_tabs'),
'type' => 'checkbox',
'std' => 0
);
$this->settings['top_artists_title'] = array(
'section' => 'tabs',
'title' => __('Title:','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'std' => 'Top artists'
);
$this->settings['top_artists_count'] = array(
'section' => 'tabs',
'title' => __('Number of top album entries to display','lastfm_tabs'),
'desc' => '',
'type' => 'text',
'size' => '3',
'std' => '4'
);
$this->settings['top_artists_period'] = array(
'section' => 'tabs',
'title' => __('Period','lastfm_tabs'),
'desc' => '',
'type' => 'select',
'std' => 'bottom',
'choices' => array(
'0' => 'overall',
'1' => '7 days',
'2' => '3 months',
'3' => '6 months',
'4' => '12 months'
)
);
$this->settings['reset_theme'] = array(
'section' => 'reset',
'title' => __('Reset options','lastfm_tabs'),
'type' => 'checkbox',
'std' => 0,
'class' => 'warning', // Custom class for CSS
'desc' => __('Check this box and click "Save Changes" below to reset all plugin options to their defaults.','lastfm_tabs')
);
}
/**
* Initialize default settings values
*/
public function initialize_settings(){
$default_settings = array();
foreach ($this->settings as $id => $setting ){
if ($setting['type'] != 'heading')
$default_settings[$id] = $setting['std'];
}
update_option('lastfm_tabs_options', $default_settings );
}
/**
* Register settings
*/
public function register_settings(){
register_setting('lastfm_tabs_options', 'lastfm_tabs_options', array(&$this, 'validate_settings'));
foreach ($this->sections as $slug => $title ){
if ($slug == 'about')
add_settings_section($slug, $title, array($this, 'display_about_section'), 'lastfm-options');
else
add_settings_section($slug, $title, array($this, 'display_section'), 'lastfm-options');
}
$this->get_settings();
foreach ($this->settings as $id => $setting ){
$setting['id'] = $id;
$this->create_setting($setting );
}
}
/**
* jQuery Tabs
*/
public function scripts(){
wp_print_scripts('jquery-ui-tabs');
}
/**
* Styling
*/
public function styles(){
wp_register_style('lastfm_tabs_admin', LASTFM_URI . '/css/lastfm_tabs_admin.css');
wp_enqueue_style('lastfm_tabs_admin');
}
/**
* Validation
*
*/
public function validate_settings($input ){
$this->lastfm_tabs->clear_transients(false);
if (! isset($input['reset_theme'] )){
$options = get_option('lastfm_tabs_options');
foreach ($this->checkboxes as $id ){
if (isset($options[$id] ) && ! isset($input[$id] ))
unset($options[$id] );
}
return $input;
}
return false;
}
}
?>
<?php
/*
Plugin Name: Last.fm Tabs
Plugin URI: http://kreativkonzentrat.de/blog/lastfm-tabs-2-0.html
Description: Last.fm track, album art, userinfo, shout and friends display
Version: 2.0.2
Author: Felix Moche
Author URI: http://kreativkonzentrat.de
*/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if (!defined('LASTFM_BASENAME'))
define('LASTFM_BASENAME', plugin_basename(__FILE__));
if (!defined('LASTFM_ABSPATH'))
define('LASTFM_ABSPATH', dirname(__FILE__));
if (!defined('LASTFM_DIRNAME'))
define('LASTFM_DIRNAME', basename(LASTFM_ABSPATH));
if (!defined('LASTFM_URI'))
define('LASTFM_URI', plugins_url().'/'.LASTFM_DIRNAME);
include_once('lastfm_options.php');
/*
* main class
*/
class lastfm_tabs{
public $options;
var $api_key = '21afc70179cc8d0aa11aae5a6253a535';
var $transients = array();
private function lastfm_tabs(){
$this->__construct();
}
/*
* construct
*/
public function __construct(){
$this->options = get_option('lastfm_tabs_options');
$this->transients = array(
'friends' => 'lastfm_friends',
'loved' => 'lastfm_favorites',
'shouts' => 'lastfm_shouts',
'info' => 'lastfm_info',
'charts' => 'lastfm_charts',
'top_albums' => 'lastfm_top_albums',
'top_artists' => 'lastfm_top_artists',
'recently' => 'lastfm_recently',
'widget' => 'lastfm_tabs_widget_cache',
'shortcode' => 'lastfm_tabs_shortcode'
);
if (is_admin()){
add_filter('plugin_action_links', array($this, 'action_links'), 10, 2);
register_deactivation_hook(__FILE__,array($this, 'deactivate'));
register_uninstall_hook(LASTFM_ABSPATH . '/lastfm_tabs.php' ,'uninstall');
}
add_action('plugins_loaded', array($this, 'init'));
}
/*
* initialisation
*/
function init(){
//load textdomain
load_plugin_textdomain('lastfm_tabs', '',LASTFM_DIRNAME . '/languages');
//sidebar widget
wp_register_sidebar_widget('lastfm_tabs', 'Last.fm Tabs', array($this, 'widget_output'));
//add shortcode
add_shortcode('lastfm', array($this,'shortcode_handler'));
add_action('publish_post', array($this, 'savepost_hook'));
add_action('publish_page', array($this, 'savepost_hook'));
add_action('wp_print_styles', array($this, 'style'));
add_action('wp_enqueue_scripts', array($this, 'tabs'));
add_action('wp_enqueue_scripts', array($this, 'hover'));
}
function savepost_hook(){
delete_transient('lastfm_tabs_template_cache');
delete_transient('lastfm_tabs_shortcode_cache');
}
/*
* callback function for shortcode
*/
function shortcode_handler($attr, $content){
extract(shortcode_atts(array(
'tab' => '',
'titles'=> false,
'count' => null,
'cache' => false
),
$attr));
//get desired tabs
$tab = explode(',',str_replace(' ','', strtolower($tab)));
$caller = 'shortcode';
if ($cache == true){
$transient = $this->getTransient('shortcode');
$op = get_transient($transient);
}
else
$op = false;
if ($op === false){
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), 'Shortcode');
if ($titles !== false)
$titles = true;
if (intval($count) < 1)
$count=null;
$op = '';
foreach($tab as $_tab){
switch($_tab){
case('recent'):
$op .= $this->get_recently_played($count, $titles, $caller);
break;
case('friends'):
$op .= $this->get_friends($count, $titles, $caller);
break;
case('info'):
$op .= $this->get_userinfo($titles, $caller);
break;
case('charts'):
$op .= $this->get_charts($count, $titles, $caller);
break;
case('artists'):
$op .= $this->get_top_artists($count, $titles, $caller);
break;
case('albums'):
$op .= $this->get_top_albums($count, $titles, $caller);
break;
case('shouts'):
$op .= $this->get_shouts($count, $titles, $caller);
break;
case('loved'):
$op .= $this->get_loved_tracks($count, $titles, $caller);
break;
default:
break;
}
}
$op = '<ul class="lastfm_widget">'.$op.'</ul>';
if ($cache == true){
set_transient($transient, $op, $this->getExpiration());
echo '<br>set cache<br>';
}
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Shortcode');
return $op;
}
/*
* uninstall and deactivate
*/
function deactivate(){
$this->clear_transients(false);
}
function uninstall(){
$this->clear_transients(false);
delete_option('lastfm_tabs_options');
}
/*
* set action links
*/
function action_links($links, $file){
if ( $file != LASTFM_BASENAME )
return $links;
$settings_link = '<a href="options-general.php?page=lastfm-options">' . __('Settings') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
/*
* clear transient cache
*/
public function clear_transients($echo=true){
delete_transient('lastfm_tabs_template_cache');
delete_transient('lastfm_tabs_shortcode_cache');
foreach($this->transients as $transient){
delete_transient($transient);
}
if ($echo)
echo '<div id="message" class="updated fade"><p>'.__('Caches cleared.','lastfm_tabs').'</p></div>';
}
/*
* Errorhandling
*/
function handle_error($errno, $errstr, $errfile, $errline, array $errcontext){
if (0 === error_reporting())
return false;
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
function handle_xml_errors($errno, $errstr, $errfile, $errline){
if (preg_match('/^DOMDocument::load\(\): (.+)$/', $errstr, $m) === 1)
throw new Exception($m[1]);
}
private function getDebug(){
if (isset($this->options['debugmode']) && intval($this->options['debugmode']))
return true;
else
return false;
}
private function getEffect(){
return $this->options['effect'];
}
private function getNoFollow(){
return ($this->options['nofollow'] == 1) ? 'rel="nofollow"' : '';
}
private function getEffectXY(){
return $this->options['effect_xy'];
}
private function getUsername(){
return $this->options['username'];
}
private function getCoversize(){
return $this->options['cover_size'];
}
private function getExpiration(){
return $this->options['expiration'] * 60;
}
private function getNoCoverUrl(){
return $this->options['nocoverurl'];
}
private function getTransient($name){
return $this->transients[$name];
}
/*
* get artist image
*/
private function get_artist_image($artistname, $cover_size, $nocoverurl){
$artistimage = new DOMDocument();
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($artistimage->load('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.urlencode($artistname).'&limit=1&api_key='.$this->api_key)){
$t_image = $artistimage->getElementsByTagName('image')->item($cover_size);
restore_error_handler();
if (isset($t_image->nodeValue) && ($t_image->nodeValue) )
return $t_image->nodeValue;
else
return $nocoverurl;
}
else
return $nocoverurl;
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
return $nocoverurl;
}
}
/*
* get album by mbid
*/
private function get_album_image($mbid, $cover_size){
$albumimage = new DOMDocument();
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($albumimage->load('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&mbid='.$mbid.'&api_key='.$this->api_key)){
$t_image = $albumimage->getElementsByTagName('image')->item($cover_size);
restore_error_handler();
if (isset($t_image->nodeValue) && ($t_image->nodeValue) )
return $t_image->nodeValue;
else
return false;
}
else
return false;
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
return false;
}
}
/*
* get album image by artist and album title
*/
private function get_album_image2($artistname, $albumtitle, $cover_size){
$albumimage = new DOMDocument();
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($albumimage->load('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&artist='.urlencode($artistname).'&album='.urlencode($albumtitle).'&api_key='.$this->api_key)){
$t_image = $albumimage->getElementsByTagName('image')->item($cover_size);
restore_error_handler();
if (isset($t_image->nodeValue) && ($t_image->nodeValue) )
return $t_image->nodeValue;
else
return false;
}
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
return false;
}
}
/*
* get friends list
*/
private function get_friends($count=false, $echotitle=false, $caller=''){
$transient = $this->getTransient('friends');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false){
$avatar_size = $this->getCoversize();
$username = $this->getUsername();
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
$friends = new DOMDocument();
$title = $this->options['friends_title'];
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
$friendscount = $count;
else
$friendscount = $this->options['friendscount'];
try{
set_error_handler(array($this,'handle_xml_errors'));
if($friends->load('http://ws.audioscrobbler.com/2.0/?method=user.getfriends&user='.$username.'&limit='.$friendscount.'&api_key='.$this->api_key)){
//cover info header
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
$i=1;
foreach ($friends->getElementsByTagName('user') as $node){
$friendsavatar = $node->getElementsByTagName('image')->item($avatar_size)->nodeValue;
$friendsurl = $node->getElementsByTagName('url')->item(0)->nodeValue;
$friendsname = $node->getElementsByTagName('name')->item(0)->nodeValue;
$op .= '<div class="lastfm_record">
<a class="mosaic-overlay '.$effect .' ' .$effect_xy.'" href="'.$friendsurl.'" target="_blank" title="'.$friendsname.'">
<div class="details">
<p>'.$friendsname.'</p>
</div>
</a>
<a href="'.$friendsurl.'" target="_blank"><img src="'.$friendsavatar.'" alt="'.$friendsname.'" title="'.$friendsname.'"/></a>
</div>'."\n";
if($i++ >= $friendscount)
break;
}
}
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Friends');
return '<li class="records_tab" id="lastfmfriends'.$caller.'">'.$op.'</li>'."\n";;
}
/*
* get list of favourite tracks
*/
private function get_loved_tracks($count=false, $echotitle=false, $caller=''){
$transient = $this->getTransient('loved');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false){
$cover_size = $this->getCoversize();
$nocoverurl = $this->getNoCoverUrl();
$username = $this->getUsername();
$effect_xy = $this->getEffectXY();
$effect = $this->getEffect();
$lovedtracks = new DOMDocument();
$title = $this->options['loved_title'];
$noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
$lovedcount = $count;
else
$lovedcount = $this->options['lovedcount'];
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($lovedtracks->load('http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user='.$username.'&limit='.$lovedcount.'&api_key='.$this->api_key)){
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
$i=1;
foreach ($lovedtracks->getElementsByTagName('track') as $node){
$lovedurl = $node->getElementsByTagName('url')->item(0)->nodeValue;
$t_artist = $node->getElementsByTagName('artist')->item(0);
$artistname = $t_artist->getElementsByTagName('name')->item(0)->nodeValue;
$artisturl = $t_artist->getElementsByTagName('url')->item(0)->nodeValue;
$lovedtrack = $node->getElementsByTagName('name')->item(0)->nodeValue;
$t_lovedcover = $node->getElementsByTagName('image')->item($cover_size);
//set cover for not found cover art
if (isset($t_lovedcover->nodeValue) && ($t_lovedcover->nodeValue) )
$lovedcover = $t_lovedcover->nodeValue;
elseif (isset($this->options['enable_artistimage']) && intval($this->options['enable_artistimage']))
$lovedcover = $this->get_artist_image($artistname, $cover_size, $nocoverurl);
else
$lovedcover = $nocoverurl;
$op .= '<div class="lastfm_record">
<a class="mosaic-overlay '.$effect.' '.$effect_xy.'" href="'.$lovedurl.'" target="_blank" ' . $noFollow . ' title="'.$lovedtrack.'">
<div class="details">
<p>'.$artistname.'</p>
<p>'.$lovedtrack.'</p>
</div>
</a>
<a href="'.$lovedurl.'" target="_blank" ' . $noFollow . ' title="'.$lovedtrack . '">
<img src="'.$lovedcover.'" alt="'.$lovedtrack.'" />
</a>
</div>'."\n";
if($i++ >= $lovedcount)
break;
}
}
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Favorites');
return '<li class="records_tab" id="lastfmloved'.$caller.'">'.$op.'</li>'."\n";
}
/*
* get list of shouts
*/
private function get_shouts($count=false, $echotitle=false, $caller=''){
$transient = $this->getTransient('shouts');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false){
$username = $this->getUsername();
$shouts = new DOMDocument();
$title = $this->options['shouts_title'];
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
$shoutcount = $count;
else
$shoutcount = $this->options['shoutcount'];
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($shouts->load('http://ws.audioscrobbler.com/2.0/?method=user.getshouts&user='.$username.'&api_key='.$this->api_key)){
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
$i=1;
foreach ($shouts->getElementsByTagName('shout') as $node){
$shout = $node->getElementsByTagName('body')->item(0)->nodeValue;
$author = $node->getElementsByTagName('author')->item(0)->nodeValue;
$date = $node->getElementsByTagName('date')->item(0)->nodeValue;
$op .= '<div class="lastfm_shout"><p>'.$shout.'</p><p>'.$author.'</p><p>'.$date.'</p></div>'."\n";
if($i++ >= $shoutcount)
break;
}
}
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Shouts');
return '<li class="records_tab" id="lastfmshouts'.$caller.'">'.$op.'</li>'."\n";
}
/*
* get user information
*/
private function get_userinfo($echotitle=false, $caller=''){
$transient = $this->getTransient('info');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false){
$username = $this->getUsername();
$cover_size = $this->getCoversize();
$userinfo = new DOMDocument();
$title = $this->options['info_title'];
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($userinfo->load('http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user='.$username.'&api_key='.$this->api_key)){
$id = $userinfo->getElementsByTagName('id')->item(0)->nodeValue;
$name = $userinfo->getElementsByTagName('name')->item(0)->nodeValue;
$real = $userinfo->getElementsByTagName('realname')->item(0)->nodeValue;
$user_url = $userinfo->getElementsByTagName('url')->item(0)->nodeValue;
$userimage = $userinfo->getElementsByTagName('image')->item($cover_size)->nodeValue;
$country = $userinfo->getElementsByTagName('country')->item(0)->nodeValue;
$age = $userinfo->getElementsByTagName('age')->item(0)->nodeValue;
$gender = $userinfo->getElementsByTagName('gender')->item(0)->nodeValue;
$playcount = $userinfo->getElementsByTagName('playcount')->item(0)->nodeValue;
$registered = $userinfo->getElementsByTagName('registered')->item(0)->nodeValue;
}
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
$op .= "\n".'
<p><img src="'.$userimage.'" alt="'.$name.'"/></p>
<p>'. __('ID: ','lastfm_tabs').$id.'</p>
<p>'. __('Nick: ','lastfm_tabs').$name.'</p>
<p>'. __('Name: ','lastfm_tabs').$real.'</p>
<p>'. __('Country: ','lastfm_tabs').$country.'</p>
<p>'. __('Age: ','lastfm_tabs').$age.'</p>
<p>'. __('Gender: ','lastfm_tabs').$gender.'</p>
<p>'. __('Playcount: ','lastfm_tabs').$playcount.'</p>
<p><a href="'.$user_url.'">'. __('Profilepage','lastfm_tabs').'</a></p>
<p>'. __('Registered: ','lastfm_tabs').$registered.'</p>'."\n";
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Profile');
return '<li class="records_tab" id="lastfminfo'.$caller.'">'.$op.'</li>'."\n";
}
/*
* get charts
*/
private function get_charts($count=false, $echotitle=false, $caller=''){
$transient = $this->getTransient('charts');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false){
$username = $this->getUsername();
$nocoverurl = $this->getNoCoverUrl();
$cover_size = $this->getCoversize();
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
$charts = new DOMDocument();
$noFollow = $this->getNoFollow();
$title = $this->options['charts_title'];
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
$chartscount = $count;
else
$chartscount = $this->options['chartscount'];
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($charts->load('http://ws.audioscrobbler.com/2.0/?method=user.getWeeklyAlbumChart&user='.$username.'&api_key='.$this->api_key)){
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
$i=1;
foreach ($charts->getElementsByTagName('album') as $node){
$artistname = $node->getElementsByTagName('artist')->item(0)->nodeValue;
$mbid = $node->getElementsByTagName('mbid')->item(0)->nodeValue;
$albumname = $node->getElementsByTagName('name')->item(0)->nodeValue;
$playcount = $node->getElementsByTagName('playcount')->item(0)->nodeValue;
$url = $node->getElementsByTagName('url')->item(0)->nodeValue;
if ($mbid != '')
$chartcover = $this->get_album_image($mbid, $cover_size);
else
$chartcover = $this->get_album_image2($artistname, $albumname, $cover_size);
if ( $chartcover == false && isset($this->options['enable_artistimage']) && intval($this->options['enable_artistimage']))
$chartcover = $this->get_artist_image($artistname, $cover_size, $nocoverurl);
elseif ( $chartcover == false )
$chartcover = $nocoverurl;
$op .= '<div class="lastfm_record">
<a class="mosaic-overlay '.$effect.' '. $effect_xy .'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$artistname.'">
<div class="details">
<p>'.$artistname.'</p>
<p>'.$playcount.'</p>
</div>
</a>
<a href="'.$url.'" target="_blank" title="'.$artistname.'" ' . $noFollow . '>
<img src="'.$chartcover.'" alt="'.$artistname.'" title="'.$artistname.'" />
</a>
</div>'."\n";
if($i++ >= $chartscount)
break;
}
}
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Charts');
return '<li class="records_tab" id="lastfmcharts'.$caller.'">'.$op.'</li>'."\n";
}
/*
* get top albums
*/
private function get_top_albums($count=false, $echotitle=false, $caller=''){
$transient = $this->getTransient('top_albums');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false){
$username = $this->getUsername();
$nocoverurl = $this->getNoCoverUrl();
$cover_size = $this->getCoversize();
$period = $this->options['top_albums_period'];
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
$title = $this->options['top_albums_title'];
$noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
$topalbumscount = $count;
else
$topalbumscount = $this->options['top_albums_count'];
switch($period){
case 0:
$period = 'overall';
break;
case 1:
$period = '7day';
break;
case 2:
$period = '3month';
break;
case 3:
$period = '6month';
break;
case 4:
default:
$period = '12month';
break;
}
$topalbums = new DOMDocument();
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($topalbums->load('http://ws.audioscrobbler.com/2.0/?method=user.getTopAlbums&user='.$username.'&period='.$period.'&limit='.$topalbumscount.'&api_key='.$this->api_key)){
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
$i=1;
foreach ($topalbums->getElementsByTagName('album') as $node){
$artistname = $node->getElementsByTagName('artist')->item(0);
foreach ($artistname->getElementsByTagName('name') as $name)
$artistname = $name->nodeValue;
$albumname = $node->getElementsByTagName('name')->item(0)->nodeValue;
$playcount = $node->getElementsByTagName('playcount')->item(0)->nodeValue;
$url = $node->getElementsByTagName('url')->item(0)->nodeValue;
$t_cover = $node->getElementsByTagName('image')->item($cover_size);
//set cover for not found cover art
if (isset($t_cover->nodeValue) && ($t_cover->nodeValue) )
$cover = $t_cover->nodeValue;
elseif (isset($this->options['enable_artistimage']) && intval($this->options['enable_artistimage']))
$cover = $this->get_artist_image($artistname, $cover_size, $nocoverurl);
else
$cover = $nocoverurl;
$op .= '<div class="lastfm_record">
<a class="mosaic-overlay '.$effect . ' '. $effect_xy .'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$albumname.'">
<div class="details">
<p>'.$albumname.'</p>
<p>'.__('by ','lastfm_tabs') . $artistname.'</p>
<p>'.sprintf(__("Played %d times.",'lastfm_tabs'), $playcount) .'</p>
</div>
</a>
<a href="'.$url.'" target="_blank" title="'.$albumname.'" ' . $noFollow . '>
<img src="'.$cover.'" alt="'.$albumname.'" title="'.$albumname.'" />
</a>
</div>'."\n";
if($i++ >= $topalbumscount)
break;
}
}
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Top albums');
return '<li class="records_tab" id="lastfmtopalbums'.$caller.'">'.$op.'</li>'."\n";
}
/*
* get top artists
*/
private function get_top_artists($count=false, $echotitle=false, $caller=''){
$transient = $this->getTransient('top_artists');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false){
$title = $this->options['top_artists_title'];
$username = $this->getUsername();
$nocoverurl = $this->getNoCoverUrl();
$cover_size = $this->getCoversize();
$topartists = new DOMDocument();
$period = $this->options['top_artists_period'];
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
$noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
$topartistscount = $count;
else
$topartistscount = $this->options['top_artists_count'];
switch($period){
case 0:
$period = 'overall';
break;
case 1:
$period = '7day';
break;
case 2:
$period = '3month';
break;
case 3:
$period = '6month';
break;
case 4:
default:
$period = '12month';
break;
}
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($topartists->load('http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user='.$username.'&period='.$period.'&limit='.$topartistscount.'&api_key='.$this->api_key)){
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
$i=1;
foreach ($topartists->getElementsByTagName('artist') as $node){
$artistname = $node->getElementsByTagName('name')->item(0)->nodeValue;
$playcount = $node->getElementsByTagName('playcount')->item(0)->nodeValue;
$url = $node->getElementsByTagName('url')->item(0)->nodeValue;
$t_cover = $node->getElementsByTagName('image')->item($cover_size);
//set cover for not found cover art
if (isset($t_cover->nodeValue) && ($t_cover->nodeValue) )
$cover = $t_cover->nodeValue;
elseif (isset($this->options['enable_artistimage']) && intval($this->options['enable_artistimage']))
$cover = $this->get_artist_image($artistname, $cover_size, $nocoverurl);
else
$cover = $nocoverurl;
$op .= '<div class="lastfm_record">
<a class="mosaic-overlay '.$effect.' '.$effect_xy.'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$artistname.'">
<div class="details">
<p>'.$artistname.'</p>
<p>'.sprintf(__("Played %d times.",'lastfm_tabs'), $playcount) .'</p>
</div>
</a>
<a href="'.$url.'" target="_blank" title="'.$artistname.'" ' . $noFollow . '>
<img src="'.$cover.'" alt="'.$artistname.'" title="'.$artistname.'" />
</a>
</div>'."\n";
if($i++ >= $topartistscount)
break;
}
}
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Top artists');
return '<li class="records_tab" id="lastfmtopartists'.$caller.'">'.$op.'</li>'."\n";
}
/*
* get recently played tracks
*/
private function get_recently_played($count=false, $echotitle=false, $caller=''){
$transient = $this->getTransient('recently');
if ($caller == '')
$op = false;
else
$op = get_transient($transient);
if ($op === false ){
$cover_size = $this->getCoversize();
$nocoverurl = $this->getNoCoverUrl();
$username = $this->getUsername();
$effect = $this->getEffect();
$effect_xy = $this->getEffectXY();
$coverinfo = new DOMDocument();
$title = $this->options['recently_title'];
$noFollow = $this->getNoFollow();
if ($this->getDebug() === true)
printf(__('%s data not cached. ','lastfm_tabs'), $title);
if ($count !== false && intval($count) > 0)
$trackcount = $count;
else
$trackcount = $this->options['trackcount'];
if ($echotitle !== false)
$op = '<h2>'.$title.'</h2>';
try{
set_error_handler(array($this,'handle_xml_errors'));
if ($coverinfo->load('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='.$username.'&limit='.$trackcount.'&api_key='.$this->api_key)){
$i = 1;
foreach ($coverinfo->getElementsByTagName('track') as $node){
$song = $node->getElementsByTagName('name')->item(0)->nodeValue;
$artist = $node->getElementsByTagName('artist')->item(0)->nodeValue;
$url = $node->getElementsByTagName('url')->item(0)->nodeValue;
$t_cover = $node->getElementsByTagName('image')->item($cover_size);
//set cover for not found cover art
if (isset($t_cover->nodeValue) && ($t_cover->nodeValue) )
$cover = $t_cover->nodeValue;
elseif (isset($this->options['enable_artistimage']) && intval($this->options['enable_artistimage']))
$cover = $this->get_artist_image($artist, $cover_size, $nocoverurl);
else
$cover = $nocoverurl;
$op .= '<div class="lastfm_record">
<a class="mosaic-overlay '.$effect . ' '. $effect_xy .'" href="'.$url.'" target="_blank" ' . $noFollow . ' title="'.$artist.'">
<div class="details">
<p>'.$song.'</p>
<p>'.$artist.'</p>
</div>
</a>
<a href="'.$url.'" target="_blank" ' . $noFollow . '>
<img src="'.$cover.'" alt="'.$song.'" title="'.$artist.' - '.$song.'"/>
</a>
</div>'."\n";
if($i++ >= $trackcount)
break;
}
}
restore_error_handler();
}
catch (Exception $e){
if ($this->getDebug() === true)
echo $e->getMessage();
}
if ($caller != '')
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Recently played');
return '<li class="records_tab" id="lastfmcovers'.$caller.'">'.$op.'</li>'."\n";
}
/*
* build widget output
*/
function tabs_main($args){
extract($args);
$widgettitle = $this->options['widget_title'];
$tabnav = '';
$widgets = '';
//build navigation and tabs if enabled
if (isset($this->options['enable_recently']) && intval($this->options['enable_recently'])){
$widgets .= $this->get_recently_played();
$tabnav .= '<li><a href="#lastfmcovers">'.$this->options['recently_title'].'</a></li>';
}
if (isset($this->options['enable_lovedtracks']) && intval($this->options['enable_lovedtracks'])){
$widgets .= $this->get_loved_tracks();
$tabnav .= '<li><a href="#lastfmloved">'.$this->options['loved_title'].'</a></li>';
}
if (isset($this->options['enable_charts']) && intval($this->options['enable_charts'])){
$widgets .= $this->get_charts();
$tabnav .= '<li><a href="#lastfmcharts">'.$this->options['charts_title'].'</a></li>';
}
if (isset($this->options['enable_top_albums']) && intval($this->options['enable_top_albums'])){
$widgets .= $this->get_top_albums();
$tabnav .= '<li><a href="#lastfmtopalbums">'.$this->options['top_albums_title'].'</a></li>';
}
if (isset($this->options['enable_top_artists']) && intval($this->options['enable_top_artists'])){
$widgets .= $this->get_top_artists();
$tabnav .= '<li><a href="#lastfmtopartists">'.$this->options['top_artists_title'].'</a></li>';
}
if (isset($this->options['enable_friends']) && intval($this->options['enable_friends'])){
$widgets .= $this->get_friends();
$tabnav .= '<li><a href="#lastfmfriends">'.$this->options['friends_title'].'</a></li>';
}
if (isset($this->options['enable_info']) && intval($this->options['enable_info'])){
$widgets .= $this->get_userinfo();
$tabnav .= '<li><a href="#lastfminfo">'.$this->options['info_title'].'</a></li>';
}
if (isset($this->options['enable_shouts']) && intval($this->options['enable_shouts'])){
$widgets .= $this->get_shouts();
$tabnav .= '<li><a href="#lastfmshouts">'.$this->options['shouts_title'].'</a></li>';
}
//tabbed navigation
$tabnav = '<ul class="idTabs molastfm clearfix">'.$tabnav.'</ul>'."\n";
//widget start
$widget_output = $before_widget . $before_title . $widgettitle . $after_title. "\n";
$widget_output .= '<div id="lastfmrecords">'.$tabnav.'<ul id="lastfm_inside">';
//echo cache info in debug mode
if ($this->getDebug() === true){
$timestamp = time();
$extime = date("H:i:s", $timestamp);
printf(__('Cacherefresh done at %s','lastfm_tabs'), $extime);
}
$widget_output .= $widgets.'</ul></div>' . $after_widget . "\n";
return $widget_output;
}
/*
* widget output
*/
function widget_output($args){
$transient = $this->getTransient('widget');
$op = get_transient($transient);
if ($op === false){
if ($this->getDebug() === true)
_e('Data not cached.','lastfm_tabs');
$op = $this->tabs_main($args, $this->options);
set_transient($transient, $op, $this->getExpiration());
}
elseif ($this->getDebug() === true)
printf(__('%s data was cached. ','lastfm_tabs'), 'Widget');
echo $op;
}
/*
* template tag function
*/
public function lastfm_tabs_tag($functions=array(), $count=false, $echotitle=false){
if ($echotitle !== false)
$echotitle = true;
if ($count === false || intval($count) <= 0)
$count = 3;
$caller = 'template';
/*
$transient = 'lastfm_tabs_template_cache';
$op = get_transient($transient);
*/
$op = false;
if ($op === false){
if ($this->getDebug() === true)
_e('Data not cached.','lastfm_tabs');
$op = '';
if (!is_array($functions))
$functions = array($functions);
foreach($functions as $function){
switch($function){
case('recent'):
$op .= $this->get_recently_played($count, $echotitle, $caller);
break;
case('friends'):
$op .= $this->get_friends($count, $echotitle, $caller);
break;
case('info'):
$op .= $this->get_userinfo($echotitle, $caller);
break;
case('charts'):
$op .= $this->get_charts($count, $echotitle, $caller);
break;
case('artists'):
$op .= $this->get_top_artists($count, $echotitle, $caller);
break;
case('albums'):
$op .= $this->get_top_albums($count, $echotitle, $caller);
break;
case('shouts'):
$op .= $this->get_shouts($count, $echotitle, $caller);
break;
case('loved'):
$op .= $this->get_loved_tracks($count, $echotitle, $caller);
break;
default:
$op .= __('Invalid function call','lastfm_tabs') . ' "' . $function .'". ' . __('Use ','lastfm_tabs') . ' friends, info, charts, artists, albums, shouts, recent '.__('or','lastfm_tabs').' loved';
return $op;
}
}
$op = '<ul class="lastfm_widget">'.$op.'</ul>';
// set_transient($transient, $op, $this->getExpiration());
}
/*
elseif ($this->getDebug() === true)
_e('Data was cached.','lastfm_tabs');
*/
return $op;
}
/*
* enable slide/fade effect
*/
function hover(){
if (isset($this->options['fancyness']) && intval($this->options['fancyness'])){
//wp_register_script('fancy_hover',LASTFM_URI.'/fancy_hover.js',array('jquery'),'1.0');
wp_register_script('mosaic',LASTFM_URI.'/js/mosaic.min.js',array('jquery'),'1.0.1');
//wp_register_script('lastfm_ajax',LASTFM_URI.'/js/ajax.js',array('jquery'),'1.0.1');
//wp_enqueue_script('lastfm_ajax');
wp_enqueue_script('mosaic');
}
}
/*
* enable tabbed navigation?
*/
function tabs(){
if (isset($this->options['enable_tabs']) && intval($this->options['enable_tabs'])){
wp_register_script('idtabs',LASTFM_URI.'/js/idtab.js',array('jquery'),'2.2');
wp_enqueue_script('idtabs');
}
}
/*
* optional stylesheets
*/
function style(){
if (isset($this->options['use_stylesheet']) && !intval($this->options['use_stylesheet']))
return;
if (@file_exists(STYLESHEETPATH.'/lastfm.css'))
$css_file = get_stylesheet_directory_uri() . '/lastfm.css';
elseif (@file_exists(TEMPLATEPATH.'/lastfm.css'))
$css_file = get_template_directory_uri() . '/lastfm.css';
else
$css_file = plugins_url('css/lastfm_tabs.css', __FILE__);
wp_enqueue_style('lastfm_tabs_mosaic', $css_file, false, '1.0.1', 'all');
add_action('wp_head', array($this,'mosaic_init'));
}
/*
* mosaic initialisation
*/
function mosaic_init(){
$origin = $this->getEffectXY();
?>
<script type="text/javascript">
jQuery(function(){
jQuery('.lastfm_record').mosaic({
animation : '<?php echo $this->options['effect'];?>',
speed : <?php echo $this->options['effect_duration'];?>,
<?php switch($origin){
case 'left':
echo 'anchor_x : \'left\'';
break;
case 'right':
echo 'anchor_x : \'right\'';
break;
case 'top':
echo 'anchor_y : \'top\'';
break;
default:
case 'bottom':
echo 'anchor_y : \'bottom\'';
break;
}?>
});
});
</script>
<?php
}
}
$lastfm_tabs = new lastfm_tabs();
$lastfm_options = new lastfm_options($lastfm_tabs);
function lastfm_tabs_render($functions=array(), $count=false, $echotitle=false, $return=false){
$lastfm_tabs = new lastfm_tabs();
if ($return === true)
return $lastfm_tabs->lastfm_tabs_tag($functions, $count, $echotitle);
else
echo $lastfm_tabs->lastfm_tabs_tag($functions, $count, $echotitle);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment