Changeset 1303296
- Timestamp:
- 12/08/2015 09:55:07 PM (10 years ago)
- File:
-
- 1 edited
-
most-commented/trunk/most-commented.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
most-commented/trunk/most-commented.php
r1303292 r1303296 17 17 extract( $args ); 18 18 $title = apply_filters( 'widget_title', $instance['title'] ); 19 $show_pass_post = (bool) $instance['show_pass_post'];19 $show_pass_post = (bool) $instance['show_pass_post']; 20 20 $duration = intval( $instance['duration'] ); 21 if ( ! in_array( $duration, array( 0, 1, 7, 30, 365 ) ) )21 if ( ! in_array( $duration, array( 0, 1, 7, 30, 365 ) ) ) { 22 22 $duration = 0; 23 } 24 23 25 $num_posts = intval( $instance['num_posts'] ); 24 if ( $num_posts < 1 ) 26 if ( $num_posts < 1 ) { 25 27 $num_posts = 5; 28 } 29 26 30 $post_type = $instance['post_type']; 27 if ( !in_array( $post_type, array( 'post', 'page', 'both' ) ) ) 31 if ( !in_array( $post_type, array( 'post', 'page', 'both' ) ) ) { 28 32 $post_type = 'both'; 29 if ( array_key_exists( 'echo', $instance ) )30 $echo = $instance['echo']; 31 else32 $echo = true; 33 } 34 35 $echo = ( array_key_exists( 'echo', $instance ) ) ? $instance['echo'] : true; 36 33 37 if ( array_key_exists( 'before', $instance ) ) { 34 38 $before = $instance['before']; … … 43 47 if ( ! $output = wp_cache_get( $widget_id ) ) { 44 48 $request = "SELECT ID, post_title, comment_count FROM $wpdb->posts WHERE comment_count > 0 AND post_status = 'publish'"; 45 if ( !$show_pass_post ) 49 if ( !$show_pass_post ) { 46 50 $request .= " AND post_password = ''"; 47 if ( 'both' != $post_type ) 48 $request .= " AND post_type = '$post_type'"; 49 if ( $duration > 0 ) 50 $request .= " AND DATE_SUB(CURDATE(), INTERVAL $duration DAY) < post_date"; 51 } 52 53 if ( 'both' != $post_type ) { 54 $request .= $wpdb->prepare( " AND post_type = %s", $post_type ); 55 } 56 57 if ( $duration > 0 ) { 58 $request .= $wpdb->prepare( " AND DATE_SUB(CURDATE(), INTERVAL %d DAY) < post_date", $duration ); 59 } 60 51 61 $request .= " ORDER BY comment_count DESC LIMIT $num_posts"; 52 62 … … 56 66 $output = ''; 57 67 58 if ( ! empty( $posts ) ) {68 if ( ! empty( $posts ) ) { 59 69 foreach ( $posts as $post ) { 60 70 $post_title = apply_filters( 'the_title', $post->post_title ); … … 66 76 } 67 77 68 if ( ! array_key_exists( 'not_widget', $instance ) ) {78 if ( ! array_key_exists( 'not_widget', $instance ) ) { 69 79 if ( $title ) 70 80 $title = $before_title . $title . $after_title; … … 79 89 } 80 90 81 if ( $echo ) 91 if ( $echo ) { 82 92 echo $output; 83 else93 } else { 84 94 return $output; 95 } 85 96 } 86 97 … … 97 108 $show_pass_post = (bool)$instance['show_pass_post']; 98 109 $duration = intval( $instance['duration'] ); 99 if ( ! in_array( $duration, array( 0, 1, 7, 30, 365 ) ) )110 if ( ! in_array( $duration, array( 0, 1, 7, 30, 365 ) ) ) { 100 111 $duration = 0; 112 } 113 101 114 $num_posts = intval( $instance['num_posts'] ); 102 if ( $num_posts < 1 ) 115 if ( $num_posts < 1 ) { 103 116 $num_posts = 5; 117 } 118 104 119 $post_type = $instance['post_type']; 105 if ( ! in_array( $post_type, array( 'post', 'page', 'both' ) ) )120 if ( ! in_array( $post_type, array( 'post', 'page', 'both' ) ) ) { 106 121 $post_type = 'both'; 122 } 107 123 ?> 108 124 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></label></p> 109 <p><label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e( 'Display:' ); ?> 110 <select id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>"> 111 <?php 112 $post_type_choices = array( 'post' => __( 'Posts' ), 'page' => __( 'Pages' ), 'both' => __( 'Posts & Pages' ) ); 113 foreach ( $post_type_choices as $post_type_value => $post_type_text ) { 114 echo "<option value='$post_type_value' " . ( $post_type == $post_type_value ? "selected='selected'" : '' ) . ">$post_type_text</option>\n"; 115 } 116 ?> 117 </select> 118 </label></p> 119 <p><label for="<?php echo $this->get_field_id( 'num_posts' ); ?>"><?php _e( 'Maximum number of results:' ); ?> 120 <select id="<?php echo $this->get_field_id('num_posts' ); ?>" name="<?php echo $this->get_field_name( 'num_posts' ); ?>"> 121 <?php 122 for ( $i = 1; $i <= 20; ++$i ) { 123 echo "<option value='$i' " . ( $num_posts == $i ? "selected='selected'" : '' ) . ">$i</option>\n"; 124 } 125 ?> 126 </select> 127 </label></p> 128 <p><label for="<?php echo $this->get_field_id( 'duration' ); ?>"><?php _e( 'Limit to:' ); ?> 129 <select id="<?php echo $this->get_field_id( 'duration' ); ?>" name="<?php echo $this->get_field_name( 'duration' ); ?>"> 130 <?php 131 $duration_choices = array( 1 => __( '1 Day' ), 7 => __( '7 Days' ), 30 => __( '30 Days' ), 365 => __( '365 Days' ), 0 => __( 'All Time' ) ); 132 foreach ( $duration_choices as $duration_num => $duration_text ) { 133 echo "<option value='$duration_num' " . ( $duration == $duration_num ? "selected='selected'" : '' ) . ">$duration_text</option>\n"; 134 } 135 ?> 136 </select> 137 </label></p> 138 <p><label for="<?php echo $this->get_field_id( 'show_pass_post' ); ?>"><input id="<?php echo $this->get_field_id( 'show_pass_post' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'show_pass_post' ); ?>"<?php echo checked( $show_pass_post ); ?> /> <?php _e( 'Include password protected posts/pages' ); ?></label></p> 125 <p> 126 <label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e( 'Display:' ); ?> 127 <select id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>"> 128 <?php 129 $post_type_choices = array( 'post' => __( 'Posts' ), 'page' => __( 'Pages' ), 'both' => __( 'Posts & Pages' ) ); 130 foreach ( $post_type_choices as $post_type_value => $post_type_text ) { 131 echo "<option value='" . esc_attr( $post_type_value ) . "' " . ( $post_type == $post_type_value ? "selected='selected'" : '' ) . ">" . esc_html( $post_type_text ) . "</option>\n"; 132 } 133 ?> 134 </select> 135 </label> 136 </p> 137 138 <p> 139 <label for="<?php echo $this->get_field_id( 'num_posts' ); ?>"><?php _e( 'Maximum number of results:' ); ?> 140 <select id="<?php echo $this->get_field_id('num_posts' ); ?>" name="<?php echo $this->get_field_name( 'num_posts' ); ?>"> 141 <?php 142 for ( $i = 1; $i <= 20; ++$i ) { 143 echo "<option value='$i' " . ( $num_posts == $i ? "selected='selected'" : '' ) . ">$i</option>\n"; 144 } 145 ?> 146 </select> 147 </label> 148 </p> 149 150 <p> 151 <label for="<?php echo $this->get_field_id( 'duration' ); ?>"><?php _e( 'Limit to:' ); ?> 152 <select id="<?php echo $this->get_field_id( 'duration' ); ?>" name="<?php echo $this->get_field_name( 'duration' ); ?>"> 153 <?php 154 $duration_choices = array( 1 => __( '1 Day' ), 7 => __( '7 Days' ), 30 => __( '30 Days' ), 365 => __( '365 Days' ), 0 => __( 'All Time' ) ); 155 foreach ( $duration_choices as $duration_num => $duration_text ) { 156 echo "<option value='$duration_num' " . ( $duration == $duration_num ? "selected='selected'" : '' ) . ">$duration_text</option>\n"; 157 } 158 ?> 159 </select> 160 </label> 161 </p> 162 163 <p> 164 <label for="<?php echo $this->get_field_id( 'show_pass_post' ); ?>"> 165 <input id="<?php echo $this->get_field_id( 'show_pass_post' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'show_pass_post' ); ?>"<?php echo checked( $show_pass_post ); ?> /> <?php _e( 'Include password protected posts/pages' ); ?> 166 </label> 167 </p> 139 168 <?php 140 169 } … … 144 173 add_action( 'widgets_init', create_function( '', 'return register_widget( "Most_Commented_Widget" );' ) ); 145 174 146 if ( ! function_exists( 'mdv_most_commented' ) ) {175 if ( ! function_exists( 'mdv_most_commented' ) ) { 147 176 function mdv_most_commented( $num_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $duration = 0, $echo = true, $post_type = 'both' ) { 148 177 $options = array( 149 'num_posts' => $num_posts,150 'before' => $before,151 'after' => $after,178 'num_posts' => $num_posts, 179 'before' => $before, 180 'after' => $after, 152 181 'show_pass_post' => $show_pass_post, 153 'duration' => $duration,154 'echo' => $echo,155 'post_type' => $post_type,156 'not_widget' => true157 );182 'duration' => $duration, 183 'echo' => $echo, 184 'post_type' => $post_type, 185 'not_widget' => true, 186 ); 158 187 $args = array( 'widget_id' => 'most_commented_widget_' . md5( var_export( $options, true ) ) ); 159 188 $most_commented = new Most_Commented_Widget(); 160 189 161 if ( $echo ) 190 if ( $echo ) { 162 191 $most_commented->widget( $args, $options ); 163 else192 } else { 164 193 return $most_commented->widget( $args, $options ); 194 } 165 195 } 166 196 }
Note: See TracChangeset
for help on using the changeset viewer.