. * ************************************************************************* */ /* Class Declaration */ class better_twitter_widget extends WP_Widget { var $version = '2.7.1'; /* Plugin settings */ var $optionsName = 'better-twitter-widget-options'; var $menuName = 'better-twitter-widget-settings'; var $pluginName = 'Better Twitter Widget'; var $options = array(); var $make_link = false; /** * Plugin Constructor Method */ function better_twitter_widget() { add_action('init', array($this, 'init')); /* Set the plugin name to use the selected language. */ $this->pluginName = __('Better Twitter Widget', 'better-twitter-widget'); $widget_ops = array('description' => __('Enhancement of Better RSS Widget, focussed on plain Twitter feeds', 'better-twitter-widget')); $control_ops = array('width' => 500, 'height' => 350); parent::WP_Widget(false, $this->pluginName, $widget_ops, $control_ops); /* Plugin paths */ $this->pluginPath = WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)); $this->pluginURL = WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)); /* Load the plugin settings */ $this->load_settings(); /* WordPress Actions */ add_action('admin_menu', array(&$this, 'admin_menu')); add_action('admin_init', array(&$this, 'admin_init')); add_action('update_option_' . $this->optionsName, array(&$this, 'update_option'), 10); /* WordPress FIlters */ add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2); /* Add shortcode_handlers */ add_shortcode('better-rss', array($this, 'shortcode_handler')); } /** * Load the plugin settings. */ function load_settings() { $options = get_option($this->optionsName); $defaults = array( 'link_target' => '_blank', 'allow_intro' => (is_array($options)) ? isset($options['allow_intro']) : true, 'show_summary' => true, 'show_author' => false, 'show_date' => true, 'show_time' => false, 'nofollow' => true, 'enable_cache' => (is_array($options)) ? isset($options['enable_cache']) : true, 'cache_duration' => 3600, 'items' => 5, 'title_length' => (is_array($options) && !empty($options['title_length'])) ? isset($options['title_length']) : 0, 'excerpt' => 360, 'suffix' => ' [...]' ); $this->options = (object) wp_parse_args($options, $defaults); } /** * Load the language file during WordPress init. */ function init() { /* Load Langague Files */ $langDir = dirname(plugin_basename(__FILE__)) . '/lang'; load_plugin_textdomain('better-twitter-widget', false, $langDir, $langDir); } /** * Add the admin page for the settings panel. * * @global string $wp_version */ function admin_menu() { $page = add_options_page($this->pluginName . __(' Settings', 'better-twitter-widget'), $this->pluginName, 'manage_options', $this->menuName, array(&$this, 'options_panel')); add_action('admin_print_styles-' . $page, array(&$this, 'admin_print_styles')); add_action('admin_print_scripts-' . $page, array(&$this, 'admin_print_scripts')); } /** * Register the options for Wordpress MU Support */ function admin_init() { register_setting($this->optionsName, $this->optionsName); wp_register_style('better-twitter-widget-admin-css', $this->pluginURL . '/includes/better-twitter-widget-admin.css'); wp_register_script('better-twitter-widget-js', $this->pluginURL . '/js/better-twitter-widget.js'); } /** * Print the administration styles. */ function admin_print_styles() { wp_enqueue_style('better-twitter-widget-admin-css'); } /** * Print the scripts needed for the admin. */ function admin_print_scripts() { wp_enqueue_script('better-twitter-widget-js'); } /** * Add a configuration link to the plugins list. * * @staticvar object $this_plugin * @param array $links * @param array $file * @return array */ function plugin_action_links($links, $file) { static $this_plugin; if (!$this_plugin) { $this_plugin = plugin_basename(__FILE__); } if ($file == $this_plugin) { $settings_link = '' . __('Settings', 'better-twitter-widget') . ''; array_unshift($links, $settings_link); } return $links; } /** * Check on update option to see if we need to reset the options. * @param $input * @return */ function update_option($input) { if ($_REQUEST['confirm-reset-options']) { delete_option($this->optionsName); wp_redirect(admin_url('options-general.php?page=' . $this->menuName . '&tab=' . $_POST['active_tab'] . '&reset=true')); exit(); } else { wp_redirect(admin_url('options-general.php?page=' . $this->menuName . '&tab=' . $_POST['active_tab'] . '&updated=true')); exit(); } } /** * Settings management panel. */ function options_panel() { include($this->pluginPath . '/includes/settings.php'); } /** * Method to create the widget. * * @param array $args * @param array $instance * @return false */ function widget($args, $instance) { $instance = $this->defaults($instance); if (isset($instance['error']) && $instance['error']) return; extract($args, EXTR_SKIP); $url = $instance['rss_url']; while (stristr($url, 'http') != $url) $url = substr($url, 1); if (empty($url)) { return; } $rss = fetch_feed($url); $desc = ''; $link = ''; if (!is_wp_error($rss)) { $desc = esc_attr(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset')))); if (empty($instance['title'])) { $instance['title'] = esc_html(strip_tags($rss->get_title())); } $link = esc_url(strip_tags($rss->get_permalink())); while (stristr($link, 'http') != $link) { $link = substr($link, 1); } } if (empty($instance['title'])) { $instance['title'] = empty($desc) ? __('Unknown Feed', 'better-twitter-widget') : $desc; } $instance['title'] = apply_filters('widget_title', $instance['title']); $url = esc_url(strip_tags($url)); $icon = includes_url('images/rss.png'); if ($instance['title_url']) { $url = $link = $instance['title_url']; } $target = ''; if ($instance['link_target'] != 'none') { $target = 'target="' . $instance['link_target'] . '"'; } if ($instance['title']) { if (!$instance['no_link_title']) { $instance['title'] = '' . $instance['title'] . ''; } if ($instance['show_icon']) { $instance['title'] = "RSS " . $instance['title']; } } print $before_widget; if ($instance['title']) { print $before_title . $instance['title'] . $after_title; } if (true == $this->options->allow_intro && !empty($instance['intro_text'])) { print '
' . $instance['intro_text'] . '
'; } $this->rss_output($rss, $instance); print $after_widget; } /** * Method to output the RSS for the widget and shortcode_handler * * @param string $rss * @param array $args * @return blank */ function rss_output($rss, $args = array()) { if (is_string($rss)) { $rss = fetch_feed($rss); } elseif (is_array($rss) && isset($rss['url'])) { $args = $rss; $rss = fetch_feed($rss['url']); } elseif (!is_object($rss)) { return; } if (is_wp_error($rss)) { if (is_admin() || current_user_can('manage_options')) { print '

' . sprintf(__('RSS Error: %s', 'better-twitter-widget'), $rss->get_error_message()) . '

'; } return; } $args = wp_parse_args($args, $this->defaults_args()); extract($args, EXTR_SKIP); $items = (int) $items; if ($items < 1 || 20 < $items) { $items = 10; } $show_summary = (int) $show_summary; $show_author = (int) $show_author; $show_date = (int) $show_date; // Set the cache duration $rss->enable_cache($enable_cache); $rss->set_cache_duration($cache_duration); $rss->init(); if (!$rss->get_item_quantity()) { print ''; return; } if (strtolower($link_target) != 'none') { $target = 'target="' . $link_target . '"'; } else { $target = ''; } print '<' . $args['html_parent'] . '>'; foreach ($rss->get_items(0, $items) as $item) { $link = $item->get_link(); while (stristr($link, 'http') != $link) { $link = substr($link, 1); } $link = esc_url(strip_tags($link)); $title = esc_attr(strip_tags($item->get_title())); if (empty($title)) { $title = __('Untitled', 'better-twitter-widget'); } $desc = str_replace(array("\n", "\r"), ' ', esc_attr(strip_tags(@html_entity_decode($item->get_description(), ENT_QUOTES, get_option('blog_charset'))))); // BUG -- CUSTOM comment out /*if (!$hide_title) { $desc = wp_html_excerpt($desc, $excerpt) . $this->options->suffix; $desc = esc_html($desc); }*/ $desc = esc_html($desc); if ($show_summary) { $summary = "
$desc
"; } else { $summary = ''; } $date = ''; if ($show_date) { $date = $item->get_date(); if ($date) { if ($date_stamp = strtotime($date)) $date = ' ' . date_i18n(get_option('date_format'), $date_stamp) . ''; else $date = ''; } } $time = ''; if ($show_time) { $time = $item->get_date(); if ($time) { if ($date_stamp = strtotime($time)) { $time = ' ' . date_i18n(get_option('time_format'), $date_stamp) . ''; } else { $time = ''; } } } $author = ''; if ($show_author) { $author = $item->get_author(); if (is_object($author)) { $author = $author->get_name(); $author = ' ' . esc_html(strip_tags($author)) . ''; } } if ($hide_title && $item->get_description()) { $title = $item->get_description(); } if (true == $args['limit_title_length'] && $args['title_length'] > 0) { $title = substr($title, 0, $args['title_length']); } if ($args['html_parent'] === 'dl') { $html_open = '
'; $html_after_title = '
'; $html_before_details = '
'; $html_close = '
'; } else { $html_open = '<' . $args['html_item'] . '>'; $html_after_title = ''; $html_before_details = ''; $html_close = ''; } if ($link == '' or $hide_link) { print $html_open . $title . $html_after_title; print $html_before_details . $date . $summary . $author . $html_close; } else { /* ORIG print $html_open . '' . $title . '' . $html_after_title; //print $html_before_details . $date . $time . $summary . $author . $html_close; print $html_close; ======================= */ $search = array(); $search[0] = '/([\s>\-\.])#([[:alnum:]öäü]+)/i'; // text #hash text; >#hash text $search[1] = '/([\s>\-\.])@([[:alnum:]_]+)/'; // @user $search[2] = '/([\s>\-\.])(https?:[\/[:alnum:]\.\?\&#\=\%\+\-\_]+)/'; // http... $replace = array(); $replace[0] = '$1#$2'; $replace[1] = '$1@$2'; $replace[2] = '$1$2'; $extsummary = preg_replace($search, $replace, $summary); // get tweet ID $tid = preg_match('/(\d)+$/', $link, $match); $tid = $match[0]; // li + text print $html_open . $extsummary ; print '' . $date . ''; // Action bar print ''; // reply icon print ''; // retweet icon print ''; // like icon print ''; // view on twitter icon print ''; print ''; // close action bar // close li print $html_close; } } print ''; } /** * Widget Update method * @param array $new_instance * @param array $old_instance * @return array */ function update($new_instance, $old_instance) { return $new_instance; } /** * Load the instance defaults. * * @param array $instance * @return array */ function defaults($instance) { /* Fix any old instances to use new naming convention. */ if (isset($instance['url'])) { $instance['rss-url'] = $instance['url']; $instance['title_url'] = $instance['titleurl']; $instance['show_icon'] = $instance['showicon']; $instance['show_summary'] = $instance['showsummary']; $instance['show_author'] = $instance['showauthor']; $instance['show_date'] = $instance['showdate']; $instance['show_time'] = $instance['showtime']; $instance['link_target'] = $instance['linktarget']; $instance['title_legnth'] = (isset($instance['title_length']) ? $instance['title_length'] : $this->options->title_length); } /* This is the new naming convention for the form fields */ $new_defaults = array( 'rss_url' => '', 'title' => '', 'title_url' => '', 'no_link_title' => false, 'show_icon' => false, 'link_icon' => 'rss_url', 'show_summary' => $this->options->show_summary, 'show_author' => $this->options->show_author, 'show_date' => $this->options->show_date, 'show_time' => $this->options->show_time, 'link_target' => $this->options->link_target, 'nofollow' => $this->options->nofollow, 'enable_cache' => $this->options->enable_cache, 'cache_duration' => $this->options->cache_duration, 'is_home' => $this->options->is_home_default, 'is_front' => $this->options->is_front_default, 'is_archive' => $this->options->is_archive_default, 'is_search' => $this->options->is_search_default, 'is_category' => $this->options->is_category_default, 'is_tag' => $this->options->is_tag_default, 'is_single' => $this->options->is_single_default, 'is_date' => $this->options->is_date_default, 'title_length' => $this->options->title_length, 'excerpt' => $this->options->excerpt, 'items' => $this->options->items ); return wp_parse_args($instance, $new_defaults); } /** * Widget form. * * @param array $instance */ function form($instance) { if (count($instance) < 1) { $instance = $this->defaults($instance); } include( $this->pluginPath . '/includes/widget-form.php'); } /** * Method for the [better-rss] short code. * * @param array $atts * @return string */ function shortcode_handler($atts) { global $post; $atts = (object) wp_parse_args($atts, $this->defaults_args()); if (!$atts->feed) { return false; } if ($atts->use_title) { $add_url[] = str_replace(' ', '+', $post->post_title); } if ($atts->use_tags) { foreach (get_the_tags() as $tag) { $add_url[] = str_replace(' ', '+', $tag->name); } } if (isset($add_url) and is_array($add_url)) { $atts->feed = $atts->feed . implode('+', $add_url); } ob_start(); $this->rss_output($atts->feed, $atts); $output.= ob_get_contents(); ob_end_clean(); return $output; } function defaults_args() { return array( // Query Attributes 'feed' => NULL, 'use_title' => false, 'use_tags' => false, 'use_category' => false, 'items' => 10, 'hide_title' => false, 'hide_link' => false, 'show_author' => $this->options->show_author, 'show_date' => $this->options->show_author, 'show_time' => $this->options->show_time, 'show_summary' => $this->options->show_summary, 'link_target' => $this->options->link_target, 'nofollow' => $this->options->nofollow, 'cache_duration' => $this->options->cache_duration, 'excerpt' => $this->options->excerpt, 'html_parent' => 'ul', 'html_item' => 'li' ); } /** * Get an object with all of the post types. * * @return object */ function get_post_types($args = array()) { if (function_exists('get_post_types')) { $post_types = get_post_types(array('public' => true)); unset($post_types['attachment']); } else { $post_types = array('post', 'page'); } $defaults = array( 'output' => 'object', 'name' => 'post_type', 'id' => 'post_type' ); $args = (object) wp_parse_args($args, $defaults); switch ($args->output) { case 'dropdown': $results = ''; break; default: $results = (object) $post_types; } return $results; } /** * Display the list of contributors. * @return boolean */ function contributor_list() { if (function_exists('simplexml_load_file')) { $this->showFields = array('NAME', 'LOCATION', 'COUNTRY'); print ''; } else { _e('PHP 5 Required to see a list of contributors.', 'pretty-sidebar-categories'); } } /** * Displayes any data sent in textareas. * * @param $input */ function debug($input) { $contents = func_get_args(); foreach ($contents as $content) { print ''; } echo '
'; } } /** * Add the widget code to the initialization action */ add_action('widgets_init', create_function('', 'return register_widget("better_twitter_widget");')); register_activation_hook(__FILE__, 'better_twitter_activate'); function better_twitter_activate() { /* Compile old options into new options Array */ $new_options = ''; $options = array('link_target', 'items', 'show_summary', 'show_author', 'show_date', 'show_time', 'enable_cache', 'cache_duration', 'is_home_default', 'is_front_default', 'is_archive_default', 'is_search_default', 'is_category_default', 'is_tag_default', 'is_single_default', 'is_date_default'); foreach ($options as $option) { if ($old_option = get_option('better_twitter_' . $option)) { $new_options[$option] = $old_option; delete_option('better_twitter_' . $option); } } if (is_array($new_options) and ! add_option('better-twitter-widget-options', $new_options)) { update_option('better-twitter-widget-options', $new_options); } }