";
foreach ($pages as $page):
// determine if widget is visible on selected page; we consider it visible if the visibility option is not set
$instance["page-{$page->ID}"] = isset($instance["page-{$page->ID}"]) ? ($instance["page-{$page->ID}"] ? true : false) : true;
if(!isset($instance["page-{$page->ID}"])) $instance["page-{$page->ID}"] = 1; ?>
ID}"); ?>" value="0">
ID}"]) ?> id="get_field_id("page-{$page->ID}"); ?>" name="get_field_name("page-{$page->ID}"); ?>" value="1" />
";
endif;
?>
ID}"] = (!isset($new_instance["page-{$page->ID}"]) ? 1 : intval($new_instance["page-{$page->ID}"]));
$instance['user-visitor'] = (!isset($new_instance['user-visitor']) ? 1 : intval($new_instance['user-visitor']));
$instance['user-registered'] = (!isset($new_instance['user-registered']) ? 1 : intval($new_instance['user-registered']));
return $instance;
}
/**
* Hide widget based on the current context
* based on "Display Widgets" plugin by Stephanie Wells - http://blog.strategy11.com/display-widgets
*
* @since 1.0
*
* @param array $instance Instance options
* @return array|bool returns instance options if widget should be visible on the current page, false otherwise
*/
function atom_widget_visibility_check($instance){
if (is_home())
$show = isset($instance['page-home']) ? ($instance['page-home']) : true;
else if (is_single())
$show = isset($instance['page-single']) ? ($instance['page-single']) : true;
else if (is_category())
$show = isset($instance['page-category']) ? ($instance['page-category']) : true;
else if (is_tag())
$show = isset($instance['page-tag']) ? ($instance['page-tag']) : true;
else if (is_author())
$show = isset($instance['page-author']) ? ($instance['page-author']) : true;
else if (is_date())
$show = isset($instance['page-date']) ? ($instance['page-date']) : true;
else if (is_search())
$show = isset($instance['page-search']) ? ($instance['page-search']) : true;
else $show = true;
if (is_page()):
global $wp_query;
$post_id = $wp_query->get_queried_object_id();
$show = isset($instance["page-{$post_id}"]) ? ($instance["page-{$post_id}"]) : true;
endif;
if (!is_user_logged_in() && isset($instance['user-visitor']) && !$instance['user-visitor']) $show = false;
if (is_user_logged_in() && isset($instance['user-registered']) && !$instance['user-registered']) $show = false;
return $show ? $instance : false;
}
/**
* Get the status of a widgetized area (replaces wp's is_active_sidebar)
* This function also verifies if the widgets inside the sidebar are visible to the current user or not (splitters are ignored too)
* Note: If we're in preview mode areas are always considered to be active, but the returned widget count may be 0 (boolean false).
* (we use type equivalence checking to avoid confusions)
*
* @since 1.0
*
* @global $wp_registered_widgets Stored registered widgets.
* @global $post Current post object
* @global $atom_area Sidebar output
*
* @param string $index Area (sidebar) ID
* @return int|bool false or the visible widget count, based on widget visibility settings and current user status
*/
function atom_is_active_area($index){
global $wp_registered_widgets, $post, $atom_area;
// always return true if the layout options are not enabled. obviously, the css designer wants to handle this
if(!atom_is_option_enabled('layout')) return true;
// -- same as atom_get_layout_type() --
if(is_404()):
$layout = 'col-1'; // 404 pages have 1 column layout
else:
// the "layout" custom field - highest priority
if(isset($post->ID)) $layout = get_post_meta($post->ID, 'layout', true);
// custom page templates - lower priority
if (empty($layout) && (is_single() || is_page()) && ($page_template = sanitize_html_class(str_replace(array('page-', '.php'), '', get_post_meta($post->ID, '_wp_page_template', true)))))
if(in_array($page_template, atom_available_layout_types())) $layout = $page_template;
// if no template is defined so far, revert to the global layout option from the theme settings - lowest priority
if(empty($layout)) $layout = atom_get_options('layout');
endif;
// -- /same as atom_get_layout_type() --
$index = (is_int($index)) ? "sidebar-{$index}" : sanitize_title($index);
$sidebars_widgets = wp_get_sidebars_widgets();
$visible_widgets = 0;
// only check widget settings if we have widgets in this sidebar
if(!empty($sidebars_widgets[$index]))
foreach($sidebars_widgets[$index] as $i => $w):
if(isset($wp_registered_widgets[$w])):
$number = $wp_registered_widgets[$w]['params'][0]['number'];
// widget settings
if(empty($wp_registered_widgets[$w]['callback'][0]->option_name))
$s = atom_get_options($wp_registered_widgets[$w]['callback_wl_redirect'][0]->option_name); // ugly for widget logic fix; need to look what's really going on there
else $s = atom_get_options($wp_registered_widgets[$w]['callback'][0]->option_name);
// widget instance settings
$s = $s[$number];
// count only visible widgets that are not "splitters"
if((strpos($w, 'atom-splitter') === false) && atom_widget_visibility_check($s)) $visible_widgets++;
endif;
endforeach;
// always show active if we're in preview mode (eg. theme setting site preview), regardless of the contents
if(atom_preview_mode()) return $visible_widgets;
// check free column(s) for sidebar(s)
if($index == 'sidebar-1' && $layout == 'col-1') return false;
if($index == 'sidebar-2' && in_array($layout, array('col-1', 'col-2-left', 'col-2-right'))) return false;
// get sidebar contents
$first_check = false;
if(!isset($atom_area[$index])):
ob_start();
dynamic_sidebar($index);
$atom_area[$index] = ob_get_clean();
$first_check = true;
endif;
// filter cannot be applied to the global variable because the column splitter can make irreversible changes
$area_contents = apply_filters('atom_area_check', $atom_area[$index], $index);
if(empty($area_contents) && $first_check) atom_add_debug_message("No active widgets in {$index}. Area disabled.");
if((!empty($sidebars_widgets[$index]) && ($visible_widgets > 0)) && !empty($area_contents)) return $visible_widgets;
return false;
}
/**
* Capture the output of a widget area (sidebar); uses output buffering to allow content filtering.
* Certain design types (like "Arclite") might need this to correct widget HTML.
* This function also checks if the number of widget splitters present in the sidebars are even, and adds the closing splitter tags if necessary.
*
* @since 1.3
*
* @param string $area Sidebar ID
* @param bool $echo Echo result, true by default
*
* @return string|bool HTML output/true, or false if sidebar is empty
*/
function atom_widget_area($area, $echo = true){
global $atom_area;
// get sidebar contents (this shouldn't run because cache should be set by is_active_area above which is called early)
if(!isset($atom_area[$area])):
ob_start();
dynamic_sidebar($area);
$atom_area[$area] = ob_get_clean();
endif;
$atom_area[$area] = apply_filters('atom_widget_area', $atom_area[$area], $area);
if(!$echo) return $atom_area[$area];
if($echo && !empty($atom_area[$area])):
echo $atom_area[$area];
return true; // echoed something
endif;
// echoed nothing
return false;
}
/**
* Get the output of a widget instance
*
* @since 1.3
* @global array $wp_registered_widgets
* @global array $wp_registered_sidebars
*
* @param string $widget_id The widget instance ID
* @param string $area Use this sidebar parameters to render the widget (default is 'arbitrary')
* @param bool|string $remove_title Display or hide the title (eg. remove 'h2', 'h3' or false to keep the title)
*
* @return string Widget HTML output
*/
function atom_get_widget($widget_id, $area = 'arbitrary', $remove_title = 'h3'){
global $wp_registered_widgets, $wp_registered_sidebars;
$widget_contents = wp_cache_get("arbitrary_{$widget_id}");
if($widget_contents === false):
// does the instance exist?
if(!isset($wp_registered_widgets[$widget_id]['callback'])) return atom_add_debug_message("Requested widget doesn't exist: {$widget_id}");
$callback = $wp_registered_widgets[$widget_id]['callback'];
ob_start(); // catch the echo output, so we can control where it appears in the text
$params = array_merge(array(array_merge($wp_registered_sidebars[$area], array('widget_id' => $widget_id, 'widget_name' => $wp_registered_widgets[$widget_id]['name']))), (array)$wp_registered_widgets[$widget_id]['params']);
// align classes?
//if($align) $params[0]['before_widget'] = str_replace('block', 'block align'.$align, $params[0]['before_widget']);
// Substitute HTML id and class attributes into before_widget
$classname_ = '';
foreach ((array)$wp_registered_widgets[$widget_id]['classname'] as $cn)
if (is_string($cn)) $classname_ .= '_'.$cn; elseif (is_object($cn)) $classname_ .= '_'.get_class($cn);
$classname_ = ltrim($classname_, '_');
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $widget_id, $classname_);
$params = apply_filters('dynamic_sidebar_params', $params);
if (is_callable($callback)) call_user_func_array($callback, $params);
// remove h3?
$widget_contents = $remove_title ? preg_replace('#<'.$remove_title.' class="title">(.*?)'.$remove_title.'>#', '', ob_get_clean()) : ob_get_clean();
wp_cache_set("arbitrary_{$area}", $widget_contents);
endif;
return apply_filters("atom_widget", $widget_contents, $widget_id); // probably useless filter
}
/**
* This is a widget form helper class.
* Its only purpose is to decrease the overall code length and make widget forms easier to work with
* -- Not used in the current version -- @todo when I have the time...
*
* @since 1.3
*/
class AtomWidgetForm{
var $widget = null;
var $instance = null;
function AtomWidgetForm($widget, $instance){ // we should use php 5's public function __construct but WP doesn't so we do the same :(
$this->widget = $widget;
$this->instance = wp_parse_args($instance, $this->widget->defaults());
}
function add_select($name, $label = '', $options, $args = array()){
ob_start();
$args = wp_parse_args($args, array(
'size' => false,
'class' => '',
'disabled' => false,
'rules' => '',
'after' => '',
'echo' => true,
));
extract($args, EXTR_SKIP);
if($rules):
$rules = $rules ? "rules=\"{$rules}\"" : '';
// @todo
endif;
$field_id = $this->widget->get_field_id($name);
$field_name = $this->widget->get_field_name($name);
$field_class = $class ? "class=\"{$class}\"": '';
?>