Ejemplo n.º 1
0
/**
 *
 * Inform the NPR API that a post needs to be deleted.
 * @param unknown_type $post_ID
 */
function npr_delete($post_ID)
{
    $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 {
            //error_log('Not pushing the story because it came from the API');
            $api->send_delete($api_id);
        }
    }
}
Ejemplo n.º 2
0
function ds_npr_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);
                $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') {
                    error_log('updating story for API ID=' . $api_id);
                    $story = $api->update_posts_from_stories();
                }
            }
            // 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();
}
Ejemplo n.º 3
0
/**
 * 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);
        }
    }
}
Ejemplo n.º 4
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;
             }
         }
     }
 }
Ejemplo n.º 5
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;
             }
         }
     }
 }