/wp-admin/includes/widgets.php

  1. <?php 
  2. /** 
  3. * WordPress Widgets Administration API 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Administration 
  7. */ 
  8.  
  9. /** 
  10. * Display list of the available widgets. 
  11. * 
  12. * @since 2.5.0 
  13. * 
  14. * @global array $wp_registered_widgets 
  15. * @global array $wp_registered_widget_controls 
  16. */ 
  17. function wp_list_widgets() { 
  18. global $wp_registered_widgets, $wp_registered_widget_controls; 
  19.  
  20. $sort = $wp_registered_widgets; 
  21. usort( $sort, '_sort_name_callback' ); 
  22. $done = array(); 
  23.  
  24. foreach ( $sort as $widget ) { 
  25. if ( in_array( $widget['callback'], $done, true ) ) // We already showed this multi-widget 
  26. continue; 
  27.  
  28. $sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false ); 
  29. $done[] = $widget['callback']; 
  30.  
  31. if ( ! isset( $widget['params'][0] ) ) 
  32. $widget['params'][0] = array(); 
  33.  
  34. $args = array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' ); 
  35.  
  36. if ( isset($wp_registered_widget_controls[$widget['id']]['id_base']) && isset($widget['params'][0]['number']) ) { 
  37. $id_base = $wp_registered_widget_controls[$widget['id']]['id_base']; 
  38. $args['_temp_id'] = "$id_base-__i__"; 
  39. $args['_multi_num'] = next_widget_id_number($id_base); 
  40. $args['_add'] = 'multi'; 
  41. } else { 
  42. $args['_add'] = 'single'; 
  43. if ( $sidebar ) 
  44. $args['_hide'] = '1'; 
  45.  
  46. $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) ); 
  47. call_user_func_array( 'wp_widget_control', $args ); 
  48.  
  49. /** 
  50. * Callback to sort array by a 'name' key. 
  51. * 
  52. * @since 3.1.0 
  53. * @access private 
  54. * 
  55. * @return int 
  56. */ 
  57. function _sort_name_callback( $a, $b ) { 
  58. return strnatcasecmp( $a['name'], $b['name'] ); 
  59.  
  60. /** 
  61. * Show the widgets and their settings for a sidebar. 
  62. * Used in the admin widget config screen. 
  63. * 
  64. * @since 2.5.0 
  65. * 
  66. * @param string $sidebar Sidebar ID. 
  67. * @param string $sidebar_name Optional. Sidebar name. Default empty. 
  68. */ 
  69. function wp_list_widget_controls( $sidebar, $sidebar_name = '' ) { 
  70. add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' ); 
  71.  
  72. $description = wp_sidebar_description( $sidebar ); 
  73.  
  74. echo '<div id="' . esc_attr( $sidebar ) . '" class="widgets-sortables">'; 
  75.  
  76. if ( $sidebar_name ) { 
  77. ?> 
  78. <div class="sidebar-name"> 
  79. <div class="sidebar-name-arrow"><br /></div> 
  80. <h2><?php echo esc_html( $sidebar_name ); ?> <span class="spinner"></span></h2> 
  81. </div> 
  82. <?php 
  83.  
  84. if ( ! empty( $description ) ) { 
  85. ?> 
  86. <div class="sidebar-description"> 
  87. <p class="description"><?php echo $description; ?></p> 
  88. </div> 
  89. <?php 
  90.  
  91. dynamic_sidebar( $sidebar ); 
  92.  
  93. echo '</div>'; 
  94.  
  95. /** 
  96. * Retrieves the widget control arguments. 
  97. * 
  98. * @since 2.5.0 
  99. * 
  100. * @global array $wp_registered_widgets 
  101. * 
  102. * @staticvar int $i 
  103. * 
  104. * @param array $params 
  105. * @return array 
  106. */ 
  107. function wp_list_widget_controls_dynamic_sidebar( $params ) { 
  108. global $wp_registered_widgets; 
  109. static $i = 0; 
  110. $i++; 
  111.  
  112. $widget_id = $params[0]['widget_id']; 
  113. $id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id; 
  114. $hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : ''; 
  115.  
  116. $params[0]['before_widget'] = "<div id='widget-{$i}_{$id}' class='widget'$hidden>"; 
  117. $params[0]['after_widget'] = "</div>"; 
  118. $params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated 
  119. $params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated 
  120. if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) { 
  121. $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback']; 
  122. $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control'; 
  123.  
  124. return $params; 
  125.  
  126. /** 
  127. * 
  128. * @global array $wp_registered_widgets 
  129. * 
  130. * @param string $id_base 
  131. * @return int 
  132. */ 
  133. function next_widget_id_number( $id_base ) { 
  134. global $wp_registered_widgets; 
  135. $number = 1; 
  136.  
  137. foreach ( $wp_registered_widgets as $widget_id => $widget ) { 
  138. if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) ) 
  139. $number = max($number, $matches[1]); 
  140. $number++; 
  141.  
  142. return $number; 
  143.  
  144. /** 
  145. * Meta widget used to display the control form for a widget. 
  146. * 
  147. * Called from dynamic_sidebar(). 
  148. * 
  149. * @since 2.5.0 
  150. * 
  151. * @global array $wp_registered_widgets 
  152. * @global array $wp_registered_widget_controls 
  153. * @global array $sidebars_widgets 
  154. * 
  155. * @param array $sidebar_args 
  156. * @return array 
  157. */ 
  158. function wp_widget_control( $sidebar_args ) { 
  159. global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets; 
  160.  
  161. $widget_id = $sidebar_args['widget_id']; 
  162. $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false; 
  163. $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar 
  164. $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array(); 
  165. $widget = $wp_registered_widgets[$widget_id]; 
  166.  
  167. $id_format = $widget['id']; 
  168. $widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : ''; 
  169. $id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id; 
  170. $multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : ''; 
  171. $add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : ''; 
  172.  
  173. $before_form = isset( $sidebar_args['before_form'] ) ? $sidebar_args['before_form'] : '<form method="post">'; 
  174. $after_form = isset( $sidebar_args['after_form'] ) ? $sidebar_args['after_form'] : '</form>'; 
  175. $before_widget_content = isset( $sidebar_args['before_widget_content'] ) ? $sidebar_args['before_widget_content'] : '<div class="widget-content">'; 
  176. $after_widget_content = isset( $sidebar_args['after_widget_content'] ) ? $sidebar_args['after_widget_content'] : '</div>'; 
  177.  
  178. $query_arg = array( 'editwidget' => $widget['id'] ); 
  179. if ( $add_new ) { 
  180. $query_arg['addnew'] = 1; 
  181. if ( $multi_number ) { 
  182. $query_arg['num'] = $multi_number; 
  183. $query_arg['base'] = $id_base; 
  184. } else { 
  185. $query_arg['sidebar'] = $sidebar_id; 
  186. $query_arg['key'] = $key; 
  187.  
  188. /** 
  189. * We aren't showing a widget control, we're outputting a template 
  190. * for a multi-widget control. 
  191. */ 
  192. if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) { 
  193. // number == -1 implies a template where id numbers are replaced by a generic '__i__' 
  194. $control['params'][0]['number'] = -1; 
  195. // With id_base widget id's are constructed like {$id_base}-{$id_number}. 
  196. if ( isset($control['id_base']) ) 
  197. $id_format = $control['id_base'] . '-__i__'; 
  198.  
  199. $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback']; 
  200. unset($wp_registered_widgets[$widget_id]['_callback']); 
  201.  
  202. $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) ); 
  203. $has_form = 'noform'; 
  204.  
  205. echo $sidebar_args['before_widget']; ?> 
  206. <div class="widget-top"> 
  207. <div class="widget-title-action"> 
  208. <a class="widget-action hide-if-no-js" href="#available-widgets"></a> 
  209. <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>"> 
  210. <span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span> 
  211. <span class="add"><?php _ex( 'Add', 'widget' ); ?></span> 
  212. <span class="screen-reader-text"><?php echo $widget_title; ?></span> 
  213. </a> 
  214. </div> 
  215. <div class="widget-title"><h3><?php echo $widget_title; ?><span class="in-widget-title"></span></h3></div> 
  216. </div> 
  217.  
  218. <div class="widget-inside"> 
  219. <?php echo $before_form; ?> 
  220. <?php echo $before_widget_content; ?> 
  221. <?php 
  222. if ( isset( $control['callback'] ) ) { 
  223. $has_form = call_user_func_array( $control['callback'], $control['params'] ); 
  224. } else { 
  225. echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; 
  226. ?> 
  227. <?php echo $after_widget_content; ?> 
  228. <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" /> 
  229. <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" /> 
  230. <input type="hidden" name="widget-width" class="widget-width" value="<?php if (isset( $control['width'] )) echo esc_attr($control['width']); ?>" /> 
  231. <input type="hidden" name="widget-height" class="widget-height" value="<?php if (isset( $control['height'] )) echo esc_attr($control['height']); ?>" /> 
  232. <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" /> 
  233. <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" /> 
  234. <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" /> 
  235.  
  236. <div class="widget-control-actions"> 
  237. <div class="alignleft"> 
  238. <a class="widget-control-remove" href="#remove"><?php _e('Delete'); ?></a> | 
  239. <a class="widget-control-close" href="#close"><?php _e('Close'); ?></a> 
  240. </div> 
  241. <div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>"> 
  242. <?php submit_button( __( 'Save' ), 'primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $id_format ) . '-savewidget' ) ); ?> 
  243. <span class="spinner"></span> 
  244. </div> 
  245. <br class="clear" /> 
  246. </div> 
  247. <?php echo $after_form; ?> 
  248. </div> 
  249.  
  250. <div class="widget-description"> 
  251. <?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?> 
  252. </div> 
  253. <?php 
  254. echo $sidebar_args['after_widget']; 
  255.  
  256. return $sidebar_args; 
  257.  
  258. /** 
  259. * 
  260. * @param string $classes 
  261. * @return string 
  262. */ 
  263. function wp_widgets_access_body_class($classes) { 
  264. return "$classes widgets_access "; 
.