Make WordPress Core

Ticket #16125: tagcloud.v3.diff

File tagcloud.v3.diff, 5.2 KB (added by gautamgupta, 14 years ago)

Based on westi's recommendations

  • default-widgets.php

     
    992992class WP_Widget_Tag_Cloud extends WP_Widget {
    993993
    994994        function __construct() {
    995                 $widget_ops = array( 'description' => __( "Your most used tags in cloud format") );
    996                 parent::__construct('tag_cloud', __('Tag Cloud'), $widget_ops);
     995                $widget_ops = array( 'description' => __( 'Your most used tags in cloud format' ) );
     996                parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops );
    997997        }
    998998
    999999        function widget( $args, $instance ) {
    1000                 extract($args);
    1001                 $current_taxonomy = $this->_get_current_taxonomy($instance);
    1002                 if ( !empty($instance['title']) ) {
     1000                extract( $args );
     1001                $current_taxonomy = $this->_get_current_taxonomy( $instance );
     1002
     1003                if ( ! taxonomy_exists( $current_taxonomy ) )
     1004                        return;
     1005
     1006                if ( ! empty( $instance['title'] ) ) {
    10031007                        $title = $instance['title'];
    10041008                } else {
    10051009                        if ( 'post_tag' == $current_taxonomy ) {
    1006                                 $title = __('Tags');
     1010                                $title = __( 'Tags' );
    10071011                        } else {
    1008                                 $tax = get_taxonomy($current_taxonomy);
     1012                                $tax   = get_taxonomy( $current_taxonomy );
    10091013                                $title = $tax->labels->name;
    10101014                        }
    10111015                }
    1012                 $title = apply_filters('widget_title', $title, $instance, $this->id_base);
     1016                $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    10131017
    10141018                echo $before_widget;
    10151019                if ( $title )
    10161020                        echo $before_title . $title . $after_title;
    10171021                echo '<div class="tagcloud">';
    1018                 wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );
     1022                wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array( 'taxonomy' => $current_taxonomy ) ) );
    10191023                echo "</div>\n";
    10201024                echo $after_widget;
    10211025        }
    10221026
    10231027        function update( $new_instance, $old_instance ) {
    1024                 $instance['title'] = strip_tags(stripslashes($new_instance['title']));
    1025                 $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
     1028                $instance['title']    = strip_tags( stripslashes( $new_instance['title'] ) );
     1029                $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
    10261030                return $instance;
    10271031        }
    10281032
    10291033        function form( $instance ) {
    1030                 $current_taxonomy = $this->_get_current_taxonomy($instance);
    1031 ?>
    1032         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
    1033         <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
    1034         <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
    1035         <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
    1036         <?php foreach ( get_object_taxonomies('post') as $taxonomy ) :
    1037                                 $tax = get_taxonomy($taxonomy);
    1038                                 if ( !$tax->show_tagcloud || empty($tax->labels->name) )
    1039                                         continue;
    1040         ?>
    1041                 <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
    1042         <?php endforeach; ?>
    1043         </select></p><?php
     1034                $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
     1035                echo '<p><label for="' . $this->get_field_id( 'title' ) .'">' . __( 'Title:' ) . '</label>
     1036                        <input type="text" class="widefat" id="' . $this->get_field_id( 'title' ) .'" name="' . $this->get_field_name( 'title' ) .'" value="' . $instance['title'] .'" />
     1037                </p>';
     1038
     1039                $current_taxonomy = $this->_get_current_taxonomy( $instance );
     1040                $taxonomies       = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
     1041
     1042                switch ( count( $taxonomies ) ) {
     1043
     1044                        // No tag cloud supporting taxonomies found, display error message
     1045                        case 0 :
     1046                                echo '<p>' . __( 'The tag cloud would not be displayed since their are no taxonomies that support the tag cloud widget.' ) . '</p>';
     1047                                echo '<input type="hidden" id="' . $this->get_field_id( 'taxonomy' ) . '" name="' . $this->get_field_name( 'taxonomy' ) . '" value="" />';
     1048                                break;
     1049
     1050                        // Just a single tag cloud supporting taxonomy found, no need to display options
     1051                        case 1 :
     1052                                $taxonomy = array_pop( array_keys( $taxonomies ) );
     1053                                echo '<input type="hidden" id="' . $this->get_field_id( 'taxonomy' ) . '" name="' . $this->get_field_name( 'taxonomy' ) . '" value="' . esc_attr( $taxonomy ) . '" />';
     1054                                break;
     1055
     1056                        // More than one tag cloud supporting taxonomy found, display options
     1057                        default :
     1058                                echo '<p><label for="' . $this->get_field_id( 'taxonomy' ) . '">' . __( 'Taxonomy:' ) . '</label>
     1059                                        <select class="widefat" id="' . $this->get_field_id( 'taxonomy' ) . '" name="' . $this->get_field_name( 'taxonomy' ) .'">';
     1060
     1061                                foreach ( $taxonomies as $taxonomy => $tax ) {
     1062                                        echo '<option value="' . esc_attr( $taxonomy ) . '"' . selected( $taxonomy, $current_taxonomy, false ) .'>' . $tax->labels->name .'</option>';
     1063                                }
     1064
     1065                                echo '</select></p>';
     1066                }
     1067
    10441068        }
    10451069
    1046         function _get_current_taxonomy($instance) {
    1047                 if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
     1070        function _get_current_taxonomy( $instance ) {
     1071                if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) )
    10481072                        return $instance['taxonomy'];
    10491073
    1050                 return 'post_tag';
     1074                return false;
    10511075        }
    10521076}
    10531077