Completed
Push — try/alt-xmlrpc ( 1e2c67...990d55 )
by
unknown
29:16 queued 15:53
created

Jetpack_Flickr_Widget::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 0
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Disable direct access/execution to/of the widget code.
4
 */
5
if ( ! defined( 'ABSPATH' ) ) {
6
	exit;
7
}
8
9
if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) {
10
	/**
11
	 * Flickr Widget
12
	 *
13
	 * Display your recent Flickr photos.
14
	 */
15
	class Jetpack_Flickr_Widget extends WP_Widget {
16
		/**
17
		 * Constructor.
18
		 */
19 View Code Duplication
		function __construct() {
20
			parent::__construct(
21
				'flickr',
22
				/** This filter is documented in modules/widgets/facebook-likebox.php */
23
				apply_filters( 'jetpack_widget_name', esc_html__( 'Flickr', 'jetpack' ) ),
24
				array(
25
					'description' => esc_html__( 'Display your recent Flickr photos.', 'jetpack' ),
26
					'customize_selective_refresh' => true,
27
				),
28
				array()
29
			);
30
31
			if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
32
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
33
			}
34
		}
35
36
		/**
37
		 * Enqueue style.
38
		 */
39
		function enqueue_style() {
40
			wp_enqueue_style( 'flickr-widget-style', plugins_url( 'flickr/style.css', __FILE__ ), array(), '20170405' );
41
		}
42
43
		/**
44
		 * Return an associative array of default values.
45
		 *
46
		 * These values are used in new widgets.
47
		 *
48
		 * @return array Default values for the widget options.
49
		 */
50
		public function defaults() {
51
			return array(
52
				'title'             => esc_html__( 'Flickr Photos', 'jetpack' ),
53
				'items'             => 3,
54
				'flickr_image_size' => 'thumbnail',
55
				'flickr_rss_url'    => ''
56
			);
57
		}
58
59
		/**
60
		 * Front-end display of the widget.
61
		 *
62
		 * @param array $args     Widget arguments.
63
		 * @param array $instance Saved values from database.
64
		 */
65
		public function widget( $args, $instance ) {
66
			$instance = wp_parse_args( $instance, $this->defaults() );
67
68
			$image_size_string = 'small' == $instance['flickr_image_size'] ? '_m.jpg' : '_t.jpg';
69
70
			if ( ! empty( $instance['flickr_rss_url'] ) ) {
71
72
				/*
73
				 * Parse the URL, and rebuild a URL that's sure to display images.
74
				 * Some Flickr Feeds do not display images by default.
75
				 */
76
				$flickr_parameters = parse_url( htmlspecialchars_decode( $instance['flickr_rss_url'] ) );
77
78
				// Is it a Flickr Feed.
79
				if (
80
					! empty( $flickr_parameters['host'] )
81
					&& ! empty( $flickr_parameters['query'] )
82
					&& false !== strpos( $flickr_parameters['host'], 'flickr' )
83
				) {
84
					parse_str( $flickr_parameters['query'], $vars );
85
86
					// Do we have an ID in the feed? Let's continue.
87
					if ( isset( $vars['id'] ) ) {
88
89
						// Flickr Feeds can be used for groups or for individuals.
90
						if (
91
							! empty( $flickr_parameters['path'] )
92
							&& false !== strpos( $flickr_parameters['path'], 'groups' )
93
						) {
94
							$feed_url = 'https://api.flickr.com/services/feeds/groups_pool.gne';
95
						} else {
96
							$feed_url = 'https://api.flickr.com/services/feeds/photos_public.gne';
97
						}
98
99
						// Build our new RSS feed.
100
						$rss_url = sprintf(
101
							'%1$s?id=%2$s&format=rss_200_enc',
102
							esc_url( $feed_url ),
103
							esc_attr( $vars['id'] )
104
						);
105
					}
106
				}
107
			} // End if().
108
109
			// Still no RSS feed URL? Get a default feed from Flickr to grab interesting photos.
110
			if ( empty( $rss_url ) ) {
111
				$rss_url = 'https://api.flickr.com/services/feeds/photos_interesting.gne?format=rss_200';
112
			}
113
114
			$rss = fetch_feed( $rss_url );
115
116
			$photos = '';
117
			if ( ! is_wp_error( $rss ) ) {
118
				foreach ( $rss->get_items( 0, $instance['items'] ) as $photo ) {
119
					switch ( $instance['flickr_image_size'] ) {
120
						case 'thumbnail':
121
							$src = $photo->get_enclosure()->get_thumbnail();
122
							break;
123
						case 'small':
124
							$src = preg_match( '/src="(.*?)"/i', $photo->get_description(), $p );
0 ignored issues
show
Unused Code introduced by
$src is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
125
							$src = $p[1];
126
							break;
127
						case 'large':
128
							$src = $photo->get_enclosure()->get_link();
129
							break;
130
					}
131
132
					$photos .= '<a href="' . esc_url( $photo->get_permalink(), array( 'http', 'https' ) ) . '">';
133
					$photos .= '<img src="' . esc_url( $src, array( 'http', 'https' ) ) . '" ';
0 ignored issues
show
Bug introduced by
The variable $src 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...
134
					$photos .= 'alt="' . esc_attr( $photo->get_title() ) . '" ';
135
					$photos .= 'border="0" ';
136
					$photos .= 'title="' . esc_attr( $photo->get_title() ) . '" ';
137
					$photos .= ' /></a><br /><br />';
138
				}
139
				if ( ! empty( $photos ) && class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
140
					$photos = Jetpack_Photon::filter_the_content( $photos );
141
				}
142
143
				$flickr_home = $rss->get_link();
144
			}
145
146
			echo $args['before_widget'];
147
			if ( empty( $photos ) ) {
148
				if ( current_user_can( 'edit_theme_options' ) ) {
149
					printf(
150
						'<p>%1$s<br />%2$s</p>',
151
						esc_html__( 'There are no photos to display. Make sure your Flickr feed URL is correct, and that your pictures are publicly accessible.', 'jetpack' ),
152
						esc_html__( '(Only admins can see this message)', 'jetpack' )
153
					);
154
				}
155
			} else {
156
				echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
157
				require( dirname( __FILE__ ) . '/flickr/widget.php' );
158
			}
159
			echo $args['after_widget'];
160
			/** This action is already documented in modules/widgets/gravatar-profile.php */
161
			do_action( 'jetpack_stats_extra', 'widget_view', 'flickr' );
162
		}
163
164
		/**
165
		 * Back-end widget form.
166
		 *
167
		 * @param array $instance Previously saved values from database.
168
		 */
169
		public function form( $instance ) {
170
			$instance = wp_parse_args( $instance, $this->defaults() );
171
			require( dirname( __FILE__ ) . '/flickr/form.php' );
172
		}
173
174
		/**
175
		 * Sanitize widget form values as they are saved.
176
		 *
177
		 * @param  array $new_instance Values just sent to be saved.
178
		 * @param  array $old_instance Previously saved values from database.
179
		 * @return array Updated safe values to be saved.
180
		 */
181
		public function update( $new_instance, $old_instance ) {
182
			$instance = array();
183
			$defaults = $this->defaults();
0 ignored issues
show
Unused Code introduced by
$defaults is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
184
185
			if ( isset( $new_instance['title'] ) ) {
186
				$instance['title'] = wp_kses( $new_instance['title'], array() );
187
			}
188
189
			if ( isset( $new_instance['items'] ) ) {
190
				$instance['items'] = intval( $new_instance['items'] );
191
			}
192
193
			if (
194
				isset( $new_instance['flickr_image_size'] ) &&
195
				in_array( $new_instance['flickr_image_size'], array( 'thumbnail', 'small', 'large' ) )
196
			) {
197
				$instance['flickr_image_size'] = $new_instance['flickr_image_size'];
198
			} else {
199
				$instance['flickr_image_size'] = 'thumbnail';
200
			}
201
202
			if ( isset( $new_instance['flickr_rss_url'] ) ) {
203
				$instance['flickr_rss_url'] = esc_url( $new_instance['flickr_rss_url'], array( 'http', 'https' ) );
204
205
				if ( strlen( $instance['flickr_rss_url'] ) < 10 ) {
206
					$instance['flickr_rss_url'] = '';
207
				}
208
			}
209
210
			return $instance;
211
		}
212
	}
213
214
	// Register Jetpack_Flickr_Widget widget.
215
	function jetpack_register_flickr_widget() {
216
		register_widget( 'Jetpack_Flickr_Widget' );
217
	}
218
	add_action( 'widgets_init', 'jetpack_register_flickr_widget' );
219
}
220