/** * Register plugin * * @uses _btc_url_open() * @uses _btc_xml_unserialize() * * @return bool True if successful; false otherwise */ function btc_register_plugin() { $processed = false; $params = array('identifier' => 'btc', 'url' => get_option('siteurl')); btc_log('Registering plugin: ' . BTC_API_REGISTER_URL . '?' . http_build_query($params, null, '&'), 'debug'); if (!($contents = _btc_url_open(BTC_API_REGISTER_URL . '?' . http_build_query($params, null, '&')))) { return false; } $xml = _btc_xml_unserialize($contents['body']); unset($contents); if (class_exists('SimpleXMLElement')) { if (!empty($xml->key)) { add_option(BTC_API_KEY_OPTION, trim($xml->key)); $processed = true; } } else { if (!empty($xml['feed']['key'])) { add_option(BTC_API_KEY_OPTION, trim($xml['feed']['key'])); $processed = true; } } return $processed; }
/** * BackType Connect settings page * * @uses btc_db_clear_comment_counts() * @uses btc_db_get_own_blog_comments() * @uses btc_db_get_retweets() * @uses btc_db_update_filtered() * @uses btc_db_update_ignored() * @uses btc_disable_plugin() * @uses btc_enable_plugin() * @uses _btc_display_message() * @uses _btc_url_open() */ function btc_settings_submenu() { if (!current_user_can('manage_options')) { die; } if (isset($_POST[BTC_API_KEY_OPTION])) { $error = false; $authenticated = false; $tries = 0; $processed = true; do { $btc_api_key = $_POST[BTC_API_KEY_OPTION]; if ($btc_api_key != get_option(BTC_API_KEY_OPTION)) { // authenticate $url = get_option('siteurl'); $params = array('identifier' => 'btc', 'key' => $btc_api_key, 'url' => $url); if (!($contents = _btc_url_open(BTC_API_PING_URL . '?' . http_build_query($params, null, '&')))) { $tries++; } else { $xml = _btc_xml_unserialize($contents['body']); unset($contents); if (class_exists('SimpleXMLElement')) { if (!empty($xml->errorCode)) { $error = 'An error occured authenticating with BackType Connect. Please contact <a href="mailto:support@backtype.com">support@backtype.com</a>'; break; } } else { if (!empty($xml['error']['errorCode'])) { $error = 'An error occured authenticating with BackType Connect. Please contact <a href="mailto:support@backtype.com">support@backtype.com</a>'; break; } } update_option(BTC_API_KEY_OPTION, $btc_api_key); $authenticated = true; $processed = true; break; } } else { $authenticated = true; $processed = true; break; } if ($tries == 3) { $error = 'Could not contact BackType, are you sure your webserver is online? If so contact <a href="mailto:support@backtype.com">support@backtype.com</a>'; } } while (!$processed && !$error); if ($authenticated) { if ($_POST['control'] == 1) { require_once BTC_DIR . '/install.php'; if (get_option(BTC_ENABLED_OPTION)) { btc_disable_plugin(); _btc_display_message(__('BackType Connect is now disabled.'), 'success'); } else { btc_enable_plugin(); _btc_display_message(__('BackType Connect is now enabled.'), 'success'); } } else { require_once BTC_DIR . '/db.php'; $btc_comment_sort = isset($_POST[BTC_COMMENT_SORT_OPTION]) && in_array($_POST[BTC_COMMENT_SORT_OPTION], array('mixed', 'end')) ? $_POST[BTC_COMMENT_SORT_OPTION] : 'mixed'; $btc_summary = isset($_POST[BTC_SUMMARY_OPTION]) ? true : false; $btc_more_comments = isset($_POST[BTC_MORE_COMMENTS_OPTION]) ? true : false; $btc_ignore_own_blog = isset($_POST[BTC_IGNORE_OWN_BLOG_OPTION]) ? true : false; $btc_ignore_retweets = isset($_POST[BTC_IGNORE_RETWEETS_OPTION]) ? true : false; $btc_moderation = isset($_POST[BTC_MODERATION_OPTION]) ? true : false; $btc_akismet = isset($_POST[BTC_AKISMET_OPTION]) ? true : false; update_option(BTC_COMMENT_SORT_OPTION, $btc_comment_sort); update_option(BTC_SUMMARY_OPTION, $btc_summary); update_option(BTC_MORE_COMMENTS_OPTION, $btc_more_comments); $clear_btc_counts = false; if ($btc_ignore_own_blog != get_option(BTC_IGNORE_OWN_BLOG_OPTION)) { $clear_btc_counts = true; $btc_own_blog_comments = btc_db_get_own_blog_comments(); btc_db_update_ignored($btc_ignore_own_blog, $btc_own_blog_comments); update_option(BTC_IGNORE_OWN_BLOG_OPTION, $btc_ignore_own_blog); } if ($btc_ignore_retweets != get_option(BTC_IGNORE_RETWEETS_OPTION)) { $clear_btc_counts = true; $btc_retweets = btc_db_get_retweets(); btc_db_update_ignored($btc_ignore_retweets, $btc_retweets); update_option(BTC_IGNORE_RETWEETS_OPTION, $btc_ignore_retweets); } update_option(BTC_MODERATION_OPTION, $btc_moderation); update_option(BTC_AKISMET_OPTION, $btc_akismet); $options = array(BTC_OPTION_DISABLED, BTC_OPTION_ENABLED); $btc_src_blog = isset($_POST[BTC_SRC_BLOG_OPTION]) && in_array((int) $_POST[BTC_SRC_BLOG_OPTION], $options) ? (int) $_POST[BTC_SRC_BLOG_OPTION] : BTC_OPTION_DISABLED; $btc_src_digg = isset($_POST[BTC_SRC_DIGG_OPTION]) && in_array((int) $_POST[BTC_SRC_DIGG_OPTION], $options) ? (int) $_POST[BTC_SRC_DIGG_OPTION] : BTC_OPTION_DISABLED; $btc_src_reddit = isset($_POST[BTC_SRC_REDDIT_OPTION]) && in_array((int) $_POST[BTC_SRC_REDDIT_OPTION], $options) ? (int) $_POST[BTC_SRC_REDDIT_OPTION] : BTC_OPTION_DISABLED; $btc_src_friendfeed = isset($_POST[BTC_SRC_FRIENDFEED_OPTION]) && in_array((int) $_POST[BTC_SRC_FRIENDFEED_OPTION], $options) ? (int) $_POST[BTC_SRC_FRIENDFEED_OPTION] : BTC_OPTION_DISABLED; $btc_src_yc = isset($_POST[BTC_SRC_YC_OPTION]) && in_array((int) $_POST[BTC_SRC_YC_OPTION], $options) ? (int) $_POST[BTC_SRC_YC_OPTION] : BTC_OPTION_DISABLED; $btc_src_twitter = isset($_POST[BTC_SRC_TWITTER_OPTION]) && in_array((int) $_POST[BTC_SRC_TWITTER_OPTION], $options) ? (int) $_POST[BTC_SRC_TWITTER_OPTION] : BTC_OPTION_DISABLED; // update filters if ($btc_src_blog != get_option(BTC_SRC_BLOG_OPTION)) { $clear_btc_counts = true; btc_db_update_filtered('btc_blog', $btc_src_blog); update_option(BTC_SRC_BLOG_OPTION, $btc_src_blog); } if ($btc_src_digg != get_option(BTC_SRC_DIGG_OPTION)) { $clear_btc_counts = true; btc_db_update_filtered('btc_digg', $btc_src_digg); update_option(BTC_SRC_DIGG_OPTION, $btc_src_digg); } if ($btc_src_reddit != get_option(BTC_SRC_REDDIT_OPTION)) { $clear_btc_counts = true; btc_db_update_filtered('btc_reddit', $btc_src_reddit); update_option(BTC_SRC_REDDIT_OPTION, $btc_src_reddit); } if ($btc_src_friendfeed != get_option(BTC_SRC_FRIENDFEED_OPTION)) { $clear_btc_counts = true; btc_db_update_filtered('btc_friendfeed', $btc_src_friendfeed); update_option(BTC_SRC_FRIENDFEED_OPTION, $btc_src_friendfeed); } if ($btc_src_yc != get_option(BTC_SRC_YC_OPTION)) { $clear_btc_counts = true; btc_db_update_filtered('btc_yc', $btc_src_yc); update_option(BTC_SRC_YC_OPTION, $btc_src_yc); } if ($btc_src_twitter != get_option(BTC_SRC_TWITTER_OPTION)) { $clear_btc_counts = true; btc_db_update_filtered('btc_twitter', $btc_src_twitter); update_option(BTC_SRC_TWITTER_OPTION, $btc_src_twitter); } if ($clear_btc_counts) { btc_db_clear_comment_counts(); } _btc_display_message(__('Settings updated.'), 'success'); } } if ($error) { _btc_display_message(__($error), 'error'); } } include BTC_DIR . '/admin-settings.php'; }
/** * Calls BackType API and parses response * * @uses btc_db_comment_counts() * @uses btc_db_dupe_comment() * @uses btc_db_add_comment_type() * @uses btc_db_add_own_blog_comment() * @uses btc_db_add_retweet() * @uses btc_db_set_comment_counts() * @uses btc_db_update_post() * @uses _btc_akismet_check_comment() * @uses _btc_comment_desc() * @uses _btc_filter_content() * @uses _btc_get_filters() * @uses _btc_url_open() * @uses _btc_xml_unserialize() * * @param object $btc_post Post to call API for * @return array Status */ function _btc_import(&$btc_post) { $filters = _btc_get_filters(); $page = 1; $processed = false; $error = 0; $url = get_permalink($btc_post->ID); $itemsperpage = '25'; $params = array('identifier' => 'btc', 'url' => $url, 'key' => get_option(BTC_API_KEY_OPTION), 'page' => '1', 'sort' => '1', 'itemsperpage' => $itemsperpage); do { btc_log('Fetching: ' . BTC_API_CONNECT_URL . '?' . http_build_query($params, null, '&'), 'debug'); if (!($contents = _btc_url_open(BTC_API_CONNECT_URL . '?' . http_build_query($params, null, '&')))) { btc_log('Failed opening url: ' . BTC_API_CONNECT_URL . '?' . http_build_query($params, null, '&'), 'error'); $error = 1; break; } $xml = _btc_xml_unserialize($contents['body']); unset($contents); if (class_exists('SimpleXMLElement')) { if (!$xml || !empty($xml->errorCode) || empty($xml->comments)) { if (!empty($xml->errorCode) && $xml->errorCode == 'related-conversations-not-found' || (int) $xml->totalresults == 0) { $btc_post->last_import_date_gmt = gmdate('Y-m-d H:i:s'); $btc_post->misses = (int) $btc_post->misses + 1; btc_db_update_post($btc_post); btc_log('No related conversations.', 'error'); $error = 2; } else { if ($xml && empty($xml->comments->entry) && (int) $xml->startindex > (int) $xml->totalresults) { btc_log('Paginated too far.', 'error'); //.. } else { btc_log('Unknown XML error.', 'error'); $error = 3; } } break; } if (empty($xml->comments->entry) || !count($xml->comments->entry)) { btc_log('No comments.', 'debug'); break; } require_once BTC_DIR . '/db.php'; $new = 0; btc_log('Comments received: ' . count($xml->comments->entry), 'debug'); $comments = array(); $ignore_own_blog = get_option(BTC_IGNORE_OWN_BLOG_OPTION); $ignore_retweets = get_option(BTC_IGNORE_RETWEETS_OPTION); $siteurl = get_option('siteurl'); $site_pieces = parse_url($siteurl); $site_domain = isset($site_pieces['host']) ? trim($site_pieces['host'], '/') : ''; $site_path = isset($site_pieces['path']) ? trim($site_pieces['path'], '/') : ''; foreach ($xml->comments->entry as $entry) { if ($entry['type'] == 'comment') { $entry_pieces = parse_url($entry->blog->url); $entry_domain = isset($entry_pieces['host']) ? trim($entry_pieces['host'], '/') : ''; $entry_path = isset($entry_pieces['path']) ? trim($entry_pieces['path'], '/') : ''; $is_own_blog = $site_domain == $entry_domain && $site_path == $entry_path ? 1 : 0; $is_retweet = 0; $commentdata = array('comment_post_ID' => $btc_post->ID, 'comment_author' => $entry->author->name ? trim($entry->author->name) : 'Anonymous', 'comment_author_email' => '', 'comment_author_url' => trim($entry->author->url), 'comment_author_IP' => '', 'comment_date' => date('Y-m-d H:i:s', strtotime(trim($entry->comment->date)) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => date('Y-m-d H:i:s', strtotime(trim($entry->comment->date))), 'comment_content' => _btc_filter_content($entry->comment->content), 'comment_approved' => $ignore_own_blog && $is_own_blog ? 'ignored' : (get_option(BTC_MODERATION_OPTION) ? '0' : '1'), 'comment_agent' => 'btc_' . $entry['src'], 'comment_type' => '', 'comment_parent' => ''); $desc = array('comment_src' => $entry['src'], 'site_url' => trim($entry->blog->url), 'site_title' => trim($entry->blog->title), 'post_url' => trim($entry->post->url), 'post_title' => $entry->post->title ? trim($entry->post->title) : trim($entry->blog->title), 'comment_url' => trim($entry->comment->url)); } elseif ($entry['type'] == 'tweet') { $is_own_blog = 0; $is_retweet = substr(strtolower($entry->tweet_text), 0, 2) == 'rt' ? 1 : 0; $commentdata = array('comment_post_ID' => $btc_post->ID, 'comment_author' => trim($entry->tweet_from_user), 'comment_author_email' => '', 'comment_author_url' => 'http://twitter.com/' . trim($entry->tweet_from_user), 'comment_author_IP' => '', 'comment_date' => date('Y-m-d H:i:s', strtotime(trim($entry->tweet_created_at)) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => date('Y-m-d H:i:s', strtotime(trim($entry->tweet_created_at))), 'comment_content' => _btc_filter_content($entry->tweet_text), 'comment_approved' => $ignore_retweets && $is_retweet ? 'ignored' : (get_option(BTC_MODERATION_OPTION) ? '0' : '1'), 'comment_agent' => 'btc_twitter', 'comment_type' => '', 'comment_parent' => ''); $desc = array('comment_src' => 'twitter', 'site_url' => 'http://twitter.com/', 'site_title' => 'Twitter', 'post_url' => 'http://twitter.com/' . trim($entry->tweet_from_user) . '/statuses/' . $entry->tweet_id, 'post_title' => trim($entry->tweet_text), 'comment_url' => 'http://twitter.com/' . trim($entry->tweet_from_user) . '/statuses/' . $entry->tweet_id); } $commentdata['comment_content'] .= _btc_comment_desc($desc); if (!btc_db_dupe_comment($commentdata)) { if (get_option(BTC_AKISMET_OPTION) && function_exists('akismet_auto_check_comment')) { $commentdata = _btc_akismet_check_comment($commentdata); } if (in_array($desc['comment_src'], $filters['srcs'])) { $commentdata['comment_approved'] = 'd_' . $commentdata['comment_approved']; } if ($comment_ID = wp_insert_comment($commentdata)) { $dupes = btc_db_dupe_clean($comment_ID); if (count($dupes) > 0) { btc_db_delete_dupe_comment($comment_ID); continue; } if ($is_own_blog) { btc_db_add_own_blog_comment($btc_post->ID, $comment_ID); } elseif ($is_retweet) { btc_db_add_retweet($btc_post->ID, $comment_ID); } btc_db_add_comment_type($btc_post->ID, $commentdata['comment_agent'], $comment_ID); $new++; } } } btc_log('Inserted ' . $new . ' new comments.', 'debug'); // paginate if ($new) { $btc_post->last_import_date_gmt = gmdate('Y-m-d H:i:s'); $btc_post->hits = (int) $btc_post->hits + $new; btc_db_update_post($btc_post); if ($new == (int) $itemsperpage && isset($xml->next_page)) { $params['page'] = (int) $xml->next_page; } else { $btc_counts = btc_db_comment_counts($btc_post->ID); btc_db_set_comment_counts($btc_post->ID, $btc_counts); if (function_exists('wp_update_comment_count')) { wp_update_comment_count($btc_post->ID); } $btc_summary = btc_db_comment_summary($btc_post->ID); btc_db_set_comment_summary($btc_post->ID, $btc_summary); $processed = true; } } else { $btc_post->last_import_date_gmt = gmdate('Y-m-d H:i:s'); $btc_post->misses = (int) $btc_post->misses + 1; btc_db_update_post($btc_post); $processed = true; } } else { if (!$xml || !empty($xml['error']['errorCode']) || empty($xml['feed']['comments'])) { if (!empty($xml['error']['errorCode']) && $xml['error']['errorCode'] == 'related-conversations-not-found') { $btc_post->last_import_date_gmt = gmdate('Y-m-d H:i:s'); $btc_post->misses = (int) $btc_post->misses + 1; btc_db_update_post($btc_post); btc_log('No related conversations.', 'error'); $error = 2; } else { if ($xml && empty($xml['feed']['comments']['entry']) && (int) $xml['feed']['startindex'] > (int) $xml['feed']['totalresults']) { btc_log('Paginated too far.', 'error'); //.. } else { btc_log('Unknown XML error.', 'error'); $error = 3; } } break; } if (empty($xml['feed']['comments']['entry']) || !count($xml['feed']['comments']['entry'])) { btc_log('No comments.', 'debug'); break; } require_once BTC_DIR . '/db.php'; $new = 0; btc_log('Comments received: ' . count($xml['feed']['comments']['entry']), 'debug'); $comments = array(); $ignore_own_blog = get_option(BTC_IGNORE_OWN_BLOG_OPTION); $ignore_retweets = get_option(BTC_IGNORE_RETWEETS_OPTION); $siteurl = get_option('siteurl'); $site_pieces = parse_url($siteurl); $site_domain = isset($site_pieces['host']) ? trim($site_pieces['host'], '/') : ''; $site_path = isset($site_pieces['path']) ? trim($site_pieces['path'], '/') : ''; foreach ($xml['feed']['comments']['entry'] as $key => $value) { if (is_int($key)) { $entry = $value; $entry['type'] = $type; $entry['src'] = $src; } else { $type = $value['type']; $src = isset($value['src']) ? $value['src'] : ''; continue; } if ($entry['type'] == 'comment') { $entry_pieces = parse_url($entry['blog']['url']); $entry_domain = isset($entry_pieces['host']) ? trim($entry_pieces['host'], '/') : ''; $entry_path = isset($entry_pieces['path']) ? trim($entry_pieces['path'], '/') : ''; $is_own_blog = $site_domain == $entry_domain && $site_path == $entry_path ? 1 : 0; $is_retweet = 0; $commentdata = array('comment_post_ID' => $btc_post->ID, 'comment_author' => $entry['author']['name'] ? trim($entry['author']['name']) : 'Anonymous', 'comment_author_email' => '', 'comment_author_url' => trim($entry['author']['url']), 'comment_author_IP' => '', 'comment_date' => date('Y-m-d H:i:s', strtotime(trim($entry['comment']['date'])) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => date('Y-m-d H:i:s', strtotime(trim($entry['comment']['date']))), 'comment_content' => _btc_filter_content($entry['comment']['content']), 'comment_approved' => $ignore_own_blog && $is_own_blog ? 'ignored' : (get_option(BTC_MODERATION_OPTION) ? '0' : '1'), 'comment_agent' => 'btc_' . $entry['src'], 'comment_type' => '', 'comment_parent' => ''); $desc = array('comment_src' => $entry['src'], 'site_url' => trim($entry['blog']['url']), 'site_title' => trim($entry['blog']['title']), 'post_url' => trim($entry['post']['url']), 'post_title' => $entry['post']['title'] ? trim($entry['post']['title']) : trim($entry['blog']['title']), 'comment_url' => trim($entry['comment']['url'])); } elseif ($entry['type'] == 'tweet') { $is_own_blog = 0; $is_retweet = substr(strtolower($entry['tweet_text']), 0, 2) == 'rt' ? 1 : 0; $commentdata = array('comment_post_ID' => $btc_post->ID, 'comment_author' => trim($entry['tweet_from_user']), 'comment_author_email' => '', 'comment_author_url' => 'http://twitter.com/' . trim($entry['tweet_from_user']), 'comment_author_IP' => '', 'comment_date' => date('Y-m-d H:i:s', strtotime(trim($entry['tweet_created_at'])) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => date('Y-m-d H:i:s', strtotime(trim($entry['tweet_created_at']))), 'comment_content' => _btc_filter_content($entry['tweet_text']), 'comment_approved' => $ignore_retweets && $is_retweet ? 'ignored' : (get_option(BTC_MODERATION_OPTION) ? '0' : '1'), 'comment_agent' => 'btc_twitter', 'comment_type' => '', 'comment_parent' => ''); $desc = array('comment_src' => 'twitter', 'site_url' => 'http://twitter.com/', 'site_title' => 'Twitter', 'post_url' => 'http://twitter.com/' . trim($entry['tweet_from_user']) . '/statuses/' . $entry['tweet_id'], 'post_title' => trim($entry['tweet_text']), 'comment_url' => 'http://twitter.com/' . trim($entry['tweet_from_user']) . '/statuses/' . $entry['tweet_id']); } $commentdata['comment_content'] .= _btc_comment_desc($desc); if (!btc_db_dupe_comment($commentdata)) { if (get_option(BTC_AKISMET_OPTION) && function_exists('akismet_auto_check_comment')) { $commentdata = _btc_akismet_check_comment($commentdata); } if (in_array($desc['comment_src'], $filters['srcs'])) { $commentdata['comment_approved'] = 'd_' . $commentdata['comment_approved']; } if ($comment_ID = wp_insert_comment($commentdata)) { $dupes = btc_db_dupe_comment($commentdata); if (count($dupes) > 1 && $comment_ID > min($dupes)) { btc_db_delete_dupe_comment($comment_ID); continue; } if ($is_own_blog) { btc_db_add_own_blog_comment($btc_post->ID, $comment_ID); } elseif ($is_retweet) { btc_db_add_retweet($btc_post->ID, $comment_ID); } btc_db_add_comment_type($btc_post->ID, $commentdata['comment_agent'], $comment_ID); $new++; } } } btc_log('Inserted ' . $new . ' new comments.', 'debug'); // paginate if ($new) { $btc_post->last_import_date_gmt = gmdate('Y-m-d H:i:s'); $btc_post->hits = (int) $btc_post->hits + $new; btc_db_update_post($btc_post); if ($new == (int) $itemsperpage && isset($xml['feed']['next_page'])) { $params['page'] = (int) $xml['feed']['next_page']; } else { $btc_counts = btc_db_comment_counts($btc_post->ID); btc_db_set_comment_counts($btc_post->ID, $btc_counts); if (function_exists('wp_update_comment_count')) { wp_update_comment_count($btc_post->ID); } $btc_summary = btc_db_comment_summary($btc_post->ID); btc_db_set_comment_summary($btc_post->ID, $btc_summary); $processed = true; } } else { $btc_post->last_import_date_gmt = gmdate('Y-m-d H:i:s'); $btc_post->misses = (int) $btc_post->misses + 1; btc_db_update_post($btc_post); $processed = true; } } } while (!$processed && !$error); if (!$error) { return array('success' => 1); } else { if ($error == 1) { if ($page == 1) { return array('success' => 0, 'message' => 'Could not open the URL.'); } else { return array('success' => 0, 'message' => 'Could not open some of the URLs.'); } } else { if ($error == 2) { return array('success' => 0, 'message' => 'No related conversations were found.'); } else { return array('success' => 0, 'message' => 'An unknown error occurred.'); } } } }