| @@ -1,6 +1,10 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + exit( 0 ); | |
| 5 | +} | |
| 6 | + | |
| 3 | 7 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. |
| 4 | 8 | |
| 5 | 9 | add_action( 'widgets_init', 'jetpack_goodreads_widget_init' ); |
| 6 | 10 | /** |
| @@ -34,8 +38,9 @@ | ||
| 34 | 38 | array( |
| 35 | 39 | 'classname' => 'widget_goodreads', |
| 36 | 40 | 'description' => __( 'Display your books from Goodreads', 'jetpack' ), |
| 37 | 41 | 'customize_selective_refresh' => true, |
| 42 | + 'show_instance_in_rest' => true, | |
| 38 | 43 | ) |
| 39 | 44 | ); |
| 40 | 45 | // For user input sanitization and display. |
| 41 | 46 | $this->shelves = array( |
| @@ -43,14 +48,23 @@ | ||
| 43 | 48 | 'currently-reading' => __( 'Currently Reading', 'jetpack' ), |
| 44 | 49 | 'to-read' => _x( 'To Read', 'my list of books to read', 'jetpack' ), |
| 45 | 50 | ); |
| 46 | 51 | |
| 47 | - if ( is_active_widget( '', '', 'wpcom-goodreads' ) || is_customize_preview() ) { | |
| 48 | - add_action( 'wp_print_styles', array( $this, 'enqueue_style' ) ); | |
| 49 | - } | |
| 52 | + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); | |
| 50 | 53 | } |
| 51 | 54 | |
| 52 | 55 | /** |
| 56 | + * Remove the "Goodreads" widget from the Legacy Widget block | |
| 57 | + * | |
| 58 | + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. | |
| 59 | + * @return array $widget_types New list of widgets that will be removed. | |
| 60 | + */ | |
| 61 | + public function hide_widget_in_block_editor( $widget_types ) { | |
| 62 | + $widget_types[] = 'wpcom-goodreads'; | |
| 63 | + return $widget_types; | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 53 | 67 | * Enqueue widget styles. |
| 54 | 68 | */ |
| 55 | 69 | public function enqueue_style() { |
| 56 | 70 | wp_enqueue_style( |
| @@ -72,9 +86,9 @@ | ||
| 72 | 86 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
| 73 | 87 | do_action( 'jetpack_stats_extra', 'widget_view', 'goodreads' ); |
| 74 | 88 | |
| 75 | 89 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 76 | - $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' ); | |
| 90 | + $title = apply_filters( 'widget_title', $instance['title'] ?? '' ); | |
| 77 | 91 | |
| 78 | 92 | if ( empty( $instance['user_id'] ) || 'invalid' === $instance['user_id'] ) { |
| 79 | 93 | if ( current_user_can( 'edit_theme_options' ) ) { |
| 80 | 94 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| @@ -100,8 +114,11 @@ | ||
| 100 | 114 | if ( ! array_key_exists( $instance['shelf'], $this->shelves ) ) { |
| 101 | 115 | return; |
| 102 | 116 | } |
| 103 | 117 | |
| 118 | + // Enqueue front end assets. | |
| 119 | + $this->enqueue_style(); | |
| 120 | + | |
| 104 | 121 | $instance['user_id'] = absint( $instance['user_id'] ); |
| 105 | 122 | |
| 106 | 123 | // Set widget ID based on shelf. |
| 107 | 124 | $this->goodreads_widget_id = $instance['user_id'] . '_' . $instance['shelf']; |
| @@ -113,11 +130,11 @@ | ||
| 113 | 130 | |
| 114 | 131 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 115 | 132 | echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 116 | 133 | |
| 117 | - $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 ); | |
| 134 | + $goodreads_url = 'https://www.goodreads.com/review/custom_widget/' . rawurlencode( (string) $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 ); | |
| 118 | 135 | |
| 119 | - echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ) . '"></div>' . "\n"; | |
| 136 | + echo '<div class="jetpack-goodreads-legacy-widget gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ) . '"></div>' . "\n"; | |
| 120 | 137 | echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript |
| 121 | 138 | |
| 122 | 139 | echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 123 | 140 | } |
| @@ -173,8 +190,9 @@ | ||
| 173 | 190 | /** |
| 174 | 191 | * Outputs the widget settings form. |
| 175 | 192 | * |
| 176 | 193 | * @param array $instance Current settings. |
| 194 | + * @return string|void | |
| 177 | 195 | */ |
| 178 | 196 | public function form( $instance ) { |
| 179 | 197 | // Defaults. |
| 180 | 198 | $instance = wp_parse_args( |