posts A"; $ids = $wpdb->get_col($thisquery); $permalinks = array(); foreach ($ids as $id) { $permalinks[get_permalink($id)] = $id; } } return $permalinks[$url]; } function import_facebook_page($notesurl, $verbose = false, $allpages = false, $start = 0) { $challenge = md5($time); // This is just a random 32-digit hex number. $test_run = $_POST['test_run']; $notespage = facebook_get_url($notesurl . ($start == 0 ? '' : "&start=$start"), $challenge, $start); $dontimport = get_option('facebook_dont_import'); //
  • Next
  • $notenumber = 0; $url2 = ""; $lines = explode("\n", $notespage); //0.34 use default URL $defaultURL = get_option('facebook_default_url'); // This was originally written in Perl, which is why there are so many bloody regexes everywhere. foreach ($lines as $line) { //James 0.21 removed space from note title if ( strstr($line, '
    ') ) { // Get the title, original URL, and Facebook URL for each note $notenumber = $notenumber + 1; //James 0.21 removed space from note title $line = preg_replace("/.*?
    /", '($1)', $line); $line = preg_replace("/<\/a><\/div>.*/", "", $line); $tmp = preg_split("/[()]/", $line); $url[$notenumber] = $tmp[1]; $line = preg_replace("/\(.*?\)/", "", $line); $title[$notenumber] = $line; $tmp = explode("?", $url[$notenumber]); $url2 = $tmp[1]; } elseif ( strstr($line, "note_redirect.php?") && strstr($line, "View Original Post") && $wpURL[$notenumber] == "" ) { $line3 = preg_replace("/.*?note_redirect.php?([^\"]+)\".*/", "$1", $line); $line3 = urldecode($line3); //$wpURL[$notenumber] = preg_replace("/.*?url=([^\"]+)\&h.*/", "", $line3; $wpURL[$notenumber] = preg_replace("/.*?url=/", "", $line3); // the following line was adding by Aaron Harp to get the real URL if it's pointing to a Feedburner one if (strstr($wpURL[$notenumber], 'feedburner.com')) { $wpURL[$notenumber] = get_real_url($wpURL[$notenumber]); } if ($verbose) { print "Checking for comments on $wpURL[$notenumber]...
    \n"; } } //0.26 This line had to be changed, because splitting the lines by a line feed stopped working. //elseif ( preg_match("/#comments\">[0-9]+ comment/", $line) ) { //if ( preg_match("/#comments\">[0-9]+ comment/", $line) ) { //0.30 change to new comment matching if ( preg_match('/>Comment<\/a>/', $line) ) { // This entry has comments. Perhaps we should follow its link! $getcomments[$notenumber] = "true"; $l2 = facebook_get_url("http://facebook.com/".$url[$notenumber], $challenge); //James: Facebook now seems to not have line feeds, so split by sometime else //$l2 = explode("\n", $l2); $l2 = explode('
    ', $l2); foreach ($l2 as $line2) { //James: Facebook now seems to have more than one space after the profile link //if ( preg_match ("/class=\"profile_link\" >.*? wrote/", $line2) ) { //if ( preg_match ("/class=\"profile_link\" *>.*? wrote/", $line2) ) { //0.30 no longer contains profile_link if ( preg_match ('/class="feed_comment_pic"/', $line2) ) { $lines2 = explode("/td>.*? wrote/", $l3) ) { //if ( preg_match ("/class=\"profile_link\" *>.*? wrote/", $l3) ) { //0.30 if ( preg_match ('/class="feed_comment_pic" *\/><\/a>/', $l3) ) { $l3 = str_replace("\n", "", $l3); //0.34 Allow for a default url for the author if you don't want to point to facebook profile switch($defaultURL) { case null: if (preg_match("/.*.*/",$l3)) { $comment_author_url = preg_replace("/.*.*/", '$1', $l3); } else { $comment_author_url = preg_replace("/.*.*/", '$1', $l3); } break; default: $comment_author_url = get_option('facebook_default_url'); } if (preg_match("/.*([^<]+)<.*/",$l3)) { $comment_author = preg_replace("/.*([^<]+)<.*/", '$1', $l3); } else { //$comment_author = preg_replace("/.*([^<]+)<.*/", '$1', $l3); //0.34 update to ensure comment_author is always set even for the last comment on a post $comment_author = preg_replace("/.*<\/span>(.*?)<\/a>.*/", '$1', $l3); } $when = preg_replace('/.*(.*?)<\/span>.*/', '$1', $l3); //Need to check this //0.34 update $comment_content = preg_replace("/.*class=\"wall_actual_text\">(.*?)<\/div>.*/", '$1', $l3); $comment_author = $comment_author . " " . get_option('facebook_add_to_author'); $when = str_replace("at ", "", $when); $when = str_replace("on ", "", $when); $epochtime = strtotime($when); // Check if this comment exists already $addcomment = 1; global $wpdb; //$thisquery = "SELECT A.ID FROM $wpdb->posts A WHERE A.guid LIKE '$wpURL[$notenumber]'"; //$comment_post_ID = $wpdb->get_var($thisquery); if ($wpURL[$notenumber] == '') { print "    Error, wordpress URL for post not stored... "; } else { $comment_post_ID = facebook_get_postid($wpURL[$notenumber]); if ($comment_post_ID == 0 || $comment_post_ID == '') { print "    Error, original comment post $comment_post_ID not found... "; } else { $continue = true; if ($dontimport) { $comment_post = get_post($comment_post_ID); $continue = $comment_post->comment_status != 'closed'; } if ($verbose) { print "    Comment found for post $comment_post_ID... "; } if ($continue) { $doescommentexist = $wpdb->get_var("SELECT count(A.comment_post_ID) FROM $wpdb->comments A WHERE A.comment_post_ID = '$comment_post_ID' AND A.comment_approved = '1' AND A.comment_author LIKE '$comment_author'"); if ($doescommentexist) { // All that the above SQL tells us is that this person already has a comment for this entry in the database. However, we don't know whether they commented more than once on this particular note/entry. So, we've got a bit more work to do. //James 0.21 this was broken in the original, get_row only returns one row, so it wouldn't work if someone comments more than once. //$thesecomments = $wpdb->get_row("SELECT A.comment_content FROM $wpdb->comments A WHERE A.comment_post_ID = '$comment_post_ID' AND A.comment_approved = '1' AND A.comment_author LIKE '$comment_author'", ARRAY_N); $thesecomments = $wpdb->get_col("SELECT A.comment_content FROM $wpdb->comments A WHERE A.comment_post_ID = '$comment_post_ID' AND A.comment_approved = '1' AND A.comment_author LIKE '$comment_author'",0); foreach ($thesecomments as $thiscomment) { // We want to strip down the comments, both from the database and from Facebook, to the bare minimum and then compare them. So no HTML, no whitespace, no anything that isn't A-Z, 0-9, or underscore. $tc = urldecode($thiscomment); $tc = preg_replace("/<[^>]*>/", "", $tc); $tc = preg_replace("/\W/", "", $tc); $fc = urldecode($comment_content); $fc = preg_replace("/<[^>]*>/", "", $fc); $fc = preg_replace("/\W/", "", $fc); if (strcmp($tc, $fc) == 0) { // If these are the same comment, don't add it again. if ($verbose) { print "already exists in Wordpress database."; } $addcomment = 0; } } } if ($addcomment == 1) { print "not in Wordpress database. "; if ($test_run) { if ($verbose) { print "Test run, skipping add..."; } } else { if ($verbose) { print "Adding to Wordpress..."; } // Insert the comment if (get_option('facebook_fake_author_email')) { $comment_author_email = get_option('facebook_fake_author_email'); } else { $comment_author_email = $email; } $comment_date = date("Y-m-d H:i:s", $epochtime); $comment_date_gmt = gmdate("Y-m-d H:i:s", $epochtime); $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_date'); $comment_ID = facebook_new_comment ($commentdata); if ($verbose) { print "OK!"; } } } // Done adding the comment now. } else { if ($verbose) { print "Comments closed for this post, skipping."; } } } } if ($verbose) { print "
    \n"; } } // end "if line contains profile_link" } // next table cell } // end "if line contains profile_link" } // next line of file } // Done with this note } // Next line of file if ($allpages) { if ( preg_match("/notes.php?.*\">Next/", $notespage) ) { if ($verbose) { print "
    Getting next page...
    \n"; } import_facebook_page($notesurl, $verbose, $allpages, $start+10); } } } function import_facebook_comments() { // Start buffering ob_start(); import_facebook_page(get_option('facebook_notesURL'), get_option('facebook_plugin_verbose'), get_option('facebook_all_pages')); // Turn off buffering ob_end_flush(); return; } function facebook_activate() { // Add options add_option('facebook_add_to_author', " (via Facebook)", "Text to append to the comment author's name"); //0.34 add ability to add default author url add_option('facebook_default_url', " (via Facebook)", "Link to use instead of the facebook profile link"); add_option('facebook_auto_freq', "86400", "How often to automatically check Facebook to import comments"); // All other options are empty by default and so we'll add them when they have data, later. // Schedule if (!wp_next_scheduled('import_facebook_comments_hook')) { $freq = get_option('facebook_auto_freq'); if ($freq == 86400) { // Daily wp_schedule_event( time(), 'daily', 'import_facebook_comments_hook' ); } elseif ($freq == 3600) { // Hourly wp_schedule_event( time(), 'hourly', 'import_facebook_comments_hook' ); } else { // Turn off automatic checking. } } } function get_real_url($url) { $ch = curl_init(); // initialize curl handle curl_setopt($ch, CURLOPT_URL, $url); // set url to post to curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s if (!curl_exec($ch)) { return false; } return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); } function get_facebook_auth() { add_submenu_page("options-general.php", "Facebook Comments", "Facebook Comments", 5, "facebooknotes.php", facebook_options_subpanel); } function facebook_options_subpanel() { if (! get_option('facebook_add_to_author')) { } if (isset($_POST['manual_run']) || isset($_POST['test_run'])) { update_option('facebook_plugin_verbose', '1'); print '

    '; print "Manually importing comments...

    \n"; import_facebook_comments(); print '

    '; update_option('facebook_plugin_verbose', ''); } if (isset($_POST['info_update'])) {?>