wp/wp-includes/widgets/class-wp-widget-categories.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 			'classname' => 'widget_categories',
    26 			'classname'                   => 'widget_categories',
    27 			'description' => __( 'A list or dropdown of categories.' ),
    27 			'description'                 => __( 'A list or dropdown of categories.' ),
    28 			'customize_selective_refresh' => true,
    28 			'customize_selective_refresh' => true,
    29 		);
    29 		);
    30 		parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
    30 		parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
    31 	}
    31 	}
    32 
    32 
    65 			'hierarchical' => $h,
    65 			'hierarchical' => $h,
    66 		);
    66 		);
    67 
    67 
    68 		if ( $d ) {
    68 		if ( $d ) {
    69 			echo sprintf( '<form action="%s" method="get">', esc_url( home_url() ) );
    69 			echo sprintf( '<form action="%s" method="get">', esc_url( home_url() ) );
    70 			$dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
    70 			$dropdown_id    = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
    71 			$first_dropdown = false;
    71 			$first_dropdown = false;
    72 
    72 
    73 			echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
    73 			echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
    74 
    74 
    75 			$cat_args['show_option_none'] = __( 'Select Category' );
    75 			$cat_args['show_option_none'] = __( 'Select Category' );
    76 			$cat_args['id'] = $dropdown_id;
    76 			$cat_args['id']               = $dropdown_id;
    77 
    77 
    78 			/**
    78 			/**
    79 			 * Filters the arguments for the Categories widget drop-down.
    79 			 * Filters the arguments for the Categories widget drop-down.
    80 			 *
    80 			 *
    81 			 * @since 2.8.0
    81 			 * @since 2.8.0
   103 	dropdown.onchange = onCatChange;
   103 	dropdown.onchange = onCatChange;
   104 })();
   104 })();
   105 /* ]]> */
   105 /* ]]> */
   106 </script>
   106 </script>
   107 
   107 
   108 <?php
   108 			<?php
   109 		} else {
   109 		} else {
   110 ?>
   110 			?>
   111 		<ul>
   111 		<ul>
   112 <?php
   112 			<?php
   113 		$cat_args['title_li'] = '';
   113 			$cat_args['title_li'] = '';
   114 
   114 
   115 		/**
   115 			/**
   116 		 * Filters the arguments for the Categories widget.
   116 			 * Filters the arguments for the Categories widget.
   117 		 *
   117 			 *
   118 		 * @since 2.8.0
   118 			 * @since 2.8.0
   119 		 * @since 4.9.0 Added the `$instance` parameter.
   119 			 * @since 4.9.0 Added the `$instance` parameter.
   120 		 *
   120 			 *
   121 		 * @param array $cat_args An array of Categories widget options.
   121 			 * @param array $cat_args An array of Categories widget options.
   122 		 * @param array $instance Array of settings for the current widget.
   122 			 * @param array $instance Array of settings for the current widget.
   123 		 */
   123 			 */
   124 		wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
   124 			wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
   125 ?>
   125 			?>
   126 		</ul>
   126 		</ul>
   127 <?php
   127 			<?php
   128 		}
   128 		}
   129 
   129 
   130 		echo $args['after_widget'];
   130 		echo $args['after_widget'];
   131 	}
   131 	}
   132 
   132 
   139 	 *                            WP_Widget::form().
   139 	 *                            WP_Widget::form().
   140 	 * @param array $old_instance Old settings for this instance.
   140 	 * @param array $old_instance Old settings for this instance.
   141 	 * @return array Updated settings to save.
   141 	 * @return array Updated settings to save.
   142 	 */
   142 	 */
   143 	public function update( $new_instance, $old_instance ) {
   143 	public function update( $new_instance, $old_instance ) {
   144 		$instance = $old_instance;
   144 		$instance                 = $old_instance;
   145 		$instance['title'] = sanitize_text_field( $new_instance['title'] );
   145 		$instance['title']        = sanitize_text_field( $new_instance['title'] );
   146 		$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
   146 		$instance['count']        = ! empty( $new_instance['count'] ) ? 1 : 0;
   147 		$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
   147 		$instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
   148 		$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
   148 		$instance['dropdown']     = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
   149 
   149 
   150 		return $instance;
   150 		return $instance;
   151 	}
   151 	}
   152 
   152 
   153 	/**
   153 	/**
   157 	 *
   157 	 *
   158 	 * @param array $instance Current settings.
   158 	 * @param array $instance Current settings.
   159 	 */
   159 	 */
   160 	public function form( $instance ) {
   160 	public function form( $instance ) {
   161 		//Defaults
   161 		//Defaults
   162 		$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
   162 		$instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
   163 		$title = sanitize_text_field( $instance['title'] );
   163 		$count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
   164 		$count = isset($instance['count']) ? (bool) $instance['count'] :false;
       
   165 		$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
   164 		$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
   166 		$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
   165 		$dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
   167 		?>
   166 		?>
   168 		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
   167 		<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   169 		<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( $title ); ?>" /></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>
   170 
   169 
   171 		<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 ); ?> />
   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 ); ?> />
   172 		<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
   171 		<label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
   173 
   172 
   174 		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
   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 ); ?> />
   175 		<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
   174 		<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label><br />
   176 
   175 
   177 		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
   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 ); ?> />
   178 		<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
   177 		<label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
   179 		<?php
   178 		<?php
   180 	}
   179 	}
   181 
   180 
   182 }
   181 }