diff --git a/classes/DailyImageWidget.php b/classes/DailyImageWidget.php index fd911fd..a4193db 100644 --- a/classes/DailyImageWidget.php +++ b/classes/DailyImageWidget.php @@ -4,8 +4,7 @@ class DailyImageWidget { function DailyImageWidget($skip_load_data = false) { $this->default_display_options = array( 'title', - 'image', - 'styles' + 'image' ); $this->_cache_time = 86400; @@ -18,8 +17,7 @@ class DailyImageWidget { $this->_valid_options = array( "image" => __("Daily Image", "hubblesite-daily-image-widget"), "title" => __("Image Title", "hubblesite-daily-image-widget"), - "credits" => __("Credits", "hubblesite-daily-image-widget"), - "styles" => __("HubbleSite Styles", "hubblesite-daily-image-widget"), + "credits" => __("Credits", "hubblesite-daily-image-widget") ); add_action('init', array($this, "_init")); @@ -103,13 +101,15 @@ class DailyImageWidget { /** * Render the widget. */ - function render() { + function render($args) { if (!empty($this->data) && is_array($this->data)) { + extract($args); $options = $this->get_display_options(); - echo '
'; - echo '

HubbleSite Daily Image

'; - + echo $before_widget; + echo $before_title; + echo "HubbleSite Daily Image"; + echo $after_title; if (in_array("image", $options)) { echo ''; echo '' . $this->data['title'] . ''; @@ -118,22 +118,16 @@ class DailyImageWidget { if (in_array("title", $options)) { echo ''; - echo $this->data['title']; + echo $this->_fix_widows($this->data['title']); echo ''; } if (in_array("credits", $options)) { echo '
'; - echo $this->data['credits']; + echo $this->_fix_widows($this->data['credits']); echo '
'; } - echo '
'; - - if (in_array("styles", $options)) { - echo ""; - } + echo $after_widget; } } @@ -240,6 +234,10 @@ class DailyImageWidget { } return false; } + + function _fix_widows($text) { + return preg_replace("#([^\ ]+)\ ([^\ \>]+)($|

|)#", '\1 \2\3', $text); + } } function the_hubblesite_daily_image_widget() { diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 74e461f..3cbed05 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -59,27 +59,21 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { array( "image", array( - '//div[@id="hubblesite-daily-image"]' => true, - '//div/a[@href="gallery_url" and @title="title"]' => true, - '//div/a/img[@src="image_url" and @alt="title"]' => true, + '//div[@id="hubblesite-daily-image"]' => false, + '//a[@href="gallery_url" and @title="title"]' => true, + '//a/img[@src="image_url" and @alt="title"]' => true, ) ), array( "title", array( - '//div/a[@href="gallery_url" and @id="hubblesite-daily-image-title"]' => "title" - ) - ), - array( - "styles", - array( - '//style[@type="text/css"]' => true + '//a[@href="gallery_url" and @id="hubblesite-daily-image-title"]' => "title" ) ), array( "credits", array( - '//div/div[@id="hubblesite-daily-image-credits"]' => 'credits' + '//div[@id="hubblesite-daily-image-credits"]' => 'credits' ) ) ); @@ -92,7 +86,12 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { update_option('hubblesite-daily-image-options', $option_string); ob_start(); - $this->diw->render(); + $this->diw->render(array( + 'before_widget' => "", + 'after_widget' => "", + 'before_title' => "", + 'after_title' => "" + )); $result = ob_get_clean(); $this->assertTrue(!empty($result)); @@ -105,8 +104,8 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { function providerTestGetDisplayOptions() { return array( - array("", array("title", "image", "styles")), - array("meow", array("title", "image", "styles")), + array("", array("title", "image")), + array("meow", array("title", "image")), array("title", array("title")), array("title,image", array("title", "image")), array("title,meow", array("title")) @@ -302,6 +301,14 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { $this->assertTrue($diw->_load_data()); $this->assertTrue(is_array(get_option('hubblesite-daily-image-cache'))); } + + function testWidowProtection() { + $this->assertEquals("this is fixed", $this->diw->_fix_widows("this is fixed")); + $this->assertEquals("

this is fixed

", $this->diw->_fix_widows("

this is fixed

")); + $this->assertEquals("this is fixed", $this->diw->_fix_widows("this is fixed")); + $this->assertEquals("word", $this->diw->_fix_widows("word")); + $this->assertEquals("

this is fixed

Also fixed

", $this->diw->_fix_widows("

this is fixed

Also fixed

")); + } } ?> \ No newline at end of file