switch to better option handling

This commit is contained in:
John Bintz 2009-06-24 17:50:49 -04:00
parent cb4c7cd68e
commit 33ba3f3a42
2 changed files with 13 additions and 17 deletions

View File

@ -260,22 +260,18 @@ class DailyImageWidget {
* @return array|boolean The cached data or false upon failure.
*/
function _get_cached_data() {
$result = get_option('hubblesite-daily-image-cache');
if (($result = get_option('hubblesite-daily-image-cache')) !== false) {
list($timestamp, $cached_data) = $result;
if (is_string($result)) {
if (($data = @unserialize($result)) !== false) {
list($timestamp, $cached_data) = $data;
if (($timestamp + $this->_cache_time) > time()) {
$is_valid = true;
foreach ($this->_valid_column_names as $field) {
if (!isset($cached_data[$field])) { $is_valid = false; break; }
}
if (($timestamp + $this->_cache_time) > time()) {
$is_valid = true;
foreach ($this->_valid_column_names as $field) {
if (!isset($cached_data[$field])) { $is_valid = false; break; }
}
if ($is_valid) {
$this->data = $cached_data;
return $cached_data;
}
if ($is_valid) {
$this->data = $cached_data;
return $cached_data;
}
}
}

View File

@ -256,11 +256,11 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
function testGetCachedData() {
$test_time = time() + 86500;
update_option('hubblesite-daily-image-cache', serialize(array($test_time, $this->sample_data)));
update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data));
$this->assertEquals($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)));
update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data));
$this->assertEquals(false, $this->diw->_get_cached_data());
update_option('hubblesite-daily-image-cache', null);