PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/widgets.php

https://github.com/exploradoreselectronicos/pressmark
PHP | 323 lines | 219 code | 70 blank | 34 comment | 37 complexity | be5d00065b54abf4550af1d7308d634d MD5 | raw file
  1. <?php
  2. require_once( 'admin.php' );
  3. require_once(ABSPATH . 'wp-admin/includes/widgets.php');
  4. if ( ! current_user_can('switch_themes') )
  5. wp_die( __( 'Cheatin&#8217; uh?' ));
  6. wp_enqueue_script( array( 'wp-lists', 'admin-widgets' ) );
  7. do_action( 'sidebar_admin_setup' );
  8. $title = __( 'Widgets' );
  9. $parent_file = 'themes.php';
  10. // $sidebar = What sidebar are we editing?
  11. if ( isset($_GET['sidebar']) && isset($wp_registered_sidebars[$_GET['sidebar']]) ) {
  12. $sidebar = attribute_escape( $_GET['sidebar'] );
  13. } elseif ( is_array($wp_registered_sidebars) && !empty($wp_registered_sidebars) ) {
  14. // By default we look at the first defined sidebar
  15. $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
  16. } else {
  17. // If no sidebars, die.
  18. require_once( 'admin-header.php' );
  19. ?>
  20. <div class="error">
  21. <p><?php _e( 'No Sidebars Defined' ); ?></p>
  22. </div>
  23. <div class="wrap">
  24. <p><?php _e( 'You are seeing this message because the theme you are currently using isn&#8217;t widget-aware, meaning that it has no sidebars that you are able to change. For information on making your theme widget-aware, please <a href="http://automattic.com/code/widgets/themes/">follow these instructions</a>.' ); /* TODO: article on codex */; ?></p>
  25. </div>
  26. <?php
  27. require_once( 'admin-footer.php' );
  28. exit;
  29. }
  30. // These are the widgets grouped by sidebar
  31. $sidebars_widgets = wp_get_sidebars_widgets();
  32. if ( empty( $sidebars_widgets ) )
  33. $sidebars_widgets = wp_get_widget_defaults();
  34. // for the sake of PHP warnings
  35. if ( empty( $sidebars_widgets[$sidebar] ) )
  36. $sidebars_widgets[$sidebar] = array();
  37. $http_post = 'post' == strtolower($_SERVER['REQUEST_METHOD']);
  38. // We're updating a sidebar
  39. if ( $http_post && isset($sidebars_widgets[$_POST['sidebar']]) ) {
  40. check_admin_referer( 'edit-sidebar_' . $_POST['sidebar'] );
  41. /* Hack #1
  42. * The widget_control is overloaded. It updates the widget's options AND echoes out the widget's HTML form.
  43. * Since we want to update before sending out any headers, we have to catch it with an output buffer,
  44. */
  45. ob_start();
  46. /* There can be multiple widgets of the same type, but the widget_control for that
  47. * widget type needs only be called once if it's a multi-widget.
  48. */
  49. $already_done = array();
  50. foreach ( $wp_registered_widget_controls as $name => $control ) {
  51. if ( in_array( $control['callback'], $already_done ) )
  52. continue;
  53. if ( is_callable( $control['callback'] ) ) {
  54. call_user_func_array( $control['callback'], $control['params'] );
  55. $control_output = ob_get_contents();
  56. if ( false !== strpos( $control_output, '%i%' ) ) // if it's a multi-widget, only call control function once.
  57. $already_done[] = $control['callback'];
  58. }
  59. ob_clean();
  60. }
  61. ob_end_clean();
  62. // Prophylactic. Take out empty ids.
  63. foreach ( (array) $_POST['widget-id'] as $key => $val )
  64. if ( !$val )
  65. unset($_POST['widget-id'][$key]);
  66. // Reset the key numbering and store
  67. $new_sidebar = isset( $_POST['widget-id'] ) && is_array( $_POST['widget-id'] ) ? array_values( $_POST['widget-id'] ) : array();
  68. $sidebars_widgets[$_POST['sidebar']] = $new_sidebar;
  69. wp_set_sidebars_widgets( $sidebars_widgets );
  70. wp_redirect( add_query_arg( 'message', 'updated' ) );
  71. exit;
  72. }
  73. // What widget (if any) are we editing
  74. $edit_widget = -1;
  75. $query_args = array('add', 'remove', 'key', 'edit', '_wpnonce', 'message', 'base' );
  76. if ( isset($_GET['add']) && $_GET['add'] ) {
  77. // Add to the end of the sidebar
  78. $control_callback;
  79. if ( isset($wp_registered_widgets[$_GET['add']]) ) {
  80. check_admin_referer( "add-widget_$_GET[add]" );
  81. $sidebars_widgets[$sidebar][] = $_GET['add'];
  82. wp_set_sidebars_widgets( $sidebars_widgets );
  83. } elseif ( isset($_GET['base']) && isset($_GET['key']) ) { // It's a multi-widget
  84. check_admin_referer( "add-widget_$_GET[add]" );
  85. // Copy minimal info from an existing instance of this widget to a new instance
  86. foreach ( $wp_registered_widget_controls as $control ) {
  87. if ( $_GET['base'] === $control['id_base'] ) {
  88. $control_callback = $control['callback'];
  89. $num = (int) $_GET['key'];
  90. $control['params'][0]['number'] = $num;
  91. $control['id'] = $control['id_base'] . '-' . $num;
  92. $wp_registered_widget_controls[$control['id']] = $control;
  93. $sidebars_widgets[$sidebar][] = $control['id'];
  94. break;
  95. }
  96. }
  97. }
  98. // it's a multi-widget. The only way to add multi-widgets without JS is to actually submit POST content...
  99. // so here we go
  100. if ( is_callable( $control_callback ) ) {
  101. require_once( 'admin-header.php' );
  102. ?>
  103. <div class="wrap">
  104. <h2><?php _e( 'Add Widget' ); ?></h2>
  105. <br />
  106. <form action="<?php echo clean_url( remove_query_arg( $query_args ) ); ?>" method="post">
  107. <ul class="widget-control-list">
  108. <li class="widget-list-control-item">
  109. <h4 class="widget-title"><?php echo $control['name']; ?></h4>
  110. <div class="widget-control" style="display: block;">
  111. <?php
  112. call_user_func_array( $control_callback, $control['params'] );
  113. ?>
  114. <div class="widget-control-actions">
  115. <input type="submit" class="button" value="<?php _e( 'Add Widget' ); ?>" />
  116. <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
  117. <?php wp_nonce_field ( 'edit-sidebar_' . $sidebar );
  118. foreach ( $sidebars_widgets[$sidebar] as $sidebar_widget_id ) : ?>
  119. <input type="hidden" name='widget-id[]' value="<?php echo $sidebar_widget_id; ?>" />
  120. <?php endforeach; ?>
  121. </div>
  122. </div>
  123. </li>
  124. </ul>
  125. </form>
  126. </div>
  127. <?php
  128. require_once( 'admin-footer.php' );
  129. exit;
  130. }
  131. wp_redirect( remove_query_arg( $query_args ) );
  132. exit;
  133. } elseif ( isset($_GET['remove']) && $_GET['remove'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
  134. // Remove from sidebar the widget of type $_GET['remove'] and in position $_GET['key']
  135. $key = (int) $_GET['key'];
  136. if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['remove']) ) && in_array($key, $keys) ) {
  137. check_admin_referer( "remove-widget_$_GET[remove]" );
  138. unset($sidebars_widgets[$sidebar][$key]);
  139. $sidebars_widgets[$sidebar] = array_values($sidebars_widgets[$sidebar]);
  140. wp_set_sidebars_widgets( $sidebars_widgets );
  141. }
  142. wp_redirect( remove_query_arg( $query_args ) );
  143. exit;
  144. } elseif ( isset($_GET['edit']) && $_GET['edit'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
  145. // Edit widget of type $_GET['edit'] and position $_GET['key']
  146. $key = (int) $_GET['key'];
  147. if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['edit']) ) && in_array($key, $keys) )
  148. $edit_widget = $key;
  149. }
  150. // Total number of registered sidebars
  151. $sidebar_widget_count = count($sidebars_widgets[$sidebar]);
  152. // This is sort of lame since "widget" won't be converted to "widgets" in the JS
  153. if ( 1 < $sidebars_count = count($wp_registered_sidebars) )
  154. $sidebar_info_text = __ngettext( 'You are using %1$s widget in the "%2$s" sidebar.', 'You are using %1$s widgets in the "%2$s" sidebar.', $sidebar_widget_count );
  155. else
  156. $sidebar_info_text = __ngettext( 'You are using %1$s widget in the sidebar.', 'You are using %1$s widgets in the sidebar.', $sidebar_widget_count );
  157. $sidebar_info_text = sprintf( wp_specialchars( $sidebar_info_text ), "<span id='widget-count'>$sidebar_widget_count</span>", $wp_registered_sidebars[$sidebar]['name'] );
  158. $page = isset($_GET['apage']) ? abs( (int) $_GET['apage'] ) : 1;
  159. /* TODO: Paginate widgets list
  160. $page_links = paginate_links( array(
  161. 'base' => add_query_arg( 'apage', '%#%' ),
  162. 'format' => '',
  163. 'total' => ceil(($total = 105 )/ 10),
  164. 'current' => $page
  165. ));
  166. */
  167. $page_links = '&nbsp;';
  168. // Unsanitized!
  169. $widget_search = isset($_GET['s']) ? $_GET['s'] : false;
  170. // Not entirely sure what all should be here
  171. $show_values = array(
  172. '' => $widget_search ? __( 'Show any widgets' ) : __( 'Show all widgets' ),
  173. 'unused' => __( 'Show unused widgets' ),
  174. 'used' => __( 'Show used widgets' )
  175. );
  176. $show = isset($_GET['show']) && isset($show_values[$_GET['show']]) ? attribute_escape( $_GET['show'] ) : false;
  177. $messages = array(
  178. 'updated' => __('Changes saved.')
  179. );
  180. require_once( 'admin-header.php' );
  181. if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) : ?>
  182. <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
  183. <?php endif; ?>
  184. <div class="wrap">
  185. <form id="widgets-filter" action="" method="get">
  186. <h2><?php _e( 'Widgets' ); ?></h2>
  187. <p id="widget-search">
  188. <input type="text" id="widget-search-input" name="s" value="<?php echo attribute_escape( $widget_search ); ?>" />
  189. <input type="submit" class="button" value="<?php _e( 'Search Widgets' ); ?>" />
  190. </p>
  191. <div class="widget-liquid-left-holder">
  192. <div id="available-widgets-filter" class="widget-liquid-left">
  193. <h3><?php _e('Available Widgets'); ?></h3>
  194. <div class="nav">
  195. <select name="show">
  196. <?php foreach ( $show_values as $show_value => $show_text ) : $show_value = attribute_escape( $show_value ); ?>
  197. <option value='<?php echo $show_value; ?>'<?php selected( $show_value, $show ); ?>><?php echo wp_specialchars( $show_text ); ?></option>
  198. <?php endforeach; ?>
  199. </select>
  200. <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
  201. <p class="pagenav">
  202. <?php echo $page_links; ?>
  203. </p>
  204. </div>
  205. </div>
  206. </div>
  207. <div id="available-sidebars" class="widget-liquid-right">
  208. <h3><?php _e('Current Widgets'); ?></h3>
  209. <div class="nav">
  210. <select id="sidebar-selector" name="sidebar">
  211. <?php foreach ( $wp_registered_sidebars as $sidebar_id => $registered_sidebar ) : $sidebar_id = attribute_escape( $sidebar_id ); ?>
  212. <option value='<?php echo $sidebar_id; ?>'<?php selected( $sidebar_id, $sidebar ); ?>><?php echo wp_specialchars( $registered_sidebar['name'] ); ?></option>
  213. <?php endforeach; ?>
  214. </select>
  215. <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
  216. </div>
  217. </div>
  218. </form>
  219. <div id="widget-content" class="widget-liquid-left-holder">
  220. <div id="available-widgets" class="widget-liquid-left">
  221. <?php wp_list_widgets( $show, $widget_search ); // This lists all the widgets for the query ( $show, $search ) ?>
  222. <div class="nav">
  223. <p class="pagenav">
  224. <?php echo $page_links; ?>
  225. </p>
  226. </div>
  227. </div>
  228. </div>
  229. <form id="widget-controls" action="" method="post">
  230. <div id="current-widgets-head" class="widget-liquid-right">
  231. <div id="sidebar-info">
  232. <p><?php echo $sidebar_info_text; ?></p>
  233. <p><?php _e( 'Add more from the Available Widgets section.' ); ?></p>
  234. </div>
  235. </div>
  236. <div id="current-widgets" class="widget-liquid-right">
  237. <div id="current-sidebar">
  238. <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
  239. </div>
  240. <p class="submit">
  241. <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
  242. <input type="hidden" id="generated-time" name="generated-time" value="<?php echo time() - 1199145600; // Jan 1, 2008 ?>" />
  243. <input type="submit" name="save-widgets" value="<?php _e( 'Save Changes' ); ?>" />
  244. <?php
  245. wp_nonce_field( 'edit-sidebar_' . $sidebar );
  246. ?>
  247. </p>
  248. </div>
  249. </form>
  250. </div>
  251. <?php do_action( 'sidebar_admin_page' ); ?>
  252. <br class="clear" />
  253. <?php require_once( 'admin-footer.php' ); ?>