WPCOM_Widget_Goodreads

Goodreads widget class Display a user's Goodreads shelf.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/goodreads.php  
  1. class WPCOM_Widget_Goodreads extends WP_Widget { 
  2.  
  3. private $goodreads_widget_id = 0; 
  4.  
  5. function __construct() { 
  6. parent::__construct( 
  7. 'wpcom-goodreads',  
  8. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  9. apply_filters( 'jetpack_widget_name', __( 'Goodreads', 'jetpack' ) ),  
  10. array( 
  11. 'classname' => 'widget_goodreads',  
  12. 'description' => __( 'Display your books from Goodreads', 'jetpack' ) 
  13. ); 
  14. // For user input sanitization and display 
  15. $this->shelves = array( 
  16. 'read' => _x( 'Read', 'past participle: books I have read', 'jetpack' ),  
  17. 'currently-reading' => __( 'Currently Reading', 'jetpack' ),  
  18. 'to-read' => _x( 'To Read', 'my list of books to read', 'jetpack' ) 
  19. ); 
  20.  
  21. if ( is_active_widget( '', '', 'wpcom-goodreads' ) ) { 
  22. add_action( 'wp_print_styles', array( $this, 'enqueue_style' ) ); 
  23.  
  24. function enqueue_style() { 
  25. if ( is_rtl() ) { 
  26. wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/rtl/goodreads-rtl.css', __FILE__ ) ); 
  27. } else { 
  28. wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/goodreads.css', __FILE__ ) ); 
  29.  
  30. function widget( $args, $instance ) { 
  31. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  32. $title = apply_filters( 'widget_title', $instance['title'] ); 
  33.  
  34. if ( empty( $instance['user_id'] ) || 'invalid' === $instance['user_id'] ) { 
  35. if ( current_user_can('edit_theme_options') ) { 
  36. echo $args['before_widget']; 
  37. echo '<p>' . sprintf( 
  38. __( 'You need to enter your numeric user ID for the <a href="%1$s">Goodreads Widget</a> to work correctly. <a href="%2$s">Full instructions</a>.', 'jetpack' ),  
  39. esc_url( admin_url( 'widgets.php' ) ),  
  40. 'http://support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' 
  41. ) . '</p>'; 
  42. echo $args['after_widget']; 
  43. return; 
  44.  
  45. if ( !array_key_exists( $instance['shelf'], $this->shelves ) ) 
  46. return; 
  47.  
  48. $instance['user_id'] = absint( $instance['user_id'] ); 
  49.  
  50. // Set widget ID based on shelf. 
  51. $this->goodreads_widget_id = $instance['user_id'] . '_' . $instance['shelf']; 
  52.  
  53. if ( empty( $title ) ) $title = esc_html__( 'Goodreads', 'jetpack' ); 
  54.  
  55. echo $args['before_widget']; 
  56. echo $args['before_title'] . $title . $args['after_title']; 
  57.  
  58. $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 ) ; 
  59.  
  60. echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ). '"></div>' . "\n"; 
  61. echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n"; 
  62.  
  63. echo $args['after_widget']; 
  64.  
  65. /** This action is already documented in modules/widgets/gravatar-profile.php */ 
  66. do_action( 'jetpack_stats_extra', 'widget', 'goodreads' ); 
  67.  
  68. function goodreads_user_id_exists( $user_id ) { 
  69. $url = "http://www.goodreads.com/user/show/$user_id/"; 
  70. $response = wp_remote_head( $url, array( 'httpversion'=>'1.1', 'timeout'=>3, 'redirection'=> 2 ) ); 
  71. if ( wp_remote_retrieve_response_code( $response ) === 200 ) 
  72. return true; 
  73. else 
  74. return false; 
  75.  
  76. function update( $new_instance, $old_instance ) { 
  77. $instance = $old_instance; 
  78.  
  79. $instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) ); 
  80. if ( ! empty( $instance['user_id'] ) && ( ! isset( $old_instance['user_id'] ) || $instance['user_id'] !== $old_instance['user_id'] ) ) { 
  81. if ( ! $this->goodreads_user_id_exists( $instance['user_id'] ) ) { 
  82. $instance['user_id'] = 'invalid'; 
  83. $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() ); 
  84. $shelf = wp_kses( stripslashes( $new_instance['shelf'] ), array() ); 
  85. if ( array_key_exists( $shelf, $this->shelves ) ) 
  86. $instance['shelf'] = $shelf; 
  87.  
  88. return $instance; 
  89.  
  90. function form( $instance ) { 
  91. //Defaults 
  92. $instance = wp_parse_args( (array) $instance, array( 'user_id' => '', 'title' => 'Goodreads', 'shelf' => 'read' ) ); 
  93.  
  94. echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . ' 
  95. <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'] ) . '" /> 
  96. </label></p> 
  97. <p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">'; 
  98. printf( __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), 'https://en.support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' ); 
  99. if ( 'invalid' === $instance['user_id'] ) { 
  100. printf( '<br /><small class="error">%s</small> ', __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) ); 
  101. $instance['user_id'] = ''; 
  102. 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'] ) . '" /> 
  103. </label></p> 
  104. <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . ' 
  105. <select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >'; 
  106. foreach( $this->shelves as $_shelf_value => $_shelf_display ) { 
  107. echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'] ) . ">" . $_shelf_display . "</option>\n"; 
  108. echo '</select> 
  109. </label></p> 
  110. ';