starting work on widget controls
This commit is contained in:
parent
19c8a6ba57
commit
0097bc65dc
27
README
27
README
@ -1,21 +1,16 @@
|
|||||||
The HubbleSite Daily Image Widget embeds a daily HubbleSite Gallery image on
|
The HubbleSite Daily Image Widget embeds a daily HubbleSite Gallery image on
|
||||||
your WordPress blog.
|
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 program is distributed in the hope that it will be useful,
|
||||||
this software and associated documentation files (the "Software"), to deal in the
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
Software without restriction, including without limitation the rights to use,
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
GNU General Public License for more details.
|
||||||
Software, and to permit persons to whom the Software is furnished to do so,
|
|
||||||
subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
You should have received a copy of the GNU General Public License
|
||||||
copies or substantial portions of the Software.
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
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.
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class DailyImageWidget {
|
class DailyImageWidget {
|
||||||
function DailyImageWidget() {
|
function DailyImageWidget($skip_load_data = false) {
|
||||||
$this->default_display_options = array(
|
$this->default_display_options = array(
|
||||||
'title',
|
'title',
|
||||||
'image',
|
'image',
|
||||||
@ -11,23 +11,61 @@ class DailyImageWidget {
|
|||||||
$this->_cache_time = 86400;
|
$this->_cache_time = 86400;
|
||||||
|
|
||||||
$this->data_source = "http://hubblesite.org/gallery/album/daily_image.php";
|
$this->data_source = "http://hubblesite.org/gallery/album/daily_image.php";
|
||||||
$this->data = false;
|
|
||||||
|
|
||||||
$this->has_simplexml = class_exists('SimpleXMLElement');
|
$this->has_simplexml = class_exists('SimpleXMLElement');
|
||||||
|
|
||||||
$this->_valid_column_names = array('title', 'caption', 'date', 'image_url', 'gallery_url', 'credits');
|
$this->_valid_column_names = array('title', 'caption', 'date', 'image_url', 'gallery_url', 'credits');
|
||||||
$this->_valid_options = array(
|
$this->_valid_options = array(
|
||||||
"image" => "Daily Image",
|
"image" => __("Daily Image", "hubblesite-daily-image-widget"),
|
||||||
"title" => "Image Title",
|
"title" => __("Image Title", "hubblesite-daily-image-widget"),
|
||||||
"caption" => "Image Caption",
|
"caption" => __("Image Caption", "hubblesite-daily-image-widget"),
|
||||||
"credits" => "Credits",
|
"credits" => __("Credits", "hubblesite-daily-image-widget"),
|
||||||
"styles" => "HubbleSite Styles",
|
"styles" => __("HubbleSite Styles", "hubblesite-daily-image-widget"),
|
||||||
);
|
);
|
||||||
|
|
||||||
wp_register_sidebar_widget("hubblesite-daily-image", "HubbleSite Daily Image", array($this, "render"));
|
add_action('init', array($this, "_init"));
|
||||||
register_widget_control("hubblesite-daily-image", array($this, "render_ui"));
|
|
||||||
|
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.
|
* Get the list of display options from the WordPress options database.
|
||||||
*/
|
*/
|
||||||
@ -42,6 +80,8 @@ class DailyImageWidget {
|
|||||||
$this->display_options = $this->default_display_options;
|
$this->display_options = $this->default_display_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_option('hubblesite-daily-image-options', implode(",", $this->display_options));
|
||||||
|
|
||||||
return $this->display_options;
|
return $this->display_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,13 +127,16 @@ class DailyImageWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function render_ui() {
|
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) {
|
foreach ($this->_valid_options as $option => $label) {
|
||||||
echo "<label>";
|
echo "<label>";
|
||||||
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" />";
|
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" />";
|
||||||
echo $label;
|
echo $label;
|
||||||
echo "</label>";
|
echo "</label>";
|
||||||
|
echo "<br />";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +219,10 @@ class DailyImageWidget {
|
|||||||
if (!isset($cached_data[$field])) { $is_valid = false; break; }
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,31 @@
|
|||||||
<?php
|
<?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");
|
require_once("classes/DailyImageWidget.php");
|
||||||
|
|
||||||
$daily_image_widget = new DailyImageWidget();
|
$daily_image_widget = new DailyImageWidget();
|
||||||
|
|
||||||
?>
|
?>
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
error_reporting(E_STRICT);
|
||||||
require_once('PHPUnit/Framework.php');
|
require_once('PHPUnit/Framework.php');
|
||||||
require_once(dirname(__FILE__) . '/../classes/DailyImageWidget.php');
|
require_once(dirname(__FILE__) . '/../classes/DailyImageWidget.php');
|
||||||
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
|
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
|
||||||
@ -8,7 +9,7 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
|||||||
function setUp() {
|
function setUp() {
|
||||||
_reset_wp();
|
_reset_wp();
|
||||||
|
|
||||||
$this->diw = new DailyImageWidget();
|
$this->diw = new DailyImageWidget(true);
|
||||||
|
|
||||||
$this->sample_data = array(
|
$this->sample_data = array(
|
||||||
'title' => 'title',
|
'title' => 'title',
|
||||||
@ -23,9 +24,8 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testWidgetRegistered() {
|
function testWidgetRegistered() {
|
||||||
global $wp_test_expectations;
|
global $wp_test_expectations;
|
||||||
$this->assertEquals("hubblesite-daily-image", $wp_test_expectations['sidebar_widgets'][0]['id']);
|
$this->assertEquals("_init", $wp_test_expectations['actions']['init'][1]);
|
||||||
$this->assertEquals("hubblesite-daily-image", $wp_test_expectations['widget_controls'][0]['name']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function providerTestRetrieveJunkData() {
|
function providerTestRetrieveJunkData() {
|
||||||
@ -123,10 +123,16 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
|||||||
*/
|
*/
|
||||||
function testGetDisplayOptions($options, $result) {
|
function testGetDisplayOptions($options, $result) {
|
||||||
update_option('hubblesite-daily-image-options', $options);
|
update_option('hubblesite-daily-image-options', $options);
|
||||||
|
|
||||||
$this->assertEquals($result, $this->diw->get_display_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() {
|
function providerTestParseBadXML() {
|
||||||
return array(
|
return array(
|
||||||
array(null),
|
array(null),
|
||||||
@ -196,6 +202,41 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
|||||||
update_option('hubblesite-daily-image-cache', null);
|
update_option('hubblesite-daily-image-cache', null);
|
||||||
$this->assertEquals(false, $this->diw->_get_cached_data());
|
$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')));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue
Block a user