459 * 'id' in $args, then they will be built for you. |
459 * 'id' in $args, then they will be built for you. |
460 * |
460 * |
461 * The default for the name is "Sidebar #", with '#' being replaced with the |
461 * The default for the name is "Sidebar #", with '#' being replaced with the |
462 * number the sidebar is currently when greater than one. If first sidebar, the |
462 * number the sidebar is currently when greater than one. If first sidebar, the |
463 * name will be just "Sidebar". The default for id is "sidebar-" followed by the |
463 * name will be just "Sidebar". The default for id is "sidebar-" followed by the |
464 * number the sidebar creation is currently at. |
464 * number the sidebar creation is currently at. If the id is provided, and multiple |
|
465 * sidebars are being defined, the id will have "-2" appended, and so on. |
465 * |
466 * |
466 * @since 2.2.0 |
467 * @since 2.2.0 |
467 * |
468 * |
468 * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here. |
469 * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here. |
469 * @uses parse_str() Converts a string to an array to be used in the rest of the function. |
470 * @uses parse_str() Converts a string to an array to be used in the rest of the function. |
478 $number = (int) $number; |
479 $number = (int) $number; |
479 |
480 |
480 if ( is_string($args) ) |
481 if ( is_string($args) ) |
481 parse_str($args, $args); |
482 parse_str($args, $args); |
482 |
483 |
483 for ( $i=1; $i <= $number; $i++ ) { |
484 for ( $i = 1; $i <= $number; $i++ ) { |
484 $_args = $args; |
485 $_args = $args; |
485 |
486 |
486 if ( $number > 1 ) { |
487 if ( $number > 1 ) |
487 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i); |
488 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i); |
488 } else { |
489 else |
489 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar'); |
490 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar'); |
490 } |
491 |
491 |
492 // Custom specified ID's are suffixed if they exist already. |
492 if (isset($args['id'])) { |
493 // Automatically generated sidebar names need to be suffixed regardless starting at -0 |
|
494 if ( isset($args['id']) ) { |
493 $_args['id'] = $args['id']; |
495 $_args['id'] = $args['id']; |
|
496 $n = 2; // Start at -2 for conflicting custom ID's |
|
497 while ( isset($wp_registered_sidebars[$_args['id']]) ) |
|
498 $_args['id'] = $args['id'] . '-' . $n++; |
494 } else { |
499 } else { |
495 $n = count($wp_registered_sidebars); |
500 $n = count($wp_registered_sidebars); |
496 do { |
501 do { |
497 $n++; |
502 $_args['id'] = 'sidebar-' . ++$n; |
498 $_args['id'] = "sidebar-$n"; |
503 } while ( isset($wp_registered_sidebars[$_args['id']]) ); |
499 } while (isset($wp_registered_sidebars[$_args['id']])); |
504 } |
500 } |
|
501 |
|
502 register_sidebar($_args); |
505 register_sidebar($_args); |
503 } |
506 } |
504 } |
507 } |
505 |
508 |
506 /** |
509 /** |
529 * <em>Content</em> is assumed to be HTML and should be formatted as such, but |
532 * <em>Content</em> is assumed to be HTML and should be formatted as such, but |
530 * doesn't have to be. |
533 * doesn't have to be. |
531 * |
534 * |
532 * @since 2.2.0 |
535 * @since 2.2.0 |
533 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID. |
536 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID. |
534 * @uses parse_str() Converts a string to an array to be used in the rest of the function. |
|
535 * @usedby register_sidebars() |
|
536 * |
537 * |
537 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values |
538 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values |
538 * @return string The sidebar id that was added. |
539 * @return string The sidebar id that was added. |
539 */ |
540 */ |
540 function register_sidebar($args = array()) { |
541 function register_sidebar($args = array()) { |
541 global $wp_registered_sidebars; |
542 global $wp_registered_sidebars; |
542 |
|
543 if ( is_string($args) ) |
|
544 parse_str($args, $args); |
|
545 |
543 |
546 $i = count($wp_registered_sidebars) + 1; |
544 $i = count($wp_registered_sidebars) + 1; |
547 |
545 |
548 $defaults = array( |
546 $defaults = array( |
549 'name' => sprintf(__('Sidebar %d'), $i ), |
547 'name' => sprintf(__('Sidebar %d'), $i ), |
550 'id' => "sidebar-$i", |
548 'id' => "sidebar-$i", |
551 'description' => '', |
549 'description' => '', |
|
550 'class' => '', |
552 'before_widget' => '<li id="%1$s" class="widget %2$s">', |
551 'before_widget' => '<li id="%1$s" class="widget %2$s">', |
553 'after_widget' => "</li>\n", |
552 'after_widget' => "</li>\n", |
554 'before_title' => '<h2 class="widgettitle">', |
553 'before_title' => '<h2 class="widgettitle">', |
555 'after_title' => "</h2>\n", |
554 'after_title' => "</h2>\n", |
556 ); |
555 ); |
557 |
556 |
558 $sidebar = array_merge($defaults, (array) $args); |
557 $sidebar = wp_parse_args( $args, $defaults ); |
559 |
558 |
560 $wp_registered_sidebars[$sidebar['id']] = $sidebar; |
559 $wp_registered_sidebars[$sidebar['id']] = $sidebar; |
|
560 |
|
561 add_theme_support('widgets'); |
|
562 |
|
563 do_action( 'register_sidebar', $sidebar ); |
561 |
564 |
562 return $sidebar['id']; |
565 return $sidebar['id']; |
563 } |
566 } |
564 |
567 |
565 /** |
568 /** |
592 * @uses $wp_register_widget_defaults Retrieves widget defaults. |
595 * @uses $wp_register_widget_defaults Retrieves widget defaults. |
593 * |
596 * |
594 * @param int|string $id Widget ID. |
597 * @param int|string $id Widget ID. |
595 * @param string $name Widget display title. |
598 * @param string $name Widget display title. |
596 * @param callback $output_callback Run when widget is called. |
599 * @param callback $output_callback Run when widget is called. |
597 * @param array|string Optional. $options Widget Options. |
600 * @param array|string $options Optional. Widget Options. |
598 * @param mixed $params,... Widget parameters to add to widget. |
601 * @param mixed $params,... Widget parameters to add to widget. |
599 * @return null Will return if $output_callback is empty after removing widget. |
602 * @return null Will return if $output_callback is empty after removing widget. |
600 */ |
603 */ |
601 function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) { |
604 function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) { |
602 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; |
605 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; |
877 |
885 |
878 $params = apply_filters( 'dynamic_sidebar_params', $params ); |
886 $params = apply_filters( 'dynamic_sidebar_params', $params ); |
879 |
887 |
880 $callback = $wp_registered_widgets[$id]['callback']; |
888 $callback = $wp_registered_widgets[$id]['callback']; |
881 |
889 |
|
890 do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] ); |
|
891 |
882 if ( is_callable($callback) ) { |
892 if ( is_callable($callback) ) { |
883 call_user_func_array($callback, $params); |
893 call_user_func_array($callback, $params); |
884 $did_one = true; |
894 $did_one = true; |
885 } |
895 } |
886 } |
896 } |
887 |
897 |
888 return $did_one; |
898 return $did_one; |
889 } |
899 } |
890 |
900 |
891 /** |
901 /** |
892 * Whether widget is displayied on the front-end. |
902 * Whether widget is displayed on the front-end. |
893 * |
903 * |
894 * Either $callback or $id_base can be used |
904 * Either $callback or $id_base can be used |
895 * $id_base is the first argument when extending WP_Widget class |
905 * $id_base is the first argument when extending WP_Widget class |
896 * Without the optional $widget_id parameter, returns the ID of the first sidebar |
906 * Without the optional $widget_id parameter, returns the ID of the first sidebar |
897 * in which the first instance of the widget with the given callback or $id_base is found. |
907 * in which the first instance of the widget with the given callback or $id_base is found. |
901 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective |
911 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective |
902 * this function has to run after widgets have initialized, at action 'init' or later. |
912 * this function has to run after widgets have initialized, at action 'init' or later. |
903 * |
913 * |
904 * @since 2.2.0 |
914 * @since 2.2.0 |
905 * |
915 * |
906 * @param callback Optional, Widget callback to check. |
916 * @param string $callback Optional, Widget callback to check. |
907 * @param int $widget_id Optional, but needed for checking. Widget ID. |
917 * @param int $widget_id Optional, but needed for checking. Widget ID. |
908 * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget. |
918 * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget. |
909 * @param bool $skip_inactive Optional, whether to check in 'wp_inactive_widgets'. |
919 * @param bool $skip_inactive Optional, whether to check in 'wp_inactive_widgets'. |
910 * @return mixed false if widget is not active or id of sidebar in which the widget is active. |
920 * @return mixed false if widget is not active or id of sidebar in which the widget is active. |
911 */ |
921 */ |
978 * needed. |
988 * needed. |
979 * |
989 * |
980 * @since 2.2.0 |
990 * @since 2.2.0 |
981 * @access private |
991 * @access private |
982 * |
992 * |
983 * @param bool $update Optional, deprecated. |
993 * @param bool $deprecated Not used (deprecated). |
984 * @return array Upgraded list of widgets to version 3 array format when called from the admin. |
994 * @return array Upgraded list of widgets to version 3 array format when called from the admin. |
985 */ |
995 */ |
986 function wp_get_sidebars_widgets($deprecated = true) { |
996 function wp_get_sidebars_widgets($deprecated = true) { |
987 global $wp_registered_widgets, $wp_registered_sidebars, $_wp_sidebars_widgets; |
997 if ( $deprecated !== true ) |
|
998 _deprecated_argument( __FUNCTION__, '2.8.1' ); |
|
999 |
|
1000 global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets; |
988 |
1001 |
989 // If loading from front page, consult $_wp_sidebars_widgets rather than options |
1002 // If loading from front page, consult $_wp_sidebars_widgets rather than options |
990 // to see if wp_convert_widget_settings() has made manipulations in memory. |
1003 // to see if wp_convert_widget_settings() has made manipulations in memory. |
991 if ( !is_admin() ) { |
1004 if ( !is_admin() ) { |
992 if ( empty($_wp_sidebars_widgets) ) |
1005 if ( empty($_wp_sidebars_widgets) ) |
993 $_wp_sidebars_widgets = get_option('sidebars_widgets', array()); |
1006 $_wp_sidebars_widgets = get_option('sidebars_widgets', array()); |
994 |
1007 |
995 $sidebars_widgets = $_wp_sidebars_widgets; |
1008 $sidebars_widgets = $_wp_sidebars_widgets; |
996 } else { |
1009 } else { |
997 $sidebars_widgets = get_option('sidebars_widgets', array()); |
1010 $sidebars_widgets = get_option('sidebars_widgets', array()); |
998 $_sidebars_widgets = array(); |
1011 } |
999 |
1012 |
1000 if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) ) |
1013 if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) ) |
1001 $sidebars_widgets['array_version'] = 3; |
|
1002 elseif ( !isset($sidebars_widgets['array_version']) ) |
|
1003 $sidebars_widgets['array_version'] = 1; |
|
1004 |
|
1005 switch ( $sidebars_widgets['array_version'] ) { |
|
1006 case 1 : |
|
1007 foreach ( (array) $sidebars_widgets as $index => $sidebar ) |
|
1008 if ( is_array($sidebar) ) |
|
1009 foreach ( (array) $sidebar as $i => $name ) { |
|
1010 $id = strtolower($name); |
|
1011 if ( isset($wp_registered_widgets[$id]) ) { |
|
1012 $_sidebars_widgets[$index][$i] = $id; |
|
1013 continue; |
|
1014 } |
|
1015 $id = sanitize_title($name); |
|
1016 if ( isset($wp_registered_widgets[$id]) ) { |
|
1017 $_sidebars_widgets[$index][$i] = $id; |
|
1018 continue; |
|
1019 } |
|
1020 |
|
1021 $found = false; |
|
1022 |
|
1023 foreach ( $wp_registered_widgets as $widget_id => $widget ) { |
|
1024 if ( strtolower($widget['name']) == strtolower($name) ) { |
|
1025 $_sidebars_widgets[$index][$i] = $widget['id']; |
|
1026 $found = true; |
|
1027 break; |
|
1028 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) { |
|
1029 $_sidebars_widgets[$index][$i] = $widget['id']; |
|
1030 $found = true; |
|
1031 break; |
|
1032 } |
|
1033 } |
|
1034 |
|
1035 if ( $found ) |
|
1036 continue; |
|
1037 |
|
1038 unset($_sidebars_widgets[$index][$i]); |
|
1039 } |
|
1040 $_sidebars_widgets['array_version'] = 2; |
|
1041 $sidebars_widgets = $_sidebars_widgets; |
|
1042 unset($_sidebars_widgets); |
|
1043 |
|
1044 case 2 : |
|
1045 $sidebars = array_keys( $wp_registered_sidebars ); |
|
1046 if ( !empty( $sidebars ) ) { |
|
1047 // Move the known-good ones first |
|
1048 foreach ( (array) $sidebars as $id ) { |
|
1049 if ( array_key_exists( $id, $sidebars_widgets ) ) { |
|
1050 $_sidebars_widgets[$id] = $sidebars_widgets[$id]; |
|
1051 unset($sidebars_widgets[$id], $sidebars[$id]); |
|
1052 } |
|
1053 } |
|
1054 |
|
1055 // move the rest to wp_inactive_widgets |
|
1056 if ( !isset($_sidebars_widgets['wp_inactive_widgets']) ) |
|
1057 $_sidebars_widgets['wp_inactive_widgets'] = array(); |
|
1058 |
|
1059 if ( !empty($sidebars_widgets) ) { |
|
1060 foreach ( $sidebars_widgets as $lost => $val ) { |
|
1061 if ( is_array($val) ) |
|
1062 $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val ); |
|
1063 } |
|
1064 } |
|
1065 |
|
1066 $sidebars_widgets = $_sidebars_widgets; |
|
1067 unset($_sidebars_widgets); |
|
1068 } |
|
1069 } |
|
1070 } |
|
1071 |
|
1072 if ( isset($sidebars_widgets['array_version']) ) |
|
1073 unset($sidebars_widgets['array_version']); |
1014 unset($sidebars_widgets['array_version']); |
1074 |
1015 |
1075 $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets); |
1016 $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets); |
1076 return $sidebars_widgets; |
1017 return $sidebars_widgets; |
1077 } |
1018 } |
1166 |
1107 |
1167 return $settings; |
1108 return $settings; |
1168 } |
1109 } |
1169 |
1110 |
1170 /** |
1111 /** |
1171 * Deprecated API |
|
1172 */ |
|
1173 |
|
1174 /** |
|
1175 * Register widget for sidebar with backwards compatibility. |
|
1176 * |
|
1177 * Allows $name to be an array that accepts either three elements to grab the |
|
1178 * first element and the third for the name or just uses the first element of |
|
1179 * the array for the name. |
|
1180 * |
|
1181 * Passes to {@link wp_register_sidebar_widget()} after argument list and |
|
1182 * backwards compatibility is complete. |
|
1183 * |
|
1184 * @since 2.2.0 |
|
1185 * @uses wp_register_sidebar_widget() Passes the compiled arguments. |
|
1186 * |
|
1187 * @param string|int $name Widget ID. |
|
1188 * @param callback $output_callback Run when widget is called. |
|
1189 * @param string $classname Classname widget option. |
|
1190 * @param mixed $params,... Widget parameters. |
|
1191 */ |
|
1192 function register_sidebar_widget($name, $output_callback, $classname = '') { |
|
1193 // Compat |
|
1194 if ( is_array($name) ) { |
|
1195 if ( count($name) == 3 ) |
|
1196 $name = sprintf($name[0], $name[2]); |
|
1197 else |
|
1198 $name = $name[0]; |
|
1199 } |
|
1200 |
|
1201 $id = sanitize_title($name); |
|
1202 $options = array(); |
|
1203 if ( !empty($classname) && is_string($classname) ) |
|
1204 $options['classname'] = $classname; |
|
1205 $params = array_slice(func_get_args(), 2); |
|
1206 $args = array($id, $name, $output_callback, $options); |
|
1207 if ( !empty($params) ) |
|
1208 $args = array_merge($args, $params); |
|
1209 |
|
1210 call_user_func_array('wp_register_sidebar_widget', $args); |
|
1211 } |
|
1212 |
|
1213 /** |
|
1214 * Alias of {@link wp_unregister_sidebar_widget()}. |
|
1215 * |
|
1216 * @see wp_unregister_sidebar_widget() |
|
1217 * |
|
1218 * @since 2.2.0 |
|
1219 * |
|
1220 * @param int|string $id Widget ID. |
|
1221 */ |
|
1222 function unregister_sidebar_widget($id) { |
|
1223 return wp_unregister_sidebar_widget($id); |
|
1224 } |
|
1225 |
|
1226 /** |
|
1227 * Registers widget control callback for customizing options. |
|
1228 * |
|
1229 * Allows $name to be an array that accepts either three elements to grab the |
|
1230 * first element and the third for the name or just uses the first element of |
|
1231 * the array for the name. |
|
1232 * |
|
1233 * Passes to {@link wp_register_widget_control()} after the argument list has |
|
1234 * been compiled. |
|
1235 * |
|
1236 * @since 2.2.0 |
|
1237 * |
|
1238 * @param int|string $name Sidebar ID. |
|
1239 * @param callback $control_callback Widget control callback to display and process form. |
|
1240 * @param int $width Widget width. |
|
1241 * @param int $height Widget height. |
|
1242 */ |
|
1243 function register_widget_control($name, $control_callback, $width = '', $height = '') { |
|
1244 // Compat |
|
1245 if ( is_array($name) ) { |
|
1246 if ( count($name) == 3 ) |
|
1247 $name = sprintf($name[0], $name[2]); |
|
1248 else |
|
1249 $name = $name[0]; |
|
1250 } |
|
1251 |
|
1252 $id = sanitize_title($name); |
|
1253 $options = array(); |
|
1254 if ( !empty($width) ) |
|
1255 $options['width'] = $width; |
|
1256 if ( !empty($height) ) |
|
1257 $options['height'] = $height; |
|
1258 $params = array_slice(func_get_args(), 4); |
|
1259 $args = array($id, $name, $control_callback, $options); |
|
1260 if ( !empty($params) ) |
|
1261 $args = array_merge($args, $params); |
|
1262 |
|
1263 call_user_func_array('wp_register_widget_control', $args); |
|
1264 } |
|
1265 |
|
1266 /** |
|
1267 * Alias of {@link wp_unregister_widget_control()}. |
|
1268 * |
|
1269 * @since 2.2.0 |
|
1270 * @see wp_unregister_widget_control() |
|
1271 * |
|
1272 * @param int|string $id Widget ID. |
|
1273 */ |
|
1274 function unregister_widget_control($id) { |
|
1275 return wp_unregister_widget_control($id); |
|
1276 } |
|
1277 |
|
1278 /** |
|
1279 * Output an arbitrary widget as a template tag |
1112 * Output an arbitrary widget as a template tag |
1280 * |
1113 * |
1281 * @since 2.8 |
1114 * @since 2.8 |
1282 * |
1115 * |
1283 * @param string $widget the widget's PHP class name (see default-widgets.php) |
1116 * @param string $widget the widget's PHP class name (see default-widgets.php) |
1290 |
1123 |
1291 $widget_obj = $wp_widget_factory->widgets[$widget]; |
1124 $widget_obj = $wp_widget_factory->widgets[$widget]; |
1292 if ( !is_a($widget_obj, 'WP_Widget') ) |
1125 if ( !is_a($widget_obj, 'WP_Widget') ) |
1293 return; |
1126 return; |
1294 |
1127 |
1295 $before_widget = sprintf('<div class="widget %s">', $widget_obj->widget_options['classname']); |
1128 $before_widget = sprintf('<div class="widget %s">', $widget_obj->widget_options['classname'] ); |
1296 $default_args = array('before_widget' => $before_widget, 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>'); |
1129 $default_args = array( 'before_widget' => $before_widget, 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' ); |
1297 |
1130 |
1298 $args = wp_parse_args($args, $default_args); |
1131 $args = wp_parse_args($args, $default_args); |
1299 $instance = wp_parse_args($instance); |
1132 $instance = wp_parse_args($instance); |
1300 |
1133 |
|
1134 do_action( 'the_widget', $widget, $instance, $args ); |
|
1135 |
1301 $widget_obj->_set(-1); |
1136 $widget_obj->_set(-1); |
1302 $widget_obj->widget($args, $instance); |
1137 $widget_obj->widget($args, $instance); |
1303 } |
1138 } |
1304 |
1139 |
1305 /** |
1140 /** |
1306 * Private |
1141 * Private |
1307 */ |
1142 */ |
1308 function _get_widget_id_base($id) { |
1143 function _get_widget_id_base($id) { |
1309 return preg_replace( '/-[0-9]+$/', '', $id ); |
1144 return preg_replace( '/-[0-9]+$/', '', $id ); |
1310 } |
1145 } |
|
1146 |
|
1147 /** |
|
1148 * Handle sidebars config after theme change |
|
1149 * |
|
1150 * @access private |
|
1151 * @since 3.3.0 |
|
1152 */ |
|
1153 function _wp_sidebars_changed() { |
|
1154 global $sidebars_widgets; |
|
1155 |
|
1156 if ( ! is_array( $sidebars_widgets ) ) |
|
1157 $sidebars_widgets = wp_get_sidebars_widgets(); |
|
1158 |
|
1159 retrieve_widgets(true); |
|
1160 } |
|
1161 |
|
1162 // look for "lost" widgets, this has to run at least on each theme change |
|
1163 function retrieve_widgets($theme_changed = false) { |
|
1164 global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets; |
|
1165 |
|
1166 $registered_sidebar_keys = array_keys( $wp_registered_sidebars ); |
|
1167 $orphaned = 0; |
|
1168 |
|
1169 $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' ); |
|
1170 if ( is_array( $old_sidebars_widgets ) ) { |
|
1171 // time() that sidebars were stored is in $old_sidebars_widgets['time'] |
|
1172 $_sidebars_widgets = $old_sidebars_widgets['data']; |
|
1173 remove_theme_mod( 'sidebars_widgets' ); |
|
1174 |
|
1175 foreach ( $_sidebars_widgets as $sidebar => $widgets ) { |
|
1176 if ( 'wp_inactive_widgets' == $sidebar || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) |
|
1177 continue; |
|
1178 |
|
1179 if ( !in_array( $sidebar, $registered_sidebar_keys ) ) { |
|
1180 $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $widgets; |
|
1181 unset( $_sidebars_widgets[$sidebar] ); |
|
1182 } |
|
1183 } |
|
1184 } else { |
|
1185 if ( empty( $sidebars_widgets ) ) |
|
1186 return; |
|
1187 |
|
1188 unset( $sidebars_widgets['array_version'] ); |
|
1189 |
|
1190 $old = array_keys($sidebars_widgets); |
|
1191 sort($old); |
|
1192 sort($registered_sidebar_keys); |
|
1193 |
|
1194 if ( $old == $registered_sidebar_keys ) |
|
1195 return; |
|
1196 |
|
1197 $_sidebars_widgets = array( |
|
1198 'wp_inactive_widgets' => !empty( $sidebars_widgets['wp_inactive_widgets'] ) ? $sidebars_widgets['wp_inactive_widgets'] : array() |
|
1199 ); |
|
1200 |
|
1201 unset( $sidebars_widgets['wp_inactive_widgets'] ); |
|
1202 |
|
1203 foreach ( $wp_registered_sidebars as $id => $settings ) { |
|
1204 if ( $theme_changed ) { |
|
1205 $_sidebars_widgets[$id] = array_shift( $sidebars_widgets ); |
|
1206 } else { |
|
1207 // no theme change, grab only sidebars that are currently registered |
|
1208 if ( isset( $sidebars_widgets[$id] ) ) { |
|
1209 $_sidebars_widgets[$id] = $sidebars_widgets[$id]; |
|
1210 unset( $sidebars_widgets[$id] ); |
|
1211 } |
|
1212 } |
|
1213 } |
|
1214 |
|
1215 foreach ( $sidebars_widgets as $val ) { |
|
1216 if ( is_array($val) && ! empty( $val ) ) |
|
1217 $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $val; |
|
1218 } |
|
1219 } |
|
1220 |
|
1221 // discard invalid, theme-specific widgets from sidebars |
|
1222 $shown_widgets = array(); |
|
1223 |
|
1224 foreach ( $_sidebars_widgets as $sidebar => $widgets ) { |
|
1225 if ( !is_array($widgets) ) |
|
1226 continue; |
|
1227 |
|
1228 $_widgets = array(); |
|
1229 foreach ( $widgets as $widget ) { |
|
1230 if ( isset($wp_registered_widgets[$widget]) ) |
|
1231 $_widgets[] = $widget; |
|
1232 } |
|
1233 |
|
1234 $_sidebars_widgets[$sidebar] = $_widgets; |
|
1235 $shown_widgets = array_merge($shown_widgets, $_widgets); |
|
1236 } |
|
1237 |
|
1238 $sidebars_widgets = $_sidebars_widgets; |
|
1239 unset($_sidebars_widgets, $_widgets); |
|
1240 |
|
1241 // find hidden/lost multi-widget instances |
|
1242 $lost_widgets = array(); |
|
1243 foreach ( $wp_registered_widgets as $key => $val ) { |
|
1244 if ( in_array($key, $shown_widgets, true) ) |
|
1245 continue; |
|
1246 |
|
1247 $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key); |
|
1248 |
|
1249 if ( 2 > (int) $number ) |
|
1250 continue; |
|
1251 |
|
1252 $lost_widgets[] = $key; |
|
1253 } |
|
1254 |
|
1255 $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']); |
|
1256 wp_set_sidebars_widgets($sidebars_widgets); |
|
1257 |
|
1258 return $sidebars_widgets; |
|
1259 } |