goodreads.php 6.1 KB

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