summaryrefslogtreecommitdiffstats
path: root/wp-includes/widgets
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--wp-includes/widgets.php2137
-rw-r--r--wp-includes/widgets/class-wp-nav-menu-widget.php206
-rw-r--r--wp-includes/widgets/class-wp-widget-archives.php230
-rw-r--r--wp-includes/widgets/class-wp-widget-block.php232
-rw-r--r--wp-includes/widgets/class-wp-widget-calendar.php105
-rw-r--r--wp-includes/widgets/class-wp-widget-categories.php205
-rw-r--r--wp-includes/widgets/class-wp-widget-custom-html.php343
-rw-r--r--wp-includes/widgets/class-wp-widget-links.php188
-rw-r--r--wp-includes/widgets/class-wp-widget-media-audio.php217
-rw-r--r--wp-includes/widgets/class-wp-widget-media-gallery.php262
-rw-r--r--wp-includes/widgets/class-wp-widget-media-image.php404
-rw-r--r--wp-includes/widgets/class-wp-widget-media-video.php274
-rw-r--r--wp-includes/widgets/class-wp-widget-media.php516
-rw-r--r--wp-includes/widgets/class-wp-widget-meta.php143
-rw-r--r--wp-includes/widgets/class-wp-widget-pages.php185
-rw-r--r--wp-includes/widgets/class-wp-widget-recent-comments.php218
-rw-r--r--wp-includes/widgets/class-wp-widget-recent-posts.php184
-rw-r--r--wp-includes/widgets/class-wp-widget-rss.php185
-rw-r--r--wp-includes/widgets/class-wp-widget-search.php94
-rw-r--r--wp-includes/widgets/class-wp-widget-tag-cloud.php220
-rw-r--r--wp-includes/widgets/class-wp-widget-text.php581
21 files changed, 7129 insertions, 0 deletions
diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php
new file mode 100644
index 0000000..c568cbe
--- /dev/null
+++ b/wp-includes/widgets.php
@@ -0,0 +1,2137 @@
+<?php
+/**
+ * Core Widgets API
+ *
+ * This API is used for creating dynamic sidebar without hardcoding functionality into
+ * themes
+ *
+ * Includes both internal WordPress routines and theme-use routines.
+ *
+ * This functionality was found in a plugin before the WordPress 2.2 release, which
+ * included it in the core from that point on.
+ *
+ * @link https://wordpress.org/documentation/article/manage-wordpress-widgets/
+ * @link https://developer.wordpress.org/themes/functionality/widgets/
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 2.2.0
+ */
+
+//
+// Global Variables.
+//
+
+/** @ignore */
+global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
+
+/**
+ * Stores the sidebars, since many themes can have more than one.
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ * @since 2.2.0
+ */
+$wp_registered_sidebars = array();
+
+/**
+ * Stores the registered widgets.
+ *
+ * @global array $wp_registered_widgets
+ * @since 2.2.0
+ */
+$wp_registered_widgets = array();
+
+/**
+ * Stores the registered widget controls (options).
+ *
+ * @global array $wp_registered_widget_controls
+ * @since 2.2.0
+ */
+$wp_registered_widget_controls = array();
+/**
+ * @global array $wp_registered_widget_updates
+ */
+$wp_registered_widget_updates = array();
+
+/**
+ * Private
+ *
+ * @global array $_wp_sidebars_widgets
+ */
+$_wp_sidebars_widgets = array();
+
+/**
+ * Private
+ *
+ * @global array $_wp_deprecated_widgets_callbacks
+ */
+$GLOBALS['_wp_deprecated_widgets_callbacks'] = array(
+ 'wp_widget_pages',
+ 'wp_widget_pages_control',
+ 'wp_widget_calendar',
+ 'wp_widget_calendar_control',
+ 'wp_widget_archives',
+ 'wp_widget_archives_control',
+ 'wp_widget_links',
+ 'wp_widget_meta',
+ 'wp_widget_meta_control',
+ 'wp_widget_search',
+ 'wp_widget_recent_entries',
+ 'wp_widget_recent_entries_control',
+ 'wp_widget_tag_cloud',
+ 'wp_widget_tag_cloud_control',
+ 'wp_widget_categories',
+ 'wp_widget_categories_control',
+ 'wp_widget_text',
+ 'wp_widget_text_control',
+ 'wp_widget_rss',
+ 'wp_widget_rss_control',
+ 'wp_widget_recent_comments',
+ 'wp_widget_recent_comments_control',
+);
+
+//
+// Template tags & API functions.
+//
+
+/**
+ * Register a widget
+ *
+ * Registers a WP_Widget widget
+ *
+ * @since 2.8.0
+ * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
+ * instead of simply a `WP_Widget` subclass name.
+ *
+ * @see WP_Widget
+ *
+ * @global WP_Widget_Factory $wp_widget_factory
+ *
+ * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
+ */
+function register_widget( $widget ) {
+ global $wp_widget_factory;
+
+ $wp_widget_factory->register( $widget );
+}
+
+/**
+ * Unregisters a widget.
+ *
+ * Unregisters a WP_Widget widget. Useful for un-registering default widgets.
+ * Run within a function hooked to the {@see 'widgets_init'} action.
+ *
+ * @since 2.8.0
+ * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
+ * instead of simply a `WP_Widget` subclass name.
+ *
+ * @see WP_Widget
+ *
+ * @global WP_Widget_Factory $wp_widget_factory
+ *
+ * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
+ */
+function unregister_widget( $widget ) {
+ global $wp_widget_factory;
+
+ $wp_widget_factory->unregister( $widget );
+}
+
+/**
+ * Creates multiple sidebars.
+ *
+ * If you wanted to quickly create multiple sidebars for a theme or internally.
+ * This function will allow you to do so. If you don't pass the 'name' and/or
+ * 'id' in `$args`, then they will be built for you.
+ *
+ * @since 2.2.0
+ *
+ * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
+ *
+ * @global array $wp_registered_sidebars The new sidebars are stored in this array by sidebar ID.
+ *
+ * @param int $number Optional. Number of sidebars to create. Default 1.
+ * @param array|string $args {
+ * Optional. Array or string of arguments for building a sidebar.
+ *
+ * @type string $id The base string of the unique identifier for each sidebar. If provided, and multiple
+ * sidebars are being defined, the ID will have "-2" appended, and so on.
+ * Default 'sidebar-' followed by the number the sidebar creation is currently at.
+ * @type string $name The name or title for the sidebars displayed in the admin dashboard. If registering
+ * more than one sidebar, include '%d' in the string as a placeholder for the uniquely
+ * assigned number for each sidebar.
+ * Default 'Sidebar' for the first sidebar, otherwise 'Sidebar %d'.
+ * }
+ */
+function register_sidebars( $number = 1, $args = array() ) {
+ global $wp_registered_sidebars;
+ $number = (int) $number;
+
+ if ( is_string( $args ) ) {
+ parse_str( $args, $args );
+ }
+
+ for ( $i = 1; $i <= $number; $i++ ) {
+ $_args = $args;
+
+ if ( $number > 1 ) {
+ if ( isset( $args['name'] ) ) {
+ $_args['name'] = sprintf( $args['name'], $i );
+ } else {
+ /* translators: %d: Sidebar number. */
+ $_args['name'] = sprintf( __( 'Sidebar %d' ), $i );
+ }
+ } else {
+ $_args['name'] = isset( $args['name'] ) ? $args['name'] : __( 'Sidebar' );
+ }
+
+ /*
+ * Custom specified ID's are suffixed if they exist already.
+ * Automatically generated sidebar names need to be suffixed regardless starting at -0.
+ */
+ if ( isset( $args['id'] ) ) {
+ $_args['id'] = $args['id'];
+ $n = 2; // Start at -2 for conflicting custom IDs.
+ while ( is_registered_sidebar( $_args['id'] ) ) {
+ $_args['id'] = $args['id'] . '-' . $n++;
+ }
+ } else {
+ $n = count( $wp_registered_sidebars );
+ do {
+ $_args['id'] = 'sidebar-' . ++$n;
+ } while ( is_registered_sidebar( $_args['id'] ) );
+ }
+ register_sidebar( $_args );
+ }
+}
+
+/**
+ * Builds the definition for a single sidebar and returns the ID.
+ *
+ * Accepts either a string or an array and then parses that against a set
+ * of default arguments for the new sidebar. WordPress will automatically
+ * generate a sidebar ID and name based on the current number of registered
+ * sidebars if those arguments are not included.
+ *
+ * When allowing for automatic generation of the name and ID parameters, keep
+ * in mind that the incrementor for your sidebar can change over time depending
+ * on what other plugins and themes are installed.
+ *
+ * If theme support for 'widgets' has not yet been added when this function is
+ * called, it will be automatically enabled through the use of add_theme_support()
+ *
+ * @since 2.2.0
+ * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments.
+ * @since 5.9.0 Added the `show_in_rest` argument.
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ *
+ * @param array|string $args {
+ * Optional. Array or string of arguments for the sidebar being registered.
+ *
+ * @type string $name The name or title of the sidebar displayed in the Widgets
+ * interface. Default 'Sidebar $instance'.
+ * @type string $id The unique identifier by which the sidebar will be called.
+ * Default 'sidebar-$instance'.
+ * @type string $description Description of the sidebar, displayed in the Widgets interface.
+ * Default empty string.
+ * @type string $class Extra CSS class to assign to the sidebar in the Widgets interface.
+ * Default empty.
+ * @type string $before_widget HTML content to prepend to each widget's HTML output when assigned
+ * to this sidebar. Receives the widget's ID attribute as `%1$s`
+ * and class name as `%2$s`. Default is an opening list item element.
+ * @type string $after_widget HTML content to append to each widget's HTML output when assigned
+ * to this sidebar. Default is a closing list item element.
+ * @type string $before_title HTML content to prepend to the sidebar title when displayed.
+ * Default is an opening h2 element.
+ * @type string $after_title HTML content to append to the sidebar title when displayed.
+ * Default is a closing h2 element.
+ * @type string $before_sidebar HTML content to prepend to the sidebar when displayed.
+ * Receives the `$id` argument as `%1$s` and `$class` as `%2$s`.
+ * Outputs after the {@see 'dynamic_sidebar_before'} action.
+ * Default empty string.
+ * @type string $after_sidebar HTML content to append to the sidebar when displayed.
+ * Outputs before the {@see 'dynamic_sidebar_after'} action.
+ * Default empty string.
+ * @type bool $show_in_rest Whether to show this sidebar publicly in the REST API.
+ * Defaults to only showing the sidebar to administrator users.
+ * }
+ * @return string Sidebar ID added to $wp_registered_sidebars global.
+ */
+function register_sidebar( $args = array() ) {
+ global $wp_registered_sidebars;
+
+ $i = count( $wp_registered_sidebars ) + 1;
+
+ $id_is_empty = empty( $args['id'] );
+
+ $defaults = array(
+ /* translators: %d: Sidebar number. */
+ 'name' => sprintf( __( 'Sidebar %d' ), $i ),
+ 'id' => "sidebar-$i",
+ 'description' => '',
+ 'class' => '',
+ 'before_widget' => '<li id="%1$s" class="widget %2$s">',
+ 'after_widget' => "</li>\n",
+ 'before_title' => '<h2 class="widgettitle">',
+ 'after_title' => "</h2>\n",
+ 'before_sidebar' => '',
+ 'after_sidebar' => '',
+ 'show_in_rest' => false,
+ );
+
+ /**
+ * Filters the sidebar default arguments.
+ *
+ * @since 5.3.0
+ *
+ * @see register_sidebar()
+ *
+ * @param array $defaults The default sidebar arguments.
+ */
+ $sidebar = wp_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) );
+
+ if ( $id_is_empty ) {
+ _doing_it_wrong(
+ __FUNCTION__,
+ sprintf(
+ /* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */
+ __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ),
+ '<code>id</code>',
+ $sidebar['name'],
+ $sidebar['id']
+ ),
+ '4.2.0'
+ );
+ }
+
+ $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
+
+ add_theme_support( 'widgets' );
+
+ /**
+ * Fires once a sidebar has been registered.
+ *
+ * @since 3.0.0
+ *
+ * @param array $sidebar Parsed arguments for the registered sidebar.
+ */
+ do_action( 'register_sidebar', $sidebar );
+
+ return $sidebar['id'];
+}
+
+/**
+ * Removes a sidebar from the list.
+ *
+ * @since 2.2.0
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ *
+ * @param string|int $sidebar_id The ID of the sidebar when it was registered.
+ */
+function unregister_sidebar( $sidebar_id ) {
+ global $wp_registered_sidebars;
+
+ unset( $wp_registered_sidebars[ $sidebar_id ] );
+}
+
+/**
+ * Checks if a sidebar is registered.
+ *
+ * @since 4.4.0
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ *
+ * @param string|int $sidebar_id The ID of the sidebar when it was registered.
+ * @return bool True if the sidebar is registered, false otherwise.
+ */
+function is_registered_sidebar( $sidebar_id ) {
+ global $wp_registered_sidebars;
+
+ return isset( $wp_registered_sidebars[ $sidebar_id ] );
+}
+
+/**
+ * Register an instance of a widget.
+ *
+ * The default widget option is 'classname' that can be overridden.
+ *
+ * The function can also be used to un-register widgets when `$output_callback`
+ * parameter is an empty string.
+ *
+ * @since 2.2.0
+ * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
+ * by adding it to the function signature.
+ * @since 5.8.0 Added show_instance_in_rest option.
+ *
+ * @global array $wp_registered_widgets Uses stored registered widgets.
+ * @global array $wp_registered_widget_controls Stores the registered widget controls (options).
+ * @global array $wp_registered_widget_updates
+ * @global array $_wp_deprecated_widgets_callbacks
+ *
+ * @param int|string $id Widget ID.
+ * @param string $name Widget display title.
+ * @param callable $output_callback Run when widget is called.
+ * @param array $options {
+ * Optional. An array of supplementary widget options for the instance.
+ *
+ * @type string $classname Class name for the widget's HTML container. Default is a shortened
+ * version of the output callback name.
+ * @type string $description Widget description for display in the widget administration
+ * panel and/or theme.
+ * @type bool $show_instance_in_rest Whether to show the widget's instance settings in the REST API.
+ * Only available for WP_Widget based widgets.
+ * }
+ * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
+ */
+function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array(), ...$params ) {
+ global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
+
+ $id = strtolower( $id );
+
+ if ( empty( $output_callback ) ) {
+ unset( $wp_registered_widgets[ $id ] );
+ return;
+ }
+
+ $id_base = _get_widget_id_base( $id );
+ if ( in_array( $output_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $output_callback ) ) {
+ unset( $wp_registered_widget_controls[ $id ] );
+ unset( $wp_registered_widget_updates[ $id_base ] );
+ return;
+ }
+
+ $defaults = array( 'classname' => $output_callback );
+ $options = wp_parse_args( $options, $defaults );
+ $widget = array(
+ 'name' => $name,
+ 'id' => $id,
+ 'callback' => $output_callback,
+ 'params' => $params,
+ );
+ $widget = array_merge( $widget, $options );
+
+ if ( is_callable( $output_callback ) && ( ! isset( $wp_registered_widgets[ $id ] ) || did_action( 'widgets_init' ) ) ) {
+
+ /**
+ * Fires once for each registered widget.
+ *
+ * @since 3.0.0
+ *
+ * @param array $widget An array of default widget arguments.
+ */
+ do_action( 'wp_register_sidebar_widget', $widget );
+ $wp_registered_widgets[ $id ] = $widget;
+ }
+}
+
+/**
+ * Retrieve description for widget.
+ *
+ * When registering widgets, the options can also include 'description' that
+ * describes the widget for display on the widget administration panel or
+ * in the theme.
+ *
+ * @since 2.5.0
+ *
+ * @global array $wp_registered_widgets
+ *
+ * @param int|string $id Widget ID.
+ * @return string|void Widget description, if available.
+ */
+function wp_widget_description( $id ) {
+ if ( ! is_scalar( $id ) ) {
+ return;
+ }
+
+ global $wp_registered_widgets;
+
+ if ( isset( $wp_registered_widgets[ $id ]['description'] ) ) {
+ return esc_html( $wp_registered_widgets[ $id ]['description'] );
+ }
+}
+
+/**
+ * Retrieve description for a sidebar.
+ *
+ * When registering sidebars a 'description' parameter can be included that
+ * describes the sidebar for display on the widget administration panel.
+ *
+ * @since 2.9.0
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ *
+ * @param string $id sidebar ID.
+ * @return string|void Sidebar description, if available.
+ */
+function wp_sidebar_description( $id ) {
+ if ( ! is_scalar( $id ) ) {
+ return;
+ }
+
+ global $wp_registered_sidebars;
+
+ if ( isset( $wp_registered_sidebars[ $id ]['description'] ) ) {
+ return wp_kses( $wp_registered_sidebars[ $id ]['description'], 'sidebar_description' );
+ }
+}
+
+/**
+ * Remove widget from sidebar.
+ *
+ * @since 2.2.0
+ *
+ * @param int|string $id Widget ID.
+ */
+function wp_unregister_sidebar_widget( $id ) {
+
+ /**
+ * Fires just before a widget is removed from a sidebar.
+ *
+ * @since 3.0.0
+ *
+ * @param int|string $id The widget ID.
+ */
+ do_action( 'wp_unregister_sidebar_widget', $id );
+
+ wp_register_sidebar_widget( $id, '', '' );
+ wp_unregister_widget_control( $id );
+}
+
+/**
+ * Registers widget control callback for customizing options.
+ *
+ * @since 2.2.0
+ * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
+ * by adding it to the function signature.
+ *
+ * @global array $wp_registered_widget_controls
+ * @global array $wp_registered_widget_updates
+ * @global array $wp_registered_widgets
+ * @global array $_wp_deprecated_widgets_callbacks
+ *
+ * @param int|string $id Sidebar ID.
+ * @param string $name Sidebar display name.
+ * @param callable $control_callback Run when sidebar is displayed.
+ * @param array $options {
+ * Optional. Array or string of control options. Default empty array.
+ *
+ * @type int $height Never used. Default 200.
+ * @type int $width Width of the fully expanded control form (but try hard to use the default width).
+ * Default 250.
+ * @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the
+ * text widget. The widget ID will end up looking like `{$id_base}-{$unique_number}`.
+ * }
+ * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
+ */
+function wp_register_widget_control( $id, $name, $control_callback, $options = array(), ...$params ) {
+ global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
+
+ $id = strtolower( $id );
+ $id_base = _get_widget_id_base( $id );
+
+ if ( empty( $control_callback ) ) {
+ unset( $wp_registered_widget_controls[ $id ] );
+ unset( $wp_registered_widget_updates[ $id_base ] );
+ return;
+ }
+
+ if ( in_array( $control_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $control_callback ) ) {
+ unset( $wp_registered_widgets[ $id ] );
+ return;
+ }
+
+ if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) {
+ return;
+ }
+
+ $defaults = array(
+ 'width' => 250,
+ 'height' => 200,
+ ); // Height is never used.
+ $options = wp_parse_args( $options, $defaults );
+ $options['width'] = (int) $options['width'];
+ $options['height'] = (int) $options['height'];
+
+ $widget = array(
+ 'name' => $name,
+ 'id' => $id,
+ 'callback' => $control_callback,
+ 'params' => $params,
+ );
+ $widget = array_merge( $widget, $options );
+
+ $wp_registered_widget_controls[ $id ] = $widget;
+
+ if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) {
+ return;
+ }
+
+ if ( isset( $widget['params'][0]['number'] ) ) {
+ $widget['params'][0]['number'] = -1;
+ }
+
+ unset( $widget['width'], $widget['height'], $widget['name'], $widget['id'] );
+ $wp_registered_widget_updates[ $id_base ] = $widget;
+}
+
+/**
+ * Registers the update callback for a widget.
+ *
+ * @since 2.8.0
+ * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
+ * by adding it to the function signature.
+ *
+ * @global array $wp_registered_widget_updates
+ *
+ * @param string $id_base The base ID of a widget created by extending WP_Widget.
+ * @param callable $update_callback Update callback method for the widget.
+ * @param array $options Optional. Widget control options. See wp_register_widget_control().
+ * Default empty array.
+ * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
+ */
+function _register_widget_update_callback( $id_base, $update_callback, $options = array(), ...$params ) {
+ global $wp_registered_widget_updates;
+
+ if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) {
+ if ( empty( $update_callback ) ) {
+ unset( $wp_registered_widget_updates[ $id_base ] );
+ }
+ return;
+ }
+
+ $widget = array(
+ 'callback' => $update_callback,
+ 'params' => $params,
+ );
+
+ $widget = array_merge( $widget, $options );
+ $wp_registered_widget_updates[ $id_base ] = $widget;
+}
+
+/**
+ * Registers the form callback for a widget.
+ *
+ * @since 2.8.0
+ * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
+ * by adding it to the function signature.
+ *
+ * @global array $wp_registered_widget_controls
+ *
+ * @param int|string $id Widget ID.
+ * @param string $name Name attribute for the widget.
+ * @param callable $form_callback Form callback.
+ * @param array $options Optional. Widget control options. See wp_register_widget_control().
+ * Default empty array.
+ * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
+ */
+
+function _register_widget_form_callback( $id, $name, $form_callback, $options = array(), ...$params ) {
+ global $wp_registered_widget_controls;
+
+ $id = strtolower( $id );
+
+ if ( empty( $form_callback ) ) {
+ unset( $wp_registered_widget_controls[ $id ] );
+ return;
+ }
+
+ if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) {
+ return;
+ }
+
+ $defaults = array(
+ 'width' => 250,
+ 'height' => 200,
+ );
+ $options = wp_parse_args( $options, $defaults );
+ $options['width'] = (int) $options['width'];
+ $options['height'] = (int) $options['height'];
+
+ $widget = array(
+ 'name' => $name,
+ 'id' => $id,
+ 'callback' => $form_callback,
+ 'params' => $params,
+ );
+ $widget = array_merge( $widget, $options );
+
+ $wp_registered_widget_controls[ $id ] = $widget;
+}
+
+/**
+ * Remove control callback for widget.
+ *
+ * @since 2.2.0
+ *
+ * @param int|string $id Widget ID.
+ */
+function wp_unregister_widget_control( $id ) {
+ wp_register_widget_control( $id, '', '' );
+}
+
+/**
+ * Display dynamic sidebar.
+ *
+ * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or
+ * 'name' parameter for its registered sidebars you can pass an ID or name as the $index parameter.
+ * Otherwise, you can pass in a numerical index to display the sidebar at that index.
+ *
+ * @since 2.2.0
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ * @global array $wp_registered_widgets Registered widgets.
+ *
+ * @param int|string $index Optional. Index, name or ID of dynamic sidebar. Default 1.
+ * @return bool True, if widget sidebar was found and called. False if not found or not called.
+ */
+function dynamic_sidebar( $index = 1 ) {
+ global $wp_registered_sidebars, $wp_registered_widgets;
+
+ if ( is_int( $index ) ) {
+ $index = "sidebar-$index";
+ } else {
+ $index = sanitize_title( $index );
+ foreach ( (array) $wp_registered_sidebars as $key => $value ) {
+ if ( sanitize_title( $value['name'] ) === $index ) {
+ $index = $key;
+ break;
+ }
+ }
+ }
+
+ $sidebars_widgets = wp_get_sidebars_widgets();
+ if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {
+ /** This action is documented in wp-includes/widget.php */
+ do_action( 'dynamic_sidebar_before', $index, false );
+ /** This action is documented in wp-includes/widget.php */
+ do_action( 'dynamic_sidebar_after', $index, false );
+ /** This filter is documented in wp-includes/widget.php */
+ return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );
+ }
+
+ $sidebar = $wp_registered_sidebars[ $index ];
+
+ $sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] );
+
+ /**
+ * Fires before widgets are rendered in a dynamic sidebar.
+ *
+ * Note: The action also fires for empty sidebars, and on both the front end
+ * and back end, including the Inactive Widgets sidebar on the Widgets screen.
+ *
+ * @since 3.9.0
+ *
+ * @param int|string $index Index, name, or ID of the dynamic sidebar.
+ * @param bool $has_widgets Whether the sidebar is populated with widgets.
+ * Default true.
+ */
+ do_action( 'dynamic_sidebar_before', $index, true );
+
+ if ( ! is_admin() && ! empty( $sidebar['before_sidebar'] ) ) {
+ echo $sidebar['before_sidebar'];
+ }
+
+ $did_one = false;
+ foreach ( (array) $sidebars_widgets[ $index ] as $id ) {
+
+ if ( ! isset( $wp_registered_widgets[ $id ] ) ) {
+ continue;
+ }
+
+ $params = array_merge(
+ array(
+ array_merge(
+ $sidebar,
+ array(
+ 'widget_id' => $id,
+ 'widget_name' => $wp_registered_widgets[ $id ]['name'],
+ )
+ ),
+ ),
+ (array) $wp_registered_widgets[ $id ]['params']
+ );
+
+ // Substitute HTML `id` and `class` attributes into `before_widget`.
+ $classname_ = '';
+ foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) {
+ if ( is_string( $cn ) ) {
+ $classname_ .= '_' . $cn;
+ } elseif ( is_object( $cn ) ) {
+ $classname_ .= '_' . get_class( $cn );
+ }
+ }
+ $classname_ = ltrim( $classname_, '_' );
+
+ $params[0]['before_widget'] = sprintf(
+ $params[0]['before_widget'],
+ str_replace( '\\', '_', $id ),
+ $classname_
+ );
+
+ /**
+ * Filters the parameters passed to a widget's display callback.
+ *
+ * Note: The filter is evaluated on both the front end and back end,
+ * including for the Inactive Widgets sidebar on the Widgets screen.
+ *
+ * @since 2.5.0
+ *
+ * @see register_sidebar()
+ *
+ * @param array $params {
+ * @type array $args {
+ * An array of widget display arguments.
+ *
+ * @type string $name Name of the sidebar the widget is assigned to.
+ * @type string $id ID of the sidebar the widget is assigned to.
+ * @type string $description The sidebar description.
+ * @type string $class CSS class applied to the sidebar container.
+ * @type string $before_widget HTML markup to prepend to each widget in the sidebar.
+ * @type string $after_widget HTML markup to append to each widget in the sidebar.
+ * @type string $before_title HTML markup to prepend to the widget title when displayed.
+ * @type string $after_title HTML markup to append to the widget title when displayed.
+ * @type string $widget_id ID of the widget.
+ * @type string $widget_name Name of the widget.
+ * }
+ * @type array $widget_args {
+ * An array of multi-widget arguments.
+ *
+ * @type int $number Number increment used for multiples of the same widget.
+ * }
+ * }
+ */
+ $params = apply_filters( 'dynamic_sidebar_params', $params );
+
+ $callback = $wp_registered_widgets[ $id ]['callback'];
+
+ /**
+ * Fires before a widget's display callback is called.
+ *
+ * Note: The action fires on both the front end and back end, including
+ * for widgets in the Inactive Widgets sidebar on the Widgets screen.
+ *
+ * The action is not fired for empty sidebars.
+ *
+ * @since 3.0.0
+ *
+ * @param array $widget {
+ * An associative array of widget arguments.
+ *
+ * @type string $name Name of the widget.
+ * @type string $id Widget ID.
+ * @type callable $callback When the hook is fired on the front end, `$callback` is an array
+ * containing the widget object. Fired on the back end, `$callback`
+ * is 'wp_widget_control', see `$_callback`.
+ * @type array $params An associative array of multi-widget arguments.
+ * @type string $classname CSS class applied to the widget container.
+ * @type string $description The widget description.
+ * @type array $_callback When the hook is fired on the back end, `$_callback` is populated
+ * with an array containing the widget object, see `$callback`.
+ * }
+ */
+ do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] );
+
+ if ( is_callable( $callback ) ) {
+ call_user_func_array( $callback, $params );
+ $did_one = true;
+ }
+ }
+
+ if ( ! is_admin() && ! empty( $sidebar['after_sidebar'] ) ) {
+ echo $sidebar['after_sidebar'];
+ }
+
+ /**
+ * Fires after widgets are rendered in a dynamic sidebar.
+ *
+ * Note: The action also fires for empty sidebars, and on both the front end
+ * and back end, including the Inactive Widgets sidebar on the Widgets screen.
+ *
+ * @since 3.9.0
+ *
+ * @param int|string $index Index, name, or ID of the dynamic sidebar.
+ * @param bool $has_widgets Whether the sidebar is populated with widgets.
+ * Default true.
+ */
+ do_action( 'dynamic_sidebar_after', $index, true );
+
+ /**
+ * Filters whether a sidebar has widgets.
+ *
+ * Note: The filter is also evaluated for empty sidebars, and on both the front end
+ * and back end, including the Inactive Widgets sidebar on the Widgets screen.
+ *
+ * @since 3.9.0
+ *
+ * @param bool $did_one Whether at least one widget was rendered in the sidebar.
+ * Default false.
+ * @param int|string $index Index, name, or ID of the dynamic sidebar.
+ */
+ return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index );
+}
+
+/**
+ * Determines whether a given widget is displayed on the front end.
+ *
+ * Either $callback or $id_base can be used
+ * $id_base is the first argument when extending WP_Widget class
+ * Without the optional $widget_id parameter, returns the ID of the first sidebar
+ * in which the first instance of the widget with the given callback or $id_base is found.
+ * With the $widget_id parameter, returns the ID of the sidebar where
+ * the widget with that callback/$id_base AND that ID is found.
+ *
+ * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
+ * this function has to run after widgets have initialized, at action {@see 'init'} or later.
+ *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
+ *
+ * @since 2.2.0
+ *
+ * @global array $wp_registered_widgets
+ *
+ * @param callable|false $callback Optional. Widget callback to check. Default false.
+ * @param string|false $widget_id Optional. Widget ID. Optional, but needed for checking.
+ * Default false.
+ * @param string|false $id_base Optional. The base ID of a widget created by extending WP_Widget.
+ * Default false.
+ * @param bool $skip_inactive Optional. Whether to check in 'wp_inactive_widgets'.
+ * Default true.
+ * @return string|false ID of the sidebar in which the widget is active,
+ * false if the widget is not active.
+ */
+function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) {
+ global $wp_registered_widgets;
+
+ $sidebars_widgets = wp_get_sidebars_widgets();
+
+ if ( is_array( $sidebars_widgets ) ) {
+ foreach ( $sidebars_widgets as $sidebar => $widgets ) {
+ if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || str_starts_with( $sidebar, 'orphaned_widgets' ) ) ) {
+ continue;
+ }
+
+ if ( is_array( $widgets ) ) {
+ foreach ( $widgets as $widget ) {
+ if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] === $callback ) || ( $id_base && _get_widget_id_base( $widget ) === $id_base ) ) {
+ if ( ! $widget_id || $widget_id === $wp_registered_widgets[ $widget ]['id'] ) {
+ return $sidebar;
+ }
+ }
+ }
+ }
+ }
+ }
+ return false;
+}
+
+/**
+ * Determines whether the dynamic sidebar is enabled and used by the theme.
+ *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
+ *
+ * @since 2.2.0
+ *
+ * @global array $wp_registered_widgets Registered widgets.
+ * @global array $wp_registered_sidebars Registered sidebars.
+ *
+ * @return bool True if using widgets, false otherwise.
+ */
+function is_dynamic_sidebar() {
+ global $wp_registered_widgets, $wp_registered_sidebars;
+
+ $sidebars_widgets = get_option( 'sidebars_widgets' );
+
+ foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
+ if ( ! empty( $sidebars_widgets[ $index ] ) ) {
+ foreach ( (array) $sidebars_widgets[ $index ] as $widget ) {
+ if ( array_key_exists( $widget, $wp_registered_widgets ) ) {
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Determines whether a sidebar contains widgets.
+ *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
+ *
+ * @since 2.8.0
+ *
+ * @param string|int $index Sidebar name, id or number to check.
+ * @return bool True if the sidebar has widgets, false otherwise.
+ */
+function is_active_sidebar( $index ) {
+ $index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index );
+ $sidebars_widgets = wp_get_sidebars_widgets();
+ $is_active_sidebar = ! empty( $sidebars_widgets[ $index ] );
+
+ /**
+ * Filters whether a dynamic sidebar is considered "active".
+ *
+ * @since 3.9.0
+ *
+ * @param bool $is_active_sidebar Whether or not the sidebar should be considered "active".
+ * In other words, whether the sidebar contains any widgets.
+ * @param int|string $index Index, name, or ID of the dynamic sidebar.
+ */
+ return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index );
+}
+
+//
+// Internal Functions.
+//
+
+/**
+ * Retrieve full list of sidebars and their widget instance IDs.
+ *
+ * Will upgrade sidebar widget list, if needed. Will also save updated list, if
+ * needed.
+ *
+ * @since 2.2.0
+ * @access private
+ *
+ * @global array $_wp_sidebars_widgets
+ * @global array $sidebars_widgets
+ *
+ * @param bool $deprecated Not used (argument deprecated).
+ * @return array Upgraded list of widgets to version 3 array format when called from the admin.
+ */
+function wp_get_sidebars_widgets( $deprecated = true ) {
+ if ( true !== $deprecated ) {
+ _deprecated_argument( __FUNCTION__, '2.8.1' );
+ }
+
+ global $_wp_sidebars_widgets, $sidebars_widgets;
+
+ /*
+ * If loading from front page, consult $_wp_sidebars_widgets rather than options
+ * to see if wp_convert_widget_settings() has made manipulations in memory.
+ */
+ if ( ! is_admin() ) {
+ if ( empty( $_wp_sidebars_widgets ) ) {
+ $_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() );
+ }
+
+ $sidebars_widgets = $_wp_sidebars_widgets;
+ } else {
+ $sidebars_widgets = get_option( 'sidebars_widgets', array() );
+ }
+
+ if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) {
+ unset( $sidebars_widgets['array_version'] );
+ }
+
+ /**
+ * Filters the list of sidebars and their widgets.
+ *
+ * @since 2.7.0
+ *
+ * @param array $sidebars_widgets An associative array of sidebars and their widgets.
+ */
+ return apply_filters( 'sidebars_widgets', $sidebars_widgets );
+}
+
+/**
+ * Retrieves the registered sidebar with the given ID.
+ *
+ * @since 5.9.0
+ *
+ * @global array $wp_registered_sidebars The registered sidebars.
+ *
+ * @param string $id The sidebar ID.
+ * @return array|null The discovered sidebar, or null if it is not registered.
+ */
+function wp_get_sidebar( $id ) {
+ global $wp_registered_sidebars;
+
+ foreach ( (array) $wp_registered_sidebars as $sidebar ) {
+ if ( $sidebar['id'] === $id ) {
+ return $sidebar;
+ }
+ }
+
+ if ( 'wp_inactive_widgets' === $id ) {
+ return array(
+ 'id' => 'wp_inactive_widgets',
+ 'name' => __( 'Inactive widgets' ),
+ );
+ }
+
+ return null;
+}
+
+/**
+ * Set the sidebar widget option to update sidebars.
+ *
+ * @since 2.2.0
+ * @access private
+ *
+ * @global array $_wp_sidebars_widgets
+ * @param array $sidebars_widgets Sidebar widgets and their settings.
+ */
+function wp_set_sidebars_widgets( $sidebars_widgets ) {
+ global $_wp_sidebars_widgets;
+
+ // Clear cached value used in wp_get_sidebars_widgets().
+ $_wp_sidebars_widgets = null;
+
+ if ( ! isset( $sidebars_widgets['array_version'] ) ) {
+ $sidebars_widgets['array_version'] = 3;
+ }
+
+ update_option( 'sidebars_widgets', $sidebars_widgets );
+}
+
+/**
+ * Retrieve default registered sidebars list.
+ *
+ * @since 2.2.0
+ * @access private
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ *
+ * @return array
+ */
+function wp_get_widget_defaults() {
+ global $wp_registered_sidebars;
+
+ $defaults = array();
+
+ foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
+ $defaults[ $index ] = array();
+ }
+
+ return $defaults;
+}
+
+/**
+ * Converts the widget settings from single to multi-widget format.
+ *
+ * @since 2.8.0
+ *
+ * @global array $_wp_sidebars_widgets
+ *
+ * @param string $base_name Root ID for all widgets of this type.
+ * @param string $option_name Option name for this widget type.
+ * @param array $settings The array of widget instance settings.
+ * @return array The array of widget settings converted to multi-widget format.
+ */
+function wp_convert_widget_settings( $base_name, $option_name, $settings ) {
+ // This test may need expanding.
+ $single = false;
+ $changed = false;
+
+ if ( empty( $settings ) ) {
+ $single = true;
+ } else {
+ foreach ( array_keys( $settings ) as $number ) {
+ if ( 'number' === $number ) {
+ continue;
+ }
+ if ( ! is_numeric( $number ) ) {
+ $single = true;
+ break;
+ }
+ }
+ }
+
+ if ( $single ) {
+ $settings = array( 2 => $settings );
+
+ // If loading from the front page, update sidebar in memory but don't save to options.
+ if ( is_admin() ) {
+ $sidebars_widgets = get_option( 'sidebars_widgets' );
+ } else {
+ if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) {
+ $GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() );
+ }
+ $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
+ }
+
+ foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
+ if ( is_array( $sidebar ) ) {
+ foreach ( $sidebar as $i => $name ) {
+ if ( $base_name === $name ) {
+ $sidebars_widgets[ $index ][ $i ] = "$name-2";
+ $changed = true;
+ break 2;
+ }
+ }
+ }
+ }
+
+ if ( is_admin() && $changed ) {
+ update_option( 'sidebars_widgets', $sidebars_widgets );
+ }
+ }
+
+ $settings['_multiwidget'] = 1;
+ if ( is_admin() ) {
+ update_option( $option_name, $settings );
+ }
+
+ return $settings;
+}
+
+/**
+ * Output an arbitrary widget as a template tag.
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Widget_Factory $wp_widget_factory
+ *
+ * @param string $widget The widget's PHP class name (see class-wp-widget.php).
+ * @param array $instance Optional. The widget's instance settings. Default empty array.
+ * @param array $args {
+ * Optional. Array of arguments to configure the display of the widget.
+ *
+ * @type string $before_widget HTML content that will be prepended to the widget's HTML output.
+ * Default `<div class="widget %s">`, where `%s` is the widget's class name.
+ * @type string $after_widget HTML content that will be appended to the widget's HTML output.
+ * Default `</div>`.
+ * @type string $before_title HTML content that will be prepended to the widget's title when displayed.
+ * Default `<h2 class="widgettitle">`.
+ * @type string $after_title HTML content that will be appended to the widget's title when displayed.
+ * Default `</h2>`.
+ * }
+ */
+function the_widget( $widget, $instance = array(), $args = array() ) {
+ global $wp_widget_factory;
+
+ if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
+ _doing_it_wrong(
+ __FUNCTION__,
+ sprintf(
+ /* translators: %s: register_widget() */
+ __( 'Widgets need to be registered using %s, before they can be displayed.' ),
+ '<code>register_widget()</code>'
+ ),
+ '4.9.0'
+ );
+ return;
+ }
+
+ $widget_obj = $wp_widget_factory->widgets[ $widget ];
+ if ( ! ( $widget_obj instanceof WP_Widget ) ) {
+ return;
+ }
+
+ $default_args = array(
+ 'before_widget' => '<div class="widget %s">',
+ 'after_widget' => '</div>',
+ 'before_title' => '<h2 class="widgettitle">',
+ 'after_title' => '</h2>',
+ );
+ $args = wp_parse_args( $args, $default_args );
+ $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] );
+
+ $instance = wp_parse_args( $instance );
+
+ /** This filter is documented in wp-includes/class-wp-widget.php */
+ $instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args );
+
+ if ( false === $instance ) {
+ return;
+ }
+
+ /**
+ * Fires before rendering the requested widget.
+ *
+ * @since 3.0.0
+ *
+ * @param string $widget The widget's class name.
+ * @param array $instance The current widget instance's settings.
+ * @param array $args An array of the widget's sidebar arguments.
+ */
+ do_action( 'the_widget', $widget, $instance, $args );
+
+ $widget_obj->_set( -1 );
+ $widget_obj->widget( $args, $instance );
+}
+
+/**
+ * Retrieves the widget ID base value.
+ *
+ * @since 2.8.0
+ *
+ * @param string $id Widget ID.
+ * @return string Widget ID base.
+ */
+function _get_widget_id_base( $id ) {
+ return preg_replace( '/-[0-9]+$/', '', $id );
+}
+
+/**
+ * Handle sidebars config after theme change
+ *
+ * @access private
+ * @since 3.3.0
+ *
+ * @global array $sidebars_widgets
+ */
+function _wp_sidebars_changed() {
+ global $sidebars_widgets;
+
+ if ( ! is_array( $sidebars_widgets ) ) {
+ $sidebars_widgets = wp_get_sidebars_widgets();
+ }
+
+ retrieve_widgets( true );
+}
+
+/**
+ * Validates and remaps any "orphaned" widgets to wp_inactive_widgets sidebar,
+ * and saves the widget settings. This has to run at least on each theme change.
+ *
+ * For example, let's say theme A has a "footer" sidebar, and theme B doesn't have one.
+ * After switching from theme A to theme B, all the widgets previously assigned
+ * to the footer would be inaccessible. This function detects this scenario, and
+ * moves all the widgets previously assigned to the footer under wp_inactive_widgets.
+ *
+ * Despite the word "retrieve" in the name, this function actually updates the database
+ * and the global `$sidebars_widgets`. For that reason it should not be run on front end,
+ * unless the `$theme_changed` value is 'customize' (to bypass the database write).
+ *
+ * @since 2.8.0
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ * @global array $sidebars_widgets
+ * @global array $wp_registered_widgets Registered widgets.
+ *
+ * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value
+ * of 'customize' defers updates for the Customizer.
+ * @return array Updated sidebars widgets.
+ */
+function retrieve_widgets( $theme_changed = false ) {
+ global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
+
+ $registered_sidebars_keys = array_keys( $wp_registered_sidebars );
+ $registered_widgets_ids = array_keys( $wp_registered_widgets );
+
+ if ( ! is_array( get_theme_mod( 'sidebars_widgets' ) ) ) {
+ if ( empty( $sidebars_widgets ) ) {
+ return array();
+ }
+
+ unset( $sidebars_widgets['array_version'] );
+
+ $sidebars_widgets_keys = array_keys( $sidebars_widgets );
+ sort( $sidebars_widgets_keys );
+ sort( $registered_sidebars_keys );
+
+ if ( $sidebars_widgets_keys === $registered_sidebars_keys ) {
+ $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
+
+ return $sidebars_widgets;
+ }
+ }
+
+ // Discard invalid, theme-specific widgets from sidebars.
+ $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
+ $sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets );
+
+ // Find hidden/lost multi-widget instances.
+ $shown_widgets = array_merge( ...array_values( $sidebars_widgets ) );
+ $lost_widgets = array_diff( $registered_widgets_ids, $shown_widgets );
+
+ foreach ( $lost_widgets as $key => $widget_id ) {
+ $number = preg_replace( '/.+?-([0-9]+)$/', '$1', $widget_id );
+
+ // Only keep active and default widgets.
+ if ( is_numeric( $number ) && (int) $number < 2 ) {
+ unset( $lost_widgets[ $key ] );
+ }
+ }
+ $sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] );
+
+ if ( 'customize' !== $theme_changed ) {
+ // Update the widgets settings in the database.
+ wp_set_sidebars_widgets( $sidebars_widgets );
+ }
+
+ return $sidebars_widgets;
+}
+
+/**
+ * Compares a list of sidebars with their widgets against an allowed list.
+ *
+ * @since 4.9.0
+ * @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping.
+ *
+ * @param array $existing_sidebars_widgets List of sidebars and their widget instance IDs.
+ * @return array Mapped sidebars widgets.
+ */
+function wp_map_sidebars_widgets( $existing_sidebars_widgets ) {
+ global $wp_registered_sidebars;
+
+ $new_sidebars_widgets = array(
+ 'wp_inactive_widgets' => array(),
+ );
+
+ // Short-circuit if there are no sidebars to map.
+ if ( ! is_array( $existing_sidebars_widgets ) || empty( $existing_sidebars_widgets ) ) {
+ return $new_sidebars_widgets;
+ }
+
+ foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
+ if ( 'wp_inactive_widgets' === $sidebar || str_starts_with( $sidebar, 'orphaned_widgets' ) ) {
+ $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], (array) $widgets );
+ unset( $existing_sidebars_widgets[ $sidebar ] );
+ }
+ }
+
+ // If old and new theme have just one sidebar, map it and we're done.
+ if ( 1 === count( $existing_sidebars_widgets ) && 1 === count( $wp_registered_sidebars ) ) {
+ $new_sidebars_widgets[ key( $wp_registered_sidebars ) ] = array_pop( $existing_sidebars_widgets );
+
+ return $new_sidebars_widgets;
+ }
+
+ // Map locations with the same slug.
+ $existing_sidebars = array_keys( $existing_sidebars_widgets );
+
+ foreach ( $wp_registered_sidebars as $sidebar => $name ) {
+ if ( in_array( $sidebar, $existing_sidebars, true ) ) {
+ $new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ];
+ unset( $existing_sidebars_widgets[ $sidebar ] );
+ } elseif ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) {
+ $new_sidebars_widgets[ $sidebar ] = array();
+ }
+ }
+
+ // If there are more sidebars, try to map them.
+ if ( ! empty( $existing_sidebars_widgets ) ) {
+
+ /*
+ * If old and new theme both have sidebars that contain phrases
+ * from within the same group, make an educated guess and map it.
+ */
+ $common_slug_groups = array(
+ array( 'sidebar', 'primary', 'main', 'right' ),
+ array( 'second', 'left' ),
+ array( 'sidebar-2', 'footer', 'bottom' ),
+ array( 'header', 'top' ),
+ );
+
+ // Go through each group...
+ foreach ( $common_slug_groups as $slug_group ) {
+
+ // ...and see if any of these slugs...
+ foreach ( $slug_group as $slug ) {
+
+ // ...and any of the new sidebars...
+ foreach ( $wp_registered_sidebars as $new_sidebar => $args ) {
+
+ // ...actually match!
+ if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {
+ continue;
+ }
+
+ // Then see if any of the existing sidebars...
+ foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
+
+ // ...and any slug in the same group...
+ foreach ( $slug_group as $slug ) {
+
+ // ... have a match as well.
+ if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {
+ continue;
+ }
+
+ // Make sure this sidebar wasn't mapped and removed previously.
+ if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {
+
+ // We have a match that can be mapped!
+ $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] );
+
+ // Remove the mapped sidebar so it can't be mapped again.
+ unset( $existing_sidebars_widgets[ $sidebar ] );
+
+ // Go back and check the next new sidebar.
+ continue 3;
+ }
+ } // End foreach ( $slug_group as $slug ).
+ } // End foreach ( $existing_sidebars_widgets as $sidebar => $widgets ).
+ } // End foreach ( $wp_registered_sidebars as $new_sidebar => $args ).
+ } // End foreach ( $slug_group as $slug ).
+ } // End foreach ( $common_slug_groups as $slug_group ).
+ }
+
+ // Move any left over widgets to inactive sidebar.
+ foreach ( $existing_sidebars_widgets as $widgets ) {
+ if ( is_array( $widgets ) && ! empty( $widgets ) ) {
+ $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], $widgets );
+ }
+ }
+
+ // Sidebars_widgets settings from when this theme was previously active.
+ $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
+ $old_sidebars_widgets = isset( $old_sidebars_widgets['data'] ) ? $old_sidebars_widgets['data'] : false;
+
+ if ( is_array( $old_sidebars_widgets ) ) {
+
+ // Remove empty sidebars, no need to map those.
+ $old_sidebars_widgets = array_filter( $old_sidebars_widgets );
+
+ // Only check sidebars that are empty or have not been mapped to yet.
+ foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) {
+ if ( array_key_exists( $new_sidebar, $old_sidebars_widgets ) && ! empty( $new_widgets ) ) {
+ unset( $old_sidebars_widgets[ $new_sidebar ] );
+ }
+ }
+
+ // Remove orphaned widgets, we're only interested in previously active sidebars.
+ foreach ( $old_sidebars_widgets as $sidebar => $widgets ) {
+ if ( str_starts_with( $sidebar, 'orphaned_widgets' ) ) {
+ unset( $old_sidebars_widgets[ $sidebar ] );
+ }
+ }
+
+ $old_sidebars_widgets = _wp_remove_unregistered_widgets( $old_sidebars_widgets );
+
+ if ( ! empty( $old_sidebars_widgets ) ) {
+
+ // Go through each remaining sidebar...
+ foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) {
+
+ // ...and check every new sidebar...
+ foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) {
+
+ // ...for every widget we're trying to revive.
+ foreach ( $old_widgets as $key => $widget_id ) {
+ $active_key = array_search( $widget_id, $new_widgets, true );
+
+ // If the widget is used elsewhere...
+ if ( false !== $active_key ) {
+
+ // ...and that elsewhere is inactive widgets...
+ if ( 'wp_inactive_widgets' === $new_sidebar ) {
+
+ // ...remove it from there and keep the active version...
+ unset( $new_sidebars_widgets['wp_inactive_widgets'][ $active_key ] );
+ } else {
+
+ // ...otherwise remove it from the old sidebar and keep it in the new one.
+ unset( $old_sidebars_widgets[ $old_sidebar ][ $key ] );
+ }
+ } // End if ( $active_key ).
+ } // End foreach ( $old_widgets as $key => $widget_id ).
+ } // End foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ).
+ } // End foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ).
+ } // End if ( ! empty( $old_sidebars_widgets ) ).
+
+ // Restore widget settings from when theme was previously active.
+ $new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets );
+ }
+
+ return $new_sidebars_widgets;
+}
+
+/**
+ * Compares a list of sidebars with their widgets against an allowed list.
+ *
+ * @since 4.9.0
+ *
+ * @param array $sidebars_widgets List of sidebars and their widget instance IDs.
+ * @param array $allowed_widget_ids Optional. List of widget IDs to compare against. Default: Registered widgets.
+ * @return array Sidebars with allowed widgets.
+ */
+function _wp_remove_unregistered_widgets( $sidebars_widgets, $allowed_widget_ids = array() ) {
+ if ( empty( $allowed_widget_ids ) ) {
+ $allowed_widget_ids = array_keys( $GLOBALS['wp_registered_widgets'] );
+ }
+
+ foreach ( $sidebars_widgets as $sidebar => $widgets ) {
+ if ( is_array( $widgets ) ) {
+ $sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $allowed_widget_ids );
+ }
+ }
+
+ return $sidebars_widgets;
+}
+
+/**
+ * Display the RSS entries in a list.
+ *
+ * @since 2.5.0
+ *
+ * @param string|array|object $rss RSS url.
+ * @param array $args Widget arguments.
+ */
+function wp_widget_rss_output( $rss, $args = array() ) {
+ if ( is_string( $rss ) ) {
+ $rss = fetch_feed( $rss );
+ } elseif ( is_array( $rss ) && isset( $rss['url'] ) ) {
+ $args = $rss;
+ $rss = fetch_feed( $rss['url'] );
+ } elseif ( ! is_object( $rss ) ) {
+ return;
+ }
+
+ if ( is_wp_error( $rss ) ) {
+ if ( is_admin() || current_user_can( 'manage_options' ) ) {
+ echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</p>';
+ }
+ return;
+ }
+
+ $default_args = array(
+ 'show_author' => 0,
+ 'show_date' => 0,
+ 'show_summary' => 0,
+ 'items' => 0,
+ );
+ $args = wp_parse_args( $args, $default_args );
+
+ $items = (int) $args['items'];
+ if ( $items < 1 || 20 < $items ) {
+ $items = 10;
+ }
+ $show_summary = (int) $args['show_summary'];
+ $show_author = (int) $args['show_author'];
+ $show_date = (int) $args['show_date'];
+
+ if ( ! $rss->get_item_quantity() ) {
+ echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
+ $rss->__destruct();
+ unset( $rss );
+ return;
+ }
+
+ echo '<ul>';
+ foreach ( $rss->get_items( 0, $items ) as $item ) {
+ $link = $item->get_link();
+ while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
+ $link = substr( $link, 1 );
+ }
+ $link = esc_url( strip_tags( $link ) );
+
+ $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
+ if ( empty( $title ) ) {
+ $title = __( 'Untitled' );
+ }
+
+ $desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
+ $desc = esc_attr( wp_trim_words( $desc, 55, ' [&hellip;]' ) );
+
+ $summary = '';
+ if ( $show_summary ) {
+ $summary = $desc;
+
+ // Change existing [...] to [&hellip;].
+ if ( str_ends_with( $summary, '[...]' ) ) {
+ $summary = substr( $summary, 0, -5 ) . '[&hellip;]';
+ }
+
+ $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
+ }
+
+ $date = '';
+ if ( $show_date ) {
+ $date = $item->get_date( 'U' );
+
+ if ( $date ) {
+ $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
+ }
+ }
+
+ $author = '';
+ if ( $show_author ) {
+ $author = $item->get_author();
+ if ( is_object( $author ) ) {
+ $author = $author->get_name();
+ $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
+ }
+ }
+
+ if ( '' === $link ) {
+ echo "<li>$title{$date}{$summary}{$author}</li>";
+ } elseif ( $show_summary ) {
+ echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
+ } else {
+ echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
+ }
+ }
+ echo '</ul>';
+ $rss->__destruct();
+ unset( $rss );
+}
+
+/**
+ * Display RSS widget options form.
+ *
+ * The options for what fields are displayed for the RSS form are all booleans
+ * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
+ * 'show_date'.
+ *
+ * @since 2.5.0
+ *
+ * @param array|string $args Values for input fields.
+ * @param array $inputs Override default display options.
+ */
+function wp_widget_rss_form( $args, $inputs = null ) {
+ $default_inputs = array(
+ 'url' => true,
+ 'title' => true,
+ 'items' => true,
+ 'show_summary' => true,
+ 'show_author' => true,
+ 'show_date' => true,
+ );
+ $inputs = wp_parse_args( $inputs, $default_inputs );
+
+ $args['title'] = isset( $args['title'] ) ? $args['title'] : '';
+ $args['url'] = isset( $args['url'] ) ? $args['url'] : '';
+ $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
+
+ if ( $args['items'] < 1 || 20 < $args['items'] ) {
+ $args['items'] = 10;
+ }
+
+ $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
+ $args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
+ $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];
+
+ if ( ! empty( $args['error'] ) ) {
+ echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $args['error'] ) . '</p>';
+ }
+
+ $esc_number = esc_attr( $args['number'] );
+ if ( $inputs['url'] ) :
+ ?>
+ <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
+ <input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p>
+<?php endif; if ( $inputs['title'] ) : ?>
+ <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
+ <input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p>
+<?php endif; if ( $inputs['items'] ) : ?>
+ <p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
+ <select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
+ <?php
+ for ( $i = 1; $i <= 20; ++$i ) {
+ echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
+ }
+ ?>
+ </select></p>
+<?php endif; if ( $inputs['show_summary'] || $inputs['show_author'] || $inputs['show_date'] ) : ?>
+ <p>
+ <?php if ( $inputs['show_summary'] ) : ?>
+ <input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> />
+ <label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label><br />
+ <?php endif; if ( $inputs['show_author'] ) : ?>
+ <input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> />
+ <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label><br />
+ <?php endif; if ( $inputs['show_date'] ) : ?>
+ <input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/>
+ <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label><br />
+ <?php endif; ?>
+ </p>
+ <?php
+ endif; // End of display options.
+foreach ( array_keys( $default_inputs ) as $input ) :
+ if ( 'hidden' === $inputs[ $input ] ) :
+ $id = str_replace( '_', '-', $input );
+ ?>
+<input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" />
+ <?php
+ endif;
+ endforeach;
+}
+
+/**
+ * Process RSS feed widget data and optionally retrieve feed items.
+ *
+ * The feed widget can not have more than 20 items or it will reset back to the
+ * default, which is 10.
+ *
+ * The resulting array has the feed title, feed url, feed link (from channel),
+ * feed items, error (if any), and whether to show summary, author, and date.
+ * All respectively in the order of the array elements.
+ *
+ * @since 2.5.0
+ *
+ * @param array $widget_rss RSS widget feed data. Expects unescaped data.
+ * @param bool $check_feed Optional. Whether to check feed for errors. Default true.
+ * @return array
+ */
+function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
+ $items = (int) $widget_rss['items'];
+ if ( $items < 1 || 20 < $items ) {
+ $items = 10;
+ }
+ $url = sanitize_url( strip_tags( $widget_rss['url'] ) );
+ $title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
+ $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
+ $show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
+ $show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
+ $error = false;
+ $link = '';
+
+ if ( $check_feed ) {
+ $rss = fetch_feed( $url );
+
+ if ( is_wp_error( $rss ) ) {
+ $error = $rss->get_error_message();
+ } else {
+ $link = esc_url( strip_tags( $rss->get_permalink() ) );
+ while ( stristr( $link, 'http' ) !== $link ) {
+ $link = substr( $link, 1 );
+ }
+
+ $rss->__destruct();
+ unset( $rss );
+ }
+ }
+
+ return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
+}
+
+/**
+ * Registers all of the default WordPress widgets on startup.
+ *
+ * Calls {@see 'widgets_init'} action after all of the WordPress widgets have been registered.
+ *
+ * @since 2.2.0
+ */
+function wp_widgets_init() {
+ if ( ! is_blog_installed() ) {
+ return;
+ }
+
+ register_widget( 'WP_Widget_Pages' );
+
+ register_widget( 'WP_Widget_Calendar' );
+
+ register_widget( 'WP_Widget_Archives' );
+
+ if ( get_option( 'link_manager_enabled' ) ) {
+ register_widget( 'WP_Widget_Links' );
+ }
+
+ register_widget( 'WP_Widget_Media_Audio' );
+
+ register_widget( 'WP_Widget_Media_Image' );
+
+ register_widget( 'WP_Widget_Media_Gallery' );
+
+ register_widget( 'WP_Widget_Media_Video' );
+
+ register_widget( 'WP_Widget_Meta' );
+
+ register_widget( 'WP_Widget_Search' );
+
+ register_widget( 'WP_Widget_Text' );
+
+ register_widget( 'WP_Widget_Categories' );
+
+ register_widget( 'WP_Widget_Recent_Posts' );
+
+ register_widget( 'WP_Widget_Recent_Comments' );
+
+ register_widget( 'WP_Widget_RSS' );
+
+ register_widget( 'WP_Widget_Tag_Cloud' );
+
+ register_widget( 'WP_Nav_Menu_Widget' );
+
+ register_widget( 'WP_Widget_Custom_HTML' );
+
+ register_widget( 'WP_Widget_Block' );
+
+ /**
+ * Fires after all default WordPress widgets have been registered.
+ *
+ * @since 2.2.0
+ */
+ do_action( 'widgets_init' );
+}
+
+/**
+ * Enables the widgets block editor. This is hooked into 'after_setup_theme' so
+ * that the block editor is enabled by default but can be disabled by themes.
+ *
+ * @since 5.8.0
+ *
+ * @access private
+ */
+function wp_setup_widgets_block_editor() {
+ add_theme_support( 'widgets-block-editor' );
+}
+
+/**
+ * Whether or not to use the block editor to manage widgets. Defaults to true
+ * unless a theme has removed support for widgets-block-editor or a plugin has
+ * filtered the return value of this function.
+ *
+ * @since 5.8.0
+ *
+ * @return bool Whether to use the block editor to manage widgets.
+ */
+function wp_use_widgets_block_editor() {
+ /**
+ * Filters whether to use the block editor to manage widgets.
+ *
+ * @since 5.8.0
+ *
+ * @param bool $use_widgets_block_editor Whether to use the block editor to manage widgets.
+ */
+ return apply_filters(
+ 'use_widgets_block_editor',
+ get_theme_support( 'widgets-block-editor' )
+ );
+}
+
+/**
+ * Converts a widget ID into its id_base and number components.
+ *
+ * @since 5.8.0
+ *
+ * @param string $id Widget ID.
+ * @return array Array containing a widget's id_base and number components.
+ */
+function wp_parse_widget_id( $id ) {
+ $parsed = array();
+
+ if ( preg_match( '/^(.+)-(\d+)$/', $id, $matches ) ) {
+ $parsed['id_base'] = $matches[1];
+ $parsed['number'] = (int) $matches[2];
+ } else {
+ // Likely an old single widget.
+ $parsed['id_base'] = $id;
+ }
+
+ return $parsed;
+}
+
+/**
+ * Finds the sidebar that a given widget belongs to.
+ *
+ * @since 5.8.0
+ *
+ * @param string $widget_id The widget ID to look for.
+ * @return string|null The found sidebar's ID, or null if it was not found.
+ */
+function wp_find_widgets_sidebar( $widget_id ) {
+ foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
+ foreach ( $widget_ids as $maybe_widget_id ) {
+ if ( $maybe_widget_id === $widget_id ) {
+ return (string) $sidebar_id;
+ }
+ }
+ }
+
+ return null;
+}
+
+/**
+ * Assigns a widget to the given sidebar.
+ *
+ * @since 5.8.0
+ *
+ * @param string $widget_id The widget ID to assign.
+ * @param string $sidebar_id The sidebar ID to assign to. If empty, the widget won't be added to any sidebar.
+ */
+function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
+ $sidebars = wp_get_sidebars_widgets();
+
+ foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
+ foreach ( $widgets as $i => $maybe_widget_id ) {
+ if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
+ unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
+ // We could technically break 2 here, but continue looping in case the ID is duplicated.
+ continue 2;
+ }
+ }
+ }
+
+ if ( $sidebar_id ) {
+ $sidebars[ $sidebar_id ][] = $widget_id;
+ }
+
+ wp_set_sidebars_widgets( $sidebars );
+}
+
+/**
+ * Calls the render callback of a widget and returns the output.
+ *
+ * @since 5.8.0
+ *
+ * @param string $widget_id Widget ID.
+ * @param string $sidebar_id Sidebar ID.
+ * @return string
+ */
+function wp_render_widget( $widget_id, $sidebar_id ) {
+ global $wp_registered_widgets, $wp_registered_sidebars;
+
+ if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
+ return '';
+ }
+
+ if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
+ $sidebar = $wp_registered_sidebars[ $sidebar_id ];
+ } elseif ( 'wp_inactive_widgets' === $sidebar_id ) {
+ $sidebar = array();
+ } else {
+ return '';
+ }
+
+ $params = array_merge(
+ array(
+ array_merge(
+ $sidebar,
+ array(
+ 'widget_id' => $widget_id,
+ 'widget_name' => $wp_registered_widgets[ $widget_id ]['name'],
+ )
+ ),
+ ),
+ (array) $wp_registered_widgets[ $widget_id ]['params']
+ );
+
+ // Substitute HTML `id` and `class` attributes into `before_widget`.
+ $classname_ = '';
+ foreach ( (array) $wp_registered_widgets[ $widget_id ]['classname'] as $cn ) {
+ if ( is_string( $cn ) ) {
+ $classname_ .= '_' . $cn;
+ } elseif ( is_object( $cn ) ) {
+ $classname_ .= '_' . get_class( $cn );
+ }
+ }
+ $classname_ = ltrim( $classname_, '_' );
+ $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ );
+
+ /** This filter is documented in wp-includes/widgets.php */
+ $params = apply_filters( 'dynamic_sidebar_params', $params );
+
+ $callback = $wp_registered_widgets[ $widget_id ]['callback'];
+
+ ob_start();
+
+ /** This filter is documented in wp-includes/widgets.php */
+ do_action( 'dynamic_sidebar', $wp_registered_widgets[ $widget_id ] );
+
+ if ( is_callable( $callback ) ) {
+ call_user_func_array( $callback, $params );
+ }
+
+ return ob_get_clean();
+}
+
+/**
+ * Calls the control callback of a widget and returns the output.
+ *
+ * @since 5.8.0
+ *
+ * @param string $id Widget ID.
+ * @return string|null
+ */
+function wp_render_widget_control( $id ) {
+ global $wp_registered_widget_controls;
+
+ if ( ! isset( $wp_registered_widget_controls[ $id ]['callback'] ) ) {
+ return null;
+ }
+
+ $callback = $wp_registered_widget_controls[ $id ]['callback'];
+ $params = $wp_registered_widget_controls[ $id ]['params'];
+
+ ob_start();
+
+ if ( is_callable( $callback ) ) {
+ call_user_func_array( $callback, $params );
+ }
+
+ return ob_get_clean();
+}
+
+/**
+ * Displays a _doing_it_wrong() message for conflicting widget editor scripts.
+ *
+ * The 'wp-editor' script module is exposed as window.wp.editor. This overrides
+ * the legacy TinyMCE editor module which is required by the widgets editor.
+ * Because of that conflict, these two shouldn't be enqueued together.
+ * See https://core.trac.wordpress.org/ticket/53569.
+ *
+ * There is also another conflict related to styles where the block widgets
+ * editor is hidden if a block enqueues 'wp-edit-post' stylesheet.
+ * See https://core.trac.wordpress.org/ticket/53569.
+ *
+ * @since 5.8.0
+ * @access private
+ *
+ * @global WP_Scripts $wp_scripts
+ * @global WP_Styles $wp_styles
+ */
+function wp_check_widget_editor_deps() {
+ global $wp_scripts, $wp_styles;
+
+ if (
+ $wp_scripts->query( 'wp-edit-widgets', 'enqueued' ) ||
+ $wp_scripts->query( 'wp-customize-widgets', 'enqueued' )
+ ) {
+ if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) {
+ _doing_it_wrong(
+ 'wp_enqueue_script()',
+ sprintf(
+ /* translators: 1: 'wp-editor', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
+ __( '"%1$s" script should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
+ 'wp-editor',
+ 'wp-edit-widgets',
+ 'wp-customize-widgets'
+ ),
+ '5.8.0'
+ );
+ }
+ if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) {
+ _doing_it_wrong(
+ 'wp_enqueue_style()',
+ sprintf(
+ /* translators: 1: 'wp-edit-post', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
+ __( '"%1$s" style should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
+ 'wp-edit-post',
+ 'wp-edit-widgets',
+ 'wp-customize-widgets'
+ ),
+ '5.8.0'
+ );
+ }
+ }
+}
+
+/**
+ * Registers the previous theme's sidebars for the block themes.
+ *
+ * @since 6.2.0
+ * @access private
+ *
+ * @global array $wp_registered_sidebars Registered sidebars.
+ */
+function _wp_block_theme_register_classic_sidebars() {
+ global $wp_registered_sidebars;
+
+ if ( ! wp_is_block_theme() ) {
+ return;
+ }
+
+ $classic_sidebars = get_theme_mod( 'wp_classic_sidebars' );
+ if ( empty( $classic_sidebars ) ) {
+ return;
+ }
+
+ // Don't use `register_sidebar` since it will enable the `widgets` support for a theme.
+ foreach ( $classic_sidebars as $sidebar ) {
+ $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
+ }
+}
diff --git a/wp-includes/widgets/class-wp-nav-menu-widget.php b/wp-includes/widgets/class-wp-nav-menu-widget.php
new file mode 100644
index 0000000..328f416
--- /dev/null
+++ b/wp-includes/widgets/class-wp-nav-menu-widget.php
@@ -0,0 +1,206 @@
+<?php
+/**
+ * Widget API: WP_Nav_Menu_Widget class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement the Navigation Menu widget.
+ *
+ * @since 3.0.0
+ *
+ * @see WP_Widget
+ */
+class WP_Nav_Menu_Widget extends WP_Widget {
+
+ /**
+ * Sets up a new Navigation Menu widget instance.
+ *
+ * @since 3.0.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'description' => __( 'Add a navigation menu to your sidebar.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'nav_menu', __( 'Navigation Menu' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Navigation Menu widget instance.
+ *
+ * @since 3.0.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Navigation Menu widget instance.
+ */
+ public function widget( $args, $instance ) {
+ // Get menu.
+ $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
+
+ if ( ! $nav_menu ) {
+ return;
+ }
+
+ $default_title = __( 'Menu' );
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ echo $args['before_widget'];
+
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /**
+ * Filters the HTML format of widgets with navigation links.
+ *
+ * @since 5.5.0
+ *
+ * @param string $format The type of markup to use in widgets with navigation links.
+ * Accepts 'html5', 'xhtml'.
+ */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+
+ $nav_menu_args = array(
+ 'fallback_cb' => '',
+ 'menu' => $nav_menu,
+ 'container' => 'nav',
+ 'container_aria_label' => $aria_label,
+ 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
+ );
+ } else {
+ $nav_menu_args = array(
+ 'fallback_cb' => '',
+ 'menu' => $nav_menu,
+ );
+ }
+
+ /**
+ * Filters the arguments for the Navigation Menu widget.
+ *
+ * @since 4.2.0
+ * @since 4.4.0 Added the `$instance` parameter.
+ *
+ * @param array $nav_menu_args {
+ * An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.
+ *
+ * @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
+ * @type mixed $menu Menu ID, slug, or name.
+ * }
+ * @param WP_Term $nav_menu Nav menu object for the current menu.
+ * @param array $args Display arguments for the current widget.
+ * @param array $instance Array of settings for the current widget.
+ */
+ wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Handles updating settings for the current Navigation Menu widget instance.
+ *
+ * @since 3.0.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = array();
+ if ( ! empty( $new_instance['title'] ) ) {
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ }
+ if ( ! empty( $new_instance['nav_menu'] ) ) {
+ $instance['nav_menu'] = (int) $new_instance['nav_menu'];
+ }
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Navigation Menu widget.
+ *
+ * @since 3.0.0
+ *
+ * @param array $instance Current settings.
+ * @global WP_Customize_Manager $wp_customize
+ */
+ public function form( $instance ) {
+ global $wp_customize;
+ $title = isset( $instance['title'] ) ? $instance['title'] : '';
+ $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
+
+ // Get menus.
+ $menus = wp_get_nav_menus();
+
+ $empty_menus_style = '';
+ $not_empty_menus_style = '';
+ if ( empty( $menus ) ) {
+ $empty_menus_style = ' style="display:none" ';
+ } else {
+ $not_empty_menus_style = ' style="display:none" ';
+ }
+
+ $nav_menu_style = '';
+ if ( ! $nav_menu ) {
+ $nav_menu_style = 'display: none;';
+ }
+
+ // If no menus exists, direct the user to go and create some.
+ ?>
+ <p class="nav-menu-widget-no-menus-message" <?php echo $not_empty_menus_style; ?>>
+ <?php
+ if ( $wp_customize instanceof WP_Customize_Manager ) {
+ $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
+ } else {
+ $url = admin_url( 'nav-menus.php' );
+ }
+
+ printf(
+ /* translators: %s: URL to create a new menu. */
+ __( 'No menus have been created yet. <a href="%s">Create some</a>.' ),
+ // The URL can be a `javascript:` link, so esc_attr() is used here instead of esc_url().
+ esc_attr( $url )
+ );
+ ?>
+ </p>
+ <div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
+ </p>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
+ <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
+ <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
+ <?php foreach ( $menus as $menu ) : ?>
+ <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
+ <?php echo esc_html( $menu->name ); ?>
+ </option>
+ <?php endforeach; ?>
+ </select>
+ </p>
+ <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
+ <p class="edit-selected-nav-menu" style="<?php echo $nav_menu_style; ?>">
+ <button type="button" class="button"><?php _e( 'Edit Menu' ); ?></button>
+ </p>
+ <?php endif; ?>
+ </div>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-archives.php b/wp-includes/widgets/class-wp-widget-archives.php
new file mode 100644
index 0000000..7457244
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-archives.php
@@ -0,0 +1,230 @@
+<?php
+/**
+ * Widget API: WP_Widget_Archives class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement the Archives widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Archives extends WP_Widget {
+
+ /**
+ * Sets up a new Archives widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_archive',
+ 'description' => __( 'A monthly archive of your site&#8217;s Posts.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'archives', __( 'Archives' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Archives widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Archives widget instance.
+ */
+ public function widget( $args, $instance ) {
+ $default_title = __( 'Archives' );
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ $count = ! empty( $instance['count'] ) ? '1' : '0';
+ $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0';
+
+ echo $args['before_widget'];
+
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ if ( $dropdown ) {
+ $dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
+ ?>
+ <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
+ <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown">
+ <?php
+ /**
+ * Filters the arguments for the Archives widget drop-down.
+ *
+ * @since 2.8.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @see wp_get_archives()
+ *
+ * @param array $args An array of Archives widget drop-down arguments.
+ * @param array $instance Settings for the current Archives widget instance.
+ */
+ $dropdown_args = apply_filters(
+ 'widget_archives_dropdown_args',
+ array(
+ 'type' => 'monthly',
+ 'format' => 'option',
+ 'show_post_count' => $count,
+ ),
+ $instance
+ );
+
+ switch ( $dropdown_args['type'] ) {
+ case 'yearly':
+ $label = __( 'Select Year' );
+ break;
+ case 'monthly':
+ $label = __( 'Select Month' );
+ break;
+ case 'daily':
+ $label = __( 'Select Day' );
+ break;
+ case 'weekly':
+ $label = __( 'Select Week' );
+ break;
+ default:
+ $label = __( 'Select Post' );
+ break;
+ }
+ ?>
+
+ <option value=""><?php echo esc_html( $label ); ?></option>
+ <?php wp_get_archives( $dropdown_args ); ?>
+
+ </select>
+
+ <?php ob_start(); ?>
+<script>
+(function() {
+ var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
+ function onSelectChange() {
+ if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
+ document.location.href = this.options[ this.selectedIndex ].value;
+ }
+ }
+ dropdown.onchange = onSelectChange;
+})();
+</script>
+ <?php
+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
+ } else {
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+ echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+ ?>
+
+ <ul>
+ <?php
+ wp_get_archives(
+ /**
+ * Filters the arguments for the Archives widget.
+ *
+ * @since 2.8.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @see wp_get_archives()
+ *
+ * @param array $args An array of Archives option arguments.
+ * @param array $instance Array of settings for the current widget.
+ */
+ apply_filters(
+ 'widget_archives_args',
+ array(
+ 'type' => 'monthly',
+ 'show_post_count' => $count,
+ ),
+ $instance
+ )
+ );
+ ?>
+ </ul>
+
+ <?php
+ if ( 'html5' === $format ) {
+ echo '</nav>';
+ }
+ }
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Handles updating settings for the current Archives widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget_Archives::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $new_instance = wp_parse_args(
+ (array) $new_instance,
+ array(
+ 'title' => '',
+ 'count' => 0,
+ 'dropdown' => '',
+ )
+ );
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ $instance['count'] = $new_instance['count'] ? 1 : 0;
+ $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
+
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Archives widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $instance = wp_parse_args(
+ (array) $instance,
+ array(
+ 'title' => '',
+ 'count' => 0,
+ 'dropdown' => '',
+ )
+ );
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
+ </p>
+ <p>
+ <input class="checkbox" type="checkbox"<?php checked( $instance['dropdown'] ); ?> id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>" />
+ <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
+ <br />
+ <input class="checkbox" type="checkbox"<?php checked( $instance['count'] ); ?> id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" />
+ <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
+ </p>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-block.php b/wp-includes/widgets/class-wp-widget-block.php
new file mode 100644
index 0000000..4437182
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-block.php
@@ -0,0 +1,232 @@
+<?php
+/**
+ * Widget API: WP_Widget_Block class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 5.8.0
+ */
+
+/**
+ * Core class used to implement a Block widget.
+ *
+ * @since 5.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Block extends WP_Widget {
+
+ /**
+ * Default instance.
+ *
+ * @since 5.8.0
+ * @var array
+ */
+ protected $default_instance = array(
+ 'content' => '',
+ );
+
+ /**
+ * Sets up a new Block widget instance.
+ *
+ * @since 5.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_block',
+ 'description' => __( 'A widget containing a block.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ $control_ops = array(
+ 'width' => 400,
+ 'height' => 350,
+ );
+ parent::__construct( 'block', __( 'Block' ), $widget_ops, $control_ops );
+
+ add_filter( 'is_wide_widget_in_customizer', array( $this, 'set_is_wide_widget_in_customizer' ), 10, 2 );
+ }
+
+ /**
+ * Outputs the content for the current Block widget instance.
+ *
+ * @since 5.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Block widget instance.
+ */
+ public function widget( $args, $instance ) {
+ $instance = wp_parse_args( $instance, $this->default_instance );
+
+ echo str_replace(
+ 'widget_block',
+ $this->get_dynamic_classname( $instance['content'] ),
+ $args['before_widget']
+ );
+
+ /**
+ * Filters the content of the Block widget before output.
+ *
+ * @since 5.8.0
+ *
+ * @param string $content The widget content.
+ * @param array $instance Array of settings for the current widget.
+ * @param WP_Widget_Block $widget Current Block widget instance.
+ */
+ echo apply_filters(
+ 'widget_block_content',
+ $instance['content'],
+ $instance,
+ $this
+ );
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Calculates the classname to use in the block widget's container HTML.
+ *
+ * Usually this is set to `$this->widget_options['classname']` by
+ * dynamic_sidebar(). In this case, however, we want to set the classname
+ * dynamically depending on the block contained by this block widget.
+ *
+ * If a block widget contains a block that has an equivalent legacy widget,
+ * we display that legacy widget's class name. This helps with theme
+ * backwards compatibility.
+ *
+ * @since 5.8.0
+ *
+ * @param string $content The HTML content of the current block widget.
+ * @return string The classname to use in the block widget's container HTML.
+ */
+ private function get_dynamic_classname( $content ) {
+ $blocks = parse_blocks( $content );
+
+ $block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;
+
+ switch ( $block_name ) {
+ case 'core/paragraph':
+ $classname = 'widget_block widget_text';
+ break;
+ case 'core/calendar':
+ $classname = 'widget_block widget_calendar';
+ break;
+ case 'core/search':
+ $classname = 'widget_block widget_search';
+ break;
+ case 'core/html':
+ $classname = 'widget_block widget_custom_html';
+ break;
+ case 'core/archives':
+ $classname = 'widget_block widget_archive';
+ break;
+ case 'core/latest-posts':
+ $classname = 'widget_block widget_recent_entries';
+ break;
+ case 'core/latest-comments':
+ $classname = 'widget_block widget_recent_comments';
+ break;
+ case 'core/tag-cloud':
+ $classname = 'widget_block widget_tag_cloud';
+ break;
+ case 'core/categories':
+ $classname = 'widget_block widget_categories';
+ break;
+ case 'core/audio':
+ $classname = 'widget_block widget_media_audio';
+ break;
+ case 'core/video':
+ $classname = 'widget_block widget_media_video';
+ break;
+ case 'core/image':
+ $classname = 'widget_block widget_media_image';
+ break;
+ case 'core/gallery':
+ $classname = 'widget_block widget_media_gallery';
+ break;
+ case 'core/rss':
+ $classname = 'widget_block widget_rss';
+ break;
+ default:
+ $classname = 'widget_block';
+ }
+
+ /**
+ * The classname used in the block widget's container HTML.
+ *
+ * This can be set according to the name of the block contained by the block widget.
+ *
+ * @since 5.8.0
+ *
+ * @param string $classname The classname to be used in the block widget's container HTML,
+ * e.g. 'widget_block widget_text'.
+ * @param string $block_name The name of the block contained by the block widget,
+ * e.g. 'core/paragraph'.
+ */
+ return apply_filters( 'widget_block_dynamic_classname', $classname, $block_name );
+ }
+
+ /**
+ * Handles updating settings for the current Block widget instance.
+ *
+ * @since 5.8.0
+
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Settings to save or bool false to cancel saving.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = array_merge( $this->default_instance, $old_instance );
+
+ if ( current_user_can( 'unfiltered_html' ) ) {
+ $instance['content'] = $new_instance['content'];
+ } else {
+ $instance['content'] = wp_kses_post( $new_instance['content'] );
+ }
+
+ return $instance;
+ }
+
+ /**
+ * Outputs the Block widget settings form.
+ *
+ * @since 5.8.0
+ *
+ * @see WP_Widget_Custom_HTML::render_control_template_scripts()
+ *
+ * @param array $instance Current instance.
+ */
+ public function form( $instance ) {
+ $instance = wp_parse_args( (array) $instance, $this->default_instance );
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'content' ); ?>">
+ <?php
+ /* translators: HTML code of the block, not an option that blocks HTML. */
+ _e( 'Block HTML:' );
+ ?>
+ </label>
+ <textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" rows="6" cols="50" class="widefat code"><?php echo esc_textarea( $instance['content'] ); ?></textarea>
+ </p>
+ <?php
+ }
+
+ /**
+ * Makes sure no block widget is considered to be wide.
+ *
+ * @since 5.8.0
+ *
+ * @param bool $is_wide Whether the widget is considered wide.
+ * @param string $widget_id Widget ID.
+ * @return bool Updated `is_wide` value.
+ */
+ public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
+ if ( str_starts_with( $widget_id, 'block-' ) ) {
+ return false;
+ }
+
+ return $is_wide;
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-calendar.php b/wp-includes/widgets/class-wp-widget-calendar.php
new file mode 100644
index 0000000..9103934
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-calendar.php
@@ -0,0 +1,105 @@
+<?php
+/**
+ * Widget API: WP_Widget_Calendar class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement the Calendar widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Calendar extends WP_Widget {
+ /**
+ * Ensure that the ID attribute only appears in the markup once
+ *
+ * @since 4.4.0
+ * @var int
+ */
+ private static $instance = 0;
+
+ /**
+ * Sets up a new Calendar widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_calendar',
+ 'description' => __( 'A calendar of your site’s posts.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'calendar', __( 'Calendar' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Calendar widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance The settings for the particular instance of the widget.
+ */
+ public function widget( $args, $instance ) {
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ echo $args['before_widget'];
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+ if ( 0 === self::$instance ) {
+ echo '<div id="calendar_wrap" class="calendar_wrap">';
+ } else {
+ echo '<div class="calendar_wrap">';
+ }
+ get_calendar();
+ echo '</div>';
+ echo $args['after_widget'];
+
+ ++self::$instance;
+ }
+
+ /**
+ * Handles updating settings for the current Calendar widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Calendar widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
+ </p>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-categories.php b/wp-includes/widgets/class-wp-widget-categories.php
new file mode 100644
index 0000000..b79881e
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-categories.php
@@ -0,0 +1,205 @@
+<?php
+/**
+ * Widget API: WP_Widget_Categories class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Categories widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Categories extends WP_Widget {
+
+ /**
+ * Sets up a new Categories widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_categories',
+ 'description' => __( 'A list or dropdown of categories.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Categories widget instance.
+ *
+ * @since 2.8.0
+ * @since 4.2.0 Creates a unique HTML ID for the `<select>` element
+ * if more than one instance is displayed on the page.
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Categories widget instance.
+ */
+ public function widget( $args, $instance ) {
+ static $first_dropdown = true;
+
+ $default_title = __( 'Categories' );
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ $count = ! empty( $instance['count'] ) ? '1' : '0';
+ $hierarchical = ! empty( $instance['hierarchical'] ) ? '1' : '0';
+ $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0';
+
+ echo $args['before_widget'];
+
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $cat_args = array(
+ 'orderby' => 'name',
+ 'show_count' => $count,
+ 'hierarchical' => $hierarchical,
+ );
+
+ if ( $dropdown ) {
+ printf( '<form action="%s" method="get">', esc_url( home_url() ) );
+ $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
+ $first_dropdown = false;
+
+ echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
+
+ $cat_args['show_option_none'] = __( 'Select Category' );
+ $cat_args['id'] = $dropdown_id;
+
+ /**
+ * Filters the arguments for the Categories widget drop-down.
+ *
+ * @since 2.8.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @see wp_dropdown_categories()
+ *
+ * @param array $cat_args An array of Categories widget drop-down arguments.
+ * @param array $instance Array of settings for the current widget.
+ */
+ wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
+
+ echo '</form>';
+
+ ob_start();
+ ?>
+
+<script>
+(function() {
+ var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
+ function onCatChange() {
+ if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
+ dropdown.parentNode.submit();
+ }
+ }
+ dropdown.onchange = onCatChange;
+})();
+</script>
+
+ <?php
+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
+ } else {
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+ echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+ ?>
+
+ <ul>
+ <?php
+ $cat_args['title_li'] = '';
+
+ /**
+ * Filters the arguments for the Categories widget.
+ *
+ * @since 2.8.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @param array $cat_args An array of Categories widget options.
+ * @param array $instance Array of settings for the current widget.
+ */
+ wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
+ ?>
+ </ul>
+
+ <?php
+ if ( 'html5' === $format ) {
+ echo '</nav>';
+ }
+ }
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Handles updating settings for the current Categories widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
+ $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
+ $instance['dropdown'] = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
+
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Categories widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ // Defaults.
+ $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
+ $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
+ $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
+ $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
+ </p>
+
+ <p>
+ <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
+ <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
+ <br />
+
+ <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
+ <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
+ <br />
+
+ <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
+ <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label>
+ </p>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-custom-html.php b/wp-includes/widgets/class-wp-widget-custom-html.php
new file mode 100644
index 0000000..771520e
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-custom-html.php
@@ -0,0 +1,343 @@
+<?php
+/**
+ * Widget API: WP_Widget_Custom_HTML class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.8.1
+ */
+
+/**
+ * Core class used to implement a Custom HTML widget.
+ *
+ * @since 4.8.1
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Custom_HTML extends WP_Widget {
+
+ /**
+ * Whether or not the widget has been registered yet.
+ *
+ * @since 4.9.0
+ * @var bool
+ */
+ protected $registered = false;
+
+ /**
+ * Default instance.
+ *
+ * @since 4.8.1
+ * @var array
+ */
+ protected $default_instance = array(
+ 'title' => '',
+ 'content' => '',
+ );
+
+ /**
+ * Sets up a new Custom HTML widget instance.
+ *
+ * @since 4.8.1
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_custom_html',
+ 'description' => __( 'Arbitrary HTML code.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ $control_ops = array(
+ 'width' => 400,
+ 'height' => 350,
+ );
+ parent::__construct( 'custom_html', __( 'Custom HTML' ), $widget_ops, $control_ops );
+ }
+
+ /**
+ * Add hooks for enqueueing assets when registering all widget instances of this widget class.
+ *
+ * @since 4.9.0
+ *
+ * @param int $number Optional. The unique order number of this widget instance
+ * compared to other instances of the same class. Default -1.
+ */
+ public function _register_one( $number = -1 ) {
+ parent::_register_one( $number );
+ if ( $this->registered ) {
+ return;
+ }
+ $this->registered = true;
+
+ /*
+ * Note that the widgets component in the customizer will also do
+ * the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
+ */
+ add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
+
+ /*
+ * Note that the widgets component in the customizer will also do
+ * the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
+ */
+ add_action( 'admin_footer-widgets.php', array( 'WP_Widget_Custom_HTML', 'render_control_template_scripts' ) );
+
+ // Note this action is used to ensure the help text is added to the end.
+ add_action( 'admin_head-widgets.php', array( 'WP_Widget_Custom_HTML', 'add_help_text' ) );
+ }
+
+ /**
+ * Filters gallery shortcode attributes.
+ *
+ * Prevents all of a site's attachments from being shown in a gallery displayed on a
+ * non-singular template where a $post context is not available.
+ *
+ * @since 4.9.0
+ *
+ * @param array $attrs Attributes.
+ * @return array Attributes.
+ */
+ public function _filter_gallery_shortcode_attrs( $attrs ) {
+ if ( ! is_singular() && empty( $attrs['id'] ) && empty( $attrs['include'] ) ) {
+ $attrs['id'] = -1;
+ }
+ return $attrs;
+ }
+
+ /**
+ * Outputs the content for the current Custom HTML widget instance.
+ *
+ * @since 4.8.1
+ *
+ * @global WP_Post $post Global post object.
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Custom HTML widget instance.
+ */
+ public function widget( $args, $instance ) {
+ global $post;
+
+ // Override global $post so filters (and shortcodes) apply in a consistent context.
+ $original_post = $post;
+ if ( is_singular() ) {
+ // Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).
+ $post = get_queried_object();
+ } else {
+ // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.
+ $post = null;
+ }
+
+ // Prevent dumping out all attachments from the media library.
+ add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
+
+ $instance = array_merge( $this->default_instance, $instance );
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
+
+ // Prepare instance data that looks like a normal Text widget.
+ $simulated_text_widget_instance = array_merge(
+ $instance,
+ array(
+ 'text' => isset( $instance['content'] ) ? $instance['content'] : '',
+ 'filter' => false, // Because wpautop is not applied.
+ 'visual' => false, // Because it wasn't created in TinyMCE.
+ )
+ );
+ unset( $simulated_text_widget_instance['content'] ); // Was moved to 'text' prop.
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */
+ $content = apply_filters( 'widget_text', $instance['content'], $simulated_text_widget_instance, $this );
+
+ // Adds 'noopener' relationship, without duplicating values, to all HTML A elements that have a target.
+ $content = wp_targeted_link_rel( $content );
+
+ /**
+ * Filters the content of the Custom HTML widget.
+ *
+ * @since 4.8.1
+ *
+ * @param string $content The widget content.
+ * @param array $instance Array of settings for the current widget.
+ * @param WP_Widget_Custom_HTML $widget Current Custom HTML widget instance.
+ */
+ $content = apply_filters( 'widget_custom_html_content', $content, $instance, $this );
+
+ // Restore post global.
+ $post = $original_post;
+ remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
+
+ // Inject the Text widget's container class name alongside this widget's class name for theme styling compatibility.
+ $args['before_widget'] = preg_replace( '/(?<=\sclass=["\'])/', 'widget_text ', $args['before_widget'] );
+
+ echo $args['before_widget'];
+ if ( ! empty( $title ) ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+ echo '<div class="textwidget custom-html-widget">'; // The textwidget class is for theme styling compatibility.
+ echo $content;
+ echo '</div>';
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Handles updating settings for the current Custom HTML widget instance.
+ *
+ * @since 4.8.1
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Settings to save or bool false to cancel saving.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = array_merge( $this->default_instance, $old_instance );
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ if ( current_user_can( 'unfiltered_html' ) ) {
+ $instance['content'] = $new_instance['content'];
+ } else {
+ $instance['content'] = wp_kses_post( $new_instance['content'] );
+ }
+ return $instance;
+ }
+
+ /**
+ * Loads the required scripts and styles for the widget control.
+ *
+ * @since 4.9.0
+ */
+ public function enqueue_admin_scripts() {
+ $settings = wp_enqueue_code_editor(
+ array(
+ 'type' => 'text/html',
+ 'codemirror' => array(
+ 'indentUnit' => 2,
+ 'tabSize' => 2,
+ ),
+ )
+ );
+
+ wp_enqueue_script( 'custom-html-widgets' );
+ wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
+
+ if ( empty( $settings ) ) {
+ $settings = array(
+ 'disabled' => true,
+ );
+ }
+ wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.init( %s );', wp_json_encode( $settings ) ), 'after' );
+
+ $l10n = array(
+ 'errorNotice' => array(
+ /* translators: %d: Error count. */
+ 'singular' => _n( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.', 1 ),
+ /* translators: %d: Error count. */
+ 'plural' => _n( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.', 2 ),
+ // @todo This is lacking, as some languages have a dedicated dual form. For proper handling of plurals in JS, see #20491.
+ ),
+ );
+ wp_add_inline_script( 'custom-html-widgets', sprintf( 'jQuery.extend( wp.customHtmlWidgets.l10n, %s );', wp_json_encode( $l10n ) ), 'after' );
+ }
+
+ /**
+ * Outputs the Custom HTML widget settings form.
+ *
+ * @since 4.8.1
+ * @since 4.9.0 The form contains only hidden sync inputs. For the control UI, see `WP_Widget_Custom_HTML::render_control_template_scripts()`.
+ *
+ * @see WP_Widget_Custom_HTML::render_control_template_scripts()
+ *
+ * @param array $instance Current instance.
+ */
+ public function form( $instance ) {
+ $instance = wp_parse_args( (array) $instance, $this->default_instance );
+ ?>
+ <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="title sync-input" type="hidden" value="<?php echo esc_attr( $instance['title'] ); ?>" />
+ <textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" class="content sync-input" hidden><?php echo esc_textarea( $instance['content'] ); ?></textarea>
+ <?php
+ }
+
+ /**
+ * Render form template scripts.
+ *
+ * @since 4.9.0
+ */
+ public static function render_control_template_scripts() {
+ ?>
+ <script type="text/html" id="tmpl-widget-custom-html-control-fields">
+ <# var elementIdPrefix = 'el' + String( Math.random() ).replace( /\D/g, '' ) + '_' #>
+ <p>
+ <label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
+ <input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
+ </p>
+
+ <p>
+ <label for="{{ elementIdPrefix }}content" id="{{ elementIdPrefix }}content-label"><?php esc_html_e( 'Content:' ); ?></label>
+ <textarea id="{{ elementIdPrefix }}content" class="widefat code content" rows="16" cols="20"></textarea>
+ </p>
+
+ <?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
+ <?php
+ $probably_unsafe_html = array( 'script', 'iframe', 'form', 'input', 'style' );
+ $allowed_html = wp_kses_allowed_html( 'post' );
+ $disallowed_html = array_diff( $probably_unsafe_html, array_keys( $allowed_html ) );
+ ?>
+ <?php if ( ! empty( $disallowed_html ) ) : ?>
+ <# if ( data.codeEditorDisabled ) { #>
+ <p>
+ <?php _e( 'Some HTML tags are not permitted, including:' ); ?>
+ <code><?php echo implode( '</code>, <code>', $disallowed_html ); ?></code>
+ </p>
+ <# } #>
+ <?php endif; ?>
+ <?php endif; ?>
+
+ <div class="code-editor-error-container"></div>
+ </script>
+ <?php
+ }
+
+ /**
+ * Add help text to widgets admin screen.
+ *
+ * @since 4.9.0
+ */
+ public static function add_help_text() {
+ $screen = get_current_screen();
+
+ $content = '<p>';
+ $content .= __( 'Use the Custom HTML widget to add arbitrary HTML code to your widget areas.' );
+ $content .= '</p>';
+
+ if ( 'false' !== wp_get_current_user()->syntax_highlighting ) {
+ $content .= '<p>';
+ $content .= sprintf(
+ /* translators: 1: Link to user profile, 2: Additional link attributes, 3: Accessibility text. */
+ __( 'The edit field automatically highlights code syntax. You can disable this in your <a href="%1$s" %2$s>user profile%3$s</a> to work in plain text mode.' ),
+ esc_url( get_edit_profile_url() ),
+ 'class="external-link" target="_blank"',
+ sprintf(
+ '<span class="screen-reader-text"> %s</span>',
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ )
+ );
+ $content .= '</p>';
+
+ $content .= '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>';
+ $content .= '<ul>';
+ $content .= '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>';
+ $content .= '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>';
+ $content .= '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>';
+ $content .= '</ul>';
+ }
+
+ $screen->add_help_tab(
+ array(
+ 'id' => 'custom_html_widget',
+ 'title' => __( 'Custom HTML Widget' ),
+ 'content' => $content,
+ )
+ );
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-links.php b/wp-includes/widgets/class-wp-widget-links.php
new file mode 100644
index 0000000..58ae2a7
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-links.php
@@ -0,0 +1,188 @@
+<?php
+/**
+ * Widget API: WP_Widget_Links class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Links widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Links extends WP_Widget {
+
+ /**
+ * Sets up a new Links widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'description' => __( 'Your blogroll' ),
+ 'customize_selective_refresh' => true,
+ );
+ parent::__construct( 'links', __( 'Links' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Links widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Links widget instance.
+ */
+ public function widget( $args, $instance ) {
+ $show_description = isset( $instance['description'] ) ? $instance['description'] : false;
+ $show_name = isset( $instance['name'] ) ? $instance['name'] : false;
+ $show_rating = isset( $instance['rating'] ) ? $instance['rating'] : false;
+ $show_images = isset( $instance['images'] ) ? $instance['images'] : true;
+ $category = isset( $instance['category'] ) ? $instance['category'] : false;
+ $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
+ $order = 'rating' === $orderby ? 'DESC' : 'ASC';
+ $limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
+
+ $before_widget = preg_replace( '/ id="[^"]*"/', ' id="%id"', $args['before_widget'] );
+
+ $widget_links_args = array(
+ 'title_before' => $args['before_title'],
+ 'title_after' => $args['after_title'],
+ 'category_before' => $before_widget,
+ 'category_after' => $args['after_widget'],
+ 'show_images' => $show_images,
+ 'show_description' => $show_description,
+ 'show_name' => $show_name,
+ 'show_rating' => $show_rating,
+ 'category' => $category,
+ 'class' => 'linkcat widget',
+ 'orderby' => $orderby,
+ 'order' => $order,
+ 'limit' => $limit,
+ );
+
+ /**
+ * Filters the arguments for the Links widget.
+ *
+ * @since 2.6.0
+ * @since 4.4.0 Added the `$instance` parameter.
+ *
+ * @see wp_list_bookmarks()
+ *
+ * @param array $widget_links_args An array of arguments to retrieve the links list.
+ * @param array $instance The settings for the particular instance of the widget.
+ */
+ wp_list_bookmarks( apply_filters( 'widget_links_args', $widget_links_args, $instance ) );
+ }
+
+ /**
+ * Handles updating settings for the current Links widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $new_instance = (array) $new_instance;
+ $instance = array(
+ 'images' => 0,
+ 'name' => 0,
+ 'description' => 0,
+ 'rating' => 0,
+ );
+ foreach ( $instance as $field => $val ) {
+ if ( isset( $new_instance[ $field ] ) ) {
+ $instance[ $field ] = 1;
+ }
+ }
+
+ $instance['orderby'] = 'name';
+ if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ), true ) ) {
+ $instance['orderby'] = $new_instance['orderby'];
+ }
+
+ $instance['category'] = (int) $new_instance['category'];
+ $instance['limit'] = ! empty( $new_instance['limit'] ) ? (int) $new_instance['limit'] : -1;
+
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Links widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+
+ // Defaults.
+ $instance = wp_parse_args(
+ (array) $instance,
+ array(
+ 'images' => true,
+ 'name' => true,
+ 'description' => false,
+ 'rating' => false,
+ 'category' => false,
+ 'orderby' => 'name',
+ 'limit' => -1,
+ )
+ );
+ $link_cats = get_terms( array( 'taxonomy' => 'link_category' ) );
+ $limit = (int) $instance['limit'];
+ if ( ! $limit ) {
+ $limit = -1;
+ }
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select Link Category:' ); ?></label>
+ <select class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>">
+ <option value=""><?php _ex( 'All Links', 'links widget' ); ?></option>
+ <?php foreach ( $link_cats as $link_cat ) : ?>
+ <option value="<?php echo (int) $link_cat->term_id; ?>" <?php selected( $instance['category'], $link_cat->term_id ); ?>>
+ <?php echo esc_html( $link_cat->name ); ?>
+ </option>
+ <?php endforeach; ?>
+ </select>
+ <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e( 'Sort by:' ); ?></label>
+ <select name="<?php echo $this->get_field_name( 'orderby' ); ?>" id="<?php echo $this->get_field_id( 'orderby' ); ?>" class="widefat">
+ <option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
+ <option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
+ <option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
+ <option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _ex( 'Random', 'Links widget' ); ?></option>
+ </select>
+ </p>
+
+ <p>
+ <input class="checkbox" type="checkbox"<?php checked( $instance['images'], true ); ?> id="<?php echo $this->get_field_id( 'images' ); ?>" name="<?php echo $this->get_field_name( 'images' ); ?>" />
+ <label for="<?php echo $this->get_field_id( 'images' ); ?>"><?php _e( 'Show Link Image' ); ?></label>
+ <br />
+
+ <input class="checkbox" type="checkbox"<?php checked( $instance['name'], true ); ?> id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" />
+ <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'Show Link Name' ); ?></label>
+ <br />
+
+ <input class="checkbox" type="checkbox"<?php checked( $instance['description'], true ); ?> id="<?php echo $this->get_field_id( 'description' ); ?>" name="<?php echo $this->get_field_name( 'description' ); ?>" />
+ <label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e( 'Show Link Description' ); ?></label>
+ <br />
+
+ <input class="checkbox" type="checkbox"<?php checked( $instance['rating'], true ); ?> id="<?php echo $this->get_field_id( 'rating' ); ?>" name="<?php echo $this->get_field_name( 'rating' ); ?>" />
+ <label for="<?php echo $this->get_field_id( 'rating' ); ?>"><?php _e( 'Show Link Rating' ); ?></label>
+ </p>
+
+ <p>
+ <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Number of links to show:' ); ?></label>
+ <input id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="text" value="<?php echo ( -1 !== $limit ) ? (int) $limit : ''; ?>" size="3" />
+ </p>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-media-audio.php b/wp-includes/widgets/class-wp-widget-media-audio.php
new file mode 100644
index 0000000..d7bffd2
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-media-audio.php
@@ -0,0 +1,217 @@
+<?php
+/**
+ * Widget API: WP_Widget_Media_Audio class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.8.0
+ */
+
+/**
+ * Core class that implements an audio widget.
+ *
+ * @since 4.8.0
+ *
+ * @see WP_Widget_Media
+ * @see WP_Widget
+ */
+class WP_Widget_Media_Audio extends WP_Widget_Media {
+
+ /**
+ * Constructor.
+ *
+ * @since 4.8.0
+ */
+ public function __construct() {
+ parent::__construct(
+ 'media_audio',
+ __( 'Audio' ),
+ array(
+ 'description' => __( 'Displays an audio player.' ),
+ 'mime_type' => 'audio',
+ )
+ );
+
+ $this->l10n = array_merge(
+ $this->l10n,
+ array(
+ 'no_media_selected' => __( 'No audio selected' ),
+ 'add_media' => _x( 'Add Audio', 'label for button in the audio widget' ),
+ 'replace_media' => _x( 'Replace Audio', 'label for button in the audio widget; should preferably not be longer than ~13 characters long' ),
+ 'edit_media' => _x( 'Edit Audio', 'label for button in the audio widget; should preferably not be longer than ~13 characters long' ),
+ 'missing_attachment' => sprintf(
+ /* translators: %s: URL to media library. */
+ __( 'That audio file cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
+ esc_url( admin_url( 'upload.php' ) )
+ ),
+ /* translators: %d: Widget count. */
+ 'media_library_state_multi' => _n_noop( 'Audio Widget (%d)', 'Audio Widget (%d)' ),
+ 'media_library_state_single' => __( 'Audio Widget' ),
+ 'unsupported_file_type' => __( 'Looks like this is not the correct kind of file. Please link to an audio file instead.' ),
+ )
+ );
+ }
+
+ /**
+ * Get schema for properties of a widget instance (item).
+ *
+ * @since 4.8.0
+ *
+ * @see WP_REST_Controller::get_item_schema()
+ * @see WP_REST_Controller::get_additional_fields()
+ * @link https://core.trac.wordpress.org/ticket/35574
+ *
+ * @return array Schema for properties.
+ */
+ public function get_instance_schema() {
+ $schema = array(
+ 'preload' => array(
+ 'type' => 'string',
+ 'enum' => array( 'none', 'auto', 'metadata' ),
+ 'default' => 'none',
+ 'description' => __( 'Preload' ),
+ ),
+ 'loop' => array(
+ 'type' => 'boolean',
+ 'default' => false,
+ 'description' => __( 'Loop' ),
+ ),
+ );
+
+ foreach ( wp_get_audio_extensions() as $audio_extension ) {
+ $schema[ $audio_extension ] = array(
+ 'type' => 'string',
+ 'default' => '',
+ 'format' => 'uri',
+ /* translators: %s: Audio extension. */
+ 'description' => sprintf( __( 'URL to the %s audio source file' ), $audio_extension ),
+ );
+ }
+
+ return array_merge( $schema, parent::get_instance_schema() );
+ }
+
+ /**
+ * Render the media on the frontend.
+ *
+ * @since 4.8.0
+ *
+ * @param array $instance Widget instance props.
+ */
+ public function render_media( $instance ) {
+ $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
+ $attachment = null;
+
+ if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
+ $attachment = get_post( $instance['attachment_id'] );
+ }
+
+ if ( $attachment ) {
+ $src = wp_get_attachment_url( $attachment->ID );
+ } else {
+ $src = $instance['url'];
+ }
+
+ echo wp_audio_shortcode(
+ array_merge(
+ $instance,
+ compact( 'src' )
+ )
+ );
+ }
+
+ /**
+ * Enqueue preview scripts.
+ *
+ * These scripts normally are enqueued just-in-time when an audio shortcode is used.
+ * In the customizer, however, widgets can be dynamically added and rendered via
+ * selective refresh, and so it is important to unconditionally enqueue them in
+ * case a widget does get added.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_preview_scripts() {
+ /** This filter is documented in wp-includes/media.php */
+ if ( 'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ) ) {
+ wp_enqueue_style( 'wp-mediaelement' );
+ wp_enqueue_script( 'wp-mediaelement' );
+ }
+ }
+
+ /**
+ * Loads the required media files for the media manager and scripts for media widgets.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_admin_scripts() {
+ parent::enqueue_admin_scripts();
+
+ wp_enqueue_style( 'wp-mediaelement' );
+ wp_enqueue_script( 'wp-mediaelement' );
+
+ $handle = 'media-audio-widget';
+ wp_enqueue_script( $handle );
+
+ $exported_schema = array();
+ foreach ( $this->get_instance_schema() as $field => $field_schema ) {
+ $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
+ }
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $exported_schema )
+ )
+ );
+
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ '
+ wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
+ wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
+ ',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $this->widget_options['mime_type'] ),
+ wp_json_encode( $this->l10n )
+ )
+ );
+ }
+
+ /**
+ * Render form template scripts.
+ *
+ * @since 4.8.0
+ */
+ public function render_control_template_scripts() {
+ parent::render_control_template_scripts()
+ ?>
+ <script type="text/html" id="tmpl-wp-media-widget-audio-preview">
+ <# if ( data.error && 'missing_attachment' === data.error ) { #>
+ <?php
+ wp_admin_notice(
+ $this->l10n['missing_attachment'],
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'notice-missing-attachment' ),
+ )
+ );
+ ?>
+ <# } else if ( data.error ) { #>
+ <?php
+ wp_admin_notice(
+ __( 'Unable to preview media due to an unknown error.' ),
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt' ),
+ )
+ );
+ ?>
+ <# } else if ( data.model && data.model.src ) { #>
+ <?php wp_underscore_audio_template(); ?>
+ <# } #>
+ </script>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-media-gallery.php b/wp-includes/widgets/class-wp-widget-media-gallery.php
new file mode 100644
index 0000000..ecc446c
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-media-gallery.php
@@ -0,0 +1,262 @@
+<?php
+/**
+ * Widget API: WP_Widget_Media_Gallery class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.9.0
+ */
+
+/**
+ * Core class that implements a gallery widget.
+ *
+ * @since 4.9.0
+ *
+ * @see WP_Widget_Media
+ * @see WP_Widget
+ */
+class WP_Widget_Media_Gallery extends WP_Widget_Media {
+
+ /**
+ * Constructor.
+ *
+ * @since 4.9.0
+ */
+ public function __construct() {
+ parent::__construct(
+ 'media_gallery',
+ __( 'Gallery' ),
+ array(
+ 'description' => __( 'Displays an image gallery.' ),
+ 'mime_type' => 'image',
+ )
+ );
+
+ $this->l10n = array_merge(
+ $this->l10n,
+ array(
+ 'no_media_selected' => __( 'No images selected' ),
+ 'add_media' => _x( 'Add Images', 'label for button in the gallery widget; should not be longer than ~13 characters long' ),
+ 'replace_media' => '',
+ 'edit_media' => _x( 'Edit Gallery', 'label for button in the gallery widget; should not be longer than ~13 characters long' ),
+ )
+ );
+ }
+
+ /**
+ * Get schema for properties of a widget instance (item).
+ *
+ * @since 4.9.0
+ *
+ * @see WP_REST_Controller::get_item_schema()
+ * @see WP_REST_Controller::get_additional_fields()
+ * @link https://core.trac.wordpress.org/ticket/35574
+ *
+ * @return array Schema for properties.
+ */
+ public function get_instance_schema() {
+ $schema = array(
+ 'title' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => 'sanitize_text_field',
+ 'description' => __( 'Title for the widget' ),
+ 'should_preview_update' => false,
+ ),
+ 'ids' => array(
+ 'type' => 'array',
+ 'items' => array(
+ 'type' => 'integer',
+ ),
+ 'default' => array(),
+ 'sanitize_callback' => 'wp_parse_id_list',
+ ),
+ 'columns' => array(
+ 'type' => 'integer',
+ 'default' => 3,
+ 'minimum' => 1,
+ 'maximum' => 9,
+ ),
+ 'size' => array(
+ 'type' => 'string',
+ 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
+ 'default' => 'thumbnail',
+ ),
+ 'link_type' => array(
+ 'type' => 'string',
+ 'enum' => array( 'post', 'file', 'none' ),
+ 'default' => 'post',
+ 'media_prop' => 'link',
+ 'should_preview_update' => false,
+ ),
+ 'orderby_random' => array(
+ 'type' => 'boolean',
+ 'default' => false,
+ 'media_prop' => '_orderbyRandom',
+ 'should_preview_update' => false,
+ ),
+ );
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-media.php */
+ $schema = apply_filters( "widget_{$this->id_base}_instance_schema", $schema, $this );
+
+ return $schema;
+ }
+
+ /**
+ * Render the media on the frontend.
+ *
+ * @since 4.9.0
+ *
+ * @param array $instance Widget instance props.
+ */
+ public function render_media( $instance ) {
+ $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
+
+ $shortcode_atts = array_merge(
+ $instance,
+ array(
+ 'link' => $instance['link_type'],
+ )
+ );
+
+ // @codeCoverageIgnoreStart
+ if ( $instance['orderby_random'] ) {
+ $shortcode_atts['orderby'] = 'rand';
+ }
+
+ // @codeCoverageIgnoreEnd
+ echo gallery_shortcode( $shortcode_atts );
+ }
+
+ /**
+ * Loads the required media files for the media manager and scripts for media widgets.
+ *
+ * @since 4.9.0
+ */
+ public function enqueue_admin_scripts() {
+ parent::enqueue_admin_scripts();
+
+ $handle = 'media-gallery-widget';
+ wp_enqueue_script( $handle );
+
+ $exported_schema = array();
+ foreach ( $this->get_instance_schema() as $field => $field_schema ) {
+ $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update', 'items' ) );
+ }
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $exported_schema )
+ )
+ );
+
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ '
+ wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
+ _.extend( wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
+ ',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $this->widget_options['mime_type'] ),
+ wp_json_encode( $this->l10n )
+ )
+ );
+ }
+
+ /**
+ * Render form template scripts.
+ *
+ * @since 4.9.0
+ */
+ public function render_control_template_scripts() {
+ parent::render_control_template_scripts();
+ ?>
+ <script type="text/html" id="tmpl-wp-media-widget-gallery-preview">
+ <#
+ var ids = _.filter( data.ids, function( id ) {
+ return ( id in data.attachments );
+ } );
+ #>
+ <# if ( ids.length ) { #>
+ <ul class="gallery media-widget-gallery-preview" role="list">
+ <# _.each( ids, function( id, index ) { #>
+ <# var attachment = data.attachments[ id ]; #>
+ <# if ( index < 6 ) { #>
+ <li class="gallery-item">
+ <div class="gallery-icon">
+ <img alt="{{ attachment.alt }}"
+ <# if ( index === 5 && data.ids.length > 6 ) { #> aria-hidden="true" <# } #>
+ <# if ( attachment.sizes.thumbnail ) { #>
+ src="{{ attachment.sizes.thumbnail.url }}" width="{{ attachment.sizes.thumbnail.width }}" height="{{ attachment.sizes.thumbnail.height }}"
+ <# } else { #>
+ src="{{ attachment.url }}"
+ <# } #>
+ <# if ( ! attachment.alt && attachment.filename ) { #>
+ aria-label="
+ <?php
+ echo esc_attr(
+ sprintf(
+ /* translators: %s: The image file name. */
+ __( 'The current image has no alternative text. The file name is: %s' ),
+ '{{ attachment.filename }}'
+ )
+ );
+ ?>
+ "
+ <# } #>
+ />
+ <# if ( index === 5 && data.ids.length > 6 ) { #>
+ <div class="gallery-icon-placeholder">
+ <p class="gallery-icon-placeholder-text" aria-label="
+ <?php
+ printf(
+ /* translators: %s: The amount of additional, not visible images in the gallery widget preview. */
+ __( 'Additional images added to this gallery: %s' ),
+ '{{ data.ids.length - 5 }}'
+ );
+ ?>
+ ">+{{ data.ids.length - 5 }}</p>
+ </div>
+ <# } #>
+ </div>
+ </li>
+ <# } #>
+ <# } ); #>
+ </ul>
+ <# } else { #>
+ <div class="attachment-media-view">
+ <button type="button" class="placeholder button-add-media"><?php echo esc_html( $this->l10n['add_media'] ); ?></button>
+ </div>
+ <# } #>
+ </script>
+ <?php
+ }
+
+ /**
+ * Whether the widget has content to show.
+ *
+ * @since 4.9.0
+ * @access protected
+ *
+ * @param array $instance Widget instance props.
+ * @return bool Whether widget has content.
+ */
+ protected function has_content( $instance ) {
+ if ( ! empty( $instance['ids'] ) ) {
+ $attachments = wp_parse_id_list( $instance['ids'] );
+ // Prime attachment post caches.
+ _prime_post_caches( $attachments, false, false );
+ foreach ( $attachments as $attachment ) {
+ if ( 'attachment' !== get_post_type( $attachment ) ) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-media-image.php b/wp-includes/widgets/class-wp-widget-media-image.php
new file mode 100644
index 0000000..2505f26
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-media-image.php
@@ -0,0 +1,404 @@
+<?php
+/**
+ * Widget API: WP_Widget_Media_Image class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.8.0
+ */
+
+/**
+ * Core class that implements an image widget.
+ *
+ * @since 4.8.0
+ *
+ * @see WP_Widget_Media
+ * @see WP_Widget
+ */
+class WP_Widget_Media_Image extends WP_Widget_Media {
+
+ /**
+ * Constructor.
+ *
+ * @since 4.8.0
+ */
+ public function __construct() {
+ parent::__construct(
+ 'media_image',
+ __( 'Image' ),
+ array(
+ 'description' => __( 'Displays an image.' ),
+ 'mime_type' => 'image',
+ )
+ );
+
+ $this->l10n = array_merge(
+ $this->l10n,
+ array(
+ 'no_media_selected' => __( 'No image selected' ),
+ 'add_media' => _x( 'Add Image', 'label for button in the image widget' ),
+ 'replace_media' => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
+ 'edit_media' => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
+ 'missing_attachment' => sprintf(
+ /* translators: %s: URL to media library. */
+ __( 'That image cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
+ esc_url( admin_url( 'upload.php' ) )
+ ),
+ /* translators: %d: Widget count. */
+ 'media_library_state_multi' => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
+ 'media_library_state_single' => __( 'Image Widget' ),
+ )
+ );
+ }
+
+ /**
+ * Get schema for properties of a widget instance (item).
+ *
+ * @since 4.8.0
+ *
+ * @see WP_REST_Controller::get_item_schema()
+ * @see WP_REST_Controller::get_additional_fields()
+ * @link https://core.trac.wordpress.org/ticket/35574
+ *
+ * @return array Schema for properties.
+ */
+ public function get_instance_schema() {
+ return array_merge(
+ array(
+ 'size' => array(
+ 'type' => 'string',
+ 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
+ 'default' => 'medium',
+ 'description' => __( 'Size' ),
+ ),
+ 'width' => array( // Via 'customWidth', only when size=custom; otherwise via 'width'.
+ 'type' => 'integer',
+ 'minimum' => 0,
+ 'default' => 0,
+ 'description' => __( 'Width' ),
+ ),
+ 'height' => array( // Via 'customHeight', only when size=custom; otherwise via 'height'.
+ 'type' => 'integer',
+ 'minimum' => 0,
+ 'default' => 0,
+ 'description' => __( 'Height' ),
+ ),
+
+ 'caption' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => 'wp_kses_post',
+ 'description' => __( 'Caption' ),
+ 'should_preview_update' => false,
+ ),
+ 'alt' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => 'sanitize_text_field',
+ 'description' => __( 'Alternative Text' ),
+ ),
+ 'link_type' => array(
+ 'type' => 'string',
+ 'enum' => array( 'none', 'file', 'post', 'custom' ),
+ 'default' => 'custom',
+ 'media_prop' => 'link',
+ 'description' => __( 'Link To' ),
+ 'should_preview_update' => true,
+ ),
+ 'link_url' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'format' => 'uri',
+ 'media_prop' => 'linkUrl',
+ 'description' => __( 'URL' ),
+ 'should_preview_update' => true,
+ ),
+ 'image_classes' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => array( $this, 'sanitize_token_list' ),
+ 'media_prop' => 'extraClasses',
+ 'description' => __( 'Image CSS Class' ),
+ 'should_preview_update' => false,
+ ),
+ 'link_classes' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => array( $this, 'sanitize_token_list' ),
+ 'media_prop' => 'linkClassName',
+ 'should_preview_update' => false,
+ 'description' => __( 'Link CSS Class' ),
+ ),
+ 'link_rel' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => array( $this, 'sanitize_token_list' ),
+ 'media_prop' => 'linkRel',
+ 'description' => __( 'Link Rel' ),
+ 'should_preview_update' => false,
+ ),
+ 'link_target_blank' => array(
+ 'type' => 'boolean',
+ 'default' => false,
+ 'media_prop' => 'linkTargetBlank',
+ 'description' => __( 'Open link in a new tab' ),
+ 'should_preview_update' => false,
+ ),
+ 'image_title' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => 'sanitize_text_field',
+ 'media_prop' => 'title',
+ 'description' => __( 'Image Title Attribute' ),
+ 'should_preview_update' => false,
+ ),
+
+ /*
+ * There are two additional properties exposed by the PostImage modal
+ * that don't seem to be relevant, as they may only be derived read-only
+ * values:
+ * - originalUrl
+ * - aspectRatio
+ * - height (redundant when size is not custom)
+ * - width (redundant when size is not custom)
+ */
+ ),
+ parent::get_instance_schema()
+ );
+ }
+
+ /**
+ * Render the media on the frontend.
+ *
+ * @since 4.8.0
+ *
+ * @param array $instance Widget instance props.
+ */
+ public function render_media( $instance ) {
+ $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
+ $instance = wp_parse_args(
+ $instance,
+ array(
+ 'size' => 'thumbnail',
+ )
+ );
+
+ $attachment = null;
+
+ if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
+ $attachment = get_post( $instance['attachment_id'] );
+ }
+
+ if ( $attachment ) {
+ $caption = '';
+ if ( ! isset( $instance['caption'] ) ) {
+ $caption = $attachment->post_excerpt;
+ } elseif ( trim( $instance['caption'] ) ) {
+ $caption = $instance['caption'];
+ }
+
+ $image_attributes = array(
+ 'class' => sprintf( 'image wp-image-%d %s', $attachment->ID, $instance['image_classes'] ),
+ 'style' => 'max-width: 100%; height: auto;',
+ );
+ if ( ! empty( $instance['image_title'] ) ) {
+ $image_attributes['title'] = $instance['image_title'];
+ }
+
+ if ( $instance['alt'] ) {
+ $image_attributes['alt'] = $instance['alt'];
+ }
+
+ $size = $instance['size'];
+
+ if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
+ $size = array( $instance['width'], $instance['height'] );
+ $width = $instance['width'];
+ } else {
+ $caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
+ $width = empty( $caption_size[0] ) ? 0 : $caption_size[0];
+ }
+
+ $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? implode( 'x', $size ) : $size );
+
+ $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
+
+ } else {
+ if ( empty( $instance['url'] ) ) {
+ return;
+ }
+
+ $instance['size'] = 'custom';
+ $caption = $instance['caption'];
+ $width = $instance['width'];
+ $classes = 'image ' . $instance['image_classes'];
+ if ( 0 === $instance['width'] ) {
+ $instance['width'] = '';
+ }
+ if ( 0 === $instance['height'] ) {
+ $instance['height'] = '';
+ }
+
+ $attr = array(
+ 'class' => $classes,
+ 'src' => $instance['url'],
+ 'alt' => $instance['alt'],
+ 'width' => $instance['width'],
+ 'height' => $instance['height'],
+ );
+
+ $loading_optimization_attr = wp_get_loading_optimization_attributes(
+ 'img',
+ $attr,
+ 'widget_media_image'
+ );
+
+ $attr = array_merge( $attr, $loading_optimization_attr );
+
+ $attr = array_map( 'esc_attr', $attr );
+ $image = '<img';
+
+ foreach ( $attr as $name => $value ) {
+ $image .= ' ' . $name . '="' . $value . '"';
+ }
+
+ $image .= ' />';
+ } // End if().
+
+ $url = '';
+ if ( 'file' === $instance['link_type'] ) {
+ $url = $attachment ? wp_get_attachment_url( $attachment->ID ) : $instance['url'];
+ } elseif ( $attachment && 'post' === $instance['link_type'] ) {
+ $url = get_attachment_link( $attachment->ID );
+ } elseif ( 'custom' === $instance['link_type'] && ! empty( $instance['link_url'] ) ) {
+ $url = $instance['link_url'];
+ }
+
+ if ( $url ) {
+ $link = sprintf( '<a href="%s"', esc_url( $url ) );
+ if ( ! empty( $instance['link_classes'] ) ) {
+ $link .= sprintf( ' class="%s"', esc_attr( $instance['link_classes'] ) );
+ }
+ if ( ! empty( $instance['link_rel'] ) ) {
+ $link .= sprintf( ' rel="%s"', esc_attr( $instance['link_rel'] ) );
+ }
+ if ( ! empty( $instance['link_target_blank'] ) ) {
+ $link .= ' target="_blank"';
+ }
+ $link .= '>';
+ $link .= $image;
+ $link .= '</a>';
+ $image = wp_targeted_link_rel( $link );
+ }
+
+ if ( $caption ) {
+ $image = img_caption_shortcode(
+ array(
+ 'width' => $width,
+ 'caption' => $caption,
+ ),
+ $image
+ );
+ }
+
+ echo $image;
+ }
+
+ /**
+ * Loads the required media files for the media manager and scripts for media widgets.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_admin_scripts() {
+ parent::enqueue_admin_scripts();
+
+ $handle = 'media-image-widget';
+ wp_enqueue_script( $handle );
+
+ $exported_schema = array();
+ foreach ( $this->get_instance_schema() as $field => $field_schema ) {
+ $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
+ }
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $exported_schema )
+ )
+ );
+
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ '
+ wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
+ wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
+ ',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $this->widget_options['mime_type'] ),
+ wp_json_encode( $this->l10n )
+ )
+ );
+ }
+
+ /**
+ * Render form template scripts.
+ *
+ * @since 4.8.0
+ */
+ public function render_control_template_scripts() {
+ parent::render_control_template_scripts();
+
+ ?>
+ <script type="text/html" id="tmpl-wp-media-widget-image-fields">
+ <# var elementIdPrefix = 'el' + String( Math.random() ) + '_'; #>
+ <# if ( data.url ) { #>
+ <p class="media-widget-image-link">
+ <label for="{{ elementIdPrefix }}linkUrl"><?php esc_html_e( 'Link to:' ); ?></label>
+ <input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link" value="{{ data.link_url }}" placeholder="https://" pattern="((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#).*">
+ </p>
+ <# } #>
+ </script>
+ <script type="text/html" id="tmpl-wp-media-widget-image-preview">
+ <# if ( data.error && 'missing_attachment' === data.error ) { #>
+ <?php
+ wp_admin_notice(
+ $this->l10n['missing_attachment'],
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'notice-missing-attachment' ),
+ )
+ );
+ ?>
+ <# } else if ( data.error ) { #>
+ <?php
+ wp_admin_notice(
+ __( 'Unable to preview media due to an unknown error.' ),
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt' ),
+ )
+ );
+ ?>
+ <# } else if ( data.url ) { #>
+ <img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}"
+ <# if ( ! data.alt && data.currentFilename ) { #>
+ aria-label="
+ <?php
+ echo esc_attr(
+ sprintf(
+ /* translators: %s: The image file name. */
+ __( 'The current image has no alternative text. The file name is: %s' ),
+ '{{ data.currentFilename }}'
+ )
+ );
+ ?>
+ "
+ <# } #>
+ />
+ <# } #>
+ </script>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-media-video.php b/wp-includes/widgets/class-wp-widget-media-video.php
new file mode 100644
index 0000000..fafb45f
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-media-video.php
@@ -0,0 +1,274 @@
+<?php
+/**
+ * Widget API: WP_Widget_Media_Video class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.8.0
+ */
+
+/**
+ * Core class that implements a video widget.
+ *
+ * @since 4.8.0
+ *
+ * @see WP_Widget_Media
+ * @see WP_Widget
+ */
+class WP_Widget_Media_Video extends WP_Widget_Media {
+
+ /**
+ * Constructor.
+ *
+ * @since 4.8.0
+ */
+ public function __construct() {
+ parent::__construct(
+ 'media_video',
+ __( 'Video' ),
+ array(
+ 'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ),
+ 'mime_type' => 'video',
+ )
+ );
+
+ $this->l10n = array_merge(
+ $this->l10n,
+ array(
+ 'no_media_selected' => __( 'No video selected' ),
+ 'add_media' => _x( 'Add Video', 'label for button in the video widget' ),
+ 'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
+ 'edit_media' => _x( 'Edit Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
+ 'missing_attachment' => sprintf(
+ /* translators: %s: URL to media library. */
+ __( 'That video cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
+ esc_url( admin_url( 'upload.php' ) )
+ ),
+ /* translators: %d: Widget count. */
+ 'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ),
+ 'media_library_state_single' => __( 'Video Widget' ),
+ /* translators: %s: A list of valid video file extensions. */
+ 'unsupported_file_type' => sprintf( __( 'Sorry, the video at the supplied URL cannot be loaded. Please check that the URL is for a supported video file (%s) or stream (e.g. YouTube and Vimeo).' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ),
+ )
+ );
+ }
+
+ /**
+ * Get schema for properties of a widget instance (item).
+ *
+ * @since 4.8.0
+ *
+ * @see WP_REST_Controller::get_item_schema()
+ * @see WP_REST_Controller::get_additional_fields()
+ * @link https://core.trac.wordpress.org/ticket/35574
+ *
+ * @return array Schema for properties.
+ */
+ public function get_instance_schema() {
+
+ $schema = array(
+ 'preload' => array(
+ 'type' => 'string',
+ 'enum' => array( 'none', 'auto', 'metadata' ),
+ 'default' => 'metadata',
+ 'description' => __( 'Preload' ),
+ 'should_preview_update' => false,
+ ),
+ 'loop' => array(
+ 'type' => 'boolean',
+ 'default' => false,
+ 'description' => __( 'Loop' ),
+ 'should_preview_update' => false,
+ ),
+ 'content' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => 'wp_kses_post',
+ 'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ),
+ 'should_preview_update' => false,
+ ),
+ );
+
+ foreach ( wp_get_video_extensions() as $video_extension ) {
+ $schema[ $video_extension ] = array(
+ 'type' => 'string',
+ 'default' => '',
+ 'format' => 'uri',
+ /* translators: %s: Video extension. */
+ 'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ),
+ );
+ }
+
+ return array_merge( $schema, parent::get_instance_schema() );
+ }
+
+ /**
+ * Render the media on the frontend.
+ *
+ * @since 4.8.0
+ *
+ * @param array $instance Widget instance props.
+ */
+ public function render_media( $instance ) {
+ $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
+ $attachment = null;
+
+ if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
+ $attachment = get_post( $instance['attachment_id'] );
+ }
+
+ $src = $instance['url'];
+ if ( $attachment ) {
+ $src = wp_get_attachment_url( $attachment->ID );
+ }
+
+ if ( empty( $src ) ) {
+ return;
+ }
+
+ $youtube_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
+ $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
+
+ if ( $attachment || preg_match( $youtube_pattern, $src ) || preg_match( $vimeo_pattern, $src ) ) {
+ add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
+
+ echo wp_video_shortcode(
+ array_merge(
+ $instance,
+ compact( 'src' )
+ ),
+ $instance['content']
+ );
+
+ remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
+ } else {
+ echo $this->inject_video_max_width_style( wp_oembed_get( $src ) );
+ }
+ }
+
+ /**
+ * Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
+ *
+ * @since 4.8.0
+ *
+ * @param string $html Video shortcode HTML output.
+ * @return string HTML Output.
+ */
+ public function inject_video_max_width_style( $html ) {
+ $html = preg_replace( '/\sheight="\d+"/', '', $html );
+ $html = preg_replace( '/\swidth="\d+"/', '', $html );
+ $html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
+ return $html;
+ }
+
+ /**
+ * Enqueue preview scripts.
+ *
+ * These scripts normally are enqueued just-in-time when a video shortcode is used.
+ * In the customizer, however, widgets can be dynamically added and rendered via
+ * selective refresh, and so it is important to unconditionally enqueue them in
+ * case a widget does get added.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_preview_scripts() {
+ /** This filter is documented in wp-includes/media.php */
+ if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
+ wp_enqueue_style( 'wp-mediaelement' );
+ wp_enqueue_script( 'mediaelement-vimeo' );
+ wp_enqueue_script( 'wp-mediaelement' );
+ }
+ }
+
+ /**
+ * Loads the required scripts and styles for the widget control.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_admin_scripts() {
+ parent::enqueue_admin_scripts();
+
+ $handle = 'media-video-widget';
+ wp_enqueue_script( $handle );
+
+ $exported_schema = array();
+ foreach ( $this->get_instance_schema() as $field => $field_schema ) {
+ $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
+ }
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $exported_schema )
+ )
+ );
+
+ wp_add_inline_script(
+ $handle,
+ sprintf(
+ '
+ wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
+ wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
+ ',
+ wp_json_encode( $this->id_base ),
+ wp_json_encode( $this->widget_options['mime_type'] ),
+ wp_json_encode( $this->l10n )
+ )
+ );
+ }
+
+ /**
+ * Render form template scripts.
+ *
+ * @since 4.8.0
+ */
+ public function render_control_template_scripts() {
+ parent::render_control_template_scripts()
+ ?>
+ <script type="text/html" id="tmpl-wp-media-widget-video-preview">
+ <# if ( data.error && 'missing_attachment' === data.error ) { #>
+ <?php
+ wp_admin_notice(
+ $this->l10n['missing_attachment'],
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'notice-missing-attachment' ),
+ )
+ );
+ ?>
+ <# } else if ( data.error && 'unsupported_file_type' === data.error ) { #>
+ <?php
+ wp_admin_notice(
+ $this->l10n['unsupported_file_type'],
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'notice-missing-attachment' ),
+ )
+ );
+ ?>
+ <# } else if ( data.error ) { #>
+ <?php
+ wp_admin_notice(
+ __( 'Unable to preview media due to an unknown error.' ),
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt' ),
+ )
+ );
+ ?>
+ <# } else if ( data.is_oembed && data.model.poster ) { #>
+ <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link">
+ <img src="{{ data.model.poster }}" />
+ </a>
+ <# } else if ( data.is_oembed ) { #>
+ <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link no-poster">
+ <span class="dashicons dashicons-format-video"></span>
+ </a>
+ <# } else if ( data.model.src ) { #>
+ <?php wp_underscore_video_template(); ?>
+ <# } #>
+ </script>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-media.php b/wp-includes/widgets/class-wp-widget-media.php
new file mode 100644
index 0000000..2352ae8
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-media.php
@@ -0,0 +1,516 @@
+<?php
+/**
+ * Widget API: WP_Media_Widget class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.8.0
+ */
+
+/**
+ * Core class that implements a media widget.
+ *
+ * @since 4.8.0
+ *
+ * @see WP_Widget
+ */
+abstract class WP_Widget_Media extends WP_Widget {
+
+ /**
+ * Translation labels.
+ *
+ * @since 4.8.0
+ * @var array
+ */
+ public $l10n = array(
+ 'add_to_widget' => '',
+ 'replace_media' => '',
+ 'edit_media' => '',
+ 'media_library_state_multi' => '',
+ 'media_library_state_single' => '',
+ 'missing_attachment' => '',
+ 'no_media_selected' => '',
+ 'add_media' => '',
+ );
+
+ /**
+ * Whether or not the widget has been registered yet.
+ *
+ * @since 4.8.1
+ * @var bool
+ */
+ protected $registered = false;
+
+ /**
+ * The default widget description.
+ *
+ * @since 6.0.0
+ * @var string
+ */
+ protected static $default_description = '';
+
+ /**
+ * The default localized strings used by the widget.
+ *
+ * @since 6.0.0
+ * @var string[]
+ */
+ protected static $l10n_defaults = array();
+
+ /**
+ * Constructor.
+ *
+ * @since 4.8.0
+ *
+ * @param string $id_base Base ID for the widget, lowercase and unique.
+ * @param string $name Name for the widget displayed on the configuration page.
+ * @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for
+ * information on accepted arguments. Default empty array.
+ * @param array $control_options Optional. Widget control options. See wp_register_widget_control()
+ * for information on accepted arguments. Default empty array.
+ */
+ public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
+ $widget_opts = wp_parse_args(
+ $widget_options,
+ array(
+ 'description' => self::get_default_description(),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ 'mime_type' => '',
+ )
+ );
+
+ $control_opts = wp_parse_args( $control_options, array() );
+
+ $this->l10n = array_merge( self::get_l10n_defaults(), array_filter( $this->l10n ) );
+
+ parent::__construct(
+ $id_base,
+ $name,
+ $widget_opts,
+ $control_opts
+ );
+ }
+
+ /**
+ * Add hooks while registering all widget instances of this widget class.
+ *
+ * @since 4.8.0
+ *
+ * @param int $number Optional. The unique order number of this widget instance
+ * compared to other instances of the same class. Default -1.
+ */
+ public function _register_one( $number = -1 ) {
+ parent::_register_one( $number );
+ if ( $this->registered ) {
+ return;
+ }
+ $this->registered = true;
+
+ /*
+ * Note that the widgets component in the customizer will also do
+ * the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
+ */
+ add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
+
+ if ( $this->is_preview() ) {
+ add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
+ }
+
+ /*
+ * Note that the widgets component in the customizer will also do
+ * the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
+ */
+ add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) );
+
+ add_filter( 'display_media_states', array( $this, 'display_media_state' ), 10, 2 );
+ }
+
+ /**
+ * Get schema for properties of a widget instance (item).
+ *
+ * @since 4.8.0
+ *
+ * @see WP_REST_Controller::get_item_schema()
+ * @see WP_REST_Controller::get_additional_fields()
+ * @link https://core.trac.wordpress.org/ticket/35574
+ *
+ * @return array Schema for properties.
+ */
+ public function get_instance_schema() {
+ $schema = array(
+ 'attachment_id' => array(
+ 'type' => 'integer',
+ 'default' => 0,
+ 'minimum' => 0,
+ 'description' => __( 'Attachment post ID' ),
+ 'media_prop' => 'id',
+ ),
+ 'url' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'format' => 'uri',
+ 'description' => __( 'URL to the media file' ),
+ ),
+ 'title' => array(
+ 'type' => 'string',
+ 'default' => '',
+ 'sanitize_callback' => 'sanitize_text_field',
+ 'description' => __( 'Title for the widget' ),
+ 'should_preview_update' => false,
+ ),
+ );
+
+ /**
+ * Filters the media widget instance schema to add additional properties.
+ *
+ * @since 4.9.0
+ *
+ * @param array $schema Instance schema.
+ * @param WP_Widget_Media $widget Widget object.
+ */
+ $schema = apply_filters( "widget_{$this->id_base}_instance_schema", $schema, $this );
+
+ return $schema;
+ }
+
+ /**
+ * Determine if the supplied attachment is for a valid attachment post with the specified MIME type.
+ *
+ * @since 4.8.0
+ *
+ * @param int|WP_Post $attachment Attachment post ID or object.
+ * @param string $mime_type MIME type.
+ * @return bool Is matching MIME type.
+ */
+ public function is_attachment_with_mime_type( $attachment, $mime_type ) {
+ if ( empty( $attachment ) ) {
+ return false;
+ }
+ $attachment = get_post( $attachment );
+ if ( ! $attachment ) {
+ return false;
+ }
+ if ( 'attachment' !== $attachment->post_type ) {
+ return false;
+ }
+ return wp_attachment_is( $mime_type, $attachment );
+ }
+
+ /**
+ * Sanitize a token list string, such as used in HTML rel and class attributes.
+ *
+ * @since 4.8.0
+ *
+ * @link http://w3c.github.io/html/infrastructure.html#space-separated-tokens
+ * @link https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList
+ * @param string|array $tokens List of tokens separated by spaces, or an array of tokens.
+ * @return string Sanitized token string list.
+ */
+ public function sanitize_token_list( $tokens ) {
+ if ( is_string( $tokens ) ) {
+ $tokens = preg_split( '/\s+/', trim( $tokens ) );
+ }
+ $tokens = array_map( 'sanitize_html_class', $tokens );
+ $tokens = array_filter( $tokens );
+ return implode( ' ', $tokens );
+ }
+
+ /**
+ * Displays the widget on the front-end.
+ *
+ * @since 4.8.0
+ *
+ * @see WP_Widget::widget()
+ *
+ * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
+ * @param array $instance Saved setting from the database.
+ */
+ public function widget( $args, $instance ) {
+ $instance = wp_parse_args( $instance, wp_list_pluck( $this->get_instance_schema(), 'default' ) );
+
+ // Short-circuit if no media is selected.
+ if ( ! $this->has_content( $instance ) ) {
+ return;
+ }
+
+ echo $args['before_widget'];
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
+
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ /**
+ * Filters the media widget instance prior to rendering the media.
+ *
+ * @since 4.8.0
+ *
+ * @param array $instance Instance data.
+ * @param array $args Widget args.
+ * @param WP_Widget_Media $widget Widget object.
+ */
+ $instance = apply_filters( "widget_{$this->id_base}_instance", $instance, $args, $this );
+
+ $this->render_media( $instance );
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Sanitizes the widget form values as they are saved.
+ *
+ * @since 4.8.0
+ * @since 5.9.0 Renamed `$instance` to `$old_instance` to match parent class
+ * for PHP 8 named parameter support.
+ *
+ * @see WP_Widget::update()
+ * @see WP_REST_Request::has_valid_params()
+ * @see WP_REST_Request::sanitize_params()
+ *
+ * @param array $new_instance Values just sent to be saved.
+ * @param array $old_instance Previously saved values from database.
+ * @return array Updated safe values to be saved.
+ */
+ public function update( $new_instance, $old_instance ) {
+
+ $schema = $this->get_instance_schema();
+ foreach ( $schema as $field => $field_schema ) {
+ if ( ! array_key_exists( $field, $new_instance ) ) {
+ continue;
+ }
+ $value = $new_instance[ $field ];
+
+ /*
+ * Workaround for rest_validate_value_from_schema() due to the fact that
+ * rest_is_boolean( '' ) === false, while rest_is_boolean( '1' ) is true.
+ */
+ if ( 'boolean' === $field_schema['type'] && '' === $value ) {
+ $value = false;
+ }
+
+ if ( true !== rest_validate_value_from_schema( $value, $field_schema, $field ) ) {
+ continue;
+ }
+
+ $value = rest_sanitize_value_from_schema( $value, $field_schema );
+
+ // @codeCoverageIgnoreStart
+ if ( is_wp_error( $value ) ) {
+ continue; // Handle case when rest_sanitize_value_from_schema() ever returns WP_Error as its phpdoc @return tag indicates.
+ }
+
+ // @codeCoverageIgnoreEnd
+ if ( isset( $field_schema['sanitize_callback'] ) ) {
+ $value = call_user_func( $field_schema['sanitize_callback'], $value );
+ }
+ if ( is_wp_error( $value ) ) {
+ continue;
+ }
+ $old_instance[ $field ] = $value;
+ }
+
+ return $old_instance;
+ }
+
+ /**
+ * Render the media on the frontend.
+ *
+ * @since 4.8.0
+ *
+ * @param array $instance Widget instance props.
+ */
+ abstract public function render_media( $instance );
+
+ /**
+ * Outputs the settings update form.
+ *
+ * Note that the widget UI itself is rendered with JavaScript via `MediaWidgetControl#render()`.
+ *
+ * @since 4.8.0
+ *
+ * @see \WP_Widget_Media::render_control_template_scripts() Where the JS template is located.
+ *
+ * @param array $instance Current settings.
+ */
+ final public function form( $instance ) {
+ $instance_schema = $this->get_instance_schema();
+ $instance = wp_array_slice_assoc(
+ wp_parse_args( (array) $instance, wp_list_pluck( $instance_schema, 'default' ) ),
+ array_keys( $instance_schema )
+ );
+
+ foreach ( $instance as $name => $value ) : ?>
+ <input
+ type="hidden"
+ data-property="<?php echo esc_attr( $name ); ?>"
+ class="media-widget-instance-property"
+ name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
+ id="<?php echo esc_attr( $this->get_field_id( $name ) ); // Needed specifically by wpWidgets.appendTitle(). ?>"
+ value="<?php echo esc_attr( is_array( $value ) ? implode( ',', $value ) : (string) $value ); ?>"
+ />
+ <?php
+ endforeach;
+ }
+
+ /**
+ * Filters the default media display states for items in the Media list table.
+ *
+ * @since 4.8.0
+ *
+ * @param array $states An array of media states.
+ * @param WP_Post $post The current attachment object.
+ * @return array
+ */
+ public function display_media_state( $states, $post = null ) {
+ if ( ! $post ) {
+ $post = get_post();
+ }
+
+ // Count how many times this attachment is used in widgets.
+ $use_count = 0;
+ foreach ( $this->get_settings() as $instance ) {
+ if ( isset( $instance['attachment_id'] ) && $instance['attachment_id'] === $post->ID ) {
+ ++$use_count;
+ }
+ }
+
+ if ( 1 === $use_count ) {
+ $states[] = $this->l10n['media_library_state_single'];
+ } elseif ( $use_count > 0 ) {
+ $states[] = sprintf( translate_nooped_plural( $this->l10n['media_library_state_multi'], $use_count ), number_format_i18n( $use_count ) );
+ }
+
+ return $states;
+ }
+
+ /**
+ * Enqueue preview scripts.
+ *
+ * These scripts normally are enqueued just-in-time when a widget is rendered.
+ * In the customizer, however, widgets can be dynamically added and rendered via
+ * selective refresh, and so it is important to unconditionally enqueue them in
+ * case a widget does get added.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_preview_scripts() {}
+
+ /**
+ * Loads the required scripts and styles for the widget control.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_admin_scripts() {
+ wp_enqueue_media();
+ wp_enqueue_script( 'media-widgets' );
+ }
+
+ /**
+ * Render form template scripts.
+ *
+ * @since 4.8.0
+ */
+ public function render_control_template_scripts() {
+ ?>
+ <script type="text/html" id="tmpl-widget-media-<?php echo esc_attr( $this->id_base ); ?>-control">
+ <# var elementIdPrefix = 'el' + String( Math.random() ) + '_' #>
+ <p>
+ <label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
+ <input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
+ </p>
+ <div class="media-widget-preview <?php echo esc_attr( $this->id_base ); ?>">
+ <div class="attachment-media-view">
+ <button type="button" class="select-media button-add-media not-selected">
+ <?php echo esc_html( $this->l10n['add_media'] ); ?>
+ </button>
+ </div>
+ </div>
+ <p class="media-widget-buttons">
+ <button type="button" class="button edit-media selected">
+ <?php echo esc_html( $this->l10n['edit_media'] ); ?>
+ </button>
+ <?php if ( ! empty( $this->l10n['replace_media'] ) ) : ?>
+ <button type="button" class="button change-media select-media selected">
+ <?php echo esc_html( $this->l10n['replace_media'] ); ?>
+ </button>
+ <?php endif; ?>
+ </p>
+ <div class="media-widget-fields">
+ </div>
+ </script>
+ <?php
+ }
+
+ /**
+ * Resets the cache for the default labels.
+ *
+ * @since 6.0.0
+ */
+ public static function reset_default_labels() {
+ self::$default_description = '';
+ self::$l10n_defaults = array();
+ }
+
+ /**
+ * Whether the widget has content to show.
+ *
+ * @since 4.8.0
+ *
+ * @param array $instance Widget instance props.
+ * @return bool Whether widget has content.
+ */
+ protected function has_content( $instance ) {
+ return ( $instance['attachment_id'] && 'attachment' === get_post_type( $instance['attachment_id'] ) ) || $instance['url'];
+ }
+
+ /**
+ * Returns the default description of the widget.
+ *
+ * @since 6.0.0
+ *
+ * @return string
+ */
+ protected static function get_default_description() {
+ if ( self::$default_description ) {
+ return self::$default_description;
+ }
+
+ self::$default_description = __( 'A media item.' );
+ return self::$default_description;
+ }
+
+ /**
+ * Returns the default localized strings used by the widget.
+ *
+ * @since 6.0.0
+ *
+ * @return (string|array)[]
+ */
+ protected static function get_l10n_defaults() {
+ if ( ! empty( self::$l10n_defaults ) ) {
+ return self::$l10n_defaults;
+ }
+
+ self::$l10n_defaults = array(
+ 'no_media_selected' => __( 'No media selected' ),
+ 'add_media' => _x( 'Add Media', 'label for button in the media widget' ),
+ 'replace_media' => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
+ 'edit_media' => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
+ 'add_to_widget' => __( 'Add to Widget' ),
+ 'missing_attachment' => sprintf(
+ /* translators: %s: URL to media library. */
+ __( 'That file cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
+ esc_url( admin_url( 'upload.php' ) )
+ ),
+ /* translators: %d: Widget count. */
+ 'media_library_state_multi' => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
+ 'media_library_state_single' => __( 'Media Widget' ),
+ 'unsupported_file_type' => __( 'Looks like this is not the correct kind of file. Please link to an appropriate file instead.' ),
+ );
+
+ return self::$l10n_defaults;
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-meta.php b/wp-includes/widgets/class-wp-widget-meta.php
new file mode 100644
index 0000000..2ab2857
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-meta.php
@@ -0,0 +1,143 @@
+<?php
+/**
+ * Widget API: WP_Widget_Meta class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Meta widget.
+ *
+ * Displays log in/out, RSS feed links, etc.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Meta extends WP_Widget {
+
+ /**
+ * Sets up a new Meta widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_meta',
+ 'description' => __( 'Login, RSS, &amp; WordPress.org links.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'meta', __( 'Meta' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Meta widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Meta widget instance.
+ */
+ public function widget( $args, $instance ) {
+ $default_title = __( 'Meta' );
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ echo $args['before_widget'];
+
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+ echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+ ?>
+
+ <ul>
+ <?php wp_register(); ?>
+ <li><?php wp_loginout(); ?></li>
+ <li><a href="<?php echo esc_url( get_bloginfo( 'rss2_url' ) ); ?>"><?php _e( 'Entries feed' ); ?></a></li>
+ <li><a href="<?php echo esc_url( get_bloginfo( 'comments_rss2_url' ) ); ?>"><?php _e( 'Comments feed' ); ?></a></li>
+
+ <?php
+ /**
+ * Filters the "WordPress.org" list item HTML in the Meta widget.
+ *
+ * @since 3.6.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @param string $html Default HTML for the WordPress.org list item.
+ * @param array $instance Array of settings for the current widget.
+ */
+ echo apply_filters(
+ 'widget_meta_poweredby',
+ sprintf(
+ '<li><a href="%1$s">%2$s</a></li>',
+ esc_url( __( 'https://wordpress.org/' ) ),
+ __( 'WordPress.org' )
+ ),
+ $instance
+ );
+
+ wp_meta();
+ ?>
+
+ </ul>
+
+ <?php
+ if ( 'html5' === $format ) {
+ echo '</nav>';
+ }
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Handles updating settings for the current Meta widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Meta widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
+ </p>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-pages.php b/wp-includes/widgets/class-wp-widget-pages.php
new file mode 100644
index 0000000..36da121
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-pages.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * Widget API: WP_Widget_Pages class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Pages widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Pages extends WP_Widget {
+
+ /**
+ * Sets up a new Pages widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_pages',
+ 'description' => __( 'A list of your site&#8217;s Pages.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'pages', __( 'Pages' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Pages widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Pages widget instance.
+ */
+ public function widget( $args, $instance ) {
+ $default_title = __( 'Pages' );
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
+
+ /**
+ * Filters the widget title.
+ *
+ * @since 2.6.0
+ *
+ * @param string $title The widget title. Default 'Pages'.
+ * @param array $instance Array of settings for the current widget.
+ * @param mixed $id_base The widget ID.
+ */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
+ $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
+
+ if ( 'menu_order' === $sortby ) {
+ $sortby = 'menu_order, post_title';
+ }
+
+ $output = wp_list_pages(
+ /**
+ * Filters the arguments for the Pages widget.
+ *
+ * @since 2.8.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @see wp_list_pages()
+ *
+ * @param array $args An array of arguments to retrieve the pages list.
+ * @param array $instance Array of settings for the current widget.
+ */
+ apply_filters(
+ 'widget_pages_args',
+ array(
+ 'title_li' => '',
+ 'echo' => 0,
+ 'sort_column' => $sortby,
+ 'exclude' => $exclude,
+ ),
+ $instance
+ )
+ );
+
+ if ( ! empty( $output ) ) {
+ echo $args['before_widget'];
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+ echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+ ?>
+
+ <ul>
+ <?php echo $output; ?>
+ </ul>
+
+ <?php
+ if ( 'html5' === $format ) {
+ echo '</nav>';
+ }
+
+ echo $args['after_widget'];
+ }
+ }
+
+ /**
+ * Handles updating settings for the current Pages widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ), true ) ) {
+ $instance['sortby'] = $new_instance['sortby'];
+ } else {
+ $instance['sortby'] = 'menu_order';
+ }
+
+ $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
+
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Pages widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ // Defaults.
+ $instance = wp_parse_args(
+ (array) $instance,
+ array(
+ 'sortby' => 'post_title',
+ 'title' => '',
+ 'exclude' => '',
+ )
+ );
+ ?>
+ <p>
+ <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
+ </p>
+
+ <p>
+ <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
+ <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
+ <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e( 'Page title' ); ?></option>
+ <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e( 'Page order' ); ?></option>
+ <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
+ </select>
+ </p>
+
+ <p>
+ <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
+ <input type="text" value="<?php echo esc_attr( $instance['exclude'] ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" class="widefat" />
+ <br />
+ <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
+ </p>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-recent-comments.php b/wp-includes/widgets/class-wp-widget-recent-comments.php
new file mode 100644
index 0000000..5d92938
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-recent-comments.php
@@ -0,0 +1,218 @@
+<?php
+/**
+ * Widget API: WP_Widget_Recent_Comments class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Recent Comments widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Recent_Comments extends WP_Widget {
+
+ /**
+ * Sets up a new Recent Comments widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_recent_comments',
+ 'description' => __( 'Your site&#8217;s most recent comments.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'recent-comments', __( 'Recent Comments' ), $widget_ops );
+ $this->alt_option_name = 'widget_recent_comments';
+
+ if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
+ add_action( 'wp_head', array( $this, 'recent_comments_style' ) );
+ }
+ }
+
+ /**
+ * Outputs the default styles for the Recent Comments widget.
+ *
+ * @since 2.8.0
+ */
+ public function recent_comments_style() {
+ /**
+ * Filters the Recent Comments default widget styles.
+ *
+ * @since 3.1.0
+ *
+ * @param bool $active Whether the widget is active. Default true.
+ * @param string $id_base The widget ID.
+ */
+ if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876.
+ || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) ) {
+ return;
+ }
+
+ $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
+
+ printf(
+ '<style%s>.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>',
+ $type_attr
+ );
+ }
+
+ /**
+ * Outputs the content for the current Recent Comments widget instance.
+ *
+ * @since 2.8.0
+ * @since 5.4.0 Creates a unique HTML ID for the `<ul>` element
+ * if more than one instance is displayed on the page.
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Recent Comments widget instance.
+ */
+ public function widget( $args, $instance ) {
+ static $first_instance = true;
+
+ if ( ! isset( $args['widget_id'] ) ) {
+ $args['widget_id'] = $this->id;
+ }
+
+ $output = '';
+
+ $default_title = __( 'Recent Comments' );
+ $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title;
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
+ if ( ! $number ) {
+ $number = 5;
+ }
+
+ $comments = get_comments(
+ /**
+ * Filters the arguments for the Recent Comments widget.
+ *
+ * @since 3.4.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @see WP_Comment_Query::query() for information on accepted arguments.
+ *
+ * @param array $comment_args An array of arguments used to retrieve the recent comments.
+ * @param array $instance Array of settings for the current widget.
+ */
+ apply_filters(
+ 'widget_comments_args',
+ array(
+ 'number' => $number,
+ 'status' => 'approve',
+ 'post_status' => 'publish',
+ ),
+ $instance
+ )
+ );
+
+ $output .= $args['before_widget'];
+ if ( $title ) {
+ $output .= $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $recent_comments_id = ( $first_instance ) ? 'recentcomments' : "recentcomments-{$this->number}";
+ $first_instance = false;
+
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+ $output .= '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+
+ $output .= '<ul id="' . esc_attr( $recent_comments_id ) . '">';
+ if ( is_array( $comments ) && $comments ) {
+ // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
+ $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
+ _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
+
+ foreach ( (array) $comments as $comment ) {
+ $output .= '<li class="recentcomments">';
+ $output .= sprintf(
+ /* translators: Comments widget. 1: Comment author, 2: Post link. */
+ _x( '%1$s on %2$s', 'widgets' ),
+ '<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',
+ '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'
+ );
+ $output .= '</li>';
+ }
+ }
+ $output .= '</ul>';
+
+ if ( 'html5' === $format ) {
+ $output .= '</nav>';
+ }
+
+ $output .= $args['after_widget'];
+
+ echo $output;
+ }
+
+ /**
+ * Handles updating settings for the current Recent Comments widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ $instance['number'] = absint( $new_instance['number'] );
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Recent Comments widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $title = isset( $instance['title'] ) ? $instance['title'] : '';
+ $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
+ </p>
+
+ <p>
+ <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label>
+ <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" />
+ </p>
+ <?php
+ }
+
+ /**
+ * Flushes the Recent Comments widget cache.
+ *
+ * @since 2.8.0
+ *
+ * @deprecated 4.4.0 Fragment caching was removed in favor of split queries.
+ */
+ public function flush_widget_cache() {
+ _deprecated_function( __METHOD__, '4.4.0' );
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-recent-posts.php b/wp-includes/widgets/class-wp-widget-recent-posts.php
new file mode 100644
index 0000000..c73bb5b
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-recent-posts.php
@@ -0,0 +1,184 @@
+<?php
+/**
+ * Widget API: WP_Widget_Recent_Posts class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Recent Posts widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Recent_Posts extends WP_Widget {
+
+ /**
+ * Sets up a new Recent Posts widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_recent_entries',
+ 'description' => __( 'Your site&#8217;s most recent Posts.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops );
+ $this->alt_option_name = 'widget_recent_entries';
+ }
+
+ /**
+ * Outputs the content for the current Recent Posts widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Recent Posts widget instance.
+ */
+ public function widget( $args, $instance ) {
+ if ( ! isset( $args['widget_id'] ) ) {
+ $args['widget_id'] = $this->id;
+ }
+
+ $default_title = __( 'Recent Posts' );
+ $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title;
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
+ if ( ! $number ) {
+ $number = 5;
+ }
+ $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
+
+ $r = new WP_Query(
+ /**
+ * Filters the arguments for the Recent Posts widget.
+ *
+ * @since 3.4.0
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @see WP_Query::get_posts()
+ *
+ * @param array $args An array of arguments used to retrieve the recent posts.
+ * @param array $instance Array of settings for the current widget.
+ */
+ apply_filters(
+ 'widget_posts_args',
+ array(
+ 'posts_per_page' => $number,
+ 'no_found_rows' => true,
+ 'post_status' => 'publish',
+ 'ignore_sticky_posts' => true,
+ ),
+ $instance
+ )
+ );
+
+ if ( ! $r->have_posts() ) {
+ return;
+ }
+ ?>
+
+ <?php echo $args['before_widget']; ?>
+
+ <?php
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+ echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+ ?>
+
+ <ul>
+ <?php foreach ( $r->posts as $recent_post ) : ?>
+ <?php
+ $post_title = get_the_title( $recent_post->ID );
+ $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
+ $aria_current = '';
+
+ if ( get_queried_object_id() === $recent_post->ID ) {
+ $aria_current = ' aria-current="page"';
+ }
+ ?>
+ <li>
+ <a href="<?php the_permalink( $recent_post->ID ); ?>"<?php echo $aria_current; ?>><?php echo $title; ?></a>
+ <?php if ( $show_date ) : ?>
+ <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span>
+ <?php endif; ?>
+ </li>
+ <?php endforeach; ?>
+ </ul>
+
+ <?php
+ if ( 'html5' === $format ) {
+ echo '</nav>';
+ }
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Handles updating the settings for the current Recent Posts widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ $instance['number'] = (int) $new_instance['number'];
+ $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
+ return $instance;
+ }
+
+ /**
+ * Outputs the settings form for the Recent Posts widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
+ $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
+ $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
+ </p>
+
+ <p>
+ <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
+ <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" />
+ </p>
+
+ <p>
+ <input class="checkbox" type="checkbox"<?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
+ <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label>
+ </p>
+ <?php
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-rss.php b/wp-includes/widgets/class-wp-widget-rss.php
new file mode 100644
index 0000000..a4a1b19
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-rss.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * Widget API: WP_Widget_RSS class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a RSS widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_RSS extends WP_Widget {
+
+ /**
+ * Sets up a new RSS widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'description' => __( 'Entries from any RSS or Atom feed.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+
+ );
+ $control_ops = array(
+ 'width' => 400,
+ 'height' => 200,
+ );
+ parent::__construct( 'rss', __( 'RSS' ), $widget_ops, $control_ops );
+ }
+
+ /**
+ * Outputs the content for the current RSS widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current RSS widget instance.
+ */
+ public function widget( $args, $instance ) {
+ if ( isset( $instance['error'] ) && $instance['error'] ) {
+ return;
+ }
+
+ $url = ! empty( $instance['url'] ) ? $instance['url'] : '';
+ while ( ! empty( $url ) && stristr( $url, 'http' ) !== $url ) {
+ $url = substr( $url, 1 );
+ }
+
+ if ( empty( $url ) ) {
+ return;
+ }
+
+ // Self-URL destruction sequence.
+ if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ), true ) ) {
+ return;
+ }
+
+ $rss = fetch_feed( $url );
+ $title = $instance['title'];
+ $desc = '';
+ $link = '';
+
+ if ( ! is_wp_error( $rss ) ) {
+ $desc = esc_attr( strip_tags( html_entity_decode( $rss->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ) ) );
+ if ( empty( $title ) ) {
+ $title = strip_tags( $rss->get_title() );
+ }
+ $link = strip_tags( $rss->get_permalink() );
+ while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
+ $link = substr( $link, 1 );
+ }
+ }
+
+ if ( empty( $title ) ) {
+ $title = ! empty( $desc ) ? $desc : __( 'Unknown Feed' );
+ }
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ if ( $title ) {
+ $feed_link = '';
+ $feed_url = strip_tags( $url );
+ $feed_icon = includes_url( 'images/rss.png' );
+ $feed_link = sprintf(
+ '<a class="rsswidget rss-widget-feed" href="%1$s"><img class="rss-widget-icon" style="border:0" width="14" height="14" src="%2$s" alt="%3$s"%4$s /></a> ',
+ esc_url( $feed_url ),
+ esc_url( $feed_icon ),
+ esc_attr__( 'RSS' ),
+ ( wp_lazy_loading_enabled( 'img', 'rss_widget_feed_icon' ) ? ' loading="lazy"' : '' )
+ );
+
+ /**
+ * Filters the classic RSS widget's feed icon link.
+ *
+ * Themes can remove the icon link by using `add_filter( 'rss_widget_feed_link', '__return_empty_string' );`.
+ *
+ * @since 5.9.0
+ *
+ * @param string|false $feed_link HTML for link to RSS feed.
+ * @param array $instance Array of settings for the current widget.
+ */
+ $feed_link = apply_filters( 'rss_widget_feed_link', $feed_link, $instance );
+
+ $title = $feed_link . '<a class="rsswidget rss-widget-title" href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>';
+ }
+
+ echo $args['before_widget'];
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : __( 'RSS Feed' );
+ echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+
+ wp_widget_rss_output( $rss, $instance );
+
+ if ( 'html5' === $format ) {
+ echo '</nav>';
+ }
+
+ echo $args['after_widget'];
+
+ if ( ! is_wp_error( $rss ) ) {
+ $rss->__destruct();
+ }
+ unset( $rss );
+ }
+
+ /**
+ * Handles updating settings for the current RSS widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings to save.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $testurl = ( isset( $new_instance['url'] ) && ( ! isset( $old_instance['url'] ) || ( $new_instance['url'] !== $old_instance['url'] ) ) );
+ return wp_widget_rss_process( $new_instance, $testurl );
+ }
+
+ /**
+ * Outputs the settings form for the RSS widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ if ( empty( $instance ) ) {
+ $instance = array(
+ 'title' => '',
+ 'url' => '',
+ 'items' => 10,
+ 'error' => false,
+ 'show_summary' => 0,
+ 'show_author' => 0,
+ 'show_date' => 0,
+ );
+ }
+ $instance['number'] = $this->number;
+
+ wp_widget_rss_form( $instance );
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-search.php b/wp-includes/widgets/class-wp-widget-search.php
new file mode 100644
index 0000000..df77778
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-search.php
@@ -0,0 +1,94 @@
+<?php
+/**
+ * Widget API: WP_Widget_Search class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Search widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Search extends WP_Widget {
+
+ /**
+ * Sets up a new Search widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_search',
+ 'description' => __( 'A search form for your site.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Search widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Search widget instance.
+ */
+ public function widget( $args, $instance ) {
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ echo $args['before_widget'];
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ // Use active theme search form if it exists.
+ get_search_form();
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Outputs the settings form for the Search widget.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
+ $title = $instance['title'];
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
+ </p>
+ <?php
+ }
+
+ /**
+ * Handles updating settings for the current Search widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Updated settings.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+ $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '' ) );
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ return $instance;
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-tag-cloud.php b/wp-includes/widgets/class-wp-widget-tag-cloud.php
new file mode 100644
index 0000000..c56a774
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-tag-cloud.php
@@ -0,0 +1,220 @@
+<?php
+/**
+ * Widget API: WP_Widget_Tag_Cloud class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Tag cloud widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Tag_Cloud extends WP_Widget {
+
+ /**
+ * Sets up a new Tag Cloud widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'description' => __( 'A cloud of your most used tags.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops );
+ }
+
+ /**
+ * Outputs the content for the current Tag Cloud widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Tag Cloud widget instance.
+ */
+ public function widget( $args, $instance ) {
+ $current_taxonomy = $this->_get_current_taxonomy( $instance );
+
+ if ( ! empty( $instance['title'] ) ) {
+ $title = $instance['title'];
+ } else {
+ if ( 'post_tag' === $current_taxonomy ) {
+ $title = __( 'Tags' );
+ } else {
+ $tax = get_taxonomy( $current_taxonomy );
+ $title = $tax->labels->name;
+ }
+ }
+
+ $default_title = $title;
+
+ $show_count = ! empty( $instance['count'] );
+
+ $tag_cloud = wp_tag_cloud(
+ /**
+ * Filters the taxonomy used in the Tag Cloud widget.
+ *
+ * @since 2.8.0
+ * @since 3.0.0 Added taxonomy drop-down.
+ * @since 4.9.0 Added the `$instance` parameter.
+ *
+ * @see wp_tag_cloud()
+ *
+ * @param array $args Args used for the tag cloud widget.
+ * @param array $instance Array of settings for the current widget.
+ */
+ apply_filters(
+ 'widget_tag_cloud_args',
+ array(
+ 'taxonomy' => $current_taxonomy,
+ 'echo' => false,
+ 'show_count' => $show_count,
+ ),
+ $instance
+ )
+ );
+
+ if ( empty( $tag_cloud ) ) {
+ return;
+ }
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ echo $args['before_widget'];
+ if ( $title ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
+ $format = apply_filters( 'navigation_widgets_format', $format );
+
+ if ( 'html5' === $format ) {
+ // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
+ $title = trim( strip_tags( $title ) );
+ $aria_label = $title ? $title : $default_title;
+ echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
+ }
+
+ echo '<div class="tagcloud">';
+
+ echo $tag_cloud;
+
+ echo "</div>\n";
+
+ if ( 'html5' === $format ) {
+ echo '</nav>';
+ }
+
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Handles updating settings for the current Tag Cloud widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Settings to save or bool false to cancel saving.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $instance = array();
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
+ $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
+ return $instance;
+ }
+
+ /**
+ * Outputs the Tag Cloud widget settings form.
+ *
+ * @since 2.8.0
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
+ $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
+ </p>
+ <?php
+ $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
+ $current_taxonomy = $this->_get_current_taxonomy( $instance );
+
+ switch ( count( $taxonomies ) ) {
+
+ // No tag cloud supporting taxonomies found, display error message.
+ case 0:
+ ?>
+ <input type="hidden" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="" />
+ <p>
+ <?php _e( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ); ?>
+ </p>
+ <?php
+ break;
+
+ // Just a single tag cloud supporting taxonomy found, no need to display a select.
+ case 1:
+ $keys = array_keys( $taxonomies );
+ $taxonomy = reset( $keys );
+ ?>
+ <input type="hidden" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="<?php echo esc_attr( $taxonomy ); ?>" />
+ <?php
+ break;
+
+ // More than one tag cloud supporting taxonomy found, display a select.
+ default:
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy:' ); ?></label>
+ <select class="widefat" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>">
+ <?php foreach ( $taxonomies as $taxonomy => $tax ) : ?>
+ <option value="<?php echo esc_attr( $taxonomy ); ?>" <?php selected( $taxonomy, $current_taxonomy ); ?>>
+ <?php echo esc_html( $tax->labels->name ); ?>
+ </option>
+ <?php endforeach; ?>
+ </select>
+ </p>
+ <?php
+ }
+
+ if ( count( $taxonomies ) > 0 ) {
+ ?>
+ <p>
+ <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" <?php checked( $count, true ); ?> />
+ <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show tag counts' ); ?></label>
+ </p>
+ <?php
+ }
+ }
+
+ /**
+ * Retrieves the taxonomy for the current Tag cloud widget instance.
+ *
+ * @since 4.4.0
+ *
+ * @param array $instance Current settings.
+ * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
+ */
+ public function _get_current_taxonomy( $instance ) {
+ if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
+ return $instance['taxonomy'];
+ }
+
+ return 'post_tag';
+ }
+}
diff --git a/wp-includes/widgets/class-wp-widget-text.php b/wp-includes/widgets/class-wp-widget-text.php
new file mode 100644
index 0000000..c744698
--- /dev/null
+++ b/wp-includes/widgets/class-wp-widget-text.php
@@ -0,0 +1,581 @@
+<?php
+/**
+ * Widget API: WP_Widget_Text class
+ *
+ * @package WordPress
+ * @subpackage Widgets
+ * @since 4.4.0
+ */
+
+/**
+ * Core class used to implement a Text widget.
+ *
+ * @since 2.8.0
+ *
+ * @see WP_Widget
+ */
+class WP_Widget_Text extends WP_Widget {
+
+ /**
+ * Whether or not the widget has been registered yet.
+ *
+ * @since 4.8.1
+ * @var bool
+ */
+ protected $registered = false;
+
+ /**
+ * Sets up a new Text widget instance.
+ *
+ * @since 2.8.0
+ */
+ public function __construct() {
+ $widget_ops = array(
+ 'classname' => 'widget_text',
+ 'description' => __( 'Arbitrary text.' ),
+ 'customize_selective_refresh' => true,
+ 'show_instance_in_rest' => true,
+ );
+ $control_ops = array(
+ 'width' => 400,
+ 'height' => 350,
+ );
+ parent::__construct( 'text', __( 'Text' ), $widget_ops, $control_ops );
+ }
+
+ /**
+ * Adds hooks for enqueueing assets when registering all widget instances of this widget class.
+ *
+ * @param int $number Optional. The unique order number of this widget instance
+ * compared to other instances of the same class. Default -1.
+ */
+ public function _register_one( $number = -1 ) {
+ parent::_register_one( $number );
+ if ( $this->registered ) {
+ return;
+ }
+ $this->registered = true;
+
+ if ( $this->is_preview() ) {
+ add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
+ }
+
+ /*
+ * Note that the widgets component in the customizer will also do
+ * the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
+ */
+ add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
+
+ /*
+ * Note that the widgets component in the customizer will also do
+ * the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
+ */
+ add_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) );
+ }
+
+ /**
+ * Determines whether a given instance is legacy and should bypass using TinyMCE.
+ *
+ * @since 4.8.1
+ *
+ * @param array $instance {
+ * Instance data.
+ *
+ * @type string $text Content.
+ * @type bool|string $filter Whether autop or content filters should apply.
+ * @type bool $legacy Whether widget is in legacy mode.
+ * }
+ * @return bool Whether Text widget instance contains legacy data.
+ */
+ public function is_legacy_instance( $instance ) {
+
+ // Legacy mode when not in visual mode.
+ if ( isset( $instance['visual'] ) ) {
+ return ! $instance['visual'];
+ }
+
+ // Or, the widget has been added/updated in 4.8.0 then filter prop is 'content' and it is no longer legacy.
+ if ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ) {
+ return false;
+ }
+
+ // If the text is empty, then nothing is preventing migration to TinyMCE.
+ if ( empty( $instance['text'] ) ) {
+ return false;
+ }
+
+ $wpautop = ! empty( $instance['filter'] );
+ $has_line_breaks = ( str_contains( trim( $instance['text'] ), "\n" ) );
+
+ // If auto-paragraphs are not enabled and there are line breaks, then ensure legacy mode.
+ if ( ! $wpautop && $has_line_breaks ) {
+ return true;
+ }
+
+ // If an HTML comment is present, assume legacy mode.
+ if ( str_contains( $instance['text'], '<!--' ) ) {
+ return true;
+ }
+
+ // In the rare case that DOMDocument is not available we cannot reliably sniff content and so we assume legacy.
+ if ( ! class_exists( 'DOMDocument' ) ) {
+ // @codeCoverageIgnoreStart
+ return true;
+ // @codeCoverageIgnoreEnd
+ }
+
+ $doc = new DOMDocument();
+
+ // Suppress warnings generated by loadHTML.
+ $errors = libxml_use_internal_errors( true );
+ // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
+ @$doc->loadHTML(
+ sprintf(
+ '<!DOCTYPE html><html><head><meta charset="%s"></head><body>%s</body></html>',
+ esc_attr( get_bloginfo( 'charset' ) ),
+ $instance['text']
+ )
+ );
+ libxml_use_internal_errors( $errors );
+
+ $body = $doc->getElementsByTagName( 'body' )->item( 0 );
+
+ // See $allowedposttags.
+ $safe_elements_attributes = array(
+ 'strong' => array(),
+ 'em' => array(),
+ 'b' => array(),
+ 'i' => array(),
+ 'u' => array(),
+ 's' => array(),
+ 'ul' => array(),
+ 'ol' => array(),
+ 'li' => array(),
+ 'hr' => array(),
+ 'abbr' => array(),
+ 'acronym' => array(),
+ 'code' => array(),
+ 'dfn' => array(),
+ 'a' => array(
+ 'href' => true,
+ ),
+ 'img' => array(
+ 'src' => true,
+ 'alt' => true,
+ ),
+ );
+ $safe_empty_elements = array( 'img', 'hr', 'iframe' );
+
+ foreach ( $body->getElementsByTagName( '*' ) as $element ) {
+ /** @var DOMElement $element */
+ $tag_name = strtolower( $element->nodeName );
+
+ // If the element is not safe, then the instance is legacy.
+ if ( ! isset( $safe_elements_attributes[ $tag_name ] ) ) {
+ return true;
+ }
+
+ // If the element is not safely empty and it has empty contents, then legacy mode.
+ if ( ! in_array( $tag_name, $safe_empty_elements, true ) && '' === trim( $element->textContent ) ) {
+ return true;
+ }
+
+ // If an attribute is not recognized as safe, then the instance is legacy.
+ foreach ( $element->attributes as $attribute ) {
+ /** @var DOMAttr $attribute */
+ $attribute_name = strtolower( $attribute->nodeName );
+
+ if ( ! isset( $safe_elements_attributes[ $tag_name ][ $attribute_name ] ) ) {
+ return true;
+ }
+ }
+ }
+
+ // Otherwise, the text contains no elements/attributes that TinyMCE could drop, and therefore the widget does not need legacy mode.
+ return false;
+ }
+
+ /**
+ * Filters gallery shortcode attributes.
+ *
+ * Prevents all of a site's attachments from being shown in a gallery displayed on a
+ * non-singular template where a $post context is not available.
+ *
+ * @since 4.9.0
+ *
+ * @param array $attrs Attributes.
+ * @return array Attributes.
+ */
+ public function _filter_gallery_shortcode_attrs( $attrs ) {
+ if ( ! is_singular() && empty( $attrs['id'] ) && empty( $attrs['include'] ) ) {
+ $attrs['id'] = -1;
+ }
+ return $attrs;
+ }
+
+ /**
+ * Outputs the content for the current Text widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Post $post Global post object.
+ *
+ * @param array $args Display arguments including 'before_title', 'after_title',
+ * 'before_widget', and 'after_widget'.
+ * @param array $instance Settings for the current Text widget instance.
+ */
+ public function widget( $args, $instance ) {
+ global $post;
+
+ $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
+
+ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
+
+ $text = ! empty( $instance['text'] ) ? $instance['text'] : '';
+ $is_visual_text_widget = ( ! empty( $instance['visual'] ) && ! empty( $instance['filter'] ) );
+
+ // In 4.8.0 only, visual Text widgets get filter=content, without visual prop; upgrade instance props just-in-time.
+ if ( ! $is_visual_text_widget ) {
+ $is_visual_text_widget = ( isset( $instance['filter'] ) && 'content' === $instance['filter'] );
+ }
+ if ( $is_visual_text_widget ) {
+ $instance['filter'] = true;
+ $instance['visual'] = true;
+ }
+
+ /*
+ * Suspend legacy plugin-supplied do_shortcode() for 'widget_text' filter for the visual Text widget to prevent
+ * shortcodes being processed twice. Now do_shortcode() is added to the 'widget_text_content' filter in core itself
+ * and it applies after wpautop() to prevent corrupting HTML output added by the shortcode. When do_shortcode() is
+ * added to 'widget_text_content' then do_shortcode() will be manually called when in legacy mode as well.
+ */
+ $widget_text_do_shortcode_priority = has_filter( 'widget_text', 'do_shortcode' );
+ $should_suspend_legacy_shortcode_support = ( $is_visual_text_widget && false !== $widget_text_do_shortcode_priority );
+ if ( $should_suspend_legacy_shortcode_support ) {
+ remove_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
+ }
+
+ // Override global $post so filters (and shortcodes) apply in a consistent context.
+ $original_post = $post;
+ if ( is_singular() ) {
+ // Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).
+ $post = get_queried_object();
+ } else {
+ // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.
+ $post = null;
+ }
+
+ // Prevent dumping out all attachments from the media library.
+ add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
+
+ /**
+ * Filters the content of the Text widget.
+ *
+ * @since 2.3.0
+ * @since 4.4.0 Added the `$widget` parameter.
+ * @since 4.8.1 The `$widget` param may now be a `WP_Widget_Custom_HTML` object in addition to a `WP_Widget_Text` object.
+ *
+ * @param string $text The widget content.
+ * @param array $instance Array of settings for the current widget.
+ * @param WP_Widget_Text|WP_Widget_Custom_HTML $widget Current text or HTML widget instance.
+ */
+ $text = apply_filters( 'widget_text', $text, $instance, $this );
+
+ if ( $is_visual_text_widget ) {
+
+ /**
+ * Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor.
+ *
+ * By default a subset of the_content filters are applied, including wpautop and wptexturize.
+ *
+ * @since 4.8.0
+ *
+ * @param string $text The widget content.
+ * @param array $instance Array of settings for the current widget.
+ * @param WP_Widget_Text $widget Current Text widget instance.
+ */
+ $text = apply_filters( 'widget_text_content', $text, $instance, $this );
+ } else {
+ // Now in legacy mode, add paragraphs and line breaks when checkbox is checked.
+ if ( ! empty( $instance['filter'] ) ) {
+ $text = wpautop( $text );
+ }
+
+ /*
+ * Manually do shortcodes on the content when the core-added filter is present. It is added by default
+ * in core by adding do_shortcode() to the 'widget_text_content' filter to apply after wpautop().
+ * Since the legacy Text widget runs wpautop() after 'widget_text' filters are applied, the widget in
+ * legacy mode here manually applies do_shortcode() on the content unless the default
+ * core filter for 'widget_text_content' has been removed, or if do_shortcode() has already
+ * been applied via a plugin adding do_shortcode() to 'widget_text' filters.
+ */
+ if ( has_filter( 'widget_text_content', 'do_shortcode' ) && ! $widget_text_do_shortcode_priority ) {
+ if ( ! empty( $instance['filter'] ) ) {
+ $text = shortcode_unautop( $text );
+ }
+ $text = do_shortcode( $text );
+ }
+ }
+
+ // Restore post global.
+ $post = $original_post;
+ remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
+
+ // Undo suspension of legacy plugin-supplied shortcode handling.
+ if ( $should_suspend_legacy_shortcode_support ) {
+ add_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
+ }
+
+ echo $args['before_widget'];
+ if ( ! empty( $title ) ) {
+ echo $args['before_title'] . $title . $args['after_title'];
+ }
+
+ $text = preg_replace_callback( '#<(video|iframe|object|embed)\s[^>]*>#i', array( $this, 'inject_video_max_width_style' ), $text );
+
+ // Adds 'noopener' relationship, without duplicating values, to all HTML A elements that have a target.
+ $text = wp_targeted_link_rel( $text );
+
+ ?>
+ <div class="textwidget"><?php echo $text; ?></div>
+ <?php
+ echo $args['after_widget'];
+ }
+
+ /**
+ * Injects max-width and removes height for videos too constrained to fit inside sidebars on frontend.
+ *
+ * @since 4.9.0
+ *
+ * @see WP_Widget_Media_Video::inject_video_max_width_style()
+ *
+ * @param array $matches Pattern matches from preg_replace_callback.
+ * @return string HTML Output.
+ */
+ public function inject_video_max_width_style( $matches ) {
+ $html = $matches[0];
+ $html = preg_replace( '/\sheight="\d+"/', '', $html );
+ $html = preg_replace( '/\swidth="\d+"/', '', $html );
+ $html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
+ return $html;
+ }
+
+ /**
+ * Handles updating settings for the current Text widget instance.
+ *
+ * @since 2.8.0
+ *
+ * @param array $new_instance New settings for this instance as input by the user via
+ * WP_Widget::form().
+ * @param array $old_instance Old settings for this instance.
+ * @return array Settings to save or bool false to cancel saving.
+ */
+ public function update( $new_instance, $old_instance ) {
+ $new_instance = wp_parse_args(
+ $new_instance,
+ array(
+ 'title' => '',
+ 'text' => '',
+ 'filter' => false, // For back-compat.
+ 'visual' => null, // Must be explicitly defined.
+ )
+ );
+
+ $instance = $old_instance;
+
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );
+ if ( current_user_can( 'unfiltered_html' ) ) {
+ $instance['text'] = $new_instance['text'];
+ } else {
+ $instance['text'] = wp_kses_post( $new_instance['text'] );
+ }
+
+ $instance['filter'] = ! empty( $new_instance['filter'] );
+
+ // Upgrade 4.8.0 format.
+ if ( isset( $old_instance['filter'] ) && 'content' === $old_instance['filter'] ) {
+ $instance['visual'] = true;
+ }
+ if ( 'content' === $new_instance['filter'] ) {
+ $instance['visual'] = true;
+ }
+
+ if ( isset( $new_instance['visual'] ) ) {
+ $instance['visual'] = ! empty( $new_instance['visual'] );
+ }
+
+ // Filter is always true in visual mode.
+ if ( ! empty( $instance['visual'] ) ) {
+ $instance['filter'] = true;
+ }
+
+ return $instance;
+ }
+
+ /**
+ * Enqueues preview scripts.
+ *
+ * These scripts normally are enqueued just-in-time when a playlist shortcode is used.
+ * However, in the customizer, a playlist shortcode may be used in a text widget and
+ * dynamically added via selective refresh, so it is important to unconditionally enqueue them.
+ *
+ * @since 4.9.3
+ */
+ public function enqueue_preview_scripts() {
+ require_once dirname( __DIR__ ) . '/media.php';
+
+ wp_playlist_scripts( 'audio' );
+ wp_playlist_scripts( 'video' );
+ }
+
+ /**
+ * Loads the required scripts and styles for the widget control.
+ *
+ * @since 4.8.0
+ */
+ public function enqueue_admin_scripts() {
+ wp_enqueue_editor();
+ wp_enqueue_media();
+ wp_enqueue_script( 'text-widgets' );
+ wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
+ wp_add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' );
+ }
+
+ /**
+ * Outputs the Text widget settings form.
+ *
+ * @since 2.8.0
+ * @since 4.8.0 Form only contains hidden inputs which are synced with JS template.
+ * @since 4.8.1 Restored original form to be displayed when in legacy mode.
+ *
+ * @see WP_Widget_Text::render_control_template_scripts()
+ * @see _WP_Editors::editor()
+ *
+ * @param array $instance Current settings.
+ */
+ public function form( $instance ) {
+ $instance = wp_parse_args(
+ (array) $instance,
+ array(
+ 'title' => '',
+ 'text' => '',
+ )
+ );
+ ?>
+ <?php if ( ! $this->is_legacy_instance( $instance ) ) : ?>
+ <?php
+
+ if ( user_can_richedit() ) {
+ add_filter( 'the_editor_content', 'format_for_editor', 10, 2 );
+ $default_editor = 'tinymce';
+ } else {
+ $default_editor = 'html';
+ }
+
+ /** This filter is documented in wp-includes/class-wp-editor.php */
+ $text = apply_filters( 'the_editor_content', $instance['text'], $default_editor );
+
+ // Reset filter addition.
+ if ( user_can_richedit() ) {
+ remove_filter( 'the_editor_content', 'format_for_editor' );
+ }
+
+ // Prevent premature closing of textarea in case format_for_editor() didn't apply or the_editor_content filter did a wrong thing.
+ $escaped_text = preg_replace( '#</textarea#i', '&lt;/textarea', $text );
+
+ ?>
+ <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="title sync-input" type="hidden" value="<?php echo esc_attr( $instance['title'] ); ?>">
+ <textarea id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" class="text sync-input" hidden><?php echo $escaped_text; ?></textarea>
+ <input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" class="filter sync-input" type="hidden" value="on">
+ <input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual sync-input" type="hidden" value="on">
+ <?php else : ?>
+ <input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value="">
+ <p>
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
+ </p>
+ <?php
+ if ( ! isset( $instance['visual'] ) ) {
+ $widget_info_message = __( 'This widget may contain code that may work better in the &#8220;Custom HTML&#8221; widget. How about trying that widget instead?' );
+ } else {
+ $widget_info_message = __( 'This widget may have contained code that may work better in the &#8220;Custom HTML&#8221; widget. If you have not yet, how about trying that widget instead?' );
+ }
+
+ wp_admin_notice(
+ $widget_info_message,
+ array(
+ 'type' => 'info',
+ 'additional_classes' => array( 'notice-alt', 'inline' ),
+ )
+ );
+ ?>
+ <p>
+ <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
+ <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo esc_textarea( $instance['text'] ); ?></textarea>
+ </p>
+ <p>
+ <input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" type="checkbox"<?php checked( ! empty( $instance['filter'] ) ); ?> />&nbsp;<label for="<?php echo $this->get_field_id( 'filter' ); ?>"><?php _e( 'Automatically add paragraphs' ); ?></label>
+ </p>
+ <?php
+ endif;
+ }
+
+ /**
+ * Renders form template scripts.
+ *
+ * @since 4.8.0
+ * @since 4.9.0 The method is now static.
+ */
+ public static function render_control_template_scripts() {
+ $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
+ ?>
+ <script type="text/html" id="tmpl-widget-text-control-fields">
+ <# var elementIdPrefix = 'el' + String( Math.random() ).replace( /\D/g, '' ) + '_' #>
+ <p>
+ <label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
+ <input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
+ </p>
+
+ <?php if ( ! in_array( 'text_widget_custom_html', $dismissed_pointers, true ) ) : ?>
+ <div hidden class="wp-pointer custom-html-widget-pointer wp-pointer-top">
+ <div class="wp-pointer-content">
+ <h3><?php _e( 'New Custom HTML Widget' ); ?></h3>
+ <?php if ( is_customize_preview() ) : ?>
+ <p><?php _e( 'Did you know there is a &#8220;Custom HTML&#8221; widget now? You can find it by pressing the &#8220;<a class="add-widget" href="#">Add a Widget</a>&#8221; button and searching for &#8220;HTML&#8221;. Check it out to add some custom code to your site!' ); ?></p>
+ <?php else : ?>
+ <p><?php _e( 'Did you know there is a &#8220;Custom HTML&#8221; widget now? You can find it by scanning the list of available widgets on this screen. Check it out to add some custom code to your site!' ); ?></p>
+ <?php endif; ?>
+ <div class="wp-pointer-buttons">
+ <a class="close" href="#"><?php _e( 'Dismiss' ); ?></a>
+ </div>
+ </div>
+ <div class="wp-pointer-arrow">
+ <div class="wp-pointer-arrow-inner"></div>
+ </div>
+ </div>
+ <?php endif; ?>
+
+ <?php if ( ! in_array( 'text_widget_paste_html', $dismissed_pointers, true ) ) : ?>
+ <div hidden class="wp-pointer paste-html-pointer wp-pointer-top">
+ <div class="wp-pointer-content">
+ <h3><?php _e( 'Did you just paste HTML?' ); ?></h3>
+ <p><?php _e( 'Hey there, looks like you just pasted HTML into the &#8220;Visual&#8221; tab of the Text widget. You may want to paste your code into the &#8220;Text&#8221; tab instead. Alternately, try out the new &#8220;Custom HTML&#8221; widget!' ); ?></p>
+ <div class="wp-pointer-buttons">
+ <a class="close" href="#"><?php _e( 'Dismiss' ); ?></a>
+ </div>
+ </div>
+ <div class="wp-pointer-arrow">
+ <div class="wp-pointer-arrow-inner"></div>
+ </div>
+ </div>
+ <?php endif; ?>
+
+ <p>
+ <label for="{{ elementIdPrefix }}text" class="screen-reader-text"><?php /* translators: Hidden accessibility text. */ esc_html_e( 'Content:' ); ?></label>
+ <textarea id="{{ elementIdPrefix }}text" class="widefat text wp-editor-area" style="height: 200px" rows="16" cols="20"></textarea>
+ </p>
+ </script>
+ <?php
+ }
+}