0) { $current['cacheLength'] = (int)$data['cacheLength']; } if (isset($data['tweetCount']) && (int)$data['tweetCount'] > 0) { $current['tweetCount'] = (int)$data['tweetCount']; } if ($current['useCache'] == 'on') { $current['useCache'] = true; } if ($current['newWindow'] == 'on') { $current['newWindow'] == true; } if (isset($data['defaultAccount'])) { $current['defaultAccount'] = trim($data['defaultAccount']); } if (isset($data['htmlBefore'])) { $current['htmlBefore'] = trim($data['htmlBefore']); } if (isset($data['htmlAfter'])) { $current['htmlAfter'] = trim($data['htmlAfter']); } /* Check for the checkbox values. We'll assume that if tweetCount * and defaultAccount are set in $data then it's coming from * the options page admin-side and the checkbox values need to * be updated. */ if (isset($data['defaultAccount']) && isset($data['tweetCount'])) { $current['useCache'] = (bool)$data['useCache']; $current['newWindow'] = (bool)$data['newWindow']; $current['loadCSS'] = (bool)$data['loadCSS']; if (isset($_POST['rtwit-clear-cache']) && $_POST['rtwit-clear-cache']==1) { $current['cache'] = array(); } } return $current; } /** * Callback to handle the short codes. * * This function is pretty much a cleanup. The actual output is handed off to * rtwit_get_code. */ function rtwit_shortcode_handler($atts, $content=null, $code = '') { /* Get the default options. We'll overridge them if the appropriate * tags are available in $atts */ $opts = get_option('rtwit_options'); $opts['account'] = strtolower((isset($atts['account'])) ? $atts['account'] : $opts['defaultAccount']); $opts['useCache'] = (isset($atts['cache'])) ? (bool)$atts['cache']: $opts['useCache']; $opts['tweetCount'] = (isset($atts['count']) && (int)$atts['count'] > 0) ? (int)$atts['count'] : $opts['tweetCount']; $opts['cacheLength'] = (isset($atts['cache_length']) && (int)$atts['cache_length'] > 0) ? (int)$atts['cache_length'] : $opts['cacheLength']; $opts['newWindow'] = (isset($atts['new_window'])) ? (bool)$atts['new_window'] : $opts['newWindow']; $opts['htmlBefore'] = (isset($atts['html_before'])) ? $atts['html_before'] : $opts['htmlBefore']; $opts['htmlAfter'] = (isset($atts['html_after'])) ? $atts['html_after'] : $opts['htmlAfter']; // We need to clean up a few things $opts['account'] = trim($opts['account']); $opts['account'] = preg_replace('/^@/', '', $opts['account']); /* If there still isn't an account available we'll just return * a message as an HTML comment so nothing shows on the screen * but the owner can still see what's going on. */ if ($opts['account'] == '') { return "\n"; } return rtwit_get_code($opts); } function rtwit_add_css($posts) { $opts = get_option('rtwit_options'); if ($opts['loadCSS']) { if (empty($posts)) { return $posts; } $found = false; foreach ($posts as $post) { //var_dump($post->post_content); echo "\n"; if (stripos($post->post_content, '[rtwit') !== false) { $found = true; break; } } if ($found) { wp_register_style('rtwit-css', plugins_url('rTwit.css', __FILE__)); wp_enqueue_style('rtwit-css'); } } return $posts; } /** * Build and return the code needed to display the plugin * * Calls itself recursively with $useCache=false if the cache is on but * has expired. */ function rtwit_get_code($opts, $cache=true) { if ($opts['useCache'] == false || !$cache) { $url = 'http://twitter.com/statuses/user_timeline.json?screen_name=' . $opts['account'] . '&count=' . $opts['tweetCount']; if (!class_exists('WP_Http')) { include_once(ABSPATH . WPINC . '/class-http.php'); } $req = new WP_Http; $result = $req->request($url); if (!isset($result['response']['code']) || $result['response']['code'] != 200) { $cacheData = ''; } else { $json = json_decode($result['body'], true); if (!$json || !is_array($json)) { $cacheData = ''; } else if (count($json) < 1) { $cacheData = ''; } else { $cacheData = ''; if ($opts['htmlBefore']) { $cacheData .= $opts['htmlBefore']; } $cacheData .= '
'; foreach ($json as $tweet) { $cacheData .= '
'; $cacheData .= '
'; $cacheData .= rtwit_format_tweet($tweet['text'], (bool)$opts['newWindow']); $cacheData .= '
'; //.rtwit-tweet-text $cacheData .= '
'; $cacheData .= date(get_option('date_format').' '.get_option('time_format'), strtotime($tweet['created_at'])); $cacheData .= '
'; $cacheData .= '
'; //.rtwit-tweet } $cacheData .= '
'; //.rtwit-wrapper } } if ($opts['htmlAfter']) { $cacheData .= $opts['htmlAfter']; } /* Save the data back into the cache. We're doing this anyway because * this function is called recursively to rebuild the cache. */ $acct = strtolower($opts['account']); $opts = get_option('rtwit-options'); $opts['cache'][$acct] = array( 'tweetCount' => count($json), 'tweetsRequested' => $opts['tweetCount'], 'data' => $cacheData, 'cacheDate' => date('U') ); update_option('rtwit_options', $opts); return $cacheData; } else { // Try getting it from the cache if possible. if (isset($opts['cache'][$opts['account']]['cacheDate'])) { $diff = date('U') - $opts['cache'][$opts['account']]['cacheDate']; $diff /= 60; if ($diff < $opts['cacheLength']) { return $opts['cache'][$opts['account']]['data']; } else { return rtwit_get_code($opts, false); } } else { return rtwit_get_code($opts, false); } } } /** * Format a tweet by adding in anchor tags as needed * @param $tweet * @return */ function rtwit_format_tweet($tweet, $newWindow = true) { if ($newWindow) { $newWindow = ' target="_blank"'; } else { $newWindow = ''; } $tweet = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", "$1$2$4", $tweet); $tweet = preg_replace("/@(\w+)/", "@\\1", $tweet); $tweet = preg_replace("/#(\w+)/", "#\\1", $tweet); return $tweet; } /** Return an array of the default settings */ function rtwit_defaults() { return array( 'useCache' => true, 'cacheLength' => 30, 'tweetCount' => 10, 'newWindow' => true, 'defaultAccount' => '', 'htmlBefore' => '', 'htmlAfter' => '', 'loadCSS' => true, 'cache' => array() ); } /** * Function to display the options page admin-side */ function rtwit_options() { $defaults = rtwit_defaults(); $opts = get_option('rtwit_options'); $opts = array_merge($defaults, $opts); ?>

rTwit Options

Default Twitter Account
Default Tweet Count
Use Cache />
Cache Length minutes
Open in New Window />
Load CSS />
HTML Before
HTML After
Clear Cache