Nenhuma Descrição

goodreads.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. }
  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 ) ) {
  66. $title = esc_html__( 'Goodreads', 'jetpack' );
  67. }
  68. echo $args['before_widget'];
  69. echo $args['before_title'] . $title . $args['after_title'];
  70. $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 );
  71. echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ) . '"></div>' . "\n";
  72. echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n";
  73. echo $args['after_widget'];
  74. }
  75. function goodreads_user_id_exists( $user_id ) {
  76. $url = "https://www.goodreads.com/user/show/$user_id/";
  77. $response = wp_remote_head(
  78. $url, array(
  79. 'httpversion' => '1.1',
  80. 'timeout' => 10,
  81. 'redirection' => 2,
  82. )
  83. );
  84. if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. function update( $new_instance, $old_instance ) {
  91. $instance = $old_instance;
  92. $instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) );
  93. if ( ! empty( $instance['user_id'] ) && ( ! isset( $old_instance['user_id'] ) || $instance['user_id'] !== $old_instance['user_id'] ) ) {
  94. if ( ! $this->goodreads_user_id_exists( $instance['user_id'] ) ) {
  95. $instance['user_id'] = 'invalid';
  96. }
  97. }
  98. $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
  99. $shelf = wp_kses( stripslashes( $new_instance['shelf'] ), array() );
  100. if ( array_key_exists( $shelf, $this->shelves ) ) {
  101. $instance['shelf'] = $shelf;
  102. }
  103. return $instance;
  104. }
  105. function form( $instance ) {
  106. //Defaults
  107. $instance = wp_parse_args(
  108. (array) $instance, array(
  109. 'user_id' => '',
  110. 'title' => 'Goodreads',
  111. 'shelf' => 'read',
  112. )
  113. );
  114. echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'jetpack' ) . '
  115. <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'] ) . '" />
  116. </label></p>
  117. <p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">';
  118. printf( __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), 'https://en.support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' );
  119. if ( 'invalid' === $instance['user_id'] ) {
  120. printf( '<br /><small class="error">%s</small>&nbsp;', __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) );
  121. $instance['user_id'] = '';
  122. }
  123. 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'] ) . '" />
  124. </label></p>
  125. <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . '
  126. <select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >';
  127. foreach ( $this->shelves as $_shelf_value => $_shelf_display ) {
  128. echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'] ) . '>' . $_shelf_display . "</option>\n";
  129. }
  130. echo '</select>
  131. </label></p>
  132. ';
  133. }
  134. }