WordPressを読む 68-3 /blog/wp-includes/widgets.php 3
2015/04/27
目次
- 1 /blog/wp-includes/widgets.php3
- 2 関数 wp_unregister_widget_control()
- 3 関数 dynamic_sidebar()
- 4 関数 is_active_widget()
- 5 関数 is_dynamic_sidebar()
- 6 関数 is_active_sidebar()
- 7 関数 wp_get_sidebars_widgets()
- 8 関数 wp_set_sidebars_widgets()
- 9 関数 wp_get_widget_defaults()
- 10 関数 wp_convert_widget_settings()
- 11 関数 the_widget()
- 12 関数 _get_widget_id_base()
- 13 関数 _wp_sidebars_changed()
/blog/wp-includes/widgets.php3
関数 wp_unregister_widget_control()
1 2 3 4 5 6 7 8 9 10 11 | /** * Remove control callback for widget. * * @since 2.2.0 * @uses wp_register_widget_control() Unregisters by using empty callback. * * @param int|string $id Widget ID. */ function wp_unregister_widget_control($id) { return wp_register_widget_control($id, '', ''); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | /** * 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 * * @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar. * @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/widgets.php */ do_action( 'dynamic_sidebar_before', $index, false ); /** This action is documented in wp-includes/widgets.php */ do_action( 'dynamic_sidebar_after', $index, false ); /** This filter is documented in wp-includes/widgets.php */ return apply_filters( 'dynamic_sidebar_has_widgets', false, $index ); } /** * 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 ); $sidebar = $wp_registered_sidebars[$index]; $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'], $id, $classname_); /** * Filter 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_id { * An associative array of widget arguments. * * @type string $name Name of the widget. * @type string $id Widget ID. * @type array|callback $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; } } /** * 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 ); /** * Filter 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. */ $did_one = apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index ); return $did_one; } |
関数 is_active_widget()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * Whether 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 'init' or later. * * @since 2.2.0 * * @param string $callback Optional, Widget callback to check. * @param int $widget_id Optional, but needed for checking. Widget ID. * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget. * @param bool $skip_inactive Optional, whether to check in 'wp_inactive_widgets'. * @return mixed false if widget is not active or id of sidebar in which the widget is 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 || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) { 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Whether the dynamic sidebar is enabled and used by theme. * * @since 2.2.0 * * @return bool True, if using widgets. False, if not using widgets. */ 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 ( count($sidebars_widgets[$index]) ) { foreach ( (array) $sidebars_widgets[$index] as $widget ) if ( array_key_exists($widget, $wp_registered_widgets) ) return true; } } return false; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Whether a sidebar is in use. * * @since 2.8.0 * * @param mixed $index Sidebar name, id or number to check. * @return bool true if the sidebar is in use, 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] ); /** * Filter 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 ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /* Internal Functions */ /** * Retrieve full list of sidebars and their widgets. * * Will upgrade sidebar widget list, if needed. Will also save updated list, if * needed. * * @since 2.2.0 * @access private * * @param bool $deprecated Not used (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 ( $deprecated !== true ) _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']); /** * Filter the list of sidebars and their widgets. * * @since 2.7.0 * * @param array $sidebars_widgets An associative array of sidebars and their widgets. */ $sidebars_widgets = apply_filters( 'sidebars_widgets', $sidebars_widgets ); return $sidebars_widgets; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Set the sidebar widget option to update sidebars. * * @since 2.2.0 * @access private * * @param array $sidebars_widgets Sidebar widgets and their settings. */ function wp_set_sidebars_widgets( $sidebars_widgets ) { if ( !isset( $sidebars_widgets['array_version'] ) ) $sidebars_widgets['array_version'] = 3; update_option( 'sidebars_widgets', $sidebars_widgets ); } |
関数 wp_get_widget_defaults()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Retrieve default registered sidebars list. * * @since 2.2.0 * @access private * * @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; } |
関数 wp_convert_widget_settings()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /** * Convert the widget settings from single to multi-widget format. * * @since 2.8.0 * * @return array */ function wp_convert_widget_settings($base_name, $option_name, $settings) { // This test may need expanding. $single = $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; } |
関数 the_widget()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * Output an arbitrary widget as a template tag. * * @since 2.8.0 * * @param string $widget the widget's PHP class name (see default-widgets.php) * @param array $instance the widget's instance settings * @param array $args the widget's sidebar args * @return void **/ function the_widget($widget, $instance = array(), $args = array()) { global $wp_widget_factory; $widget_obj = $wp_widget_factory->widgets[$widget]; if ( !is_a($widget_obj, 'WP_Widget') ) return; $before_widget = sprintf('<div class="widget %s">', $widget_obj->widget_options['classname'] ); $default_args = array( 'before_widget' => $before_widget, 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' ); $args = wp_parse_args($args, $default_args); $instance = wp_parse_args($instance); /** * 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); } |
関数 _get_widget_id_base()
1 2 3 4 5 6 | /** * Private */ function _get_widget_id_base($id) { return preg_replace( '/-[0-9]+$/', '', $id ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Handle sidebars config after theme change * * @access private * @since 3.3.0 */ function _wp_sidebars_changed() { global $sidebars_widgets; if ( ! is_array( $sidebars_widgets ) ) $sidebars_widgets = wp_get_sidebars_widgets(); retrieve_widgets(true); } |