working on cached data
This commit is contained in:
parent
bfc2114ed7
commit
a06b70e6ed
@ -8,12 +8,24 @@ class DailyImageWidget {
|
||||
'styles'
|
||||
);
|
||||
|
||||
$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",
|
||||
);
|
||||
|
||||
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');
|
||||
$this->display_options = array();
|
||||
if (!empty($display_options)) {
|
||||
$this->display_options = array_intersect(
|
||||
explode(",", $display_options),
|
||||
array("title", "image", "styles", "caption", "credits")
|
||||
);
|
||||
$this->display_options = array_intersect(explode(",", $display_options), array_keys($this->_valid_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.
|
||||
*/
|
||||
@ -142,6 +162,22 @@ class DailyImageWidget {
|
||||
function _character_data_handler($parser, $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 {
|
||||
function setUp() {
|
||||
_reset_wp();
|
||||
|
||||
$this->diw = new DailyImageWidget();
|
||||
|
||||
$this->diw->data = array(
|
||||
$this->sample_data = array(
|
||||
'title' => 'title',
|
||||
'caption' => 'caption',
|
||||
'date' => '12345',
|
||||
@ -17,7 +19,13 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
||||
'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() {
|
||||
@ -155,19 +163,41 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'title' => 'title',
|
||||
'caption' => 'caption',
|
||||
'date' => '12345',
|
||||
'image_url' => 'image_url',
|
||||
'gallery_url' => 'gallery_url',
|
||||
'credits' => 'credits'
|
||||
),
|
||||
$this->sample_data,
|
||||
$result,
|
||||
"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