wp/wp-includes/widgets/class-wp-widget-tag-cloud.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    21 	 *
    21 	 *
    22 	 * @since 2.8.0
    22 	 * @since 2.8.0
    23 	 */
    23 	 */
    24 	public function __construct() {
    24 	public function __construct() {
    25 		$widget_ops = array(
    25 		$widget_ops = array(
    26 			'description' => __( 'A cloud of your most used tags.' ),
    26 			'description'                 => __( 'A cloud of your most used tags.' ),
    27 			'customize_selective_refresh' => true,
    27 			'customize_selective_refresh' => true,
    28 		);
    28 		);
    29 		parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops );
    29 		parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops );
    30 	}
    30 	}
    31 
    31 
    45 			$title = $instance['title'];
    45 			$title = $instance['title'];
    46 		} else {
    46 		} else {
    47 			if ( 'post_tag' === $current_taxonomy ) {
    47 			if ( 'post_tag' === $current_taxonomy ) {
    48 				$title = __( 'Tags' );
    48 				$title = __( 'Tags' );
    49 			} else {
    49 			} else {
    50 				$tax = get_taxonomy( $current_taxonomy );
    50 				$tax   = get_taxonomy( $current_taxonomy );
    51 				$title = $tax->labels->name;
    51 				$title = $tax->labels->name;
    52 			}
    52 			}
    53 		}
    53 		}
    54 
    54 
    55 		$show_count = ! empty( $instance['count'] );
    55 		$show_count = ! empty( $instance['count'] );
    64 		 * @see wp_tag_cloud()
    64 		 * @see wp_tag_cloud()
    65 		 *
    65 		 *
    66 		 * @param array $args     Args used for the tag cloud widget.
    66 		 * @param array $args     Args used for the tag cloud widget.
    67 		 * @param array $instance Array of settings for the current widget.
    67 		 * @param array $instance Array of settings for the current widget.
    68 		 */
    68 		 */
    69 		$tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
    69 		$tag_cloud = wp_tag_cloud(
    70 			'taxonomy'   => $current_taxonomy,
    70 			apply_filters(
    71 			'echo'       => false,
    71 				'widget_tag_cloud_args',
    72 			'show_count' => $show_count,
    72 				array(
    73 		), $instance ) );
    73 					'taxonomy'   => $current_taxonomy,
       
    74 					'echo'       => false,
       
    75 					'show_count' => $show_count,
       
    76 				),
       
    77 				$instance
       
    78 			)
       
    79 		);
    74 
    80 
    75 		if ( empty( $tag_cloud ) ) {
    81 		if ( empty( $tag_cloud ) ) {
    76 			return;
    82 			return;
    77 		}
    83 		}
    78 
    84 
   101 	 *                            WP_Widget::form().
   107 	 *                            WP_Widget::form().
   102 	 * @param array $old_instance Old settings for this instance.
   108 	 * @param array $old_instance Old settings for this instance.
   103 	 * @return array Settings to save or bool false to cancel saving.
   109 	 * @return array Settings to save or bool false to cancel saving.
   104 	 */
   110 	 */
   105 	public function update( $new_instance, $old_instance ) {
   111 	public function update( $new_instance, $old_instance ) {
   106 		$instance = array();
   112 		$instance             = array();
   107 		$instance['title'] = sanitize_text_field( $new_instance['title'] );
   113 		$instance['title']    = sanitize_text_field( $new_instance['title'] );
   108 		$instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
   114 		$instance['count']    = ! empty( $new_instance['count'] ) ? 1 : 0;
   109 		$instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
   115 		$instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
   110 		return $instance;
   116 		return $instance;
   111 	}
   117 	}
   112 
   118 
   113 	/**
   119 	/**
   114 	 * Outputs the Tag Cloud widget settings form.
   120 	 * Outputs the Tag Cloud widget settings form.
   116 	 * @since 2.8.0
   122 	 * @since 2.8.0
   117 	 *
   123 	 *
   118 	 * @param array $instance Current settings.
   124 	 * @param array $instance Current settings.
   119 	 */
   125 	 */
   120 	public function form( $instance ) {
   126 	public function form( $instance ) {
   121 		$current_taxonomy = $this->_get_current_taxonomy($instance);
   127 		$current_taxonomy  = $this->_get_current_taxonomy( $instance );
   122 		$title_id = $this->get_field_id( 'title' );
   128 		$title_id          = $this->get_field_id( 'title' );
   123 		$count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
   129 		$count             = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
   124 		$instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
   130 		$instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
   125 
   131 
   126 		echo '<p><label for="' . $title_id .'">' . __( 'Title:' ) . '</label>
   132 		echo '<p><label for="' . $title_id . '">' . __( 'Title:' ) . '</label>
   127 			<input type="text" class="widefat" id="' . $title_id .'" name="' . $this->get_field_name( 'title' ) .'" value="' . $instance['title'] .'" />
   133 			<input type="text" class="widefat" id="' . $title_id . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $instance['title'] . '" />
   128 		</p>';
   134 		</p>';
   129 
   135 
   130 		$taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
   136 		$taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
   131 		$id = $this->get_field_id( 'taxonomy' );
   137 		$id         = $this->get_field_id( 'taxonomy' );
   132 		$name = $this->get_field_name( 'taxonomy' );
   138 		$name       = $this->get_field_name( 'taxonomy' );
   133 		$input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
   139 		$input      = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
   134 
   140 
   135 		$count_checkbox = sprintf(
   141 		$count_checkbox = sprintf(
   136 			'<p><input type="checkbox" class="checkbox" id="%1$s" name="%2$s"%3$s /> <label for="%1$s">%4$s</label></p>',
   142 			'<p><input type="checkbox" class="checkbox" id="%1$s" name="%2$s"%3$s /> <label for="%1$s">%4$s</label></p>',
   137 			$this->get_field_id( 'count' ),
   143 			$this->get_field_id( 'count' ),
   138 			$this->get_field_name( 'count' ),
   144 			$this->get_field_name( 'count' ),
   140 			__( 'Show tag counts' )
   146 			__( 'Show tag counts' )
   141 		);
   147 		);
   142 
   148 
   143 		switch ( count( $taxonomies ) ) {
   149 		switch ( count( $taxonomies ) ) {
   144 
   150 
   145 		// No tag cloud supporting taxonomies found, display error message
   151 			// No tag cloud supporting taxonomies found, display error message
   146 		case 0:
   152 			case 0:
   147 			echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>';
   153 				echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>';
   148 			printf( $input, '' );
   154 				printf( $input, '' );
   149 			break;
   155 				break;
   150 
   156 
   151 		// Just a single tag cloud supporting taxonomy found, no need to display a select.
   157 			// Just a single tag cloud supporting taxonomy found, no need to display a select.
   152 		case 1:
   158 			case 1:
   153 			$keys = array_keys( $taxonomies );
   159 				$keys     = array_keys( $taxonomies );
   154 			$taxonomy = reset( $keys );
   160 				$taxonomy = reset( $keys );
   155 			printf( $input, esc_attr( $taxonomy ) );
   161 				printf( $input, esc_attr( $taxonomy ) );
   156 			echo $count_checkbox;
   162 				echo $count_checkbox;
   157 			break;
   163 				break;
   158 
   164 
   159 		// More than one tag cloud supporting taxonomy found, display a select.
   165 			// More than one tag cloud supporting taxonomy found, display a select.
   160 		default:
   166 			default:
   161 			printf(
       
   162 				'<p><label for="%1$s">%2$s</label>' .
       
   163 				'<select class="widefat" id="%1$s" name="%3$s">',
       
   164 				$id,
       
   165 				__( 'Taxonomy:' ),
       
   166 				$name
       
   167 			);
       
   168 
       
   169 			foreach ( $taxonomies as $taxonomy => $tax ) {
       
   170 				printf(
   167 				printf(
   171 					'<option value="%s"%s>%s</option>',
   168 					'<p><label for="%1$s">%2$s</label>' .
   172 					esc_attr( $taxonomy ),
   169 					'<select class="widefat" id="%1$s" name="%3$s">',
   173 					selected( $taxonomy, $current_taxonomy, false ),
   170 					$id,
   174 					$tax->labels->name
   171 					__( 'Taxonomy:' ),
       
   172 					$name
   175 				);
   173 				);
   176 			}
   174 
   177 
   175 				foreach ( $taxonomies as $taxonomy => $tax ) {
   178 			echo '</select></p>' . $count_checkbox;
   176 					printf(
       
   177 						'<option value="%s"%s>%s</option>',
       
   178 						esc_attr( $taxonomy ),
       
   179 						selected( $taxonomy, $current_taxonomy, false ),
       
   180 						$tax->labels->name
       
   181 					);
       
   182 				}
       
   183 
       
   184 				echo '</select></p>' . $count_checkbox;
   179 		}
   185 		}
   180 	}
   186 	}
   181 
   187 
   182 	/**
   188 	/**
   183 	 * Retrieves the taxonomy for the current Tag cloud widget instance.
   189 	 * Retrieves the taxonomy for the current Tag cloud widget instance.
   185 	 * @since 4.4.0
   191 	 * @since 4.4.0
   186 	 *
   192 	 *
   187 	 * @param array $instance Current settings.
   193 	 * @param array $instance Current settings.
   188 	 * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
   194 	 * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
   189 	 */
   195 	 */
   190 	public function _get_current_taxonomy($instance) {
   196 	public function _get_current_taxonomy( $instance ) {
   191 		if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
   197 		if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
   192 			return $instance['taxonomy'];
   198 			return $instance['taxonomy'];
       
   199 		}
   193 
   200 
   194 		return 'post_tag';
   201 		return 'post_tag';
   195 	}
   202 	}
   196 }
   203 }