wp/wp-includes/widgets/class-wp-widget-links.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' => __( 'Your blogroll' ),
    26 			'description'                 => __( 'Your blogroll' ),
    27 			'customize_selective_refresh' => true,
    27 			'customize_selective_refresh' => true,
    28 		);
    28 		);
    29 		parent::__construct( 'links', __( 'Links' ), $widget_ops );
    29 		parent::__construct( 'links', __( 'Links' ), $widget_ops );
    30 	}
    30 	}
    31 
    31 
    37 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    37 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    38 	 *                        'before_widget', and 'after_widget'.
    38 	 *                        'before_widget', and 'after_widget'.
    39 	 * @param array $instance Settings for the current Links widget instance.
    39 	 * @param array $instance Settings for the current Links widget instance.
    40 	 */
    40 	 */
    41 	public function widget( $args, $instance ) {
    41 	public function widget( $args, $instance ) {
    42 		$show_description = isset($instance['description']) ? $instance['description'] : false;
    42 		$show_description = isset( $instance['description'] ) ? $instance['description'] : false;
    43 		$show_name = isset($instance['name']) ? $instance['name'] : false;
    43 		$show_name        = isset( $instance['name'] ) ? $instance['name'] : false;
    44 		$show_rating = isset($instance['rating']) ? $instance['rating'] : false;
    44 		$show_rating      = isset( $instance['rating'] ) ? $instance['rating'] : false;
    45 		$show_images = isset($instance['images']) ? $instance['images'] : true;
    45 		$show_images      = isset( $instance['images'] ) ? $instance['images'] : true;
    46 		$category = isset($instance['category']) ? $instance['category'] : false;
    46 		$category         = isset( $instance['category'] ) ? $instance['category'] : false;
    47 		$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
    47 		$orderby          = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
    48 		$order = $orderby == 'rating' ? 'DESC' : 'ASC';
    48 		$order            = $orderby == 'rating' ? 'DESC' : 'ASC';
    49 		$limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
    49 		$limit            = isset( $instance['limit'] ) ? $instance['limit'] : -1;
    50 
    50 
    51 		$before_widget = preg_replace( '/id="[^"]*"/', 'id="%id"', $args['before_widget'] );
    51 		$before_widget = preg_replace( '/id="[^"]*"/', 'id="%id"', $args['before_widget'] );
    52 
    52 
    53 		$widget_links_args = array(
    53 		$widget_links_args = array(
    54 			'title_before'     => $args['before_title'],
    54 			'title_before'     => $args['before_title'],
    90 	 * @param array $old_instance Old settings for this instance.
    90 	 * @param array $old_instance Old settings for this instance.
    91 	 * @return array Updated settings to save.
    91 	 * @return array Updated settings to save.
    92 	 */
    92 	 */
    93 	public function update( $new_instance, $old_instance ) {
    93 	public function update( $new_instance, $old_instance ) {
    94 		$new_instance = (array) $new_instance;
    94 		$new_instance = (array) $new_instance;
    95 		$instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0 );
    95 		$instance     = array(
       
    96 			'images'      => 0,
       
    97 			'name'        => 0,
       
    98 			'description' => 0,
       
    99 			'rating'      => 0,
       
   100 		);
    96 		foreach ( $instance as $field => $val ) {
   101 		foreach ( $instance as $field => $val ) {
    97 			if ( isset($new_instance[$field]) )
   102 			if ( isset( $new_instance[ $field ] ) ) {
    98 				$instance[$field] = 1;
   103 				$instance[ $field ] = 1;
       
   104 			}
    99 		}
   105 		}
   100 
   106 
   101 		$instance['orderby'] = 'name';
   107 		$instance['orderby'] = 'name';
   102 		if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) )
   108 		if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) ) {
   103 			$instance['orderby'] = $new_instance['orderby'];
   109 			$instance['orderby'] = $new_instance['orderby'];
       
   110 		}
   104 
   111 
   105 		$instance['category'] = intval( $new_instance['category'] );
   112 		$instance['category'] = intval( $new_instance['category'] );
   106 		$instance['limit'] = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
   113 		$instance['limit']    = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
   107 
   114 
   108 		return $instance;
   115 		return $instance;
   109 	}
   116 	}
   110 
   117 
   111 	/**
   118 	/**
   116 	 * @param array $instance Current settings.
   123 	 * @param array $instance Current settings.
   117 	 */
   124 	 */
   118 	public function form( $instance ) {
   125 	public function form( $instance ) {
   119 
   126 
   120 		//Defaults
   127 		//Defaults
   121 		$instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false, 'orderby' => 'name', 'limit' => -1 ) );
   128 		$instance  = wp_parse_args(
       
   129 			(array) $instance,
       
   130 			array(
       
   131 				'images'      => true,
       
   132 				'name'        => true,
       
   133 				'description' => false,
       
   134 				'rating'      => false,
       
   135 				'category'    => false,
       
   136 				'orderby'     => 'name',
       
   137 				'limit'       => -1,
       
   138 			)
       
   139 		);
   122 		$link_cats = get_terms( 'link_category' );
   140 		$link_cats = get_terms( 'link_category' );
   123 		if ( ! $limit = intval( $instance['limit'] ) )
   141 		if ( ! $limit = intval( $instance['limit'] ) ) {
   124 			$limit = -1;
   142 			$limit = -1;
   125 			?>
   143 		}
       
   144 		?>
   126 		<p>
   145 		<p>
   127 		<label for="<?php echo $this->get_field_id('category'); ?>"><?php _e( 'Select Link Category:' ); ?></label>
   146 		<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select Link Category:' ); ?></label>
   128 		<select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
   147 		<select class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>">
   129 		<option value=""><?php _ex('All Links', 'links widget'); ?></option>
   148 		<option value=""><?php _ex( 'All Links', 'links widget' ); ?></option>
   130 		<?php
   149 		<?php
   131 		foreach ( $link_cats as $link_cat ) {
   150 		foreach ( $link_cats as $link_cat ) {
   132 			echo '<option value="' . intval( $link_cat->term_id ) . '"'
   151 			echo '<option value="' . intval( $link_cat->term_id ) . '"'
   133 				. selected( $instance['category'], $link_cat->term_id, false )
   152 				. selected( $instance['category'], $link_cat->term_id, false )
   134 				. '>' . $link_cat->name . "</option>\n";
   153 				. '>' . $link_cat->name . "</option>\n";
   135 		}
   154 		}
   136 		?>
   155 		?>
   137 		</select>
   156 		</select>
   138 		<label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort by:' ); ?></label>
   157 		<label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e( 'Sort by:' ); ?></label>
   139 		<select name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>" class="widefat">
   158 		<select name="<?php echo $this->get_field_name( 'orderby' ); ?>" id="<?php echo $this->get_field_id( 'orderby' ); ?>" class="widefat">
   140 			<option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
   159 			<option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
   141 			<option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
   160 			<option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
   142 			<option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
   161 			<option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
   143 			<option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _ex( 'Random', 'Links widget' ); ?></option>
   162 			<option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _ex( 'Random', 'Links widget' ); ?></option>
   144 		</select>
   163 		</select>
   145 		</p>
   164 		</p>
   146 		<p>
   165 		<p>
   147 		<input class="checkbox" type="checkbox"<?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" />
   166 		<input class="checkbox" type="checkbox"<?php checked( $instance['images'], true ); ?> id="<?php echo $this->get_field_id( 'images' ); ?>" name="<?php echo $this->get_field_name( 'images' ); ?>" />
   148 		<label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
   167 		<label for="<?php echo $this->get_field_id( 'images' ); ?>"><?php _e( 'Show Link Image' ); ?></label><br />
   149 		<input class="checkbox" type="checkbox"<?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" />
   168 		<input class="checkbox" type="checkbox"<?php checked( $instance['name'], true ); ?> id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" />
   150 		<label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label><br />
   169 		<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'Show Link Name' ); ?></label><br />
   151 		<input class="checkbox" type="checkbox"<?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" />
   170 		<input class="checkbox" type="checkbox"<?php checked( $instance['description'], true ); ?> id="<?php echo $this->get_field_id( 'description' ); ?>" name="<?php echo $this->get_field_name( 'description' ); ?>" />
   152 		<label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label><br />
   171 		<label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e( 'Show Link Description' ); ?></label><br />
   153 		<input class="checkbox" type="checkbox"<?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" />
   172 		<input class="checkbox" type="checkbox"<?php checked( $instance['rating'], true ); ?> id="<?php echo $this->get_field_id( 'rating' ); ?>" name="<?php echo $this->get_field_name( 'rating' ); ?>" />
   154 		<label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
   173 		<label for="<?php echo $this->get_field_id( 'rating' ); ?>"><?php _e( 'Show Link Rating' ); ?></label>
   155 		</p>
   174 		</p>
   156 		<p>
   175 		<p>
   157 		<label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e( 'Number of links to show:' ); ?></label>
   176 		<label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Number of links to show:' ); ?></label>
   158 		<input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit == -1 ? '' : intval( $limit ); ?>" size="3" />
   177 		<input id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="text" value="<?php echo $limit == -1 ? '' : intval( $limit ); ?>" size="3" />
   159 		</p>
   178 		</p>
   160 		<?php
   179 		<?php
   161 	}
   180 	}
   162 }
   181 }