function pugpig_validate_post($post_id)
{
    global $SKIP_EDITION_VALIDATION;
    if ($SKIP_EDITION_VALIDATION) {
        return;
    }
    $post = get_post($post_id);
    if (!isset($post)) {
        return;
    }
    if ($post->post_type == "revision") {
        return;
    }
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || $post->post_status == 'auto-draft') {
        return;
    }
    // Validate an edition publish
    if ($post->post_type == PUGPIG_EDITION_POST_TYPE && (isset($_POST['publish']) || isset($_POST['save'])) && $_POST['post_status'] == 'publish') {
        pugpig_add_debug_notice("Validating " . $post_id . " (" . $post->post_type . ") -> status: " . $post->post_status, "error");
        // init completion marker (add more as needed)
        $publish_errors = array();
        // retrieve meta to be validated
        $meta_edition_key = get_post_meta($post_id, 'edition_key', true);
        if (empty($meta_edition_key)) {
            array_push($publish_errors, "Edition Key must be supplied.");
        } else {
            if (preg_match('|' . '^[0-9a-zA-Z.,-_]+$' . '|', $meta_edition_key, $matches) === 0) {
                array_push($publish_errors, "Edition Key may only contain letters (a-Z), digits (0-9), fullstop (.), comma (,), hyphen (-) and underscore(_).");
            } else {
                if (preg_match('|' . '^[0-9]+$' . '|', $meta_edition_key, $matches) === 1) {
                    set_transient('edition_key_count', (int) $meta_edition_key);
                }
            }
        }
        $meta_edition_date = get_post_meta($post_id, 'edition_date', true);
        if (empty($meta_edition_date)) {
            array_push($publish_errors, $post->post_title . ": Edition date must be supplied.");
        } else {
            if (!pugpig_check_date_format($meta_edition_date)) {
                array_push($publish_errors, $post->post_title . ": Edition date " . $meta_edition_date . " is not valid.");
            }
        }
        $taxonomy_name = pugpig_get_taxonomy_name();
        if (pugpig_should_auto_tag_edition() && !empty($taxonomy_name) && taxonomy_exists($taxonomy_name)) {
            wp_set_object_terms($post_id, $post->post_name, $taxonomy_name, true);
        } else {
            if (pugpig_should_auto_tag_edition()) {
                array_push($publish_errors, "Cannot automatically tag edition as the taxonomy does not exist");
            }
        }
        $is_pdf_edition = false;
        $pdf = get_attached_media('application/pdf', $post->ID);
        if (count($pdf) > 0) {
            $media_id = max(array_keys($pdf));
            $pdf = $pdf[$media_id];
            $is_pdf_edition = true;
        }
        $ecr = pugpig_get_edition_array(get_post_custom($post->ID));
        if (count($ecr) == 0 && !$is_pdf_edition) {
            array_push($publish_errors, "You cannot publish an empty edition.");
        }
        // on attempting to publish - check for completion and intervene if necessary
        //  don't allow publishing while any of these are incomplete
        $wordpress_title = get_the_title($post->ID);
        if (count($publish_errors) > 0) {
            if (!empty($wordpress_title)) {
                array_push($publish_errors, "<b>Please fix these errors before publishing this edition. The post status for edition '" . $wordpress_title . "' has been set to 'Pending'.</b>");
            } else {
                array_push($publish_errors, "<b>Please fix these errors before publishing this edition. The post status for this edition has been set to 'Pending'.</b>");
            }
            global $wpdb;
            $wpdb->update($wpdb->posts, array('post_status' => 'pending'), array('ID' => $post_id));
            foreach ($publish_errors as $e) {
                pugpig_add_admin_notice($e, "error");
            }
            // filter the query URL to change the published message
            add_filter('redirect_post_location', create_function('$location', 'return add_query_arg("message", "4", $location);'));
            return;
        }
        // Rebuild the edition manifests
        // pugpig_build_edition_manifests($post);
        // Increase the count of items needed for push notifications
        if ($post->post_status == "publish") {
            global $pugpig_edition_changed;
            $pugpig_edition_changed++;
            pugpig_add_debug_notice("Published edition changed: " . $post->ID);
        }
    }
}
function pugpig_get_edition($id, $include_hidden = true, $use_package = true, $search_term = null)
{
    $edition = get_post($id);
    $attachment_id = get_post_meta($edition->ID, '_thumbnail_id', true);
    $thumbnail = BASE_URL . "common/images/nocover.jpg";
    if (!empty($attachment_id)) {
        $thumbnail = wp_get_attachment_url($attachment_id);
    }
    $thumbnail = pugpig_strip_domain($thumbnail);
    // Use the CDN in the feed for the cover if the edition is published
    if ($edition->post_status == 'publish') {
        $cdn = get_option('pugpig_opt_cdn_domain');
        if (!empty($cdn)) {
            $thumbnail = $cdn . $thumbnail;
        }
    }
    $region = isset($_GET["region"]) ? $_GET["region"] : "";
    $url = pugpig_strip_domain(pugpig_get_edition_atom_url($edition, true, $region));
    $url_type = 'application/atom+xml';
    $packaged_timestamp = '';
    if ($use_package) {
        $url_type = 'application/pugpigpkg+xml';
        // TODO: Check if something exists.
        // If yes, use pugpig_get_package_manifest_url
        $packaged_timestamp = get_edition_package_timestamp($edition->ID);
        $url = get_edition_package_url($edition->ID);
        if ($url != '') {
            $packaged_timestamp = get_edition_package_timestamp($edition->ID);
            $url = pugpig_strip_domain($url);
        }
    }
    $custom = get_post_custom($edition->ID);
    $price = "FREE";
    if (!isset($custom["edition_free"])) {
        $price = "PAID";
    }
    $has_samples = isset($custom['edition_samples']);
    $deleted = false;
    if (isset($custom["edition_deleted"])) {
        $deleted = true;
    }
    $pdf_attachement_url = '';
    $pdf_modified = '';
    $is_pdf_edition = false;
    $pdf = get_attached_media('application/pdf', $edition->ID);
    if (count($pdf) > 0) {
        $media_id = max(array_keys($pdf));
        $pdf = $pdf[$media_id];
        $pdf_attachement_url = pugpig_strip_domain($pdf->guid);
        $pdf_modified = strtotime($pdf->post_modified);
        $is_pdf_edition = true;
    }
    $newsstand_cover_attachment_url = "";
    $newsstand_cover_attachment_id = get_post_meta($edition->ID, 'pugpigmb_newsstand_cover', true);
    if (!empty($newsstand_cover_attachment_id)) {
        $newsstand_cover_attachment_url = wp_get_attachment_url($newsstand_cover_attachment_id);
    }
    $custom_categories = pugpig_get_edition_opds_custom_categories($edition, $use_package);
    $page_id_array = pugpig_get_edition_array(get_post_custom($edition->ID));
    // If we have a search term, filter further
    if ($search_term) {
        $search_result_ids = array();
        $args = array('post__in' => $page_id_array, 's' => $search_term);
        $wp_query = new WP_Query($args);
        while ($wp_query->have_posts()) {
            $wp_query->the_post();
            global $post;
            $search_result_ids[] = $post->ID;
        }
        $page_id_array = $search_result_ids;
    }
    $item = array('id' => $edition->ID, 'title' => $edition->post_title, 'key' => pugpig_get_full_edition_key($edition), 'summary' => $edition->post_excerpt, 'is_pdf' => $is_pdf_edition, 'pdf_url' => $pdf_attachement_url, 'pdf_modified' => $pdf_modified, 'newsstand_summary' => get_post_meta($edition->ID, 'pugpigmb_newsstand_long_desc', true), 'newsstand_cover_art_icon_source' => $newsstand_cover_attachment_url, 'page_ids' => $page_id_array, 'author' => get_post_meta($edition->ID, 'edition_author', true), 'price' => $price, 'has_samples' => $has_samples, 'date' => get_post_meta($edition->ID, 'edition_date', true), 'status' => $edition->post_status == 'publish' ? 'published' : $edition->post_status, 'modified' => pugpig_get_page_modified($edition), 'thumbnail' => $thumbnail, 'url' => $url, 'url_type' => $url_type, 'sharing_link' => get_post_meta($edition->ID, 'edition_sharing_link', true), 'zip' => $url, 'packaged' => $packaged_timestamp, 'custom_categories' => $custom_categories, 'links' => pugpig_get_edition_opds_links($edition), 'tombstone' => $deleted);
    return $item;
}