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

/wp-admin/widgets.php

https://gitlab.com/geyson/geyson
PHP | 464 lines | 357 code | 69 blank | 38 comment | 71 complexity | 5e537ca5a57c6e2f409f20b9ad3fd366 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Widgets administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once( dirname( __FILE__ ) . '/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?' ), 403 );
  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. /**
  20. *
  21. * @param string $classes
  22. * @return string
  23. */
  24. function wp_widgets_access_body_class($classes) {
  25. return "$classes widgets_access ";
  26. }
  27. if ( 'on' == $widgets_access ) {
  28. add_filter( 'admin_body_class', 'wp_widgets_access_body_class' );
  29. } else {
  30. wp_enqueue_script('admin-widgets');
  31. if ( wp_is_mobile() )
  32. wp_enqueue_script( 'jquery-touch-punch' );
  33. }
  34. /**
  35. * Fires early before the Widgets administration screen loads,
  36. * after scripts are enqueued.
  37. *
  38. * @since 2.2.0
  39. */
  40. do_action( 'sidebar_admin_setup' );
  41. $title = __( 'Widgets' );
  42. $parent_file = 'themes.php';
  43. get_current_screen()->add_help_tab( array(
  44. 'id' => 'overview',
  45. 'title' => __('Overview'),
  46. 'content' =>
  47. '<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>
  48. <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>'
  49. ) );
  50. get_current_screen()->add_help_tab( array(
  51. 'id' => 'removing-reusing',
  52. 'title' => __('Removing and Reusing'),
  53. 'content' =>
  54. '<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>
  55. <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>
  56. <p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>'
  57. ) );
  58. get_current_screen()->add_help_tab( array(
  59. 'id' => 'missing-widgets',
  60. 'title' => __('Missing Widgets'),
  61. 'content' =>
  62. '<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>' .
  63. '<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>'
  64. ) );
  65. get_current_screen()->set_help_sidebar(
  66. '<p><strong>' . __('For more information:') . '</strong></p>' .
  67. '<p>' . __('<a href="https://codex.wordpress.org/Appearance_Widgets_Screen" target="_blank">Documentation on Widgets</a>') . '</p>' .
  68. '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
  69. );
  70. if ( ! current_theme_supports( 'widgets' ) ) {
  71. 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="https://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ) );
  72. }
  73. // These are the widgets grouped by sidebar
  74. $sidebars_widgets = wp_get_sidebars_widgets();
  75. if ( empty( $sidebars_widgets ) )
  76. $sidebars_widgets = wp_get_widget_defaults();
  77. foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
  78. if ( 'wp_inactive_widgets' == $sidebar_id )
  79. continue;
  80. if ( !isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
  81. if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar
  82. register_sidebar(array(
  83. 'name' => __( 'Inactive Sidebar (not used)' ),
  84. 'id' => $sidebar_id,
  85. 'class' => 'inactive-sidebar orphan-sidebar',
  86. '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.' ),
  87. 'before_widget' => '',
  88. 'after_widget' => '',
  89. 'before_title' => '',
  90. 'after_title' => '',
  91. ));
  92. } else {
  93. unset( $sidebars_widgets[ $sidebar_id ] );
  94. }
  95. }
  96. }
  97. // register the inactive_widgets area as sidebar
  98. register_sidebar(array(
  99. 'name' => __('Inactive Widgets'),
  100. 'id' => 'wp_inactive_widgets',
  101. 'class' => 'inactive-sidebar',
  102. 'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
  103. 'before_widget' => '',
  104. 'after_widget' => '',
  105. 'before_title' => '',
  106. 'after_title' => '',
  107. ));
  108. retrieve_widgets();
  109. // We're saving a widget without js
  110. if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
  111. $widget_id = $_POST['widget-id'];
  112. check_admin_referer("save-delete-widget-$widget_id");
  113. $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
  114. if ( $number ) {
  115. foreach ( $_POST as $key => $val ) {
  116. if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
  117. $_POST[$key] = array( $number => array_shift($val) );
  118. break;
  119. }
  120. }
  121. }
  122. $sidebar_id = $_POST['sidebar'];
  123. $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
  124. $id_base = $_POST['id_base'];
  125. $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
  126. // Delete.
  127. if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
  128. if ( !in_array($widget_id, $sidebar, true) ) {
  129. wp_redirect( admin_url('widgets.php?error=0') );
  130. exit;
  131. }
  132. $sidebar = array_diff( $sidebar, array($widget_id) );
  133. $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
  134. }
  135. $_POST['widget-id'] = $sidebar;
  136. foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
  137. if ( $name != $id_base || !is_callable($control['callback']) )
  138. continue;
  139. ob_start();
  140. call_user_func_array( $control['callback'], $control['params'] );
  141. ob_end_clean();
  142. break;
  143. }
  144. $sidebars_widgets[$sidebar_id] = $sidebar;
  145. // Remove old position.
  146. if ( !isset($_POST['delete_widget']) ) {
  147. foreach ( $sidebars_widgets as $key => $sb ) {
  148. if ( is_array($sb) )
  149. $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
  150. }
  151. array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
  152. }
  153. wp_set_sidebars_widgets($sidebars_widgets);
  154. wp_redirect( admin_url('widgets.php?message=0') );
  155. exit;
  156. }
  157. // Output the widget form without js
  158. if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
  159. $widget_id = $_GET['editwidget'];
  160. if ( isset($_GET['addnew']) ) {
  161. // Default to the first sidebar
  162. $keys = array_keys( $wp_registered_sidebars );
  163. $sidebar = reset( $keys );
  164. if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
  165. // Copy minimal info from an existing instance of this widget to a new instance
  166. foreach ( $wp_registered_widget_controls as $control ) {
  167. if ( $_GET['base'] === $control['id_base'] ) {
  168. $control_callback = $control['callback'];
  169. $multi_number = (int) $_GET['num'];
  170. $control['params'][0]['number'] = -1;
  171. $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
  172. $wp_registered_widget_controls[$control['id']] = $control;
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
  179. $control = $wp_registered_widget_controls[$widget_id];
  180. $control_callback = $control['callback'];
  181. } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
  182. $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
  183. }
  184. if ( !isset($name) )
  185. $name = esc_html( strip_tags($control['name']) );
  186. if ( !isset($sidebar) )
  187. $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
  188. if ( !isset($multi_number) )
  189. $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
  190. $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
  191. // Show the widget form.
  192. $width = ' style="width:' . max($control['width'], 350) . 'px"';
  193. $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
  194. require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
  195. <div class="wrap">
  196. <h1><?php echo esc_html( $title ); ?></h1>
  197. <div class="editwidget"<?php echo $width; ?>>
  198. <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
  199. <form action="widgets.php" method="post">
  200. <div class="widget-inside">
  201. <?php
  202. if ( is_callable( $control_callback ) )
  203. call_user_func_array( $control_callback, $control['params'] );
  204. else
  205. echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
  206. </div>
  207. <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
  208. <div class="widget-position">
  209. <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
  210. <?php
  211. foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
  212. echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
  213. if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
  214. echo '&nbsp;';
  215. } else {
  216. if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
  217. $j = 1;
  218. $sidebars_widgets[$sbname] = array();
  219. } else {
  220. $j = count($sidebars_widgets[$sbname]);
  221. if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
  222. $j++;
  223. }
  224. $selected = '';
  225. echo "\t\t<select name='{$sbname}_position'>\n";
  226. echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
  227. for ( $i = 1; $i <= $j; $i++ ) {
  228. if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
  229. $selected = selected( $i, $key + 1, false );
  230. echo "\t\t<option value='$i'$selected> $i </option>\n";
  231. }
  232. echo "\t\t</select>\n";
  233. }
  234. echo "</td></tr>\n";
  235. } ?>
  236. </tbody></table>
  237. </div>
  238. <div class="widget-control-actions">
  239. <?php
  240. if ( isset($_GET['addnew']) ) { ?>
  241. <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
  242. <?php
  243. } else {
  244. submit_button( __( 'Delete' ), 'button alignleft', 'removewidget', false );
  245. }
  246. submit_button( __( 'Save Widget' ), 'button-primary alignright', 'savewidget', false ); ?>
  247. <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
  248. <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
  249. <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
  250. <?php wp_nonce_field("save-delete-widget-$widget_id"); ?>
  251. <br class="clear" />
  252. </div>
  253. </form>
  254. </div>
  255. </div>
  256. <?php
  257. require_once( ABSPATH . 'wp-admin/admin-footer.php' );
  258. exit;
  259. }
  260. $messages = array(
  261. __('Changes saved.')
  262. );
  263. $errors = array(
  264. __('Error while saving.'),
  265. __('Error in displaying the widget settings form.')
  266. );
  267. require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
  268. <div class="wrap">
  269. <h1>
  270. <?php
  271. echo esc_html( $title );
  272. if ( current_user_can( 'customize' ) ) {
  273. printf(
  274. ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
  275. esc_url( add_query_arg(
  276. array(
  277. array( 'autofocus' => array( 'panel' => 'widgets' ) ),
  278. 'return' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) )
  279. ),
  280. admin_url( 'customize.php' )
  281. ) ),
  282. __( 'Manage in Customizer' )
  283. );
  284. }
  285. ?>
  286. </h1>
  287. <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
  288. <div id="message" class="updated notice is-dismissible"><p><?php echo $messages[$_GET['message']]; ?></p></div>
  289. <?php } ?>
  290. <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
  291. <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
  292. <?php } ?>
  293. <?php
  294. /**
  295. * Fires before the Widgets administration page content loads.
  296. *
  297. * @since 3.0.0
  298. */
  299. do_action( 'widgets_admin_page' ); ?>
  300. <div class="widget-liquid-left">
  301. <div id="widgets-left">
  302. <div id="available-widgets" class="widgets-holder-wrap">
  303. <div class="sidebar-name">
  304. <div class="sidebar-name-arrow"><br /></div>
  305. <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _ex('Deactivate', 'removing-widget'); ?> <span></span></span></h3>
  306. </div>
  307. <div class="widget-holder">
  308. <div class="sidebar-description">
  309. <p class="description"><?php _e('To activate a widget drag it to a sidebar or click on it. To deactivate a widget and delete its settings, drag it back.'); ?></p>
  310. </div>
  311. <div id="widget-list">
  312. <?php wp_list_widgets(); ?>
  313. </div>
  314. <br class='clear' />
  315. </div>
  316. <br class="clear" />
  317. </div>
  318. <?php
  319. $theme_sidebars = array();
  320. foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
  321. if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
  322. $wrap_class = 'widgets-holder-wrap';
  323. if ( !empty( $registered_sidebar['class'] ) )
  324. $wrap_class .= ' ' . $registered_sidebar['class'];
  325. ?>
  326. <div class="<?php echo esc_attr( $wrap_class ); ?>">
  327. <div class="widget-holder inactive">
  328. <?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?>
  329. <div class="clear"></div>
  330. </div>
  331. </div>
  332. <?php
  333. } else {
  334. $theme_sidebars[$sidebar] = $registered_sidebar;
  335. }
  336. }
  337. ?>
  338. </div>
  339. </div>
  340. <?php
  341. $i = $split = 0;
  342. $single_sidebar_class = '';
  343. $sidebars_count = count( $theme_sidebars );
  344. if ( $sidebars_count > 1 ) {
  345. $split = ceil( $sidebars_count / 2 );
  346. } else {
  347. $single_sidebar_class = ' class="single-sidebar"';
  348. }
  349. ?>
  350. <div class="widget-liquid-right">
  351. <div id="widgets-right"<?php echo $single_sidebar_class; ?>>
  352. <div class="sidebars-column-1">
  353. <?php
  354. foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) {
  355. $wrap_class = 'widgets-holder-wrap';
  356. if ( !empty( $registered_sidebar['class'] ) )
  357. $wrap_class .= ' sidebar-' . $registered_sidebar['class'];
  358. if ( $i > 0 )
  359. $wrap_class .= ' closed';
  360. if ( $split && $i == $split ) {
  361. ?>
  362. </div><div class="sidebars-column-2">
  363. <?php
  364. }
  365. ?>
  366. <div class="<?php echo esc_attr( $wrap_class ); ?>">
  367. <?php wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); // Show the control forms for each of the widgets in this sidebar ?>
  368. </div>
  369. <?php
  370. $i++;
  371. }
  372. ?>
  373. </div>
  374. </div>
  375. </div>
  376. <form method="post">
  377. <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
  378. </form>
  379. <br class="clear" />
  380. </div>
  381. <div class="widgets-chooser">
  382. <ul class="widgets-chooser-sidebars"></ul>
  383. <div class="widgets-chooser-actions">
  384. <button class="button-secondary"><?php _e( 'Cancel' ); ?></button>
  385. <button class="button-primary"><?php _e( 'Add Widget' ); ?></button>
  386. </div>
  387. </div>
  388. <?php
  389. /**
  390. * Fires after the available widgets and sidebars have loaded, before the admin footer.
  391. *
  392. * @since 2.2.0
  393. */
  394. do_action( 'sidebar_admin_page' );
  395. require_once( ABSPATH . 'wp-admin/admin-footer.php' );