Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 119
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
wpcomsh_pdtoprated_widget_init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
PD_Top_Rated
0.00% covered (danger)
0.00%
0 / 116
0.00% covered (danger)
0.00%
0 / 5
702
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 PD_Top_Rated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 widget
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
462
 update
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
6
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2/**
3 * PollDaddy Top Rated Widget
4 *
5 * Copied from WordPress.com (retired)
6 */
7class PD_Top_Rated extends WP_Widget {
8
9    /**
10     * Constructor.
11     */
12    public function __construct() {
13        $widget_ops = array(
14            'classname'   => 'top_rated',
15            'description' => __( 'A list of your top rated posts, pages or comments.', 'wpcomsh' ),
16        );
17        parent::__construct( 'PD_Top_Rated', __( 'Top Rated', 'wpcomsh' ), $widget_ops );
18    }
19
20    /**
21     * Call the constructor. :^|
22     */
23    public function PD_Top_Rated() {
24        $this->__construct();
25    }
26
27    /**
28     * Display the widget.
29     *
30     * @param array $args     Widget arguments.
31     * @param array $instance Widget instance.
32     */
33    public function widget( $args, $instance ) {
34        $defaults = array(
35            'show_posts'         => 1,
36            'show_pages'         => 1,
37            'show_comments'      => 1,
38            'filter_by_category' => 0,
39            'item_count'         => 5,
40        );
41        $instance = wp_parse_args( $instance, $defaults );
42
43        echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
44        $title              = empty( $instance['title'] ) ? __( 'Top Rated', 'wpcomsh' ) : apply_filters( 'widget_title', $instance['title'] );
45        $posts_rating_id    = (int) get_option( 'pd-rating-posts-id' );
46        $pages_rating_id    = (int) get_option( 'pd-rating-pages-id' );
47        $comments_rating_id = (int) get_option( 'pd-rating-comments-id' );
48
49        if ( ! empty( $posts_rating_id ) || ! empty( $pages_rating_id ) || ! empty( $comments_rating_id ) ) {
50            echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
51
52            $top_class = 'posts';
53            if ( $instance['show_pages'] === 1 ) {
54                $top_class = 'pages';
55            } elseif ( $instance['show_comments'] === 1 ) {
56                $top_class = 'comments';
57            }
58
59            echo '<div id="pd_top_rated_holder" class="pd_top_rated_holder_' . esc_attr( $top_class ) . '"></div>';
60            $top_rated_url = is_ssl() ? 'https://polldaddy.com/js/rating/top-rated.js' : 'http://i0.poll.fm/js/rating/top-rated.js';
61            echo '<script language="javascript" src="' . esc_url( $top_rated_url ) . '"></script>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
62            echo '<script language="javascript" type="text/javascript">';
63
64            echo 'PDRTJS_TOP = new PDRTJS_RATING_TOP( ' . esc_html( (string) $posts_rating_id ) . ', ' . esc_html( (string) $pages_rating_id ) . ', ' . esc_html( (string) $comments_rating_id ) . ", '" . (int) $instance['show_posts'] . (int) $instance['show_pages'] . (int) $instance['show_comments'] . "', " . (int) $instance['item_count'] . ' );';
65
66            if ( $instance['show_posts'] === 1 && $instance['filter_by_category'] === 1 ) {
67                $current_category = array();
68                if ( is_single() ) { // get all posts in current category
69                    global $post;
70                    if ( ! empty( $post ) ) {
71                        $current_category = get_the_category( $post->ID );
72                    }
73                }
74
75                if ( is_category() ) { // get all posts in category archive page
76                    global $posts;
77                    if ( ! empty( $posts ) ) {
78                        $current_category = get_the_category( $posts[0]->ID );
79                    }
80                }
81
82                if ( is_array( $current_category ) && isset( $current_category[0]->cat_ID ) && (int) $current_category[0]->cat_ID > 0 ) {
83                    $args     = array(
84                        'category' => $current_category[0]->cat_ID,
85                        'fields'   => 'ids',
86                    );
87                    $post_ids = get_posts( $args );
88                }
89
90                if ( ! empty( $post_ids ) ) { // set variable
91                    echo 'PDRTJS_TOP.filters = ' . wp_json_encode( $post_ids, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
92                }
93            }
94
95            if ( $instance['show_posts'] === 1 ) {
96                echo "PDRTJS_TOP.get_top( 'posts', '0' );";
97            } elseif ( $instance['show_pages'] === 1 ) {
98                echo "PDRTJS_TOP.get_top( 'pages', '0' );";
99            } elseif ( $instance['show_comments'] === 1 ) {
100                echo "PDRTJS_TOP.get_top( 'comments', '0' );";
101            }
102
103            echo '</script>';
104        }
105        echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
106        do_action( 'jetpack_stats_extra', 'widget_view', 'top_rated' );
107    }
108
109    /**
110     * Update the widget settings.
111     *
112     * @param array $new_instance New settings.
113     * @param array $old_instance Old settings.
114     */
115    public function update( $new_instance, $old_instance ) {
116
117        $instance                       = $old_instance;
118        $instance['title']              = wp_strip_all_tags( $new_instance['title'] );
119        $instance['show_posts']         = (int) $new_instance['show_posts'];
120        $instance['show_pages']         = (int) $new_instance['show_pages'];
121        $instance['show_comments']      = (int) $new_instance['show_comments'];
122        $instance['filter_by_category'] = (int) $new_instance['filter_by_category'];
123        $instance['item_count']         = (int) $new_instance['item_count'];
124        return $instance;
125    }
126
127    /**
128     * Display the widget settings form.
129     *
130     * @param array $instance Current settings.
131     * @return never
132     */
133    public function form( $instance ) {
134
135        $instance           = wp_parse_args(
136            (array) $instance,
137            array(
138                'title'              => '',
139                'show_posts'         => '1',
140                'show_pages'         => '1',
141                'show_comments'      => '1',
142                'item_count'         => '5',
143                'filter_by_category' => '',
144            )
145        );
146        $title              = wp_strip_all_tags( $instance['title'] );
147        $show_posts         = (int) $instance['show_posts'];
148        $show_pages         = (int) $instance['show_pages'];
149        $show_comments      = (int) $instance['show_comments'];
150        $filter_by_category = (int) $instance['filter_by_category'];
151        $item_count         = (int) $instance['item_count'];
152        ?>
153        <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'wpcomsh' ); ?><input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
154        <p>
155            <label for="<?php echo esc_attr( $this->get_field_id( 'show_posts' ) ); ?>">
156                <input type="checkbox" class="checkbox"  id="<?php echo esc_attr( $this->get_field_id( 'show_posts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_posts' ) ); ?>" value="1" <?php checked( $show_posts, 1 ); ?> />
157                <?php esc_html_e( 'Show for posts', 'wpcomsh' ); ?>
158            </label>
159        </p>
160        <p>
161            <label for="<?php echo esc_attr( $this->get_field_id( 'show_pages' ) ); ?>">
162                <input type="checkbox" class="checkbox"  id="<?php echo esc_attr( $this->get_field_id( 'show_pages' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_pages' ) ); ?>" value="1" <?php checked( $show_pages, 1 ); ?> />
163                <?php esc_html_e( 'Show for pages', 'wpcomsh' ); ?>
164            </label>
165        </p>
166        <p>
167            <label for="<?php echo esc_attr( $this->get_field_id( 'show_comments' ) ); ?>">
168                <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_comments' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_comments' ) ); ?>" value="1" <?php checked( $show_comments, 1 ); ?>/>
169                <?php esc_html_e( 'Show for comments', 'wpcomsh' ); ?>
170            </label>
171        </p>
172        <p>
173            <label for="<?php echo esc_attr( $this->get_field_id( 'filter_by_category' ) ); ?>">
174                <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'filter_by_category' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'filter_by_category' ) ); ?>" value="1" <?php checked( $filter_by_category, 1 ); ?>/>
175                <?php esc_html_e( 'Filter by category', 'wpcomsh' ); ?>
176            </label>
177        </p>
178        <p>
179            <label for="rss-items-<?php echo esc_attr( (string) $item_count ); ?>"><?php esc_html_e( 'How many items would you like to display?', 'wpcomsh' ); ?>
180                <select id="<?php echo esc_attr( $this->get_field_id( 'item_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'item_count' ) ); ?>">
181                    <?php
182                    for ( $i = 1; $i <= 20; ++$i ) {
183                        echo '<option value="' . (int) $i . '"' . selected( $item_count, $i, false ) . '>' . (int) $i . '</option>'; // @phan-suppress-current-line PhanRedundantConditionInLoop -- phpcs needs the explicit cast.
184                    }
185                    ?>
186                </select>
187            </label>
188        </p>
189        <?php
190    }
191}
192
193/**
194 * Register the widget.
195 */
196function wpcomsh_pdtoprated_widget_init() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
197    if ( get_option( 'pd-rating-usercode' ) && is_active_widget( false, false, 'pd_top_rated' ) ) {
198        register_widget( 'PD_Top_Rated' );
199    }
200}
201add_action( 'widgets_init', 'wpcomsh_pdtoprated_widget_init' );