Asana account to use this plugin. After activating, go to the settings page to configure this plugin * Author: Ed Goode * Author URI: http://mackerelsky.co.nz/ */ //global variables global $api_key; global $asanaURL; //Add options and set default values add_option(asana_api_key, ''); add_option(asana_selected_workspace, ''); add_option(asana_workspace_id, ''); //Create options page add_action('admin_menu', 'add_asana_option_page'); // Hook in options page function add_asana_option_page() { add_options_page('Asana Task Widget Settings', 'Asana Tasks', 'manage_options', 'asana-tasks', 'asana_option_page'); } function asana_option_page() { $selected_workspace; $result; //update api key when saved if ($_POST['save']) { $api_key = $_POST[asana_api_key]; update_option(asana_api_key, $api_key); } //update workspace when saved if ($_POST['workspace']) { $selected_workspace = $_POST[asana_workspace]; update_option(asana_selected_workspace, $selected_workspace); } //display form ?>

Asana Task Display Options

Please enter your Asana API key

\n"; ?>

You can find the key by logging into Asana, clicking on your name in the bottom left hand corner, choosing Account Settings, and going to the API tab

"; echo "

Choose a workspace

"; $asana_url = "https://app.asana.com/api/1.0/workspaces"; $api_key = get_option(asana_api_key).":"; //note API key must have colon added to it for basic auth to work //encode key and set header arguments (note sslverify must be false) $args = array('headers' => array('Authorization' => 'Basic ' .base64_encode($api_key)), 'sslverify' => false); //call API $result = wp_remote_get($asana_url, $args); //then decode the results if(!is_null($result)){ $resultJson = json_decode($result['body']); $workspaces = $resultJson->data; } //Create drop-down echo ""; echo ""; echo ""; } if (get_option(asana_selected_workspace) !=""){ //echo "

Your selected workspace is ".get_option(asana_selected_workspace)."

"; if(!is_null($result)){ $resultJson = json_decode($result['body']); $workspace = $resultJson->data; foreach($workspace as $w){ if($w->name == get_option(asana_selected_workspace)){ //update workspace ID option $asana_workspace_ID = $w->id; update_option(asana_workspace_id, $asana_workspace_ID); } } } //echo "

Workspace ID is ".get_option(asana_workspace_id)."

"; } ?>
the Asana Tasks settings page to configure this widget"; } else { //set correct timezone so that we get the right date for today's tasks $timezone = get_option('timezone_string'); date_default_timezone_set($timezone); $today = date("Y-m-d"); $asana_due_today = "https://app.asana.com/api/1.0/tasks?opt_fields=name,completed,due_on,assignee_status&workspace=".get_option(asana_workspace_id)."&assignee=me"; $api_key = get_option(asana_api_key).":"; //note API key must have colon added to it for basic auth to work echo "

Tasks in the ".get_option(asana_selected_workspace)." workspace due today:

"; //encode key and set header arguments (note sslverify must be false) $args = array('headers' => array('Authorization' => 'Basic ' .base64_encode($api_key)), 'sslverify' => false); //call API $results = wp_remote_get($asana_due_today, $args); if(!is_null($results)){ //set up the form (note that it's calling PHP_SELF, which will mean that we process the output on the same page echo "
"; //get list of tasks due today $resultsJson = json_decode($results['body']); $tasks = $resultsJson->data; //display incomplete tasks due today foreach($tasks as $t){ if($t->completed != 1 && $t->due_on == $today) { $task_name = $t->name; $task_id = $t->id; echo "

".$task_name."

"; } } echo "

Tasks in the ".get_option(asana_selected_workspace)." workspace assigned for today:

"; //get list of tasks to be worked on today $resultsJson = json_decode($results['body']); $tasks = $resultsJson->data; //display incomplete tasks assigned for today foreach($tasks as $t){ if($t->completed != 1 && $t->assignee_status == 'today') { $task_name = $t->name; $task_id = $t->id; echo "

".$task_name."

"; } } //add submit button and close out the form echo ""; echo "
"; } } } //update tasks in asana when button clicked function update_asana_tasks($task_list){ $api_key = get_option(asana_api_key).":"; //note API key must have colon added to it for basic auth to work //for each task we have to update foreach($task_list as $task_to_update){ //set up the url $url = "https://app.asana.com/api/1.0/tasks/".$task_to_update; $completed = array("completed" => true); $body = array("data" => $completed); $body = json_encode($body); //note that content type has been set to application/json. That's the only way to get data out of this sucker $args = array('method' => 'PUT', 'body' => $body, 'headers' => array('Content-Type'=> 'application/json', 'Authorization' => 'Basic ' .base64_encode($api_key)),'sslverify' => false); //call it $response = wp_remote_retrieve_response_code(wp_remote_request( $url, $args)); //print_r($response); if($response == 200){ echo "Task successfully updated."; } else { echo "There was a problem communicating with Asana. The result code was ".$response; } } } //register widget function register_asana_task_widget(){ wp_add_dashboard_widget( 'asana-tasks', 'Asana Tasks', 'asana_task_widget'); } add_action('wp_dashboard_setup', 'register_asana_task_widget'); ?>