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_ad_bundle_html_file($unzipped_path)
{
    // Get the index path and ensure it exists
    $index = '';
    //pugpig_value($node, 'field_ad_index');
    $orig_index = $index;
    if ($index != '' && !file_exists($unzipped_path . '/' . $index)) {
        pugpig_add_admin_notice('Ad Packager: Deleting bad index path: ' . $index, "error");
        $index = '';
    }
    if ($index != '' && !is_file($unzipped_path . '/' . $index)) {
        pugpig_add_admin_notice('Ad Packager: Path should point to a file. Deleting directory path: ' . $index, "error");
        $index = '';
    }
    if ($index == '') {
        $index = _pugpig_get_first_file($unzipped_path, array('htm', 'html'));
        $slashpos = strrpos($index, "/");
        if (strpos($index, " ", $slashpos) != FALSE) {
            pugpig_add_admin_notice("{$index} <br /><strong>Error:</strong> html filename cannot contain spaces: {$index}", "error");
        }
        if ($index != '') {
            $index = substr($index, 0, 1) == '/' ? $index : '/' . $index;
            pugpig_add_admin_notice('Ad Packager: Found index: ' . $index);
        }
    }
    if ($index == '') {
        pugpig_add_admin_notice('Ad Packager: [' . $unzipped_path . '] Could not find any index HTML file', "error");
        //watchdog('Pugpig: Unpacking Ad', 'Error: Could not find any index HTML file in ' . $zip_name, array(), WATCHDOG_WARNING, 'admin/reports/dblog/pugpig');
        $_unzip_failures[] = $unzipped_path;
        _package_rmdir($unzipped_path);
        return;
    }
    // Save the new value of  the index if it has changed
    if ($index != $orig_index) {
        $node->field_ad_index['und'] = array(array('value' => $index));
        //$node->field_ad_manifest['und'] = array(array('value' => (substr($manifest, 0, 1) == '/' ? $manifest : '/' . $manifest)));
    }
    return $index;
}
    // global $wp_rewrite;
    // $wp_rewrite->flush_rules();
}
// deactivating
function pugpig_deactivate()
{
    update_option('plugin_error', "");
    // pugpig_delete_directory(PUGPIG_MANIFESTPATH);
}
// uninstalling
function pugpig_uninstall()
{
    pugpig_deactivate();
}
/************************************************************************
Debug on activate
Keeps the last activation error in a setting and echo it back
*************************************************************************/
add_action('activated_plugin', 'save_error');
function save_error()
{
    update_option('plugin_error', ob_get_contents());
}
$_pp_err = get_option('plugin_error');
if (!empty($_pp_err)) {
    pugpig_add_admin_notice("Pugpig Core Activation Error: " . $_pp_err, "error");
    update_option('plugin_error', "");
}
if (isset($wp_ud_arr['error']) && !empty($wp_ud_arr['error'])) {
    pugpig_add_admin_notice("Error creating Pugpig area: " . $wp_ud_arr['error'], "error");
}