This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dude-lastfm-feed.php
133 lines (113 loc) · 3.57 KB
/
dude-lastfm-feed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Plugin Name: Dude last.fm feed
* Plugin URL: https://www.dude.fi
* Description: Fetches the latest scrobbles from last.fm user
* Version: 0.1.0
* Author: Timi Wahalahti / DUDE
* Author URL: http://dude.fi
* Requires at least: 4.4.2
* Tested up to: 4.4.2
*
* Text Domain: dude-lastfm-feed
* Domain Path: /languages
*/
if( !defined( 'ABSPATH' ) )
exit();
Class Dude_Lastfm_Feed {
private static $_instance = null;
/**
* Construct everything and begin the magic!
*
* @since 0.1.0
* @version 0.1.0
*/
public function __construct() {
// Add actions to make magic happen
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
} // end function __construct
/**
* Prevent cloning
*
* @since 0.1.0
* @version 0.1.0
*/
public function __clone () {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'dude-lastfm-feed' ) );
} // end function __clone
/**
* Prevent unserializing instances of this class
*
* @since 0.1.0
* @version 0.1.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'dude-lastfm-feed' ) );
} // end function __wakeup
/**
* Ensure that only one instance of this class is loaded and can be loaded
*
* @since 0.1.0
* @version 0.1.0
* @return Main instance
*/
public static function instance() {
if( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} // end function instance
/**
* Load plugin localisation
*
* @since 0.1.0
* @version 0.1.0
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'dude-lastfm-feed', false, dirname( plugin_basename( __FILE__ ) ).'/languages/' );
} // end function load_plugin_textdomain
public function get_user_scrobbles( $username = '' ) {
if( empty( $username ) )
return;
$transient_name = apply_filters( 'dude-lastfm-feed/user_scrobbles_transient', 'dude-lastfm-user-'.$username, $username );
$scrobbles = get_transient( $transient_name );
if( !empty( $scrobbles ) || false != $scrobbles )
return $scrobbles;
$parameters = array(
'method' => 'user.getRecentTracks',
'api_key' => apply_filters( 'dude-lastfm-feed/api_key', '' ),
'format' => 'json',
'user' => $username,
'limit' => '5',
);
$response = self::_call_api( apply_filters( 'dude-lastfm-feed/user_scrobbles_parameters', $parameters ) );
if( $response === FALSE )
return;
$response = apply_filters( 'dude-lastfm-feed/user_scrobbles', json_decode( $response['body'], true ) );
set_transient( $transient_name, $response, apply_filters( 'dude-lastfm-feed/user_scrobbles_lifetime', '600' ) );
return $response;
} // end function get_users_scrobbles
private function _call_api( $parameters = array() ) {
if( empty( $parameters ) )
return false;
$parameters = http_build_query( $parameters );
$response = wp_remote_get( 'http://ws.audioscrobbler.com/2.0/?'.$parameters );
if( $response['response']['code'] !== 200 ) {
self::_write_log( 'response status code not 200 OK, function: '.$parameters['endpoint'] );
return false;
}
return $response;
} // end function _call_api
private function _write_log ( $log ) {
if( true === WP_DEBUG ) {
if( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
} // end _write_log
} // end class Dude_Lastfm_Feed
function dude_lastfm_feed() {
return new Dude_Lastfm_Feed();
} // end function dude_lastfm_feed