function inline_edit_mg_task_meta() { $response = false; $new_data = ''; $r = array(); // we need the post IDs $post_id = isset($_POST['post_ID']) && !empty($_POST['post_ID']) ? $_POST['post_ID'] : NULL; // if we have post IDs if (!empty($post_id) && is_numeric($post_id)) { if (!empty($_POST['referrer']) && $_POST['referrer'] !== 'quick_save') { $_SESSION['doing_inline_edit'] = true; $field = str_replace('-', '_', $_POST['referrer']); $_SESSION['changed_docs'] = array('invoices' => array(), 'estimates' => array()); if ($field === 'status') { $field = 'task_status'; } // if it has a value, doesn't update if empty on bulk if (isset($_POST[$field]) && !empty($_POST[$field])) { if ($field === 'task_status') { $update_post = wp_update_post(array('ID' => $post_id, 'post_status' => $_POST[$field])); if (is_numeric($update_post) && intval($update_post) > 0) { $new_data = $_POST[$field]; } else { $new_data = false; } } else { $old_value = get_post_meta($post_id, $field, true); $response = update_post_meta($post_id, $field, $_POST[$field], $old_value); $new_data = get_post_meta($post_id, $field, true); if ($field === 'estimated_time' && $response === true) { if (isset($_SESSION['changed_docs']['invoices']) && check_for_value($_SESSION['changed_docs']['invoices'])) { foreach ($_SESSION['changed_docs']['invoices'] as $item) { $item_id = maybe_get_pod_id($item); $object = SI_Invoice::get_instance($item_id[0]); if (isset($object) && is_object($object)) { $object->set_calculated_total(); } } } if (isset($_SESSION['changed_docs']['estimates']) && check_for_value($_SESSION['changed_docs']['estimates'])) { foreach ($_SESSION['changed_docs']['estimates'] as $item) { $item_id = maybe_get_pod_id($item); $object = SI_Estimate::get_instance($item_id[0]); if (isset($object) && is_object($object)) { $object->set_calculated_total(); } } } if (!is_numeric($new_data)) { $new_data = convert_estimated_time_to_minutes($new_data); } } } $r = array('post_id' => (int) $post_id, $field => $new_data); } } elseif ($_POST['referrer'] == 'quick_save') { $new_data = get_post_meta($post_id, 'estimated_time', true); $r = array('post_id' => (int) $post_id, 'estimated_time' => (int) $new_data); } unset($_SESSION['changed_docs']); } unset($_SESSION['doing_inline_edit']); $response = json_encode($r, JSON_FORCE_OBJECT); exit($response); }
function add_estimate_line_item($pieces, $estimates, $id) { $priority = sanitize_title(get_post_meta($id, 'priority', true)); if (check_for_value($priority) !== true) { $priority = 'no-priority'; } $issue_type = sanitize_title(get_post_meta($id, 'issue_type', true)); if (check_for_value($issue_type) !== true) { $issue_type = 'no-issue'; } if (!isset($estimates) || check_for_value($estimates) !== true) { return; //this is not the function you are looking for } $fields = array('rate', 'description', 'quantity', 'percent_adjustment', 'estimated_time'); $populated_fields = array(); foreach ($fields as $field) { if (!isset($pieces['fields'][$field]['value']) || isset($pieces['fields'][$field]['value']) && $pieces['fields'][$field]['value'] == null) { $val = get_post_meta($id, $field, true); if ($field === "percent_adjustment") { if ($val === '') { $val = 0; } } $populated_fields[$field] = $val; if ($field === 'estimated_time') { $populated_fields[$field] = convert_estimated_time_to_minutes($populated_fields[$field]); } } else { $populated_fields[$field] = $pieces['fields'][$field]['value']; } } $convert_min_to_hr = intval($populated_fields['estimated_time']) / 60; $rate = floatval($populated_fields['rate']); $quantity = floatval($populated_fields['quantity']); $percent_adjustment = floatval($populated_fields['percent_adjustment']); $new_rate = $convert_min_to_hr * $rate; $total = round($new_rate * $quantity - $new_rate * $quantity * ($percent_adjustment / 100), 2); if (!is_array($estimates)) { $estimates = explode(",", $estimates); } foreach ($estimates as $estimate) { unset($fired); $SI_Estimate = SI_Estimate::get_instance($estimate); $line_items = $SI_Estimate->get_line_items(); /** * Even though we are adding a new line items, sometimes meta gets broken * Lets make sure that we dnn't ACTUALLY need to just update a line item. */ $new_line_items_array = array(); foreach ($line_items as $li) { if (strpos($li['desc'], "task-{$id}") !== false || strpos($li['desc'], 'task: ' . $id) !== false) { $new_line_items_array[] = array('rate' => $new_rate, 'qty' => $populated_fields['quantity'], 'desc' => "<span id='task-{$id}' class='{$priority} {$issue_type}'><strong class='title'>" . get_the_title($id) . "</strong><br/>" . $populated_fields['description'] . "</span>", 'type' => 'task', 'total' => $total, 'tax' => $populated_fields['percent_adjustment']); $fired = true; } else { $new_line_items_array[] = $li; } } /** * If there wasn't an update then let's actually add one * Is the ADD function uneccessary? Since we are checking for an update * anyhow. */ if (!isset($fired) || isset($fired) && $fired !== true) { $new_line_items_array[] = array('rate' => $new_rate, 'qty' => $populated_fields['quantity'], 'desc' => "<span id='task-{$id}' class='{$priority} {$issue_type}'><strong class='title'>" . get_the_title($id) . "</strong><br/>" . $populated_fields['description'] . "</span>", 'type' => 'task', 'total' => $total, 'tax' => $populated_fields['percent_adjustment']); } /** * Line Items array is now updated, lets save them */ $SI_Estimate->set_line_items($new_line_items_array); } if (isset($_SESSION['changed_docs']['estimates'])) { $_SESSION['changed_docs']['estimates'] = array_merge($estimates, $_SESSION['changed_docs']['estimates']); } }