diff --git src/wp-includes/widgets/class-wp-widget-custom-html.php src/wp-includes/widgets/class-wp-widget-custom-html.php
index 855c3c5895..87e25244df 100644
|
|
|
class WP_Widget_Custom_HTML extends WP_Widget { |
| 80 | 80 | add_action( 'admin_head-widgets.php', array( 'WP_Widget_Custom_HTML', 'add_help_text' ) ); |
| 81 | 81 | } |
| 82 | 82 | |
| | 83 | /** |
| | 84 | * Filter gallery shortcode attributes. |
| | 85 | * |
| | 86 | * Prevents all of a site's attachments from being shown in a gallery displayed on a |
| | 87 | * non-singular template where a $post context is not available. |
| | 88 | * |
| | 89 | * @since 4.9.0 |
| | 90 | * |
| | 91 | * @param array $attrs Attributes. |
| | 92 | * @return array Attributes. |
| | 93 | */ |
| | 94 | public function _filter_gallery_shortcode_attrs( $attrs ) { |
| | 95 | if ( ! is_singular() && empty( $attrs['id'] ) && empty( $attrs['include'] ) ) { |
| | 96 | $attrs['id'] = -1; |
| | 97 | } |
| | 98 | return $attrs; |
| | 99 | } |
| | 100 | |
| 83 | 101 | /** |
| 84 | 102 | * Outputs the content for the current Custom HTML widget instance. |
| 85 | 103 | * |
| 86 | 104 | * @since 4.8.1 |
| 87 | 105 | * |
| | 106 | * @global WP_Post $post |
| 88 | 107 | * @param array $args Display arguments including 'before_title', 'after_title', |
| 89 | 108 | * 'before_widget', and 'after_widget'. |
| 90 | 109 | * @param array $instance Settings for the current Custom HTML widget instance. |
| 91 | 110 | */ |
| 92 | 111 | public function widget( $args, $instance ) { |
| | 112 | global $post; |
| | 113 | |
| | 114 | // Override global $post so filters (and shortcodes) apply in a consistent context. |
| | 115 | $original_post = $post; |
| | 116 | if ( is_singular() ) { |
| | 117 | // Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post). |
| | 118 | $post = get_queried_object(); |
| | 119 | } else { |
| | 120 | // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries. |
| | 121 | $post = null; |
| | 122 | } |
| | 123 | |
| | 124 | // Prevent dumping out all attachments from the media library. |
| | 125 | add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ), 1000 ); |
| 93 | 126 | |
| 94 | 127 | $instance = array_merge( $this->default_instance, $instance ); |
| 95 | 128 | |
| … |
… |
class WP_Widget_Custom_HTML extends WP_Widget { |
| 118 | 151 | */ |
| 119 | 152 | $content = apply_filters( 'widget_custom_html_content', $content, $instance, $this ); |
| 120 | 153 | |
| | 154 | // Restore post global. |
| | 155 | $post = $original_post; |
| | 156 | remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ), 1000 ); |
| | 157 | |
| 121 | 158 | // Inject the Text widget's container class name alongside this widget's class name for theme styling compatibility. |
| 122 | 159 | $args['before_widget'] = preg_replace( '/(?<=\sclass=["\'])/', 'widget_text ', $args['before_widget'] ); |
| 123 | 160 | |
diff --git src/wp-includes/widgets/class-wp-widget-text.php src/wp-includes/widgets/class-wp-widget-text.php
index ffcfe0cd09..aa02d34b2e 100644
|
|
|
class WP_Widget_Text extends WP_Widget { |
| 178 | 178 | return false; |
| 179 | 179 | } |
| 180 | 180 | |
| | 181 | /** |
| | 182 | * Filter gallery shortcode attributes. |
| | 183 | * |
| | 184 | * Prevents all of a site's attachments from being shown in a gallery displayed on a |
| | 185 | * non-singular template where a $post context is not available. |
| | 186 | * |
| | 187 | * @since 4.9.0 |
| | 188 | * |
| | 189 | * @param array $attrs Attributes. |
| | 190 | * @return array Attributes. |
| | 191 | */ |
| | 192 | public function _filter_gallery_shortcode_attrs( $attrs ) { |
| | 193 | if ( ! is_singular() && empty( $attrs['id'] ) && empty( $attrs['include'] ) ) { |
| | 194 | $attrs['id'] = -1; |
| | 195 | } |
| | 196 | return $attrs; |
| | 197 | } |
| | 198 | |
| 181 | 199 | /** |
| 182 | 200 | * Outputs the content for the current Text widget instance. |
| 183 | 201 | * |
| … |
… |
class WP_Widget_Text extends WP_Widget { |
| 221 | 239 | remove_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority ); |
| 222 | 240 | } |
| 223 | 241 | |
| 224 | | // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context. |
| 225 | | $suspended_post = null; |
| 226 | | if ( isset( $post ) ) { |
| 227 | | $suspended_post = $post; |
| | 242 | // Override global $post so filters (and shortcodes) apply in a consistent context. |
| | 243 | $original_post = $post; |
| | 244 | if ( is_singular() ) { |
| | 245 | // Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post). |
| | 246 | $post = get_queried_object(); |
| | 247 | } else { |
| | 248 | // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries. |
| 228 | 249 | $post = null; |
| 229 | 250 | } |
| 230 | 251 | |
| | 252 | // Prevent dumping out all attachments from the media library. |
| | 253 | add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ), 1000 ); |
| | 254 | |
| 231 | 255 | /** |
| 232 | 256 | * Filters the content of the Text widget. |
| 233 | 257 | * |
| … |
… |
class WP_Widget_Text extends WP_Widget { |
| 278 | 302 | } |
| 279 | 303 | |
| 280 | 304 | // Restore post global. |
| 281 | | if ( isset( $suspended_post ) ) { |
| 282 | | $post = $suspended_post; |
| 283 | | } |
| | 305 | $post = $original_post; |
| | 306 | remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ), 1000 ); |
| 284 | 307 | |
| 285 | 308 | // Undo suspension of legacy plugin-supplied shortcode handling. |
| 286 | 309 | if ( $should_suspend_legacy_shortcode_support ) { |