wp_convert_widget_settings()将单一小工具的设置装换到多个小工具设置中

目录

描述

Convert the widget settings from single to multi-widget format. See ticket #8441 for more information.

wp_convert_widget_settings() 描述

参数

$base_name

(string) (必填) The base slug for a widget type.

默认值: None

$option_name

(string) (必填) The name of the option where the widgets settings are saved.

默认值: None

$settings

(array) (必填) The array of settings for this type of widget.

默认值: None

wp_convert_widget_settings() 参数

返回值

(array

The setting for the widget(s), updated to the multi-widget setting format introduced in 2.8

wp_convert_widget_settings() 返回值

历史

添加于 版本: 2.8.0

wp_convert_widget_settings() 历史

源文件

wp_convert_widget_settings() 函数的代码位于 wp-includes/widgets.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Convert the widget settings from single to multi-widget format.
 *
 * @since 2.8.0
 *
 * @global array $_wp_sidebars_widgets
 *
 * @param string $base_name
 * @param string $option_name
 * @param array  $settings
 * @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;
}

wp_convert_widget_settings() 源文件

相关