PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.1
Jetpack – WP Security, Backup, Speed, & Growth v12.1
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 12.1, at modules/widgets/goodreads.php

222 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
4
5 add_action( 'widgets_init', 'jetpack_goodreads_widget_init' );
6 /**
7 * Register the widget for use in Appearance -> Widgets
8 */
9 function jetpack_goodreads_widget_init() {
10 register_widget( 'WPCOM_Widget_Goodreads' );
11 }
12
13 /**
14 * Goodreads widget class
15 * Display a user's Goodreads shelf.
16 * Customize user_id, title, and shelf
17 */
18 class WPCOM_Widget_Goodreads extends WP_Widget {
19 /**
20 * Widget ID based on Goodreads user ID and shelf.
21 *
22 * @var int
23 */
24 private $goodreads_widget_id = 0;
25
26 /**
27 * WPCOM_Widget_Goodreads constructor.
28 */
29 public function __construct() {
30 parent::__construct(
31 'wpcom-goodreads',
32 /** This filter is documented in modules/widgets/facebook-likebox.php */
33 apply_filters( 'jetpack_widget_name', __( 'Goodreads', 'jetpack' ) ),
34 array(
35 'classname' => 'widget_goodreads',
36 'description' => __( 'Display your books from Goodreads', 'jetpack' ),
37 'customize_selective_refresh' => true,
38 )
39 );
40 // For user input sanitization and display.
41 $this->shelves = array(
42 'read' => _x( 'Read', 'past participle: books I have read', 'jetpack' ),
43 'currently-reading' => __( 'Currently Reading', 'jetpack' ),
44 'to-read' => _x( 'To Read', 'my list of books to read', 'jetpack' ),
45 );
46
47 if ( is_active_widget( '', '', 'wpcom-goodreads' ) || is_customize_preview() ) {
48 add_action( 'wp_print_styles', array( $this, 'enqueue_style' ) );
49 }
50 }
51
52 /**
53 * Enqueue widget styles.
54 */
55 public function enqueue_style() {
56 wp_enqueue_style(
57 'goodreads-widget',
58 plugins_url( 'goodreads/css/goodreads.css', __FILE__ ),
59 array(),
60 JETPACK__VERSION
61 );
62 wp_style_add_data( 'goodreads-widget', 'rtl', 'replace' );
63 }
64
65 /**
66 * Display the widget.
67 *
68 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
69 * @param array $instance The settings for the particular instance of the widget.
70 */
71 public function widget( $args, $instance ) {
72 /** This action is documented in modules/widgets/gravatar-profile.php */
73 do_action( 'jetpack_stats_extra', 'widget_view', 'goodreads' );
74
75 /** This filter is documented in core/src/wp-includes/default-widgets.php */
76 $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
77
78 if ( empty( $instance['user_id'] ) || 'invalid' === $instance['user_id'] ) {
79 if ( current_user_can( 'edit_theme_options' ) ) {
80 echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
81 echo '<p>' . sprintf(
82 wp_kses(
83 /* translators: %1$s: link to the widget settings page. %2$s: support article URL for Goodreads widget. */
84 __( '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' ),
85 array(
86 'a' => array(
87 'href' => array(),
88 'target' => array(),
89 ),
90 )
91 ),
92 esc_url( admin_url( 'widgets.php' ) ),
93 'https://wordpress.com/support/widgets/goodreads-widget/#set-up-the-widget'
94 ) . '</p>';
95 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
96 }
97 return;
98 }
99
100 if ( ! array_key_exists( $instance['shelf'], $this->shelves ) ) {
101 return;
102 }
103
104 $instance['user_id'] = absint( $instance['user_id'] );
105
106 // Set widget ID based on shelf.
107 $this->goodreads_widget_id = $instance['user_id'] . '_' . $instance['shelf'];
108 $this->goodreads_widget_id = str_replace( '-', '_', $this->goodreads_widget_id ); // Goodreads' custom widget does not like dashes.
109
110 if ( empty( $title ) ) {
111 $title = esc_html__( 'Goodreads', 'jetpack' );
112 }
113
114 echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
115 echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
116
117 $goodreads_url = 'https://www.goodreads.com/review/custom_widget/' . rawurlencode( $instance['user_id'] ) . '.' . rawurlencode( $instance['title'] ) . ':%20' . rawurlencode( $instance['shelf'] ) . '?cover_position=&cover_size=small&num_books=5&order=d&shelf=' . rawurlencode( $instance['shelf'] ) . '&sort=date_added&widget_bg_transparent=&widget_id=' . rawurlencode( $this->goodreads_widget_id );
118
119 echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ) . '"></div>' . "\n";
120 echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
121
122 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
123 }
124
125 /**
126 * Check if given Goodreads user ID exists.
127 *
128 * @param string $user_id User ID.
129 */
130 public function goodreads_user_id_exists( $user_id ) {
131 $url = "https://www.goodreads.com/user/show/$user_id/";
132 $response = wp_remote_head(
133 $url,
134 array(
135 'httpversion' => '1.1',
136 'timeout' => 10,
137 'redirection' => 2,
138 )
139 );
140 if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
141 return true;
142 } else {
143 return false;
144 }
145 }
146
147 /**
148 * Update widget.
149 *
150 * @see WP_Widget::update()
151 *
152 * @param array $new_instance New widget instance data.
153 * @param array $old_instance Old widget instance data.
154 */
155 public function update( $new_instance, $old_instance ) {
156 $instance = $old_instance;
157
158 $instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) );
159 if ( ! empty( $instance['user_id'] ) && ( ! isset( $old_instance['user_id'] ) || $instance['user_id'] !== $old_instance['user_id'] ) ) {
160 if ( ! $this->goodreads_user_id_exists( $instance['user_id'] ) ) {
161 $instance['user_id'] = 'invalid';
162 }
163 }
164 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
165 $shelf = wp_kses( stripslashes( $new_instance['shelf'] ), array() );
166 if ( array_key_exists( $shelf, $this->shelves ) ) {
167 $instance['shelf'] = $shelf;
168 }
169
170 return $instance;
171 }
172
173 /**
174 * Outputs the widget settings form.
175 *
176 * @param array $instance Current settings.
177 */
178 public function form( $instance ) {
179 // Defaults.
180 $instance = wp_parse_args(
181 (array) $instance,
182 array(
183 'user_id' => '',
184 'title' => 'Goodreads',
185 'shelf' => 'read',
186 )
187 );
188
189 echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
190 <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'] ) . '" />
191 </label></p>
192 <p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">';
193 printf(
194 wp_kses(
195 /* translators: %s: support article URL for Goodreads widget. */
196 __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ),
197 array(
198 'a' => array(
199 'href' => array(),
200 'target' => array(),
201 ),
202 )
203 ),
204 'https://wordpress.com/support/widgets/goodreads-widget/#set-up-the-widget'
205 );
206 if ( 'invalid' === $instance['user_id'] ) {
207 printf( '<br /><small class="error">%s</small>&nbsp;', esc_html( __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) ) );
208 $instance['user_id'] = '';
209 }
210 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'] ) . '" />
211 </label></p>
212 <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . '
213 <select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >';
214 foreach ( $this->shelves as $_shelf_value => $_shelf_display ) {
215 echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'], false ) . '>' . $_shelf_display . "</option>\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
216 }
217 echo '</select>
218 </label></p>
219 ';
220 }
221 }
222