/** * 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; }
/** * Update a venue's cached gig count when gig is deleted. * * Determines if a venue's gig_count meta field needs to be updated * when a gig is deleted. * * @since 1.0.0 * * @param int $post_id ID of the gig being deleted. */ function audiotheme_gig_before_delete($post_id) { if ('audiotheme_gig' === get_post_type($post_id)) { $gig = get_audiotheme_gig($post_id); if (isset($gig->venue->ID)) { $count = get_audiotheme_venue_gig_count($gig->venue->ID); update_audiotheme_venue_gig_count($gig->venue->ID, --$count); } } }