/** * Display the venue add/edit screen. * * @since 1.0.0 */ function audiotheme_venue_edit_screen() { $screen = get_current_screen(); $post_type_object = get_post_type_object('audiotheme_venue'); $action = 'add'; $nonce_field = wp_nonce_field('add-venue', 'audiotheme_venue_nonce', true, false); $values = get_default_audiotheme_venue_properties(); if (isset($_GET['action']) && 'edit' === $_GET['action'] && isset($_GET['venue_id']) && is_numeric($_GET['venue_id'])) { $venue_to_edit = get_audiotheme_venue($_GET['venue_id']); $action = 'edit'; $nonce_field = wp_nonce_field('update-venue_' . $venue_to_edit->ID, 'audiotheme_venue_nonce', true, false); $values = wp_parse_args(get_object_vars($venue_to_edit), $values); } extract($values, EXTR_SKIP); require AUDIOTHEME_DIR . 'modules/gigs/admin/views/edit-venue.php'; }
/** * Save a venue. * * Accepts an array of properties, whitelists them and then saves. Will update values if the ID isn't 0. * Sets all post meta fields upon initial save, even if empty. * * @since 1.0.0 */ function save_audiotheme_venue($data) { global $wpdb; $action = 'update'; $current_user = wp_get_current_user(); $defaults = get_default_audiotheme_venue_properties(); // New venue. if (empty($data['ID'])) { $action = 'insert'; $data = wp_parse_args($data, $defaults); } else { $current_venue = get_audiotheme_venue($data['ID']); } // Copy gig count before cleaning the data array. $gig_count = isset($data['gig_count']) && is_numeric($data['gig_count']) ? absint($data['gig_count']) : 0; // Remove properties that aren't whitelisted. $data = array_intersect_key($data, $defaults); // Map the 'name' property to the 'post_title' field. if (isset($data['name']) && !empty($data['name'])) { $post_title = get_unique_audiotheme_venue_name($data['name'], $data['ID']); if (!isset($current_venue) || $post_title !== $current_venue->name) { $venue['post_title'] = $post_title; $venue['post_name'] = ''; } } // Insert the post container. if ('insert' === $action) { $venue['post_author'] = $current_user->ID; $venue['post_status'] = 'publish'; $venue['post_type'] = 'audiotheme_venue'; $venue_id = wp_insert_post($venue); } else { $venue_id = absint($data['ID']); $venue['ID'] = $venue_id; wp_update_post($venue); } // Set the venue title as the venue ID if the name argument was empty. if (isset($data['name']) && empty($data['name'])) { wp_update_post(array('ID' => $venue_id, 'post_title' => get_unique_audiotheme_venue_name($venue_id, $venue_id), 'post_name' => '')); } // Save additional properties to post meta. if ($venue_id) { unset($data['ID']); unset($data['name']); foreach ($data as $key => $val) { $key = '_audiotheme_' . $key; update_post_meta($venue_id, $key, $val); } // Update gig count. update_audiotheme_venue_gig_count($venue_id, $gig_count); return $venue_id; } return false; }