From 0097bc65dc1b2659c6a21e6a69ac628b5e1c0cbe Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 10 Jun 2009 15:41:27 -0400 Subject: [PATCH] starting work on widget controls --- README | 27 ++++++-------- classes/DailyImageWidget.php | 68 +++++++++++++++++++++++++++++------ daily-image-widget.php | 26 +++++++++++++- test/DailyImageWidgetTest.php | 51 +++++++++++++++++++++++--- 4 files changed, 139 insertions(+), 33 deletions(-) diff --git a/README b/README index 6744d5d..8ce7a55 100644 --- a/README +++ b/README @@ -1,21 +1,16 @@ The HubbleSite Daily Image Widget embeds a daily HubbleSite Gallery image on your WordPress blog. -Copyright (c) 2009 John Bintz +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in the -Software without restriction, including without limitation the rights to use, -copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the -Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA diff --git a/classes/DailyImageWidget.php b/classes/DailyImageWidget.php index aaa7fd7..7ccc78b 100644 --- a/classes/DailyImageWidget.php +++ b/classes/DailyImageWidget.php @@ -1,7 +1,7 @@ default_display_options = array( 'title', 'image', @@ -11,23 +11,61 @@ class DailyImageWidget { $this->_cache_time = 86400; $this->data_source = "http://hubblesite.org/gallery/album/daily_image.php"; - $this->data = false; $this->has_simplexml = class_exists('SimpleXMLElement'); $this->_valid_column_names = array('title', 'caption', 'date', 'image_url', 'gallery_url', 'credits'); $this->_valid_options = array( - "image" => "Daily Image", - "title" => "Image Title", - "caption" => "Image Caption", - "credits" => "Credits", - "styles" => "HubbleSite Styles", + "image" => __("Daily Image", "hubblesite-daily-image-widget"), + "title" => __("Image Title", "hubblesite-daily-image-widget"), + "caption" => __("Image Caption", "hubblesite-daily-image-widget"), + "credits" => __("Credits", "hubblesite-daily-image-widget"), + "styles" => __("HubbleSite Styles", "hubblesite-daily-image-widget"), ); - wp_register_sidebar_widget("hubblesite-daily-image", "HubbleSite Daily Image", array($this, "render")); - register_widget_control("hubblesite-daily-image", array($this, "render_ui")); + add_action('init', array($this, "_init")); + + if (!$skip_load_data) { + if (!$this->_load_data()) { + add_action("admin_notices", array($this, "_connection_warning")); + } + } else { + $this->data = false; + } + + $this->display_options = $this->get_display_options(); } + function _init() { + register_sidebar_widget(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array($this, "render")); + register_widget_control(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array($this, "render_ui")); + } + + function _connection_warning() { + echo "
"; + _e("HubbleSite Daily Image Widget was unable to retrieve new data from HubbleSite.", "hubblesite-daily-image-widget"); + _e("The widget will appear as empty in your site until data can be downloaded again.", "hubblesite-daily-image-widget"); + echo "
"; + } + + function _get_from_data_source() { + return @file_get_contents($this->data_source); + } + + function _load_data() { + if (($result = $this->_get_cached_data()) === false) { + if (($xml_text = $this->_get_from_data_source()) !== false) { + if (($result = $this->parse_xml($xml_text)) !== false) { + update_option('hubblesite-daily-image-cache', array(time(), $result)); + return true; + } + } + return false; + } else { + return true; + } + } + /** * Get the list of display options from the WordPress options database. */ @@ -42,6 +80,8 @@ class DailyImageWidget { $this->display_options = $this->default_display_options; } + update_option('hubblesite-daily-image-options', implode(",", $this->display_options)); + return $this->display_options; } @@ -87,13 +127,16 @@ class DailyImageWidget { } function render_ui() { - echo "

Show on Widget:

"; + echo "

"; + _e("Show on Widget:", "hubblesite-daily-image-widget"); + echo "

"; foreach ($this->_valid_options as $option => $label) { echo ""; + echo "
"; } } @@ -176,7 +219,10 @@ class DailyImageWidget { if (!isset($cached_data[$field])) { $is_valid = false; break; } } - return ($is_valid) ? $cached_data : false; + if ($is_valid) { + $this->data = $cached_data; + return $cached_data; + } } } } diff --git a/daily-image-widget.php b/daily-image-widget.php index b8d1812..74a11d3 100644 --- a/daily-image-widget.php +++ b/daily-image-widget.php @@ -1,7 +1,31 @@ \ No newline at end of file diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 6c0687c..7ce5ea4 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -1,5 +1,6 @@ diw = new DailyImageWidget(); + $this->diw = new DailyImageWidget(true); $this->sample_data = array( 'title' => 'title', @@ -23,9 +24,8 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { } function testWidgetRegistered() { - global $wp_test_expectations; - $this->assertEquals("hubblesite-daily-image", $wp_test_expectations['sidebar_widgets'][0]['id']); - $this->assertEquals("hubblesite-daily-image", $wp_test_expectations['widget_controls'][0]['name']); + global $wp_test_expectations; + $this->assertEquals("_init", $wp_test_expectations['actions']['init'][1]); } function providerTestRetrieveJunkData() { @@ -123,10 +123,16 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { */ function testGetDisplayOptions($options, $result) { update_option('hubblesite-daily-image-options', $options); - $this->assertEquals($result, $this->diw->get_display_options()); } + function testGetDefaultDisplayOptions() { + _reset_wp(); + $this->assertFalse(get_option('hubblesite-daily-image-options')); + $this->diw->get_display_options(); + $this->assertTrue(get_option('hubblesite-daily-image-options') !== false); + } + function providerTestParseBadXML() { return array( array(null), @@ -196,6 +202,41 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { update_option('hubblesite-daily-image-cache', null); $this->assertEquals(false, $this->diw->_get_cached_data()); } + + function testLoadData() { + $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); + $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(false)); + $diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue(false)); + _reset_wp(); + + $this->assertFalse($diw->_load_data()); + $this->assertFalse(is_array(get_option('hubblesite-daily-image-cache'))); + + $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); + $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(true)); + _reset_wp(); + + $this->assertTrue($diw->_load_data()); + $this->assertFalse(is_array(get_option('hubblesite-daily-image-cache'))); + + $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); + $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(false)); + $diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue(true)); + $diw->expects($this->once())->method('parse_xml')->will($this->returnValue(false)); + _reset_wp(); + + $this->assertFalse($diw->_load_data()); + $this->assertFalse(is_array(get_option('hubblesite-daily-image-cache'))); + + $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); + $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(false)); + $diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue(true)); + $diw->expects($this->once())->method('parse_xml')->will($this->returnValue(true)); + _reset_wp(); + + $this->assertTrue($diw->_load_data()); + $this->assertTrue(is_array(get_option('hubblesite-daily-image-cache'))); + } } ?> \ No newline at end of file