PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.3
Jetpack – WP Security, Backup, Speed, & Growth v7.2.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / widgets / goodreads.php
goodreads.php
158 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/goodreads.css', __FILE__ ) );
46 wp_style_add_data( 'goodreads-widget', 'rtl', 'replace' );
47 }
48
49 function widget( $args, $instance ) {
50 /** This action is documented in modules/widgets/gravatar-profile.php */
51 do_action( 'jetpack_stats_extra', 'widget_view', 'goodreads' );
52
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 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 'https://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
73 $instance['user_id'] = absint( $instance['user_id'] );
74
75 // Set widget ID based on shelf.
76 $this->goodreads_widget_id = $instance['user_id'] . '_' . $instance['shelf'];
77
78 if ( empty( $title ) ) {
79 $title = esc_html__( 'Goodreads', 'jetpack' );
80 }
81
82 echo $args['before_widget'];
83 echo $args['before_title'] . $title . $args['after_title'];
84
85 $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 );
86
87 echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ) . '"></div>' . "\n";
88 echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n";
89
90 echo $args['after_widget'];
91 }
92
93 function goodreads_user_id_exists( $user_id ) {
94 $url = "https://www.goodreads.com/user/show/$user_id/";
95 $response = wp_remote_head(
96 $url, array(
97 'httpversion' => '1.1',
98 'timeout' => 3,
99 'redirection' => 2,
100 )
101 );
102 if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
103 return true;
104 } else {
105 return false;
106 }
107 }
108
109 function update( $new_instance, $old_instance ) {
110 $instance = $old_instance;
111
112 $instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) );
113 if ( ! empty( $instance['user_id'] ) && ( ! isset( $old_instance['user_id'] ) || $instance['user_id'] !== $old_instance['user_id'] ) ) {
114 if ( ! $this->goodreads_user_id_exists( $instance['user_id'] ) ) {
115 $instance['user_id'] = 'invalid';
116 }
117 }
118 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
119 $shelf = wp_kses( stripslashes( $new_instance['shelf'] ), array() );
120 if ( array_key_exists( $shelf, $this->shelves ) ) {
121 $instance['shelf'] = $shelf;
122 }
123
124 return $instance;
125 }
126
127 function form( $instance ) {
128 //Defaults
129 $instance = wp_parse_args(
130 (array) $instance, array(
131 'user_id' => '',
132 'title' => 'Goodreads',
133 'shelf' => 'read',
134 )
135 );
136
137 echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
138 <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'] ) . '" />
139 </label></p>
140 <p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">';
141 printf( __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), 'https://en.support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' );
142 if ( 'invalid' === $instance['user_id'] ) {
143 printf( '<br /><small class="error">%s</small>&nbsp;', __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) );
144 $instance['user_id'] = '';
145 }
146 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'] ) . '" />
147 </label></p>
148 <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . '
149 <select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >';
150 foreach ( $this->shelves as $_shelf_value => $_shelf_display ) {
151 echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'] ) . '>' . $_shelf_display . "</option>\n";
152 }
153 echo '</select>
154 </label></p>
155 ';
156 }
157 }
158