/** * Save the event transaction * * */ function mdjm_save_event_transaction_ajax() { global $mdjm_event; $result = array(); $mdjm_event = new MDJM_Event($_POST['event_id']); $mdjm_txn = new MDJM_Txn(); $txn_data = array('post_parent' => $_POST['event_id'], 'post_author' => $mdjm_event->client, 'post_status' => $_POST['direction'] == 'Out' ? 'mdjm-expenditure' : 'mdjm-income', 'post_date' => date('Y-m-d H:i:s', strtotime($_POST['date']))); $txn_meta = array('_mdjm_txn_status' => 'Completed', '_mdjm_payment_from' => $mdjm_event->client, '_mdjm_txn_total' => $_POST['amount'], '_mdjm_payer_firstname' => mdjm_get_client_firstname($mdjm_event->client), '_mdjm_payer_lastname' => mdjm_get_client_lastname($mdjm_event->client), '_mdjm_payer_email' => mdjm_get_client_email($mdjm_event->client), '_mdjm_payment_from' => mdjm_get_client_display_name($mdjm_event->client), '_mdjm_txn_source' => $_POST['src']); if ($_POST['direction'] == 'In') { if (!empty($_POST['from'])) { $txn_meta['_mdjm_payment_from'] = sanitize_text_field($_POST['from']); } else { $txn_meta['_mdjm_payment_from'] = mdjm_get_client_display_name($mdjm_event->client); } } if ($_POST['direction'] == 'Out') { if (!empty($_POST['to'])) { $txn_meta['_mdjm_payment_to'] = sanitize_text_field($_POST['to']); } else { $txn_meta['_mdjm_payment_to'] = mdjm_get_client_display_name($mdjm_event->client); } } $mdjm_txn->create($txn_data, $txn_meta); if ($mdjm_txn->ID > 0) { $result['type'] = 'success'; mdjm_set_txn_type($mdjm_txn->ID, $_POST['for']); $args = array('user_id' => get_current_user_id(), 'event_id' => $_POST['event_id'], 'comment_content' => sprintf(__('%1$s payment of %2$s received for %3$s %4$s.', 'mobile-dj-manager'), $_POST['direction'] == 'In' ? __('Incoming', 'mobile-dj-manager') : __('Outgoing', 'mobile-dj-manager'), mdjm_currency_filter(mdjm_format_amount($_POST['amount'])), mdjm_get_label_singular(true), mdjm_get_event_contract_id($_POST['event_id']))); mdjm_add_journal($args); // Email overide if (empty($_POST['send_notice']) && mdjm_get_option('manual_payment_cfm_template')) { $manual_email_template = mdjm_get_option('manual_payment_cfm_template'); mdjm_update_option('manual_payment_cfm_template', 0); } $payment_for = $mdjm_txn->get_type(); $amount = mdjm_currency_filter(mdjm_format_amount($_POST['amount'])); mdjm_add_content_tag('payment_for', __('Reason for payment', 'mobile-dj-manager'), function () use($payment_for) { return $payment_for; }); mdjm_add_content_tag('payment_amount', __('Payment amount', 'mobile-dj-manager'), function () use($amount) { return $amount; }); mdjm_add_content_tag('payment_date', __('Date of payment', 'mobile-dj-manager'), 'mdjm_content_tag_ddmmyyyy'); /** * Allow hooks into this payment. The hook is suffixed with 'in' or 'out' depending * on the payment direction. i.e. mdjm_post_add_manual_txn_in and mdjm_post_add_manual_txn_out * * @since 1.3.7 * @param int $event_id * @param obj $txn_id */ do_action('mdjm_post_add_manual_txn_' . strtolower($_POST['direction']), $_POST['event_id'], $mdjm_txn->ID); // Email overide if (empty($_POST['send_notice']) && isset($manual_email_template)) { mdjm_update_option('manual_payment_cfm_template', $manual_email_template); } $result['deposit_paid'] = 'N'; $result['balance_paid'] = 'N'; if ($mdjm_event->get_remaining_deposit() < 1) { mdjm_update_event_meta($mdjm_event->ID, array('_mdjm_event_deposit_status' => 'Paid')); $result['deposit_paid'] = 'Y'; } if ($mdjm_event->get_balance() < 1) { mdjm_update_event_meta($mdjm_event->ID, array('_mdjm_event_balance_status' => 'Paid')); mdjm_update_event_meta($mdjm_event->ID, array('_mdjm_event_deposit_status' => 'Paid')); $result['balance_paid'] = 'Y'; $result['deposit_paid'] = 'Y'; } } else { $result['type'] = 'error'; $result['msg'] = __('Unable to add transaction', 'mobile-dj-manager'); } ob_start(); mdjm_do_event_txn_table($_POST['event_id']); $result['transactions'] = ob_get_contents(); ob_get_clean(); echo json_encode($result); die; }
/** * Updates an event once a payment is completed. * * @since 1.3.8 * @param arr $txn_data Transaction data from gateway. * @return void */ function mdjm_update_event_after_payment($txn_data) { $type = mdjm_get_txn_type($txn_data['txn_id']); if ($type == mdjm_get_deposit_label()) { $meta['_mdjm_event_deposit_status'] = 'Paid'; } else { if ($type == mdjm_get_balance_label()) { $meta['_mdjm_event_deposit_status'] = 'Paid'; $meta['_mdjm_event_balance_status'] = 'Paid'; } else { if (mdjm_get_event_remaining_deposit($txn_data['event_id']) < 1) { $meta['_mdjm_event_deposit_status'] = 'Paid'; } if (mdjm_get_event_balance($txn_data['event_id']) < 1) { $meta['_mdjm_event_deposit_status'] = 'Paid'; $meta['_mdjm_event_balance_status'] = 'Paid'; } } } mdjm_update_event_meta($txn_data['event_id'], $meta); // Update the journal mdjm_add_journal(array('user_id' => $txn_data['client_id'], 'event_id' => $txn_data['event_id'], 'comment_content' => sprintf(__('%s of %s received via %s', 'mobile-dj-manager'), $type, mdjm_currency_filter(mdjm_format_amount($txn_data['total'])), mdjm_get_gateway_admin_label($txn_data['gateway'])))); }
/** * Creates an event * * @since 1.3 * @param arr $data Array of attributes for an event. See $defaults. * @return mixed false if data isn't passed and class not instantiated for creation, or New Event ID */ public function create($data = array(), $meta = array()) { if ($this->id != 0) { return false; } remove_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3); $defaults = array('post_type' => 'mdjm-event', 'post_author' => 1, 'post_content' => '', 'post_status' => 'mdjm-enquiry', 'post_title' => __('New Event', 'mobile-dj-manager')); $default_meta = array('_mdjm_event_date' => date('Y-m-d'), '_mdjm_event_dj' => !mdjm_get_option('employer') ? 1 : 0, '_mdjm_event_playlist_access' => mdjm_generate_playlist_guest_code(), '_mdjm_event_playlist' => mdjm_get_option('enable_playlists') ? 'Y' : 'N', '_mdjm_event_contract' => mdjm_get_default_event_contract(), '_mdjm_event_cost' => 0, '_mdjm_event_deposit' => 0, '_mdjm_event_deposit_status' => __('Due', 'mobile-dj-manager'), '_mdjm_event_balance_status' => __('Due', 'mobile-dj-manager'), 'mdjm_event_type' => mdjm_get_option('event_type_default'), 'mdjm_enquiry_source' => mdjm_get_option('enquiry_source_default'), '_mdjm_event_venue_id' => 'Manual'); $data = wp_parse_args($data, $defaults); $meta = wp_parse_args($meta, $default_meta); do_action('mdjm_event_pre_create', $data, $meta); $id = wp_insert_post($data, true); $event = WP_Post::get_instance($id); if ($event) { if (!empty($meta['mdjm_event_type'])) { mdjm_set_event_type($event->ID, $meta['mdjm_event_type']); $meta['_mdjm_event_name'] = get_term($meta['mdjm_event_type'], 'event-types')->name; $meta['_mdjm_event_name'] = apply_filters('mdjm_event_name', $meta['_mdjm_event_name'], $id); } if (!empty($meta['mdjm_enquiry_source'])) { mdjm_set_enquiry_source($event->ID, $meta['mdjm_enquiry_source']); } if (!empty($meta['_mdjm_event_start']) && !empty($meta['_mdjm_event_finish'])) { if (date('H', strtotime($meta['_mdjm_event_finish'])) > date('H', strtotime($meta['_mdjm_event_start']))) { $meta['_mdjm_event_end_date'] = $meta['_mdjm_event_date']; } else { $meta['_mdjm_event_end_date'] = date('Y-m-d', strtotime('+1 day', strtotime($meta['_mdjm_event_date']))); } } if (!empty($meta['_mdjm_event_package'])) { $meta['_mdjm_event_cost'] += mdjm_get_package_price($meta['_mdjm_event_package'], $meta['_mdjm_event_date']); } if (!empty($meta['_mdjm_event_addons'])) { foreach ($meta['_mdjm_event_addons'] as $addon) { $meta['_mdjm_event_cost'] += mdjm_get_addon_price($addon); } } if (empty($meta['_mdjm_event_deposit'])) { $meta['_mdjm_event_deposit'] = mdjm_calculate_deposit($meta['_mdjm_event_cost']); } mdjm_update_event_meta($event->ID, $meta); wp_update_post(array('ID' => $id, 'post_title' => mdjm_get_event_contract_id($id), 'post_name' => mdjm_get_event_contract_id($id))); } do_action('mdjm_event_post_create', $id, $data); add_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3); return $this->setup_event($event); }
/** * Manage checkboxes on event save. * When an event is saved on the event screen and a checkbox is not checked, * no value is posted and therefore the event meta key is not updated. * * @params obj $post The events WP_POST object * @return void */ function manage_custom_fields_on_event_save($post) { $mdjm_event = new MDJM_Event($post->ID); $query = mdjm_get_custom_fields(); $custom_fields = $query->get_posts(); foreach ($custom_fields as $custom_field) { if ('checkbox' != get_post_meta($custom_field->ID, '_mdjm_field_type', true)) { continue; } $key = '_mdjm_event_' . str_replace('-', '_', $custom_field->post_name); if ($mdjm_event->get_meta($key) && empty($_POST[$key])) { $meta[$key] = ''; } } if (!empty($meta)) { mdjm_update_event_meta($mdjm_event->ID, $meta); } }
/** * Update event status to Rejected Enquiry. * * If you're looking for hooks, see the mdjm_update_event_status() function. * Do not call this function directly, instead call mdjm_update_event_status() to ensure * all hooks are processed. * * @since 1.3 * @param int $event_id The event ID. * @param str $old_status The old event status. * @param arr $args Array of data required for transition. * @return int The ID of the event if it is successfully updated. Otherwise returns 0. */ function mdjm_set_event_status_mdjm_rejected($event_id, $old_status, $args = array()) { remove_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3); $update = wp_update_post(array('ID' => $event_id, 'post_status' => 'mdjm-rejected')); add_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3); // Meta updates $args['meta']['_mdjm_event_last_updated_by'] = is_user_logged_in() ? get_current_user_id() : 1; mdjm_update_event_meta($event_id, $args['meta']); return $update; }
/** * Save the meta data for the event * * @since 0.7 * @param int $post_id The current event post ID. * @param obj $post The current event post object (WP_Post). * @param bool $update Whether this is an existing post being updated or not. * * @return void */ function mdjm_save_event_post($post_id, $post, $update) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if ($post->post_status == 'trash') { return; } if (empty($update)) { return; } // Permission Check if (!mdjm_employee_can('manage_events')) { MDJM()->debug->log_it(sprintf('PERMISSION ERROR: User %s is not allowed to edit events', get_current_user_id())); return; } // Remove the save post action to avoid loops. remove_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3); // Fire our pre-save hook do_action('mdjm_pre_event_save', $post_id, $post, $update); $debug[] = 'Starting Event Save'; // Get current meta data for the post so we can track changes within the journal. $current_meta = get_post_meta($post_id); /** * Get the Client ID and store it in the event data array. * If a client has been selected from the dropdown, we simply use that ID. * If adding a new client, call the method and use the returned user ID. */ $event_data['_mdjm_event_client'] = $_POST['client_name'] != 'add_new' ? $_POST['client_name'] : mdjm_add_client(); /** * For new events we fire the 'mdjm_add_new_event' action */ if (empty($update)) { do_action('mdjm_create_new_event', $post); } /** * If the client is flagged to have their password reset, set the flag. * The flag will be checked and processed during the content tag filtering process. */ if (!empty($_POST['mdjm_reset_pw'])) { $debug[] = sprintf('Client %s flagged for password reset', $event_data['_mdjm_event_client']); update_user_meta($event_data['_mdjm_event_client'], 'mdjm_pass_action', true); } /** * Determine the Venue ID if an existing venue was selected. * Otherwise, determine if we're using the client's address or adding a manual venue address */ if ($_POST['venue_id'] != 'manual' && $_POST['venue_id'] != 'client') { $event_data['_mdjm_event_venue_id'] = $_POST['venue_id']; } elseif (!empty($_POST['_mdjm_event_venue_id']) && $_POST['_mdjm_event_venue_id'] == 'client') { $event_data['_mdjm_event_venue_id'] = 'client'; } else { $event_data['_mdjm_event_venue_id'] = 'manual'; } /** * If the option was selected to save the venue, prepare the post and post meta data * for the venue. */ if ($_POST['venue_id'] == 'manual' && !empty($_POST['save_venue'])) { foreach ($_POST as $venue_key => $venue_value) { if (substr($venue_key, 0, 6) == 'venue_') { $venue_meta[$venue_key] = $venue_value; if ($venue_key == 'venue_postcode' && !empty($venue_value)) { $venue_meta[$venue_key] = strtoupper($venue_value); } elseif ($venue_key == 'venue_email' && !empty($venue_value)) { $venue_meta[$venue_key] = sanitize_email($venue_value); } else { $venue_meta[$venue_key] = sanitize_text_field(ucwords($venue_value)); } } } // Create the new venue $event_data['_mdjm_event_venue_id'] = mdjm_add_venue($_POST['venue_name'], $venue_meta); } else { // Manual venue address entry if ($_POST['venue_id'] != 'client') { $event_data['_mdjm_event_venue_name'] = sanitize_text_field(ucwords($_POST['venue_name'])); $event_data['_mdjm_event_venue_contact'] = sanitize_text_field(ucwords($_POST['venue_contact'])); $event_data['_mdjm_event_venue_phone'] = sanitize_text_field($_POST['venue_phone']); $event_data['_mdjm_event_venue_email'] = sanitize_email(strtolower($_POST['venue_email'])); $event_data['_mdjm_event_venue_address1'] = sanitize_text_field(ucwords($_POST['venue_address1'])); $event_data['_mdjm_event_venue_address2'] = sanitize_text_field(ucwords($_POST['venue_address2'])); $event_data['_mdjm_event_venue_town'] = sanitize_text_field(ucwords($_POST['venue_town'])); $event_data['_mdjm_event_venue_county'] = sanitize_text_field(ucwords($_POST['venue_county'])); $event_data['_mdjm_event_venue_postcode'] = strtoupper(sanitize_text_field($_POST['venue_postcode'])); } else { // Using clients address $client_data = get_userdata($event_data['_mdjm_event_client']); $event_data['_mdjm_event_venue_name'] = __('Client Address', 'mobile-dj-manager'); $event_data['_mdjm_event_venue_contact'] = sprintf('%s %s', !empty($client_data->first_name) ? sanitize_text_field($client_data->first_name) : '', !empty($client_data->last_name) ? sanitize_text_field($client_data->last_name) : ''); $event_data['_mdjm_event_venue_phone'] = !empty($client_data->phone1) ? $client_data->phone1 : ''; $event_data['_mdjm_event_venue_email'] = !empty($client_data->user_email) ? $client_data->user_email : ''; $event_data['_mdjm_event_venue_address1'] = !empty($client_data->address1) ? $client_data->address1 : ''; $event_data['_mdjm_event_venue_address2'] = !empty($client_data->address2) ? $client_data->address2 : ''; $event_data['_mdjm_event_venue_town'] = !empty($client_data->town) ? $client_data->town : ''; $event_data['_mdjm_event_venue_county'] = !empty($client_data->county) ? $client_data->county : ''; $event_data['_mdjm_event_venue_postcode'] = !empty($client_data->postcode) ? $client_data->postcode : ''; } } /** * Travel data */ $travel_fields = mdjm_get_event_travel_fields(); foreach ($travel_fields as $travel_field) { $field = 'travel_' . $travel_field; $travel_data[$travel_field] = !empty($_POST[$field]) ? $_POST[$field] : ''; if ('cost' == $travel_field && !empty($_POST[$field])) { $travel_data[$travel_field] = mdjm_sanitize_amount($_POST[$field]); } } $event_data['_mdjm_event_travel_data'] = $travel_data; /** * Prepare the remaining event meta data. */ $event_data['_mdjm_event_last_updated_by'] = get_current_user_id(); /** * Event name. * If no name is defined, use the event type. * Allow filtering of the event name with the `mdjm_event_name` filter. */ if (empty($_POST['_mdjm_event_name'])) { $_POST['_mdjm_event_name'] = get_term($_POST['mdjm_event_type'], 'event-types')->name; } $_POST['_mdjm_event_name'] = apply_filters('mdjm_event_name', $_POST['_mdjm_event_name'], $post_id); // Generate the playlist reference for guest access if (empty($update) || empty($current_meta['_mdjm_event_playlist_access'][0])) { $event_data['_mdjm_event_playlist_access'] = mdjm_generate_playlist_guest_code(); } // Set whether or not the playlist is enabled for the event $event_data['_mdjm_event_playlist'] = !empty($_POST['enable_playlist']) ? $_POST['enable_playlist'] : 'N'; /** * All the remaining custom meta fields are prefixed with '_mdjm_event_'. * Loop through all $_POST data and put all event meta fields into the $event_data array */ foreach ($_POST as $key => $value) { if (substr($key, 0, 12) == '_mdjm_event_') { if ($key == '_mdjm_event_dj_wage' || $key == '_mdjm_event_cost' || $key == '_mdjm_event_deposit') { $value = mdjm_sanitize_amount($value); } $event_data[$key] = $value; } } /** * We store all times in H:i:s but the user may prefer a different format so we * determine their time format setting and adjust to H:i:s for saving. */ if (mdjm_get_option('time_format', 'H:i') == 'H:i') { // 24 Hr $event_data['_mdjm_event_start'] = date('H:i:s', strtotime($_POST['event_start_hr'] . ':' . $_POST['event_start_min'])); $event_data['_mdjm_event_finish'] = date('H:i:s', strtotime($_POST['event_finish_hr'] . ':' . $_POST['event_finish_min'])); $event_data['_mdjm_event_djsetup_time'] = date('H:i:s', strtotime($_POST['dj_setup_hr'] . ':' . $_POST['dj_setup_min'])); } else { // 12 hr $event_data['_mdjm_event_start'] = date('H:i:s', strtotime($_POST['event_start_hr'] . ':' . $_POST['event_start_min'] . $_POST['event_start_period'])); $event_data['_mdjm_event_finish'] = date('H:i:s', strtotime($_POST['event_finish_hr'] . ':' . $_POST['event_finish_min'] . $_POST['event_finish_period'])); $event_data['_mdjm_event_djsetup_time'] = date('H:i:s', strtotime($_POST['dj_setup_hr'] . ':' . $_POST['dj_setup_min'] . $_POST['dj_setup_period'])); } if (empty($_POST['_mdjm_event_djsetup'])) { $event_data['_mdjm_event_djsetup'] = $_POST['_mdjm_event_date']; } /** * Set the event end date. * If the finish time is less than the start time, assume following day. */ if (date('H', strtotime($event_data['_mdjm_event_finish'])) > date('H', strtotime($event_data['_mdjm_event_start']))) { $event_data['_mdjm_event_end_date'] = $_POST['_mdjm_event_date']; } else { // End date is following day $event_data['_mdjm_event_end_date'] = date('Y-m-d', strtotime('+1 day', strtotime($_POST['_mdjm_event_date']))); } /** * Determine the state of the Deposit & Balance payments. * */ $event_data['_mdjm_event_deposit_status'] = !empty($_POST['deposit_paid']) ? $_POST['deposit_paid'] : 'Due'; $event_data['_mdjm_event_balance_status'] = !empty($_POST['balance_paid']) ? $_POST['balance_paid'] : 'Due'; $deposit_payment = $event_data['_mdjm_event_deposit_status'] == 'Paid' && $current_meta['_mdjm_event_deposit_status'][0] != 'Paid' ? true : false; $balance_payment = $event_data['_mdjm_event_balance_status'] == 'Paid' && $current_meta['_mdjm_event_balance_status'][0] != 'Paid' ? true : false; // Add-Ons if (mdjm_packages_enabled()) { $event_data['_mdjm_event_addons'] = !empty($_POST['event_addons']) ? $_POST['event_addons'] : ''; } // Assign the event type $existing_event_type = wp_get_object_terms($post_id, 'event-types'); mdjm_set_event_type($post_id, (int) $_POST['mdjm_event_type']); // Assign the enquiry source mdjm_set_enquiry_source($post_id, (int) $_POST['mdjm_enquiry_source']); /** * Update the event post meta data */ $debug[] = 'Beginning Meta Updates'; mdjm_update_event_meta($post_id, $event_data); $debug[] = 'Meta Updates Completed'; if ($deposit_payment == true || $balance_payment == true) { if ($balance_payment == true) { unset($event_data['_mdjm_event_balance_status']); unset($event_data['_mdjm_event_deposit_status']); mdjm_mark_event_balance_paid($post_id); } else { unset($event_data['_mdjm_event_deposit_status']); mdjm_mark_event_deposit_paid($post_id); } } // Set the event status & initiate tasks based on the status if ($_POST['original_post_status'] != $_POST['mdjm_event_status']) { mdjm_update_event_status($post_id, $_POST['mdjm_event_status'], $_POST['original_post_status'], array('client_notices' => empty($_POST['mdjm_block_emails']) ? true : false, 'email_template' => !empty($_POST['mdjm_email_template']) ? $_POST['mdjm_email_template'] : false, 'quote_template' => !empty($_POST['mdjm_online_quote']) ? $_POST['mdjm_online_quote'] : false)); } else { // Event status is un-changed so just log the changes to the journal mdjm_add_journal(array('user_id' => get_current_user_id(), 'event_id' => $post_id, 'comment_content' => sprintf('%s %s via Admin', mdjm_get_label_singular(), empty($update) ? 'created' : 'updated')), array('type' => 'update-event', 'visibility' => '2')); } // Fire the save event hook do_action('mdjm_save_event', $post, $_POST['mdjm_event_status']); // Fire our post save hook do_action('mdjm_after_event_save', $post_id, $post, $update); // Re-add the save post action to avoid loops add_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3); $debug[] = sprintf('Completed Event Save for event %s', $post_id); if (!empty($debug) && MDJM_DEBUG == true) { $true = true; foreach ($debug as $log) { MDJM()->debug->log_it($log, $true); $true = false; } } }