From 577d5d307e7d38e9619a653d16d5bdbd607ee1fb Mon Sep 17 00:00:00 2001 From: donncha Date: Mon, 21 May 2007 18:38:24 +0000 Subject: Forgot this file git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@973 7be80a69-a1ef-0310-a953-fb0f7c49ff36 --- wp-includes/widgets.php | 992 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 992 insertions(+) create mode 100644 wp-includes/widgets.php (limited to 'wp-includes') diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php new file mode 100644 index 0000000..6365261 --- /dev/null +++ b/wp-includes/widgets.php @@ -0,0 +1,992 @@ + 1 ) { + $_args['name'] = isset($args['name']) ? $args['name'] : sprintf(__('Sidebar %d'), $i); + } else { + $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar'); + } + $_args['id'] = isset($args['id']) ? $args['id'] : "sidebar-$i"; + register_sidebar($_args); + ++$i; + } +} + +function register_sidebar($args = array()) { + global $wp_registered_sidebars; + + if ( is_string($args) ) + parse_str($args, $args); + + $i = count($wp_registered_sidebars) + 1; + + $defaults = array( + 'name' => sprintf(__('Sidebar %d'), $i ), + 'id' => "sidebar-$i", + 'before_widget' => '
  • ', + 'after_widget' => "
  • \n", + 'before_title' => '

    ', + 'after_title' => "

    \n", + ); + + $sidebar = array_merge($defaults, $args); + + $wp_registered_sidebars[$sidebar['id']] = $sidebar; + + return $sidebar['id']; +} + +function unregister_sidebar( $name ) { + global $wp_registered_sidebars; + + if ( isset( $wp_registered_sidebars[$name] ) ) + unset( $wp_registered_sidebars[$name] ); +} + +function register_sidebar_widget($name, $output_callback, $classname = '') { + // Compat + if ( is_array($name) ) { + if ( count($name) == 3 ) + $name = sprintf($name[0], $name[2]); + else + $name = $name[0]; + } + + $id = sanitize_title($name); + $options = array(); + if ( !empty($classname) && is_string($classname) ) + $options['classname'] = $classname; + $params = array_slice(func_get_args(), 2); + $args = array($id, $name, $output_callback, $options); + if ( !empty($params) ) + $args = array_merge($args, $params); + + call_user_func_array('wp_register_sidebar_widget', $args); +} + +function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) { + + global $wp_registered_widgets, $wp_register_widget_defaults; + + $id = sanitize_title($id); + + if ( empty($output_callback) ) { + unset($wp_registered_widgets[$id]); + return; + } + + $defaults = array('classname' => $output_callback); + $options = wp_parse_args($options, $defaults); + $widget = array( + 'name' => $name, + 'id' => $id, + 'callback' => $output_callback, + 'params' => array_slice(func_get_args(), 4) + ); + $widget = array_merge($widget, $options); + + if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || !$wp_register_widget_defaults) ) + $wp_registered_widgets[$id] = $widget; +} + +function unregister_sidebar_widget($id) { + return wp_unregister_sidebar_widget($id); +} + +function wp_unregister_sidebar_widget($id) { + wp_register_sidebar_widget($id, '', ''); + wp_unregister_widget_control($id); +} + +function register_widget_control($name, $control_callback, $width = '', $height = '') { + // Compat + if ( is_array($name) ) { + if ( count($name) == 3 ) + $name = sprintf($name[0], $name[2]); + else + $name = $name[0]; + } + + $id = sanitize_title($name); + $options = array(); + if ( !empty($width) ) + $options['width'] = $width; + if ( !empty($height) ) + $options['height'] = $height; + $params = array_slice(func_get_args(), 4); + $args = array($id, $name, $control_callback, $options); + if ( !empty($params) ) + $args = array_merge($args, $params); + + call_user_func_array('wp_register_widget_control', $args); +} + +function wp_register_widget_control($id, $name, $control_callback, $options = array()) { + global $wp_registered_widget_controls, $wp_register_widget_defaults; + + $id = sanitize_title($id); + + if ( empty($control_callback) ) { + unset($wp_registered_widget_controls[$id]); + return; + } + + if ( isset($wp_registered_widget_controls[$id]) && $wp_register_widget_defaults ) + return; + + $defaults = array('width' => 300, 'height' => 200); + $options = wp_parse_args($options, $defaults); + $options['width'] = (int) $options['width']; + $options['height'] = (int) $options['height']; + $options['width'] = $options['width'] > 90 ? $options['width'] + 60 : 360; + $options['height'] = $options['height'] > 60 ? $options['height'] + 40 : 240; + + $widget = array( + 'name' => $name, + 'id' => $id, + 'callback' => $control_callback, + 'params' => array_slice(func_get_args(), 4) + ); + $widget = array_merge($widget, $options); + + $wp_registered_widget_controls[$id] = $widget; +} + +function unregister_widget_control($id) { + return wp_unregister_widget_control($id); +} + +function wp_unregister_widget_control($id) { + return wp_register_widget_control($id, '', ''); +} + +function dynamic_sidebar($index = 1) { + global $wp_registered_sidebars, $wp_registered_widgets; + + if ( is_int($index) ) { + $index = "sidebar-$index"; + } else { + $index = sanitize_title($index); + foreach ( $wp_registered_sidebars as $key => $value ) { + if ( sanitize_title($value['name']) == $index ) { + $index = $key; + break; + } + } + } + + $sidebars_widgets = wp_get_sidebars_widgets(); + + if ( empty($wp_registered_sidebars[$index]) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) ) + return false; + + $sidebar = $wp_registered_sidebars[$index]; + + $did_one = false; + foreach ( $sidebars_widgets[$index] as $id ) { + $callback = $wp_registered_widgets[$id]['callback']; + + $params = array_merge(array($sidebar), (array) $wp_registered_widgets[$id]['params']); + + // Substitute HTML id and class attributes into before_widget + $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $wp_registered_widgets[$id]['classname']); + + if ( is_callable($callback) ) { + call_user_func_array($callback, $params); + $did_one = true; + } + } + + return $did_one; +} + +function is_active_widget($callback) { + global $wp_registered_widgets; + + $sidebars_widgets = wp_get_sidebars_widgets(false); + + if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets ) + if ( is_array($widgets) ) foreach ( $widgets as $widget ) + if ( $wp_registered_widgets[$widget]['callback'] == $callback ) + return true; + + return false; +} + +function is_dynamic_sidebar() { + global $wp_registered_widgets, $wp_registered_sidebars; + $sidebars_widgets = get_option('sidebars_widgets'); + foreach ( $wp_registered_sidebars as $index => $sidebar ) { + if ( count($sidebars_widgets[$index]) ) { + foreach ( $sidebars_widgets[$index] as $widget ) + if ( array_key_exists($widget, $wp_registered_widgets) ) + return true; + } + } + return false; +} + +/* Internal Functions */ + +function wp_get_sidebars_widgets($update = true) { + global $wp_registered_widgets, $wp_registered_sidebars; + + $sidebars_widgets = get_option('sidebars_widgets'); + $_sidebars_widgets = array(); + + if ( !isset($sidebars_widgets['array_version']) ) + $sidebars_widgets['array_version'] = 1; + + switch ( $sidebars_widgets['array_version'] ) { + case 1 : + foreach ( $sidebars_widgets as $index => $sidebar ) + if ( is_array($sidebar) ) + foreach ( $sidebar as $i => $name ) { + $id = strtolower($name); + if ( isset($wp_registered_widgets[$id]) ) { + $_sidebars_widgets[$index][$i] = $id; + continue; + } + $id = sanitize_title($name); + if ( isset($wp_registered_widgets[$id]) ) { + $_sidebars_widgets[$index][$i] = $id; + continue; + } + unset($_sidebars_widgets[$index][$i]); + } + $_sidebars_widgets['array_version'] = 2; + $sidebars_widgets = $_sidebars_widgets; + unset($_sidebars_widgets); + + case 2 : + $sidebars = array_keys( $wp_registered_sidebars ); + if ( !empty( $sidebars ) ) { + // Move the known-good ones first + foreach ( $sidebars as $id ) { + if ( array_key_exists( $id, $sidebars_widgets ) ) { + $_sidebars_widgets[$id] = $sidebars_widgets[$id]; + unset($sidebars_widgets[$id], $sidebars[$id]); + } + } + + // Assign to each unmatched registered sidebar the first available orphan + unset( $sidebars_widgets[ 'array_version' ] ); + while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) ) + $_sidebars_widgets[ $sidebar ] = $widgets; + + $_sidebars_widgets['array_version'] = 3; + $sidebars_widgets = $_sidebars_widgets; + unset($_sidebars_widgets); + } + + if ( $update ) + update_option('sidebars_widgets', $sidebars_widgets); + } + + unset($sidebars_widgets['array_version']); + + return $sidebars_widgets; +} + +function wp_set_sidebars_widgets( $sidebars_widgets ) { + update_option( 'sidebars_widgets', $sidebars_widgets ); +} + +function wp_get_widget_defaults() { + global $wp_registered_sidebars; + + $defaults = array(); + + foreach ( $wp_registered_sidebars as $index => $sidebar ) + $defaults[$index] = array(); + + return $defaults; +} + +/* Default Widgets */ + +function wp_widget_pages( $args ) { + extract( $args ); + $options = get_option( 'widget_pages' ); + + $title = empty( $options['title'] ) ? __( 'Pages' ) : $options['title']; + $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby']; + + if ( $sortby == 'menu_order' ) { + $sortby = 'menu_order, post_title'; + } + + $out = wp_list_pages( 'title_li=&echo=0&sort_column=' . $sortby ); + + if ( !empty( $out ) ) { +?> + + + + + +

    +

    + + $before_title, 'title_after' => $after_title, + 'category_before' => $before_widget, 'category_after' => $after_widget, + 'show_images' => true, 'class' => 'linkcat widget' + )); + } +} + +function wp_widget_search($args) { + extract($args); +?> + +
    +
    +
    + +
    +
    + + + + + + +

    +

    +

    + + + + + + + +

    + +'; + get_calendar(); + echo ''; + echo $after_widget; +} +function wp_widget_calendar_control() { + $options = $newoptions = get_option('widget_calendar'); + if ( $_POST["calendar-submit"] ) { + $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"])); + } + if ( $options != $newoptions ) { + $options = $newoptions; + update_option('widget_calendar', $options); + } + $title = attribute_escape($options['title']); +?> +

    + + + + +
    + + + + + " name="text-submit-" value="1" /> + 9 ) $number = 9; + if ( $number < 1 ) $number = 1; + $newoptions['number'] = $number; + } + if ( $options != $newoptions ) { + $options = $newoptions; + update_option('widget_text', $options); + wp_widget_text_register($options['number']); + } +} + +function wp_widget_text_page() { + $options = $newoptions = get_option('widget_text'); +?> +
    +
    +

    +

    + +

    +
    +
    + 9 ) $number = 9; + $dims = array('width' => 460, 'height' => 350); + $class = array('classname' => 'widget_text'); + for ($i = 1; $i <= 9; $i++) { + $name = sprintf(__('Text %d'), $i); + $id = "text-$i"; // Never never never translate an id + wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_text' : /* unregister */ '', $class, $i); + wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_text_control' : /* unregister */ '', $dims, $i); + } + add_action('sidebar_admin_setup', 'wp_widget_text_setup'); + add_action('sidebar_admin_page', 'wp_widget_text_page'); +} + +function wp_widget_categories($args) { + extract($args); + $options = get_option('widget_categories'); + $c = $options['count'] ? '1' : '0'; + $h = $options['hierarchical'] ? '1' : '0'; + $d = $options['dropdown'] ? '1' : '0'; + $title = empty($options['title']) ? __('Categories') : $options['title']; + + echo $before_widget; + echo $before_title . $title . $after_title; + + $cat_args = "orderby=name&show_count={$c}&hierarchical={$h}"; + + if($d) { + wp_dropdown_categories($cat_args . '&show_option_none= ' . __('Select Category')); +?> + + + + + + +

    +

    +

    +

    + + 15 ) + $number = 15; + + $r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0"); + if ($r->have_posts()) : +?> + + + + + +

    +

    + + 15 ) + $number = 15; + + if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { + $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number"); + wp_cache_add( 'recent_comments', $comments, 'widget' ); + } +?> + + + + + + +

    +

    + + + + 320, 'height' => 90); + $class = array('classname' => 'widget_recent_comments'); + wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $class); + wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control', $dims); + + if ( is_active_widget('wp_widget_recent_comments') ) + add_action('wp_head', 'wp_widget_recent_comments_style'); +} + +function wp_widget_rss($args, $number = 1) { + require_once(ABSPATH . WPINC . '/rss.php'); + extract($args); + $options = get_option('widget_rss'); + if ( isset($options['error']) && $options['error'] ) + return; + $num_items = (int) $options[$number]['items']; + $show_summary = $options[$number]['show_summary']; + if ( empty($num_items) || $num_items < 1 || $num_items > 10 ) $num_items = 10; + $url = $options[$number]['url']; + while ( strstr($url, 'http') != $url ) + $url = substr($url, 1); + if ( empty($url) ) + return; + $rss = fetch_rss($url); + $link = clean_url(strip_tags($rss->channel['link'])); + while ( strstr($link, 'http') != $link ) + $link = substr($link, 1); + $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES))); + $title = $options[$number]['title']; + if ( empty($title) ) + $title = htmlentities(strip_tags($rss->channel['title'])); + if ( empty($title) ) + $title = $desc; + if ( empty($title) ) + $title = __('Unknown Feed'); + $url = clean_url(strip_tags($url)); + if ( file_exists(dirname(__FILE__) . '/rss.png') ) + $icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png'; + else + $icon = get_option('siteurl').'/wp-includes/images/rss.png'; + $title = "RSS $title"; +?> + + + + + +

    + " name="rss-url-" type="text" value="" /> +

    + " name="rss-title-" type="text" value="" /> +

    + " name="rss-submit-" value="1" /> + 9 ) $number = 9; + if ( $number < 1 ) $number = 1; + $newoptions['number'] = $number; + } + if ( $options != $newoptions ) { + $options = $newoptions; + update_option('widget_rss', $options); + wp_widget_rss_register($options['number']); + } +} + +function wp_widget_rss_page() { + $options = $newoptions = get_option('widget_rss'); +?> +
    +
    +

    +

    + +

    +
    +
    + 9 ) $number = 9; + $dims = array('width' => 410, 'height' => 200); + $class = array('classname' => 'widget_rss'); + for ($i = 1; $i <= 9; $i++) { + $name = sprintf(__('RSS %d'), $i); + $id = "rss-$i"; // Never never never translate an id + wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_rss' : /* unregister */ '', $class, $i); + wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_rss_control' : /* unregister */ '', $dims, $i); + } + add_action('sidebar_admin_setup', 'wp_widget_rss_setup'); + add_action('sidebar_admin_page', 'wp_widget_rss_page'); +} + +function wp_widgets_init() { + global $wp_register_widget_defaults; + + $wp_register_widget_defaults = true; + $dims90 = array('height' => 90, 'width' => 300); + $dims100 = array('height' => 100, 'width' => 300); + $dims150 = array('height' => 150, 'width' => 300); + $class = array('classname' => 'widget_pages'); + wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $class); + wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control', $dims90); + $class['classname'] = 'widget_calendar'; + wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $class); + wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control', $dims90); + $class['classname'] = 'widget_archives'; + wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $class); + wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control', $dims100); + $class['classname'] = 'widget_links'; + wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $class); + $class['classname'] = 'widget_meta'; + wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $class); + wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control', $dims90); + $class['classname'] = 'widget_search'; + wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $class); + $class['classname'] = 'widget_categories'; + wp_register_sidebar_widget('categories', __('Categories'), 'wp_widget_categories', $class); + wp_register_widget_control('categories', __('Categories'), 'wp_widget_categories_control', $dims150); + $class['classname'] = 'widget_recent_entries'; + wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $class); + wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control', $dims90); + wp_widget_text_register(); + wp_widget_rss_register(); + wp_widget_recent_comments_register(); + + $wp_register_widget_defaults = false; + + do_action('widgets_init'); +} + +add_action('init', 'wp_widgets_init', 1); + +?> -- cgit