starting work on widget controls

This commit is contained in:
John Bintz 2009-06-10 15:41:27 -04:00
parent 19c8a6ba57
commit 0097bc65dc
4 changed files with 139 additions and 33 deletions

27
README
View File

@ -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.
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

View File

@ -1,7 +1,7 @@
<?php
class DailyImageWidget {
function DailyImageWidget() {
function DailyImageWidget($skip_load_data = false) {
$this->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 "<div class=\"updated fade\">";
_e("<strong>HubbleSite Daily Image Widget</strong> 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 "</div>";
}
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 "<p>Show on Widget:</p>";
echo "<p>";
_e("Show on Widget:", "hubblesite-daily-image-widget");
echo "</p>";
foreach ($this->_valid_options as $option => $label) {
echo "<label>";
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" />";
echo $label;
echo "</label>";
echo "<br />";
}
}
@ -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;
}
}
}
}

View File

@ -1,7 +1,31 @@
<?php
/*
Plugin Name: HubbleSite Daily Image Widget
Plugin URI: http://github.com/johnbintz/hubblesite-daily-image-wordpress/tree/master
Description: The HubbleSite Daily Image Widget embeds a daily HubbleSite Gallery image on your WordPress blog.
Version: 0.1
Author: John Bintz
Author URI: http://hubblesite.org/
Copyright 2009 John Bintz (email : bintz@stsci.edu)
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.
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.
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
*/
require_once("classes/DailyImageWidget.php");
$daily_image_widget = new DailyImageWidget();
?>

View File

@ -1,5 +1,6 @@
<?php
error_reporting(E_STRICT);
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/DailyImageWidget.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
@ -8,7 +9,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->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')));
}
}
?>