/** * 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); }
/** * 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; } } }