<?php $authorized = false; $plugin = new Libsyn\Service(); $sanitize = new Libsyn\Service\Sanitize(); $api = $plugin->getApis(); $render = true; $error = false; if (isset($_GET) && is_array($_GET)) { parse_str(http_build_query($_GET)); } if (!isset($_POST['redirect_url'])) { if (isset($_GET)) { $redirectUri = get_site_url() . '/wp-admin/?' . http_build_query($_GET); } else { $redirectUri = get_site_url() . '/wp-admin/'; } } else { $redirectUri = $_POST['redirect_url']; } /* Handle saved api */ if ($api instanceof Libsyn\Api && !$api->isRefreshExpired()) { $refreshApi = $api->refreshToken(); if ($refreshApi) { //successfully refreshed $api = $api->retrieveApiById($api->getPluginApiId()); } else { //in case of a api call error... $handleApi = true; $clientId = !isset($clientId) ? $api->getClientId() : $clientId; $clientSecret = !isset($clientSecret) ? $api->getClientSecret() : $clientSecret;
/** * Main Post script which handles Libsyn API posting. Used for post scheduled/immediate post. * * @param <WP_Post> $post * @param <int> $post_id * @param <bool> $schedule * @param <bool> $draft * * @return <Libsyn_Item|mixed> */ public static function postEpisode($post, $isSchedule = false, $isDraft = false) { /* Back out quickly if the post to libsyn is not checked */ if (get_post_meta($post->ID, 'libsyn-post-episode', true) !== 'isLibsynPost') { return; } $plugin = new Service(); $api = $plugin->getApis(); //testing //get post player settings $playerSettings = array(); $playerSettings['player_use_thumbnail'] = get_option('libsyn-podcasting-player_use_thumbnail'); $playerSettings['player_use_theme'] = get_option('libsyn-podcasting-player_use_theme'); $playerSettings['player_height'] = get_option('libsyn-podcasting-player_height'); $playerSettings['player_width'] = get_option('libsyn-podcasting-player_width'); $playerSettings['player_placement'] = get_option('libsyn-podcasting-player_placement'); //Create item API array $item = array(); $item['show_id'] = $api->getShowId(); $item['item_title'] = $post->post_title; $item['item_subtitle'] = get_post_meta($post->ID, 'libsyn-post-episode-subtitle', true); $item['thumbnail_url'] = get_post_meta($post->ID, 'libsyn-new-media-image', true); $item['item_body'] = $content = wp_kses_post(self::stripShortcode('podcast', self::stripShortcode('podcast', $post->post_content))); $item['item_category'] = get_post_meta($post->ID, 'libsyn-post-episode-category-selection', true); $item['itunes_explicit'] = get_post_meta($post->ID, 'libsyn-post-episode-itunes', true); if ($item['itunes_explicit'] === 'explicit') { $item['itunes_explicit'] = 'yes'; } $item['tv_rating'] = get_post_meta($post->ID, 'libsyn-post-episode-tvrating', true); if ($item['tv_rating'] === 'no') { $item['tv_rating'] = ''; } //$item['tv_subrating'] = get_post_meta($post->ID, 'libsyn-post-episode-tvrating-contains', true); $item['item_keywords'] = get_post_meta($post->ID, 'libsyn-post-episode-keywords', true); //player settings //post params are height(int),theme(standard,mini),width(int) $item['height'] = get_post_meta($post->ID, 'libsyn-post-episode-player_height', true); $item['width'] = get_post_meta($post->ID, 'libsyn-post-episode-player_width', true); $item['theme'] = get_post_meta($post->ID, 'libsyn-post-episode-player_use_theme', true); //handle primary content $url = get_post_meta($post->ID, 'libsyn-new-media-media', true); if (strpos($url, 'libsyn-ftp-') !== false) { $content_id = str_replace('http:', '', str_replace('https:', '', str_replace('/', '', str_replace('libsyn-ftp-', '', $url)))); } if (strpos($url, 'libsyn-upload-') !== false) { $content_id = str_replace('http:', '', str_replace('https:', '', str_replace('/', '', str_replace('libsyn-upload-', '', $url)))); } if (isset($content_id) && is_numeric($content_id)) { //then is ftp/unreleased $item['primary_content_id'] = intval($content_id); } elseif (!empty($url)) { //is regular $sanitize = new \Libsyn\Service\Sanitize(); $item['primary_content_url'] = $sanitize->url_raw($url); } else { //throw new Exception('Primary media error, please check your Libsyn settings.'); } //get destinations $destinations = $plugin->getDestinations($api); //TODO: commenting out all Exceptions put in place a logging system. //if(!$destinations) throw new Exception('Error using the Libsyn API, please try again later.'); //TODO: Handle validation error on bad api call for destinations. if ($isSchedule) { $releaseDate = $post->post_date_gmt; } else { $releaseDate = 'now'; } if ($isDraft) { $item['is_draft'] = 'true'; } else { $item['is_draft'] = 'false'; } $item['releases'] = array(); foreach ($destinations->destinations as $destination) { if ($destination->destination_type !== 'WordPress') { $item['releases'][] = array('destination_id' => $destination->destination_id, 'release_date' => $releaseDate); } } //is this post an update or new? $wp_libsyn_item_id = get_post_meta($post->ID, 'libsyn-item-id', true); $isUpdatePost = empty($wp_libsyn_item_id) ? false : true; if ($isUpdatePost) { $item['item_id'] = $wp_libsyn_item_id; } //run post $libsyn_post = $plugin->postPost($api, $item); if ($libsyn_post !== false) { self::updatePost($post, $libsyn_post, $isUpdatePost); } else { add_post_meta($post->ID, 'libsyn-post-error', 'true', true); } }
/** * Create new Libsyn-WP Api * * @param <array> $settings * * @return <Libsyn\Api> */ public function createLibsynApi(array $settings) { global $wpdb; /* * We'll set the default character set and collation for this table. * If we don't do this, some characters could end up being converted * to just ?'s when saved in our table. */ $charset_collate = ''; if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } if (!empty($wpdb->collate)) { $charset_collate .= " COLLATE {$wpdb->collate}"; } $sql = "CREATE TABLE {$this->api_table_name} (\r\n\t\t plugin_api_id mediumint(9) NOT NULL AUTO_INCREMENT,\r\n\t\t client_id varchar(64) NOT NULL,\r\n\t\t client_secret varchar(80) NOT NULL,\r\n\t\t access_token varchar(40) NOT NULL,\r\n\t\t refresh_token varchar(40) NOT NULL,\r\n\t\t is_active tinyint(3) DEFAULT 0 NOT NULL,\r\n\t\t refresh_token_expires DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL,\r\n\t\t access_token_expires datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,\r\n\t\t show_id int(8),\r\n\t\t feed_redirect_url varchar(510),\r\n\t\t itunes_subscription_url varchar(510),\r\n\t\t creation_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,\r\n\t\t last_updated timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\r\n\t\t UNIQUE KEY id (plugin_api_id)\r\n\t\t) {$charset_collate};"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql); //Sanitize Data $sanitize = new \Libsyn\Service\Sanitize(); //insert $settings $wpdb->insert($this->api_table_name, array('client_id' => $sanitize->clientId($settings['client_id']), 'client_secret' => $sanitize->clientSecret($settings['client_secret']), 'access_token' => $sanitize->accessToken($settings['access_token']), 'refresh_token' => $sanitize->refreshToken($settings['refresh_token']), 'is_active' => 1, 'refresh_token_expires' => $sanitize->date_format(date("Y-m-d H:i:s", strtotime("+60 days", strtotime(current_time('mysql'))))), 'access_token_expires' => $sanitize->date_format(date("Y-m-d H:i:s", strtotime("+1 hour", strtotime(current_time('mysql'))))), 'creation_date' => $sanitize->date_format(current_time('mysql')))); $lastId = $wpdb->insert_id; $data = $wpdb->get_results("SELECT * FROM {$this->api_table_name} WHERE plugin_api_id = {$lastId} AND is_active = 1"); return new \Libsyn\Api($data[0]); }