function import() { $comment_author = $this->author; $comment_author_url = $this->authoruri; $comment_author_email = $this->authoremail; $comment_date = $this->updated; $comment_content = $this->content; $comment_post_ID = $this->post_ID; $comment_author_IP = '127.0.0.1'; //Blogger does not supply the IP so default this // Clean up content // Simplepie does some cleaning but does not do these. $comment_content = str_replace('<br>', '<br />', $comment_content); $comment_content = str_replace('<hr>', '<hr />', $comment_content); $comment_parent = isset($this->parentcommentid) ? $this->parentcommentid : 0; $comment = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email','comment_author_IP','comment_date', 'comment_content', 'comment_parent'); $comment = wp_filter_comment($comment); $comment_id = wp_insert_comment($comment); //links of the form /feeds/417730729915399755/8397846992898424746/comments/default/7732208643735403000 add_comment_meta($comment_id, 'blogger_internal', $this->self, true); return $comment_id; }
function wp_new_comment($commentdata) { $commentdata = apply_filters('preprocess_comment', $commentdata); $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; $commentdata['user_ID'] = (int) $commentdata['user_ID']; $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT']; $commentdata['comment_date'] = current_time('mysql'); $commentdata['comment_date_gmt'] = current_time('mysql', 1); $commentdata = wp_filter_comment($commentdata); $commentdata['comment_approved'] = wp_allow_comment($commentdata); $comment_ID = wp_insert_comment($commentdata); do_action('comment_post', $comment_ID, $commentdata['comment_approved']); if ('spam' !== $commentdata['comment_approved']) { // If it's spam save it silently for later crunching if ('0' == $commentdata['comment_approved']) { wp_notify_moderator($comment_ID); } $post =& get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment if (get_settings('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID']) { wp_notify_postauthor($comment_ID, $commentdata['comment_type']); } } return $comment_ID; }
function process_comments($post_id, $item_id) { if (empty($post_id)) { return; } if (!is_numeric($post_id)) { return; } $comments = $this->model->get_item_comments($item_id); if (!$comments || !isset($comments['data'])) { return; } $comments = $comments['data']; if (!count($comments)) { return false; } foreach ($comments as $comment) { if ($this->model->comment_already_imported($comment['id'])) { continue; } // We already have this comment, continue. $data = array('comment_post_ID' => $post_id, 'comment_date_gmt' => date('Y-m-d H:i:s', strtotime($comment['created_time'])), 'comment_author' => $comment['from']['name'], 'comment_author_url' => 'http://www.facebook.com/profile.php?id=' . $comment['from']['id'], 'comment_content' => $comment['message']); $meta = array('fb_comment_id' => $comment['id'], 'fb_author_id' => $comment['from']['id']); $data = wp_filter_comment($data); $comment_id = wp_insert_comment($data); add_comment_meta($comment_id, 'wdfb_comment', $meta); if ($this->model->data->get_option('wdfb_comments', 'notify_authors')) { wp_notify_postauthor($comment_id, 'comment'); } } }
public static function spawn_random_comments($post) { $fake_comment = array('user_id' => get_current_user_id(), 'comment_author' => 'Author', 'comment_author_IP' => '', 'comment_author_url' => '', 'comment_author_email' => '', 'comment_post_ID' => $post->ID, 'comment_type' => '', 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1), 'comment_approved' => 1, 'comment_content' => 'test stuff author comment'); $comments = array(array_merge($fake_comment, array('user_id' => get_current_user_id(), 'comment_author' => 'Author', 'comment_content' => 'test stuff author comment')), array_merge($fake_comment, array('user_id' => 0, 'comment_author' => 'Visitor', 'comment_content' => 'test stuff visitor comment')), array_merge($fake_comment, array('user_id' => 0, 'comment_author' => 'Trackback', 'comment_type' => 'trackback', 'comment_content' => 'test stuff visitor trackback')), array_merge($fake_comment, array('user_id' => 0, 'comment_author' => 'Pingback', 'comment_type' => 'pingback', 'comment_content' => 'test stuff visitor pingkback'))); foreach ($comments as $cid => $comment) { $comment['comment_ID'] = $cid; $comments[$cid] = (object) wp_filter_comment($comment); } return $comments; }
/** * Update the event journal. * * @since 1.3 * @param arr $data * @param arr $meta * @return: int|bool comment_id or false on failure */ function mdjm_add_journal($args = array(), $meta = array()) { // Return if journaling is disabled. if (!mdjm_get_option('journaling', false)) { return false; } $defaults = array('user_id' => get_current_user_id(), 'event_id' => '', 'comment_content' => '', 'comment_type' => 'mdjm-journal'); $data = wp_parse_args($args, $defaults); // Make sure we have the required data if (empty($data['comment_content']) || empty($data['event_id'])) { return false; } $comment_author = !empty($data['user_id']) ? get_userdata($data['user_id']) : 'mdjm'; $comment_data = apply_filters('mdjm_add_journal', array('comment_post_ID' => (int) $data['event_id'], 'comment_author' => $comment_author != 'mdjm' ? $comment_author->display_name : 'MDJM', 'comment_author_email' => $comment_author != 'mdjm' ? $comment_author->user_email : mdjm_get_option('system_email'), 'comment_author_IP' => '', 'comment_agent' => '', 'comment_author_url' => '', 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1), 'comment_content' => $data['comment_content'], 'comment_type' => 'mdjm-journal', 'user_id' => $comment_author != 'mdjm' ? $comment_author->ID : '0', 'comment_parent' => 0, 'comment_approved' => 1)); // Filter the comment data before inserting $comment_data = apply_filters('preprocess_comment', $comment_data); $comment_data = wp_filter_comment($comment_data); // Disable comment duplication check filter remove_filter('commentdata', 'comment_duplicate_trigger'); do_action('mdjm_pre_add_journal', $data, $meta, $comment_data); // Insert the comment $comment_id = wp_insert_comment($comment_data); if (!$comment_id) { return false; } $comment_meta = array('mdjm_type' => !empty($meta['type']) ? $meta['type'] : 'mdjm-journal', 'mdjm_visibility' => !empty($meta['visibility']) ? $meta['visibility'] : '0', 'mdjm_notify' => !empty($meta['notify']) ? $meta['notify'] : '', 'mdjm_to' => !empty($meta['to']) ? $meta['to'] : '', 'mdjm_isread' => !empty($meta['isread']) ? $meta['isread'] : ''); $comment_meta = wp_parse_args($meta, $comment_meta); foreach ($comment_meta as $key => $value) { if (!empty($value)) { add_comment_meta($comment_id, $key, $value, false); } } // Enable comment duplication check filter add_filter('commentdata', 'comment_duplicate_trigger'); do_action('mdjm_post_add_journal', $data, $meta, $comment_data); return $comment_id; }
function process_posts() { global $wpdb; $i = -1; echo "<ol>"; foreach ($this->posts as $post) { if ('' != trim($post)) { ++$i; unset($post_categories); // Take the pings out first preg_match("|(-----\n\nPING:.*)|s", $post, $pings); $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post); // Then take the comments out preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments); $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post); // We ignore the keywords $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post); // We want the excerpt preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt); $excerpt = $wpdb->escape(trim($excerpt[1])); $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post); // We're going to put extended body into main body with a more tag preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended); $extended = trim($extended[1]); if ('' != $extended) { $extended = "\n<!--more-->\n{$extended}"; } $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post); // Now for the main body preg_match("|-----\nBODY:(.*)|s", $post, $body); $body = trim($body[1]); $post_content = $wpdb->escape($body . $extended); $post = preg_replace("|(-----\nBODY:.*)|s", '', $post); // Grab the metadata from what's left $metadata = explode("\n", $post); foreach ($metadata as $line) { preg_match("/^(.*?):(.*)/", $line, $token); $key = trim($token[1]); $value = trim($token[2]); // Now we decide what it is and what to do with it switch ($key) { case '': break; case 'AUTHOR': $post_author = $value; break; case 'TITLE': $post_title = $wpdb->escape($value); break; case 'STATUS': // "publish" and "draft" enumeration items match up; no change required $post_status = $value; if (empty($post_status)) { $post_status = 'publish'; } break; case 'ALLOW COMMENTS': $post_allow_comments = $value; if ($post_allow_comments == 1) { $comment_status = 'open'; } else { $comment_status = 'closed'; } break; case 'CONVERT BREAKS': $post_convert_breaks = $value; break; case 'ALLOW PINGS': $post_allow_pings = trim($meta[2][0]); if ($post_allow_pings == 1) { $post_allow_pings = 'open'; } else { $post_allow_pings = 'closed'; } break; case 'PRIMARY CATEGORY': if (!empty($value)) { $post_categories[] = $wpdb->escape($value); } break; case 'CATEGORY': if (!empty($value)) { $post_categories[] = $wpdb->escape($value); } break; case 'DATE': $post_modified = strtotime($value); $post_modified = date('Y-m-d H:i:s', $post_modified); $post_modified_gmt = get_gmt_from_date("{$post_modified}"); $post_date = $post_modified; $post_date_gmt = $post_modified_gmt; break; default: // echo "\n$key: $value"; break; } // end switch } // End foreach // Let's check to see if it's in already if ($post_id = post_exists($post_title, '', $post_date)) { echo '<li>'; printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); } else { echo '<li>'; printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt'); $post_id = wp_insert_post($postdata); // Add categories. if (0 != count($post_categories)) { wp_create_categories($post_categories, $post_id); } } $comment_post_ID = $post_id; // Now for comments $comments = explode("-----\nCOMMENT:", $comments[0]); $num_comments = 0; foreach ($comments as $comment) { if ('' != trim($comment)) { // Author preg_match("|AUTHOR:(.*)|", $comment, $comment_author); $comment_author = $wpdb->escape(trim($comment_author[1])); $comment = preg_replace('|(\\n?AUTHOR:.*)|', '', $comment); preg_match("|EMAIL:(.*)|", $comment, $comment_author_email); $comment_author_email = $wpdb->escape(trim($comment_author_email[1])); $comment = preg_replace('|(\\n?EMAIL:.*)|', '', $comment); preg_match("|IP:(.*)|", $comment, $comment_author_IP); $comment_author_IP = trim($comment_author_IP[1]); $comment = preg_replace('|(\\n?IP:.*)|', '', $comment); preg_match("|URL:(.*)|", $comment, $comment_author_url); $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); $comment = preg_replace('|(\\n?URL:.*)|', '', $comment); preg_match("|DATE:(.*)|", $comment, $comment_date); $comment_date = trim($comment_date[1]); $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); $comment = preg_replace('|(\\n?DATE:.*)|', '', $comment); $comment_content = $wpdb->escape(trim($comment)); $comment_content = str_replace('-----', '', $comment_content); // Check if it's already there if (!comment_exists($comment_author, $comment_date)) { $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content'); $commentdata = wp_filter_comment($commentdata); wp_insert_comment($commentdata); $num_comments++; } } } if ($num_comments) { printf(__('(%s comments)'), $num_comments); } // Finally the pings // fix the double newline on the first one $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]); $pings = explode("-----\nPING:", $pings[0]); $num_pings = 0; foreach ($pings as $ping) { if ('' != trim($ping)) { // 'Author' preg_match("|BLOG NAME:(.*)|", $ping, $comment_author); $comment_author = $wpdb->escape(trim($comment_author[1])); $ping = preg_replace('|(\\n?BLOG NAME:.*)|', '', $ping); preg_match("|IP:(.*)|", $ping, $comment_author_IP); $comment_author_IP = trim($comment_author_IP[1]); $ping = preg_replace('|(\\n?IP:.*)|', '', $ping); preg_match("|URL:(.*)|", $ping, $comment_author_url); $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); $ping = preg_replace('|(\\n?URL:.*)|', '', $ping); preg_match("|DATE:(.*)|", $ping, $comment_date); $comment_date = trim($comment_date[1]); $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); $ping = preg_replace('|(\\n?DATE:.*)|', '', $ping); preg_match("|TITLE:(.*)|", $ping, $ping_title); $ping_title = $wpdb->escape(trim($ping_title[1])); $ping = preg_replace('|(\\n?TITLE:.*)|', '', $ping); $comment_content = $wpdb->escape(trim($ping)); $comment_content = str_replace('-----', '', $comment_content); $comment_content = "<strong>{$ping_title}</strong>\n\n{$comment_content}"; $comment_type = 'trackback'; // Check if it's already there if (!comment_exists($comment_author, $comment_date)) { $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_type'); $commentdata = wp_filter_comment($commentdata); wp_insert_comment($commentdata); $num_pings++; } } } if ($num_pings) { printf(__('(%s pings)'), $num_pings); } echo "</li>"; } flush(); } echo '</ol>'; wp_import_cleanup($this->id); echo '<h3>' . sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')) . '</h3>'; }
function process_post($post) { global $wpdb; $post_ID = (int) $this->get_tag($post, 'wp:post_id'); if ($post_ID && !empty($this->post_ids_processed[$post_ID])) { // Processed already return 0; } set_time_limit(60); // There are only ever one of these $post_title = $this->get_tag($post, 'title'); $post_date = $this->get_tag($post, 'wp:post_date'); $post_date_gmt = $this->get_tag($post, 'wp:post_date_gmt'); $comment_status = $this->get_tag($post, 'wp:comment_status'); $ping_status = $this->get_tag($post, 'wp:ping_status'); $post_status = $this->get_tag($post, 'wp:status'); $post_name = $this->get_tag($post, 'wp:post_name'); $post_parent = $this->get_tag($post, 'wp:post_parent'); $menu_order = $this->get_tag($post, 'wp:menu_order'); $post_type = $this->get_tag($post, 'wp:post_type'); $post_password = $this->get_tag($post, 'wp:post_password'); $is_sticky = $this->get_tag($post, 'wp:is_sticky'); $guid = $this->get_tag($post, 'guid'); $post_author = $this->get_tag($post, 'dc:creator'); $post_excerpt = $this->get_tag($post, 'excerpt:encoded'); $post_excerpt = preg_replace_callback('|<(/?[A-Z]+)|', array(&$this, '_normalize_tag'), $post_excerpt); $post_excerpt = str_replace('<br>', '<br />', $post_excerpt); $post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt); $post_content = $this->get_tag($post, 'content:encoded'); $post_content = preg_replace_callback('|<(/?[A-Z]+)|', array(&$this, '_normalize_tag'), $post_content); $post_content = str_replace('<br>', '<br />', $post_content); $post_content = str_replace('<hr>', '<hr />', $post_content); preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags); $tags = $tags[1]; $tag_index = 0; foreach ($tags as $tag) { $tags[$tag_index] = $wpdb->escape(html_entity_decode(str_replace(array('<![CDATA[', ']]>'), '', $tag))); $tag_index++; } preg_match_all('|<category>(.*?)</category>|is', $post, $categories); $categories = $categories[1]; $cat_index = 0; foreach ($categories as $category) { $categories[$cat_index] = $wpdb->escape(html_entity_decode(str_replace(array('<![CDATA[', ']]>'), '', $category))); $cat_index++; } $post_exists = $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = '" . $post_title . "' && post_status = 'publish'", 'ARRAY_N'); if ($post_exists) { echo '<li>'; printf(__('Post <em>%s</em> already exists.', 'wordpress-importer'), stripslashes($post_title)); $comment_post_ID = $post_id = $post_exists; } else { // If it has parent, process parent first. $post_parent = (int) $post_parent; if ($post_parent) { // if we already know the parent, map it to the local ID if (isset($this->post_ids_processed[$post_parent])) { $post_parent = $this->post_ids_processed[$post_parent]; // new ID of the parent } else { // record the parent for later $this->orphans[intval($post_ID)] = $post_parent; } } echo '<li>'; $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', 'post_title', 'post_status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', 'menu_order', 'post_type', 'post_password'); $postdata['import_id'] = $post_ID; if ($post_type == 'attachment') { $remote_url = $this->get_tag($post, 'wp:attachment_url'); if (!$remote_url) { $remote_url = $guid; } $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url); if (!$post_id or is_wp_error($post_id)) { return $post_id; } } else { printf(__('Importing post <em>%s</em>...', 'wordpress-importer') . "\n", stripslashes($post_title)); $comment_post_ID = $post_id = wp_insert_post($postdata); if ($post_id && $is_sticky == 1) { stick_post($post_id); } } if (is_wp_error($post_id)) { return $post_id; } // Memorize old and new ID. if ($post_id && $post_ID) { $this->post_ids_processed[intval($post_ID)] = intval($post_id); } // Add categories. if (count($categories) > 0) { $post_cats = array(); foreach ($categories as $category) { if ('' == $category) { continue; } $slug = sanitize_term_field('slug', $category, 0, 'category', 'db'); $cat = get_term_by('slug', $slug, 'category'); $cat_ID = 0; if (!empty($cat)) { $cat_ID = $cat->term_id; } if ($cat_ID == 0) { $category = $wpdb->escape($category); $cat_ID = wp_insert_category(array('cat_name' => $category)); if (is_wp_error($cat_ID)) { continue; } } $post_cats[] = $cat_ID; } wp_set_post_categories($post_id, $post_cats); } // Add tags. if (count($tags) > 0) { $post_tags = array(); foreach ($tags as $tag) { if ('' == $tag) { continue; } $slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); $tag_obj = get_term_by('slug', $slug, 'post_tag'); $tag_id = 0; if (!empty($tag_obj)) { $tag_id = $tag_obj->term_id; } if ($tag_id == 0) { $tag = $wpdb->escape($tag); $tag_id = wp_insert_term($tag, 'post_tag'); if (is_wp_error($tag_id)) { continue; } $tag_id = $tag_id['term_id']; } $post_tags[] = intval($tag_id); } wp_set_post_tags($post_id, $post_tags); } } // Now for comments preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments); $comments = $comments[1]; $num_comments = 0; $inserted_comments = array(); if ($comments) { foreach ($comments as $comment) { $comment_id = $this->get_tag($comment, 'wp:comment_id'); $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; $newcomments[$comment_id]['comment_author'] = $this->get_tag($comment, 'wp:comment_author'); $newcomments[$comment_id]['comment_author_email'] = $this->get_tag($comment, 'wp:comment_author_email'); $newcomments[$comment_id]['comment_author_IP'] = $this->get_tag($comment, 'wp:comment_author_IP'); $newcomments[$comment_id]['comment_author_url'] = $this->get_tag($comment, 'wp:comment_author_url'); $newcomments[$comment_id]['comment_date'] = $this->get_tag($comment, 'wp:comment_date'); $newcomments[$comment_id]['comment_date_gmt'] = $this->get_tag($comment, 'wp:comment_date_gmt'); $newcomments[$comment_id]['comment_content'] = $this->get_tag($comment, 'wp:comment_content'); $newcomments[$comment_id]['comment_approved'] = $this->get_tag($comment, 'wp:comment_approved'); $newcomments[$comment_id]['comment_type'] = $this->get_tag($comment, 'wp:comment_type'); $newcomments[$comment_id]['comment_parent'] = $this->get_tag($comment, 'wp:comment_parent'); } // Sort by comment ID, to make sure comment parents exist (if there at all) ksort($newcomments); foreach ($newcomments as $key => $comment) { // if this is a new post we can skip the comment_exists() check if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) { if (isset($inserted_comments[$comment['comment_parent']])) { $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; } $comment = wp_filter_comment($comment); $inserted_comments[$key] = wp_insert_comment($comment); $num_comments++; } } } if ($num_comments) { printf(' ' . _n('(%s comment)', '(%s comments)', $num_comments, 'wordpress-importer'), $num_comments); } // Now for post meta preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta); $postmeta = $postmeta[1]; if ($postmeta) { foreach ($postmeta as $p) { $key = $this->get_tag($p, 'wp:meta_key'); $value = $this->get_tag($p, 'wp:meta_value'); $this->process_post_meta($post_id, $key, $value); } } do_action('import_post_added', $post_id); print "</li>\n"; }
function sanitize_inputs($data) { // sanitize inputs $cleaned_data = array(); foreach ($data as $key => $value) { // 1. do we care ? if so, add it if (in_array($key, self::$comment_fields)) { $cleaned_data[$key] = $value; } } return wp_filter_comment($cleaned_data); }
function download_comment_bodies() { global $wpdb; $cookie = $this->get_session(); if (is_wp_error($cookie)) { return $cookie; } // Load previous state (if any) $this->usermap = (array) get_option('ljapi_usermap'); $maxid = get_option('ljapi_maxid') ? (int) get_option('ljapi_maxid') : 1; $highest_id = (int) get_option('ljapi_highest_comment_id'); $loop = 0; while ($maxid > $highest_id && $loop < 5) { // We do 5 loops per call to avoid memory limits $loop++; // Get a batch of comments, using the highest_id we've already got as a starting point $results = wp_remote_get($this->comments_url . '?get=comment_body&startid=' . ($highest_id + 1), array('cookies' => array($cookie), 'timeout' => 20)); if (is_wp_error($results)) { return new WP_Error('comment_bodies', __('Failed to retrieve comment bodies from LiveJournal. Please try again soon.')); } $results = wp_remote_retrieve_body($results); // Parse out each comment and insert directly preg_match_all('|<comment id=\'(\\d+)\'.*</comment>|iUs', $results, $matches); for ($c = 0; $c < count($matches[0]); $c++) { // Keep track of highest id seen if ($matches[1][$c] > $highest_id) { $highest_id = $matches[1][$c]; update_option('ljapi_highest_comment_id', $highest_id); } $comment = $matches[0][$c]; // Filter out any captured, deleted comments (nothing useful to import) $comment = preg_replace('|<comment id=\'\\d+\' jitemid=\'\\d+\' posterid=\'\\d+\' state=\'D\'[^/]*/>|is', '', $comment); // Parse this comment into an array and insert $comment = $this->parse_comment($comment); $comment = wp_filter_comment($comment); $id = wp_insert_comment($comment); // Clear cache clean_comment_cache($id); } // Clear cache to preseve memory wp_cache_flush(); } // endwhile - all comments downloaded and ready for bulk processing // Counter just used to show progress to user update_option('ljapi_comment_batch', (int) get_option('ljapi_comment_batch') + 1); return true; }
function import() { global $wpdb; $wpvarstoreset = array('gmpath', 'archivespath', 'lastentry'); for ($i = 0; $i < count($wpvarstoreset); $i += 1) { $wpvar = $wpvarstoreset[$i]; if (!isset(${$wpvar})) { if (empty($_POST["{$wpvar}"])) { if (empty($_GET["{$wpvar}"])) { ${$wpvar} = ''; } else { ${$wpvar} = $_GET["{$wpvar}"]; } } else { ${$wpvar} = $_POST["{$wpvar}"]; } } } if (!chdir($archivespath)) { wp_die(sprintf(__("Wrong path, %s\ndoesn't exist\non the server"), $archivespath)); } if (!chdir($gmpath)) { wp_die(sprintf(__("Wrong path, %s\ndoesn't exist\non the server"), $gmpath)); } $this->header(); ?> <p><?php _e('The importer is running...'); ?> </p> <ul> <li><?php _e('importing users...'); ?> <ul><?php chdir($gmpath); $userbase = file("gm-authors.cgi"); foreach ($userbase as $user) { $userdata = explode("|", $user); $user_ip = "127.0.0.1"; $user_domain = "localhost"; $user_browser = "server"; $s = $userdata[4]; $user_joindate = substr($s, 6, 4) . "-" . substr($s, 0, 2) . "-" . substr($s, 3, 2) . " 00:00:00"; $user_login = $wpdb->escape($userdata[0]); $pass1 = $wpdb->escape($userdata[1]); $user_nickname = $wpdb->escape($userdata[0]); $user_email = $wpdb->escape($userdata[2]); $user_url = $wpdb->escape($userdata[3]); $user_joindate = $wpdb->escape($user_joindate); $user_id = username_exists($user_login); if ($user_id) { printf('<li>' . __('user %s') . '<strong>' . __('Already exists') . '</strong></li>', "<em>{$user_login}</em>"); $this->gmnames[$userdata[0]] = $user_id; continue; } $user_info = array("user_login" => "{$user_login}", "user_pass" => "{$pass1}", "user_nickname" => "{$user_nickname}", "user_email" => "{$user_email}", "user_url" => "{$user_url}", "user_ip" => "{$user_ip}", "user_domain" => "{$user_domain}", "user_browser" => "{$user_browser}", "dateYMDhour" => "{$user_joindate}", "user_level" => "1", "user_idmode" => "nickname"); $user_id = wp_insert_user($user_info); $this->gmnames[$userdata[0]] = $user_id; printf('<li>' . __('user %s...') . ' <strong>' . __('Done') . '</strong></li>', "<em>{$user_login}</em>"); } ?> </ul><strong><?php _e('Done'); ?> </strong></li> <li><?php _e('importing posts, comments, and karma...'); ?> <br /><ul><?php chdir($archivespath); for ($i = 0; $i <= $lastentry; $i = $i + 1) { $entryfile = ""; if ($i < 10000000) { $entryfile .= "0"; if ($i < 1000000) { $entryfile .= "0"; if ($i < 100000) { $entryfile .= "0"; if ($i < 10000) { $entryfile .= "0"; if ($i < 1000) { $entryfile .= "0"; if ($i < 100) { $entryfile .= "0"; if ($i < 10) { $entryfile .= "0"; } } } } } } } $entryfile .= "{$i}"; if (is_file($entryfile . ".cgi")) { $entry = file($entryfile . ".cgi"); $postinfo = explode("|", $entry[0]); $postmaincontent = $this->gm2autobr($entry[2]); $postmorecontent = $this->gm2autobr($entry[3]); $post_author = trim($wpdb->escape($postinfo[1])); $post_title = $this->gm2autobr($postinfo[2]); printf('<li>' . __('entry # %s : %s : by %s'), $entryfile, $post_title, $postinfo[1]); $post_title = $wpdb->escape($post_title); $postyear = $postinfo[6]; $postmonth = zeroise($postinfo[4], 2); $postday = zeroise($postinfo[5], 2); $posthour = zeroise($postinfo[7], 2); $postminute = zeroise($postinfo[8], 2); $postsecond = zeroise($postinfo[9], 2); if ($postinfo[10] == "PM" && $posthour != "12") { $posthour = $posthour + 12; } $post_date = "{$postyear}-{$postmonth}-{$postday} {$posthour}:{$postminute}:{$postsecond}"; $post_content = $postmaincontent; if (strlen($postmorecontent) > 3) { $post_content .= "<!--more--><br /><br />" . $postmorecontent; } $post_content = $wpdb->escape($post_content); $post_karma = $postinfo[12]; $post_status = 'publish'; //in greymatter, there are no drafts $comment_status = 'open'; $ping_status = 'closed'; if ($post_ID = post_exists($post_title, '', $post_date)) { echo ' '; _e('(already exists)'); } else { //just so that if a post already exists, new users are not created by checkauthor // we'll check the author is registered, or if it's a deleted author $user_id = username_exists($post_author); if (!$user_id) { // if deleted from GM, we register the author as a level 0 user $user_ip = "127.0.0.1"; $user_domain = "localhost"; $user_browser = "server"; $user_joindate = "1979-06-06 00:41:00"; $user_login = $wpdb->escape($post_author); $pass1 = $wpdb->escape("password"); $user_nickname = $wpdb->escape($post_author); $user_email = $wpdb->escape("*****@*****.**"); $user_url = $wpdb->escape(""); $user_joindate = $wpdb->escape($user_joindate); $user_info = array("user_login" => $user_login, "user_pass" => $pass1, "user_nickname" => $user_nickname, "user_email" => $user_email, "user_url" => $user_url, "user_ip" => $user_ip, "user_domain" => $user_domain, "user_browser" => $user_browser, "dateYMDhour" => $user_joindate, "user_level" => 0, "user_idmode" => "nickname"); $user_id = wp_insert_user($user_info); $this->gmnames[$postinfo[1]] = $user_id; echo ': '; printf(__('registered deleted user %s at level 0 '), "<em>{$user_login}</em>"); } if (array_key_exists($postinfo[1], $this->gmnames)) { $post_author = $this->gmnames[$postinfo[1]]; } else { $post_author = $user_id; } $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt'); $post_ID = wp_insert_post($postdata); } $c = count($entry); if ($c > 4) { $numAddedComments = 0; $numComments = 0; for ($j = 4; $j < $c; $j++) { $entry[$j] = $this->gm2autobr($entry[$j]); $commentinfo = explode("|", $entry[$j]); $comment_post_ID = $post_ID; $comment_author = $wpdb->escape($commentinfo[0]); $comment_author_email = $wpdb->escape($commentinfo[2]); $comment_author_url = $wpdb->escape($commentinfo[3]); $comment_author_IP = $wpdb->escape($commentinfo[1]); $commentyear = $commentinfo[7]; $commentmonth = zeroise($commentinfo[5], 2); $commentday = zeroise($commentinfo[6], 2); $commenthour = zeroise($commentinfo[8], 2); $commentminute = zeroise($commentinfo[9], 2); $commentsecond = zeroise($commentinfo[10], 2); if ($commentinfo[11] == "PM" && $commenthour != "12") { $commenthour = $commenthour + 12; } $comment_date = "{$commentyear}-{$commentmonth}-{$commentday} {$commenthour}:{$commentminute}:{$commentsecond}"; $comment_content = $wpdb->escape($commentinfo[12]); if (!comment_exists($comment_author, $comment_date)) { $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved'); $commentdata = wp_filter_comment($commentdata); wp_insert_comment($commentdata); $numAddedComments++; } $numComments++; } if ($numAddedComments > 0) { echo ': '; printf(__('imported %d comment(s)'), $numAddedComments); } $preExisting = $numComments - numAddedComments; if ($preExisting > 0) { echo ' '; printf(__('ignored %d pre-existing comments'), $preExisting); } } echo '... <strong>' . __('Done') . '</strong></li>'; } } ?> </ul><strong><?php _e('Done'); ?> </strong></li></ul> <p> </p> <p><?php _e('Completed GreyMatter import!'); ?> </p> <?php $this->footer(); }
function import_posts() { global $wpdb, $current_user; set_magic_quotes_runtime(0); $importdata = file($this->file); // Read the file into an array $importdata = implode('', $importdata); // squish it $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts); $posts = $posts[1]; unset($importdata); echo '<ol>'; foreach ($posts as $post) { flush(); preg_match('|<item type=\"(.*?)\">|is', $post, $post_type); $post_type = $post_type[1]; if($post_type == "photo") { preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title); } else { preg_match('|<title>(.*?)</title>|is', $post, $post_title); } $post_title = $wpdb->escape(trim($post_title[1])); preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date); $post_date = strtotime($post_date[1]); $post_date = gmdate('Y-m-d H:i:s', $post_date); preg_match_all('|<category>(.*?)</category>|is', $post, $categories); $categories = $categories[1]; $cat_index = 0; foreach ($categories as $category) { $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); $cat_index++; } if(strcasecmp($post_type, "photo") === 0) { preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content); $post_content = '<img src="'.trim($post_content[1]).'" />'; $post_content = $this->unhtmlentities($post_content); } else { preg_match('|<body>(.*?)</body>|is', $post, $post_content); $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1])); $post_content = $this->unhtmlentities($post_content); } // Clean up content $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); $post_content = str_replace('<br>', '<br />', $post_content); $post_content = str_replace('<hr>', '<hr />', $post_content); $post_content = $wpdb->escape($post_content); $post_author = $current_user->ID; preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status); $post_status = trim($post_status[1]); echo '<li>'; if ($post_id = post_exists($post_title, $post_content, $post_date)) { printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title)); } else { printf(__('Importing post <em>%s</em>...'), stripslashes($post_title)); $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status'); $post_id = wp_insert_post($postdata); if ( is_wp_error( $post_id ) ) { return $post_id; } if (!$post_id) { _e("Couldn't get post ID"); echo '</li>'; break; } if(0 != count($categories)) wp_create_categories($categories, $post_id); } preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments); $comments = $comments[1]; if ( $comments ) { $comment_post_ID = (int) $post_id; $num_comments = 0; foreach ($comments as $comment) { preg_match('|<body>(.*?)</body>|is', $comment, $comment_content); $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1])); $comment_content = $this->unhtmlentities($comment_content); // Clean up content $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content); $comment_content = str_replace('<br>', '<br />', $comment_content); $comment_content = str_replace('<hr>', '<hr />', $comment_content); $comment_content = $wpdb->escape($comment_content); preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date); $comment_date = trim($comment_date[1]); $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); preg_match('|<author>(.*?)</author>|is', $comment, $comment_author); $comment_author = $wpdb->escape(trim($comment_author[1])); $comment_author_email = NULL; $comment_approved = 1; // Check if it's already there if (!comment_exists($comment_author, $comment_date)) { $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved'); $commentdata = wp_filter_comment($commentdata); wp_insert_comment($commentdata); $num_comments++; } } } if ( $num_comments ) { echo ' '; printf( __ngettext('%s comment', '%s comments', $num_comments), $num_comments ); } echo '</li>'; flush(); ob_flush(); } echo '</ol>'; }
/** * wp_update_comment() - Parses and updates an existing comment in the database * * {@internal Missing Long Description}} * * @since 2.0.0 * @uses $wpdb * * @param array $commentarr Contains information on the comment * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); // Escape data pulled from DB. foreach ((array) $comment as $key => $value) { $comment[$key] = $wpdb->escape($value); } // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. extract($commentarr, EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); $wpdb->query("UPDATE {$wpdb->comments} SET\n\t\t\tcomment_content = '{$comment_content}',\n\t\t\tcomment_author = '{$comment_author}',\n\t\t\tcomment_author_email = '{$comment_author_email}',\n\t\t\tcomment_approved = '{$comment_approved}',\n\t\t\tcomment_author_url = '{$comment_author_url}',\n\t\t\tcomment_date = '{$comment_date}',\n\t\t\tcomment_date_gmt = '{$comment_date_gmt}'\n\t\tWHERE comment_ID = {$comment_ID}"); $rval = $wpdb->rows_affected; clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); return $rval; }
public function add_journal($data, $meta) { global $mdjm, $post; if (MDJM_JOURNAL != true) { if (MDJM_DEBUG == true) { MDJM()->debug->log_it('ERROR: Instructed to Journal whilst Journalling is disabled'); } return; } if (empty($data['comment_content'])) { if (MDJM_DEBUG == true) { MDJM()->debug->log_it('ERROR: Missing Comment Contents in ' . __FUNCTION__, true); } return false; } if (empty($meta['type'])) { if (MDJM_DEBUG == true) { MDJM()->debug->log_it('ERROR: Missing Comment Type in ' . __FUNCTION__, true); } return false; } /* -- Disable Comment Flood Prevention -- */ add_filter('comment_flood_filter', '__return_false'); $event_id = !empty($data['event']) ? $data['event'] : $post->ID; if (empty($event_id)) { if (MDJM_DEBUG == true) { MDJM()->debug->log_it('ERROR: Missing event id in ' . __FUNCTION__, true); } return false; } /* -- Set the content -- */ if (isset($data['user'])) { $commenter = get_userdata($data['user']); } else { $commenter = 'mdjm'; } $comment_data = array('comment_post_ID' => (int) $event_id, 'comment_author' => $commenter != 'mdjm' ? $commenter->display_name : '', 'comment_author_email' => $commenter != 'mdjm' ? $commenter->user_email : '', 'comment_author_IP' => !empty($_SERVER['REMOTE_ADDR']) ? preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']) : '', 'comment_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? substr($_SERVER['HTTP_USER_AGENT'], 0, 254) : '', 'comment_author_url' => $commenter != 'mdjm' ? !empty($commenter->user_url) ? $commenter->user_url : '' : '', 'comment_content' => $data['comment_content'] . ' (' . time() . ')', 'comment_type' => !empty($data['comment_type']) ? $data['comment_type'] : 'mdjm-journal', 'comment_date' => !empty($data['comment_date']) ? $data['comment_date'] : current_time('mysql'), 'user_id' => $commenter != 'mdjm' ? $commenter->ID : '0', 'comment_parent' => 0, 'comment_approved' => 1); // Filter the comment data before inserting $comment_data = apply_filters('preprocess_comment', $comment_data); $comment_data = wp_filter_comment($comment_data); /* -- Disable comment duplication check filter -- */ remove_filter('commentdata', 'comment_duplicate_trigger'); /* -- Insert the entry -- */ $comment_id = wp_insert_comment($comment_data); if (empty($comment_id)) { return false; } /* -- Set the meta -- */ $comment_meta = array('mdjm_type' => $meta['type'], 'mdjm_visibility' => !empty($meta['visibility']) ? $meta['visibility'] : '0', 'mdjm_notify' => !empty($meta['notify']) ? $meta['notify'] : '', 'mdjm_to' => !empty($meta['to']) ? $meta['to'] : '', 'mdjm_isread' => !empty($meta['isread']) ? $meta['isread'] : ''); /* -- Insert the meta -- */ foreach ($comment_meta as $key => $value) { if (!empty($value)) { add_comment_meta($comment_id, $key, $value, false); } } /* -- Enable comment filter -- */ add_filter('commentdata', 'comment_duplicate_trigger'); return $comment_id; }
function nxs_postNewComment($cmnt, $aa = false) { $cmnt['comment_post_ID'] = (int) $cmnt['comment_post_ID']; $cmnt['comment_parent'] = isset($cmnt['comment_parent']) ? absint($cmnt['comment_parent']) : 0; $parent_status = 0 < $cmnt['comment_parent'] ? wp_get_comment_status($cmnt['comment_parent']) : ''; $cmnt['comment_parent'] = 'approved' == $parent_status || 'unapproved' == $parent_status ? $cmnt['comment_parent'] : 0; $cmnt['comment_author_IP'] = ''; $cmnt['comment_agent'] = 'SNAP'; $cmnt['comment_date'] = get_date_from_gmt($cmnt['comment_date_gmt']); $cmnt = wp_filter_comment($cmnt); if ($aa) { $cmnt['comment_approved'] = 1; } else { $cmnt['comment_approved'] = wp_allow_comment($cmnt); } $cmntID = wp_insert_comment($cmnt); if ('spam' !== $cmnt['comment_approved']) { if ('0' == $cmnt['comment_approved']) { wp_notify_moderator($cmntID); } $post =& get_post($cmnt['comment_post_ID']); if (get_option('comments_notify') && $cmnt['comment_approved'] && (!isset($cmnt['user_id']) || $post->post_author != $cmnt['user_id'])) { wp_notify_postauthor($cmntID, isset($cmnt['comment_type']) ? $cmnt['comment_type'] : ''); } global $wpdb, $dsq_api; if (isset($dsq_api)) { $plugins_url = str_replace('social-networks-auto-poster-facebook-twitter-g/', '', plugin_dir_path(__FILE__)); require_once $plugins_url . 'disqus-comment-system/export.php'; if (function_exists('dsq_export_wp')) { $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = " . $cmntID)); // prr($comments); $wxr = dsq_export_wp($post, $comments); $response = $dsq_api->import_wordpress_comments($wxr, time()); // prr($response); } } } return $cmntID; }
/** * 从服务器pull评论到本地 * * @param array $posts */ public function createPost($post) { global $wpdb; // 将img加到白名单中,仅对WordPress 3.5.0以后有效 if (!has_filter('wp_kses_allowed_html', array($this, 'allowedHtml'))) { add_filter('wp_kses_allowed_html', array($this, 'allowedHtml')); } static $approvedMap = array('pending' => '0', 'approved' => '1', 'deleted' => 'trash', 'spam' => 'spam', 'thread-deleted' => 'post-trashed'); $post_id = isset($post['thread_key']) ? $post['thread_key'] : $wpdb->get_var("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'duoshuo_thread_id' AND meta_value = {$post['thread_id']}"); if (!is_numeric($post_id)) { // 找不到对应的文章 return array(); } $data = array('comment_author' => trim(strip_tags($post['author_name'])), 'comment_author_email' => $post['author_email'], 'comment_author_url' => $post['author_url'], 'comment_author_IP' => $post['ip'], 'comment_date' => $this->rfc3339_to_mysql($post['created_at']), 'comment_date_gmt' => $this->rfc3339_to_mysql_gmt($post['created_at']), 'comment_content' => $post['message'], 'comment_approved' => $approvedMap[$post['status']], 'comment_agent' => 'Duoshuo/' . self::VERSION . ':' . $post['post_id'], 'comment_type' => $post['type'], 'comment_post_ID' => $post_id); if ($post['parent_id']) { $parent_id = $wpdb->get_var("SELECT comment_ID FROM {$wpdb->commentmeta} WHERE meta_key = 'duoshuo_post_id' AND meta_value = '{$post['parent_id']}'"); if (isset($parent_id)) { $data['comment_parent'] = $parent_id; } } $author_id = isset($post['author_key']) ? $post['author_key'] : $wpdb->get_var("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'duoshuo_user_id' AND meta_value = {$post['author_id']}"); if (is_numeric($author_id)) { $data['user_id'] = $author_id; } if (isset($post['post_key'])) { $data['comment_ID'] = $post['post_key']; } elseif (isset($post['post_id'])) { $data['comment_ID'] = $wpdb->get_var("SELECT comment_ID FROM {$wpdb->commentmeta} WHERE meta_key = 'duoshuo_post_id' AND meta_value = '{$post['post_id']}'"); } if (isset($data['comment_ID'])) { // wp_update_comment 中会做 wp_filter_comment wp_update_comment($data); } else { $data = wp_filter_comment($data); $data['comment_ID'] = wp_insert_comment($data); } if ($post['parent_id']) { update_comment_meta($data['comment_ID'], 'duoshuo_parent_id', $post['parent_id']); } else { delete_comment_meta($data['comment_ID'], 'duoshuo_parent_id'); } update_comment_meta($data['comment_ID'], 'duoshuo_post_id', $post['post_id']); return array($post_id); }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * @uses $wpdb * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param array $commentarr Contains information on the comment. * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); if (empty($comment)) { return 0; } // Escape data pulled from DB. $comment = wp_slash($comment); $old_status = $comment['comment_approved']; // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. extract(wp_unslash($commentarr), EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); if (!isset($comment_approved)) { $comment_approved = 1; } else { if ('hold' == $comment_approved) { $comment_approved = 0; } else { if ('approve' == $comment_approved) { $comment_approved = 1; } } } $data = compact('comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent'); $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment->comment_approved, $old_status, $comment); return $rval; }
function import_posts() { global $wpdb, $current_user; set_magic_quotes_runtime(0); $importdata = file($this->file); // Read the file into an array $importdata = implode('', $importdata); // squish it $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts); $posts = $posts[1]; unset($importdata); echo '<ol>'; foreach ($posts as $post) { flush(); preg_match('|<subject>(.*?)</subject>|is', $post, $post_title); $post_title = $wpdb->escape(trim($post_title[1])); if ( empty($post_title) ) { preg_match('|<itemid>(.*?)</itemid>|is', $post, $post_title); $post_title = $wpdb->escape(trim($post_title[1])); } preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date); $post_date = strtotime($post_date[1]); $post_date = gmdate('Y-m-d H:i:s', $post_date); preg_match('|<event>(.*?)</event>|is', $post, $post_content); $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1])); $post_content = $this->unhtmlentities($post_content); // Clean up content $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); $post_content = str_replace('<br>', '<br />', $post_content); $post_content = str_replace('<hr>', '<hr />', $post_content); $post_content = $wpdb->escape($post_content); $post_author = $current_user->ID; $post_status = 'publish'; echo '<li>'; if ($post_id = post_exists($post_title, $post_content, $post_date)) { printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); } else { printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status'); $post_id = wp_insert_post($postdata); if (!$post_id) { _e("Couldn't get post ID"); echo '</li>'; break; } } preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments); $comments = $comments[1]; if ( $comments ) { $comment_post_ID = (int) $post_id; $num_comments = 0; foreach ($comments as $comment) { preg_match('|<event>(.*?)</event>|is', $comment, $comment_content); $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1])); $comment_content = $this->unhtmlentities($comment_content); // Clean up content $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content); $comment_content = str_replace('<br>', '<br />', $comment_content); $comment_content = str_replace('<hr>', '<hr />', $comment_content); $comment_content = $wpdb->escape($comment_content); preg_match('|<eventtime>(.*?)</eventtime>|is', $comment, $comment_date); $comment_date = trim($comment_date[1]); $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); preg_match('|<name>(.*?)</name>|is', $comment, $comment_author); $comment_author = $wpdb->escape(trim($comment_author[1])); preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email); $comment_author_email = $wpdb->escape(trim($comment_author_email[1])); $comment_approved = 1; // Check if it's already there if (!comment_exists($comment_author, $comment_date)) { $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved'); $commentdata = wp_filter_comment($commentdata); wp_insert_comment($commentdata); $num_comments++; } } } if ( $num_comments ) { echo ' '; printf(__('(%s comments)'), $num_comments); } echo '</li>'; flush(); ob_flush(); } echo '</ol>'; }
/** * Add a note to a payment * * @since 1.4 * @param int $payment_id The payment ID to store a note for * @param string $note The note to store * @return int The new note ID */ function edd_insert_payment_note($payment_id = 0, $note = '') { if (empty($payment_id)) { return false; } do_action('edd_pre_insert_payment_note', $payment_id, $note); $note_id = wp_insert_comment(wp_filter_comment(array('comment_post_ID' => $payment_id, 'comment_content' => $note, 'user_id' => is_admin() ? get_current_user_id() : 0, 'comment_date' => current_time('mysql'), 'comment_date_gmt' => current_time('mysql', 1), 'comment_approved' => 1, 'comment_parent' => 0, 'comment_author' => '', 'comment_author_IP' => '', 'comment_author_url' => '', 'comment_author_email' => '', 'comment_type' => 'edd_payment_note'))); do_action('edd_insert_payment_note', $note_id, $payment_id, $note); return $note_id; }
function get_archive() { global $wpdb; $output = '<h2>'.__('Importing Blogger archives into WordPress').'</h2>'; $did_one = false; $post_array = $posts = array(); foreach ( $this->import['blogs'][$_GET['blog']]['archives'] as $url => $status ) { $archivename = substr(basename($url),0,7); if ( $status || $did_one ) { $foo = 'bar'; // Do nothing. } else { // Import the selected month $postcount = 0; $skippedpostcount = 0; $commentcount = 0; $skippedcommentcount = 0; $status = __('in progress...'); $this->import['blogs'][$_GET['blog']]['archives']["$url"] = $status; update_option('import-blogger', $import); $archive = $this->get_blogger($url); if ( $archive['code'] > 200 ) continue; $posts = explode('<wordpresspost>', $archive['body']); for ($i = 1; $i < count($posts); $i = $i + 1) { $postparts = explode('<wordpresscomment>', $posts[$i]); $postinfo = explode('|W|P|', $postparts[0]); $post_date = $postinfo[0]; $post_content = $postinfo[2]; // Don't try to re-use the original numbers // because the new, longer numbers are too // big to handle as ints. //$post_number = $postinfo[3]; $post_title = ( $postinfo[4] != '' ) ? $postinfo[4] : $postinfo[3]; $post_author_name = $wpdb->escape(trim($postinfo[1])); $post_author_email = $postinfo[5] ? $postinfo[5] : '*****@*****.**'; if ( $this->lump_authors ) { // Ignore Blogger authors. Use the current user_ID for all posts imported. $post_author = $GLOBALS['user_ID']; } else { // Add a user for each new author encountered. if (! username_exists($post_author_name) ) { $user_login = $wpdb->escape($post_author_name); $user_email = $wpdb->escape($post_author_email); $user_password = substr(md5(uniqid(microtime())), 0, 6); $result = wp_create_user( $user_login, $user_password, $user_email ); $status.= sprintf('Registered user <strong>%s</strong>.', $user_login); $this->import['blogs'][$_GET['blog']]['newusers'][] = $user_login; } $userdata = get_userdatabylogin( $post_author_name ); $post_author = $userdata->ID; } $post_date = explode(' ', $post_date); $post_date_Ymd = explode('/', $post_date[0]); $postyear = $post_date_Ymd[2]; $postmonth = zeroise($post_date_Ymd[0], 2); $postday = zeroise($post_date_Ymd[1], 2); $post_date_His = explode(':', $post_date[1]); $posthour = zeroise($post_date_His[0], 2); $postminute = zeroise($post_date_His[1], 2); $postsecond = zeroise($post_date_His[2], 2); if (($post_date[2] == 'PM') && ($posthour != '12')) $posthour = $posthour + 12; else if (($post_date[2] == 'AM') && ($posthour == '12')) $posthour = '00'; $post_date = "$postyear-$postmonth-$postday $posthour:$postminute:$postsecond"; $post_content = addslashes($post_content); $post_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $post_content); // the XHTML touch... ;) $post_title = addslashes($post_title); $post_status = 'publish'; if ( $ID = post_exists($post_title, '', $post_date) ) { $post_array[$i]['ID'] = $ID; $skippedpostcount++; } else { $post_array[$i]['post'] = compact('post_author', 'post_content', 'post_title', 'post_category', 'post_author', 'post_date', 'post_status'); $post_array[$i]['comments'] = false; } // Import any comments attached to this post. if ($postparts[1]) : for ($j = 1; $j < count($postparts); $j = $j + 1) { $commentinfo = explode('|W|P|', $postparts[$j]); $comment_date = explode(' ', $commentinfo[0]); $comment_date_Ymd = explode('/', $comment_date[0]); $commentyear = $comment_date_Ymd[2]; $commentmonth = zeroise($comment_date_Ymd[0], 2); $commentday = zeroise($comment_date_Ymd[1], 2); $comment_date_His = explode(':', $comment_date[1]); $commenthour = zeroise($comment_date_His[0], 2); $commentminute = zeroise($comment_date_His[1], 2); $commentsecond = '00'; if (($comment_date[2] == 'PM') && ($commenthour != '12')) $commenthour = $commenthour + 12; else if (($comment_date[2] == 'AM') && ($commenthour == '12')) $commenthour = '00'; $comment_date = "$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond"; $comment_author = addslashes(strip_tags($commentinfo[1])); if ( strpos($commentinfo[1], 'a href') ) { $comment_author_parts = explode('"', htmlentities($commentinfo[1])); $comment_author_url = $comment_author_parts[1]; } else $comment_author_url = ''; $comment_content = $commentinfo[2]; $comment_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $comment_content); $comment_approved = 1; if ( comment_exists($comment_author, $comment_date) ) { $skippedcommentcount++; } else { $comment = compact('comment_author', 'comment_author_url', 'comment_date', 'comment_content', 'comment_approved'); $post_array[$i]['comments'][$j] = wp_filter_comment($comment); } $commentcount++; } endif; $postcount++; } if ( count($post_array) ) { krsort($post_array); foreach($post_array as $post) { if ( ! $comment_post_ID = $post['ID'] ) $comment_post_ID = wp_insert_post($post['post']); if ( $post['comments'] ) { foreach ( $post['comments'] as $comment ) { $comment['comment_post_ID'] = $comment_post_ID; wp_insert_comment($comment); } } } } $status = sprintf(__('%s post(s) parsed, %s skipped...'), $postcount, $skippedpostcount).' '. sprintf(__('%s comment(s) parsed, %s skipped...'), $commentcoun, $skippedcommentcount).' '. ' <strong>'.__('Done').'</strong>'; $import = $this->import; $import['blogs'][$_GET['blog']]['archives']["$url"] = $status; update_option('import-blogger', $import); $did_one = true; } $output.= "<p>$archivename $status</p>\n"; } if ( ! $did_one ) $this->set_next_step(7); die( $this->refresher(1000) . $output ); }
function cherry_plugin_import_posts() { $nonce = $_POST['nonce']; if (!wp_verify_nonce($nonce, 'import_ajax-nonce')) { exit('instal_error'); } if (session_id() != "import_xml") { session_name("import_xml"); session_start(); } do_action('cherry_plugin_import_posts'); $_SESSION['url_remap'] = array(); $_SESSION['featured_images'] = array(); $_SESSION['attachment_posts'] = array(); $_SESSION['processed_posts'] = array(); $posts_array = $_SESSION['posts']; $posts_array = apply_filters('wp_import_posts', $posts_array); $attachment_posts = array(); foreach ($posts_array as $post) { $post = apply_filters('wp_import_post_data_raw', $post); if (!post_type_exists($post['post_type'])) { // Failed to import do_action('wp_import_post_exists', $post); continue; } if (isset($_SESSION['processed_posts'][$post['post_id']]) && !empty($post['post_id'])) { continue; } if ($post['status'] == 'auto-draft') { continue; } if ('nav_menu_item' == $post['post_type']) { continue; } //!!!!$post_type_object = get_post_type_object( $post['post_type'] ); $post_exists = post_exists($post['post_title'], '', $post['post_date']); if ($post_exists && get_post_type($post_exists) == $post['post_type']) { // already exists $comment_post_ID = $post_id = $post_exists; } else { $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($_SESSION['processed_posts'][$post_parent])) { $post_parent = $_SESSION['processed_posts'][$post_parent]; // otherwise record the parent for later } else { $_SESSION['post_orphans'][intval($post['post_id'])] = $post_parent; $post_parent = 0; } } $author = (int) get_current_user_id(); $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); $original_post_ID = $post['post_id']; $postdata = apply_filters('wp_import_post_data_processed', $postdata, $post); if ('attachment' == $postdata['post_type']) { array_push($attachment_posts, $post); } if ('attachment' != $postdata['post_type']) { ini_set('max_execution_time', -1); set_time_limit(0); $comment_post_ID = $post_id = wp_insert_post($postdata, true); do_action('wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post); if (is_wp_error($post_id)) { // Failed to import continue; } if ($post['is_sticky'] == 1) { stick_post($post_id); } // map pre-import ID to local ID $_SESSION['processed_posts'][intval($post['post_id'])] = intval($post_id); if (!isset($post['terms'])) { $post['terms'] = array(); } $post['terms'] = apply_filters('wp_import_post_terms', $post['terms'], $post_id, $post); // add categories, tags and other terms if (!empty($post['terms'])) { $terms_to_set = array(); foreach ($post['terms'] as $term) { // back compat with WXR 1.0 map 'tag' to 'post_tag' $taxonomy = 'tag' == $term['domain'] ? 'post_tag' : $term['domain']; $term_exists = term_exists($term['slug'], $taxonomy); $term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists; if (!$term_id) { $t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug'])); if (!is_wp_error($t)) { $term_id = $t['term_id']; do_action('cherry_plugin_import_insert_term', $t, $term, $post_id, $post); } else { // Failed to import do_action('cherry_plugin_import_insert_term_failed', $t, $term, $post_id, $post); continue; } } $terms_to_set[$taxonomy][] = intval($term_id); } foreach ($terms_to_set as $tax => $ids) { $tt_ids = wp_set_post_terms($post_id, $ids, $tax); do_action('wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post); } unset($post['terms'], $terms_to_set); } if (!isset($post['comments'])) { $post['comments'] = array(); } $post['comments'] = apply_filters('wp_import_post_comments', $post['comments'], $post_id, $post); // add/update comments if (!empty($post['comments'])) { $num_comments = 0; $inserted_comments = array(); foreach ($post['comments'] as $comment) { $comment_id = $comment['comment_id']; $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; $newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); if (isset($processed_authors[$comment['comment_user_id']])) { $newcomments[$comment_id]['user_id'] = $processed_authors[$comment['comment_user_id']]; } } ksort($newcomments); foreach ($newcomments as $key => $comment) { // if this is a new post we can skip the comment_exists() check if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) { if (isset($inserted_comments[$comment['comment_parent']])) { $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; } $comment = wp_filter_comment($comment); $inserted_comments[$key] = wp_insert_comment($comment); do_action('cherry_plugin_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post); foreach ($comment['commentmeta'] as $meta) { $value = maybe_unserialize($meta['value']); add_comment_meta($inserted_comments[$key], $meta['key'], $value); } $num_comments++; } } unset($newcomments, $inserted_comments, $post['comments']); } if (!isset($post['postmeta'])) { $post['postmeta'] = array(); } $post['postmeta'] = apply_filters('wp_import_post_meta', $post['postmeta'], $post_id, $post); // add/update post meta if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = apply_filters('import_post_meta_key', $meta['key']); $value = false; if ('_edit_last' == $key) { if (isset($processed_authors[intval($meta['value'])])) { $value = $processed_authors[intval($meta['value'])]; } else { $key = false; } } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } ini_set('max_execution_time', -1); set_time_limit(0); add_post_meta($post_id, $key, $value); do_action('cherry_plugin_import_post_meta', $post_id, $key, $value); // if the post has a featured image, take note of this in case of remap if ('_thumbnail_id' == $key) { $_SESSION['featured_images'][$post_id] = (int) $value; } } } } } } } $_SESSION['attachment_posts'] = $attachment_posts; exit('import_menu_item'); }
function dsq_sync_comments($comments) { if (count($comments) < 1) { return; } global $wpdb; // user MUST be logged out during this process wp_set_current_user(0); // we need the thread_ids so we can map them to posts $thread_map = array(); foreach ($comments as $comment) { $thread_map[$comment->thread->id] = null; } $thread_ids = array_keys($thread_map); $threads_query = implode(', ', array_fill(0, count($thread_ids), '%s')); // add as many placeholders as needed $sql = "\r\n SELECT post_id, meta_value \r\n FROM {$wpdb->postmeta} \r\n WHERE meta_key = 'dsq_thread_id' AND meta_value IN (" . $threads_query . ")\r\n "; // Call $wpdb->prepare passing the values of the array as separate arguments $query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $thread_ids)); $results = $wpdb->get_results($query); foreach ($results as $result) { $thread_map[$result->meta_value] = $result->post_id; } unset($result); foreach ($comments as $comment) { $ts = strtotime($comment->created_at); if (!$thread_map[$comment->thread->id] && !empty($comment->thread->identifier)) { // legacy threads dont already have their meta stored foreach ($comment->thread->identifier as $identifier) { // we know identifier starts with post_ID if ($post_ID = (int) substr($identifier, 0, strpos($identifier, ' '))) { $thread_map[$comment->thread->id] = $post_ID; $cleaned_thread_id = sanitize_meta('dsq_thread_id', $comment->thread->id, 'post'); update_post_meta($post_ID, 'dsq_thread_id', $cleaned_thread_id); if (DISQUS_DEBUG) { echo "updated post {$post_ID}: dsq_thread_id set to {$comment->thread->id}\n"; } } } unset($identifier); } if (!$thread_map[$comment->thread->id]) { // shouldn't ever happen, but we can't be certain if (DISQUS_DEBUG) { if (!empty($comment->thread->identifier)) { $idents = implode(', ', $comment->thread->identifier); echo "skipped {$comment->id}: missing thread for identifiers ({$idents})\n"; } else { echo "skipped {$comment->id}: missing thread (no identifier)\n"; } } continue; } $results = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->id)); if (count($results)) { // already exists if (DISQUS_DEBUG) { echo "skipped {$comment->id}: comment already exists\n"; } if (count($results) > 1) { // clean up duplicates -- fixes an issue where a race condition allowed comments to be synced multiple times $results = array_slice($results, 1); foreach ($results as $result) { $wpdb->prepare("DELETE FROM {$wpdb->commentmeta} WHERE comment_id = %s LIMIT 1", $result); } } continue; } $commentdata = false; // first lets check by the id we have stored if ($comment->meta) { $meta = explode(';', $comment->meta); foreach ($meta as $value) { $value = explode('=', $value); $meta[$value[0]] = $value[1]; } unset($value); if ($meta['wp_id']) { $commentdata = $wpdb->get_row($wpdb->prepare("SELECT comment_ID, comment_parent FROM {$wpdb->comments} WHERE comment_ID = %s LIMIT 1", $meta['wp_id']), ARRAY_A); } } // skip comments that were imported but are missing meta information if (!$commentdata && $comment->imported) { if (DISQUS_DEBUG) { echo "skipped {$comment->id}: comment not found and marked as imported\n"; } continue; } // and follow up using legacy Disqus agent if (!$commentdata) { $commentdata = $wpdb->get_row($wpdb->prepare("SELECT comment_ID, comment_parent FROM {$wpdb->comments} WHERE comment_agent = %s LIMIT 1", 'Disqus/1.0:' . $comment->id), ARRAY_A); } if (!$commentdata) { // Comment doesnt exist yet, lets insert it if ($comment->status == 'approved') { $approved = 1; } elseif ($comment->status == 'spam') { $approved = 'spam'; } else { $approved = 0; } $commentdata = array('comment_post_ID' => $thread_map[$comment->thread->id], 'comment_date' => date('Y-m-d\\TH:i:s', strtotime($comment->created_at) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => $comment->created_at, 'comment_content' => apply_filters('pre_comment_content', $comment->message), 'comment_approved' => $approved, 'comment_agent' => 'Disqus/1.1(' . DISQUS_VERSION . '):' . intval($comment->id), 'comment_type' => ''); if ($comment->is_anonymous) { $commentdata['comment_author'] = $comment->anonymous_author->name; $commentdata['comment_author_email'] = $comment->anonymous_author->email; $commentdata['comment_author_url'] = $comment->anonymous_author->url; $commentdata['comment_author_IP'] = $comment->ip_address; } else { if (!empty($comment->author->display_name)) { $commentdata['comment_author'] = $comment->author->display_name; } else { $commentdata['comment_author'] = $comment->author->username; } $commentdata['comment_author_email'] = $comment->author->email; $commentdata['comment_author_url'] = $comment->author->url; $commentdata['comment_author_IP'] = $comment->ip_address; } $commentdata = wp_filter_comment($commentdata); if ($comment->parent_post) { $parent_id = $wpdb->get_var($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->parent_post)); if ($parent_id) { $commentdata['comment_parent'] = $parent_id; } } // due to a race condition we need to test again for coment existance if ($wpdb->get_row($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->id))) { // already exists if (DISQUS_DEBUG) { echo "skipped {$comment->id}: comment already exists (second check)\n"; } continue; } $commentdata['comment_ID'] = wp_insert_comment($commentdata); if (DISQUS_DEBUG) { echo "inserted {$comment->id}: id is {$commentdata[comment_ID]}\n"; } } if (isset($commentdata['comment_parent']) && !$commentdata['comment_parent'] && $comment->parent_post) { $parent_id = $wpdb->get_var($wpdb->prepare("SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->parent_post)); if ($parent_id) { $wpdb->query($wpdb->prepare("UPDATE {$wpdb->comments} SET comment_parent = %s WHERE comment_id = %s", $parent_id, $commentdata['comment_ID'])); if (DISQUS_DEBUG) { echo "updated {$comment->id}: comment_parent changed to {$parent_id}\n"; } } } $comment_id = $commentdata['comment_ID']; update_comment_meta($comment_id, 'dsq_parent_post_id', $comment->parent_post); update_comment_meta($comment_id, 'dsq_post_id', $comment->id); } unset($comment); if (isset($_POST['dsq_api_key']) && $_POST['dsq_api_key'] == get_option('disqus_api_key')) { if (isset($_GET['dsq_sync_action']) && isset($_GET['dsq_sync_comment_id'])) { $comment_parts = explode('=', $_GET['dsq_sync_comment_id']); if (!($comment_id = intval($comment_parts[1])) > 0) { return; } if ('wp_id' != $comment_parts[0]) { $comment_id = $wpdb->get_var($wpdb->prepare('SELECT comment_ID FROM ' . $wpdb->comments . ' WHERE comment_post_ID = %d AND comment_agent LIKE %s', intval($post->ID), 'Disqus/1.0:' . $comment_id)); } switch ($_GET['dsq_sync_action']) { case 'mark_spam': wp_set_comment_status($comment_id, 'spam'); echo "<!-- dsq_sync: wp_set_comment_status({$comment_id}, 'spam') -->"; break; case 'mark_approved': wp_set_comment_status($comment_id, 'approve'); echo "<!-- dsq_sync: wp_set_comment_status({$comment_id}, 'approve') -->"; break; case 'mark_killed': wp_set_comment_status($comment_id, 'hold'); echo "<!-- dsq_sync: wp_set_comment_status({$comment_id}, 'hold') -->"; break; } } } }
/** * Process and import comment data. * * @param array $comments List of comment data arrays. * @param int $post_id Post to associate with. * @param array $post Post data. * @return int|WP_Error Number of comments imported on success, error otherwise. */ protected function process_comments($comments, $post_id, $post) { // TODO: this should use the real value for delta updating $post_exists = false; $comments = apply_filters('wp_import_post_comments', $comments, $post_id, $post); if (empty($comments)) { return 0; } $num_comments = 0; // Sort by ID to avoid excessive remapping later usort($comments, array($this, 'sort_comments_by_id')); foreach ($comments as $key => $comment) { /** * Pre-process comment data * * @param array $comment Comment data. (Return empty to skip.) * @param int $post_id Post the comment is attached to. */ $comment = apply_filters('wxr_importer.pre_process.comment', $comment, $post_id); if (empty($comment)) { return false; } $original_id = isset($comment['comment_id']) ? (int) $comment['comment_id'] : 0; $parent_id = isset($comment['comment_parent']) ? (int) $comment['comment_parent'] : 0; $author_id = isset($comment['comment_user_id']) ? (int) $comment['comment_user_id'] : 0; // if this is a new post we can skip the comment_exists() check // TODO: Check comment_exists for performance if ($post_exists && ($existing = $this->comment_exists($comment))) { $this->mapping['comment'][$original_id] = $exists; continue; } // Remove meta from the main array $meta = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); unset($comment['commentmeta']); // Map the parent comment, or mark it as one we need to fix $requires_remapping = false; if ($parent_id) { if (isset($this->mapping['comment'][$parent_id])) { $comment['comment_parent'] = $this->mapping['comment'][$parent_id]; } else { // Prepare for remapping later $meta[] = array('key' => '_wxr_import_parent', 'value' => $parent_id); $requires_remapping = true; // Wipe the parent for now $comment['comment_parent'] = 0; } } // Map the author, or mark it as one we need to fix if ($author_id) { if (isset($this->mapping['user'][$author_id])) { $comment['user_id'] = $this->mapping['user'][$author_id]; } else { // Prepare for remapping later $meta[] = array('key' => '_wxr_import_user', 'value' => $author_id); $requires_remapping = true; // Wipe the user for now $comment['user_id'] = 0; } } // Run standard core filters $comment['comment_post_ID'] = $post_id; $comment = wp_filter_comment($comment); // wp_insert_comment expects slashed data $comment_id = wp_insert_comment(wp_slash($comment)); $this->mapping['comment'][$original_id] = $comment_id; if ($requires_remapping) { $this->requires_remapping['comment'][$comment_id] = true; } $this->mark_comment_exists($comment, $comment_id); /** * Comment has been imported. * * @param int $comment_id New comment ID * @param array $comment Comment inserted (`comment_id` item refers to the original ID) * @param int $post_id Post parent of the comment * @param array $post Post data */ do_action('wp_import_insert_comment', $comment_id, $comment, $post_id, $post); // Process the meta items foreach ($meta as $meta_item) { $value = maybe_unserialize($meta_item['value']); add_comment_meta($comment_id, wp_slash($meta_item['key']), wp_slash($value)); } /** * Post processing completed. * * @param int $post_id New post ID. * @param array $comment Raw data imported for the comment. * @param array $meta Raw meta data, already processed by {@see process_post_meta}. * @param array $post_id Parent post ID. */ do_action('wxr_importer.processed.comment', $comment_id, $comment, $meta, $post_id); $num_comments++; } return $num_comments; }
function import_comment($comment_arr) { // Parse this comment into an array and insert $comment = $this->parse_comment($comment_arr); $comment = wp_filter_comment($comment); // redo comment approval if (check_comment($comment['comment_author'], $comment['comment_author_email'], $comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'], $comment['comment_type'])) { $approved = 1; } else { $approved = 0; } if (wp_blacklist_check($comment['comment_author'], $comment['comment_author_email'], $comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'])) { $approved = 'spam'; } elseif ($this->askimet_spam_checker($comment)) { $approved = 'spam'; } // auto approve imported comments if (get_t3i_options('approve_comments') && $approved !== 'spam') { $approved = 1; } $comment['comment_approved'] = $approved; // Simple duplicate check $dupe = "\n\t\t\tSELECT comment_ID\n\t\t\tFROM {$this->wpdb->comments}\n\t\t\tWHERE comment_post_ID = '{$comment['comment_post_ID']}'\n\t\t\t\tAND comment_approved != 'trash'\n\t\t\t\tAND comment_author = '{$comment['comment_author']}'\n\t\t\t\tAND comment_author_email = '{$comment['comment_author_email']}'\n\t\t\t\tAND comment_content = '{$comment['comment_content']}'\n\t\t\tLIMIT 1\n\t\t"; $comment_ID = $this->wpdb->get_var($dupe); // echo '<li>'; if (!$comment_ID) { // printf( __( 'Imported comment from <strong>%s</strong>', 'typo3-importer'), stripslashes( $comment['comment_author'] ) ); $inserted = wp_insert_comment($comment); } else { // printf( __( 'Comment from <strong>%s</strong> already exists.', 'typo3-importer'), stripslashes( $comment['comment_author'] ) ); $inserted = false; } // echo '</li>'; // ob_flush(); flush(); return $inserted; }
function import_comment($entry) { global $importing_blog; // Drop the #fragment and we have the comment's old post permalink. foreach ($entry->links as $link) { if ($link['rel'] == 'alternate') { $parts = parse_url($link['href']); $entry->old_permalink = $parts['fragment']; $entry->old_post_permalink = $parts['path']; break; } } $comment_post_ID = (int) $this->blogs[$importing_blog]['posts'][$entry->old_post_permalink]; preg_match('#<name>(.+?)</name>.*(?:\\<uri>(.+?)</uri>)?#', $entry->author, $matches); $comment_author = addslashes($this->no_apos(strip_tags((string) $matches[1]))); $comment_author_url = addslashes($this->no_apos(strip_tags((string) $matches[2]))); $comment_date = $this->convert_date($entry->updated); $comment_content = addslashes($this->no_apos(@html_entity_decode($entry->content, ENT_COMPAT, get_option('blog_charset')))); // Clean up content $comment_content = preg_replace_callback('|<(/?[A-Z]+)|', array(&$this, '_normalize_tag'), $comment_content); $comment_content = str_replace('<br>', '<br />', $comment_content); $comment_content = str_replace('<hr>', '<hr />', $comment_content); // Checks for duplicates if (isset($this->blogs[$importing_blog]['comments'][$entry->old_permalink]) || comment_exists($comment_author, $comment_date)) { ++$this->blogs[$importing_blog]['comments_skipped']; } else { $comment = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_date', 'comment_content'); $comment = wp_filter_comment($comment); $comment_id = wp_insert_comment($comment); $this->blogs[$importing_blog]['comments'][$entry->old_permalink] = $comment_id; ++$this->blogs[$importing_blog]['comments_done']; } $this->save_vars(); }
public static function set_last_comment($comment) { if (is_null($comment)) { self::$last_comment = null; } else { // We filter it here so that it matches the filtered comment data that we'll have to compare against later. // wp_filter_comment expects comment_author_IP self::$last_comment = wp_filter_comment(array_merge(array('comment_author_IP' => self::get_ip_address()), $comment)); } }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * @uses $wpdb * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object * * @param array $commentarr Contains information on the comment. * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); // Escape data pulled from DB. foreach ((array) $comment as $key => $value) { $comment[$key] = $wpdb->escape($value); } // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. extract(stripslashes_deep($commentarr), EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); if (!isset($comment_approved)) { $comment_approved = 1; } else { if ('hold' == $comment_approved) { $comment_approved = 0; } else { if ('approve' == $comment_approved) { $comment_approved = 1; } } } $wpdb->query($wpdb->prepare("UPDATE {$wpdb->comments} SET\n\t\t\tcomment_content = %s,\n\t\t\tcomment_author = %s,\n\t\t\tcomment_author_email = %s,\n\t\t\tcomment_approved = %s,\n\t\t\tcomment_author_url = %s,\n\t\t\tcomment_date = %s,\n\t\t\tcomment_date_gmt = %s\n\t\tWHERE comment_ID = %d", $comment_content, $comment_author, $comment_author_email, $comment_approved, $comment_author_url, $comment_date, $comment_date_gmt, $comment_ID)); $rval = $wpdb->rows_affected; clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment_approved, $comment->comment_approved, $comment); return $rval; }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $commentarr Contains information on the comment. * @return int Comment was updated if value is 1, or was not updated if value is 0. */ function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); if (empty($comment)) { return 0; } // Make sure that the comment post ID is valid (if specified). if (isset($commentarr['comment_post_ID']) && !get_post($commentarr['comment_post_ID'])) { return 0; } // Escape data pulled from DB. $comment = wp_slash($comment); $old_status = $comment['comment_approved']; // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment($commentarr); // Now extract the merged array. $data = wp_unslash($commentarr); /** * Filter the comment content before it is updated in the database. * * @since 1.5.0 * * @param string $comment_content The comment data. */ $data['comment_content'] = apply_filters('comment_save_pre', $data['comment_content']); $data['comment_date_gmt'] = get_gmt_from_date($data['comment_date']); if (!isset($data['comment_approved'])) { $data['comment_approved'] = 1; } elseif ('hold' == $data['comment_approved']) { $data['comment_approved'] = 0; } elseif ('approve' == $data['comment_approved']) { $data['comment_approved'] = 1; } $comment_ID = $data['comment_ID']; $comment_post_ID = $data['comment_post_ID']; $keys = array('comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id'); $data = wp_array_slice_assoc($data, $keys); $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); /** * Fires immediately after a comment is updated in the database. * * The hook also fires immediately before comment status transition hooks are fired. * * @since 1.2.0 * * @param int $comment_ID The comment ID. */ do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment->comment_approved, $old_status, $comment); return $rval; }
function save_post(&$post, &$comments, &$pings) { // Reset the counter set_time_limit(30); $post = get_object_vars($post); $post = add_magic_quotes($post); $post = (object) $post; if ($post_id = post_exists($post->post_title, '', $post->post_date)) { echo '<li>'; printf(__('Post <em>%s</em> already exists.'), stripslashes($post->post_title)); } else { echo '<li>'; printf(__('Importing post <em>%s</em>...'), stripslashes($post->post_title)); if ('' != trim($post->extended)) { $post->post_content .= "\n<!--more-->\n{$post->extended}"; } $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor $post_id = wp_insert_post($post); if (is_wp_error($post_id)) { return $post_id; } // Add categories. if (0 != count($post->categories)) { wp_create_categories($post->categories, $post_id); } // Add tags or keywords if (1 < strlen($post->post_keywords)) { // Keywords exist. printf(__('<br />Adding tags <i>%s</i>...'), stripslashes($post->post_keywords)); wp_add_post_tags($post_id, $post->post_keywords); } } $num_comments = 0; foreach ($comments as $comment) { $comment = get_object_vars($comment); $comment = add_magic_quotes($comment); if (!comment_exists($comment['comment_author'], $comment['comment_date'])) { $comment['comment_post_ID'] = $post_id; $comment = wp_filter_comment($comment); wp_insert_comment($comment); $num_comments++; } } if ($num_comments) { printf(' ' . __ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments); } $num_pings = 0; foreach ($pings as $ping) { $ping = get_object_vars($ping); $ping = add_magic_quotes($ping); if (!comment_exists($ping['comment_author'], $ping['comment_date'])) { $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}"; $ping['comment_post_ID'] = $post_id; $ping = wp_filter_comment($ping); wp_insert_comment($ping); $num_pings++; } } if ($num_pings) { printf(' ' . __ngettext('(%s ping)', '(%s pings)', $num_pings), $num_pings); } echo "</li>"; //ob_flush();flush(); }
/** * Create new posts based on import information * * Posts marked as having a parent which doesn't exist will become top level items. * Doesn't create a new post if: the post type doesn't exist, the given post ID * is already noted as imported or a post with the same title and date already exists. * Note that new/updated terms, comments and meta are imported for the last of the above. */ function process_posts() { foreach ($this->posts as $post) { if (!post_type_exists($post['post_type'])) { printf(__('Failed to import “%s”: Invalid post type %s', 'radium'), esc_html($post['post_title']), esc_html($post['post_type'])); echo '<br />'; continue; } if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) { continue; } if ($post['status'] == 'auto-draft') { continue; } if ('nav_menu_item' == $post['post_type']) { $this->process_menu_item($post); continue; } $post_type_object = get_post_type_object($post['post_type']); $post_exists = post_exists($post['post_title'], '', $post['post_date']); if ($post_exists && get_post_type($post_exists) == $post['post_type']) { printf(__('%s “%s” already exists.', 'radium'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); echo '<br />'; $comment_post_ID = $post_id = $post_exists; } else { $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($this->processed_posts[$post_parent])) { $post_parent = $this->processed_posts[$post_parent]; // otherwise record the parent for later } else { $this->post_orphans[intval($post['post_id'])] = $post_parent; $post_parent = 0; } } // map the post author $author = sanitize_user($post['post_author'], true); if (isset($this->author_mapping[$author])) { $author = $this->author_mapping[$author]; } else { $author = (int) get_current_user_id(); } $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); if ('attachment' == $postdata['post_type']) { $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; // try to use _wp_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata['upload_date'] = $post['post_date']; if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { if ($meta['key'] == '_wp_attached_file') { if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { $postdata['upload_date'] = $matches[0]; } break; } } } $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url); } else { $comment_post_ID = $post_id = wp_insert_post($postdata, true); } if (is_wp_error($post_id)) { printf(__('Failed to import %s “%s”', 'radium'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $post_id->get_error_message(); } echo '<br />'; continue; } if ($post['is_sticky'] == 1) { stick_post($post_id); } } // map pre-import ID to local ID $this->processed_posts[intval($post['post_id'])] = (int) $post_id; // add categories, tags and other terms if (!empty($post['terms'])) { $terms_to_set = array(); foreach ($post['terms'] as $term) { // back compat with WXR 1.0 map 'tag' to 'post_tag' $taxonomy = 'tag' == $term['domain'] ? 'post_tag' : $term['domain']; $term_exists = term_exists($term['slug'], $taxonomy); $term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists; if (!$term_id) { $t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug'])); if (!is_wp_error($t)) { $term_id = $t['term_id']; } else { printf(__('Failed to import %s %s', 'radium'), esc_html($taxonomy), esc_html($term['name'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $t->get_error_message(); } echo '<br />'; continue; } } $terms_to_set[$taxonomy][] = intval($term_id); } foreach ($terms_to_set as $tax => $ids) { $tt_ids = wp_set_post_terms($post_id, $ids, $tax); } unset($post['terms'], $terms_to_set); } // add/update comments if (!empty($post['comments'])) { $num_comments = 0; $inserted_comments = array(); foreach ($post['comments'] as $comment) { $comment_id = $comment['comment_id']; $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; $newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); if (isset($this->processed_authors[$comment['comment_user_id']])) { $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; } } ksort($newcomments); foreach ($newcomments as $key => $comment) { // if this is a new post we can skip the comment_exists() check if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) { if (isset($inserted_comments[$comment['comment_parent']])) { $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; } $comment = wp_filter_comment($comment); $inserted_comments[$key] = wp_insert_comment($comment); foreach ($comment['commentmeta'] as $meta) { $value = maybe_unserialize($meta['value']); add_comment_meta($inserted_comments[$key], $meta['key'], $value); } $num_comments++; } } unset($newcomments, $inserted_comments, $post['comments']); } // add/update post meta if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = apply_filters('import_post_meta_key', $meta['key']); $value = false; if ('_edit_last' == $key) { if (isset($this->processed_authors[intval($meta['value'])])) { $value = $this->processed_authors[intval($meta['value'])]; } else { $key = false; } } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } add_post_meta($post_id, $key, $value); do_action('import_post_meta', $post_id, $key, $value); // if the post has a featured image, take note of this in case of remap if ('_thumbnail_id' == $key) { $this->featured_images[$post_id] = (int) $value; } } } } } unset($this->posts); }
function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); // Escape data pulled from DB. foreach ($comment as $key => $value) $comment[$key] = $wpdb->escape($value); // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment( $commentarr ); // Now extract the merged array. extract($commentarr); $comment_content = apply_filters('comment_save_pre', $comment_content); $result = $wpdb->query( "UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author', comment_author_email = '$comment_author_email', comment_approved = '$comment_approved', comment_author_url = '$comment_author_url', comment_date = '$comment_date' WHERE comment_ID = $comment_ID" ); $rval = $wpdb->rows_affected; $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); if( is_object( $c ) ) $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); do_action('edit_comment', $comment_ID); return $rval; }