| @@ -1,14 +1,25 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | +/** | |
| 3 | + * Flickr Widget, pulling recent photos from Flickr using RSS feed. | |
| 4 | + * | |
| 5 | + * This widget is now deprecated. | |
| 6 | + * Existing widgets will continue to work, but Flickr no longer displays RSS feeds, | |
| 7 | + * making it impossible for site owners to configure this widget. | |
| 8 | + * We consequently only register the widget if it's already in use on the site. | |
| 9 | + * | |
| 10 | + * @see https://github.com/Automattic/jetpack/issues/39824 | |
| 11 | + * | |
| 12 | + * @package automattic/jetpack | |
| 13 | + */ | |
| 2 | 14 | |
| 3 | 15 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. |
| 4 | 16 | |
| 5 | - | |
| 6 | 17 | /** |
| 7 | 18 | * Disable direct access/execution to/of the widget code. |
| 8 | 19 | */ |
| 9 | 20 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | - exit; | |
| 21 | + exit( 0 ); | |
| 11 | 22 | } |
| 12 | 23 | |
| 13 | 24 | if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { |
| 14 | 25 | /** |
| @@ -30,12 +41,8 @@ | ||
| 30 | 41 | 'customize_selective_refresh' => true, |
| 31 | 42 | ), |
| 32 | 43 | array() |
| 33 | 44 | ); |
| 34 | - | |
| 35 | - if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { | |
| 36 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); | |
| 37 | - } | |
| 38 | 45 | } |
| 39 | 46 | |
| 40 | 47 | /** |
| 41 | 48 | * Enqueue style. |
| @@ -63,8 +70,12 @@ | ||
| 63 | 70 | |
| 64 | 71 | /** |
| 65 | 72 | * Front-end display of the widget. |
| 66 | 73 | * |
| 74 | + * @html-template-var array $instance | |
| 75 | + * @html-template-var string|null $flickr_home | |
| 76 | + * @html-template-var string $photos'; | |
| 77 | + * | |
| 67 | 78 | * @param array $args Widget arguments. |
| 68 | 79 | * @param array $instance Saved values from database. |
| 69 | 80 | */ |
| 70 | 81 | public function widget( $args, $instance ) { |
| @@ -69,8 +80,11 @@ | ||
| 69 | 80 | */ |
| 70 | 81 | public function widget( $args, $instance ) { |
| 71 | 82 | $instance = wp_parse_args( $instance, $this->defaults() ); |
| 72 | 83 | |
| 84 | + // Enqueue front end assets. | |
| 85 | + $this->enqueue_style(); | |
| 86 | + | |
| 73 | 87 | if ( ! empty( $instance['flickr_rss_url'] ) ) { |
| 74 | 88 | /* |
| 75 | 89 | * Parse the URL, and rebuild a URL that's sure to display images. |
| 76 | 90 | * Some Flickr Feeds do not display images by default. |
| @@ -114,9 +128,10 @@ | ||
| 114 | 128 | } |
| 115 | 129 | |
| 116 | 130 | $rss = fetch_feed( $rss_url ); |
| 117 | 131 | |
| 118 | - $photos = ''; | |
| 132 | + $photos = ''; | |
| 133 | + $flickr_home = null; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in flickr/widget.php template file. | |
| 119 | 134 | if ( ! is_wp_error( $rss ) ) { |
| 120 | 135 | foreach ( $rss->get_items( 0, $instance['items'] ) as $photo ) { |
| 121 | 136 | switch ( $instance['flickr_image_size'] ) { |
| 122 | 137 | case 'thumbnail': |
| @@ -128,8 +143,11 @@ | ||
| 128 | 143 | break; |
| 129 | 144 | case 'large': |
| 130 | 145 | $src = $photo->get_enclosure()->get_link(); |
| 131 | 146 | break; |
| 147 | + default: | |
| 148 | + $src = ''; | |
| 149 | + break; | |
| 132 | 150 | } |
| 133 | 151 | |
| 134 | 152 | $photos .= '<a href="' . esc_url( $photo->get_permalink(), array( 'http', 'https' ) ) . '" '; |
| 135 | 153 | if ( $instance['target'] ) { |
| @@ -167,9 +185,12 @@ | ||
| 167 | 185 | |
| 168 | 186 | /** |
| 169 | 187 | * Back-end widget form. |
| 170 | 188 | * |
| 189 | + * @html-template-var array $instance | |
| 190 | + * | |
| 171 | 191 | * @param array $instance Previously saved values from database. |
| 192 | + * @return string|void | |
| 172 | 193 | */ |
| 173 | 194 | public function form( $instance ) { |
| 174 | 195 | $instance = wp_parse_args( $instance, $this->defaults() ); |
| 175 | 196 | require __DIR__ . '/flickr/form.php'; |
| @@ -221,8 +242,20 @@ | ||
| 221 | 242 | /** |
| 222 | 243 | * Register Jetpack_Flickr_Widget widget. |
| 223 | 244 | */ |
| 224 | 245 | function jetpack_register_flickr_widget() { |
| 225 | - register_widget( 'Jetpack_Flickr_Widget' ); | |
| 246 | + $transient = 'jetpack_flickr_widget::is_active'; | |
| 247 | + $has_widget = get_transient( $transient ); | |
| 248 | + | |
| 249 | + if ( false === $has_widget ) { | |
| 250 | + $is_active_widget = is_active_widget( false, false, 'flickr', false ); | |
| 251 | + $has_widget = (int) ! empty( $is_active_widget ); | |
| 252 | + set_transient( $transient, $has_widget, 1 * HOUR_IN_SECONDS ); | |
| 253 | + } | |
| 254 | + | |
| 255 | + // [DEPRECATION]: Only register widget if active widget exists already | |
| 256 | + if ( $has_widget ) { | |
| 257 | + register_widget( 'Jetpack_Flickr_Widget' ); | |
| 258 | + } | |
| 226 | 259 | } |
| 227 | 260 | add_action( 'widgets_init', 'jetpack_register_flickr_widget' ); |
| 228 | 261 | } |