function pugpig_send_push_notification($message, $with_download)
{
    global $push_notification_count;
    if ($push_notification_count) {
        return;
    }
    $push_notification_count = true;
    if (!pugpig_should_send_push()) {
        pugpig_add_debug_notice("Push notifications not enabled");
        return;
    }
    $invalid_request = true;
    $description = '';
    if (empty($message)) {
        if ($with_download) {
            $description = "Sending Newsstand Push with no Message...";
            $invalid_request = false;
        } else {
            $description = "Error: no message or download.";
            $invalid_request = true;
        }
    } else {
        $invalid_request = false;
        if ($with_download) {
            $description = "Sending Newsstand Push with Message ({$message})...";
        } else {
            $description = "Sending Message only ({$message})...";
        }
    }
    $ret = "<div>{$description}</div>";
    if ($invalid_request) {
        return $ret;
    }
    $key = get_option("pugpig_opt_urbanairship_key");
    $secret = get_option("pugpig_opt_urbanairship_secret");
    $report = '';
    if (!empty($key) && !empty($secret)) {
        $proxy_server = '';
        $proxy_port = '';
        $report = pugpig_send_urban_airship_push($key, $secret, $num = 1, $message, $with_download, $proxy_server, $proxy_port);
    } else {
        $link = _pugpig_push_notification_get_settings_link();
        $report = "Could not send push notification. Urban Airship key/secret not set in settings area. See {$link}.";
    }
    $ret .= "<div>{$report}</div>";
    return $ret;
}
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);
        }
    }
}