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(); }
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 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; } } } }