Example #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;
}
Example #2
0
 public static function assemble_feed_for_pull($feedObj = 0)
 {
     pf_log('Invoked: PF_Feed_Item::assemble_feed_for_pull()');
     ignore_user_abort(true);
     set_time_limit(0);
     # Chunking control, the goal here is to ensure that no feed assembly occurs while the feed assembly is already occuring.
     # Option: If true (1), the system is ready to assemble a chunk. If false (0), the system is already assembling a chunk.
     $ready_for_chunk_assembly = get_option(PF_SLUG . '_ready_to_chunk', 1);
     if ($ready_for_chunk_assembly === 0) {
         pf_log('Chunk already in progress.');
         return;
     } else {
         pf_log('Beginning next import chunk.');
         pf_log('The chunk state is set?');
         $chunk_state = update_option(PF_SLUG . '_ready_to_chunk', 0);
         pf_log($chunk_state);
     }
     if ($feedObj == 0) {
         $theFeed = self::get_the_feed_object();
         $feedObj = $theFeed;
     }
     # We need to init $sourceRepeat so it can be if 0 if nothing is happening.
     $sourceRepeat = 0;
     # We'll need this for our fancy query.
     global $wpdb;
     # Since rss_object places all the feed items into an array of arrays whose structure is standardized throughout,
     # We can do stuff with it, using the same structure of items as we do everywhere else.
     pf_log('Now beginning check and processing for entering items into the database.');
     $parent = $feedObj['parent_feed_id'];
     unset($feedObj['parent_feed_id']);
     foreach ($feedObj as $item) {
         $thepostscheck = 0;
         $thePostsDoubleCheck = 0;
         $item_id = $item['item_id'];
         $sourceRepeat = 0;
         //$queryForCheck = new WP_Query( array( 'post_type' => 'rssarchival', 'meta_key' => 'item_id', 'meta_value' => $item_id ) );
         # Originally this query tried to get every archive post earlier than 'now' to check.
         # But it occured to me that, since I'm doing a custom query anyway, I could just query for items with the ID I want.
         # Less query results, less time.
         //Perhaps I should do this outside of the foreach? One query and search it for each item_id and then return those not in?
         $querystr = $wpdb->prepare("\n\t\t\t\tSELECT {$wpdb->posts}.*, {$wpdb->postmeta}.*\n\t\t\t\tFROM {$wpdb->posts}, {$wpdb->postmeta}\n\t\t\t\tWHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id\n\t\t\t\tAND {$wpdb->postmeta}.meta_key = 'item_id'\n\t\t\t\tAND {$wpdb->postmeta}.meta_value = %s\n\t\t\t\tAND {$wpdb->posts}.post_type = %s\n\t\t\t\tORDER BY {$wpdb->posts}.post_date DESC\n\t\t\t ", $item_id, pf_feed_item_post_type());
         // AND $wpdb->posts.post_date < NOW() <- perhaps by removing we can better prevent simultaneous duplications?
         # Since I've altered the query, I could change this to just see if there are any items in the query results
         # and check based on that. But I haven't yet.
         $checkposts = $wpdb->get_results($querystr, OBJECT);
         //print_r($checkposts);
         if ($checkposts) {
             global $post;
             foreach ($checkposts as $post) {
                 setup_postdata($post);
                 //print_r(get_the_ID());
                 //print_r('< the ID');
                 if (get_post_meta($post->ID, 'item_id', $item_id, true) === $item_id) {
                     $thepostscheck++;
                     pf_log('We already have post ' . $item_id);
                 }
             }
         }
         wp_reset_query();
         if ($thepostscheck === 0) {
             $queryMoreStr = $wpdb->prepare("\n\t\t\t\t\t\tSELECT {$wpdb->posts}.*, {$wpdb->postmeta}.*\n\t\t\t\t\t\tFROM {$wpdb->posts}, {$wpdb->postmeta}\n\t\t\t\t\t\tWHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id\n\t\t\t\t\t\tAND {$wpdb->postmeta}.meta_key = 'item_link'\n\t\t\t\t\t\tAND {$wpdb->postmeta}.meta_value = %s\n\t\t\t\t\t\tAND {$wpdb->posts}.post_type = %s\n\t\t\t\t\t\tORDER BY {$wpdb->posts}.post_date DESC\n\t\t\t\t\t ", $item['item_link'], pf_feed_item_post_type());
             $checkpoststwo = $wpdb->get_results($queryMoreStr, OBJECT);
             if ($checkpoststwo) {
                 pf_log('Check for posts with the same link.');
                 foreach ($checkpoststwo as $post) {
                     setup_postdata($post);
                     # Post comparative values.
                     $theTitle = $post->post_title;
                     $postID = $post->ID;
                     $postDate = strtotime($post->post_date);
                     $postItemLink = get_post_meta($post->ID, 'item_link', true);
                     # Item comparative values.
                     $itemDate = strtotime($item['item_date']);
                     $itemTitle = $item['item_title'];
                     $itemLink = $item['item_link'];
                     # First check if it more recent than the currently stored item.
                     if ($theTitle == $itemTitle || $postItemLink == $itemLink) {
                         $thePostsDoubleCheck++;
                         pf_log('We already have the post ' . $theTitle . ' with the link ' . $itemLink);
                         $sourceRepeat = get_post_meta($postID, 'source_repeat', true);
                         if ($itemDate > $postDate) {
                             # If it is more recent, than this is the new dominant post.
                             $sourceRepeat++;
                         } elseif ($itemDate <= $postDate) {
                             # if it is less recent, then we need to increment the source count.
                             $sourceRepeat++;
                             if ($thePostsDoubleCheck > $sourceRepeat) {
                                 update_post_meta($postID, 'source_repeat', $sourceRepeat);
                             }
                             $thepostscheck++;
                         } else {
                             $thepostscheck = 0;
                         }
                     } else {
                         # If it isn't duplicated at all, then we need to give it a source repeat count of 0
                         $sourceRepeat = 0;
                     }
                 }
             }
         }
         wp_reset_query();
         # Why an increment here instead of a bool?
         # If I start getting errors, I can use this to check how many times an item is in the database.
         # Potentially I could even use this to clean the database from duplicates that might occur if
         # someone were to hit the refresh button at the same time as another person.
         #			$fo = fopen(PF_ROOT . "/modules/rss-import/rss-import.txt", 'a') or print_r('Can\'t open log file.');
         #			if ($fo != false){
         #				fwrite($fo, "\nSending " . $item['item_title'] . " to post table.");
         #				fclose($fo);
         #			}
         if ($thepostscheck === 0) {
             $item_title = $item['item_title'];
             $item_content = $item['item_content'];
             $item_feat_img = $item['item_feat_img'];
             $source_title = $item['source_title'];
             $item_date = $item['item_date'];
             $item_author = $item['item_author'];
             $item_link = $item['item_link'];
             $item_wp_date = $item['item_wp_date'];
             $item_tags = $item['item_tags'];
             if (!isset($item['parent_feed_id']) || !$item['parent_feed_id']) {
                 $item['parent_feed_id'] = $parent;
             }
             $feed_obj_id = $item['parent_feed_id'];
             $source_repeat = $sourceRepeat;
             # Trying to prevent bad or malformed HTML from entering the database.
             $item_title = strip_tags($item_title);
             $item_content = strip_tags($item_content, '<p> <strong> <bold> <i> <em> <emphasis> <del> <h1> <h2> <h3> <h4> <h5> <a> <img>');
             # Need to get rid of some weird characters that prevent inserting posts into the database.
             # From: http://www.alexpoole.name/web-development/282/remove-smart-quotes-bullets-dashes-and-other-junky-characters-from-a-string-with-php
             # And: http://www.enghiong.com/wp_insert_post-could-not-insert-post-into-the-database.html
             //$item_content = self::extra_special_sanatize($item_content);
             //$item_title = self::extra_special_sanatize($item_title);
             //$item_content = wpautop($item_content);
             //$postcontent = sanitize_post($item_content);
             //If we use the @ to prevent showing errors, everything seems to work. But it is still dedicating crap to the database...
             //Perhaps sanitize_post isn't the cause? What is then?
             # Do we want or need the post_status to be published?
             $data = array('post_status' => 'publish', 'post_type' => pf_feed_item_post_type(), 'item_title' => $item_title, 'post_parent' => $feed_obj_id, 'item_content' => $item_content, 'item_link' => $item_link, 'source_title' => $source_title, 'item_wp_date' => $item_wp_date, 'item_tags' => $item_tags);
             //RIGHT HERE is where the content is getting assigned a bunch of screwed up tags.
             //The content is coming in from the rss_object assembler a-ok. But something here saves them to the database screwy.
             //It looks like sanitize post is screwing them up terribly. But what to do about it without removing the security measures which we need to apply?
             $worked = 1;
             # The post gets created here, the $newNomID variable contains the new post's ID.
             $newNomID = self::create($data);
             $post_inserted_bool = self::post_inserted($newNomID, $data);
             if (!$post_inserted_bool) {
                 # It's the end of the world! Let's throw everything at this.
                 pf_log('Post will not go into the database. We will try again.');
                 $item_content = htmlentities(strip_tags($item_content), ENT_QUOTES, "UTF-8");
                 $item_content = wp_kses(stripslashes($item_content), array('p', 'a', 'b', 'em', 'strong'));
                 $item_content = self::extra_special_sanatize($item_content, true);
                 $item_content = wpautop($item_content);
                 $item_title = self::extra_special_sanatize($item_title, true);
                 $data['item_content'] = $item_content;
                 $newNomID = self::create($data);
                 $post_inserted_bool = self::post_inserted($newNomID, $data);
             }
             pf_log('End of wp_insert_post process.');
             //$posttest = get_post($newNomID);
             //print_r($posttest->post_content);
             # Somewhere in the process links with complex queries at the end (joined by ampersands) are getting encoded.
             # I don't want that, so I turn it back here.
             # For some reason this is only happening to the ampersands, so that's the only thing I'm changing.
             $item_link = str_replace('&amp;', '&', $item_link);
             # If it doesn't have a featured image assigned already, I use the set_ext_as_featured function to try and find one.
             # It also, if it finds one, sets it as the featured image for that post.
             if (!empty($_POST['item_feat_img'])) {
                 # Turned off set_ext_as_featured here, as that should only occur when items are nominated.
                 # Before nominations, the featured image should remain a meta field with an external link.
                 if (false === ($itemFeatImg = get_transient('feed_img_' . $itemUID))) {
                     set_time_limit(0);
                     # if it forces the issue when we try and get the image, there's nothing we can do.
                     $itemLink = str_replace('&amp;', '&', $itemLink);
                     if (pressforward()->og_reader->fetch($itemLink)) {
                         //If there is no featured image passed, let's try and grab the opengraph image.
                         $node = pressforward()->og_reader->fetch($itemLink);
                         $itemFeatImg = $node->image;
                     }
                     if ($itemFeatImg == '') {
                         //Thinking of starting a method here to pull the first image from the body of a post.
                         //http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php
                         //http://stackoverflow.com/questions/1513418/get-all-images-url-from-string
                         //http://stackoverflow.com/questions/7479835/getting-the-first-image-in-string-with-php
                         //preg_match_all('/<img[^>]+>/i',$itemContent, $imgResult);
                         //$imgScript = $imgResult[0][0];
                     }
                     //Most RSS feed readers don't store the image locally. Should we?
                     set_transient('feed_img_' . $itemUID, $itemFeatImg, 60 * 60 * 24);
                 }
             }
             # adding the meta info about the feed item to the post's meta.
             $pf_meta_args = array(pf_meta_for_entry('item_id', $item_id), pf_meta_for_entry('source_title', $source_title), pf_meta_for_entry('item_date', $item_date), pf_meta_for_entry('item_author', $item_author), pf_meta_for_entry('item_link', $item_link), pf_meta_for_entry('item_feat_img', $item_feat_img), pf_meta_for_entry('item_wp_date', $item_wp_date), pf_meta_for_entry('sortable_item_date', strtotime($item_date)), pf_meta_for_entry('item_tags', $item_tags), pf_meta_for_entry('source_repeat', $source_repeat), pf_meta_for_entry('revertible_feed_text', $item_content));
             pf_meta_establish_post($newNomID, $pf_meta_args);
         }
     }
     update_option(PF_SLUG . '_ready_to_chunk', 1);
     #$Feed_Retrieve = new PF_Feed_Retrieve();
     pressforward()->pf_retrieve->advance_feeds();
     //die('Refreshing...');
 }