Asana account to use this plugin. After activating, go to your profile page to configure this plugin
* Author: Ed Goode
* Author URI: http://mackerelsky.co.nz/
*/
//global variables
global $api_key;
add_option('show_asana_upgrade_notice', '');
//Add extra fields to user profile and update db when profile options are saved
add_action( 'show_user_profile', 'asana_profile_info' );
add_action( 'edit_user_profile', 'asana_profile_info' );
add_action( 'personal_options_update', 'save_asana_profile_info' );
add_action( 'edit_user_profile_update', 'save_asana_profile_info' );
//load jquery ui theme (http://snippets.webaware.com.au/snippets/load-a-nice-jquery-ui-theme-in-wordpress/)
add_action('init', 'load_jquery_ui');
function load_jquery_ui() {
global $wp_scripts;
// tell WordPress to load jQuery UI datepicker
wp_enqueue_script('jquery-ui-datepicker');
// get registered script object for jquery-ui
$ui = $wp_scripts->query('jquery-ui-core');
// tell WordPress to load the Smoothness theme from Google CDN
$url = "https://ajax.googleapis.com/ajax/libs/jqueryui/{$ui->ver}/themes/smoothness/jquery.ui.all.css";
wp_enqueue_style('jquery-ui-smoothness', $url, false, $ui->ver);
}
function asana_profile_info( $user ) {
global $wpdb;
?>
ID);
$thisweek = get_the_author_meta('asana_show_due_this_week', $user->ID);
$overdue = get_the_author_meta('asana_show_overdue', $user->ID);
$tagged = get_the_author_meta('asana_show_todays_tasks', $user->ID);
?>
true);
$body = array("data" => $completed);
$url = "tasks/".$task;
$api_key = get_the_author_meta('asana_api_key', $user_id).":";
//call the API
$response = put_asana_info($url, "PUT", $body, $api_key);
$response_r = $response["response"];
$code = $response_r["code"];
if($code == 200){
echo "Task successfully updated.";
}
else
{
echo "There was a problem communicating with Asana. The error returned was ".$response_r["message"];
}
}
}
if(get_the_author_meta('asana_api_key', $user_id) == "" || get_the_author_meta('asana_selected_workspace', $user_id) == ""){
//if either no api key or no workspace, display a message
echo "Please go to your profile 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 "";
}
}
function asana_create_task_widget(){
$user_id = get_current_user_id();
//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];
$due_date = $_POST[asana_due_date];
$url = "tasks";
$method = "POST";
$bodydata = array("name" => $name, "workspace" => get_the_author_meta('asana_workspace_id', $user_id), "assignee" => "me");
if ($notes != ""){
$bodydata["notes"] = $notes;
}
if ($due_date != ""){
$bodydata["due_on"] = $due_date;
}
$body = array("data" => $bodydata);
$api_key = get_the_author_meta('asana_api_key', $user_id).":";
//call task creation
$response = put_asana_info($url, $method, $body, $api_key);
if ( is_wp_error($response) ) {
echo "Error communicating with Asana: ".$response->get_error_message();
}
else
{
$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);
$api_key = get_the_author_meta('asana_api_key', $user_id).":";
$response = put_asana_info($url, "POST", $body, $api_key);
if ( is_wp_error($response) ) {
echo "Error communicating with Asana: ".$response->get_error_message();
}
else
{
$response_r = $response["response"];
$code = $response_r["code"];
if($code == 200){
echo " Task added to project.";
}
else
{
echo " There was a problem adding the task to the project. The error returned was ".$response_r["message"];
}
}
}
}
//can't create projects until the plugin has been configured
if(get_the_author_meta('asana_api_key', $user_id) == "" || get_the_author_meta('asana_selected_workspace', $user_id) == ""){
//if either no api key or no workspace, display a message
echo "Please go to your profile page to configure this widget";
}
else
{
//create form
$projects_url = "workspaces/".get_the_author_meta('asana_workspace_id', $user_id)."/projects";
$api_key = get_the_author_meta('asana_api_key', $user_id).":"; //note API key must have colon added to it for basic auth to work
$projects=get_asana_info($projects_url, $api_key);
echo "
array('Authorization' => 'Basic ' .base64_encode($api_key)), 'sslverify' => false);
//call API
$results = wp_remote_get($asana_url, $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){
$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;
}
// display notice about change to plugin
add_action('admin_notices', 'asana_settings_notice');
function asana_settings_notice() {
if (current_user_can('manage_options')) {
if (get_option('show_asana_upgrade_notice') != 'Version 1.0' ) {
echo '';
printf(__('The Asana Task Widget plugin now stores its information in usermeta. Please go to your profile page to set it up. | Hide Notice'), '?ignore_asana_settings_notice=0');
echo "
";
}
}
}
add_action('admin_init', 'ignore_asana_settings_notice');
function ignore_asana_settings_notice() {
// If user clicks to ignore the notice, update the option
if ( isset($_GET['ignore_asana_settings_notice']) && '0' == $_GET['ignore_asana_settings_notice'] ) {
update_option('show_asana_upgrade_notice', 'Version 1.0');
}
}
//function for calling jquery datepicker
function asana_admin_footer() {
?>