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; //Add options and set default values add_option(asana_api_key, ''); add_option(asana_selected_workspace, ''); add_option(asana_workspace_id, ''); add_option(asana_show_due_today, ''); add_option(asana_show_due_this_week, ''); add_option(asana_show_todays_tasks, ''); add_option(asana_show_overdue, ''); //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); } //update display options if ($_POST['display']) { if (isset($_POST[asana_show_due_today])) { update_option(asana_show_due_today, true); } else{ update_option(asana_show_due_today, false); } if (isset($_POST[asana_show_due_this_week])) { update_option(asana_show_due_this_week, true); } else{ update_option(asana_show_due_this_week, false); } if (isset($_POST[asana_show_overdue])) { update_option(asana_show_overdue, true); } else{ update_option(asana_show_overdue, false); } if (isset($_POST[asana_show_todays_tasks])) { update_option(asana_show_todays_tasks, true); } else{ update_option(asana_show_todays_tasks, false); } } //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

"; $workspace_url = "workspaces"; $workspaces=get_asana_info($workspace_url); //Create drop-down echo ""; echo ""; echo ""; } if (get_option(asana_selected_workspace) !=""){ $workspace = get_asana_info($workspace_url); 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); } } //display options for what's shown in the widget ?>

Please select what should be displayed in the widget

name='asana_show_due_today'id='asana_show_due_today'value='1' />Show tasks due today

name='asana_show_todays_tasks'id='asana_show_todays_tasks'value='1' />Show tasks tagged to be worked on today

name='asana_show_due_this_week'id='asana_show_due_this_week'value='1' />Show tasks due this week

name='asana_show_overdue'id='asana_show_overdue'value='1' />Show overdue tasks

Save display options

"; echo "
"; } ?>
true); $body = array("data" => $completed); $url = "tasks/".$task; //call the API $response = put_asana_info($url, "PUT", $body); $response_r = $response["response"]; $code = $response_r["code"]; if($code == 200){ echo "Task successfully created."; } else { echo "There was a problem communicating with Asana. The error returned was ".$response_r["message"]; } } } if(get_option(asana_api_key) == "" || get_option(asana_selected_workspace) == ""){ //if either no api key or no workspace, display a message echo "Please go to the Asana Tasks settings page to configure this widget"; } else { //set correct timezone so that we get the right date for tasks $timezone = get_option('timezone_string'); date_default_timezone_set($timezone); $today = date("Y-m-d"); $endofweek = strtotime( "next Sunday" ); $weekend = date("Y-m-d",$endofweek); //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 the tasks $task_url_ending = "tasks?opt_fields=name,completed,due_on,assignee_status&workspace=".get_option(asana_workspace_id)."&assignee=me"; $tasks = get_asana_info($task_url_ending); //check which options are set and call function accordingly if (get_option(asana_show_due_today) == 1){ echo "

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

"; $todaycount = 0; //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."

"; $todaycount = $todaycount+1; } } //if there are no tasks in the category, say so if($todaycount == 0){ echo "

There are no tasks due today

"; } } if (get_option(asana_show_due_this_week) == 1){ echo "

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

"; $thisweekcount = 0; //display incomplete tasks due for the rest of the week foreach($tasks as $t){ if($t->completed != 1 && $t->due_on >= $today && $t->due_on <= $weekend) { $task_name = $t->name; $task_id = $t->id; echo "

".$task_name."

"; $thisweekcount = $thisweekcount+1; } } if($thisweekcount == 0){ echo "

There are no tasks due this week

"; } } if (get_option(asana_show_todays_tasks) == 1){ echo "

Tasks in the ".get_option(asana_selected_workspace)." workspace tagged to be worked on today:

"; $taggedcount = 0; //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."

"; $taggedcount = $taggedcount + 1; } } if($taggedcount == 0){ echo "

There are no tasks assigned for today

"; } } if (get_option(asana_show_overdue) == 1){ echo "

Overdue tasks in the ".get_option(asana_selected_workspace)." workspace:

"; $overduecount = 0; //display incomplete tasks due today foreach($tasks as $t){ if($t->completed != 1 && $t->due_on != null && $t->due_on < $today) { $task_name = $t->name; $task_id = $t->id; echo "

".$task_name."

"; $overduecount = $overduecount + 1; } } if($overduecount == 0){ echo "

There are no overdue tasks

"; } } //add submit button and close out the form echo ""; echo "
"; } } function asana_create_task_widget(){ //call task creation when form is submitted if ($_POST['asana_create_task']) { //get variables and set up data $name = $_POST[asana_new_task_name]; $notes = $_POST[asana_new_task_notes]; $project = $_POST[asana_project]; $url = "tasks"; $method = "POST"; $bodydata = array("name" => $name, "notes" => $notes, "workspace" => get_option(asana_workspace_id), "assignee" => "me"); $body = array("data" => $bodydata); //call task creation $response = put_asana_info($url, $method, $body); $response_r = $response["response"]; $code = $response_r["code"]; if($code == 201){ echo "Task successfully created."; } else { echo "There was a problem communicating with Asana. The error returned was ".$response_r["message"]; } //call the api again to associate it with the project $response_body = json_decode($response['body']); $data = $response_body->data; $id = $data->id; $url = "tasks/".$id."/addProject"; $bodydata = array("project" => $project); $body = array("data" => $bodydata); $response = put_asana_info($url, "POST", $body); $response_r = $response["response"]; $code = $response_r["code"]; if($code == 200){ echo " Task added to project."; } else { echo " There was a problem communicating with Asana. The error returned was ".$response_r["message"]; } } //can't create projects until the plugin has been configured if(get_option(asana_api_key) == "" || get_option(asana_selected_workspace) == ""){ //if either no api key or no workspace, display a message echo "Please go to the Asana Tasks settings page to configure this widget"; } else { //create form $projects_url = "workspaces/".get_option(asana_workspace_id)."/projects"; $projects=get_asana_info($projects_url); echo "
";?>
Task name
Notes
Project "; echo ""; foreach ($projects as $p) { echo ""; } ?>
array('Authorization' => 'Basic ' .base64_encode($api_key)), 'sslverify' => false); //call API $results = wp_remote_get($asana_due_today, $args); if(!is_null($results)){ //get results $resultsJson = json_decode($results['body']); $data = $resultsJson->data; } return $data; } function put_asana_info($url_ending, $method, $data){ $api_key = get_option(asana_api_key).":"; //note API key must have colon added to it for basic auth to work $url = "https://app.asana.com/api/1.0/".$url_ending; $body = json_encode($data); //method is POST for new tasks/projects, PUT for updating existing stuff //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' => $method, 'body' => $body, 'headers' => array('Content-Type'=> 'application/json', 'Authorization' => 'Basic ' .base64_encode($api_key)),'sslverify' => false); //call it $response = wp_remote_request( $url, $args); return $response; } //register widgets function register_asana_task_widget(){ wp_add_dashboard_widget( 'asana-tasks', 'Asana Tasks', 'asana_task_widget'); wp_add_dashboard_widget( 'create-asana-tasks', 'Create Asana Tasks', 'asana_create_task_widget'); } add_action('wp_dashboard_setup', 'register_asana_task_widget'); ?>