/** * get the instance of this class * @return object the instance */ public static function get_instance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; }
static function ajax_save() { if (!isset($_POST[Kanban_Utils::get_nonce()]) || !wp_verify_nonce($_POST[Kanban_Utils::get_nonce()], sprintf('%s-save', Kanban::get_instance()->settings->basename)) || !is_user_logged_in()) { wp_send_json_error(); } do_action(sprintf('%s_before_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); $_POST['task']['modified_dt_gmt'] = Kanban_Utils::mysql_now_gmt(); if (!isset($_POST['task']['user_id_author'])) { $_POST['task']['user_id_author'] = get_current_user_id(); } $is_successful = self::_replace($_POST['task']); $task_id = isset($_POST['task']['id']) ? $_POST['task']['id'] : self::_insert_id(); $post_data = self::get_row('id', $task_id); if (!$post_data) { wp_send_json_error(); } do_action(sprintf('%s_after_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); if (!empty($_POST['comment'])) { do_action(sprintf('%s_before_%s_ajax_comment_save', Kanban::get_instance()->settings->basename, self::$slug)); Kanban_Comment::add($_POST['comment'], 'system', $task_id); do_action(sprintf('%s_after_%s_ajax_comment_save', Kanban::get_instance()->settings->basename, self::$slug)); } if (isset($_POST['status_id_old'])) { do_action(sprintf('%s_before_%s_ajax_status_change_save', Kanban::get_instance()->settings->basename, self::$slug)); Kanban_Status_Change::add($task_id, $post_data->status_id, $_POST['status_id_old']); do_action(sprintf('%s_after_%s_ajax_status_change_save', Kanban::get_instance()->settings->basename, self::$slug)); } if ($is_successful) { wp_send_json_success(array('message' => sprintf('%s saved', self::$slug), self::$slug => $post_data)); } else { wp_send_json_error(array('message' => sprintf('Error saving %s', self::$slug))); } }
static function ajax_save() { if (!isset($_POST[Kanban_Utils::get_nonce()]) || !wp_verify_nonce($_POST[Kanban_Utils::get_nonce()], sprintf('%s-save', Kanban::get_instance()->settings->basename)) || !is_user_logged_in()) { wp_send_json_error(); } do_action(sprintf('%s_before_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); $user_id_author = isset($_POST['user_id_author']) ? $_POST['user_id_author'] : get_current_user_id(); if (empty($_POST['user_id_worked'])) { $_POST['user_id_worked'] = $user_id_author; } try { $operator = substr($_POST['operator'], 0, 1) == '-' ? '-' : '+'; $val = sprintf('%s%s', $operator, abs(floatval($_POST['operator']))); } catch (Exception $e) { wp_send_json_error(array('message' => sprintf('Error saving %s', str_replace('_', ' ', self::$slug)))); } eval(sprintf('$hours = 0%s;', $val)); $data = array('task_id' => $_POST['task']['id'], 'worked_dt_gmt' => Kanban_Utils::mysql_now_gmt(), 'hours' => $hours, 'status_is' => $_POST['task']['status_id'], 'user_id_author' => $user_id_author, 'user_id_worked' => $_POST['user_id_worked']); $is_successful = self::_insert($data); do_action(sprintf('%s_after_%s_ajax_save', Kanban::get_instance()->settings->basename, self::$slug)); if (!empty($_POST['comment'])) { do_action(sprintf('%s_before_%s_ajax_comment_save', Kanban::get_instance()->settings->basename, self::$slug)); Kanban_Comment::add($_POST['comment'], 'system', $_POST['task']['id']); do_action(sprintf('%s_after_%s_ajax_comment_save', Kanban::get_instance()->settings->basename, self::$slug)); } if ($is_successful) { wp_send_json_success(array('message' => sprintf('%s saved', str_replace('_', ' ', self::$slug)))); } else { wp_send_json_error(array('message' => sprintf('Error saving %s', str_replace('_', ' ', self::$slug)))); } }
static function ajax_delete() { if (!isset($_POST[Kanban_Utils::get_nonce()]) || !wp_verify_nonce($_POST[Kanban_Utils::get_nonce()], 'kanban-save') || !is_user_logged_in()) { wp_send_json_error(); } do_action('kanban_task_ajax_delete_before', $_POST['task']['id']); // $is_successful = Kanban_Post::delete($_POST); $is_successful = self::delete($_POST['task']['id']); do_action('kanban_task_ajax_delete_after', $_POST['task']['id']); if (!empty($_POST['comment'])) { do_action('kanban_task_ajax_delete_before_comment'); Kanban_Comment::add($_POST['comment'], 'system', $_POST['task']['id']); do_action('kanban_task_ajax_delete_after_comment'); } if ($is_successful) { wp_send_json_success(array('message' => sprintf('%s deleted', self::$slug))); } else { wp_send_json_error(array('message' => sprintf('Error deleting %s', self::$slug))); } }
/** * move users * move terms (not deleted, as we need them for tasks) * move projects * move tasks * move comments * delete projects * delete terms * clean up * @return [type] [description] */ static function ajax_migrate_records() { global $wpdb; // build response $response = array('posts_remaining' => Kanban::get_instance()->settings->records_to_move, 'continue' => FALSE, 'done' => FALSE); // check for users to move $is_users_moved = get_option('kanban_migrate_users_moved'); if (!$is_users_moved) { $sql = "SELECT\n\t\t\t\t\t{$wpdb->prefix}options.option_value\n\t\t\t\t\tFROM {$wpdb->prefix}options\n\t\t\t\t\tWHERE `option_name` = 'kanban_user'\n\t\t\t;"; $users = $wpdb->get_var($sql); if (!empty($users)) { $users = unserialize($users); $data = array('name' => 'allowed_users', 'value' => serialize($users['allowed_users'])); Kanban_Option::_replace($data); delete_option('kanban_user'); } update_option('kanban_migrate_users_moved', TRUE); $response['posts_remaining'] = $response['posts_remaining'] - 1; $response['continue'] = TRUE; $response['message'] = 'Allowed users moved'; wp_send_json_success($response); } // check for terms to move $is_terms_moved = get_option('kanban_migrate_terms_moved'); if (!$is_terms_moved) { $sql = "SELECT\n\t\t\t\t\t{$wpdb->prefix}term_taxonomy.`taxonomy`\n\t\t\t\t\t, {$wpdb->prefix}term_taxonomy.`term_taxonomy_id`\n\t\t\t\t\t, {$wpdb->prefix}terms.`term_id`\n\t\t\t\t\t, {$wpdb->prefix}terms.`name`\n\t\t\t\t\t, {$wpdb->prefix}terms.`slug`\n\t\t\t\t\tFROM {$wpdb->prefix}terms\n\t\t\t\t\tJOIN {$wpdb->prefix}term_taxonomy\n\t\t\t\t\tON {$wpdb->prefix}terms.`term_id` = {$wpdb->prefix}term_taxonomy.`term_id`\n\t\t\t;"; $terms = $wpdb->get_results($sql); // if we need to move terms if (!empty($terms)) { // get settings $kanban_task_status_order = get_option('kanban_task_status_order'); $kanban_task_status_colors = get_option('kanban_task_status_colors'); $kanban_task_estimate_order = get_option('kanban_task_estimate_order'); // get statuses for matching $status_table = Kanban_Status::table_name(); $sql = "SELECT * FROM {$status_table};"; $statuses = $wpdb->get_results($sql); $status_arr = array(); foreach ($statuses as $status) { $status_arr[$status->title] = $status->id; } // get estimates for matching $estimates_table = Kanban_Estimate::table_name(); $sql = "SELECT * FROM {$estimates_table};"; $estimates = $wpdb->get_results($sql); $estimate_arr = array(); foreach ($estimates as $estimate) { $estimate_arr[$estimate->title] = $estimate->id; } // add each term foreach ($terms as $term) { switch ($term->taxonomy) { case 'kanban_task_status': if (isset($status_arr[$term->name])) { continue; } $data = array('title' => $term->name, 'color_hex' => $kanban_task_status_colors[$term->term_id], 'position' => $kanban_task_status_order[$term->term_id]); $success = Kanban_Status::replace($data); break; case 'kanban_task_estimate': if (isset($estimate_arr[$term->name])) { continue; } $data = array('title' => $term->name, 'hours' => $term->slug, 'position' => $kanban_task_estimate_order[$term->term_id]); $success = Kanban_Estimate::replace($data); break; } } } update_option('kanban_migrate_terms_moved', TRUE); $response['posts_remaining'] = $response['posts_remaining'] - 2; $response['continue'] = TRUE; $response['message'] = 'statuses and estimates updated'; wp_send_json_success($response); } // is_terms_moved // move projects $sql = "SELECT posts.ID as id\n\t\t\t, posts.post_title as title\n\t\t\t, posts.post_modified_gmt as modified_dt_gmt\n\t\t\t, posts.post_date_gmt as created_dt_gmt\n\t\t\t, posts.post_author as user_id_author\n\n\t\t\tFROM {$wpdb->prefix}posts posts\n\n\t\t\tWHERE posts.`post_type` = 'kanban_project'\n\t\t\tAND posts.`post_status` = 'publish'\n\t\t\tLIMIT 1\n\t\t;"; $projects = $wpdb->get_results($sql); if (!empty($projects)) { foreach ($projects as $post) { $data = array('title' => $post->title, 'created_dt_gmt' => $post->created_dt_gmt, 'modified_dt_gmt' => $post->modified_dt_gmt, 'user_id_author' => $post->user_id_author, 'is_active' => 1); $success = Kanban_Project::replace($data); if ($success) { $wpdb->update("{$wpdb->prefix}posts", array('post_status' => 'trash'), array('ID' => $post->id)); } } // projects $response['posts_remaining'] = $response['posts_remaining'] - 3; $response['continue'] = TRUE; $response['message'] = sprintf('project %s moved', $post->id); wp_send_json_success($response); } // projects // move tasks $sql = "SELECT posts.ID as id\n\t\t\t, posts.post_title as title\n\t\t\t, posts.post_modified_gmt as modified_dt_gmt\n\t\t\t, posts.post_date_gmt as created_dt_gmt\n\t\t\t, posts.post_author as user_id_author\n\t\t\t, p_project_name.post_title as project_name\n\t\t\t, pm_user_id_assigned.meta_value as user_id_assigned\n\t\t\t, pm_work_hour_count.meta_value as work_hour_count\n\n\t\t\tFROM {$wpdb->prefix}posts posts\n\n\t\t\tJOIN {$wpdb->prefix}postmeta pm_project_id\n\t\t\t\tON pm_project_id.post_id = posts.ID\n\t\t\t\tAND pm_project_id.meta_key = 'kanban_task_project_id'\n\n\t\t\tJOIN {$wpdb->prefix}posts p_project_name\n\t\t\t\tON pm_project_id.meta_value = p_project_name.ID\n\n\t\t\tJOIN {$wpdb->prefix}postmeta pm_user_id_assigned\n\t\t\t\tON pm_user_id_assigned.post_id = posts.ID\n\t\t\t\tAND pm_user_id_assigned.meta_key = 'kanban_task_user_id_assigned'\n\n\t\t\tJOIN {$wpdb->prefix}postmeta pm_work_hour_count\n\t\t\t\tON pm_work_hour_count.post_id = posts.ID\n\t\t\t\tAND pm_work_hour_count.meta_key = 'kanban_task_work_hour_count'\n\n\t\t\tWHERE posts.`post_type` = 'kanban_task'\n\t\t\tAND posts.`post_status` = 'publish'\n\t\t\tLIMIT 1\n\t\t;"; $tasks = $wpdb->get_results($sql); if (!empty($tasks)) { // get statuses for matching $status_table = Kanban_Status::table_name(); $sql = "SELECT * FROM {$status_table};"; $statuses = $wpdb->get_results($sql); $status_arr = array(); foreach ($statuses as $status) { $status_arr[$status->title] = $status->id; } // get estimates for matching $estimates_table = Kanban_Estimate::table_name(); $sql = "SELECT * FROM {$estimates_table};"; $estimates = $wpdb->get_results($sql); $estimate_arr = array(); foreach ($estimates as $estimate) { $estimate_arr[$estimate->title] = $estimate->id; } // get projects for matching $projects_table = Kanban_Project::table_name(); $sql = "SELECT * FROM {$projects_table};"; $projects = $wpdb->get_results($sql); // build look up array by name $projects_arr = array(); if (!empty($projects)) { foreach ($projects as $project) { $projects_arr[$project->title] = $project->id; } } $estimate_arr = array(); foreach ($estimates as $estimate) { $estimate_arr[$estimate->title] = $estimate->id; } foreach ($tasks as $post) { // get terms for this task $sql = "SELECT\n\t\t\t\t\t\t{$wpdb->prefix}terms.name\n\t\t\t\t\t\t, {$wpdb->prefix}term_taxonomy.`taxonomy`\n\t\t\t\t\t\tFROM {$wpdb->prefix}terms\n\t\t\t\t\t\tJOIN {$wpdb->prefix}term_taxonomy\n\t\t\t\t\t\tON {$wpdb->prefix}terms.term_id = {$wpdb->prefix}term_taxonomy.term_id\n\t\t\t\t\t\tJOIN {$wpdb->prefix}term_relationships\n\t\t\t\t\t\tON {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id\n\t\t\t\t\t\tWHERE {$wpdb->prefix}term_relationships.object_id = %s\n\t\t\t\t;"; $terms = $wpdb->get_results($wpdb->prepare($sql, $post->id)); $terms_arr = array(); foreach ($terms as $term_data) { $terms_arr[$term_data->taxonomy] = $term_data->name; } if (isset($terms_arr['kanban_task_status']) && isset($status_arr[$terms_arr['kanban_task_status']])) { $status_id = $status_arr[$terms_arr['kanban_task_status']]; } else { $status_id = 0; } if (isset($terms_arr['kanban_task_estimate']) && isset($estimate_arr[$terms_arr['kanban_task_estimate']])) { $estimate_id = $estimate_arr[$terms_arr['kanban_task_estimate']]; } else { $estimate_id = 0; } if (isset($projects_arr[$post->project_name])) { $project_id = $projects_arr[$post->project_name]; } else { $project_id = 0; } // build task data to save $data = array('title' => $post->title, 'created_dt_gmt' => $post->created_dt_gmt, 'modified_dt_gmt' => $post->modified_dt_gmt, 'user_id_author' => $post->user_id_author, 'user_id_assigned' => $post->user_id_assigned, 'status_id' => $status_id, 'project_id' => $project_id, 'estimate_id' => $estimate_id, 'is_active' => 1); $success = Kanban_Task::replace($data); $task_id = self::_insert_id(); if ($success) { $wpdb->update("{$wpdb->prefix}posts", array('post_status' => 'trash'), array('ID' => $post->id)); } $response['message'] = sprintf('task %s moved', $post->id); // get comments for task $sql = "SELECT *\n\t\t\t\t\t\tFROM {$wpdb->prefix}posts posts\n\t\t\t\t\t\tWHERE posts.`post_type` = 'kanban_task_comment'\n\t\t\t\t\t\tAND posts.`post_parent` = {$post->id}\n\t\t\t\t\t\tAND posts.`post_status` = 'publish'\n\t\t\t\t\t\t;"; $comments = $wpdb->get_results($sql); if (count($comments) > 0) { foreach ($comments as $comment) { $data = array('description' => $comment->post_content, 'created_dt_gmt' => $comment->post_date_gmt, 'modified_dt_gmt' => $comment->post_modified_gmt, 'comment_type' => 'system', 'task_id' => $task_id, 'user_id_author' => $comment->post_author); $success = Kanban_Comment::insert($data); // mark as trash if ($success) { $wpdb->update("{$wpdb->prefix}posts", array('post_status' => 'trash'), array('ID' => $comment->ID)); } // add task hour if (strpos($comment->post_content, 'hour of work') !== FALSE) { $data = array('task_id' => $task_id, 'created_dt_gmt' => $comment->post_date_gmt, 'hours' => 1, 'status_id' => $status_id, 'user_id_author' => $comment->post_author, 'user_id_worked' => $comment->post_author); $success = Kanban_Task_Hour::insert($data); } } $response['message'] .= sprintf('. %s comments moved', count($comments)); } // $comments } // $tasks $response['posts_remaining'] = Kanban::get_instance()->settings->records_to_move - 4; $response['continue'] = TRUE; wp_send_json_success($response); } // task // $is_posts_deleted = get_option('kanban_migrate_posts_deleted'); // if ( !$is_posts_deleted ) // { // // delete any orphaned posts // $success = $wpdb->query("DELETE FROM $wpdb->posts // WHERE post_type LIKE 'kanban%' // OR post_type LIKE 'kbwp%' // ;" // ); // update_option('kanban_migrate_posts_deleted', TRUE); // $response['posts_remaining'] = Kanban::get_instance()->settings->records_to_move; // $response['continue'] = TRUE; // $response['message'] = sprintf('%s posts cleaned up', count($success)); // wp_send_json_success($response); // } // // delete terms // $sql = "SELECT // {$wpdb->prefix}term_taxonomy.`taxonomy` // , {$wpdb->prefix}term_taxonomy.`term_taxonomy_id` // , {$wpdb->prefix}terms.`term_id` // , {$wpdb->prefix}terms.`name` // , {$wpdb->prefix}terms.`slug` // FROM {$wpdb->prefix}terms // JOIN {$wpdb->prefix}term_taxonomy // ON {$wpdb->prefix}terms.`term_id` = {$wpdb->prefix}term_taxonomy.`term_id` // ;"; // $terms = $wpdb->get_results($sql); // // if we need to delete terms // if ( !empty($terms) ) // { // // add each term // foreach ($terms as $term) // { // $wpdb->delete( // sprintf('%sterms', $wpdb->prefix), // array('term_id' => $term->term_id) // ); // $wpdb->delete( // sprintf('%sterm_relationships', $wpdb->prefix), // array('term_taxonomy_id' => $term->term_taxonomy_id) // ); // $wpdb->delete( // sprintf('%sterm_taxonomy', $wpdb->prefix), // array('term_taxonomy_id' => $term->term_taxonomy_id) // ); // } // $response['continue'] = TRUE; // $response['message'] = 'statuses and estimates removed'; // wp_send_json_success($response); // } // cleanup records $wpdb->update("{$wpdb->prefix}posts", array('post_status' => 'trash'), array('post_status' => 'publish', 'post_type' => 'kanban_task')); $wpdb->update("{$wpdb->prefix}posts", array('post_status' => 'trash'), array('post_status' => 'publish', 'post_type' => 'kanban_project')); $wpdb->update("{$wpdb->prefix}posts", array('post_status' => 'trash'), array('post_status' => 'publish', 'post_type' => 'kanban_task_comment')); // clean up temp values delete_option('kanban_migrate_users_moved'); delete_option('kanban_migrate_terms_moved'); delete_option('kanban_migrate_posts_deleted'); // clean up old data // delete_option('kanban_task_status_order'); // delete_option('kanban_task_status_colors'); // delete_option('kanban_task_estimate_order'); Kanban_Db::add_defaults(); $response['done'] = TRUE; wp_send_json_success($response); }