Plugin Directory

Changeset 1303296


Ignore:
Timestamp:
12/08/2015 09:55:07 PM (10 years ago)
Author:
nickmomrik
Message:

Coding stardards and data validation cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • most-commented/trunk/most-commented.php

    r1303292 r1303296  
    1717        extract( $args );
    1818        $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'];
    2020        $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 ) ) ) {
    2222            $duration = 0;
     23        }
     24
    2325        $num_posts = intval( $instance['num_posts'] );
    24         if ( $num_posts < 1 )
     26        if ( $num_posts < 1 ) {
    2527            $num_posts = 5;
     28        }
     29
    2630        $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' ) ) ) {
    2832            $post_type = 'both';
    29         if ( array_key_exists( 'echo', $instance ) )
    30             $echo = $instance['echo'];
    31         else
    32             $echo = true;
     33        }
     34
     35        $echo = ( array_key_exists( 'echo', $instance ) ) ? $instance['echo'] : true;
     36
    3337        if ( array_key_exists( 'before', $instance ) ) {
    3438            $before = $instance['before'];
     
    4347        if ( ! $output = wp_cache_get( $widget_id ) ) {
    4448            $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 ) {
    4650                $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
    5161            $request .= " ORDER BY comment_count DESC LIMIT $num_posts";
    5262
     
    5666                $output = '';
    5767
    58                 if ( !empty( $posts ) ) {
     68                if ( ! empty( $posts ) ) {
    5969                    foreach ( $posts as $post ) {
    6070                        $post_title = apply_filters( 'the_title', $post->post_title );
     
    6676                }
    6777
    68                 if ( !array_key_exists( 'not_widget', $instance ) ) {
     78                if ( ! array_key_exists( 'not_widget', $instance ) ) {
    6979                    if ( $title )
    7080                        $title = $before_title . $title . $after_title;
     
    7989        }
    8090
    81         if ( $echo )
     91        if ( $echo ) {
    8292            echo $output;
    83         else
     93        } else {
    8494            return $output;
     95        }
    8596    }
    8697
     
    97108        $show_pass_post = (bool)$instance['show_pass_post'];
    98109        $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 ) ) ) {
    100111            $duration = 0;
     112        }
     113
    101114        $num_posts = intval( $instance['num_posts'] );
    102         if ( $num_posts < 1 )
     115        if ( $num_posts < 1 ) {
    103116            $num_posts = 5;
     117        }
     118
    104119        $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' ) ) ) {
    106121            $post_type = 'both';
     122        }
    107123        ?>
    108124        <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>
    139168        <?php
    140169    }
     
    144173add_action( 'widgets_init', create_function( '', 'return register_widget( "Most_Commented_Widget" );' ) );
    145174
    146 if ( !function_exists( 'mdv_most_commented' ) ) {
     175if ( ! function_exists( 'mdv_most_commented' ) ) {
    147176    function mdv_most_commented( $num_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $duration = 0, $echo = true, $post_type = 'both' ) {
    148177        $options = array(
    149             'num_posts' => $num_posts,
    150             'before' => $before,
    151             'after' => $after,
     178            'num_posts'      => $num_posts,
     179            'before'         => $before,
     180            'after'          => $after,
    152181            'show_pass_post' => $show_pass_post,
    153             'duration' => $duration,
    154             'echo' => $echo,
    155             'post_type' => $post_type,
    156             'not_widget' => true
    157             );
     182            'duration'       => $duration,
     183            'echo'           => $echo,
     184            'post_type'      => $post_type,
     185            'not_widget'     => true,
     186        );
    158187        $args = array( 'widget_id' => 'most_commented_widget_' . md5( var_export( $options, true ) ) );
    159188        $most_commented = new Most_Commented_Widget();
    160189
    161         if ( $echo )
     190        if ( $echo ) {
    162191            $most_commented->widget( $args, $options );
    163         else
     192        } else {
    164193            return $most_commented->widget( $args, $options );
     194        }
    165195    }
    166196}
Note: See TracChangeset for help on using the changeset viewer.