Completed
Push — add/sync-rest-2 ( dcf3c8...9df295 )
by
unknown
10:28
created

WPCOM_Widget_Goodreads::widget()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 40
Code Lines 24

Duplication

Lines 12
Ratio 30 %
Metric Value
dl 12
loc 40
rs 6.7272
cc 7
eloc 24
nc 5
nop 2
1
<?php
2
/**
3
 * Register the widget for use in Appearance -> Widgets
4
 */
5
add_action( 'widgets_init', 'jetpack_goodreads_widget_init' );
6
7
function jetpack_goodreads_widget_init() {
8
	register_widget( 'WPCOM_Widget_Goodreads' );
9
}
10
11
/**
12
 * Goodreads widget class
13
 * Display a user's Goodreads shelf.
14
 * Customize user_id, title, and shelf
15
 *
16
 */
17
class WPCOM_Widget_Goodreads extends WP_Widget {
18
19
	private $goodreads_widget_id = 0;
20
21
	function __construct() {
22
		parent::__construct(
23
			'wpcom-goodreads',
24
			/** This filter is documented in modules/widgets/facebook-likebox.php */
25
			apply_filters( 'jetpack_widget_name', __( 'Goodreads', 'jetpack' ) ),
26
			array(
27
				'classname'   => 'widget_goodreads',
28
				'description' => __( 'Display your books from Goodreads', 'jetpack' ),
29
				'customize_selective_refresh' => true,
30
			)
31
		);
32
		// For user input sanitization and display
33
		$this->shelves = array(
34
			'read'              => _x( 'Read', 'past participle: books I have read', 'jetpack' ),
35
			'currently-reading' => __( 'Currently Reading', 'jetpack' ),
36
			'to-read'           => _x( 'To Read', 'my list of books to read', 'jetpack' )
37
		);
38
39
		if ( is_active_widget( '', '', 'wpcom-goodreads' ) || is_customize_preview() ) {
40
			add_action( 'wp_print_styles', array( $this, 'enqueue_style' ) );
41
		}
42
	}
43
44
	function enqueue_style() {
45
		if ( is_rtl() ) {
46
			wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/rtl/goodreads-rtl.css', __FILE__ ) );
47
		} else {
48
			wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/goodreads.css', __FILE__ ) );
49
		}
50
	}
51
52
	function widget( $args, $instance ) {
53
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
54
		$title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
55
56 View Code Duplication
		if ( empty( $instance['user_id'] ) || 'invalid' === $instance['user_id'] ) {
57
			if ( current_user_can('edit_theme_options') ) {
58
				echo $args['before_widget'];
59
				echo '<p>' . sprintf(
60
					__( 'You need to enter your numeric user ID for the <a href="%1$s">Goodreads Widget</a> to work correctly. <a href="%2$s" target="_blank">Full instructions</a>.', 'jetpack' ),
61
					esc_url( admin_url( 'widgets.php' ) ),
62
					'http://support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id'
63
				) . '</p>';
64
				echo $args['after_widget'];
65
			}
66
			return;
67
		}
68
69
		if ( !array_key_exists( $instance['shelf'], $this->shelves ) )
70
			return;
71
72
		$instance['user_id'] = absint( $instance['user_id'] );
73
74
		// Set widget ID based on shelf.
75
		$this->goodreads_widget_id = $instance['user_id'] . '_' . $instance['shelf'];
0 ignored issues
show
Documentation Bug introduced by
The property $goodreads_widget_id was declared of type integer, but $instance['user_id'] . '_' . $instance['shelf'] is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
76
77
		if ( empty( $title ) ) $title = esc_html__( 'Goodreads', 'jetpack' );
78
79
		echo $args['before_widget'];
80
		echo $args['before_title'] . $title . $args['after_title'];
81
82
		$goodreads_url = 'https://www.goodreads.com/review/custom_widget/' . urlencode( $instance['user_id'] ) . '.' . urlencode( $instance['title'] ) . ':%20' . urlencode( $instance['shelf'] ) . '?cover_position=&cover_size=small&num_books=5&order=d&shelf=' . urlencode( $instance['shelf'] ) . '&sort=date_added&widget_bg_transparent=&widget_id=' . esc_attr( $this->goodreads_widget_id ) ;
83
84
		echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ). '"></div>' . "\n";
85
		echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n";
86
87
		echo $args['after_widget'];
88
89
		/** This action is already documented in modules/widgets/gravatar-profile.php */
90
		do_action( 'jetpack_stats_extra', 'widget', 'goodreads' );
91
	}
92
93
	function goodreads_user_id_exists( $user_id ) {
94
		$url = "http://www.goodreads.com/user/show/$user_id/";
95
		$response = wp_remote_head( $url, array( 'httpversion'=>'1.1', 'timeout'=>3, 'redirection'=> 2 ) );
96
		if ( wp_remote_retrieve_response_code( $response ) === 200 )
97
			return true;
98
		else
99
			return false;
100
	}
101
102
	function update( $new_instance, $old_instance ) {
103
		$instance = $old_instance;
104
105
		$instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) );
106
		if ( ! empty( $instance['user_id'] ) && ( ! isset( $old_instance['user_id'] ) || $instance['user_id'] !== $old_instance['user_id'] ) ) {
107
			if ( ! $this->goodreads_user_id_exists( $instance['user_id'] ) ) {
108
				$instance['user_id'] = 'invalid';
109
			}
110
		}
111
		$instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
112
		$shelf = wp_kses( stripslashes( $new_instance['shelf'] ), array() );
113
		if ( array_key_exists( $shelf, $this->shelves ) )
114
			$instance['shelf'] = $shelf;
115
116
		return $instance;
117
	}
118
119
	function form( $instance ) {
120
		//Defaults
121
		$instance = wp_parse_args( (array) $instance, array( 'user_id' => '', 'title' => 'Goodreads', 'shelf' => 'read' ) );
122
123
		echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
124
		<input class="widefat" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( $instance['title'] ) . '" />
125
		</label></p>
126
		<p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">';
127
		printf( __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), 'https://en.support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' );
128
		if ( 'invalid' === $instance['user_id'] ) {
129
			printf( '<br /><small class="error">%s</small>&nbsp;', __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) );
130
			$instance['user_id'] = '';
131
		}
132
		echo '<input class="widefat" id="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '" name="' . esc_attr( $this->get_field_name( 'user_id' ) ) . '" type="text" value="' . esc_attr( $instance['user_id'] ) . '" />
133
		</label></p>
134
		<p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . '
135
		<select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >';
136
		foreach( $this->shelves as $_shelf_value => $_shelf_display ) {
137
			echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'] ) . ">" . $_shelf_display . "</option>\n";
138
		}
139
		echo '</select>
140
		</label></p>
141
		';
142
	}
143
}
144