working on cached data
This commit is contained in:
parent
bfc2114ed7
commit
a06b70e6ed
@ -8,12 +8,24 @@ class DailyImageWidget {
|
|||||||
'styles'
|
'styles'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$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->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(
|
||||||
|
"image" => "Daily Image",
|
||||||
|
"title" => "Image Title",
|
||||||
|
"caption" => "Image Caption",
|
||||||
|
"credits" => "Credits",
|
||||||
|
"styles" => "HubbleSite Styles",
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_register_sidebar_widget("hubblesite-daily-image", "HubbleSite Daily Image", array($this, "render"));
|
||||||
|
register_widget_control("hubblesite-daily-image", array($this, "render_ui"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,10 +35,7 @@ class DailyImageWidget {
|
|||||||
$display_options = get_option('hubblesite-daily-image-options');
|
$display_options = get_option('hubblesite-daily-image-options');
|
||||||
$this->display_options = array();
|
$this->display_options = array();
|
||||||
if (!empty($display_options)) {
|
if (!empty($display_options)) {
|
||||||
$this->display_options = array_intersect(
|
$this->display_options = array_intersect(explode(",", $display_options), array_keys($this->_valid_options));
|
||||||
explode(",", $display_options),
|
|
||||||
array("title", "image", "styles", "caption", "credits")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($this->display_options)) {
|
if (empty($this->display_options)) {
|
||||||
@ -77,6 +86,17 @@ class DailyImageWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function render_ui() {
|
||||||
|
echo "<p>Show on Widget:</p>";
|
||||||
|
|
||||||
|
foreach ($this->_valid_options as $option => $label) {
|
||||||
|
echo "<label>";
|
||||||
|
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" />";
|
||||||
|
echo $label;
|
||||||
|
echo "</label>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a string of XML from the HubbleSite Daily Gallery Image feed.
|
* Parse a string of XML from the HubbleSite Daily Gallery Image feed.
|
||||||
*/
|
*/
|
||||||
@ -142,6 +162,22 @@ class DailyImageWidget {
|
|||||||
function _character_data_handler($parser, $data) {
|
function _character_data_handler($parser, $data) {
|
||||||
$this->_character_data .= $data;
|
$this->_character_data .= $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _get_cached_data() {
|
||||||
|
$result = get_option('hubblesite-daily-image-cache');
|
||||||
|
|
||||||
|
if (is_string($result)) {
|
||||||
|
if (($data = @unserialize($result)) !== false) {
|
||||||
|
list($timestamp, $cached_data) = $data;
|
||||||
|
|
||||||
|
if (($timestamp + $this->_cache_time) > time()) {
|
||||||
|
$is_valid = (count(array_intersect()) == count($this->_valid_column_names));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
@ -6,9 +6,11 @@ require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
|
|||||||
|
|
||||||
class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
||||||
function setUp() {
|
function setUp() {
|
||||||
|
_reset_wp();
|
||||||
|
|
||||||
$this->diw = new DailyImageWidget();
|
$this->diw = new DailyImageWidget();
|
||||||
|
|
||||||
$this->diw->data = array(
|
$this->sample_data = array(
|
||||||
'title' => 'title',
|
'title' => 'title',
|
||||||
'caption' => 'caption',
|
'caption' => 'caption',
|
||||||
'date' => '12345',
|
'date' => '12345',
|
||||||
@ -17,7 +19,13 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
|||||||
'credits' => 'credits'
|
'credits' => 'credits'
|
||||||
);
|
);
|
||||||
|
|
||||||
_reset_wp();
|
$this->diw->data = $this->sample_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
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']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function providerTestRetrieveJunkData() {
|
function providerTestRetrieveJunkData() {
|
||||||
@ -155,19 +163,41 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array(
|
$this->sample_data,
|
||||||
'title' => 'title',
|
|
||||||
'caption' => 'caption',
|
|
||||||
'date' => '12345',
|
|
||||||
'image_url' => 'image_url',
|
|
||||||
'gallery_url' => 'gallery_url',
|
|
||||||
'credits' => 'credits'
|
|
||||||
),
|
|
||||||
$result,
|
$result,
|
||||||
"simplexml? $simplexml"
|
"simplexml? $simplexml"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testWidgetUI() {
|
||||||
|
ob_start();
|
||||||
|
$this->diw->render_ui();
|
||||||
|
$result = ob_get_clean();
|
||||||
|
|
||||||
|
$this->assertTrue(!empty($result));
|
||||||
|
|
||||||
|
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
|
||||||
|
foreach ($this->diw->_valid_options as $option => $label) {
|
||||||
|
$xpath = "//label[contains(text(), '${label}')]";
|
||||||
|
$this->assertTrue(_xpath_test($xml, $xpath, true), $xpath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetCachedData() {
|
||||||
|
$test_time = time() + 86500;
|
||||||
|
update_option('hubblesite-daily-image-cache', serialize(array($test_time, $this->sample_data)));
|
||||||
|
$this->assertEquals(array($test_time, $this->sample_data), $this->diw->_get_cached_data());
|
||||||
|
|
||||||
|
$test_time = time() - 86500;
|
||||||
|
update_option('hubblesite-daily-image-cache', serialize(array($test_time, $this->sample_data)));
|
||||||
|
$this->assertEquals(false, $this->diw->_get_cached_data());
|
||||||
|
|
||||||
|
update_option('hubblesite-daily-image-cache', null);
|
||||||
|
$this->assertEquals(false, $this->diw->_get_cached_data());
|
||||||
|
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue
Block a user