PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/widgets.php

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 1259 lines | 643 code | 196 blank | 420 comment | 139 complexity | 679707098b8cc17e23b1d3af238ba1da MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * API for creating dynamic sidebar without hardcoding functionality into
  4. * themes. Includes both internal WordPress routines and theme use routines.
  5. *
  6. * This functionality was found in a plugin before WordPress 2.2 release which
  7. * included it in the core from that point on.
  8. *
  9. * @link http://codex.wordpress.org/Plugins/WordPress_Widgets WordPress Widgets
  10. * @link http://codex.wordpress.org/Plugins/WordPress_Widgets_Api Widgets API
  11. *
  12. * @package WordPress
  13. * @subpackage Widgets
  14. */
  15. /**
  16. * This class must be extended for each widget and WP_Widget::widget(), WP_Widget::update()
  17. * and WP_Widget::form() need to be over-ridden.
  18. *
  19. * @package WordPress
  20. * @subpackage Widgets
  21. * @since 2.8
  22. */
  23. class WP_Widget {
  24. var $id_base; // Root id for all widgets of this type.
  25. var $name; // Name for this widget type.
  26. var $widget_options; // Option array passed to wp_register_sidebar_widget()
  27. var $control_options; // Option array passed to wp_register_widget_control()
  28. var $number = false; // Unique ID number of the current instance.
  29. var $id = false; // Unique ID string of the current instance (id_base-number)
  30. var $updated = false; // Set true when we update the data after a POST submit - makes sure we don't do it twice.
  31. // Member functions that you must over-ride.
  32. /** Echo the widget content.
  33. *
  34. * Subclasses should over-ride this function to generate their widget code.
  35. *
  36. * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
  37. * @param array $instance The settings for the particular instance of the widget
  38. */
  39. function widget($args, $instance) {
  40. die('function WP_Widget::widget() must be over-ridden in a sub-class.');
  41. }
  42. /** Update a particular instance.
  43. *
  44. * This function should check that $new_instance is set correctly.
  45. * The newly calculated value of $instance should be returned.
  46. * If "false" is returned, the instance won't be saved/updated.
  47. *
  48. * @param array $new_instance New settings for this instance as input by the user via form()
  49. * @param array $old_instance Old settings for this instance
  50. * @return array Settings to save or bool false to cancel saving
  51. */
  52. function update($new_instance, $old_instance) {
  53. return $new_instance;
  54. }
  55. /** Echo the settings update form
  56. *
  57. * @param array $instance Current settings
  58. */
  59. function form($instance) {
  60. echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
  61. return 'noform';
  62. }
  63. // Functions you'll need to call.
  64. /**
  65. * PHP4 constructor
  66. */
  67. function WP_Widget( $id_base = false, $name, $widget_options = array(), $control_options = array() ) {
  68. WP_Widget::__construct( $id_base, $name, $widget_options, $control_options );
  69. }
  70. /**
  71. * PHP5 constructor
  72. *
  73. * @param string $id_base Optional Base ID for the widget, lower case,
  74. * if left empty a portion of the widget's class name will be used. Has to be unique.
  75. * @param string $name Name for the widget displayed on the configuration page.
  76. * @param array $widget_options Optional Passed to wp_register_sidebar_widget()
  77. * - description: shown on the configuration page
  78. * - classname
  79. * @param array $control_options Optional Passed to wp_register_widget_control()
  80. * - width: required if more than 250px
  81. * - height: currently not used but may be needed in the future
  82. */
  83. function __construct( $id_base = false, $name, $widget_options = array(), $control_options = array() ) {
  84. $this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
  85. $this->name = $name;
  86. $this->option_name = 'widget_' . $this->id_base;
  87. $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
  88. $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
  89. }
  90. /**
  91. * Constructs name attributes for use in form() fields
  92. *
  93. * This function should be used in form() methods to create name attributes for fields to be saved by update()
  94. *
  95. * @param string $field_name Field name
  96. * @return string Name attribute for $field_name
  97. */
  98. function get_field_name($field_name) {
  99. return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
  100. }
  101. /**
  102. * Constructs id attributes for use in form() fields
  103. *
  104. * This function should be used in form() methods to create id attributes for fields to be saved by update()
  105. *
  106. * @param string $field_name Field name
  107. * @return string ID attribute for $field_name
  108. */
  109. function get_field_id($field_name) {
  110. return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
  111. }
  112. // Private Functions. Don't worry about these.
  113. function _register() {
  114. $settings = $this->get_settings();
  115. $empty = true;
  116. if ( is_array($settings) ) {
  117. foreach ( array_keys($settings) as $number ) {
  118. if ( is_numeric($number) ) {
  119. $this->_set($number);
  120. $this->_register_one($number);
  121. $empty = false;
  122. }
  123. }
  124. }
  125. if ( $empty ) {
  126. // If there are none, we register the widget's existence with a
  127. // generic template
  128. $this->_set(1);
  129. $this->_register_one();
  130. }
  131. }
  132. function _set($number) {
  133. $this->number = $number;
  134. $this->id = $this->id_base . '-' . $number;
  135. }
  136. function _get_display_callback() {
  137. return array($this, 'display_callback');
  138. }
  139. function _get_update_callback() {
  140. return array($this, 'update_callback');
  141. }
  142. function _get_form_callback() {
  143. return array($this, 'form_callback');
  144. }
  145. /** Generate the actual widget content.
  146. * Just finds the instance and calls widget().
  147. * Do NOT over-ride this function. */
  148. function display_callback( $args, $widget_args = 1 ) {
  149. if ( is_numeric($widget_args) )
  150. $widget_args = array( 'number' => $widget_args );
  151. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  152. $this->_set( $widget_args['number'] );
  153. $instance = $this->get_settings();
  154. if ( array_key_exists( $this->number, $instance ) ) {
  155. $instance = $instance[$this->number];
  156. // filters the widget's settings, return false to stop displaying the widget
  157. $instance = apply_filters('widget_display_callback', $instance, $this, $args);
  158. if ( false !== $instance )
  159. $this->widget($args, $instance);
  160. }
  161. }
  162. /** Deal with changed settings.
  163. * Do NOT over-ride this function. */
  164. function update_callback( $widget_args = 1 ) {
  165. global $wp_registered_widgets;
  166. if ( is_numeric($widget_args) )
  167. $widget_args = array( 'number' => $widget_args );
  168. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  169. $all_instances = $this->get_settings();
  170. // We need to update the data
  171. if ( $this->updated )
  172. return;
  173. $sidebars_widgets = wp_get_sidebars_widgets();
  174. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
  175. // Delete the settings for this instance of the widget
  176. if ( isset($_POST['the-widget-id']) )
  177. $del_id = $_POST['the-widget-id'];
  178. else
  179. return;
  180. if ( isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
  181. $number = $wp_registered_widgets[$del_id]['params'][0]['number'];
  182. if ( $this->id_base . '-' . $number == $del_id )
  183. unset($all_instances[$number]);
  184. }
  185. } else {
  186. if ( isset($_POST['widget-' . $this->id_base]) && is_array($_POST['widget-' . $this->id_base]) ) {
  187. $settings = $_POST['widget-' . $this->id_base];
  188. } elseif ( isset($_POST['id_base']) && $_POST['id_base'] == $this->id_base ) {
  189. $num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
  190. $settings = array( $num => array() );
  191. } else {
  192. return;
  193. }
  194. foreach ( $settings as $number => $new_instance ) {
  195. $new_instance = stripslashes_deep($new_instance);
  196. $this->_set($number);
  197. $old_instance = isset($all_instances[$number]) ? $all_instances[$number] : array();
  198. $instance = $this->update($new_instance, $old_instance);
  199. // filters the widget's settings before saving, return false to cancel saving (keep the old settings if updating)
  200. $instance = apply_filters('widget_update_callback', $instance, $new_instance, $old_instance, $this);
  201. if ( false !== $instance )
  202. $all_instances[$number] = $instance;
  203. break; // run only once
  204. }
  205. }
  206. $this->save_settings($all_instances);
  207. $this->updated = true;
  208. }
  209. /** Generate the control form.
  210. * Do NOT over-ride this function. */
  211. function form_callback( $widget_args = 1 ) {
  212. if ( is_numeric($widget_args) )
  213. $widget_args = array( 'number' => $widget_args );
  214. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  215. $all_instances = $this->get_settings();
  216. if ( -1 == $widget_args['number'] ) {
  217. // We echo out a form where 'number' can be set later
  218. $this->_set('__i__');
  219. $instance = array();
  220. } else {
  221. $this->_set($widget_args['number']);
  222. $instance = $all_instances[ $widget_args['number'] ];
  223. }
  224. // filters the widget admin form before displaying, return false to stop displaying it
  225. $instance = apply_filters('widget_form_callback', $instance, $this);
  226. $return = null;
  227. if ( false !== $instance ) {
  228. $return = $this->form($instance);
  229. // add extra fields in the widget form - be sure to set $return to null if you add any
  230. // if the widget has no form the text echoed from the default form method can be hidden using css
  231. do_action_ref_array( 'in_widget_form', array(&$this, &$return, $instance) );
  232. }
  233. return $return;
  234. }
  235. /** Helper function: Registers a single instance. */
  236. function _register_one($number = -1) {
  237. wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
  238. _register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
  239. _register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
  240. }
  241. function save_settings($settings) {
  242. $settings['_multiwidget'] = 1;
  243. update_option( $this->option_name, $settings );
  244. }
  245. function get_settings() {
  246. $settings = get_option($this->option_name);
  247. if ( false === $settings && isset($this->alt_option_name) )
  248. $settings = get_option($this->alt_option_name);
  249. if ( !is_array($settings) )
  250. $settings = array();
  251. if ( !empty($settings) && !array_key_exists('_multiwidget', $settings) ) {
  252. // old format, convert if single widget
  253. $settings = wp_convert_widget_settings($this->id_base, $this->option_name, $settings);
  254. }
  255. unset($settings['_multiwidget'], $settings['__i__']);
  256. return $settings;
  257. }
  258. }
  259. /**
  260. * Singleton that registers and instantiates WP_Widget classes.
  261. *
  262. * @package WordPress
  263. * @subpackage Widgets
  264. * @since 2.8
  265. */
  266. class WP_Widget_Factory {
  267. var $widgets = array();
  268. function WP_Widget_Factory() {
  269. add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
  270. }
  271. function register($widget_class) {
  272. $this->widgets[$widget_class] = new $widget_class();
  273. }
  274. function unregister($widget_class) {
  275. if ( isset($this->widgets[$widget_class]) )
  276. unset($this->widgets[$widget_class]);
  277. }
  278. function _register_widgets() {
  279. global $wp_registered_widgets;
  280. $keys = array_keys($this->widgets);
  281. $registered = array_keys($wp_registered_widgets);
  282. $registered = array_map('_get_widget_id_base', $registered);
  283. foreach ( $keys as $key ) {
  284. // don't register new widget if old widget with the same id is already registered
  285. if ( in_array($this->widgets[$key]->id_base, $registered, true) ) {
  286. unset($this->widgets[$key]);
  287. continue;
  288. }
  289. $this->widgets[$key]->_register();
  290. }
  291. }
  292. }
  293. /* Global Variables */
  294. /** @ignore */
  295. global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
  296. /**
  297. * Stores the sidebars, since many themes can have more than one.
  298. *
  299. * @global array $wp_registered_sidebars
  300. * @since 2.2.0
  301. */
  302. $wp_registered_sidebars = array();
  303. /**
  304. * Stores the registered widgets.
  305. *
  306. * @global array $wp_registered_widgets
  307. * @since 2.2.0
  308. */
  309. $wp_registered_widgets = array();
  310. /**
  311. * Stores the registered widget control (options).
  312. *
  313. * @global array $wp_registered_widget_controls
  314. * @since 2.2.0
  315. */
  316. $wp_registered_widget_controls = array();
  317. $wp_registered_widget_updates = array();
  318. /**
  319. * Private
  320. */
  321. $_wp_sidebars_widgets = array();
  322. /**
  323. * Private
  324. */
  325. $GLOBALS['_wp_deprecated_widgets_callbacks'] = array(
  326. 'wp_widget_pages',
  327. 'wp_widget_pages_control',
  328. 'wp_widget_calendar',
  329. 'wp_widget_calendar_control',
  330. 'wp_widget_archives',
  331. 'wp_widget_archives_control',
  332. 'wp_widget_links',
  333. 'wp_widget_meta',
  334. 'wp_widget_meta_control',
  335. 'wp_widget_search',
  336. 'wp_widget_recent_entries',
  337. 'wp_widget_recent_entries_control',
  338. 'wp_widget_tag_cloud',
  339. 'wp_widget_tag_cloud_control',
  340. 'wp_widget_categories',
  341. 'wp_widget_categories_control',
  342. 'wp_widget_text',
  343. 'wp_widget_text_control',
  344. 'wp_widget_rss',
  345. 'wp_widget_rss_control',
  346. 'wp_widget_recent_comments',
  347. 'wp_widget_recent_comments_control'
  348. );
  349. /* Template tags & API functions */
  350. /**
  351. * Register a widget
  352. *
  353. * Registers a WP_Widget widget
  354. *
  355. * @since 2.8.0
  356. *
  357. * @see WP_Widget
  358. * @see WP_Widget_Factory
  359. * @uses WP_Widget_Factory
  360. *
  361. * @param string $widget_class The name of a class that extends WP_Widget
  362. */
  363. function register_widget($widget_class) {
  364. global $wp_widget_factory;
  365. $wp_widget_factory->register($widget_class);
  366. }
  367. /**
  368. * Unregister a widget
  369. *
  370. * Unregisters a WP_Widget widget. Useful for unregistering default widgets.
  371. * Run within a function hooked to the widgets_init action.
  372. *
  373. * @since 2.8.0
  374. *
  375. * @see WP_Widget
  376. * @see WP_Widget_Factory
  377. * @uses WP_Widget_Factory
  378. *
  379. * @param string $widget_class The name of a class that extends WP_Widget
  380. */
  381. function unregister_widget($widget_class) {
  382. global $wp_widget_factory;
  383. $wp_widget_factory->unregister($widget_class);
  384. }
  385. /**
  386. * Creates multiple sidebars.
  387. *
  388. * If you wanted to quickly create multiple sidebars for a theme or internally.
  389. * This function will allow you to do so. If you don't pass the 'name' and/or
  390. * 'id' in $args, then they will be built for you.
  391. *
  392. * The default for the name is "Sidebar #", with '#' being replaced with the
  393. * number the sidebar is currently when greater than one. If first sidebar, the
  394. * name will be just "Sidebar". The default for id is "sidebar-" followed by the
  395. * number the sidebar creation is currently at. If the id is provided, and multiple
  396. * sidebars are being defined, the id will have "-2" appended, and so on.
  397. *
  398. * @since 2.2.0
  399. *
  400. * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
  401. * @uses parse_str() Converts a string to an array to be used in the rest of the function.
  402. * @uses register_sidebar() Sends single sidebar information [name, id] to this
  403. * function to handle building the sidebar.
  404. *
  405. * @param int $number Number of sidebars to create.
  406. * @param string|array $args Builds Sidebar based off of 'name' and 'id' values.
  407. */
  408. function register_sidebars($number = 1, $args = array()) {
  409. global $wp_registered_sidebars;
  410. $number = (int) $number;
  411. if ( is_string($args) )
  412. parse_str($args, $args);
  413. for ( $i = 1; $i <= $number; $i++ ) {
  414. $_args = $args;
  415. if ( $number > 1 )
  416. $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
  417. else
  418. $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
  419. // Custom specified ID's are suffixed if they exist already.
  420. // Automatically generated sidebar names need to be suffixed regardless starting at -0
  421. if ( isset($args['id']) ) {
  422. $_args['id'] = $args['id'];
  423. $n = 2; // Start at -2 for conflicting custom ID's
  424. while ( isset($wp_registered_sidebars[$_args['id']]) )
  425. $_args['id'] = $args['id'] . '-' . $n++;
  426. } else {
  427. $n = count($wp_registered_sidebars);
  428. do {
  429. $_args['id'] = 'sidebar-' . ++$n;
  430. } while ( isset($wp_registered_sidebars[$_args['id']]) );
  431. }
  432. register_sidebar($_args);
  433. }
  434. }
  435. /**
  436. * Builds the definition for a single sidebar and returns the ID.
  437. *
  438. * The $args parameter takes either a string or an array with 'name' and 'id'
  439. * contained in either usage. It will be noted that the values will be applied
  440. * to all sidebars, so if creating more than one, it will be advised to allow
  441. * for WordPress to create the defaults for you.
  442. *
  443. * Example for string would be <code>'name=whatever;id=whatever1'</code> and for
  444. * the array it would be <code>array(
  445. * 'name' => 'whatever',
  446. * 'id' => 'whatever1')</code>.
  447. *
  448. * name - The name of the sidebar, which presumably the title which will be
  449. * displayed.
  450. * id - The unique identifier by which the sidebar will be called by.
  451. * before_widget - The content that will prepended to the widgets when they are
  452. * displayed.
  453. * after_widget - The content that will be appended to the widgets when they are
  454. * displayed.
  455. * before_title - The content that will be prepended to the title when displayed.
  456. * after_title - the content that will be appended to the title when displayed.
  457. *
  458. * <em>Content</em> is assumed to be HTML and should be formatted as such, but
  459. * doesn't have to be.
  460. *
  461. * @since 2.2.0
  462. * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
  463. *
  464. * @param string|array $args Builds Sidebar based off of 'name' and 'id' values
  465. * @return string The sidebar id that was added.
  466. */
  467. function register_sidebar($args = array()) {
  468. global $wp_registered_sidebars;
  469. $i = count($wp_registered_sidebars) + 1;
  470. $defaults = array(
  471. 'name' => sprintf(__('Sidebar %d'), $i ),
  472. 'id' => "sidebar-$i",
  473. 'description' => '',
  474. 'class' => '',
  475. 'before_widget' => '<li id="%1$s" class="widget %2$s">',
  476. 'after_widget' => "</li>\n",
  477. 'before_title' => '<h2 class="widgettitle">',
  478. 'after_title' => "</h2>\n",
  479. );
  480. $sidebar = wp_parse_args( $args, $defaults );
  481. $wp_registered_sidebars[$sidebar['id']] = $sidebar;
  482. add_theme_support('widgets');
  483. do_action( 'register_sidebar', $sidebar );
  484. return $sidebar['id'];
  485. }
  486. /**
  487. * Removes a sidebar from the list.
  488. *
  489. * @since 2.2.0
  490. *
  491. * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
  492. *
  493. * @param string $name The ID of the sidebar when it was added.
  494. */
  495. function unregister_sidebar( $name ) {
  496. global $wp_registered_sidebars;
  497. if ( isset( $wp_registered_sidebars[$name] ) )
  498. unset( $wp_registered_sidebars[$name] );
  499. }
  500. /**
  501. * Register widget for use in sidebars.
  502. *
  503. * The default widget option is 'classname' that can be override.
  504. *
  505. * The function can also be used to unregister widgets when $output_callback
  506. * parameter is an empty string.
  507. *
  508. * @since 2.2.0
  509. *
  510. * @uses $wp_registered_widgets Uses stored registered widgets.
  511. * @uses $wp_register_widget_defaults Retrieves widget defaults.
  512. *
  513. * @param int|string $id Widget ID.
  514. * @param string $name Widget display title.
  515. * @param callback $output_callback Run when widget is called.
  516. * @param array|string $options Optional. Widget Options.
  517. * @param mixed $params,... Widget parameters to add to widget.
  518. * @return null Will return if $output_callback is empty after removing widget.
  519. */
  520. function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
  521. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
  522. $id = strtolower($id);
  523. if ( empty($output_callback) ) {
  524. unset($wp_registered_widgets[$id]);
  525. return;
  526. }
  527. $id_base = _get_widget_id_base($id);
  528. if ( in_array($output_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($output_callback) ) {
  529. if ( isset($wp_registered_widget_controls[$id]) )
  530. unset($wp_registered_widget_controls[$id]);
  531. if ( isset($wp_registered_widget_updates[$id_base]) )
  532. unset($wp_registered_widget_updates[$id_base]);
  533. return;
  534. }
  535. $defaults = array('classname' => $output_callback);
  536. $options = wp_parse_args($options, $defaults);
  537. $widget = array(
  538. 'name' => $name,
  539. 'id' => $id,
  540. 'callback' => $output_callback,
  541. 'params' => array_slice(func_get_args(), 4)
  542. );
  543. $widget = array_merge($widget, $options);
  544. if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) {
  545. do_action( 'wp_register_sidebar_widget', $widget );
  546. $wp_registered_widgets[$id] = $widget;
  547. }
  548. }
  549. /**
  550. * Retrieve description for widget.
  551. *
  552. * When registering widgets, the options can also include 'description' that
  553. * describes the widget for display on the widget administration panel or
  554. * in the theme.
  555. *
  556. * @since 2.5.0
  557. *
  558. * @param int|string $id Widget ID.
  559. * @return string Widget description, if available. Null on failure to retrieve description.
  560. */
  561. function wp_widget_description( $id ) {
  562. if ( !is_scalar($id) )
  563. return;
  564. global $wp_registered_widgets;
  565. if ( isset($wp_registered_widgets[$id]['description']) )
  566. return esc_html( $wp_registered_widgets[$id]['description'] );
  567. }
  568. /**
  569. * Retrieve description for a sidebar.
  570. *
  571. * When registering sidebars a 'description' parameter can be included that
  572. * describes the sidebar for display on the widget administration panel.
  573. *
  574. * @since 2.9.0
  575. *
  576. * @param int|string $id sidebar ID.
  577. * @return string Sidebar description, if available. Null on failure to retrieve description.
  578. */
  579. function wp_sidebar_description( $id ) {
  580. if ( !is_scalar($id) )
  581. return;
  582. global $wp_registered_sidebars;
  583. if ( isset($wp_registered_sidebars[$id]['description']) )
  584. return esc_html( $wp_registered_sidebars[$id]['description'] );
  585. }
  586. /**
  587. * Remove widget from sidebar.
  588. *
  589. * @since 2.2.0
  590. *
  591. * @param int|string $id Widget ID.
  592. */
  593. function wp_unregister_sidebar_widget($id) {
  594. do_action( 'wp_unregister_sidebar_widget', $id );
  595. wp_register_sidebar_widget($id, '', '');
  596. wp_unregister_widget_control($id);
  597. }
  598. /**
  599. * Registers widget control callback for customizing options.
  600. *
  601. * The options contains the 'height', 'width', and 'id_base' keys. The 'height'
  602. * option is never used. The 'width' option is the width of the fully expanded
  603. * control form, but try hard to use the default width. The 'id_base' is for
  604. * multi-widgets (widgets which allow multiple instances such as the text
  605. * widget), an id_base must be provided. The widget id will end up looking like
  606. * {$id_base}-{$unique_number}.
  607. *
  608. * @since 2.2.0
  609. *
  610. * @param int|string $id Sidebar ID.
  611. * @param string $name Sidebar display name.
  612. * @param callback $control_callback Run when sidebar is displayed.
  613. * @param array|string $options Optional. Widget options. See above long description.
  614. * @param mixed $params,... Optional. Additional parameters to add to widget.
  615. */
  616. function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
  617. global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
  618. $id = strtolower($id);
  619. $id_base = _get_widget_id_base($id);
  620. if ( empty($control_callback) ) {
  621. unset($wp_registered_widget_controls[$id]);
  622. unset($wp_registered_widget_updates[$id_base]);
  623. return;
  624. }
  625. if ( in_array($control_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($control_callback) ) {
  626. if ( isset($wp_registered_widgets[$id]) )
  627. unset($wp_registered_widgets[$id]);
  628. return;
  629. }
  630. if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
  631. return;
  632. $defaults = array('width' => 250, 'height' => 200 ); // height is never used
  633. $options = wp_parse_args($options, $defaults);
  634. $options['width'] = (int) $options['width'];
  635. $options['height'] = (int) $options['height'];
  636. $widget = array(
  637. 'name' => $name,
  638. 'id' => $id,
  639. 'callback' => $control_callback,
  640. 'params' => array_slice(func_get_args(), 4)
  641. );
  642. $widget = array_merge($widget, $options);
  643. $wp_registered_widget_controls[$id] = $widget;
  644. if ( isset($wp_registered_widget_updates[$id_base]) )
  645. return;
  646. if ( isset($widget['params'][0]['number']) )
  647. $widget['params'][0]['number'] = -1;
  648. unset($widget['width'], $widget['height'], $widget['name'], $widget['id']);
  649. $wp_registered_widget_updates[$id_base] = $widget;
  650. }
  651. function _register_widget_update_callback($id_base, $update_callback, $options = array()) {
  652. global $wp_registered_widget_updates;
  653. if ( isset($wp_registered_widget_updates[$id_base]) ) {
  654. if ( empty($update_callback) )
  655. unset($wp_registered_widget_updates[$id_base]);
  656. return;
  657. }
  658. $widget = array(
  659. 'callback' => $update_callback,
  660. 'params' => array_slice(func_get_args(), 3)
  661. );
  662. $widget = array_merge($widget, $options);
  663. $wp_registered_widget_updates[$id_base] = $widget;
  664. }
  665. function _register_widget_form_callback($id, $name, $form_callback, $options = array()) {
  666. global $wp_registered_widget_controls;
  667. $id = strtolower($id);
  668. if ( empty($form_callback) ) {
  669. unset($wp_registered_widget_controls[$id]);
  670. return;
  671. }
  672. if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
  673. return;
  674. $defaults = array('width' => 250, 'height' => 200 );
  675. $options = wp_parse_args($options, $defaults);
  676. $options['width'] = (int) $options['width'];
  677. $options['height'] = (int) $options['height'];
  678. $widget = array(
  679. 'name' => $name,
  680. 'id' => $id,
  681. 'callback' => $form_callback,
  682. 'params' => array_slice(func_get_args(), 4)
  683. );
  684. $widget = array_merge($widget, $options);
  685. $wp_registered_widget_controls[$id] = $widget;
  686. }
  687. /**
  688. * Remove control callback for widget.
  689. *
  690. * @since 2.2.0
  691. * @uses wp_register_widget_control() Unregisters by using empty callback.
  692. *
  693. * @param int|string $id Widget ID.
  694. */
  695. function wp_unregister_widget_control($id) {
  696. return wp_register_widget_control($id, '', '');
  697. }
  698. /**
  699. * Display dynamic sidebar.
  700. *
  701. * By default it displays the default sidebar or 'sidebar-1'. The 'sidebar-1' is
  702. * not named by the theme, the actual name is '1', but 'sidebar-' is added to
  703. * the registered sidebars for the name. If you named your sidebar 'after-post',
  704. * then the parameter $index will still be 'after-post', but the lookup will be
  705. * for 'sidebar-after-post'.
  706. *
  707. * It is confusing for the $index parameter, but just know that it should just
  708. * work. When you register the sidebar in the theme, you will use the same name
  709. * for this function or "Pay no heed to the man behind the curtain." Just accept
  710. * it as an oddity of WordPress sidebar register and display.
  711. *
  712. * @since 2.2.0
  713. *
  714. * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
  715. * @return bool True, if widget sidebar was found and called. False if not found or not called.
  716. */
  717. function dynamic_sidebar($index = 1) {
  718. global $wp_registered_sidebars, $wp_registered_widgets;
  719. if ( is_int($index) ) {
  720. $index = "sidebar-$index";
  721. } else {
  722. $index = sanitize_title($index);
  723. foreach ( (array) $wp_registered_sidebars as $key => $value ) {
  724. if ( sanitize_title($value['name']) == $index ) {
  725. $index = $key;
  726. break;
  727. }
  728. }
  729. }
  730. $sidebars_widgets = wp_get_sidebars_widgets();
  731. if ( empty( $sidebars_widgets ) )
  732. return false;
  733. if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
  734. return false;
  735. $sidebar = $wp_registered_sidebars[$index];
  736. $did_one = false;
  737. foreach ( (array) $sidebars_widgets[$index] as $id ) {
  738. if ( !isset($wp_registered_widgets[$id]) ) continue;
  739. $params = array_merge(
  740. array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
  741. (array) $wp_registered_widgets[$id]['params']
  742. );
  743. // Substitute HTML id and class attributes into before_widget
  744. $classname_ = '';
  745. foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
  746. if ( is_string($cn) )
  747. $classname_ .= '_' . $cn;
  748. elseif ( is_object($cn) )
  749. $classname_ .= '_' . get_class($cn);
  750. }
  751. $classname_ = ltrim($classname_, '_');
  752. $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
  753. $params = apply_filters( 'dynamic_sidebar_params', $params );
  754. $callback = $wp_registered_widgets[$id]['callback'];
  755. do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );
  756. if ( is_callable($callback) ) {
  757. call_user_func_array($callback, $params);
  758. $did_one = true;
  759. }
  760. }
  761. return $did_one;
  762. }
  763. /**
  764. * Whether widget is displayed on the front-end.
  765. *
  766. * Either $callback or $id_base can be used
  767. * $id_base is the first argument when extending WP_Widget class
  768. * Without the optional $widget_id parameter, returns the ID of the first sidebar
  769. * in which the first instance of the widget with the given callback or $id_base is found.
  770. * With the $widget_id parameter, returns the ID of the sidebar where
  771. * the widget with that callback/$id_base AND that ID is found.
  772. *
  773. * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
  774. * this function has to run after widgets have initialized, at action 'init' or later.
  775. *
  776. * @since 2.2.0
  777. *
  778. * @param string $callback Optional, Widget callback to check.
  779. * @param int $widget_id Optional, but needed for checking. Widget ID.
  780. * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget.
  781. * @param bool $skip_inactive Optional, whether to check in 'wp_inactive_widgets'.
  782. * @return mixed false if widget is not active or id of sidebar in which the widget is active.
  783. */
  784. function is_active_widget($callback = false, $widget_id = false, $id_base = false, $skip_inactive = true) {
  785. global $wp_registered_widgets;
  786. $sidebars_widgets = wp_get_sidebars_widgets();
  787. if ( is_array($sidebars_widgets) ) {
  788. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  789. if ( $skip_inactive && 'wp_inactive_widgets' == $sidebar )
  790. continue;
  791. if ( is_array($widgets) ) {
  792. foreach ( $widgets as $widget ) {
  793. if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && _get_widget_id_base($widget) == $id_base ) ) {
  794. if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
  795. return $sidebar;
  796. }
  797. }
  798. }
  799. }
  800. }
  801. return false;
  802. }
  803. /**
  804. * Whether the dynamic sidebar is enabled and used by theme.
  805. *
  806. * @since 2.2.0
  807. *
  808. * @return bool True, if using widgets. False, if not using widgets.
  809. */
  810. function is_dynamic_sidebar() {
  811. global $wp_registered_widgets, $wp_registered_sidebars;
  812. $sidebars_widgets = get_option('sidebars_widgets');
  813. foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
  814. if ( count($sidebars_widgets[$index]) ) {
  815. foreach ( (array) $sidebars_widgets[$index] as $widget )
  816. if ( array_key_exists($widget, $wp_registered_widgets) )
  817. return true;
  818. }
  819. }
  820. return false;
  821. }
  822. /**
  823. * Whether a sidebar is in use.
  824. *
  825. * @since 2.8
  826. *
  827. * @param mixed $index Sidebar name, id or number to check.
  828. * @return bool true if the sidebar is in use, false otherwise.
  829. */
  830. function is_active_sidebar( $index ) {
  831. $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
  832. $sidebars_widgets = wp_get_sidebars_widgets();
  833. if ( !empty($sidebars_widgets[$index]) )
  834. return true;
  835. return false;
  836. }
  837. /* Internal Functions */
  838. /**
  839. * Retrieve full list of sidebars and their widgets.
  840. *
  841. * Will upgrade sidebar widget list, if needed. Will also save updated list, if
  842. * needed.
  843. *
  844. * @since 2.2.0
  845. * @access private
  846. *
  847. * @param bool $deprecated Not used (deprecated).
  848. * @return array Upgraded list of widgets to version 3 array format when called from the admin.
  849. */
  850. function wp_get_sidebars_widgets($deprecated = true) {
  851. if ( $deprecated !== true )
  852. _deprecated_argument( __FUNCTION__, '2.8.1' );
  853. global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets;
  854. // If loading from front page, consult $_wp_sidebars_widgets rather than options
  855. // to see if wp_convert_widget_settings() has made manipulations in memory.
  856. if ( !is_admin() ) {
  857. if ( empty($_wp_sidebars_widgets) )
  858. $_wp_sidebars_widgets = get_option('sidebars_widgets', array());
  859. $sidebars_widgets = $_wp_sidebars_widgets;
  860. } else {
  861. $sidebars_widgets = get_option('sidebars_widgets', array());
  862. }
  863. if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) )
  864. unset($sidebars_widgets['array_version']);
  865. $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets);
  866. return $sidebars_widgets;
  867. }
  868. /**
  869. * Set the sidebar widget option to update sidebars.
  870. *
  871. * @since 2.2.0
  872. * @access private
  873. *
  874. * @param array $sidebars_widgets Sidebar widgets and their settings.
  875. */
  876. function wp_set_sidebars_widgets( $sidebars_widgets ) {
  877. if ( !isset( $sidebars_widgets['array_version'] ) )
  878. $sidebars_widgets['array_version'] = 3;
  879. update_option( 'sidebars_widgets', $sidebars_widgets );
  880. }
  881. /**
  882. * Retrieve default registered sidebars list.
  883. *
  884. * @since 2.2.0
  885. * @access private
  886. *
  887. * @return array
  888. */
  889. function wp_get_widget_defaults() {
  890. global $wp_registered_sidebars;
  891. $defaults = array();
  892. foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
  893. $defaults[$index] = array();
  894. return $defaults;
  895. }
  896. /**
  897. * Convert the widget settings from single to multi-widget format.
  898. *
  899. * @since 2.8.0
  900. *
  901. * @return array
  902. */
  903. function wp_convert_widget_settings($base_name, $option_name, $settings) {
  904. // This test may need expanding.
  905. $single = $changed = false;
  906. if ( empty($settings) ) {
  907. $single = true;
  908. } else {
  909. foreach ( array_keys($settings) as $number ) {
  910. if ( 'number' == $number )
  911. continue;
  912. if ( !is_numeric($number) ) {
  913. $single = true;
  914. break;
  915. }
  916. }
  917. }
  918. if ( $single ) {
  919. $settings = array( 2 => $settings );
  920. // If loading from the front page, update sidebar in memory but don't save to options
  921. if ( is_admin() ) {
  922. $sidebars_widgets = get_option('sidebars_widgets');
  923. } else {
  924. if ( empty($GLOBALS['_wp_sidebars_widgets']) )
  925. $GLOBALS['_wp_sidebars_widgets'] = get_option('sidebars_widgets', array());
  926. $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
  927. }
  928. foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
  929. if ( is_array($sidebar) ) {
  930. foreach ( $sidebar as $i => $name ) {
  931. if ( $base_name == $name ) {
  932. $sidebars_widgets[$index][$i] = "$name-2";
  933. $changed = true;
  934. break 2;
  935. }
  936. }
  937. }
  938. }
  939. if ( is_admin() && $changed )
  940. update_option('sidebars_widgets', $sidebars_widgets);
  941. }
  942. $settings['_multiwidget'] = 1;
  943. if ( is_admin() )
  944. update_option( $option_name, $settings );
  945. return $settings;
  946. }
  947. /**
  948. * Output an arbitrary widget as a template tag
  949. *
  950. * @since 2.8
  951. *
  952. * @param string $widget the widget's PHP class name (see default-widgets.php)
  953. * @param array $instance the widget's instance settings
  954. * @param array $args the widget's sidebar args
  955. * @return void
  956. **/
  957. function the_widget($widget, $instance = array(), $args = array()) {
  958. global $wp_widget_factory;
  959. $widget_obj = $wp_widget_factory->widgets[$widget];
  960. if ( !is_a($widget_obj, 'WP_Widget') )
  961. return;
  962. $before_widget = sprintf('<div class="widget %s">', $widget_obj->widget_options['classname'] );
  963. $default_args = array( 'before_widget' => $before_widget, 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' );
  964. $args = wp_parse_args($args, $default_args);
  965. $instance = wp_parse_args($instance);
  966. do_action( 'the_widget', $widget, $instance, $args );
  967. $widget_obj->_set(-1);
  968. $widget_obj->widget($args, $instance);
  969. }
  970. /**
  971. * Private
  972. */
  973. function _get_widget_id_base($id) {
  974. return preg_replace( '/-[0-9]+$/', '', $id );
  975. }
  976. /**
  977. * Handle sidebars config after theme change
  978. *
  979. * @access private
  980. * @since 3.3.0
  981. */
  982. function _wp_sidebars_changed() {
  983. global $sidebars_widgets;
  984. if ( ! is_array( $sidebars_widgets ) )
  985. $sidebars_widgets = wp_get_sidebars_widgets();
  986. retrieve_widgets(true);
  987. }
  988. // look for "lost" widgets, this has to run at least on each theme change
  989. function retrieve_widgets($theme_changed = false) {
  990. global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
  991. $registered_sidebar_keys = array_keys( $wp_registered_sidebars );
  992. $orphaned = 0;
  993. $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
  994. if ( is_array( $old_sidebars_widgets ) ) {
  995. // time() that sidebars were stored is in $old_sidebars_widgets['time']
  996. $_sidebars_widgets = $old_sidebars_widgets['data'];
  997. remove_theme_mod( 'sidebars_widgets' );
  998. foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
  999. if ( 'wp_inactive_widgets' == $sidebar || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
  1000. continue;
  1001. if ( !in_array( $sidebar, $registered_sidebar_keys ) ) {
  1002. $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $widgets;
  1003. unset( $_sidebars_widgets[$sidebar] );
  1004. }
  1005. }
  1006. } else {
  1007. if ( empty( $sidebars_widgets ) )
  1008. return;
  1009. unset( $sidebars_widgets['array_version'] );
  1010. $old = array_keys($sidebars_widgets);
  1011. sort($old);
  1012. sort($registered_sidebar_keys);
  1013. if ( $old == $registered_sidebar_keys )
  1014. return;
  1015. $_sidebars_widgets = array(
  1016. 'wp_inactive_widgets' => !empty( $sidebars_widgets['wp_inactive_widgets'] ) ? $sidebars_widgets['wp_inactive_widgets'] : array()
  1017. );
  1018. unset( $sidebars_widgets['wp_inactive_widgets'] );
  1019. foreach ( $wp_registered_sidebars as $id => $settings ) {
  1020. if ( $theme_changed ) {
  1021. $_sidebars_widgets[$id] = array_shift( $sidebars_widgets );
  1022. } else {
  1023. // no theme change, grab only sidebars that are currently registered
  1024. if ( isset( $sidebars_widgets[$id] ) ) {
  1025. $_sidebars_widgets[$id] = $sidebars_widgets[$id];
  1026. unset( $sidebars_widgets[$id] );
  1027. }
  1028. }
  1029. }
  1030. foreach ( $sidebars_widgets as $val ) {
  1031. if ( is_array($val) && ! empty( $val ) )
  1032. $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $val;
  1033. }
  1034. }
  1035. // discard invalid, theme-specific widgets from sidebars
  1036. $shown_widgets = array();
  1037. foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
  1038. if ( !is_array($widgets) )
  1039. continue;
  1040. $_widgets = array();
  1041. foreach ( $widgets as $widget ) {
  1042. if ( isset($wp_registered_widgets[$widget]) )
  1043. $_widgets[] = $widget;
  1044. }
  1045. $_sidebars_widgets[$sidebar] = $_widgets;
  1046. $shown_widgets = array_merge($shown_widgets, $_widgets);
  1047. }
  1048. $sidebars_widgets = $_sidebars_widgets;
  1049. unset($_sidebars_widgets, $_widgets);
  1050. // find hidden/lost multi-widget instances
  1051. $lost_widgets = array();
  1052. foreach ( $wp_registered_widgets as $key => $val ) {
  1053. if ( in_array($key, $shown_widgets, true) )
  1054. continue;
  1055. $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
  1056. if ( 2 > (int) $number )
  1057. continue;
  1058. $lost_widgets[] = $key;
  1059. }
  1060. $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
  1061. wp_set_sidebars_widgets($sidebars_widgets);
  1062. return $sidebars_widgets;
  1063. }