Пример #1
0
/**
 * Press It form handler.
 *
 * @package WordPress
 * @subpackage Press_This
 * @since 2.6.0
 *
 * @return int Post ID
 */
function nominate_it()
{
    $post = get_default_post_to_edit();
    $post = get_object_vars($post);
    $post_ID = $post['ID'] = (int) $_POST['post_id'];
    if (!current_user_can('edit_post', $post_ID)) {
        wp_die(__('You are not allowed to edit this post.'));
    }
    $post['post_category'] = isset($_POST['post_category']) ? $_POST['post_category'] : '';
    $post['tax_input'] = isset($_POST['tax_input']) ? $_POST['tax_input'] : '';
    $post['post_title'] = isset($_POST['title']) ? $_POST['title'] : '';
    $content = isset($_POST['content']) ? $_POST['content'] : '';
    $upload = false;
    if (!empty($_POST['photo_src']) && current_user_can('upload_files')) {
        foreach ((array) $_POST['photo_src'] as $key => $image) {
            // see if files exist in content - we don't want to upload non-used selected files.
            if (strpos($_POST['content'], htmlspecialchars($image)) !== false) {
                $desc = isset($_POST['photo_description'][$key]) ? $_POST['photo_description'][$key] : '';
                $upload = media_sideload_image($image, $post_ID, $desc);
                // Replace the POSTED content <img> with correct uploaded ones. Regex contains fix for Magic Quotes
                if (!is_wp_error($upload)) {
                    $content = preg_replace('/<img ([^>]*)src=\\\\?(\\"|\')' . preg_quote(htmlspecialchars($image), '/') . '\\\\?(\\2)([^>\\/]*)\\/*>/is', $upload, $content);
                }
            }
        }
    }
    // set the post_content and status
    $post['post_content'] = $content;
    if (isset($_POST['publish']) && current_user_can('publish_posts')) {
        $post['post_status'] = 'publish';
    } elseif (isset($_POST['review'])) {
        $post['post_status'] = 'pending';
    } else {
        $post['post_status'] = 'draft';
    }
    $nom_check = false;
    // error handling for media_sideload
    if (is_wp_error($upload)) {
        wp_delete_post($post_ID);
        wp_die($upload);
        $nom_check = true;
    } else {
        // Post formats
        if (isset($_POST['post_format'])) {
            if (current_theme_supports('post-formats', $_POST['post_format'])) {
                set_post_format($post_ID, $_POST['post_format']);
            } elseif ('0' == $_POST['post_format']) {
                set_post_format($post_ID, false);
            }
        }
        # PF NOTE: Switching post type to nomination.
        $post['post_type'] = 'nomination';
        $post['post_date_gmt'] = gmdate('Y-m-d H:i:s');
        # PF NOTE: This is where the inital post is created.
        # PF NOTE: Put get_post_nomination_status here.
        $item_id = md5($_POST['nomination_permalink'] . $post['post_title']);
        if (!isset($_POST['item_date'])) {
            $newDate = gmdate('Y-m-d H:i:s');
            $item_date = $newDate;
        } else {
            $item_date = $_POST['item_date'];
        }
        $pf_nomination = new PF_Nominations();
        $nom_check = $pf_nomination->get_post_nomination_status($item_date, $item_id, 'nomination');
        if (!$nom_check) {
            $post_ID = wp_update_post($post);
        }
    }
    # var_dump($_POST); die();
    if (!$nom_check) {
        $already_has_thumb = has_post_thumbnail($post_ID);
        if ($already_has_thumb) {
            $post_thumbnail_id = get_post_thumbnail_id($post_ID);
            $post_thumbnail_url = wp_get_attachment_image_src($attachment_id);
        } else {
            $post_thumbnail_url = false;
        }
        $pf_meta_args = array(pf_meta_for_entry('item_id', $post_ID), pf_meta_for_entry('origin_item_ID', $item_id), pf_meta_for_entry('nomination_permalink', $_POST['nomination_permalink']), pf_meta_for_entry('nomination_count', 1), pf_meta_for_entry('source_title', 'Bookmarklet'), pf_meta_for_entry('item_date', $item_date), pf_meta_for_entry('posted_date', $item_date), pf_meta_for_entry('date_nominated', $_POST['date_nominated']), pf_meta_for_entry('item_author', $_POST['authors']), pf_meta_for_entry('authors', $_POST['authors']), pf_meta_for_entry('item_link', $_POST['nomination_permalink']), pf_meta_for_entry('item_feat_img', $post_thumbnail_url), pf_meta_for_entry('nominator_array', array(get_current_user_id())), pf_meta_for_entry('item_wp_date', $item_date), pf_meta_for_entry('sortable_item_date', strtotime($item_date)), pf_meta_for_entry('item_tags', 'via bookmarklet'), pf_meta_for_entry('source_repeat', 1), pf_meta_for_entry('revertible_feed_text', $post['post_content']));
        pf_meta_establish_post($post_ID, $pf_meta_args);
    }
    if (isset($_POST['publish']) && $_POST['publish'] == "Send to Draft") {
        $post_check = $pf_nomination->get_post_nomination_status($item_date, $item_id, 'post', false);
        if ($post_check != true) {
            add_post_meta($post_ID, 'nom_id', $post_ID, true);
            $d_post = $post;
            $d_post['post_type'] = 'post';
            $d_post['post_status'] = 'draft';
            $newPostID = wp_insert_post($d_post, true);
            #var_dump($newPostID); die();
            #pf_meta_transition_post($post_ID, $newPostID);
            $already_has_thumb = has_post_thumbnail($post_ID);
            if ($already_has_thumb) {
                $post_thumbnail_id = get_post_thumbnail_id($post_ID);
                set_post_thumbnail($newPostID, $post_thumbnail_id);
            }
            pf_meta_transition_post($post_ID, $newPostID);
        }
    }
    #var_dump($post); die();
    return $post_ID;
}
Пример #2
0
 function build_nom_draft()
 {
     global $post;
     // verify if this is an auto save routine.
     // If it is our form has not been submitted, so we dont want to do anything
     //if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
     $pf_drafted_nonce = $_POST['pf_drafted_nonce'];
     if (!wp_verify_nonce($pf_drafted_nonce, 'drafter')) {
         die($this->__('Nonce not recieved. Are you sure you should be drafting?', 'pf'));
     } else {
         ##Check
         # print_r(__('Sending to Draft.', 'pf'));
         ##Check
         //print_r($_POST);
         ob_start();
         $item_content = $_POST['nom_content'];
         $item_content = htmlspecialchars_decode($item_content);
         #$args_fi['url'] = $_POST['item_link'];
         #$posts = $pf->pf_feed_items->get($args_fi);
         $linked = get_option('pf_link_to_source', 0);
         if ($linked < 1) {
             $item_content = $item_content . $this->get_the_source_statement($_POST['nom_id']);
         }
         $item_title = $_POST['nom_title'];
         $data = array('post_status' => 'draft', 'post_type' => 'post', 'post_title' => $item_title, 'post_content' => $item_content);
         //Will need to use a meta field to pass the content's md5 id around to check if it has already been posted.
         //We assume that it is already in nominations, so no need to check there. This might be why we can't use post_exists here.
         //No need to origonate the check at the time of the feed item either. It can't become a post with the proper meta if it wasn't a nomination first.
         $item_id = $_POST['item_id'];
         //YYYY-MM-DD
         $nom_date = strtotime($_POST['nom_date']);
         $nom_date = date('Y-m-d', $nom_date);
         //Now function will not update nomination count when it pushes nomination to publication.
         $post_check = $this->get_post_nomination_status($nom_date, $item_id, 'post', false);
         $newPostID = 'repeat';
         #
         # Check if the item was rendered readable, if not, make it so.
         $readable_state = get_post_meta($_POST['nom_id'], 'readable_status', true);
         if ($readable_state != 1) {
             $readArgs = array('force' => false, 'descrip' => htmlspecialchars_decode($item_content), 'url' => $_POST['item_link'], 'authorship' => $_POST['item_author']);
             $readReady = pressforward()->readability->get_readable_text($readArgs);
             #var_dump($readReady); die();
             $data['post_content'] = $readReady['readable'];
         }
         #
         //Alternative check with post_exists? or use same as above?
         if ($post_check != true) {
             ##Check
             #var_dump($data); die();
             //print_r('No Post exists.');
             $newPostID = wp_insert_post($data, true);
             ##Check
             add_post_meta($_POST['nom_id'], 'nom_id', $_POST['nom_id'], true);
             pf_meta_transition_post($_POST['nom_id'], $newPostID);
             $already_has_thumb = has_post_thumbnail($_POST['nom_id']);
             if ($already_has_thumb) {
                 $post_thumbnail_id = get_post_thumbnail_id($_POST['nom_id']);
                 set_post_thumbnail($newPostID, $post_thumbnail_id);
             }
         }
         $response = array('what' => 'draft', 'action' => 'build_nom_draft', 'id' => $newPostID, 'data' => $data['post_content'] . ' drafted.', 'supplemental' => array('content' => $item_content, 'originID' => $item_id, 'repeat' => $post_check, 'buffered' => ob_get_contents()));
         $xmlResponse = new WP_Ajax_Response($response);
         $xmlResponse->send();
         ob_end_flush();
         die;
     }
 }