start work on converting social widget

This commit is contained in:
John Bintz 2009-12-01 07:11:06 -05:00
parent 374871e158
commit 1bb7edfd04
2 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,23 @@
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once(dirname(__FILE__) . '/../../widgets/SocialWidget.inc');
class SocialWidgetTest extends PHPUnit_Framework_TestCase {
function setUp() {
$this->w = new SocialWidget();
}
function testUpdate() {
$this->assertEquals(array(
'title' => 'title',
'urlstr' => 'urlstr',
'image' => 'image',
), $this->w->update(array(
'title' => '<em>title</em>',
'urlstr' => '<em>urlstr</em>',
'image' => '<em>image</em>',
), array()));
}
}

85
widgets/SocialWidget.inc Normal file
View File

@ -0,0 +1,85 @@
<?php
/*
Widget Name: ComicPress Social
Widget URI: http://comicpress.org/
Description: Translates a user's URL line into a social widget, associating it to images.
Author: Philip M. Hofer (Frumph)
Version: 1.01
Author URI: http://frumph.net/
*/
class SocialWidget extends WP_Widget {
function SocialWidget() {
$widget_ops = array('classname' => 'SocialWidget', 'description' => __('Display a user defined social button, with translated URL','comicpress') );
$this->WP_Widget('social', __('ComicPress Social','comicpress'), $widget_ops);
}
function widget($args, $instance) {
global $post;
extract($args, EXTR_SKIP);
/* Get the info necessary */
$permalink = get_permalink($post->ID);
$posttitle = $post->post_title;
$posttitle = str_replace(' ', '%20', $posttitle);
$title = $instance['title'];
if (empty($title)) $title = $posttitle;
$rss = get_bloginfo('rss2_url');
$blogname = urlencode(get_bloginfo('name')." ".get_bloginfo('description'));
// Grab the excerpt, if there is no excerpt, create one
$excerpt = urlencode(strip_tags(strip_shortcodes($post->post_excerpt)));
if ($excerpt == "") {
$excerpt = urlencode(substr(strip_tags(strip_shortcodes($post->post_content)),0,250));
}
// Clean the excerpt for use with links
$excerpt = str_replace('+','%20',$excerpt);
echo $before_widget;
$url = $instance['urlstr'];
$url = str_replace('[URL]', $permalink, $url);
$url = str_replace('[TITLE]', $posttitle, $url);
$url = str_replace('[RSS]', $rss, $url);
$url = str_replace('[BLOGNAME]', $blogname, $url);
$url = str_replace('[EXCERPT]', $excerpt, $url); ?>
<div class="social-<?php echo sanitize_title($title); ?>">
<?php if (!empty($instance['image'])) { ?>
<a href="<?php echo $url; ?>" target="_blank" class="social-<?php echo sanitize_title($title); ?>"><img src="<?php echo $instance['image']; ?>" alt="<?php echo $title; ?>" /></a>
<?php } else { ?>
<a href="<?php echo $url; ?>" target="_blank" class="social-<?php echo sanitize_title($title); ?>"><?php echo $title; ?></a>
<?php } ?>
</div>
<?php echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = array();
foreach (array('title', 'urlstr', 'image') as $field) {
$instance[$field] = strip_tags($new_instance[$field]);
}
return $instance;
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => 'Share This!', 'urlstr' => '', 'image' => '') );
$title = strip_tags($instance['title']);
if (empty($title)) $title = 'Share This!';
$urlstr = strip_tags($instance['urlstr']);
$image = strip_tags($instance['image']);
?>
<small>
<strong>Translated Strings:</strong> [URL] [TITLE] [RSS] [BLOGNAME] [EXCERPT]<br />
<strong>Examples:</strong><br />
http://twitter.com/home?status=[TITLE]%20-%20[URL]<br />
http://www.stumbleupon.com/submit?url=[URL]&amp;title=[TITLE]<br />
</small>
<br />
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title (used as CSS marker too):','comicpress'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('urlstr'); ?>"><?php _e('URL','comicpress'); ?> <input class="widefat" id="<?php echo $this->get_field_id('urlstr'); ?>" name="<?php echo $this->get_field_name('urlstr'); ?>" type="text" value="<?php echo attribute_escape($urlstr); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image','comicpress'); ?> <input class="widefat" id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="text" value="<?php echo attribute_escape($image); ?>" /></label></p>
<?php
}
}