PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.2
Jetpack – WP Security, Backup, Speed, & Growth v6.2
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 in Jetpack – WP Security, Backup, Speed, & Growth 6.2, at modules/widgets/goodreads.php

142 lines 6.1 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 $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'];
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
90 function goodreads_user_id_exists( $user_id ) {
91 $url = "https://www.goodreads.com/user/show/$user_id/";
92 $response = wp_remote_head( $url, array( 'httpversion' => '1.1', 'timeout' => 3, 'redirection' => 2 ) );
93 if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
94 return true;
95 } else {
96 return false;
97 }
98 }
99
100 function update( $new_instance, $old_instance ) {
101 $instance = $old_instance;
102
103 $instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) );
104 if ( ! empty( $instance['user_id'] ) && ( ! isset( $old_instance['user_id'] ) || $instance['user_id'] !== $old_instance['user_id'] ) ) {
105 if ( ! $this->goodreads_user_id_exists( $instance['user_id'] ) ) {
106 $instance['user_id'] = 'invalid';
107 }
108 }
109 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
110 $shelf = wp_kses( stripslashes( $new_instance['shelf'] ), array() );
111 if ( array_key_exists( $shelf, $this->shelves ) )
112 $instance['shelf'] = $shelf;
113
114 return $instance;
115 }
116
117 function form( $instance ) {
118 //Defaults
119 $instance = wp_parse_args( (array) $instance, array( 'user_id' => '', 'title' => 'Goodreads', 'shelf' => 'read' ) );
120
121 echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
122 <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'] ) . '" />
123 </label></p>
124 <p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">';
125 printf( __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), 'https://en.support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' );
126 if ( 'invalid' === $instance['user_id'] ) {
127 printf( '<br /><small class="error">%s</small>&nbsp;', __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) );
128 $instance['user_id'] = '';
129 }
130 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'] ) . '" />
131 </label></p>
132 <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . '
133 <select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >';
134 foreach( $this->shelves as $_shelf_value => $_shelf_display ) {
135 echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'] ) . ">" . $_shelf_display . "</option>\n";
136 }
137 echo '</select>
138 </label></p>
139 ';
140 }
141 }
142