/** * Log debug message in flat file if applicable * * @param mixed $in String, array or message to log if we're in debug mode */ function ozh_ta_debug($in) { static $log_debug = null; if ($log_debug === null) { $log_debug = ozh_ta_is_debug(); } if ($log_debug === false) { return; } if (is_array($in) or is_object($in)) { $in = print_r($in, true); } $ts = date('Y-m-d H:i:s'); error_log("{$ts}: {$in}\n", 3, dirname(dirname(__FILE__)) . '/debug.log'); }
/** * Insert tweets as posts * * @param array $tweets Array of tweet objects * @return array Array of stats about the insertion */ function ozh_ta_insert_tweets($tweets) { // Flag as importing : this will cut some queries in the process, regarding (ping|track)backs if (!defined('WP_IMPORTING')) { define('WP_IMPORTING', true); } global $ozh_ta; $inserted = $skipped = $tagged = $num_tags = 0; $user = array(); if (ozh_ta_is_debug()) { $num_sql_batch = new ozh_ta_query_count(); } foreach ((array) $tweets as $tweet) { if (ozh_ta_is_debug()) { $num_sql_post = new ozh_ta_query_count(); } // Current tweet $tid = (string) $tweet->id_str; $text = ozh_ta_linkify_tweet($tweet); $date = date('Y-m-d H:i:s', strtotime($tweet->created_at) + 3600 * get_option('gmt_offset')); $source = $tweet->source; $has_hashtags = count($tweet->entities->hashtags) > 0; $reply_to_name = $tweet->in_reply_to_screen_name; $reply_to_tweet = (string) $tweet->in_reply_to_status_id_str; // Info about Twitter user if (!$user) { $user = array('tweet_counts' => $tweet->user->statuses_count, 'followers' => $tweet->user->followers_count, 'following' => $tweet->user->friends_count, 'listed_count' => $tweet->user->listed_count, 'profile_image_url' => $tweet->user->profile_image_url, 'tweeting_since' => date('Y-m-d H:i:s', strtotime($tweet->user->created_at))); } // Check for duplicate posts before inserting global $wpdb; $sql = "SELECT post_id\n\t\t FROM `{$wpdb->postmeta}`\n\t\t\t\tWHERE `meta_key` = 'ozh_ta_id' AND `meta_value` = '{$tid} ' LIMIT 0,1"; // Yeah, trusting api.twitter.com so we don't sanitize the SQL query, yeeeha if (!$wpdb->get_var($sql)) { // Insert tweet as new post $post = array('post_title' => strip_tags($text), 'post_content' => $text, 'post_date' => $date, 'post_category' => array($ozh_ta['post_category']), 'post_status' => 'publish', 'post_author' => $ozh_ta['post_author'], 'guid' => home_url() . '/?tid=' . $tid); // Post format if ('standard' != $ozh_ta['post_format']) { $post['tax_input'] = array('post_format' => array('post-format-' . $ozh_ta['post_format'])); } // Plugins: hack here $post = apply_filters('ozh_ta_insert_tweets_post', $post); $post_id = wp_insert_post($post); // Apply post format when line 294-296 failed process the request if ('standard' != $ozh_ta['post_format']) { set_post_format($post_id, $ozh_ta['post_format']); } // Insert post meta data add_post_meta($post_id, 'ozh_ta_id', $tid); if ($source) { add_post_meta($post_id, 'ozh_ta_source', $source); } if ($reply_to_name) { add_post_meta($post_id, 'ozh_ta_reply_to_name', $reply_to_name); } if ($reply_to_tweet) { add_post_meta($post_id, 'ozh_ta_reply_to_tweet', $reply_to_tweet); } ozh_ta_debug(" Inserted #{$post_id} (tweet id: {$tid}, tweet: " . ozh_ta_trim_long_string($text, 100) . ')'); if (ozh_ta_is_debug()) { ozh_ta_debug(' Import query cost: ' . $num_sql_post->stop()); } // Tag post if applicable if ($has_hashtags && $ozh_ta['add_hash_as_tags'] == 'yes') { $hashtags = ozh_ta_get_hashtags($tweet); $num_tags += count($hashtags); $hashtags = implode(', ', $hashtags); ozh_ta_debug(" Tagging post {$post_id} with " . $hashtags); $tagged++; if (ozh_ta_is_debug()) { $num_sql_tag = new ozh_ta_query_count(); } wp_set_post_tags($post_id, $hashtags); if (ozh_ta_is_debug()) { ozh_ta_debug(' Tagging query cost: ' . $num_sql_tag->stop()); } } $inserted++; } else { // This tweet has already been imported ?! ozh_ta_debug(" Skipping tweet {$tid}, already imported?!"); $skipped++; } } if (ozh_ta_is_debug()) { ozh_ta_debug('Batch import query cost: ' . $num_sql_batch->stop()); } return array('inserted' => $inserted, 'skipped' => $skipped, 'tagged' => $tagged, 'num_tags' => $num_tags, 'user' => $user); }
function ozh_ta_do_page() { global $ozh_ta; ?> <div class="wrap"> <?php screen_icon(); ?> <h2>Ozh' Tweet Archiver</h2> <style> fieldset.ozh_ta_fs { border:1px solid #ddd; padding:2px 10px; padding-bottom:10px; margin-bottom:10px; } </style> <?php if (ozh_ta_is_debug()) { echo "<pre style='border:1px solid;padding:5px 15px;'>"; var_dump($ozh_ta); echo "</pre>"; } if (wp_next_scheduled('ozh_ta_cron_import') && !ozh_ta_is_manually_archiving()) { ozh_ta_do_page_scheduled(); } if (ozh_ta_is_configured()) { ozh_ta_do_page_manual(); } // Option page when not doing operation if (!isset($_GET['action']) or $_GET['action'] != 'import_all') { ?> <form action="options.php" method="post"> <?php settings_fields('ozh_ta'); ?> <?php do_settings_sections('ozh_ta'); ?> <script type="text/javascript"> (function($){ $('.toggler').change(function(){ var id = $(this).attr('id'); var val = $(this).val(); $('.toggle_'+id).hide(); $('#toggle_'+id+'_'+val).show(); pulse('#toggle_'+id+'_'+val ); }); var maxw = 0; $('select').each(function(i,e){ var w = jQuery(this).css('width').replace('px', ''); maxw = Math.max( maxw, w ); }).css('width', maxw+'px'); $('#link_hashtags').change(function(){ if( $(this).val() == 'local' ) { pulse( $('#add_hash_as_tags').val('yes').change() ); } }); $('#add_hash_as_tags').change(function(){ if( $(this).val() == 'no' && $('#link_hashtags').val() == 'local' ) { pulse( $('#link_hashtags').val('twitter').change() ); } }); function pulse( el ){ var bg = $(el).css('backgroundColor'); $(el).animate({backgroundColor: '#ff4'}, 500, function(){ $(el).animate({backgroundColor: bg}, 500, function(){ $(el).css('backgroundColor', bg); }); }); } })(jQuery); </script> <?php submit_button(); submit_button('Reset all settings', 'delete', 'delete_btn'); ?> <script> (function($){ $('#delete_btn').click( function() { if (!confirm('Really reset everything? No undo!')) { return false; } }); })(jQuery); </script> </form> <?php } ?> </div> <?php }