/** * Handle the custom Bulk Action * * Uses switch/case to either delete from CartoDB, restore to CartoDB, or update postmeta with CartoDB data. Does not effect any WordPress core data. Based on the post http://wordpress.stackexchange.com/questions/29822/custom-bulk-action * * @since 0.1.0 */ function custom_bulk_action() { global $typenow; $pagenow; $post_type = $typenow; if ($post_type == 'post' || $post_type == 'page') { // get the action $wp_list_table = _get_list_table('WP_Posts_List_Table'); // depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc $action = $wp_list_table->current_action(); $allowed_actions = array("cartopress_delete", "cartopress_restore", "cartopress_update"); if (!in_array($action, $allowed_actions)) { return; } // security check check_admin_referer('bulk-posts'); // 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']); } if (empty($post_ids)) { return; } // this is based on wp-admin/edit.php $sendback = remove_query_arg(array('cartopress_deleted', 'cartopress_restored', 'cartopress_updated', 'untrashed', 'deleted', 'ids'), wp_get_referer()); if (!$sendback) { $sendback = admin_url("edit.php?post_type={$post_type}"); } $pagenum = $wp_list_table->get_pagenum(); $sendback = add_query_arg('paged', $pagenum, $sendback); switch ($action) { case 'cartopress_delete': $sql_distinct = 'SELECT DISTINCT cp_post_id FROM ' . CARTOPRESS_TABLE; $cartopress_ids = cartopress_sync::update_cartodb($sql_distinct, CARTOPRESS_APIKEY, CARTOPRESS_USERNAME, true); $cartopress_ids = $cartopress_ids->rows; $temp = array(); foreach ($cartopress_ids as $key => $value) { array_push($temp, $value->cp_post_id); } $deleted = 0; foreach ($post_ids as $post_id) { if (in_array($post_id, $temp)) { cartopress_sync::cartodb_delete($post_id); $deleted++; } else { return false; } } $sendback = add_query_arg(array('cartopress_deleted' => $deleted, 'ids' => join(',', $post_ids)), $sendback); break; case 'cartopress_restore': $restored = 0; foreach ($post_ids as $post_id) { cartopress_sync::cartodb_sync($post_id); $restored++; } $sendback = add_query_arg(array('cartopress_restored' => $restored, 'ids' => join(',', $post_ids)), $sendback); break; case 'cartopress_update': $updated = 0; foreach ($post_ids as $post_id) { cartopress_sync::cartopress_update_postmeta($post_id); $updated++; } $sendback = add_query_arg(array('cartopress_updated' => $updated, 'ids' => join(',', $post_ids)), $sendback); break; default: return; } $sendback = remove_query_arg(array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback); wp_redirect($sendback); exit; } //end }
/** * Performs add, update and delete functions for selected CartoDB row * * @since 0.1.0 * @param int $post_id The global post id. */ function cartopress_update_row($post_id) { if (get_post_meta($post_id, '_cp_post_donotsync', true) == 1) { return; } else { if (get_post_status($post_id) != 'publish') { cartopress_sync::cartodb_delete($post_id); } else { // Special case: Post Format is not updated until after 'save_post' is called when using the Bulk Edit tool. The below sets the post format during the save_post process if the $_POST data is available. if (isset($_REQUEST['post_format']) && $_REQUEST['post_format'] != -1) { set_post_format($post_id, $_REQUEST['post_format']); } cartopress_sync::cartodb_sync($post_id); } // end if } // end if }