| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
global $wpdb,$current_user,$wppmfunction; |
| 6 |
if ( !empty( sanitize_text_field(wp_unslash($_POST['_ajax_nonce'])) ) ) { |
| 7 |
$nonce = sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ); |
| 8 |
if ( ! wp_verify_nonce( $nonce, 'wppm_set_clone_task' ) ) { |
| 9 |
wp_send_json_error( array( |
| 10 |
'message' => esc_html__( 'Security check failed.', 'taskbuilder' ) |
| 11 |
) ); |
| 12 |
} |
| 13 |
} |
| 14 |
$prev_task_id = isset($_POST['task_id']) ? absint(sanitize_text_field(wp_unslash($_POST['task_id']))) : $ptask_id ; |
| 15 |
$task = $wppmfunction->get_task($prev_task_id); |
| 16 |
if ( empty($task) ) { |
| 17 |
wp_send_json_error('Invalid task', 404); |
| 18 |
} |
| 19 |
if (!(($current_user->has_cap('manage_options')) || $wppmfunction->has_permission('clone_task',$task['id']))) { |
| 20 |
wp_send_json_error('Unauthorized', 403); |
| 21 |
} |
| 22 |
$wppm_default_duplicate_project_email_notification = get_option('wppm_default_duplicate_project_email_notification'); |
| 23 |
$checklists = $wppmfunction->get_checklist($prev_task_id); |
| 24 |
$task_project = isset($project_id) ? $project_id : $task['project']; |
| 25 |
$prev_task_id = esc_sql($prev_task_id); |
| 26 |
$task_comments = $wpdb->get_results( |
| 27 |
$wpdb->prepare( |
| 28 |
"SELECT * FROM {$wpdb->prefix}wppm_task_comment WHERE task_id = %d", |
| 29 |
$prev_task_id |
| 30 |
) |
| 31 |
); |
| 32 |
$task_name = isset($_POST['task_name']) ? stripslashes(sanitize_text_field(wp_unslash($_POST['task_name']))) : $task['task_name'] ; |
| 33 |
$now = wp_date("Y-m-d H:i:s"); |
| 34 |
$values = array( |
| 35 |
'created_by'=>$task['created_by'], |
| 36 |
'name'=>$task_name, |
| 37 |
'wppm_task_description'=>$task['description'], |
| 38 |
'wppm_task_project'=>$task_project, |
| 39 |
'wppm_task_start_date'=>$task['start_date'], |
| 40 |
'wppm_task_end_date'=>$task['end_date'], |
| 41 |
'status'=>$task['status'], |
| 42 |
'wppm_create_task_priority'=>$task['priority'], |
| 43 |
'user_names'=>explode(",",$task['users']), |
| 44 |
'date_created'=>$task['date_created'], |
| 45 |
'task_auth_code'=> $wppmfunction->getRandomString(10), |
| 46 |
'active'=>1 |
| 47 |
); |
| 48 |
$task_id = $wppmfunction->create_task($values); |
| 49 |
if(!isset($project_id) || $wppm_default_duplicate_project_email_notification==1){ |
| 50 |
do_action('wppm_after_task_created', $task_id); |
| 51 |
} |
| 52 |
if(!empty($checklists)){ |
| 53 |
foreach($checklists as $checklist){ |
| 54 |
$chk_values = array( |
| 55 |
'task_id' => $task_id, |
| 56 |
'checklist_name' => $checklist['checklist_name'], |
| 57 |
'created_by'=> $checklist['created_by'] |
| 58 |
); |
| 59 |
$checklist_id = $wppmfunction->create_checklist($chk_values); |
| 60 |
$checklist_items = $wppmfunction->get_checklist_items($checklist['id']); |
| 61 |
if(!empty($checklist_items)){ |
| 62 |
$chk_items_values = array(); |
| 63 |
$checklist_item_id = array(); |
| 64 |
foreach($checklist_items as $checklist_item){ |
| 65 |
$chk_items_values = array( |
| 66 |
'checklist_id' => $checklist_id, |
| 67 |
'item_name' => $checklist_item['item_name'], |
| 68 |
'checked'=>$checklist_item['checked'], |
| 69 |
'members'=>$checklist_item['members'], |
| 70 |
'due_date'=> $checklist_item['due_date'] |
| 71 |
); |
| 72 |
if(!empty($chk_items_values)){ |
| 73 |
$checklist_item_id[] = $wppmfunction->create_checklist_item($chk_items_values); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
if(!empty($task_comments)){ |
| 81 |
$args = array(); |
| 82 |
foreach($task_comments as $task_comment){ |
| 83 |
$task_comment_data = json_decode(wp_json_encode($task_comment), true); |
| 84 |
$attachment_ids = $task_comment_data['attachment_ids']; |
| 85 |
$attachment_ids_array = explode(",",$attachment_ids); |
| 86 |
if(!empty($attachment_ids_array)){ |
| 87 |
$attachment_id = array(); |
| 88 |
$values = array(); |
| 89 |
foreach($attachment_ids_array as $attach_id){ |
| 90 |
if(!empty($attach_id)){ |
| 91 |
$attachment_data = $wppmfunction->get_attachment($attach_id); |
| 92 |
$values=array( |
| 93 |
'name'=>!empty($attachment_data['name']) ? $attachment_data['name'] : '', |
| 94 |
'file_name'=> !empty($attachment_data['file_name']) ? $attachment_data['file_name'] : '', |
| 95 |
'file_path'=>!empty($attachment_data['file_path']) ? $attachment_data['file_path'] : '', |
| 96 |
'is_image'=>!empty($attachment_data['is_image']) ? $attachment_data['is_image'] : 0, |
| 97 |
'is_active'=>1, |
| 98 |
'is_uploaded'=>0, |
| 99 |
'date_created'=>!empty($attachment_data['date_created']) ? $attachment_data['date_created'] : '' |
| 100 |
); |
| 101 |
if(!empty($values)){ |
| 102 |
$wpdb->insert($wpdb->prefix.'wppm_attachments',$values); |
| 103 |
$attachment_id[] = $wpdb->insert_id; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
$args = array( |
| 109 |
'task_id'=>$task_id, |
| 110 |
'body'=>$task_comment_data['body'], |
| 111 |
'attachment_ids' =>implode(',',$attachment_id) , |
| 112 |
'create_time'=>$task_comment_data['create_time'], |
| 113 |
'created_by'=>$task_comment_data['created_by'] |
| 114 |
); |
| 115 |
if(!empty($args)){ |
| 116 |
$comment_id = $wppmfunction->wppm_submit_task_comment($args); |
| 117 |
$task_comm_id = esc_sql($task_comment->id); |
| 118 |
$task_comment_type = $wpdb->get_var( $wpdb->prepare("SELECT comment_type FROM {$wpdb->prefix}wppm_task_comment_meta WHERE task_id = %d AND comment_id = %d",$prev_task_id,$task_comm_id)); |
| 119 |
if(!empty($task_comment_type)){ |
| 120 |
$meta_values = array( |
| 121 |
'task_id'=>$task_id, |
| 122 |
'comment_id'=>$comment_id, |
| 123 |
'comment_type'=>$task_comment_type |
| 124 |
); |
| 125 |
$comment_meta_id = $wppmfunction->wppm_submit_task_comment_meta($meta_values); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
do_action("wppm_after_task_clone",$prev_task_id,$task_id); |
| 131 |
wp_send_json_success(array( |
| 132 |
'task_id' => $task_id |
| 133 |
)); |