function test_nprstory_show_message()
 {
     $test_message = 'Test message';
     ob_start();
     nprstory_show_message($test_message, false);
     $result = ob_get_clean();
     $this->assertTrue((bool) strstr($result, $test_message));
     ob_start();
     nprstory_show_message($test_message, true);
     $result = ob_get_clean();
     $this->assertTrue((bool) strstr($result, 'class="error"'));
 }
 /**
  * 
  * 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'])) {
         $params = explode('&', $parsed_url['query']);
         if (!empty($params)) {
             foreach ($params as $p) {
                 $attrs = explode('=', $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[] = __('No data available.');
             }
         } else {
             nprstory_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];
         }
         nprstory_show_message('Error pulling story for url=' . $url . $error_text, TRUE);
         nprstory_error_log('Error retrieving story for url=' . $url);
     }
 }
Example #3
0
 function load_page_hook()
 {
     // find the input that is allegedly a story id
     // We validate these later
     if (isset($_POST) && isset($_POST['story_id'])) {
         $story_id = $_POST['story_id'];
         if (isset($_POST['publishNow'])) {
             $publish = true;
         }
         if (isset($_POST['createDaft'])) {
             $publish = false;
         }
         if (!check_admin_referer('nprstory_nonce_story_id', 'nprstory_nonce_story_id_field')) {
             wp_die(__('Nonce did not verify in DS_NPR_API::load_page_hook. Are you sure you should be doing this?'), __('NPR Story API Error'), 403);
         }
     } else {
         if (isset($_GET['story_id']) && isset($_GET['create_draft'])) {
             $story_id = $_GET['story_id'];
         }
     }
     // if the current user shouldn't be doing this, fail
     if (!current_user_can('edit_posts')) {
         wp_die(__('You do not have permission to edit posts, and therefore you do not have permission to pull posts from the NPR API'), __('NPR Story API Error'), 403);
     }
     // try to get the ID of the story from the URL
     if (isset($story_id)) {
         //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);
             }
         }
     }
     // Don't do anything if $story_id isn't an ID
     if (is_numeric($story_id)) {
         // start the API class
         // todo: check that the API key is actually set
         $api = new NPRAPIWordpress();
         $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);
                 nprstory_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 NPR Story API');
                 // debug use
                 return;
             }
         }
     }
 }
function nprstory_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) {
        nprstory_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) {
        nprstory_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 
    }
    // Get the story ID from the URL, then paste it into the input's value field with esc_attr
    $story_id = '';
    if ((isset($_POST) and isset($_POST['story_id'])) || isset($_GET) && isset($_GET['story_id'])) {
        if (!empty($_POST['story_id'])) {
            $story_id = $_POST['story_id'];
        }
        if (!empty($_GET['story_id'])) {
            $story_id = $_GET['story_id'];
        }
    }
    ?>

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

       </div>
        <?php 
}