PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/widgets.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 1217 lines | 625 code | 181 blank | 411 comment | 133 complexity | 3823bc40db68e70051034f5de218fabf MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.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 existance 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 ( !array_key_exists('_multiwidget', $settings) ) {
  252. // old format, conver 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. $_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 mutliple
  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. 'before_widget' => '<li id="%1$s" class="widget %2$s">',
  475. 'after_widget' => "</li>\n",
  476. 'before_title' => '<h2 class="widgettitle">',
  477. 'after_title' => "</h2>\n",
  478. );
  479. $sidebar = wp_parse_args( $args, $defaults );
  480. $wp_registered_sidebars[$sidebar['id']] = $sidebar;
  481. add_theme_support('widgets');
  482. do_action( 'register_sidebar', $sidebar );
  483. return $sidebar['id'];
  484. }
  485. /**
  486. * Removes a sidebar from the list.
  487. *
  488. * @since 2.2.0
  489. *
  490. * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
  491. *
  492. * @param string $name The ID of the sidebar when it was added.
  493. */
  494. function unregister_sidebar( $name ) {
  495. global $wp_registered_sidebars;
  496. if ( isset( $wp_registered_sidebars[$name] ) )
  497. unset( $wp_registered_sidebars[$name] );
  498. }
  499. /**
  500. * Register widget for use in sidebars.
  501. *
  502. * The default widget option is 'classname' that can be override.
  503. *
  504. * The function can also be used to unregister widgets when $output_callback
  505. * parameter is an empty string.
  506. *
  507. * @since 2.2.0
  508. *
  509. * @uses $wp_registered_widgets Uses stored registered widgets.
  510. * @uses $wp_register_widget_defaults Retrieves widget defaults.
  511. *
  512. * @param int|string $id Widget ID.
  513. * @param string $name Widget display title.
  514. * @param callback $output_callback Run when widget is called.
  515. * @param array|string $options Optional. Widget Options.
  516. * @param mixed $params,... Widget parameters to add to widget.
  517. * @return null Will return if $output_callback is empty after removing widget.
  518. */
  519. function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
  520. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
  521. $id = strtolower($id);
  522. if ( empty($output_callback) ) {
  523. unset($wp_registered_widgets[$id]);
  524. return;
  525. }
  526. $id_base = _get_widget_id_base($id);
  527. if ( in_array($output_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($output_callback) ) {
  528. if ( isset($wp_registered_widget_controls[$id]) )
  529. unset($wp_registered_widget_controls[$id]);
  530. if ( isset($wp_registered_widget_updates[$id_base]) )
  531. unset($wp_registered_widget_updates[$id_base]);
  532. return;
  533. }
  534. $defaults = array('classname' => $output_callback);
  535. $options = wp_parse_args($options, $defaults);
  536. $widget = array(
  537. 'name' => $name,
  538. 'id' => $id,
  539. 'callback' => $output_callback,
  540. 'params' => array_slice(func_get_args(), 4)
  541. );
  542. $widget = array_merge($widget, $options);
  543. if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) {
  544. do_action( 'wp_register_sidebar_widget', $widget );
  545. $wp_registered_widgets[$id] = $widget;
  546. }
  547. }
  548. /**
  549. * Retrieve description for widget.
  550. *
  551. * When registering widgets, the options can also include 'description' that
  552. * describes the widget for display on the widget administration panel or
  553. * in the theme.
  554. *
  555. * @since 2.5.0
  556. *
  557. * @param int|string $id Widget ID.
  558. * @return string Widget description, if available. Null on failure to retrieve description.
  559. */
  560. function wp_widget_description( $id ) {
  561. if ( !is_scalar($id) )
  562. return;
  563. global $wp_registered_widgets;
  564. if ( isset($wp_registered_widgets[$id]['description']) )
  565. return esc_html( $wp_registered_widgets[$id]['description'] );
  566. }
  567. /**
  568. * Retrieve description for a sidebar.
  569. *
  570. * When registering sidebars a 'description' parameter can be included that
  571. * describes the sidebar for display on the widget administration panel.
  572. *
  573. * @since 2.9.0
  574. *
  575. * @param int|string $id sidebar ID.
  576. * @return string Sidebar description, if available. Null on failure to retrieve description.
  577. */
  578. function wp_sidebar_description( $id ) {
  579. if ( !is_scalar($id) )
  580. return;
  581. global $wp_registered_sidebars;
  582. if ( isset($wp_registered_sidebars[$id]['description']) )
  583. return esc_html( $wp_registered_sidebars[$id]['description'] );
  584. }
  585. /**
  586. * Remove widget from sidebar.
  587. *
  588. * @since 2.2.0
  589. *
  590. * @param int|string $id Widget ID.
  591. */
  592. function wp_unregister_sidebar_widget($id) {
  593. do_action( 'wp_unregister_sidebar_widget', $id );
  594. wp_register_sidebar_widget($id, '', '');
  595. wp_unregister_widget_control($id);
  596. }
  597. /**
  598. * Registers widget control callback for customizing options.
  599. *
  600. * The options contains the 'height', 'width', and 'id_base' keys. The 'height'
  601. * option is never used. The 'width' option is the width of the fully expanded
  602. * control form, but try hard to use the default width. The 'id_base' is for
  603. * multi-widgets (widgets which allow multiple instances such as the text
  604. * widget), an id_base must be provided. The widget id will end up looking like
  605. * {$id_base}-{$unique_number}.
  606. *
  607. * @since 2.2.0
  608. *
  609. * @param int|string $id Sidebar ID.
  610. * @param string $name Sidebar display name.
  611. * @param callback $control_callback Run when sidebar is displayed.
  612. * @param array|string $options Optional. Widget options. See above long description.
  613. * @param mixed $params,... Optional. Additional parameters to add to widget.
  614. */
  615. function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
  616. global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
  617. $id = strtolower($id);
  618. $id_base = _get_widget_id_base($id);
  619. if ( empty($control_callback) ) {
  620. unset($wp_registered_widget_controls[$id]);
  621. unset($wp_registered_widget_updates[$id_base]);
  622. return;
  623. }
  624. if ( in_array($control_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($control_callback) ) {
  625. if ( isset($wp_registered_widgets[$id]) )
  626. unset($wp_registered_widgets[$id]);
  627. return;
  628. }
  629. if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
  630. return;
  631. $defaults = array('width' => 250, 'height' => 200 ); // height is never used
  632. $options = wp_parse_args($options, $defaults);
  633. $options['width'] = (int) $options['width'];
  634. $options['height'] = (int) $options['height'];
  635. $widget = array(
  636. 'name' => $name,
  637. 'id' => $id,
  638. 'callback' => $control_callback,
  639. 'params' => array_slice(func_get_args(), 4)
  640. );
  641. $widget = array_merge($widget, $options);
  642. $wp_registered_widget_controls[$id] = $widget;
  643. if ( isset($wp_registered_widget_updates[$id_base]) )
  644. return;
  645. if ( isset($widget['params'][0]['number']) )
  646. $widget['params'][0]['number'] = -1;
  647. unset($widget['width'], $widget['height'], $widget['name'], $widget['id']);
  648. $wp_registered_widget_updates[$id_base] = $widget;
  649. }
  650. function _register_widget_update_callback($id_base, $update_callback, $options = array()) {
  651. global $wp_registered_widget_updates;
  652. if ( isset($wp_registered_widget_updates[$id_base]) ) {
  653. if ( empty($update_callback) )
  654. unset($wp_registered_widget_updates[$id_base]);
  655. return;
  656. }
  657. $widget = array(
  658. 'callback' => $update_callback,
  659. 'params' => array_slice(func_get_args(), 3)
  660. );
  661. $widget = array_merge($widget, $options);
  662. $wp_registered_widget_updates[$id_base] = $widget;
  663. }
  664. function _register_widget_form_callback($id, $name, $form_callback, $options = array()) {
  665. global $wp_registered_widget_controls;
  666. $id = strtolower($id);
  667. if ( empty($form_callback) ) {
  668. unset($wp_registered_widget_controls[$id]);
  669. return;
  670. }
  671. if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
  672. return;
  673. $defaults = array('width' => 250, 'height' => 200 );
  674. $options = wp_parse_args($options, $defaults);
  675. $options['width'] = (int) $options['width'];
  676. $options['height'] = (int) $options['height'];
  677. $widget = array(
  678. 'name' => $name,
  679. 'id' => $id,
  680. 'callback' => $form_callback,
  681. 'params' => array_slice(func_get_args(), 4)
  682. );
  683. $widget = array_merge($widget, $options);
  684. $wp_registered_widget_controls[$id] = $widget;
  685. }
  686. /**
  687. * Remove control callback for widget.
  688. *
  689. * @since 2.2.0
  690. * @uses wp_register_widget_control() Unregisters by using empty callback.
  691. *
  692. * @param int|string $id Widget ID.
  693. */
  694. function wp_unregister_widget_control($id) {
  695. return wp_register_widget_control($id, '', '');
  696. }
  697. /**
  698. * Display dynamic sidebar.
  699. *
  700. * By default it displays the default sidebar or 'sidebar-1'. The 'sidebar-1' is
  701. * not named by the theme, the actual name is '1', but 'sidebar-' is added to
  702. * the registered sidebars for the name. If you named your sidebar 'after-post',
  703. * then the parameter $index will still be 'after-post', but the lookup will be
  704. * for 'sidebar-after-post'.
  705. *
  706. * It is confusing for the $index parameter, but just know that it should just
  707. * work. When you register the sidebar in the theme, you will use the same name
  708. * for this function or "Pay no heed to the man behind the curtain." Just accept
  709. * it as an oddity of WordPress sidebar register and display.
  710. *
  711. * @since 2.2.0
  712. *
  713. * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
  714. * @return bool True, if widget sidebar was found and called. False if not found or not called.
  715. */
  716. function dynamic_sidebar($index = 1) {
  717. global $wp_registered_sidebars, $wp_registered_widgets;
  718. if ( is_int($index) ) {
  719. $index = "sidebar-$index";
  720. } else {
  721. $index = sanitize_title($index);
  722. foreach ( (array) $wp_registered_sidebars as $key => $value ) {
  723. if ( sanitize_title($value['name']) == $index ) {
  724. $index = $key;
  725. break;
  726. }
  727. }
  728. }
  729. $sidebars_widgets = wp_get_sidebars_widgets();
  730. if ( empty( $sidebars_widgets ) )
  731. return false;
  732. if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
  733. return false;
  734. $sidebar = $wp_registered_sidebars[$index];
  735. $did_one = false;
  736. foreach ( (array) $sidebars_widgets[$index] as $id ) {
  737. if ( !isset($wp_registered_widgets[$id]) ) continue;
  738. $params = array_merge(
  739. array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
  740. (array) $wp_registered_widgets[$id]['params']
  741. );
  742. // Substitute HTML id and class attributes into before_widget
  743. $classname_ = '';
  744. foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
  745. if ( is_string($cn) )
  746. $classname_ .= '_' . $cn;
  747. elseif ( is_object($cn) )
  748. $classname_ .= '_' . get_class($cn);
  749. }
  750. $classname_ = ltrim($classname_, '_');
  751. $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
  752. $params = apply_filters( 'dynamic_sidebar_params', $params );
  753. $callback = $wp_registered_widgets[$id]['callback'];
  754. do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );
  755. if ( is_callable($callback) ) {
  756. call_user_func_array($callback, $params);
  757. $did_one = true;
  758. }
  759. }
  760. return $did_one;
  761. }
  762. /**
  763. * Whether widget is displayed on the front-end.
  764. *
  765. * Either $callback or $id_base can be used
  766. * $id_base is the first argument when extending WP_Widget class
  767. * Without the optional $widget_id parameter, returns the ID of the first sidebar
  768. * in which the first instance of the widget with the given callback or $id_base is found.
  769. * With the $widget_id parameter, returns the ID of the sidebar where
  770. * the widget with that callback/$id_base AND that ID is found.
  771. *
  772. * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
  773. * this function has to run after widgets have initialized, at action 'init' or later.
  774. *
  775. * @since 2.2.0
  776. *
  777. * @param string $callback Optional, Widget callback to check.
  778. * @param int $widget_id Optional, but needed for checking. Widget ID.
  779. * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget.
  780. * @param bool $skip_inactive Optional, whether to check in 'wp_inactive_widgets'.
  781. * @return mixed false if widget is not active or id of sidebar in which the widget is active.
  782. */
  783. function is_active_widget($callback = false, $widget_id = false, $id_base = false, $skip_inactive = true) {
  784. global $wp_registered_widgets;
  785. $sidebars_widgets = wp_get_sidebars_widgets();
  786. if ( is_array($sidebars_widgets) ) {
  787. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  788. if ( $skip_inactive && 'wp_inactive_widgets' == $sidebar )
  789. continue;
  790. if ( is_array($widgets) ) {
  791. foreach ( $widgets as $widget ) {
  792. if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && _get_widget_id_base($widget) == $id_base ) ) {
  793. if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
  794. return $sidebar;
  795. }
  796. }
  797. }
  798. }
  799. }
  800. return false;
  801. }
  802. /**
  803. * Whether the dynamic sidebar is enabled and used by theme.
  804. *
  805. * @since 2.2.0
  806. *
  807. * @return bool True, if using widgets. False, if not using widgets.
  808. */
  809. function is_dynamic_sidebar() {
  810. global $wp_registered_widgets, $wp_registered_sidebars;
  811. $sidebars_widgets = get_option('sidebars_widgets');
  812. foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
  813. if ( count($sidebars_widgets[$index]) ) {
  814. foreach ( (array) $sidebars_widgets[$index] as $widget )
  815. if ( array_key_exists($widget, $wp_registered_widgets) )
  816. return true;
  817. }
  818. }
  819. return false;
  820. }
  821. /**
  822. * Whether a sidebar is in use.
  823. *
  824. * @since 2.8
  825. *
  826. * @param mixed $index Sidebar name, id or number to check.
  827. * @return bool true if the sidebar is in use, false otherwise.
  828. */
  829. function is_active_sidebar( $index ) {
  830. $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
  831. $sidebars_widgets = wp_get_sidebars_widgets();
  832. if ( !empty($sidebars_widgets[$index]) )
  833. return true;
  834. return false;
  835. }
  836. /* Internal Functions */
  837. /**
  838. * Retrieve full list of sidebars and their widgets.
  839. *
  840. * Will upgrade sidebar widget list, if needed. Will also save updated list, if
  841. * needed.
  842. *
  843. * @since 2.2.0
  844. * @access private
  845. *
  846. * @param bool $deprecated Not used (deprecated).
  847. * @return array Upgraded list of widgets to version 3 array format when called from the admin.
  848. */
  849. function wp_get_sidebars_widgets($deprecated = true) {
  850. if ( $deprecated !== true )
  851. _deprecated_argument( __FUNCTION__, '2.8.1' );
  852. global $wp_registered_widgets, $wp_registered_sidebars, $_wp_sidebars_widgets;
  853. // If loading from front page, consult $_wp_sidebars_widgets rather than options
  854. // to see if wp_convert_widget_settings() has made manipulations in memory.
  855. if ( !is_admin() ) {
  856. if ( empty($_wp_sidebars_widgets) )
  857. $_wp_sidebars_widgets = get_option('sidebars_widgets', array());
  858. $sidebars_widgets = $_wp_sidebars_widgets;
  859. } else {
  860. $sidebars_widgets = get_option('sidebars_widgets', array());
  861. $_sidebars_widgets = array();
  862. if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) )
  863. $sidebars_widgets['array_version'] = 3;
  864. elseif ( !isset($sidebars_widgets['array_version']) )
  865. $sidebars_widgets['array_version'] = 1;
  866. switch ( $sidebars_widgets['array_version'] ) {
  867. case 1 :
  868. foreach ( (array) $sidebars_widgets as $index => $sidebar )
  869. if ( is_array($sidebar) )
  870. foreach ( (array) $sidebar as $i => $name ) {
  871. $id = strtolower($name);
  872. if ( isset($wp_registered_widgets[$id]) ) {
  873. $_sidebars_widgets[$index][$i] = $id;
  874. continue;
  875. }
  876. $id = sanitize_title($name);
  877. if ( isset($wp_registered_widgets[$id]) ) {
  878. $_sidebars_widgets[$index][$i] = $id;
  879. continue;
  880. }
  881. $found = false;
  882. foreach ( $wp_registered_widgets as $widget_id => $widget ) {
  883. if ( strtolower($widget['name']) == strtolower($name) ) {
  884. $_sidebars_widgets[$index][$i] = $widget['id'];
  885. $found = true;
  886. break;
  887. } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
  888. $_sidebars_widgets[$index][$i] = $widget['id'];
  889. $found = true;
  890. break;
  891. }
  892. }
  893. if ( $found )
  894. continue;
  895. unset($_sidebars_widgets[$index][$i]);
  896. }
  897. $_sidebars_widgets['array_version'] = 2;
  898. $sidebars_widgets = $_sidebars_widgets;
  899. unset($_sidebars_widgets);
  900. case 2 :
  901. $sidebars = array_keys( $wp_registered_sidebars );
  902. if ( !empty( $sidebars ) ) {
  903. // Move the known-good ones first
  904. foreach ( (array) $sidebars as $id ) {
  905. if ( array_key_exists( $id, $sidebars_widgets ) ) {
  906. $_sidebars_widgets[$id] = $sidebars_widgets[$id];
  907. unset($sidebars_widgets[$id], $sidebars[$id]);
  908. }
  909. }
  910. // move the rest to wp_inactive_widgets
  911. if ( !isset($_sidebars_widgets['wp_inactive_widgets']) )
  912. $_sidebars_widgets['wp_inactive_widgets'] = array();
  913. if ( !empty($sidebars_widgets) ) {
  914. foreach ( $sidebars_widgets as $lost => $val ) {
  915. if ( is_array($val) )
  916. $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
  917. }
  918. }
  919. $sidebars_widgets = $_sidebars_widgets;
  920. unset($_sidebars_widgets);
  921. }
  922. }
  923. }
  924. if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) )
  925. unset($sidebars_widgets['array_version']);
  926. $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets);
  927. return $sidebars_widgets;
  928. }
  929. /**
  930. * Set the sidebar widget option to update sidebars.
  931. *
  932. * @since 2.2.0
  933. * @access private
  934. *
  935. * @param array $sidebars_widgets Sidebar widgets and their settings.
  936. */
  937. function wp_set_sidebars_widgets( $sidebars_widgets ) {
  938. if ( !isset( $sidebars_widgets['array_version'] ) )
  939. $sidebars_widgets['array_version'] = 3;
  940. update_option( 'sidebars_widgets', $sidebars_widgets );
  941. }
  942. /**
  943. * Retrieve default registered sidebars list.
  944. *
  945. * @since 2.2.0
  946. * @access private
  947. *
  948. * @return array
  949. */
  950. function wp_get_widget_defaults() {
  951. global $wp_registered_sidebars;
  952. $defaults = array();
  953. foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
  954. $defaults[$index] = array();
  955. return $defaults;
  956. }
  957. /**
  958. * Convert the widget settings from single to multi-widget format.
  959. *
  960. * @since 2.8.0
  961. *
  962. * @return array
  963. */
  964. function wp_convert_widget_settings($base_name, $option_name, $settings) {
  965. // This test may need expanding.
  966. $single = $changed = false;
  967. if ( empty($settings) ) {
  968. $single = true;
  969. } else {
  970. foreach ( array_keys($settings) as $number ) {
  971. if ( 'number' == $number )
  972. continue;
  973. if ( !is_numeric($number) ) {
  974. $single = true;
  975. break;
  976. }
  977. }
  978. }
  979. if ( $single ) {
  980. $settings = array( 2 => $settings );
  981. // If loading from the front page, update sidebar in memory but don't save to options
  982. if ( is_admin() ) {
  983. $sidebars_widgets = get_option('sidebars_widgets');
  984. } else {
  985. if ( empty($GLOBALS['_wp_sidebars_widgets']) )
  986. $GLOBALS['_wp_sidebars_widgets'] = get_option('sidebars_widgets', array());
  987. $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
  988. }
  989. foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
  990. if ( is_array($sidebar) ) {
  991. foreach ( $sidebar as $i => $name ) {
  992. if ( $base_name == $name ) {
  993. $sidebars_widgets[$index][$i] = "$name-2";
  994. $changed = true;
  995. break 2;
  996. }
  997. }
  998. }
  999. }
  1000. if ( is_admin() && $changed )
  1001. update_option('sidebars_widgets', $sidebars_widgets);
  1002. }
  1003. $settings['_multiwidget'] = 1;
  1004. if ( is_admin() )
  1005. update_option( $option_name, $settings );
  1006. return $settings;
  1007. }
  1008. /**
  1009. * Output an arbitrary widget as a template tag
  1010. *
  1011. * @since 2.8
  1012. *
  1013. * @param string $widget the widget's PHP class name (see default-widgets.php)
  1014. * @param array $instance the widget's instance settings
  1015. * @param array $args the widget's sidebar args
  1016. * @return void
  1017. **/
  1018. function the_widget($widget, $instance = array(), $args = array()) {
  1019. global $wp_widget_factory;
  1020. $widget_obj = $wp_widget_factory->widgets[$widget];
  1021. if ( !is_a($widget_obj, 'WP_Widget') )
  1022. return;
  1023. $before_widget = sprintf('<div class="widget %s">', $widget_obj->widget_options['classname']);
  1024. $default_args = array('before_widget' => $before_widget, 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>');
  1025. $args = wp_parse_args($args, $default_args);
  1026. $instance = wp_parse_args($instance);
  1027. do_action( 'the_widget', $widget, $instance, $args );
  1028. $widget_obj->_set(-1);
  1029. $widget_obj->widget($args, $instance);
  1030. }
  1031. /**
  1032. * Private
  1033. */
  1034. function _get_widget_id_base($id) {
  1035. return preg_replace( '/-[0-9]+$/', '', $id );
  1036. }