DEFAULT_WIDGET_TITLE,
'username' => DEFAULT_USERNAME,
'limit' => DEFAULT_LIMIT
);
if (is_array($saved_options = get_option ($this->stocktwits_op_name)))
$this->stocktwits_options = $saved_options;
else
{
add_option ($this->stocktwits_op_name, $default_options);
$this->stocktwits_options = $default_options;
}
}
//------------------------------------------
//------------------------------------------
public function GetOptions ()
{
return ($this->stocktwits_options);
}
//------------------------------------------
//------------------------------------------
public function GetWidgetHTML ($widget_title=0, $username=0, $limit=0)
{
$widget_html = WIDGET_HTML_TEMPLATE;
$widget_html = preg_replace ('|__TITLE__|', $widget_title?$widget_title:$this->stocktwits_options['widget_title'], $widget_html);
if ($this->stocktwits_options['username'][0] == '$') {
$username = substr(trim($this->stocktwits_options['username']), 1);
$widget_html = preg_replace ('|__SYMBOL__|', $username, $widget_html);
$widget_html = preg_replace ('|__USERNAME__|', '', $widget_html);
}
else {
$widget_html = preg_replace ('|__USERNAME__|', $username?$username:$this->stocktwits_options['username'], $widget_html);
$widget_html = preg_replace ('|__SYMBOL__|', '', $widget_html);
}
$widget_html = preg_replace ('|__LIMIT__|', $limit?$limit:$this->stocktwits_options['limit'], $widget_html);
return ($widget_html);
}
//------------------------------------------
}
//===========================================================================
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// Instantiate plugin object.
global $g_StockTwits_Plugin;
$g_StockTwits_Plugin = new StockTwits();
//Initialize the admin panel
add_action ('admin_menu', 'StockTwits_AdminPanel');
add_action ('init', 'init_action' );
add_action ('plugins_loaded', 'RegisterStockTwitsWidget');
add_filter ('the_content', array(&$g_StockTwits_Plugin, 'ContentFilter'), 9);
add_filter ('the_content_limit', array(&$g_StockTwits_Plugin, 'ContentFilter'), 9);
add_filter ('the_excerpt', array(&$g_StockTwits_Plugin, 'ContentFilter_stub'), 8);
add_filter ('the_excerpt_rss', array(&$g_StockTwits_Plugin, 'ContentFilter_stub'), 8);
add_filter ('the_content_rss', array(&$g_StockTwits_Plugin, 'ContentFilter_stub'), 8);
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//===========================================================================
function RegisterStockTwitsWidget()
{
register_sidebar_widget(__('StockTwits Widget'), 'StockTwitsWidget');
}
// Actual widget HTML emitting function.
function StockTwitsWidget ($args)
{
global $g_StockTwits_Plugin;
$widget_title = $g_StockTwits_Plugin->GetOptions();
$widget_title = $widget_title['widget_title'];
extract($args);
echo $before_widget;
echo $before_title;
echo $widget_title;
echo $after_title;
echo $g_StockTwits_Plugin->GetWidgetHTML ();
echo $after_widget;
}
//===========================================================================
//===========================================================================
// Template function to display StockTwits.com widget
// Could be embedded into index.php, single.php, etc... as:
// Syntax:
// stocktwits_widget();
// stocktwits_widget("Intel Inside", "$INTC");
// stocktwits_widget("Intel Inside", "$INTC", 20, 10);
function stocktwits_widget ($widget_title=0, $username=0, $limit=0)
{
global $g_StockTwits_Plugin;
echo $g_StockTwits_Plugin->GetWidgetHTML ($widget_title, $username, $limit);
}
//===========================================================================
//===========================================================================
function StockTwits_AdminPanel ()
{
global $g_StockTwits_Plugin;
add_options_page('StockTwits', 'StockTwits Plugin', 9, basename(__FILE__), array(&$g_StockTwits_Plugin, 'PrintAdminPage'));
}
//===========================================================================
//===========================================================================
function init_action ()
{
wp_enqueue_script('st-widget', 'http://stocktwits.com/addon/widget/2/widget-loader.min.js');
}
//===========================================================================
//===========================================================================
function enforce_values ($input, $min, $max)
{
if ($input < $min)
return $min;
if ($input > $max)
return $max;
return $input;
}
//===========================================================================
//===========================================================================
//
// Returns no-slashed WEB URL of directory where this file is.
function get_base_dir_url ()
{
$base_dir_url = get_bloginfo ('wpurl') . preg_replace ('#^.*[/\\\\](.*?)[/\\\\].*?$#', "/wp-content/plugins/$1", __FILE__);
return ($base_dir_url);
}
//===========================================================================
//===========================================================================
// log_event (__FILE__, __LINE__, "Message", "extra data");
if (!function_exists('log_event'))
{
function log_event ($filename, $linenum, $message, $extra_text="")
{
$log_filename = dirname(__FILE__) . '/__log.php';
$logfile_header = '";
// Delete too long logfiles.
if (@file_exists ($log_filename) && @filesize($log_filename)>1000000)
unlink ($log_filename);
$filename = basename ($filename);
if (file_exists ($log_filename))
{
// 'r+' non destructive R/W mode.
$fhandle = fopen ($log_filename, 'r+');
if ($fhandle)
fseek ($fhandle, -strlen($logfile_tail), SEEK_END);
}
else
{
$fhandle = fopen ($log_filename, 'w');
if ($fhandle)
fwrite ($fhandle, $logfile_header);
}
if ($fhandle)
{
fwrite ($fhandle, "\r\n// " . $_SERVER['REMOTE_ADDR'] . ' -> ' . date("Y-m-d, G:i:s.u") . "|$filename($linenum)|: " . $message . ($extra_text?"\r\n// Extra Data: $extra_text":"") . $logfile_tail);
fclose ($fhandle);
}
}
}
//===========================================================================
?>