hubblesite-daily-image-word.../classes/DailyImageWidget.php

293 lines
8.9 KiB
PHP
Raw Permalink Normal View History

2009-06-02 20:12:47 +00:00
<?php
2009-06-24 21:50:33 +00:00
/**
* Show a HubbleSite daily image as a widget.
*/
2009-06-02 20:12:47 +00:00
class DailyImageWidget {
2009-12-21 21:36:57 +00:00
var $display_options = array();
2009-06-24 21:50:33 +00:00
/**
* Initialize the widget.
* For unit testing purposes, you can disable remote data loading by passing true to this function.
* @param boolean $skip_load_data True if data from the remote server should not be loaded.
*/
2009-06-10 19:41:27 +00:00
function DailyImageWidget($skip_load_data = false) {
2009-06-04 21:36:51 +00:00
$this->default_display_options = array(
'title',
'image'
2009-06-09 19:55:50 +00:00
);
2009-12-21 21:36:57 +00:00
2009-06-09 21:45:59 +00:00
$this->_cache_time = 86400;
2009-07-15 15:53:44 +00:00
$this->_source_stamp = "&amp;f=wpw";
2009-06-09 19:55:50 +00:00
$this->data_source = "http://hubblesite.org/gallery/album/daily_image.php";
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
$this->has_simplexml = class_exists('SimpleXMLElement');
2009-12-21 21:36:57 +00:00
2009-06-11 13:09:13 +00:00
$this->_valid_column_names = array('title', 'date', 'image_url', 'gallery_url', 'credits');
2009-06-09 21:45:59 +00:00
$this->_valid_options = array(
2009-06-10 19:41:27 +00:00
"image" => __("Daily Image", "hubblesite-daily-image-widget"),
"title" => __("Image Title", "hubblesite-daily-image-widget"),
"credits" => __("Credits", "hubblesite-daily-image-widget")
2009-06-09 21:45:59 +00:00
);
2009-12-21 21:36:57 +00:00
2009-06-10 19:41:27 +00:00
add_action('init', array($this, "_init"));
2009-06-04 21:36:51 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* WordPress init hook.
*/
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreStart
2009-06-10 19:41:27 +00:00
function _init() {
2009-11-02 18:05:07 +00:00
wp_register_sidebar_widget(
'hubblesite-daily-image',
__("HubbleSite Daily Image", "hubblesite-daily-image-widget"),
array(&$this, "render"),
array(
'description' => __('Embed a daily HubbleSite Gallery image on your WordPress blog.', 'hubblesite-daily-image-widget')
)
);
2009-12-21 21:36:57 +00:00
register_widget_control(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array(&$this, "render_ui"));
2009-07-15 15:53:44 +00:00
if (!$skip_load_data) {
if (!$this->_load_data()) {
add_action("admin_notices", array(&$this, "_connection_warning"));
}
} else {
$this->data = false;
}
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
$this->handle_post();
$this->get_display_options();
2009-06-10 19:41:27 +00:00
}
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreEnd
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Display a warning if the connection failed.
*/
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreStart
2009-06-10 19:41:27 +00:00
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>";
}
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreEnd
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Wrapper around a remote data call for unit testing purposes.
* @return string The data from the remote source.
*/
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreStart
2009-06-10 19:41:27 +00:00
function _get_from_data_source() {
2009-11-02 18:05:07 +00:00
$response = wp_remote_request($this->data_source, array('method' => 'GET'));
if (!is_wp_error($response)) {
if (isset($response['body'])) {
2009-12-21 21:36:57 +00:00
return $response['body'];
}
2009-07-15 12:06:33 +00:00
}
2009-11-02 18:05:07 +00:00
return false;
2009-06-10 19:41:27 +00:00
}
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreEnd
2009-06-10 19:41:27 +00:00
2009-06-24 21:50:33 +00:00
/**
* Load the remote data into the object.
* This will try to pull from cache and, if necessary, retrieve and parse the XML from the
* remote server. If any of this fails, returns false.
* @return boolean True if data could be loaded, false otherwise.
*/
2009-06-10 19:41:27 +00:00
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;
}
}
2009-06-24 21:50:33 +00:00
/**
* Handle updating the widget options.
*/
2009-06-10 20:44:19 +00:00
function handle_post() {
if (isset($_POST['hubblesite']['_wpnonce'])) {
if (wp_verify_nonce($_POST['hubblesite']['_wpnonce'], 'hubble')) {
$options = array();
foreach ($this->_valid_options as $option => $label) {
if (isset($_POST['hubblesite'][$option])) {
$options[] = $option;
}
}
$this->display_options = $options;
update_option('hubblesite-daily-image-options', implode(",", $this->display_options));
}
}
}
2009-06-09 19:55:50 +00:00
/**
* Get the list of display options from the WordPress options database.
*/
2009-06-04 21:36:51 +00:00
function get_display_options() {
$display_options = get_option('hubblesite-daily-image-options');
$this->display_options = array();
if (!empty($display_options)) {
2009-06-09 21:45:59 +00:00
$this->display_options = array_intersect(explode(",", $display_options), array_keys($this->_valid_options));
2009-06-04 21:36:51 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
if (empty($this->display_options)) {
$this->display_options = $this->default_display_options;
}
2009-06-10 19:41:27 +00:00
update_option('hubblesite-daily-image-options', implode(",", $this->display_options));
2009-06-04 21:36:51 +00:00
return $this->display_options;
}
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
/**
* Render the widget.
2009-06-24 21:50:33 +00:00
* @param array $args The theme's widget layout arguments.
2009-06-09 19:55:50 +00:00
*/
2009-12-21 21:36:57 +00:00
function render($args = array()) {
2009-06-04 21:36:51 +00:00
if (!empty($this->data) && is_array($this->data)) {
extract($args);
2009-06-04 21:36:51 +00:00
$options = $this->get_display_options();
2009-12-21 21:36:57 +00:00
echo $before_widget;
echo $before_title;
echo "HubbleSite Daily Image";
echo $after_title;
2009-06-04 21:36:51 +00:00
if (in_array("image", $options)) {
2009-07-15 15:53:44 +00:00
echo '<a href="' . $this->data['gallery_url'] . $this->_source_stamp . '" title="' . $this->data['title'] . '">';
2009-06-11 13:09:13 +00:00
echo '<img src="' . $this->data['image_url'] . '" alt="' . $this->data['title'] . '" width="100%" />';
2009-06-04 21:36:51 +00:00
echo '</a>';
}
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
if (in_array("title", $options)) {
2009-07-15 15:53:44 +00:00
echo '<a id="hubblesite-daily-image-title" href="' . $this->data['gallery_url'] . $this->_source_stamp . '">';
echo $this->_fix_widows($this->data['title']);
2009-06-04 21:36:51 +00:00
echo '</a>';
}
if (in_array("credits", $options)) {
echo '<div id="hubblesite-daily-image-credits">';
echo $this->_fix_widows($this->data['credits']);
2009-06-04 21:36:51 +00:00
echo '</div>';
}
2009-12-21 21:36:57 +00:00
echo $after_widget;
2009-06-03 21:29:23 +00:00
}
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Render the widget admin UI.
*/
2009-06-09 21:45:59 +00:00
function render_ui() {
2009-06-10 20:44:19 +00:00
echo "<input type=\"hidden\" name=\"hubblesite[_wpnonce]\" value=\"" . wp_create_nonce('hubble') . "\" />";
2009-06-10 19:41:27 +00:00
echo "<p>";
2009-06-10 20:44:19 +00:00
_e("Show on Widget <em>(must select at least one)</em>:", "hubblesite-daily-image-widget");
2009-06-10 19:41:27 +00:00
echo "</p>";
2009-12-21 21:36:57 +00:00
2009-06-09 21:45:59 +00:00
foreach ($this->_valid_options as $option => $label) {
echo "<label>";
2009-06-10 20:44:19 +00:00
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" " . (in_array($option, $this->display_options) ? "checked=\"checked\"" : "") . "/> ";
2009-06-09 21:45:59 +00:00
echo $label;
echo "</label>";
2009-06-10 19:41:27 +00:00
echo "<br />";
2009-06-09 21:45:59 +00:00
}
}
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
/**
* Parse a string of XML from the HubbleSite Daily Gallery Image feed.
2009-06-24 21:50:33 +00:00
* This will try to use SimpleXML if vailable. If not, will fall back on Expat.
* @param string $xml_text The text to parse.
* @return array|boolean The retrieved data, or false on failure.
2009-06-09 19:55:50 +00:00
*/
function parse_xml($xml_text) {
2009-07-15 15:53:44 +00:00
$parser = xml_parser_create();
$this->_character_data = "";
$this->_xml_data = array();
xml_set_element_handler(
$parser,
array(&$this, "_start_element_handler"),
array(&$this, "_end_element_handler")
);
xml_set_character_data_handler($parser, array(&$this, "_character_data_handler"));
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
$this->data = false;
if (xml_parse($parser, $xml_text)) {
if (count($this->_xml_data) == count($this->_valid_column_names)) {
2009-12-21 21:36:57 +00:00
$this->data = $this->_xml_data;
2009-06-09 19:55:50 +00:00
}
}
return $this->data;
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Expat start element handler.
*/
2009-06-09 19:55:50 +00:00
function _start_element_handler($parser, $name, $attributes) {
2009-12-21 21:36:57 +00:00
$this->_character_data = "";
2009-06-09 19:55:50 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Expat end element handler.
*/
2009-06-09 19:55:50 +00:00
function _end_element_handler($parser, $name) {
$name = strtolower($name);
if (in_array($name, $this->_valid_column_names)) {
$this->_xml_data[$name] = $this->_character_data;
}
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
$this->_character_data = "";
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Expat character data handler.
*/
2009-06-09 19:55:50 +00:00
function _character_data_handler($parser, $data) {
$this->_character_data .= $data;
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Retrieve the cached data from WP Options.
* @return array|boolean The cached data or false upon failure.
*/
2009-06-09 21:45:59 +00:00
function _get_cached_data() {
2009-12-21 21:36:57 +00:00
if (($result = get_option('hubblesite-daily-image-cache')) !== false) {
2009-06-24 21:50:49 +00:00
list($timestamp, $cached_data) = $result;
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:49 +00:00
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; }
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:49 +00:00
if ($is_valid) {
$this->data = $cached_data;
return $cached_data;
2009-06-09 21:45:59 +00:00
}
}
}
return false;
}
2009-12-21 21:36:57 +00:00
2009-06-24 21:50:33 +00:00
/**
* Try to ensure that no words in a paragraph or link are widowed.
* @param string $text The text to process.
* @return string The processed text.
*/
function _fix_widows($text) {
return preg_replace("#([^\ ]+)\ ([^\ \>]+)($|</p>|</a>)#", '\1&nbsp;\2\3', $text);
}
2009-06-02 20:12:47 +00:00
}
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreStart
2009-06-10 21:08:26 +00:00
function the_hubblesite_daily_image_widget() {
$diw = new DailyImageWidget();
$diw->render();
}
2009-12-21 21:46:16 +00:00
// @codeCoverageIgnoreEnd