public static function nprstory_cron_pull() { // here we should get the list of IDs/full urls that need to be checked hourly //because this is run on cron, and may be fired off by an non-admin, we need to load a bunch of stuff require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; // This is debug code. It may be save future devs some time; please keep it around. /* $now = gmDate("D, d M Y G:i:s O "); error_log("right now the time is -- ".$now); // debug use */ // here we go. $num = get_option('ds_npr_num'); for ($i = 0; $i < $num; $i++) { $api = new NPRAPIWordpress(); $q = 'ds_npr_query_' . $i; $query_string = get_option($q); if (!empty($query_string)) { nprstory_error_log('Cron ' . $i . ' querying NPR API for ' . $query_string); //if the query string contains the pull url and 'query', just make request from the API if (stristr($query_string, get_option('ds_npr_api_pull_url')) && stristr($query_string, 'query')) { $api->query_by_url($query_string); } else { //if the string doesn't contain the base url, try to query using an ID if (stristr($query_string, 'http:')) { error_log('Not going to run query because the query string contains http and is not pointing to the pullURL: ' . $query_string); // debug use } else { $params = array('id' => $query_string, 'apiKey' => get_option('ds_npr_api_key')); $api->request($params, 'query', get_option('ds_npr_api_pull_url')); } } $api->parse(); try { if (empty($api->message) || $api->message->level != 'warning') { //check the publish flag and send that along. $pub_flag = FALSE; $pub_option = get_option('ds_npr_query_publish_' . $i); if ($pub_option == 'Publish') { $pub_flag = TRUE; } $story = $api->update_posts_from_stories($pub_flag, $i); } else { if (empty($story)) { error_log('NPR Story API: not going to save story. Query ' . $query_string . ' returned an error ' . $api->message->id . ' error'); // debug use } } } catch (Exception $e) { error_log('NPR Story API: error in ' . __FUNCTION__ . ' like this :' . $e); // debug use } } } }
function nprstory_activate() { update_option('dp_npr_query_multi_cron_interval', 60); if (!wp_next_scheduled('npr_ds_hourly_cron')) { nprstory_error_log('turning on cron event for NPR Story API plugin'); wp_schedule_event(time(), 'hourly', 'npr_ds_hourly_cron'); } $num = get_option('ds_npr_num'); if (empty($num)) { update_option('ds_npr_num', 5); } $def_url = 'http://api.npr.org'; $pull_url = get_option('ds_npr_api_pull_url'); if (empty($pull_url)) { update_option('ds_npr_api_pull_url', $def_url); } }
function nprstory_bulk_action_update_action() { // 1. get the action $wp_list_table = _get_list_table('WP_Posts_List_Table'); $action = $wp_list_table->current_action(); switch ($action) { // 3. Perform the action case 'updateNprStory': // make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids' if (isset($_REQUEST['post'])) { $post_ids = array_map('intval', $_REQUEST['post']); } $exported = 0; foreach ($post_ids as $post_id) { $api_id = get_post_meta($post_id, NPR_STORY_ID_META_KEY, TRUE); // don't run API queries for posts that have no ID // @todo: why do some posts have no ID // @todo: oh, it's only imported drafts that don't have an ID if (!empty($api_id)) { $api = new NPRAPIWordpress(); $params = array('id' => $api_id, 'apiKey' => get_option('ds_npr_api_key')); $api->request($params, 'query', get_option('ds_npr_api_pull_url')); $api->parse(); if (empty($api->message) || $api->message->level != 'warning') { nprstory_error_log('updating story for API ID=' . $api_id); $story = $api->update_posts_from_stories(); } } else { } } // build the redirect url //$sendback = add_query_arg( array('exported' => $exported, 'ids' => join(',', $post_ids) ), $sendback ); break; default: return; } // ... // 4. Redirect client //wp_redirect($sendback); //exit(); }
function nprstory_api_get_multi_settings_callback() { $run_multi = get_option('dp_npr_query_run_multi'); if ($run_multi) { DS_NPR_API::nprstory_cron_pull(); } //change the cron timer if (wp_next_scheduled('npr_ds_hourly_cron')) { wp_clear_scheduled_hook('npr_ds_hourly_cron'); } nprstory_error_log('NPR Story API plugin: updating the npr_ds_hourly_cron event timer'); wp_schedule_event(time(), 'ds_interval', 'npr_ds_hourly_cron'); }
/** * This function will send the push request to the NPR API to add/update a story. * * @see NPRAPI::send_request() * * @param string $nprml * @param int $post_ID */ function send_request($nprml, $post_ID) { $error_text = ''; $org_id = get_option('ds_npr_api_org_id'); if (!empty($org_id)) { $url = add_query_arg(array('orgId' => $org_id, 'apiKey' => get_option('ds_npr_api_key')), get_option('ds_npr_api_push_url') . '/story'); nprstory_error_log('Sending nprml = ' . $nprml); $result = wp_remote_post($url, array('body' => $nprml)); if (!is_wp_error($result)) { if ($result['response']['code'] == 200) { $body = wp_remote_retrieve_body($result); if ($body) { $response_xml = simplexml_load_string($body); $npr_story_id = (string) $response_xml->list->story['id']; update_post_meta($post_ID, NPR_STORY_ID_META_KEY, $npr_story_id); } else { error_log('Error returned from NPR Story API with status code 200 OK but failed wp_remote_retrieve_body: ' . print_r($result, true)); // debug use } } else { $error_text = ''; if (!empty($result['response']['message'])) { $error_text = 'Error pushing story with post_id = ' . $post_ID . ' for url=' . $url . ' HTTP Error response = ' . $result['response']['message']; } $body = wp_remote_retrieve_body($result); if ($body) { $response_xml = simplexml_load_string($body); $error_text .= ' API Error Message = ' . $response_xml->message->text; } error_log('Error returned from NPR Story API with status code other than 200 OK: ' . $error_text); // debug use } } else { $error_text = 'WP_Error returned when sending story with post_ID ' . $post_ID . ' for url ' . $url . ' to NPR Story API:' . $result->get_error_message(); error_log($error_text); // debug use } } else { $error_text = 'OrgID was not set when tried to push post_ID ' . $post_ID . ' to the NPR Story API.'; error_log($error_text); // debug use } // Add errors to the post that you just tried to push if (!empty($error_text)) { update_post_meta($post_ID, NPR_PUSH_STORY_ERROR, $error_text); } else { delete_post_meta($post_ID, NPR_PUSH_STORY_ERROR); } }
/** * Inform the NPR API that a post needs to be deleted. * * Limited to users that can delete other users' posts * * @param unknown_type $post_ID */ function nprstory_api_delete($post_ID) { if (!current_user_can('delete_others_posts')) { wp_die(__('You do not have permission to delete posts in the NPR API. Users that can delete other users\' posts have that ability: administrators and editors.'), __('NPR Story API Error'), 403); } $push_post_type = get_option('ds_npr_push_post_type'); if (empty($push_post_type)) { $push_post_type = 'post'; } $api_id_meta = get_post_meta($post_ID, NPR_STORY_ID_META_KEY); $api_id = $api_id_meta[0]; $post = get_post($post_ID); //if the push url isn't set, don't even try to delete. $push_url = get_option('ds_npr_api_push_url'); if ($post->post_type == $push_post_type && !empty($push_url) && !empty($api_id)) { // For now, only submit regular posts, and only on publish. if ($post->post_type != 'post' || $post->post_status != 'publish') { return; } $api = new NPRAPIWordpress(); $retrieved = get_post_meta($post_ID, NPR_RETRIEVED_STORY_META_KEY, true); if (empty($retrieved) || $retrieved == 0) { $api->send_request($api->create_NPRML($post), $post_ID); } else { nprstory_error_log('Pushing delete action to the NPR Story API for the story with post_ID ' . $post_ID); $api->send_delete($api_id); } } }
/** * * Do the mapping from WP post to the array that we're going to build the NPRML from. * This is also where we will do custom mapping if need be. * If a mapped custom field does not exist in a certain post, just send the default field. * @param $post */ function nprstory_post_to_nprml_story($post) { $story = array(); $story[] = array('tag' => 'link', 'attr' => array('type' => 'html'), 'text' => get_permalink($post)); $use_custom = get_option('dp_npr_push_use_custom_map'); //get the list of metas available for this post $post_metas = get_post_custom_keys($post->ID); $teaser_text = ''; if (!empty($post->post_excerpt)) { $teaser_text = $post->post_excerpt; } $custom_content_meta = get_option('ds_npr_api_mapping_body'); if ($use_custom && !empty($custom_content_meta) && $custom_content_meta != '#NONE#' && in_array($custom_content_meta, $post_metas)) { $content = get_post_meta($post->ID, $custom_content_meta, true); $post_for_teaser = $post; $post_for_teaser->post_content = $content; if (empty($teaser_text)) { $teaser_text = nprstory_nai_get_excerpt($post_for_teaser); } } else { $content = $post->post_content; if (empty($teaser_text)) { $teaser_text = nprstory_nai_get_excerpt($post); } } //lets see if there are any plugins that need to fix their shortcodes before we run do_shortcode if (has_filter('npr_ds_shortcode_filter')) { $content = apply_filters('npr_ds_shortcode_filter', $content); } //let any plugin that has short codes try and replace those with HTML $content = do_shortcode($content); //for any remaining short codes, nuke 'em $content = strip_shortcodes($content); $content = apply_filters('the_content', $content); $story[] = array('tag' => 'teaser', 'text' => $teaser_text); $custom_title_meta = get_option('ds_npr_api_mapping_title'); if ($use_custom && !empty($custom_title_meta) && $custom_title_meta != '#NONE#' && in_array($custom_content_meta, $post_metas)) { $custom_title = get_post_meta($post->ID, $custom_title_meta, true); $story[] = array('tag' => 'title', 'text' => $custom_title); } else { $story[] = array('tag' => 'title', 'text' => $post->post_title); } /** * *If there is a custom byline configured, send that. *If the site is using the coauthurs plugin, and get_coauthors exists, send the display names * *If no cool things are going on, just send the display name for the post_author field. * */ $byline = FALSE; $custom_byline_meta = get_option('ds_npr_api_mapping_byline'); if ($use_custom && !empty($custom_byline_meta) && $custom_byline_meta != '#NONE#' && in_array($custom_content_meta, $post_metas)) { $byline = TRUE; $story[] = array('tag' => 'byline', 'children' => array(array('tag' => 'name', 'text' => get_post_meta($post->ID, $custom_byline_meta, true)))); } if (function_exists('get_coauthors')) { $coauthors = get_coauthors($post->ID); if (!empty($coauthors)) { $byline = TRUE; foreach ($coauthors as $i => $co) { $story[] = array('tag' => 'byline', 'children' => array(array('tag' => 'name', 'text' => $co->display_name))); } } else { nprstory_error_log('we do not have co authors'); } } else { nprstory_error_log('can not find get_coauthors'); } if (!$byline) { $story[] = array('tag' => 'byline', 'children' => array(array('tag' => 'name', 'text' => get_the_author_meta('display_name', $post->post_author)))); } // NPR One // If the box is checked, the value here is '1' if (!empty($_POST['send_to_nprone'])) { $story[] = array('tag' => 'parent', 'attr' => array('id' => '319418027', 'type' => 'collection')); } #'miniTeaser' => array( 'text' => '' ), #'slug' => array( 'text' => '' ), $story[] = array('tag' => 'storyDate', 'text' => mysql2date('D, d M Y H:i:s +0000', $post->post_date_gmt)); $story[] = array('tag' => 'pubDate', 'text' => mysql2date('D, d M Y H:i:s +0000', $post->post_modified_gmt)); $story[] = array('tag' => 'lastModifiedDate', 'text' => mysql2date('D, d M Y H:i:s +0000', $post->post_modified_gmt)); $story[] = array('tag' => 'partnerId', 'text' => $post->guid); //TODO: When the API accepts sending both text and textWithHTML, send a totally bare text. Don't do do_shortcode(). //for now (using the npr story api) we can either send text or textWithHTML, not both. //it would be nice to send text after we strip all html and shortcodes, but we need the html //and sending both will duplicate the data in the API $story[] = array('tag' => 'textWithHtml', 'children' => nprstory_nprml_split_paragraphs($content)); $perms_group = get_option('ds_npr_story_default_permission'); if (!empty($perms_group)) { $story[] = array('tag' => 'permissions', 'children' => array(array('tag' => 'permGroup', 'attr' => array('id' => $perms_group)))); } $custom_media_credit = get_option('ds_npr_api_mapping_media_credit'); $custom_media_agency = get_option('ds_npr_api_mapping_media_agency'); /* remove this for now until we decide if we're going to actually do this...km $dist_media_option = get_option('ds_npr_api_mapping_distribute_media'); $dist_media_polarity = get_option('ds_npr_api_mapping_distribute_media_polarity'); */ $args = array('order' => 'DESC', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'post_status' => null, 'post_type' => 'attachment'); $images = get_children($args); $primary_image = get_post_thumbnail_id($post->ID); foreach ($images as $image) { $custom_credit = ''; $custom_agency = ''; $image_metas = get_post_custom_keys($image->ID); if ($use_custom && !empty($custom_media_credit) && $custom_media_credit != '#NONE#' && in_array($custom_media_credit, $image_metas)) { $custom_credit = get_post_meta($image->ID, $custom_media_credit, true); } if ($use_custom && !empty($custom_media_agency) && $custom_media_agency != '#NONE#' && in_array($custom_media_agency, $image_metas)) { $custom_agency = get_post_meta($image->ID, $custom_media_agency, true); } if ($use_custom && !empty($dist_media_option) && $dist_media_option != '#NONE#' && in_array($dist_media_option, $image_metas)) { $dist_media = get_post_meta($image->ID, $dist_media_option, true); } //if the image field for distribute is set and polarity then send it. //all kinds of other math when polarity is negative or the field isn't set. $image_type = 'standard'; if ($image->ID == $primary_image) { $image_type = 'primary'; } //is the image in the content? If so, tell the APi with a flag that CorePublisher knows //WP may add something like "-150X150" to the end of the filename, before the extension. Isn't that nice? $image_name_parts = split(".", $image_guid); $image_regex = "/" . $image_name_parts[0] . "\\-[a-zA-Z0-9]*" . $image_name_parts[1] . "/"; $in_body = ""; if (preg_match($image_regex, $content)) { if (strstr($image->guid, '?')) { $in_body = "&origin=body"; } else { $in_body = "?origin=body"; } } $story[] = array('tag' => 'image', 'attr' => array('src' => $image->guid . $in_body, 'type' => $image_type), 'children' => array(array('tag' => 'title', 'text' => $image->post_title), array('tag' => 'caption', 'text' => $image->post_excerpt), array('tag' => 'producer', 'text' => $custom_credit), array('tag' => 'provider', 'text' => $custom_agency))); } //should be able to do the same as image for audio, with post_mime_typ = 'audio' or something. $args = array('order' => 'DESC', 'post_mime_type' => 'audio', 'post_parent' => $post->ID, 'post_status' => null, 'post_type' => 'attachment'); $audios = get_children($args); foreach ($audios as $audio) { $audio_meta = wp_get_attachment_metadata($audio->ID); $caption = $audio->post_excerpt; //if we don't have excerpt filled in, try content if (empty($caption)) { $caption = $audio->post_content; } $story[] = array('tag' => 'audio', 'children' => array(array('tag' => 'format', 'children' => array(array('tag' => 'mp3', 'text' => $audio->guid))), array('tag' => 'description', 'text' => $caption), array('tag' => 'duration', 'text' => $audio_meta['length']))); } if ($enclosures = get_metadata('post', $post->ID, 'enclosure')) { // This logic is specifically driven by enclosure metadata items that are // created by the PowerPress podcasting plug-in. It will likely have to be // re-worked if we need to accomodate other plug-ins that use enclosures. foreach ($enclosures as $enclosure) { $pieces = explode("\n", $enclosure); if (!empty($pieces[3])) { $metadata = unserialize($pieces[3]); $duration = !empty($metadata['duration']) ? nprstory_convert_duration_to_seconds($metadata['duration']) : NULL; } $story[] = array('tag' => 'audio', 'children' => array(array('tag' => 'duration', 'text' => !empty($duration) ? $duration : 0), array('tag' => 'format', 'children' => array(array('tag' => 'mp3', 'text' => $pieces[0]))))); } } return $story; }