Completed
Push — fix/videopress-missing-from-se... ( 60564b )
by
unknown
22:41 queued 12:31
created

Jetpack_RSS_Links_Widget::_rss_link()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 60
Code Lines 30

Duplication

Lines 6
Ratio 10 %

Importance

Changes 0
Metric Value
cc 10
eloc 30
nc 192
nop 2
dl 6
loc 60
rs 6.08
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Module Name: RSS Links Widget
4
 * Module Description: Easily add RSS links to your theme's sidebar.
5
 * Sort Order: 20
6
 * First Introduced: 1.2
7
 */
8
9
class Jetpack_RSS_Links_Widget extends WP_Widget {
10
11 View Code Duplication
	function __construct() {
12
		$widget_ops = array(
13
			'classname' => 'widget_rss_links',
14
			'description' => __( "Links to your blog's RSS feeds", 'jetpack' ),
15
			'customize_selective_refresh' => true,
16
		);
17
		parent::__construct(
18
			'rss_links',
19
			/** This filter is documented in modules/widgets/facebook-likebox.php */
20
			apply_filters( 'jetpack_widget_name', __( 'RSS Links', 'jetpack' ) ),
21
			$widget_ops
22
		);
23
	}
24
25
	function widget( $args, $instance ) {
26
		$instance = wp_parse_args( (array) $instance, $this->defaults() );
27
28
		extract( $args );
29
30
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
31
		$title = apply_filters( 'widget_title', $instance['title'] );
32
		echo $before_widget;
33
34
		if ( $title )
35
			echo $before_title . stripslashes( $title ) . $after_title;
36
37
		if ( 'text' == $instance['format'] ) echo '<ul>';
38
39
		if ( 'posts' == $instance['display'] ) {
40
			$this->_rss_link( 'posts', $instance);
41
		} elseif ( 'comments' == $instance['display'] ) {
42
			$this->_rss_link( 'comments', $instance);
43
		} elseif ( 'posts-comments' == $instance['display'] ) {
44
			$this->_rss_link( 'posts', $instance );
45
			$this->_rss_link( 'comments', $instance );
46
		}
47
48
		if ( 'text' == $instance['format'] ) echo '</ul>';
49
50
		echo "\n" . $after_widget;
51
52
		/** This action is documented in modules/widgets/gravatar-profile.php */
53
		do_action( 'jetpack_stats_extra', 'widget_view', 'rss-links' );
54
	}
55
56
	/**
57
	 * Return an associative array of default values
58
	 * These values are used in new widgets as well as when sanitizing input.
59
	 *
60
	 * @return array Array of default values for the Widget's options
61
	 */
62
	function defaults() {
63
		return array(
64
			'title'	  => '',
65
			'display' => 'posts-comments',
66
			'format'  => 'text'
67
		);
68
	}
69
70
	function update( $new_instance, $old_instance ) {
71
		$instance = $old_instance;
72
73
		$instance['title'] = wp_filter_nohtml_kses( $new_instance['title'] );
74
		$instance['display'] = $new_instance['display'];
75
		$instance['format'] = $new_instance['format'];
76
		$instance['imagesize'] = $new_instance['imagesize'];
77
		$instance['imagecolor'] = $new_instance['imagecolor'];
78
79
		return $instance;
80
	}
81
82
	function form( $instance ) {
83
		$instance = wp_parse_args( (array) $instance, $this->defaults() );
84
85
		$title = stripslashes( $instance['title'] );
86
		$display = $instance['display'];
87
		$format = $instance['format'];
88
		$image_size = isset( $instance['imagesize'] ) ? $instance['imagesize'] : 0 ;
89
		$image_color = isset( $instance['imagecolor'] ) ? $instance['imagecolor'] : 'red';
90
91
		echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
92
		<input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( $title ) . '" />
93
		</label></p>';
94
95
		$displays = array(
96
			'posts'          => __( 'Posts', 'jetpack' ),
97
			'comments'       => __( 'Comments', 'jetpack' ),
98
			'posts-comments' => __( 'Posts & Comments', 'jetpack' )
99
		);
100
		echo '<p><label for="' . $this->get_field_id( 'display' ) . '">' . esc_html__( 'Feed(s) to Display:', 'jetpack' ) . '
101
		<select class="widefat" id="' . $this->get_field_id( 'display' ) . '" name="' . $this->get_field_name( 'display' ) . '">';
102
		foreach ( $displays as $display_option => $label ) {
103
			echo '<option value="' . esc_attr( $display_option ) . '"';
104
			if ( $display_option == $display ) echo ' selected="selected"';
105
			echo '>' . esc_html( $label ) . '</option>' . "\n";
106
		}
107
		echo '</select></label></p>';
108
109
		$formats = array(
110
			'text'       => __( 'Text Link', 'jetpack' ),
111
			'image'      => __( 'Image Link', 'jetpack' ),
112
			'text-image' => __( 'Text & Image Links', 'jetpack' )
113
		);
114
		echo '<p><label for="' . $this->get_field_id( 'format' ) . '">' . _x( 'Format:', 'Noun', 'jetpack' ) . '
115
		<select class="widefat" id="' . $this->get_field_id( 'format' ) . '" name="' . $this->get_field_name( 'format' ) . '" onchange="if ( this.value == \'text\' ) jQuery( \'#' . $this->get_field_id( 'image-settings' ) . '\' ).fadeOut(); else jQuery( \'#' . $this->get_field_id( 'image-settings' ) . '\' ).fadeIn();">';
116 View Code Duplication
		foreach ( $formats as $format_option => $label ) {
117
			echo '<option value="' . esc_attr( $format_option ) . '"';
118
			if ( $format_option == $format ) echo ' selected="selected"';
119
			echo '>' . esc_html( $label ) . '</option>' . "\n";
120
		}
121
		echo '</select></label></p>';
122
123
		echo '<div id="' . $this->get_field_id( 'image-settings' ) . '"';
124
		if ( 'text' == $format ) echo ' style="display: none;"';
125
		echo '><h3>' . esc_html__( 'Image Settings:', 'jetpack' ) . '</h3>';
126
127
		$sizes = array(
128
			'small'  => __( 'Small', 'jetpack' ),
129
			'medium' => __( 'Medium', 'jetpack' ),
130
			'large'  => __( 'Large', 'jetpack' )
131
		);
132
		echo '<p><label for="' . $this->get_field_id( 'imagesize' ) . '">' . esc_html__( 'Image Size:', 'jetpack' ) . '
133
		<select class="widefat" id="' . $this->get_field_id( 'imagesize' ) . '" name="' . $this->get_field_name( 'imagesize' ) . '">';
134 View Code Duplication
		foreach ( $sizes as $size => $label ) {
135
			echo '<option value="' . esc_attr( $size) . '"';
136
			if ( $size == $image_size ) echo ' selected="selected"';
137
			echo '>' . esc_html( $label ) . '</option>' . "\n";
138
		}
139
		echo '</select></label></p>';
140
141
		$colors = array(
142
			'red'    => __( 'Red', 'jetpack' ),
143
			'orange' => __( 'Orange', 'jetpack' ),
144
			'green'  => __( 'Green', 'jetpack' ),
145
			'blue'   => __( 'Blue', 'jetpack' ),
146
			'purple' => __( 'Purple', 'jetpack' ),
147
			'pink'   => __( 'Pink', 'jetpack' ),
148
			'silver' => __( 'Silver', 'jetpack' ),
149
		);
150
		echo '<p><label for="' . $this->get_field_id( 'imagecolor' ) . '">' . esc_html__( 'Image Color:', 'jetpack' ) . '
151
		<select class="widefat" id="' . $this->get_field_id( 'imagecolor' ) . '" name="' . $this->get_field_name( 'imagecolor' ) . '">';
152
		foreach ( $colors as $color => $label ) {
153
			echo '<option value="' . esc_attr( $color) . '"';
154
			if ( $color == $image_color ) echo ' selected="selected"';
155
			echo '>' . esc_html( $label ) . '</option>' . "\n";
156
		}
157
		echo '</select></label></p></div>';
158
	}
159
160
	function _rss_link( $type = 'posts', $args ) {
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
161
		if ( 'posts' == $type ) {
162
			$type_text = __( 'Posts', 'jetpack' );
163
			$rss_type = 'rss2_url';
164
		} elseif ( 'comments' == $type ) {
165
			$type_text = __( 'Comments', 'jetpack' );
166
			$rss_type = 'comments_rss2_url';
167
		}
168
169
		$subscribe_to = sprintf( __( 'Subscribe to %s', 'jetpack' ), $type_text );
0 ignored issues
show
Bug introduced by
The variable $type_text does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
170
171
		$link_item = '';
172
		$format = $args['format'];
173
174
		/**
175
		 * Filters the target link attribute for the RSS link in the RSS widget.
176
		 *
177
		 * @module widgets
178
		 *
179
		 * @since 3.4.0
180
		 *
181
		 * @param bool false Control whether the link should open in a new tab. Default to false.
182
		 */
183
		if ( apply_filters( 'jetpack_rsslinks_widget_target_blank', false ) ) {
184
			$link_target = '_blank';
185
		} else {
186
			$link_target = '_self';
187
		}
188
189
		if ( 'image' == $format || 'text-image' == $format ) {
190
			/**
191
			 * Filters the image used as RSS icon in the RSS widget.
192
			 *
193
			 * @module widgets
194
			 *
195
			 * @since 3.6.0
196
			 *
197
			 * @param string $var URL of RSS Widget icon.
198
			 */
199
			$link_image = apply_filters( 'jetpack_rss_widget_icon', plugins_url( 'images/rss/' . $args['imagecolor'] . '-' . $args['imagesize'] . '.png', dirname( dirname( __FILE__ ) ) ) );
200
			$link_item = '<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '"><img src="' . esc_url( $link_image ) . '" alt="RSS Feed" /></a>';
0 ignored issues
show
Bug introduced by
The variable $rss_type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
201
		}
202 View Code Duplication
		if ( 'text-image' == $format ) {
203
			$link_item .= '&nbsp;<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__( 'RSS - ' . $type_text, 'jetpack' ). '</a>';
204
		}
205 View Code Duplication
		if ( 'text' == $format ) {
206
			$link_item = '<a target="' . $link_target . '" href="' . get_bloginfo( $rss_type ) . '" title="' . esc_attr( $subscribe_to ) . '">' . esc_html__( 'RSS - ' . $type_text, 'jetpack' ). '</a>';
207
		}
208
209
		if ( 'text' == $format )
210
			echo '<li>';
211
		else
212
			echo '<p>';
213
		echo $link_item;
214
		if ( 'text' == $format )
215
			echo '</li>';
216
		else
217
			echo '</p>';
218
219
	}
220
} // Class Jetpack_RSS_Links_Widget
221
222
function jetpack_rss_links_widget_init() {
223
	register_widget( 'Jetpack_RSS_Links_Widget' );
224
}
225
add_action( 'widgets_init', 'jetpack_rss_links_widget_init' );
226