Completed
Push — fix/incompatible-flickr-format... ( 4f02da )
by Jeremy
10:18
created

Jetpack_Flickr_Widget   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 171
Duplicated Lines 9.36 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 16
loc 171
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 3
A enqueue_style() 0 3 1
A defaults() 0 8 1
C widget() 0 71 13
A form() 0 4 1
C update() 0 31 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
			$rss_url = ( ! isset( $instance['flickr_rss_url'] ) || empty( $instance['flickr_rss_url'] ) )
71
				? 'https://api.flickr.com/services/feeds/photos_interesting.gne?format=rss_200'
72
				: htmlspecialchars_decode( $instance['flickr_rss_url'] );
73
74
			/**
75
			 * Parse the URL, and rebuild a URL that's sure to display images.
76
			 * Some Flickr Feeds do not display images by default.
77
			 */
78
			$flickr_parameters = parse_url( $rss_url );
79
			parse_str( $flickr_parameters['query'], $vars );
80
81
			// Do we have an ID in the feed? Let's continue.
82
			if ( isset( $vars['id'] ) ) {
83
				/**
84
				 * Flickr Feeds can be used for groups or for individuals.
85
				 */
86
				if (
87
					! empty( $flickr_parameters['path'] )
88
					&& false !== strpos( $flickr_parameters['path'], 'groups' )
89
				) {
90
					$feed_url = 'https://api.flickr.com/services/feeds/groups_pool.gne';
91
				} else {
92
					$feed_url = 'https://api.flickr.com/services/feeds/photos_public.gne';
93
				}
94
95
				// Build our new RSS feed.
96
				$rss_url = sprintf(
97
					'%1$s?id=%2$s&format=rss_200_enc',
98
					esc_url( $feed_url ),
99
					esc_attr( $vars['id'] )
100
				);
101
			}
102
103
			$rss = fetch_feed( $rss_url );
104
105
			$photos = '';
106
			if ( ! is_wp_error( $rss ) ) {
107
				foreach ( $rss->get_items( 0, $instance['items'] ) as $photo ) {
108
					if ( $enclosure = $photo->get_enclosure() ) {
109
						$src = str_replace( '_s.jpg', $image_size_string, $enclosure->get_thumbnail() );
110
					} else {
111
						$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...
112
						$src = str_replace( '_m.jpg', $image_size_string, $p[1] );
113
					}
114
115
					$photos .= '<a href="' . esc_url( $photo->get_permalink(), array( 'http', 'https' ) ) . '">';
116
					$photos .= '<img src="' . esc_url( $src, array( 'http', 'https' ) ) . '" ';
117
					$photos .= 'alt="' . esc_attr( $photo->get_title() ) . '" ';
118
					$photos .= 'border="0" ';
119
					$photos .= 'title="' . esc_attr( $photo->get_title() ) . '" ';
120
					$photos .= ' /></a><br /><br />';
121
				}
122
				if ( ! empty( $photos ) && class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
123
					$photos = Jetpack_Photon::filter_the_content( $photos );
124
				}
125
126
				$flickr_home = $rss->get_link();
127
			}
128
129
			echo $args['before_widget'];
130
			echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
131
			require( dirname( __FILE__ ) . '/flickr/widget.php' );
132
			echo $args['after_widget'];
133
			/** This action is already documented in modules/widgets/gravatar-profile.php */
134
			do_action( 'jetpack_stats_extra', 'widget_view', 'flickr' );
135
		}
136
137
		/**
138
		 * Back-end widget form.
139
		 *
140
		 * @param array $instance Previously saved values from database.
141
		 */
142
		public function form( $instance ) {
143
			$instance = wp_parse_args( $instance, $this->defaults() );
144
			require( dirname( __FILE__ ) . '/flickr/form.php' );
145
		}
146
147
		/**
148
		 * Sanitize widget form values as they are saved.
149
		 *
150
		 * @param  array $new_instance Values just sent to be saved.
151
		 * @param  array $old_instance Previously saved values from database.
152
		 * @return array Updated safe values to be saved.
153
		 */
154
		public function update( $new_instance, $old_instance ) {
155
			$instance = array();
156
			$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...
157
158
			if ( isset( $new_instance['title'] ) ) {
159
				$instance['title'] = wp_kses( $new_instance['title'], array() );
160
			}
161
162
			if ( isset( $new_instance['items'] ) ) {
163
				$instance['items'] = intval( $new_instance['items'] );
164
			}
165
166
			if (
167
				isset( $new_instance['flickr_image_size'] ) &&
168
				in_array( $new_instance['flickr_image_size'], array( 'thumbnail', 'small' ) )
169
			) {
170
				$instance['flickr_image_size'] = $new_instance['flickr_image_size'];
171
			} else {
172
				$instance['flickr_image_size'] = 'thumbnail';
173
			}
174
175
			if ( isset( $new_instance['flickr_rss_url'] ) ) {
176
				$instance['flickr_rss_url'] = esc_url( $new_instance['flickr_rss_url'], array( 'http', 'https' ) );
177
178
				if ( strlen( $instance['flickr_rss_url'] ) < 10 ) {
179
					$instance['flickr_rss_url'] = '';
180
				}
181
			}
182
183
			return $instance;
184
		}
185
	}
186
187
	// Register Jetpack_Flickr_Widget widget.
188
	function jetpack_register_flickr_widget() {
189
		register_widget( 'Jetpack_Flickr_Widget' );
190
	}
191
	add_action( 'widgets_init', 'jetpack_register_flickr_widget' );
192
}
193