goodreads.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if ( is_rtl() ) {
  40. wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/rtl/goodreads-rtl.css', __FILE__ ) );
  41. } else {
  42. wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/goodreads.css', __FILE__ ) );
  43. }
  44. }
  45. function widget( $args, $instance ) {
  46. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  47. $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
  48. if ( empty( $instance['user_id'] ) || 'invalid' === $instance['user_id'] ) {
  49. if ( current_user_can('edit_theme_options') ) {
  50. echo $args['before_widget'];
  51. echo '<p>' . sprintf(
  52. __( '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' ),
  53. esc_url( admin_url( 'widgets.php' ) ),
  54. 'http://support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id'
  55. ) . '</p>';
  56. echo $args['after_widget'];
  57. }
  58. return;
  59. }
  60. if ( !array_key_exists( $instance['shelf'], $this->shelves ) )
  61. return;
  62. $instance['user_id'] = absint( $instance['user_id'] );
  63. // Set widget ID based on shelf.
  64. $this->goodreads_widget_id = $instance['user_id'] . '_' . $instance['shelf'];
  65. if ( empty( $title ) ) $title = esc_html__( 'Goodreads', 'jetpack' );
  66. echo $args['before_widget'];
  67. echo $args['before_title'] . $title . $args['after_title'];
  68. $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 ) ;
  69. echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ). '"></div>' . "\n";
  70. echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n";
  71. echo $args['after_widget'];
  72. /** This action is already documented in modules/widgets/gravatar-profile.php */
  73. do_action( 'jetpack_stats_extra', 'widget', 'goodreads' );
  74. }
  75. function goodreads_user_id_exists( $user_id ) {
  76. $url = "http://www.goodreads.com/user/show/$user_id/";
  77. $response = wp_remote_head( $url, array( 'httpversion'=>'1.1', 'timeout'=>3, 'redirection'=> 2 ) );
  78. if ( wp_remote_retrieve_response_code( $response ) === 200 )
  79. return true;
  80. else
  81. return false;
  82. }
  83. function update( $new_instance, $old_instance ) {
  84. $instance = $old_instance;
  85. $instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) );
  86. if ( ! empty( $instance['user_id'] ) && ( ! isset( $old_instance['user_id'] ) || $instance['user_id'] !== $old_instance['user_id'] ) ) {
  87. if ( ! $this->goodreads_user_id_exists( $instance['user_id'] ) ) {
  88. $instance['user_id'] = 'invalid';
  89. }
  90. }
  91. $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
  92. $shelf = wp_kses( stripslashes( $new_instance['shelf'] ), array() );
  93. if ( array_key_exists( $shelf, $this->shelves ) )
  94. $instance['shelf'] = $shelf;
  95. return $instance;
  96. }
  97. function form( $instance ) {
  98. //Defaults
  99. $instance = wp_parse_args( (array) $instance, array( 'user_id' => '', 'title' => 'Goodreads', 'shelf' => 'read' ) );
  100. echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
  101. <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'] ) . '" />
  102. </label></p>
  103. <p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">';
  104. printf( __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), 'https://en.support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' );
  105. if ( 'invalid' === $instance['user_id'] ) {
  106. printf( '<br /><small class="error">%s</small>&nbsp;', __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) );
  107. $instance['user_id'] = '';
  108. }
  109. 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'] ) . '" />
  110. </label></p>
  111. <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . '
  112. <select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >';
  113. foreach( $this->shelves as $_shelf_value => $_shelf_display ) {
  114. echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'] ) . ">" . $_shelf_display . "</option>\n";
  115. }
  116. echo '</select>
  117. </label></p>
  118. ';
  119. }
  120. }