wp/wp-includes/widgets/class-wp-widget-categories.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    32 
    32 
    33 	/**
    33 	/**
    34 	 * Outputs the content for the current Categories widget instance.
    34 	 * Outputs the content for the current Categories widget instance.
    35 	 *
    35 	 *
    36 	 * @since 2.8.0
    36 	 * @since 2.8.0
    37 	 *
    37 	 * @since 4.2.0 Creates a unique HTML ID for the `<select>` element
    38 	 * @staticvar bool $first_dropdown
    38 	 *              if more than one instance is displayed on the page.
    39 	 *
    39 	 *
    40 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    40 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    41 	 *                        'before_widget', and 'after_widget'.
    41 	 *                        'before_widget', and 'after_widget'.
    42 	 * @param array $instance Settings for the current Categories widget instance.
    42 	 * @param array $instance Settings for the current Categories widget instance.
    43 	 */
    43 	 */
    44 	public function widget( $args, $instance ) {
    44 	public function widget( $args, $instance ) {
    45 		static $first_dropdown = true;
    45 		static $first_dropdown = true;
    46 
    46 
    47 		$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories' );
    47 		$default_title = __( 'Categories' );
       
    48 		$title         = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
    48 
    49 
    49 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    50 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    50 		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    51 		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    51 
    52 
    52 		$c = ! empty( $instance['count'] ) ? '1' : '0';
    53 		$count        = ! empty( $instance['count'] ) ? '1' : '0';
    53 		$h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
    54 		$hierarchical = ! empty( $instance['hierarchical'] ) ? '1' : '0';
    54 		$d = ! empty( $instance['dropdown'] ) ? '1' : '0';
    55 		$dropdown     = ! empty( $instance['dropdown'] ) ? '1' : '0';
    55 
    56 
    56 		echo $args['before_widget'];
    57 		echo $args['before_widget'];
    57 
    58 
    58 		if ( $title ) {
    59 		if ( $title ) {
    59 			echo $args['before_title'] . $title . $args['after_title'];
    60 			echo $args['before_title'] . $title . $args['after_title'];
    60 		}
    61 		}
    61 
    62 
    62 		$cat_args = array(
    63 		$cat_args = array(
    63 			'orderby'      => 'name',
    64 			'orderby'      => 'name',
    64 			'show_count'   => $c,
    65 			'show_count'   => $count,
    65 			'hierarchical' => $h,
    66 			'hierarchical' => $hierarchical,
    66 		);
    67 		);
    67 
    68 
    68 		if ( $d ) {
    69 		if ( $dropdown ) {
    69 			echo sprintf( '<form action="%s" method="get">', esc_url( home_url() ) );
    70 			printf( '<form action="%s" method="get">', esc_url( home_url() ) );
    70 			$dropdown_id    = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
    71 			$dropdown_id    = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
    71 			$first_dropdown = false;
    72 			$first_dropdown = false;
    72 
    73 
    73 			echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
    74 			echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
    74 
    75 
    87 			 * @param array $instance Array of settings for the current widget.
    88 			 * @param array $instance Array of settings for the current widget.
    88 			 */
    89 			 */
    89 			wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
    90 			wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
    90 
    91 
    91 			echo '</form>';
    92 			echo '</form>';
       
    93 
       
    94 			$type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
    92 			?>
    95 			?>
    93 
    96 
    94 <script type='text/javascript'>
    97 <script<?php echo $type_attr; ?>>
    95 /* <![CDATA[ */
    98 /* <![CDATA[ */
    96 (function() {
    99 (function() {
    97 	var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
   100 	var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
    98 	function onCatChange() {
   101 	function onCatChange() {
    99 		if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
   102 		if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
   105 /* ]]> */
   108 /* ]]> */
   106 </script>
   109 </script>
   107 
   110 
   108 			<?php
   111 			<?php
   109 		} else {
   112 		} else {
       
   113 			$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
       
   114 
       
   115 			/** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
       
   116 			$format = apply_filters( 'navigation_widgets_format', $format );
       
   117 
       
   118 			if ( 'html5' === $format ) {
       
   119 				// The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
       
   120 				$title      = trim( strip_tags( $title ) );
       
   121 				$aria_label = $title ? $title : $default_title;
       
   122 				echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
       
   123 			}
   110 			?>
   124 			?>
   111 		<ul>
   125 
       
   126 			<ul>
       
   127 				<?php
       
   128 				$cat_args['title_li'] = '';
       
   129 
       
   130 				/**
       
   131 				 * Filters the arguments for the Categories widget.
       
   132 				 *
       
   133 				 * @since 2.8.0
       
   134 				 * @since 4.9.0 Added the `$instance` parameter.
       
   135 				 *
       
   136 				 * @param array $cat_args An array of Categories widget options.
       
   137 				 * @param array $instance Array of settings for the current widget.
       
   138 				 */
       
   139 				wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
       
   140 				?>
       
   141 			</ul>
       
   142 
   112 			<?php
   143 			<?php
   113 			$cat_args['title_li'] = '';
   144 			if ( 'html5' === $format ) {
   114 
   145 				echo '</nav>';
   115 			/**
   146 			}
   116 			 * Filters the arguments for the Categories widget.
       
   117 			 *
       
   118 			 * @since 2.8.0
       
   119 			 * @since 4.9.0 Added the `$instance` parameter.
       
   120 			 *
       
   121 			 * @param array $cat_args An array of Categories widget options.
       
   122 			 * @param array $instance Array of settings for the current widget.
       
   123 			 */
       
   124 			wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
       
   125 			?>
       
   126 		</ul>
       
   127 			<?php
       
   128 		}
   147 		}
   129 
   148 
   130 		echo $args['after_widget'];
   149 		echo $args['after_widget'];
   131 	}
   150 	}
   132 
   151 
   156 	 * @since 2.8.0
   175 	 * @since 2.8.0
   157 	 *
   176 	 *
   158 	 * @param array $instance Current settings.
   177 	 * @param array $instance Current settings.
   159 	 */
   178 	 */
   160 	public function form( $instance ) {
   179 	public function form( $instance ) {
   161 		//Defaults
   180 		// Defaults.
   162 		$instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
   181 		$instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
   163 		$count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
   182 		$count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
   164 		$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
   183 		$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
   165 		$dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
   184 		$dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
   166 		?>
   185 		?>
   167 		<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   186 		<p>
   168 		<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /></p>
   187 			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   169 
   188 			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
   170 		<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
   189 		</p>
   171 		<label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
   190 
   172 
   191 		<p>
   173 		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
   192 			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
   174 		<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label><br />
   193 			<label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
   175 
   194 			<br />
   176 		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
   195 
   177 		<label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
   196 			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
       
   197 			<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
       
   198 			<br />
       
   199 
       
   200 			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
       
   201 			<label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label>
       
   202 		</p>
   178 		<?php
   203 		<?php
   179 	}
   204 	}
   180 
   205 
   181 }
   206 }