Beispiel #1
0
function sc_render_manager_page()
{
    require_once plugin_dir_path(__FILE__) . 'includes/Services/Soundcloud.php';
    require_once plugin_dir_path(__FILE__) . 'includes/class/pagination.class.php';
    $options = get_option('sc_options');
    $client = new Services_Soundcloud($options['client_id'], $options['client_secret'], $options['callback_url']);
    // Error message if app settings are not filled in.
    if ($options['client_id'] == "Your client ID goes here" || $options['client_secret'] == "Your client secret goes here" || $options['callback_url'] == "Your callback url goes here") {
        echo '<div class="error fade"><p>Warning: One or more of the required fields have not been set. Please insure that your cliend ID, client secret, and callback url are defined <a href="' . admin_url('options-general.php?page=soundcloud/soundcloud.php') . '">HERE</a>.</br>Soundcloud functionality will not be available until these fields are set.</p></div></br>';
    }
    // SoundCloud login/logout
    if (isset($_GET['code']) || get_option('soundcloud_access_token')) {
        echo '<a href="' . admin_url('admin.php?page=soundcloud-disconnect') . '" class="soundcloud-icon"><img src="' . plugins_url() . '/soundcloud/includes/images/btn-disconnect-l.png" alt="Disconnect"/></a>';
        try {
            $client = new Services_Soundcloud($options['client_id'], $options['client_secret'], $options['callback_url']);
            if (isset($_GET['code'])) {
                $accessToken = $client->accessToken($_GET['code']);
                update_option('soundcloud_access_token', $accessToken['access_token']);
                update_option('soundcloud_refresh_token', $accessToken['refresh_token']);
                update_option('soundcloud_token_expiration', time() + $accessToken['expires_in']);
            } else {
                if (get_option('soundcloud_access_token')) {
                    // refresh token code deprecated due to non-expiring tokens
                    /*if (time() > get_option('soundcloud_token_expiration')) {
                    			$client->setAccessToken(get_option('soundcloud_access_token'));
                    			
                    			// refresh access token
                    			$accessToken = $client->accessTokenRefresh(get_option('soundcloud_refresh_token'));	
                    			//$client->setAccessToken($accessToken['access_token']);							
                    			update_option('soundcloud_access_token', $accessToken['access_token']);
                    			update_option('soundcloud_refresh_token', $accessToken['refresh_token']);
                    			update_option('soundcloud_token_expiration', time() + $accessToken['expires_in']);
                    		}
                    		else
                    		{*/
                    $client->setAccessToken(get_option('soundcloud_access_token'));
                    //}
                }
            }
        } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
            var_dump($e->getMessage());
            exit;
        }
    } else {
        echo '<a href="' . $client->getAuthorizeUrl(array('scope' => 'non-expiring')) . '" class="soundcloud-icon"><img src="' . plugins_url() . '/soundcloud/includes/images/btn-connect-sc-l.png" alt="Connect with Soundcloud"/></a>';
    }
    // tab switching
    if (isset($_GET['tab'])) {
        sc_admin_tabs($_GET['tab']);
        $tab = $_GET['tab'];
    } else {
        sc_admin_tabs('user');
        $tab = 'user';
    }
    if (get_option('soundcloud_access_token')) {
        try {
            $me = json_decode($client->get('me'), true);
        } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
            var_dump($e->getMessage());
            exit;
        }
        echo '<h3>Logged in as: ' . $me['username'] . '</h3>';
    } else {
        echo '<h3>Not logged in.</h3>';
    }
    switch ($tab) {
        case 'user':
            try {
                $me = json_decode($client->get('me'), true);
                ?>
				<form action="" method="post">
					<table class="form-table">
						<tr valign="top">
						  <th scope="row"><label for="username">Username:</label></th>
						  <td><input type="text" name="username" value="<?php 
                echo $me['username'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="permalink">Permalink:</label></th>
						  <td><input type="text" name="permalink" value="<?php 
                echo $me['permalink'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="description">Description:</label></th>
						  <td><input type="text" name="description" value="<?php 
                echo $me['description'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="website">Website:</label></th>
						  <td><input type="text" name="website" value="<?php 
                echo $me['website'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="website_title">Website Name:</label></th>
						  <td><input type="text" name="website_title" value="<?php 
                echo $me['website_title'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
					</table>
					<p class="submit"><input type="submit" value="Update Account" class="button-primary"></p>
					
				</form>
				<?php 
                if (isset($_POST['username']) || isset($_POST['permalink']) || isset($_POST['description']) || isset($_POST['website']) || isset($_POST['website_title'])) {
                    $response = json_decode($client->put('me', array('user[username]' => $_POST['username'], 'user[permalink]' => $_POST['permalink'], 'user[description]' => $_POST['description'], 'user[website]' => strlen($_POST['website']) ? $_POST['website'] : null, 'user[website_title]' => strlen($_POST['website_title']) ? $_POST['website_title'] : null)));
                }
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                var_dump($e->getMessage());
                exit;
            }
            break;
        case 'upload':
            ?>
	
			<form action="" enctype="multipart/form-data" method="post" id ="add_track">
				<table class="form-table">
					<tr valign="top">
					  <th scope="row"><label for="track_title">Track title:</label></th>
					  <td><input type="text" name="track_title" size="30" class="regular-text code"></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="track_file">Please specify a track:</label></th>
					  <td><input type="file" name="track_file" id="track_file" size="40" accept="audio/*" class="regular-text code"></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="track_art">Please specify track artwork:</label></th>
					  <td><input type="file" name="track_art" id="track_art" size="40" accept="image/*" class="regular-text code"></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="tag">Track tags:</label></th>
					  <td><input type="text" name="tag" id="tag" size="30"><input type="button" id="add_tag" name="add_tag" class="button" value="Add Tag" />
						<br><ul id="tags"></ul></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="sharing">Track privacy:</label></th>
					  <td><select name="sharing">
						<option value="public">Public</option>
						<option value="private">Private</option>
					  </select></td>
					</tr>
					</table>
			<p class="submit"><input type="submit" value="Upload" class="button-primary"></p>
			</form>					
			<?php 
            try {
                $tmp_file = '/tmp/' . stripslashes($_FILES['track_file']['name']);
                $tmp_art_file = '/tmp/' . stripslashes($_FILES['track_art']['name']);
                if (move_uploaded_file($_FILES['track_file']['tmp_name'], $tmp_file) && move_uploaded_file($_FILES['track_art']['tmp_name'], $tmp_art_file)) {
                    // upload audio file
                    $track = json_decode($client->post('tracks', array('track[title]' => $_POST['track_title'], 'track[asset_data]' => '@' . $tmp_file, 'track[artwork_data]' => '@' . $tmp_art_file, 'track[tags]' => strlen($_POST['tags']) ? $_POST['tags'] : null, 'track[sharing]' => $_POST['sharing'])));
                    unlink(realpath($tmp_file));
                    unlink(realpath($tmp_art_file));
                }
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                var_dump($e->getMessage());
                exit;
            }
            break;
        case 'tracks':
            try {
                echo '</br>';
                $page_size = 5;
                // Pagination code
                $p = new pagination();
                $p->items($me['track_count']);
                $p->limit($page_size);
                // Limit entries per page
                $p->target("admin.php?page=soundcloud-manager&tab=tracks");
                $p->currentPage($_GET[$p->paging]);
                // Gets and validates the current page
                $p->calculate();
                // Calculates what to show
                $p->parameterName('paging');
                $p->adjacents(1);
                //No. of page away from the current page
                if (!isset($_GET['paging'])) {
                    $p->page = 1;
                } else {
                    $p->page = $_GET['paging'];
                }
                if ($p->page == 1) {
                    // get first page of tracks
                    $tracks = json_decode($client->get('users/' . $me['id'] . '/tracks', array('order' => 'created_at', 'limit' => $page_size)));
                } else {
                    // get additional pages of tracks
                    $tracks = json_decode($client->get('users/' . $me['id'] . '/tracks', array('order' => 'created_at', 'limit' => $page_size, 'offset' => $page_size * $page)));
                }
                ?>
				<div class="tablenav">
					<div class='tablenav-pages'>
						<?php 
                echo $p->show();
                ?>
					</div>
				</div>
				<?php 
                foreach ($tracks as $track) {
                    $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
                    $embed_info = json_decode($client->get('oembed', array('url' => $track->permalink_url)));
                    // render the html for the player widget
                    echo $embed_info->html . '</br></br>';
                }
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                var_dump($e->getMessage());
                exit;
            }
            break;
    }
}
    /**
     * Import the podcasts using the SoundCloud API
     * 
     * @since   1.0.0
     */
    public function import_podcasts()
    {
        require_once plugin_dir_path(__FILE__) . 'assets/php-soundcloud-master/Services/Soundcloud.php';
        $output = '';
        $options = get_option('hbi_soundcloud_podcasts_settings');
        $client = new Services_Soundcloud($options['soundcloud_client_id'], $options['soundcloud_client_secret'], $options['soundcloud_redirect_uri']);
        $soundcloud_user_id = $options['soundcloud_user_id'];
        $access_token = $client->setAccessToken();
        $current_time = current_time('timestamp') - 21600;
        // grab the tracks
        $tracks = json_decode($client->get('users/' . $soundcloud_user_id . '/tracks', array('created_at[from]' => date('Y-m-d', $current_time))));
        if (count($tracks)) {
            $output .= "Currently processing " . count($tracks) . " tracks.\r\n";
            $totalimport = 0;
            // loop through the tracks
            foreach ($tracks as $track) {
                if ($track->downloadable != 1) {
                    $output .= "Track " . $track->id . " is not set to downloadable. \r\n";
                    $downloadable = <<<EOH
<track>
    <downloadable>true</downloadable>
</track>
EOH;
                    try {
                        $client->put('tracks/' . $track->id, $downloadable, array(CURLOPT_HTTPHEADER => array('Content-Type: application/xml')));
                        $output .= "Updated Track " . $track->id . " in SoundCloud to make sure it was downloadable.\r\n\r\n";
                    } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                        $output .= "Error setting track " . $track->id . "to be downloadable. " . $e->getMessage() . "\r\n\r\n";
                    }
                }
                $args = array('post_type' => 'podcasts', 'meta_key' => 'soundcloud-track-id', 'post_status' => 'any', 'meta_value' => $track->id);
                $test = get_posts($args);
                if (count($test) == 0) {
                    $downloadURL = 'http://feeds.soundcloud.com/stream/' . $track->id . '-101sports-' . $track->permalink . '.mp3';
                    $show = 'zach-and-rammer';
                    // Added in trim to try to fix all of the wrong things happening from extra spaces
                    $release = str_replace(' ', '-', strtolower(trim($track->release)));
                    $release = str_replace('&', 'and', $release);
                    if ($release != '') {
                        $show = $release;
                    }
                    $content = array('post_type' => 'podcasts', 'post_status' => 'publish', 'post_title' => $track->title, 'post_content' => $track->description, 'tax_input' => array('podcast-segment' => array(trim(strtolower($track->label_name)))));
                    $post_id = wp_insert_post($content);
                    $term = term_exists($track->label_name, 'podcast_segment');
                    wp_set_post_terms($post_id, $term['term_id'], 'podcast_segment');
                    add_post_meta($post_id, 'soundcloud-file', $downloadURL);
                    add_post_meta($post_id, 'use-soundcloud', 1);
                    add_post_meta($post_id, 'soundcloud-shortcode', '[soundcloud url="http://api.soundcloud.com/tracks/' . $track->id . '" params="" width=" 100%" height="166" iframe="true" /]');
                    add_post_meta($post_id, 'soundcloud-track-id', $track->id);
                    add_post_meta($post_id, 'podcast-show', $show);
                    $totalimport++;
                    $output .= "Imported " . $track->title . "(" . $track->id . ") into the system.\r\n";
                }
            }
            $output .= "Finished processing " . count($tracks) . " tracks. " . $totalimport . " have been imported into the system.\r\n";
        }
    }