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