Last active
June 24, 2019 17:35
-
-
Save dirkkelly/34e77d54b5a8fcfd052619db759f755e to your computer and use it in GitHub Desktop.
Wordpress Simcast 2.0 API Support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once('Simcast_LifeCycle.php'); | |
class Simcast_Plugin extends Simcast_LifeCycle { | |
/** | |
* See: http://plugin.michael-simpson.com/?page_id=31 | |
* @return array of option meta data. | |
*/ | |
public function getOptionMetaData() { | |
// http://plugin.michael-simpson.com/?page_id=31 | |
return array( | |
//'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades. | |
'SimpleCastAPI' => array(__('SimpleCast API Key', 'simcast-plugin')), | |
'PodcastID' => array(__('Your Podcast ID', 'simcast-plugin')), | |
'ShowEmbeds' => array(__('Show embedded player with each episode?', 'simcast-plugin'), 'false', 'true'), | |
'UseStyling' => array(__('Use styling?', 'simcast-plugin'), 'true', 'false'), | |
'CacheLength' => array(__('How long should the episode list be cached?', 'simcast-plugin'), | |
'One Week', 'One Day', 'One Month') | |
/* | |
'CanDoSomething' => array(__('Which user role can do something', 'simcast-plugin'), | |
'Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber', 'Anyone') | |
*/ | |
); | |
} | |
// protected function getOptionValueI18nString($optionValue) { | |
// $i18nValue = parent::getOptionValueI18nString($optionValue); | |
// return $i18nValue; | |
// } | |
protected function initOptions() { | |
$options = $this->getOptionMetaData(); | |
if (!empty($options)) { | |
foreach ($options as $key => $arr) { | |
if (is_array($arr) && count($arr > 1)) { | |
$this->addOption($key, $arr[1]); | |
} | |
} | |
} | |
} | |
public function getPluginDisplayName() { | |
return 'Simcast'; | |
} | |
protected function getMainPluginFileName() { | |
return 'simcast.php'; | |
} | |
/** | |
* See: http://plugin.michael-simpson.com/?page_id=101 | |
* Called by install() to create any database tables if needed. | |
* Best Practice: | |
* (1) Prefix all table names with $wpdb->prefix | |
* (2) make table names lower case only | |
* @return void | |
*/ | |
protected function installDatabaseTables() { | |
// global $wpdb; | |
// $tableName = $this->prefixTableName('mytable'); | |
// $wpdb->query("CREATE TABLE IF NOT EXISTS `$tableName` ( | |
// `id` INTEGER NOT NULL"); | |
} | |
/** | |
* See: http://plugin.michael-simpson.com/?page_id=101 | |
* Drop plugin-created tables on uninstall. | |
* @return void | |
*/ | |
protected function unInstallDatabaseTables() { | |
// global $wpdb; | |
// $tableName = $this->prefixTableName('mytable'); | |
// $wpdb->query("DROP TABLE IF EXISTS `$tableName`"); | |
} | |
/** | |
* Perform actions when upgrading from version X to version Y | |
* See: http://plugin.michael-simpson.com/?page_id=35 | |
* @return void | |
*/ | |
public function upgrade() { | |
} | |
public function addActionsAndFilters() { | |
// Add options administration page | |
// http://plugin.michael-simpson.com/?page_id=47 | |
add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage')); | |
// Example adding a script & style just for the options administration page | |
// http://plugin.michael-simpson.com/?page_id=47 | |
// if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) { | |
// wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__)); | |
// wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__)); | |
// } | |
// Add Actions & Filters | |
// http://plugin.michael-simpson.com/?page_id=37 | |
// Adding scripts & styles to all pages | |
// Examples: | |
// wp_enqueue_script('jquery'); | |
// wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__)); | |
// wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__)); | |
// Register short codes | |
// http://plugin.michael-simpson.com/?page_id=39 | |
add_shortcode('simcast', array($this, 'doSimcastShortcode')); | |
// Register AJAX hooks | |
// http://plugin.michael-simpson.com/?page_id=41 | |
add_action('wp_ajax_ClearTransients', array(&$this, 'ajaxClearTransients')); | |
} | |
public function ajaxClearTransients() { | |
// Delete the transient | |
delete_transient( 'simplecastdata' ); | |
header("Content-type: application/json"); | |
echo 'Success!'; | |
die(); | |
} | |
public function doSimcastShortcode($atts) { | |
// Add by Erick in V2 | |
extract(shortcode_atts(array( | |
'limit' => '', | |
'hide_player' => '', | |
'link_text' => '' | |
), $atts)); | |
$simcast_api_key = get_option('Simcast_Plugin_SimpleCastAPI'); | |
$simcast_show_id = get_option('Simcast_Plugin_PodcastID'); | |
$show_embeds = get_option('Simcast_Plugin_ShowEmbeds'); | |
$use_styling = get_option('Simcast_Plugin_UseStyling'); | |
$cache_length = get_option('Simcast_Plugin_CacheLength'); | |
switch ($cache_length) { | |
case "One Week": | |
$simcast_cache = WEEK_IN_SECONDS; | |
break; | |
case "One Day": | |
$simcast_cache = DAY_IN_SECONDS; | |
break; | |
case "One Month": | |
$simcast_cache = MONTH_IN_SECONDS; | |
break; | |
} | |
if ($simcast_api_key && $simcast_show_id) { | |
// If the transient is already saved, let's use that data | |
if (get_transient('simplecastdata')) { | |
$json_data = get_transient('simplecastdata'); | |
// If not, then let's get the fresh data and save it to transients | |
} else { | |
// The SimpleCast V2.0 AP | |
$url = 'https://api.simplecast.com/podcasts/'.$simcast_show_id.'/episodes'; | |
$context = stream_context_create(array( | |
'http' => array( | |
'header' => "Authorization: Bearer ".$simcast_api_key."" | |
) | |
)); | |
// Get the feed | |
$data = file_get_contents($url, false, $context); | |
// JSON decode the feed | |
$json_data = json_decode($data); | |
// Set the transient with that data, broken? | |
set_transient('simplecastdata', $json_data, 1 * $simcast_cache); | |
} | |
$x = 0; | |
$feed_data = ''; | |
// Let's loop over that data to produce the list of episodes | |
foreach ($json_data->collection as $episode){ | |
$x++; | |
// A hack to get the iframe URL | |
$sharing_url = $episode->href; | |
$embed_id = substr($sharing_url, 36, 36); | |
if($use_styling == 'true'){ | |
$styles = 'margin-bottom: 24px; padding: 15px 0;'; | |
} else { | |
$styles = ''; | |
} | |
$feed_data .= '<div class="simcast_episode" style="'.$styles.'">'; | |
$feed_data .= '<h2 style="font-size: 2em;">'.$episode->title.'</h2>'; | |
$feed_data .= '<p>'.$episode->description; | |
// API does not. return episode url, need show slug to generate it | |
// $feed_data .= '<a href="'.$sharing_url.'">'; | |
// if($link_text) { | |
// $feed_data .= $link_text; | |
// } else { | |
// $feed_data .= 'Read Full Show Notes →'; | |
// } | |
// $feed_data .= '</a>'; | |
$feed_data .= '</p>'; | |
if($show_embeds == 'true' && $hide_player !== 'true'){ | |
$feed_data .= '<iframe src="https://player.simplecast.com/'.$embed_id.'" width="100%" frameborder="0" height="200px" scrolling="no" seamless></iframe>'; | |
} | |
$feed_data .= '</div>'; | |
if ($x == $limit) { | |
break; | |
} | |
} | |
return $feed_data; | |
} else { | |
return 'Your API key and show ID must be saved in order to display your podcast feed.'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment