'', 'width' => '', 'playops' => '', 'url' => NULL, 'flash' => '', 'tags' => NULL, 'artist' => NULL, 'dj' => NULL, 'collection' => '', 'smart_id' => NULL, 'sort' => '', 'lists' => '', 'is_widget' => '', ), $atts, '8tracks' ) ); //If anything other than a URL is defined, you probably want a collection. if (isset($url)) { $collection = "no"; } else { $collection = "yes"; } //It's either a widget, or it's not. $allowed_widget_options = array( 'yes', 'no', ); if ( !in_array( $is_widget, $allowed_widget_options ) ) $is_widget = 'no'; // Let's set the default width parameter. We'll check the validity of the supplied value via regex. if (preg_match("/^([0-9]+(%?)$)/", $width)) { $width = $width; } else if (($is_widget=="yes") && ($collection=="yes")) { $width = '100%'; } else if (($is_widget=="no") && ($collection=="yes")) { $width = 500; } else if (($is_widget=="yes") && ($collection=="no")) { $width = '100%'; } else { $width = 300; } // Now for the height parameter. We check this the same way as width. if (preg_match("/^([0-9]+(%?)$)/", $height)) { $height = $height; } else if (($is_widget=="yes") && ($collection=="yes")) { $height = 500; } else if (($is_widget=="yes") && ($collection=="no")) { $height = 300; } else if (($is_widget=="no") && ($collection=="yes")) { $height = 500; } else if (($is_widget=="no") && ($collection=="no")) { $height = 250; } // Make sure that a user can only enter a whitelisted set of playops. $allowed_playops = array( 'shuffle', 'autoplay', 'shuffle+autoplay', ); if ( !in_array( $playops, $allowed_playops ) ) $playops = ''; //Tweak the playops for collections: if ($playops=="shuffle" || $playops=="autoplay") { $options = '&options=' . ($playops) . ''; $playops = '/' . ($playops) . ''; } else if ($playops=="shuffle+autoplay") { $options = "&options=shuffle,autoplay"; $playops = '/' . ($playops) . ''; } // Make sure flash has a value. Default is no. if (!isset( $flash['yes'] )) $flash="no"; // Make sure the URL we are loading is from 8tracks.com if (isset($url)) { $url_bits = parse_url( $url ); if ( '8tracks.com' != $url_bits['host'] ) return ''; } //Make sure our sort values are valid. $allowed_sorts = array( 'recent', 'hot', 'popular', ); if ( !in_array( $sort, $allowed_sorts ) ) { $sort = ''; } //Make sure our list settings are valid. $allowed_lists = array( 'liked', 'listen_later', 'listened', 'recommended', ); if ( !in_array( $lists, $allowed_lists ) ) $lists = ''; //These arrays contain character substitutions to ensure the URLs are well-formed for querying 8tracks. $badchars = array(' ', '_', '/', '.', ',', ', '); $goodchars = array('_', '__', '\\', '%5E', '%2B', '%2B'); //We should probably make sure our smart_id is free of non-id elements before processing. $needle1 = "http://8tracks.com/mix_sets/"; $needle2 = "/collections/"; if ((strpos($smart_id, $needle1)) !== false) { $smart_id = str_replace("http://8tracks.com/mix_sets/", "", $smart_id); } //Collection URLs on 8tracks only return JSON. Here, we get that JSON, extract the collection's smart_id, and pass that back to the plugin. else if ((strpos($smart_id, $needle2)) !== false) { $json_body = wp_remote_get( esc_url($smart_id) . '.json' . (api_key) . '' ); $json_data = json_decode($json_body['body'], true); $smart_id = $json_data["collection"]["smart_id"]; } //We'll also make sure that any DJ URLs are stripped down to just the DJ's ID. $dj_needle = "http://8tracks.com/"; if ((strpos($dj, $dj_needle)) !== false) { $dj = str_replace("http://8tracks.com/", "", $dj); } //Let's do some mix set processing: if (is_null($url)) { //Did we specify a sort? Let's make sure that works. if ((in_array( $sort, $allowed_sorts )) && ((isset($tags)) || (isset($artist)) || (isset($dj)))) { $sort = ':' . ($sort) . ''; } //Here, we create the smart id from tags or artist: if (isset($tags)) $smart_id = 'tags:' . str_replace($badchars, $goodchars, $tags) . '' . ($sort) . ''; if (isset($artist)) $smart_id = 'artist:' . str_replace($badchars, $goodchars, $artist) . '' . ($sort) . ''; //We also need to make sure that smart IDs we copy from 8tracks have their characters escaped. if (isset($smart_id)) { $smart_id = str_replace($badchars, $goodchars, $smart_id); } //This handles collections made from smart_id, dj, or sort. if (!is_null($smart_id)) { $the_body = wp_remote_get ('http://8tracks.com/mix_sets/' . ($smart_id) . '.xml' . (api_key) . '' ); } else if (!empty($dj)) { //Not escaping dj strings fixes the problem of missing DJ sets from users with _ in their name. $the_body = wp_remote_get ('http://8tracks.com/' . ($dj) . '.xml' . (api_key) . '' ); } else if (!empty($sort)) { //This handles collections where only sort is set. $the_body = wp_remote_get ('http://8tracks.com/mix_sets/all:' . ($sort) . '.xml' . (api_key) . '' ); } //Error handling for URL processing. if ( is_wp_error( $the_body ) || $the_body['response']['code'] != '200' ) return ''; if ( ! isset( $the_body['body'] ) ) return ''; try { $xml = new SimpleXMLElement( $the_body['body'] ); } catch ( Exception $e ) { return ''; } //Collection processing: if ((!empty($smart_id)) && (empty($dj))) { //This handles smart-ids (as distinct from DJs). $output = '