| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
global $current_user,$wpdb,$wppmfunction; |
| 6 |
if ( check_ajax_referer( 'wppm_add_new_subtask', '_ajax_nonce', false ) != 1 ) { |
| 7 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 8 |
} |
| 9 |
$task_id = isset($_POST) && isset($_POST['task_id']) ? absint(sanitize_text_field(wp_unslash($_POST['task_id']))) : ''; |
| 10 |
if (!$task_id) {exit;} |
| 11 |
|
| 12 |
$parent_task = $wppmfunction->get_task($task_id); |
| 13 |
if (empty($parent_task)) {exit;} |
| 14 |
// Subtasks are only one level deep - adding a subtask under an existing subtask is not allowed. |
| 15 |
if (!empty($parent_task['parent_task_id'])) {exit;} |
| 16 |
|
| 17 |
if (!(($current_user->ID && $current_user->has_cap('manage_options')) || $wppmfunction->has_permission('add_new_task',$task_id))) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
$subtask_name = isset($_POST) && isset($_POST['label']) ? sanitize_text_field(wp_unslash($_POST['label'])) : ''; |
| 22 |
if (!$subtask_name) {exit;} |
| 23 |
|
| 24 |
$args = array( |
| 25 |
'name'=>$subtask_name, |
| 26 |
'wppm_task_project'=>$parent_task['project'], |
| 27 |
'parent_task_id'=>$task_id |
| 28 |
); |
| 29 |
|
| 30 |
// Description |
| 31 |
if ( !empty( $_POST['wppm_subtask_description'] ) ) { |
| 32 |
$args['wppm_task_description'] = wp_kses_post( wp_unslash( $_POST['wppm_subtask_description'] ) ); |
| 33 |
} |
| 34 |
|
| 35 |
// Status - must be a real status row, otherwise fall back to the default. |
| 36 |
$subtask_status = isset($_POST['wppm_subtask_status']) ? absint(sanitize_text_field(wp_unslash($_POST['wppm_subtask_status']))) : 0; |
| 37 |
if ($subtask_status) { |
| 38 |
$subtask_status_exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}wppm_task_statuses WHERE id = %d", $subtask_status)); |
| 39 |
if ($subtask_status_exists) { |
| 40 |
$args['status'] = $subtask_status; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Due date |
| 45 |
$subtask_due_date = isset($_POST['wppm_subtask_due_date']) ? sanitize_text_field(wp_unslash($_POST['wppm_subtask_due_date'])) : ''; |
| 46 |
if ($subtask_due_date) { |
| 47 |
$args['wppm_task_end_date'] = wp_date("Y-m-d H:i:s", strtotime($subtask_due_date)); |
| 48 |
} |
| 49 |
|
| 50 |
// Assignees |
| 51 |
if ( isset( $_POST['wppm_subtask_user_names'] ) && is_array( $_POST['wppm_subtask_user_names'] ) ) { |
| 52 |
$subtask_users = array_unique( array_map( 'absint', wp_unslash( $_POST['wppm_subtask_user_names'] ) ) ); |
| 53 |
$subtask_users = array_filter( $subtask_users ); |
| 54 |
if ( $subtask_users ) { |
| 55 |
$args['user_names'] = $subtask_users; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
$args = apply_filters( 'wppm_before_create_subtask_args', $args, $task_id); |
| 60 |
$subtask_id = WPPM_Functions::create_task($args); |
| 61 |
do_action('wppm_after_task_created', $subtask_id); |
| 62 |
do_action('wppm_after_subtask_created', $subtask_id, $task_id); |
| 63 |
|