WordPressを読む 68-2 /blog/wp-includes/widgets.php 2
2015/04/27
目次
- 1 /blog/wp-includes/widgets.php2
- 2 グローバル変数など
- 3 関数 register_widget()
- 4 関数 unregister_widget()
- 5 関数 register_sidebars()
- 6 関数 register_sidebar()
- 7 関数 unregister_sidebar()
- 8 関数 wp_register_sidebar_widget()
- 9 関数 wp_widget_description()
- 10 関数 wp_sidebar_description()
- 11 関数 wp_unregister_sidebar_widget()
- 12 関数 wp_register_widget_control()
- 13 関数 _register_widget_update_callback()
- 14 関数 _register_widget_form_callback()
/blog/wp-includes/widgets.php2
グローバル変数など
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 | /* 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 * @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 control (options). * * @global array $wp_registered_widget_controls * @since 2.2.0 */ $wp_registered_widget_controls = array(); $wp_registered_widget_updates = array(); /** * Private */ $_wp_sidebars_widgets = array(); /** * Private */ $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' ); |
関数 register_widget()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /* Template tags & API functions */ /** * Register a widget * * Registers a WP_Widget widget * * @since 2.8.0 * * @see WP_Widget * @see WP_Widget_Factory * @uses WP_Widget_Factory * * @param string $widget_class The name of a class that extends WP_Widget */ function register_widget($widget_class) { global $wp_widget_factory; $wp_widget_factory->register($widget_class); } |
関数 unregister_widget()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Unregister a widget * * Unregisters a WP_Widget widget. Useful for unregistering default widgets. * Run within a function hooked to the widgets_init action. * * @since 2.8.0 * * @see WP_Widget * @see WP_Widget_Factory * @uses WP_Widget_Factory * * @param string $widget_class The name of a class that extends WP_Widget */ function unregister_widget($widget_class) { global $wp_widget_factory; $wp_widget_factory->unregister($widget_class); } |
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 | /** * 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. * * The default for the name is "Sidebar #", with '#' being replaced with the * number the sidebar is currently when greater than one. If first sidebar, the * name will be just "Sidebar". The default for id is "sidebar-" followed by the * number the sidebar creation is currently at. If the id is provided, and multiple * sidebars are being defined, the id will have "-2" appended, and so on. * * @since 2.2.0 * * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here. * @uses parse_str() Converts a string to an array to be used in the rest of the function. * @uses register_sidebar() Sends single sidebar information [name, id] to this * function to handle building the sidebar. * * @param int $number Number of sidebars to create. * @param string|array $args Builds Sidebar based off of 'name' and 'id' values. */ 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 ) $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : 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 ID's while ( isset($wp_registered_sidebars[$_args['id']]) ) $_args['id'] = $args['id'] . '-' . $n++; } else { $n = count($wp_registered_sidebars); do { $_args['id'] = 'sidebar-' . ++$n; } while ( isset($wp_registered_sidebars[$_args['id']]) ); } register_sidebar($_args); } } |
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 | /** * 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() * * Arguments passed as a string should be separated by '&': * * e.g. 'name=Sidebar&id=my_prefix_sidebar' * * The same arguments passed as an array: * * array( * 'name' => 'Sidebar', * 'id' => 'my_prefix_sidebar', * ) * * Arguments: * name - The name or title of the sidebar displayed in the admin dashboard. * id - The unique identifier by which the sidebar will be called. * before_widget - HTML content that will be prepended to each widget's HTML output * when assigned to this sidebar. * after_widget - HTML content that will be appended to each widget's HTML output * when assigned to this sidebar. * before_title - HTML content that will be prepended to the sidebar title when displayed. * after_title - HTML content that will be appended to the sidebar title when displayed. * * @since 2.2.0 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID. * @uses add_theme_support() to ensure widget support has been added. * * @param string|array $args Arguments for the sidebar being registered. * @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; $defaults = array( '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", ); $sidebar = wp_parse_args( $args, $defaults ); $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']; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Removes a sidebar from the list. * * @since 2.2.0 * * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID. * * @param string $name The ID of the sidebar when it was added. */ function unregister_sidebar( $name ) { global $wp_registered_sidebars; if ( isset( $wp_registered_sidebars[$name] ) ) unset( $wp_registered_sidebars[$name] ); } |
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 | /** * Register widget for use in sidebars. * * 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 * * @uses $wp_registered_widgets Uses stored registered widgets. * @uses $wp_register_widget_defaults Retrieves widget defaults. * * @param int|string $id Widget ID. * @param string $name Widget display title. * @param callback $output_callback Run when widget is called. * @param array|string $options Optional. Widget Options. * @param mixed $params,... Widget parameters to add to widget. * @return null Will return if $output_callback is empty after removing widget. */ function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) { 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) ) { if ( isset($wp_registered_widget_controls[$id]) ) unset($wp_registered_widget_controls[$id]); if ( isset($wp_registered_widget_updates[$id_base]) ) 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' => array_slice(func_get_args(), 4) ); $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; } } |
関数 wp_widget_description()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 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 * * @param int|string $id Widget ID. * @return string Widget description, if available. Null on failure to retrieve description. */ 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'] ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * 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 * * @param int|string $id sidebar ID. * @return string Sidebar description, if available. Null on failure to retrieve description. */ function wp_sidebar_description( $id ) { if ( !is_scalar($id) ) return; global $wp_registered_sidebars; if ( isset($wp_registered_sidebars[$id]['description']) ) return esc_html( $wp_registered_sidebars[$id]['description'] ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 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 $id The widget ID. */ do_action( 'wp_unregister_sidebar_widget', $id ); wp_register_sidebar_widget($id, '', ''); wp_unregister_widget_control($id); } |
関数 wp_register_widget_control()
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 | /** * Registers widget control callback for customizing options. * * The options contains the 'height', 'width', and 'id_base' keys. The 'height' * option is never used. The 'width' option is the width of the fully expanded * control form, but try hard to use the default width. The 'id_base' is for * multi-widgets (widgets which allow multiple instances such as the text * widget), an id_base must be provided. The widget id will end up looking like * {$id_base}-{$unique_number}. * * @since 2.2.0 * * @param int|string $id Sidebar ID. * @param string $name Sidebar display name. * @param callback $control_callback Run when sidebar is displayed. * @param array|string $options Optional. Widget options. See above long description. * @param mixed $params,... Optional. Additional parameters to add to widget. */ function wp_register_widget_control($id, $name, $control_callback, $options = array()) { 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) ) { if ( isset($wp_registered_widgets[$id]) ) 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' => array_slice(func_get_args(), 4) ); $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; } |
関数 _register_widget_update_callback()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function _register_widget_update_callback($id_base, $update_callback, $options = array()) { 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' => array_slice(func_get_args(), 3) ); $widget = array_merge($widget, $options); $wp_registered_widget_updates[$id_base] = $widget; } |
関数 _register_widget_form_callback()
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 | function _register_widget_form_callback($id, $name, $form_callback, $options = array()) { 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' => array_slice(func_get_args(), 4) ); $widget = array_merge($widget, $options); $wp_registered_widget_controls[$id] = $widget; } |