wp/wp-includes/widgets.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Core Widgets API
     3  * Core Widgets API
     4  *
     4  *
     5  * This API is used for creating dynamic sidebar without hardcoding functionality into
     5  * This API is used for creating dynamic sidebar without hardcoding functionality into
     6  * themes
     6  * themes.
     7  *
     7  *
     8  * Includes both internal WordPress routines and theme-use routines.
     8  * Includes both internal WordPress routines and theme-use routines.
     9  *
     9  *
    10  * This functionality was found in a plugin before the WordPress 2.2 release, which
    10  * This functionality was found in a plugin before the WordPress 2.2 release, which
    11  * included it in the core from that point on.
    11  * included it in the core from that point on.
   101 //
   101 //
   102 // Template tags & API functions.
   102 // Template tags & API functions.
   103 //
   103 //
   104 
   104 
   105 /**
   105 /**
   106  * Register a widget
   106  * Registers a widget.
   107  *
   107  *
   108  * Registers a WP_Widget widget
   108  * Registers a WP_Widget widget
   109  *
   109  *
   110  * @since 2.8.0
   110  * @since 2.8.0
   111  * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
   111  * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
   224  * When allowing for automatic generation of the name and ID parameters, keep
   224  * When allowing for automatic generation of the name and ID parameters, keep
   225  * in mind that the incrementor for your sidebar can change over time depending
   225  * in mind that the incrementor for your sidebar can change over time depending
   226  * on what other plugins and themes are installed.
   226  * on what other plugins and themes are installed.
   227  *
   227  *
   228  * If theme support for 'widgets' has not yet been added when this function is
   228  * If theme support for 'widgets' has not yet been added when this function is
   229  * called, it will be automatically enabled through the use of add_theme_support()
   229  * called, it will be automatically enabled through the use of add_theme_support().
   230  *
   230  *
   231  * @since 2.2.0
   231  * @since 2.2.0
   232  * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments.
   232  * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments.
   233  * @since 5.9.0 Added the `show_in_rest` argument.
   233  * @since 5.9.0 Added the `show_in_rest` argument.
   234  *
   234  *
   359 
   359 
   360 	return isset( $wp_registered_sidebars[ $sidebar_id ] );
   360 	return isset( $wp_registered_sidebars[ $sidebar_id ] );
   361 }
   361 }
   362 
   362 
   363 /**
   363 /**
   364  * Register an instance of a widget.
   364  * Registers an instance of a widget.
   365  *
   365  *
   366  * The default widget option is 'classname' that can be overridden.
   366  * The default widget option is 'classname' that can be overridden.
   367  *
   367  *
   368  * The function can also be used to un-register widgets when `$output_callback`
   368  * The function can also be used to un-register widgets when `$output_callback`
   369  * parameter is an empty string.
   369  * parameter is an empty string.
   433 		$wp_registered_widgets[ $id ] = $widget;
   433 		$wp_registered_widgets[ $id ] = $widget;
   434 	}
   434 	}
   435 }
   435 }
   436 
   436 
   437 /**
   437 /**
   438  * Retrieve description for widget.
   438  * Retrieves description for widget.
   439  *
   439  *
   440  * When registering widgets, the options can also include 'description' that
   440  * When registering widgets, the options can also include 'description' that
   441  * describes the widget for display on the widget administration panel or
   441  * describes the widget for display on the widget administration panel or
   442  * in the theme.
   442  * in the theme.
   443  *
   443  *
   459 		return esc_html( $wp_registered_widgets[ $id ]['description'] );
   459 		return esc_html( $wp_registered_widgets[ $id ]['description'] );
   460 	}
   460 	}
   461 }
   461 }
   462 
   462 
   463 /**
   463 /**
   464  * Retrieve description for a sidebar.
   464  * Retrieves description for a sidebar.
   465  *
   465  *
   466  * When registering sidebars a 'description' parameter can be included that
   466  * When registering sidebars a 'description' parameter can be included that
   467  * describes the sidebar for display on the widget administration panel.
   467  * describes the sidebar for display on the widget administration panel.
   468  *
   468  *
   469  * @since 2.9.0
   469  * @since 2.9.0
   667 
   667 
   668 	$wp_registered_widget_controls[ $id ] = $widget;
   668 	$wp_registered_widget_controls[ $id ] = $widget;
   669 }
   669 }
   670 
   670 
   671 /**
   671 /**
   672  * Remove control callback for widget.
   672  * Removes control callback for widget.
   673  *
   673  *
   674  * @since 2.2.0
   674  * @since 2.2.0
   675  *
   675  *
   676  * @param int|string $id Widget ID.
   676  * @param int|string $id Widget ID.
   677  */
   677  */
   678 function wp_unregister_widget_control( $id ) {
   678 function wp_unregister_widget_control( $id ) {
   679 	wp_register_widget_control( $id, '', '' );
   679 	wp_register_widget_control( $id, '', '' );
   680 }
   680 }
   681 
   681 
   682 /**
   682 /**
   683  * Display dynamic sidebar.
   683  * Displays dynamic sidebar.
   684  *
   684  *
   685  * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or
   685  * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or
   686  * 'name' parameter for its registered sidebars you can pass an ID or name as the $index parameter.
   686  * 'name' parameter for its registered sidebars you can pass an ID or name as the $index parameter.
   687  * Otherwise, you can pass in a numerical index to display the sidebar at that index.
   687  * Otherwise, you can pass in a numerical index to display the sidebar at that index.
   688  *
   688  *
   881 }
   881 }
   882 
   882 
   883 /**
   883 /**
   884  * Determines whether a given widget is displayed on the front end.
   884  * Determines whether a given widget is displayed on the front end.
   885  *
   885  *
   886  * Either $callback or $id_base can be used
   886  * Either $callback or $id_base can be used.
   887  * $id_base is the first argument when extending WP_Widget class
   887  * $id_base is the first argument when extending WP_Widget class.
   888  * Without the optional $widget_id parameter, returns the ID of the first sidebar
   888  * Without the optional $widget_id parameter, returns the ID of the first sidebar
   889  * in which the first instance of the widget with the given callback or $id_base is found.
   889  * in which the first instance of the widget with the given callback or $id_base is found.
   890  * With the $widget_id parameter, returns the ID of the sidebar where
   890  * With the $widget_id parameter, returns the ID of the sidebar where
   891  * the widget with that callback/$id_base AND that ID is found.
   891  * the widget with that callback/$id_base AND that ID is found.
   892  *
   892  *
  1000 //
  1000 //
  1001 // Internal Functions.
  1001 // Internal Functions.
  1002 //
  1002 //
  1003 
  1003 
  1004 /**
  1004 /**
  1005  * Retrieve full list of sidebars and their widget instance IDs.
  1005  * Retrieves the full list of sidebars and their widget instance IDs.
  1006  *
  1006  *
  1007  * Will upgrade sidebar widget list, if needed. Will also save updated list, if
  1007  * Will upgrade sidebar widget list, if needed. Will also save updated list, if
  1008  * needed.
  1008  * needed.
  1009  *
  1009  *
  1010  * @since 2.2.0
  1010  * @since 2.2.0
  1079 
  1079 
  1080 	return null;
  1080 	return null;
  1081 }
  1081 }
  1082 
  1082 
  1083 /**
  1083 /**
  1084  * Set the sidebar widget option to update sidebars.
  1084  * Sets the sidebar widget option to update sidebars.
  1085  *
  1085  *
  1086  * @since 2.2.0
  1086  * @since 2.2.0
  1087  * @access private
  1087  * @access private
  1088  *
  1088  *
  1089  * @global array $_wp_sidebars_widgets
  1089  * @global array $_wp_sidebars_widgets
  1101 
  1101 
  1102 	update_option( 'sidebars_widgets', $sidebars_widgets );
  1102 	update_option( 'sidebars_widgets', $sidebars_widgets );
  1103 }
  1103 }
  1104 
  1104 
  1105 /**
  1105 /**
  1106  * Retrieve default registered sidebars list.
  1106  * Retrieves default registered sidebars list.
  1107  *
  1107  *
  1108  * @since 2.2.0
  1108  * @since 2.2.0
  1109  * @access private
  1109  * @access private
  1110  *
  1110  *
  1111  * @global array $wp_registered_sidebars The registered sidebars.
  1111  * @global array $wp_registered_sidebars The registered sidebars.
  1192 
  1192 
  1193 	return $settings;
  1193 	return $settings;
  1194 }
  1194 }
  1195 
  1195 
  1196 /**
  1196 /**
  1197  * Output an arbitrary widget as a template tag.
  1197  * Outputs an arbitrary widget as a template tag.
  1198  *
  1198  *
  1199  * @since 2.8.0
  1199  * @since 2.8.0
  1200  *
  1200  *
  1201  * @global WP_Widget_Factory $wp_widget_factory
  1201  * @global WP_Widget_Factory $wp_widget_factory
  1202  *
  1202  *
  1280 function _get_widget_id_base( $id ) {
  1280 function _get_widget_id_base( $id ) {
  1281 	return preg_replace( '/-[0-9]+$/', '', $id );
  1281 	return preg_replace( '/-[0-9]+$/', '', $id );
  1282 }
  1282 }
  1283 
  1283 
  1284 /**
  1284 /**
  1285  * Handle sidebars config after theme change
  1285  * Handles sidebars config after theme change.
  1286  *
  1286  *
  1287  * @access private
  1287  * @access private
  1288  * @since 3.3.0
  1288  * @since 3.3.0
  1289  *
  1289  *
  1290  * @global array $sidebars_widgets
  1290  * @global array $sidebars_widgets
  1573 
  1573 
  1574 	return $sidebars_widgets;
  1574 	return $sidebars_widgets;
  1575 }
  1575 }
  1576 
  1576 
  1577 /**
  1577 /**
  1578  * Display the RSS entries in a list.
  1578  * Displays the RSS entries in a list.
  1579  *
  1579  *
  1580  * @since 2.5.0
  1580  * @since 2.5.0
  1581  *
  1581  *
  1582  * @param string|array|object $rss  RSS url.
  1582  * @param string|array|object $rss  RSS url.
  1583  * @param array               $args Widget arguments.
  1583  * @param array               $args Widget arguments.
  1680 	$rss->__destruct();
  1680 	$rss->__destruct();
  1681 	unset( $rss );
  1681 	unset( $rss );
  1682 }
  1682 }
  1683 
  1683 
  1684 /**
  1684 /**
  1685  * Display RSS widget options form.
  1685  * Displays RSS widget options form.
  1686  *
  1686  *
  1687  * The options for what fields are displayed for the RSS form are all booleans
  1687  * The options for what fields are displayed for the RSS form are all booleans
  1688  * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
  1688  * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
  1689  * 'show_date'.
  1689  * 'show_date'.
  1690  *
  1690  *
  1744 		<label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label><br />
  1744 		<label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label><br />
  1745 	<?php endif; if ( $inputs['show_author'] ) : ?>
  1745 	<?php endif; if ( $inputs['show_author'] ) : ?>
  1746 		<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'] ); ?> />
  1746 		<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'] ); ?> />
  1747 		<label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label><br />
  1747 		<label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label><br />
  1748 	<?php endif; if ( $inputs['show_date'] ) : ?>
  1748 	<?php endif; if ( $inputs['show_date'] ) : ?>
  1749 		<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'] ); ?>/>
  1749 		<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'] ); ?> />
  1750 		<label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label><br />
  1750 		<label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label><br />
  1751 	<?php endif; ?>
  1751 	<?php endif; ?>
  1752 	</p>
  1752 	</p>
  1753 	<?php
  1753 	<?php
  1754 	endif; // End of display options.
  1754 	endif; // End of display options.
  1761 	endif;
  1761 	endif;
  1762 	endforeach;
  1762 	endforeach;
  1763 }
  1763 }
  1764 
  1764 
  1765 /**
  1765 /**
  1766  * Process RSS feed widget data and optionally retrieve feed items.
  1766  * Processes RSS feed widget data and optionally retrieve feed items.
  1767  *
  1767  *
  1768  * The feed widget can not have more than 20 items or it will reset back to the
  1768  * The feed widget can not have more than 20 items or it will reset back to the
  1769  * default, which is 10.
  1769  * default, which is 10.
  1770  *
  1770  *
  1771  * The resulting array has the feed title, feed url, feed link (from channel),
  1771  * The resulting array has the feed title, feed url, feed link (from channel),
  1881 function wp_setup_widgets_block_editor() {
  1881 function wp_setup_widgets_block_editor() {
  1882 	add_theme_support( 'widgets-block-editor' );
  1882 	add_theme_support( 'widgets-block-editor' );
  1883 }
  1883 }
  1884 
  1884 
  1885 /**
  1885 /**
  1886  * Whether or not to use the block editor to manage widgets. Defaults to true
  1886  * Determines whether or not to use the block editor to manage widgets.
  1887  * unless a theme has removed support for widgets-block-editor or a plugin has
  1887  * Defaults to true unless a theme has removed support for widgets-block-editor
  1888  * filtered the return value of this function.
  1888  * or a plugin has filtered the return value of this function.
  1889  *
  1889  *
  1890  * @since 5.8.0
  1890  * @since 5.8.0
  1891  *
  1891  *
  1892  * @return bool Whether to use the block editor to manage widgets.
  1892  * @return bool Whether to use the block editor to manage widgets.
  1893  */
  1893  */