/**
  * 
  * Query a single url.  If there is not an API Key in the query string, append one, but otherwise just do a straight query
  * 
  * @param string $url -- the full url to query.
  */
 function query_by_url($url)
 {
     //check to see if the API key is included, if not, add the one from the options
     if (!stristr($url, 'apiKey=')) {
         $url .= '&apiKey=' . get_option('ds_npr_api_key');
     }
     $this->request->request_url = $url;
     //fill out the $this->request->param array so we can know what params were sent
     $parsed_url = parse_url($url);
     if (!empty($parsed_url['query'])) {
         $parms = split('&', $parsed_url['query']);
         if (!empty($params)) {
             foreach ($params as $p) {
                 $attrs = split('=', $p);
                 $this->request->param[$attrs[0]] = $attrs[1];
             }
         }
     }
     $response = wp_remote_get($url);
     if (!is_wp_error($response)) {
         $this->response = $response;
         if ($response['response']['code'] == self::NPRAPI_STATUS_OK) {
             if ($response['body']) {
                 $this->xml = $response['body'];
             } else {
                 $this->notice[] = t('No data available.');
             }
         } else {
             ds_npr_show_message('An error occurred pulling your story from the NPR API.  The API responded with message =' . $response['response']['message'], TRUE);
         }
     } else {
         $error_text = '';
         if (!empty($response->errors['http_request_failed'][0])) {
             $error_text = '<br> HTTP Error response =  ' . $response->errors['http_request_failed'][0];
         }
         ds_npr_show_message('Error pulling story for url=' . $url . $error_text, TRUE);
         error_log('Error retrieving story for url=' . $url);
     }
 }
Esempio n. 2
0
 function load_page_hook()
 {
     if (isset($_POST) && isset($_POST['story_id'])) {
         $story_id = $_POST['story_id'];
         if (isset($_POST['publishNow'])) {
             $publish = true;
         }
         if (isset($_POST['createDaft'])) {
             $publish = false;
         }
     } else {
         if (isset($_GET['story_id']) && isset($_GET['create_draft'])) {
             $story_id = $_GET['story_id'];
         }
     }
     if (isset($story_id)) {
         // XXX: check that the API key is actually set
         $api = new NPRAPIWordpress();
         //check to see if we got an ID or a URL
         if (is_numeric($story_id)) {
             if (strlen($story_id) >= 8) {
                 $story_id = $story_id;
             }
         } else {
             // url format: /yyyy/mm/dd/id
             // url format: /blogs/name/yyyy/mm/dd/id
             $story_id = preg_replace('/http\\:\\/\\/[^\\s\\/]*npr\\.org\\/((([^\\/]*\\/){3,5})([0-9]{8,12}))\\/.*/', '$4', $story_id);
             if (!is_numeric($story_id)) {
                 // url format: /templates/story/story.php?storyId=id
                 $story_id = preg_replace('/http\\:\\/\\/[^\\s\\/]*npr\\.org\\/([^&\\s\\<]*storyId\\=([0-9]+)).*/', '$2', $story_id);
             }
         }
         $params = array('id' => $story_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') {
             $post_id = $api->update_posts_from_stories($publish);
             if (!empty($post_id)) {
                 //redirect to the edit page if we just updated one story
                 $post_link = admin_url('post.php?action=edit&post=' . $post_id);
                 wp_redirect($post_link);
             }
         } else {
             if (empty($story)) {
                 $xml = simplexml_load_string($api->xml);
                 ds_npr_show_message('Error retrieving story for id = ' . $story_id . '<br> API error =' . $api->message->id . '<br> API Message =' . $xml->message->text, TRUE);
                 error_log('Not going to save the return from query for story_id=' . $story_id . ', we got an error=' . $api->message->id . ' from the API');
                 return;
             }
         }
     }
 }
function ds_npr_get_stories()
{
    global $is_IE;
    $api_key = get_option('ds_npr_api_key');
    $pull_url = get_option('ds_npr_api_pull_url');
    ?>
        <div class="wrap">
            <?php 
    screen_icon();
    ?>
            <h2>Get NPR Stories</h2>
            <?php 
    if (!$api_key) {
        ds_npr_show_message('You do not currently have an API Key set.  <a href="' . admin_url('options-general.php?page=ds_npr_api') . '">Set your API Key here.</a>', TRUE);
        ?>
            <?php 
    }
    if (!$pull_url) {
        ds_npr_show_message('You do not currently have an API Pull URL set.  <a href="' . admin_url('options-general.php?page=ds_npr_api') . '">Set your API Pull URL here.</a>', TRUE);
        ?>
            <?php 
    }
    $story_id = '';
    if ((isset($_POST) and isset($_POST['story_id'])) || isset($_GET) && isset($_GET['story_id'])) {
        ?>
                <div class="updated">
                <?php 
        if (!empty($_POST['story_id'])) {
            $story_id = $_POST['story_id'];
        }
        if (!empty($_GET['story_id'])) {
            $story_id = $_GET['story_id'];
        }
        ?>
                </div>
            <?php 
    }
    ?>

            <div style="float: left;">
                <form action="" method="POST">
                    Enter an NPR Story ID or URL: <input type="text" name="story_id" value="<?php 
    echo $story_id;
    ?>
" />
                    <input type="submit" name='createDaft' value="Create Draft" />
                    <input type="submit" name='publishNow' value="Publish Now" />
                </form>
            </div>

       </div>
        <?php 
}