PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/widgets.php

https://bitbucket.org/lordmuffin/origin
PHP | 399 lines | 321 code | 61 blank | 17 comment | 67 complexity | 33b7e4485c4c1833856abf58c764f2db MD5 | raw file
  1. <?php
  2. /**
  3. * Widgets administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once( './admin.php' );
  10. /** WordPress Administration Widgets API */
  11. require_once(ABSPATH . 'wp-admin/includes/widgets.php');
  12. if ( ! current_user_can('edit_theme_options') )
  13. wp_die( __( 'Cheatin&#8217; uh?' ));
  14. $widgets_access = get_user_setting( 'widgets_access' );
  15. if ( isset($_GET['widgets-access']) ) {
  16. $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
  17. set_user_setting( 'widgets_access', $widgets_access );
  18. }
  19. function wp_widgets_access_body_class($classes) {
  20. return "$classes widgets_access ";
  21. }
  22. if ( 'on' == $widgets_access ) {
  23. add_filter( 'admin_body_class', 'wp_widgets_access_body_class' );
  24. } else {
  25. wp_enqueue_script('admin-widgets');
  26. if ( wp_is_mobile() )
  27. wp_enqueue_script( 'jquery-touch-punch' );
  28. }
  29. do_action( 'sidebar_admin_setup' );
  30. $title = __( 'Widgets' );
  31. $parent_file = 'themes.php';
  32. get_current_screen()->add_help_tab( array(
  33. 'id' => 'overview',
  34. 'title' => __('Overview'),
  35. 'content' =>
  36. '<p>' . __('Widgets are independent sections of content that can be placed into any widgetized area provided by your theme (commonly called sidebars). To populate your sidebars/widget areas with individual widgets, drag and drop the title bars into the desired area. By default, only the first widget area is expanded. To populate additional widget areas, click on their title bars to expand them.') . '</p>
  37. <p>' . __('The Available Widgets section contains all the widgets you can choose from. Once you drag a widget into a sidebar, it will open to allow you to configure its settings. When you are happy with the widget settings, click the Save button and the widget will go live on your site. If you click Delete, it will remove the widget.') . '</p>'
  38. ) );
  39. get_current_screen()->add_help_tab( array(
  40. 'id' => 'removing-reusing',
  41. 'title' => __('Removing and Reusing'),
  42. 'content' =>
  43. '<p>' . __('If you want to remove the widget but save its setting for possible future use, just drag it into the Inactive Widgets area. You can add them back anytime from there. This is especially helpful when you switch to a theme with fewer or different widget areas.') . '</p>
  44. <p>' . __('Widgets may be used multiple times. You can give each widget a title, to display on your site, but it&#8217;s not required.') . '</p>
  45. <p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>'
  46. ) );
  47. get_current_screen()->add_help_tab( array(
  48. 'id' => 'missing-widgets',
  49. 'title' => __('Missing Widgets'),
  50. 'content' =>
  51. '<p>' . __('Many themes show some sidebar widgets by default until you edit your sidebars, but they are not automatically displayed in your sidebar management tool. After you make your first widget change, you can re-add the default widgets by adding them from the Available Widgets area.') . '</p>' .
  52. '<p>' . __('When changing themes, there is often some variation in the number and setup of widget areas/sidebars and sometimes these conflicts make the transition a bit less smooth. If you changed themes and seem to be missing widgets, scroll down on this screen to the Inactive Widgets area, where all of your widgets and their settings will have been saved.') . '</p>'
  53. ) );
  54. get_current_screen()->set_help_sidebar(
  55. '<p><strong>' . __('For more information:') . '</strong></p>' .
  56. '<p>' . __('<a href="http://codex.wordpress.org/Appearance_Widgets_Screen" target="_blank">Documentation on Widgets</a>') . '</p>' .
  57. '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
  58. );
  59. if ( ! current_theme_supports( 'widgets' ) ) {
  60. wp_die( __( '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://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ) );
  61. }
  62. // These are the widgets grouped by sidebar
  63. $sidebars_widgets = wp_get_sidebars_widgets();
  64. if ( empty( $sidebars_widgets ) )
  65. $sidebars_widgets = wp_get_widget_defaults();
  66. foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
  67. if ( 'wp_inactive_widgets' == $sidebar_id )
  68. continue;
  69. if ( !isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
  70. if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar
  71. register_sidebar(array(
  72. 'name' => __( 'Inactive Sidebar (not used)' ),
  73. 'id' => $sidebar_id,
  74. 'class' => 'inactive-sidebar orphan-sidebar',
  75. 'description' => __( 'This sidebar is no longer available and does not show anywhere on your site. Remove each of the widgets below to fully remove this inactive sidebar.' ),
  76. 'before_widget' => '',
  77. 'after_widget' => '',
  78. 'before_title' => '',
  79. 'after_title' => '',
  80. ));
  81. } else {
  82. unset( $sidebars_widgets[ $sidebar_id ] );
  83. }
  84. }
  85. }
  86. // register the inactive_widgets area as sidebar
  87. register_sidebar(array(
  88. 'name' => __('Inactive Widgets'),
  89. 'id' => 'wp_inactive_widgets',
  90. 'class' => 'inactive-sidebar',
  91. 'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
  92. 'before_widget' => '',
  93. 'after_widget' => '',
  94. 'before_title' => '',
  95. 'after_title' => '',
  96. ));
  97. retrieve_widgets();
  98. // We're saving a widget without js
  99. if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
  100. $widget_id = $_POST['widget-id'];
  101. check_admin_referer("save-delete-widget-$widget_id");
  102. $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
  103. if ( $number ) {
  104. foreach ( $_POST as $key => $val ) {
  105. if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
  106. $_POST[$key] = array( $number => array_shift($val) );
  107. break;
  108. }
  109. }
  110. }
  111. $sidebar_id = $_POST['sidebar'];
  112. $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
  113. $id_base = $_POST['id_base'];
  114. $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
  115. // delete
  116. if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
  117. if ( !in_array($widget_id, $sidebar, true) ) {
  118. wp_redirect( admin_url('widgets.php?error=0') );
  119. exit;
  120. }
  121. $sidebar = array_diff( $sidebar, array($widget_id) );
  122. $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
  123. }
  124. $_POST['widget-id'] = $sidebar;
  125. foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
  126. if ( $name != $id_base || !is_callable($control['callback']) )
  127. continue;
  128. ob_start();
  129. call_user_func_array( $control['callback'], $control['params'] );
  130. ob_end_clean();
  131. break;
  132. }
  133. $sidebars_widgets[$sidebar_id] = $sidebar;
  134. // remove old position
  135. if ( !isset($_POST['delete_widget']) ) {
  136. foreach ( $sidebars_widgets as $key => $sb ) {
  137. if ( is_array($sb) )
  138. $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
  139. }
  140. array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
  141. }
  142. wp_set_sidebars_widgets($sidebars_widgets);
  143. wp_redirect( admin_url('widgets.php?message=0') );
  144. exit;
  145. }
  146. // Output the widget form without js
  147. if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
  148. $widget_id = $_GET['editwidget'];
  149. if ( isset($_GET['addnew']) ) {
  150. // Default to the first sidebar
  151. $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
  152. if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
  153. // Copy minimal info from an existing instance of this widget to a new instance
  154. foreach ( $wp_registered_widget_controls as $control ) {
  155. if ( $_GET['base'] === $control['id_base'] ) {
  156. $control_callback = $control['callback'];
  157. $multi_number = (int) $_GET['num'];
  158. $control['params'][0]['number'] = -1;
  159. $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
  160. $wp_registered_widget_controls[$control['id']] = $control;
  161. break;
  162. }
  163. }
  164. }
  165. }
  166. if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
  167. $control = $wp_registered_widget_controls[$widget_id];
  168. $control_callback = $control['callback'];
  169. } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
  170. $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
  171. }
  172. if ( !isset($name) )
  173. $name = esc_html( strip_tags($control['name']) );
  174. if ( !isset($sidebar) )
  175. $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
  176. if ( !isset($multi_number) )
  177. $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
  178. $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
  179. // show the widget form
  180. $width = ' style="width:' . max($control['width'], 350) . 'px"';
  181. $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
  182. require_once( './admin-header.php' ); ?>
  183. <div class="wrap">
  184. <?php screen_icon(); ?>
  185. <h2><?php echo esc_html( $title ); ?></h2>
  186. <div class="editwidget"<?php echo $width; ?>>
  187. <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
  188. <form action="widgets.php" method="post">
  189. <div class="widget-inside">
  190. <?php
  191. if ( is_callable( $control_callback ) )
  192. call_user_func_array( $control_callback, $control['params'] );
  193. else
  194. echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
  195. </div>
  196. <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
  197. <div class="widget-position">
  198. <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
  199. <?php
  200. foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
  201. echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
  202. if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
  203. echo '&nbsp;';
  204. } else {
  205. if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
  206. $j = 1;
  207. $sidebars_widgets[$sbname] = array();
  208. } else {
  209. $j = count($sidebars_widgets[$sbname]);
  210. if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
  211. $j++;
  212. }
  213. $selected = '';
  214. echo "\t\t<select name='{$sbname}_position'>\n";
  215. echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
  216. for ( $i = 1; $i <= $j; $i++ ) {
  217. if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
  218. $selected = selected( $i, $key + 1, false );
  219. echo "\t\t<option value='$i'$selected> $i </option>\n";
  220. }
  221. echo "\t\t</select>\n";
  222. }
  223. echo "</td></tr>\n";
  224. } ?>
  225. </tbody></table>
  226. </div>
  227. <div class="widget-control-actions">
  228. <?php
  229. if ( isset($_GET['addnew']) ) { ?>
  230. <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
  231. <?php
  232. } else {
  233. submit_button( __( 'Delete' ), 'button alignleft', 'removewidget', false );
  234. }
  235. submit_button( __( 'Save Widget' ), 'button-primary alignright', 'savewidget', false ); ?>
  236. <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
  237. <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
  238. <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
  239. <?php wp_nonce_field("save-delete-widget-$widget_id"); ?>
  240. <br class="clear" />
  241. </div>
  242. </form>
  243. </div>
  244. </div>
  245. <?php
  246. require_once( './admin-footer.php' );
  247. exit;
  248. }
  249. $messages = array(
  250. __('Changes saved.')
  251. );
  252. $errors = array(
  253. __('Error while saving.'),
  254. __('Error in displaying the widget settings form.')
  255. );
  256. require_once( './admin-header.php' ); ?>
  257. <div class="wrap">
  258. <?php screen_icon(); ?>
  259. <h2><?php echo esc_html( $title ); ?></h2>
  260. <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
  261. <div id="message" class="updated"><p><?php echo $messages[$_GET['message']]; ?></p></div>
  262. <?php } ?>
  263. <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
  264. <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
  265. <?php } ?>
  266. <?php do_action( 'widgets_admin_page' ); ?>
  267. <div class="widget-liquid-left">
  268. <div id="widgets-left">
  269. <div id="available-widgets" class="widgets-holder-wrap">
  270. <div class="sidebar-name">
  271. <div class="sidebar-name-arrow"><br /></div>
  272. <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _ex('Deactivate', 'removing-widget'); ?> <span></span></span></h3></div>
  273. <div class="widget-holder">
  274. <p class="description"><?php _e('Drag widgets from here to a sidebar on the right to activate them. Drag widgets back here to deactivate them and delete their settings.'); ?></p>
  275. <div id="widget-list">
  276. <?php wp_list_widgets(); ?>
  277. </div>
  278. <br class='clear' />
  279. </div>
  280. <br class="clear" />
  281. </div>
  282. <?php
  283. foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
  284. if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
  285. $wrap_class = 'widgets-holder-wrap';
  286. if ( !empty( $registered_sidebar['class'] ) )
  287. $wrap_class .= ' ' . $registered_sidebar['class'];
  288. ?>
  289. <div class="<?php echo esc_attr( $wrap_class ); ?>">
  290. <div class="sidebar-name">
  291. <div class="sidebar-name-arrow"><br /></div>
  292. <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
  293. <span class="spinner"></span>
  294. </h3>
  295. </div>
  296. <div class="widget-holder inactive">
  297. <?php wp_list_widget_controls( $registered_sidebar['id'] ); ?>
  298. <div class="clear"></div>
  299. </div>
  300. </div>
  301. <?php
  302. }
  303. }
  304. ?>
  305. </div>
  306. </div>
  307. <div class="widget-liquid-right">
  308. <div id="widgets-right">
  309. <?php
  310. $i = 0;
  311. foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
  312. if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
  313. continue;
  314. $wrap_class = 'widgets-holder-wrap';
  315. if ( !empty( $registered_sidebar['class'] ) )
  316. $wrap_class .= ' sidebar-' . $registered_sidebar['class'];
  317. if ( $i )
  318. $wrap_class .= ' closed'; ?>
  319. <div class="<?php echo esc_attr( $wrap_class ); ?>">
  320. <div class="sidebar-name">
  321. <div class="sidebar-name-arrow"><br /></div>
  322. <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
  323. <span class="spinner"></span></h3></div>
  324. <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
  325. </div>
  326. <?php
  327. $i++;
  328. } ?>
  329. </div>
  330. </div>
  331. <form action="" method="post">
  332. <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
  333. </form>
  334. <br class="clear" />
  335. </div>
  336. <?php
  337. do_action( 'sidebar_admin_page' );
  338. require_once( './admin-footer.php' );