PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/widgets.php

https://github.com/schr/wordpress
PHP | 2147 lines | 1316 code | 238 blank | 593 comment | 259 complexity | 67e265fcd456a75333e7889abb0ddabe MD5 | raw file
  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. /* Global Variables */
  16. /** @ignore */
  17. global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
  18. /**
  19. * Stores the sidebars, since many themes can have more than one.
  20. *
  21. * @global array $wp_registered_sidebars
  22. * @since 2.2.0
  23. */
  24. $wp_registered_sidebars = array();
  25. /**
  26. * Stores the registered widgets.
  27. *
  28. * @global array $wp_registered_widgets
  29. * @since 2.2.0
  30. */
  31. $wp_registered_widgets = array();
  32. /**
  33. * Stores the registered widget control (options).
  34. *
  35. * @global array $wp_registered_widget_controls
  36. * @since 2.2.0
  37. */
  38. $wp_registered_widget_controls = array();
  39. /* Template tags & API functions */
  40. /**
  41. * Creates multiple sidebars.
  42. *
  43. * If you wanted to quickly create multiple sidebars for a theme or internally.
  44. * This function will allow you to do so. If you don't pass the 'name' and/or
  45. * 'id' in $args, then they will be built for you.
  46. *
  47. * The default for the name is "Sidebar #", with '#' being replaced with the
  48. * number the sidebar is currently when greater than one. If first sidebar, the
  49. * name will be just "Sidebar". The default for id is "sidebar-" followed by the
  50. * number the sidebar creation is currently at.
  51. *
  52. * @since 2.2.0
  53. *
  54. * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
  55. * @uses parse_str() Converts a string to an array to be used in the rest of the function.
  56. * @uses register_sidebar() Sends single sidebar information [name, id] to this
  57. * function to handle building the sidebar.
  58. *
  59. * @param int $number Number of sidebars to create.
  60. * @param string|array $args Builds Sidebar based off of 'name' and 'id' values.
  61. */
  62. function register_sidebars($number = 1, $args = array()) {
  63. global $wp_registered_sidebars;
  64. $number = (int) $number;
  65. if ( is_string($args) )
  66. parse_str($args, $args);
  67. for ( $i=1; $i <= $number; $i++ ) {
  68. $_args = $args;
  69. if ( $number > 1 ) {
  70. $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
  71. } else {
  72. $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
  73. }
  74. if (isset($args['id'])) {
  75. $_args['id'] = $args['id'];
  76. } else {
  77. $n = count($wp_registered_sidebars);
  78. do {
  79. $n++;
  80. $_args['id'] = "sidebar-$n";
  81. } while (isset($wp_registered_sidebars[$_args['id']]));
  82. }
  83. register_sidebar($_args);
  84. }
  85. }
  86. /**
  87. * Builds the definition for a single sidebar and returns the ID.
  88. *
  89. * The $args parameter takes either a string or an array with 'name' and 'id'
  90. * contained in either usage. It will be noted that the values will be applied
  91. * to all sidebars, so if creating more than one, it will be advised to allow
  92. * for WordPress to create the defaults for you.
  93. *
  94. * Example for string would be <code>'name=whatever;id=whatever1'</code> and for
  95. * the array it would be <code>array(
  96. * 'name' => 'whatever',
  97. * 'id' => 'whatever1')</code>.
  98. *
  99. * name - The name of the sidebar, which presumably the title which will be
  100. * displayed.
  101. * id - The unique identifier by which the sidebar will be called by.
  102. * before_widget - The content that will prepended to the widgets when they are
  103. * displayed.
  104. * after_widget - The content that will be appended to the widgets when they are
  105. * displayed.
  106. * before_title - The content that will be prepended to the title when displayed.
  107. * after_title - the content that will be appended to the title when displayed.
  108. *
  109. * <em>Content</em> is assumed to be HTML and should be formatted as such, but
  110. * doesn't have to be.
  111. *
  112. * @since 2.2.0
  113. * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
  114. * @uses parse_str() Converts a string to an array to be used in the rest of the function.
  115. * @usedby register_sidebars()
  116. *
  117. * @param string|array $args Builds Sidebar based off of 'name' and 'id' values
  118. * @return string The sidebar id that was added.
  119. */
  120. function register_sidebar($args = array()) {
  121. global $wp_registered_sidebars;
  122. if ( is_string($args) )
  123. parse_str($args, $args);
  124. $i = count($wp_registered_sidebars) + 1;
  125. $defaults = array(
  126. 'name' => sprintf(__('Sidebar %d'), $i ),
  127. 'id' => "sidebar-$i",
  128. 'before_widget' => '<li id="%1$s" class="widget %2$s">',
  129. 'after_widget' => "</li>\n",
  130. 'before_title' => '<h2 class="widgettitle">',
  131. 'after_title' => "</h2>\n",
  132. );
  133. $sidebar = array_merge($defaults, (array) $args);
  134. $wp_registered_sidebars[$sidebar['id']] = $sidebar;
  135. return $sidebar['id'];
  136. }
  137. /**
  138. * Removes a sidebar from the list.
  139. *
  140. * @since 2.2.0
  141. *
  142. * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
  143. *
  144. * @param string $name The ID of the sidebar when it was added.
  145. */
  146. function unregister_sidebar( $name ) {
  147. global $wp_registered_sidebars;
  148. if ( isset( $wp_registered_sidebars[$name] ) )
  149. unset( $wp_registered_sidebars[$name] );
  150. }
  151. /**
  152. * Register widget for sidebar with backwards compatibility.
  153. *
  154. * Allows $name to be an array that accepts either three elements to grab the
  155. * first element and the third for the name or just uses the first element of
  156. * the array for the name.
  157. *
  158. * Passes to {@link wp_register_sidebar_widget()} after argument list and
  159. * backwards compatibility is complete.
  160. *
  161. * @since 2.2.0
  162. * @uses wp_register_sidebar_widget() Passes the compiled arguments.
  163. *
  164. * @param string|int $name Widget ID.
  165. * @param callback $output_callback Run when widget is called.
  166. * @param string $classname Classname widget option.
  167. * @param mixed $params,... Widget parameters.
  168. */
  169. function register_sidebar_widget($name, $output_callback, $classname = '') {
  170. // Compat
  171. if ( is_array($name) ) {
  172. if ( count($name) == 3 )
  173. $name = sprintf($name[0], $name[2]);
  174. else
  175. $name = $name[0];
  176. }
  177. $id = sanitize_title($name);
  178. $options = array();
  179. if ( !empty($classname) && is_string($classname) )
  180. $options['classname'] = $classname;
  181. $params = array_slice(func_get_args(), 2);
  182. $args = array($id, $name, $output_callback, $options);
  183. if ( !empty($params) )
  184. $args = array_merge($args, $params);
  185. call_user_func_array('wp_register_sidebar_widget', $args);
  186. }
  187. /**
  188. * Register widget for use in sidebars.
  189. *
  190. * The default widget option is 'classname' that can be override.
  191. *
  192. * The function can also be used to unregister widgets when $output_callback
  193. * parameter is an empty string.
  194. *
  195. * @since 2.2.0
  196. *
  197. * @uses $wp_registered_widgets Uses stored registered widgets.
  198. * @uses $wp_register_widget_defaults Retrieves widget defaults.
  199. *
  200. * @param int|string $id Widget ID.
  201. * @param string $name Widget display title.
  202. * @param callback $output_callback Run when widget is called.
  203. * @param array|string Optional. $options Widget Options.
  204. * @param mixed $params,... Widget parameters to add to widget.
  205. * @return null Will return if $output_callback is empty after removing widget.
  206. */
  207. function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
  208. global $wp_registered_widgets;
  209. $id = strtolower($id);
  210. if ( empty($output_callback) ) {
  211. unset($wp_registered_widgets[$id]);
  212. return;
  213. }
  214. $defaults = array('classname' => $output_callback);
  215. $options = wp_parse_args($options, $defaults);
  216. $widget = array(
  217. 'name' => $name,
  218. 'id' => $id,
  219. 'callback' => $output_callback,
  220. 'params' => array_slice(func_get_args(), 4)
  221. );
  222. $widget = array_merge($widget, $options);
  223. if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) )
  224. $wp_registered_widgets[$id] = $widget;
  225. }
  226. /**
  227. * Retrieve description for widget.
  228. *
  229. * When registering widgets, the options can also include 'description' that
  230. * describes the widget for display on the widget administration panel or
  231. * in the theme.
  232. *
  233. * @since 2.5.0
  234. *
  235. * @param int|string $id Widget ID.
  236. * @return string Widget description, if available. Null on failure to retrieve description.
  237. */
  238. function wp_widget_description( $id ) {
  239. if ( !is_scalar($id) )
  240. return;
  241. global $wp_registered_widgets;
  242. if ( isset($wp_registered_widgets[$id]['description']) )
  243. return wp_specialchars( $wp_registered_widgets[$id]['description'] );
  244. }
  245. /**
  246. * Alias of {@link wp_unregister_sidebar_widget()}.
  247. *
  248. * @see wp_unregister_sidebar_widget()
  249. *
  250. * @since 2.2.0
  251. *
  252. * @param int|string $id Widget ID.
  253. */
  254. function unregister_sidebar_widget($id) {
  255. return wp_unregister_sidebar_widget($id);
  256. }
  257. /**
  258. * Remove widget from sidebar.
  259. *
  260. * @since 2.2.0
  261. *
  262. * @param int|string $id Widget ID.
  263. */
  264. function wp_unregister_sidebar_widget($id) {
  265. wp_register_sidebar_widget($id, '', '');
  266. wp_unregister_widget_control($id);
  267. }
  268. /**
  269. * Registers widget control callback for customizing options.
  270. *
  271. * Allows $name to be an array that accepts either three elements to grab the
  272. * first element and the third for the name or just uses the first element of
  273. * the array for the name.
  274. *
  275. * Passes to {@link wp_register_widget_control()} after the argument list has
  276. * been compiled.
  277. *
  278. * @since 2.2.0
  279. *
  280. * @param int|string $name Sidebar ID.
  281. * @param callback $control_callback Widget control callback to display and process form.
  282. * @param int $width Widget width.
  283. * @param int $height Widget height.
  284. */
  285. function register_widget_control($name, $control_callback, $width = '', $height = '') {
  286. // Compat
  287. if ( is_array($name) ) {
  288. if ( count($name) == 3 )
  289. $name = sprintf($name[0], $name[2]);
  290. else
  291. $name = $name[0];
  292. }
  293. $id = sanitize_title($name);
  294. $options = array();
  295. if ( !empty($width) )
  296. $options['width'] = $width;
  297. if ( !empty($height) )
  298. $options['height'] = $height;
  299. $params = array_slice(func_get_args(), 4);
  300. $args = array($id, $name, $control_callback, $options);
  301. if ( !empty($params) )
  302. $args = array_merge($args, $params);
  303. call_user_func_array('wp_register_widget_control', $args);
  304. }
  305. /**
  306. * Registers widget control callback for customizing options.
  307. *
  308. * The options contains the 'height', 'width', and 'id_base' keys. The 'height'
  309. * option is never used. The 'width' option is the width of the fully expanded
  310. * control form, but try hard to use the default width. The 'id_base' is for
  311. * multi-widgets (widgets which allow multiple instances such as the text
  312. * widget), an id_base must be provided. The widget id will end up looking like
  313. * {$id_base}-{$unique_number}.
  314. *
  315. * @since 2.2.0
  316. *
  317. * @param int|string $id Sidebar ID.
  318. * @param string $name Sidebar display name.
  319. * @param callback $control_callback Run when sidebar is displayed.
  320. * @param array|string $options Optional. Widget options. See above long description.
  321. * @param mixed $params,... Optional. Additional parameters to add to widget.
  322. */
  323. function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
  324. global $wp_registered_widget_controls;
  325. $id = strtolower($id);
  326. if ( empty($control_callback) ) {
  327. unset($wp_registered_widget_controls[$id]);
  328. return;
  329. }
  330. if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
  331. return;
  332. $defaults = array('width' => 250, 'height' => 200 ); // height is never used
  333. $options = wp_parse_args($options, $defaults);
  334. $options['width'] = (int) $options['width'];
  335. $options['height'] = (int) $options['height'];
  336. $widget = array(
  337. 'name' => $name,
  338. 'id' => $id,
  339. 'callback' => $control_callback,
  340. 'params' => array_slice(func_get_args(), 4)
  341. );
  342. $widget = array_merge($widget, $options);
  343. $wp_registered_widget_controls[$id] = $widget;
  344. }
  345. /**
  346. * Alias of {@link wp_unregister_widget_control()}.
  347. *
  348. * @since 2.2.0
  349. * @see wp_unregister_widget_control()
  350. *
  351. * @param int|string $id Widget ID.
  352. */
  353. function unregister_widget_control($id) {
  354. return wp_unregister_widget_control($id);
  355. }
  356. /**
  357. * Remove control callback for widget.
  358. *
  359. * @since 2.2.0
  360. * @uses wp_register_widget_control() Unregisters by using empty callback.
  361. *
  362. * @param int|string $id Widget ID.
  363. */
  364. function wp_unregister_widget_control($id) {
  365. return wp_register_widget_control($id, '', '');
  366. }
  367. /**
  368. * Display dynamic sidebar.
  369. *
  370. * By default it displays the default sidebar or 'sidebar-1'. The 'sidebar-1' is
  371. * not named by the theme, the actual name is '1', but 'sidebar-' is added to
  372. * the registered sidebars for the name. If you named your sidebar 'after-post',
  373. * then the parameter $index will still be 'after-post', but the lookup will be
  374. * for 'sidebar-after-post'.
  375. *
  376. * It is confusing for the $index parameter, but just know that it should just
  377. * work. When you register the sidebar in the theme, you will use the same name
  378. * for this function or "Pay no heed to the man behind the curtain." Just accept
  379. * it as an oddity of WordPress sidebar register and display.
  380. *
  381. * @since 2.2.0
  382. *
  383. * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
  384. * @return bool True, if widget sidebar was found and called. False if not found or not called.
  385. */
  386. function dynamic_sidebar($index = 1) {
  387. global $wp_registered_sidebars, $wp_registered_widgets;
  388. if ( is_int($index) ) {
  389. $index = "sidebar-$index";
  390. } else {
  391. $index = sanitize_title($index);
  392. foreach ( (array) $wp_registered_sidebars as $key => $value ) {
  393. if ( sanitize_title($value['name']) == $index ) {
  394. $index = $key;
  395. break;
  396. }
  397. }
  398. }
  399. $sidebars_widgets = wp_get_sidebars_widgets();
  400. if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
  401. return false;
  402. $sidebar = $wp_registered_sidebars[$index];
  403. $did_one = false;
  404. foreach ( (array) $sidebars_widgets[$index] as $id ) {
  405. $params = array_merge(
  406. array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
  407. (array) $wp_registered_widgets[$id]['params']
  408. );
  409. // Substitute HTML id and class attributes into before_widget
  410. $classname_ = '';
  411. foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
  412. if ( is_string($cn) )
  413. $classname_ .= '_' . $cn;
  414. elseif ( is_object($cn) )
  415. $classname_ .= '_' . get_class($cn);
  416. }
  417. $classname_ = ltrim($classname_, '_');
  418. $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
  419. $params = apply_filters( 'dynamic_sidebar_params', $params );
  420. $callback = $wp_registered_widgets[$id]['callback'];
  421. if ( is_callable($callback) ) {
  422. call_user_func_array($callback, $params);
  423. $did_one = true;
  424. }
  425. }
  426. return $did_one;
  427. }
  428. /**
  429. * Whether widget is registered using callback with widget ID.
  430. *
  431. * Without the optional $widget_id parameter, returns the ID of the first sidebar in which the first instance of the widget with the given callback is found.
  432. * With the $widget_id parameter, returns the ID of the sidebar in which the widget with that callback AND that ID is found.
  433. *
  434. * @since 2.2.0
  435. *
  436. * @param callback $callback Widget callback to check.
  437. * @param int $widget_id Optional, but needed for checking. Widget ID.
  438. /* @return mixed false if widget is not active or id of sidebar in which the widget is active.
  439. */
  440. function is_active_widget($callback, $widget_id = false) {
  441. global $wp_registered_widgets;
  442. $sidebars_widgets = wp_get_sidebars_widgets(false);
  443. if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
  444. if ( is_array($widgets) ) foreach ( $widgets as $widget )
  445. if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
  446. if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
  447. return $sidebar;
  448. return false;
  449. }
  450. /**
  451. * Whether the dynamic sidebar is enabled and used by theme.
  452. *
  453. * @since 2.2.0
  454. *
  455. * @return bool True, if using widgets. False, if not using widgets.
  456. */
  457. function is_dynamic_sidebar() {
  458. global $wp_registered_widgets, $wp_registered_sidebars;
  459. $sidebars_widgets = get_option('sidebars_widgets');
  460. foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
  461. if ( count($sidebars_widgets[$index]) ) {
  462. foreach ( (array) $sidebars_widgets[$index] as $widget )
  463. if ( array_key_exists($widget, $wp_registered_widgets) )
  464. return true;
  465. }
  466. }
  467. return false;
  468. }
  469. /* Internal Functions */
  470. /**
  471. * Retrieve full list of sidebars and their widgets.
  472. *
  473. * Will upgrade sidebar widget list, if needed. Will also save updated list, if
  474. * needed.
  475. *
  476. * @since 2.2.0
  477. * @access private
  478. *
  479. * @param bool $update Optional, default is true. Whether to save upgrade of widget array list.
  480. * @return array Upgraded list of widgets to version 2 array format.
  481. */
  482. function wp_get_sidebars_widgets($update = true) {
  483. global $wp_registered_widgets, $wp_registered_sidebars;
  484. $sidebars_widgets = get_option('sidebars_widgets', array());
  485. $_sidebars_widgets = array();
  486. if ( !isset($sidebars_widgets['array_version']) )
  487. $sidebars_widgets['array_version'] = 1;
  488. switch ( $sidebars_widgets['array_version'] ) {
  489. case 1 :
  490. foreach ( (array) $sidebars_widgets as $index => $sidebar )
  491. if ( is_array($sidebar) )
  492. foreach ( (array) $sidebar as $i => $name ) {
  493. $id = strtolower($name);
  494. if ( isset($wp_registered_widgets[$id]) ) {
  495. $_sidebars_widgets[$index][$i] = $id;
  496. continue;
  497. }
  498. $id = sanitize_title($name);
  499. if ( isset($wp_registered_widgets[$id]) ) {
  500. $_sidebars_widgets[$index][$i] = $id;
  501. continue;
  502. }
  503. $found = false;
  504. foreach ( $wp_registered_widgets as $widget_id => $widget ) {
  505. if ( strtolower($widget['name']) == strtolower($name) ) {
  506. $_sidebars_widgets[$index][$i] = $widget['id'];
  507. $found = true;
  508. break;
  509. } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
  510. $_sidebars_widgets[$index][$i] = $widget['id'];
  511. $found = true;
  512. break;
  513. }
  514. }
  515. if ( $found )
  516. continue;
  517. unset($_sidebars_widgets[$index][$i]);
  518. }
  519. $_sidebars_widgets['array_version'] = 2;
  520. $sidebars_widgets = $_sidebars_widgets;
  521. unset($_sidebars_widgets);
  522. case 2 :
  523. $sidebars = array_keys( $wp_registered_sidebars );
  524. if ( !empty( $sidebars ) ) {
  525. // Move the known-good ones first
  526. foreach ( (array) $sidebars as $id ) {
  527. if ( array_key_exists( $id, $sidebars_widgets ) ) {
  528. $_sidebars_widgets[$id] = $sidebars_widgets[$id];
  529. unset($sidebars_widgets[$id], $sidebars[$id]);
  530. }
  531. }
  532. // Assign to each unmatched registered sidebar the first available orphan
  533. unset( $sidebars_widgets[ 'array_version' ] );
  534. while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
  535. $_sidebars_widgets[ $sidebar ] = $widgets;
  536. $_sidebars_widgets['array_version'] = 3;
  537. $sidebars_widgets = $_sidebars_widgets;
  538. unset($_sidebars_widgets);
  539. }
  540. if ( $update )
  541. update_option('sidebars_widgets', $sidebars_widgets);
  542. }
  543. if ( isset($sidebars_widgets['array_version']) )
  544. unset($sidebars_widgets['array_version']);
  545. $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets);
  546. return $sidebars_widgets;
  547. }
  548. /**
  549. * Set the sidebar widget option to update sidebars.
  550. *
  551. * @since 2.2.0
  552. * @access private
  553. *
  554. * @param array $sidebars_widgets Sidebar widgets and their settings.
  555. */
  556. function wp_set_sidebars_widgets( $sidebars_widgets ) {
  557. if ( !isset( $sidebars_widgets['array_version'] ) )
  558. $sidebars_widgets['array_version'] = 3;
  559. update_option( 'sidebars_widgets', $sidebars_widgets );
  560. }
  561. /**
  562. * Retrieve default registered sidebars list.
  563. *
  564. * @since 2.2.0
  565. * @access private
  566. *
  567. * @return array
  568. */
  569. function wp_get_widget_defaults() {
  570. global $wp_registered_sidebars;
  571. $defaults = array();
  572. foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
  573. $defaults[$index] = array();
  574. return $defaults;
  575. }
  576. /* Default Widgets */
  577. /**
  578. * Display pages widget.
  579. *
  580. * @since 2.2.0
  581. *
  582. * @param array $args Widget arguments.
  583. */
  584. function wp_widget_pages( $args ) {
  585. extract( $args );
  586. $options = get_option( 'widget_pages' );
  587. $title = empty( $options['title'] ) ? __( 'Pages' ) : apply_filters('widget_title', $options['title']);
  588. $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
  589. $exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
  590. if ( $sortby == 'menu_order' ) {
  591. $sortby = 'menu_order, post_title';
  592. }
  593. $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
  594. if ( !empty( $out ) ) {
  595. ?>
  596. <?php echo $before_widget; ?>
  597. <?php echo $before_title . $title . $after_title; ?>
  598. <ul>
  599. <?php echo $out; ?>
  600. </ul>
  601. <?php echo $after_widget; ?>
  602. <?php
  603. }
  604. }
  605. /**
  606. * Display and process pages widget options form.
  607. *
  608. * @since 2.2.0
  609. */
  610. function wp_widget_pages_control() {
  611. $options = $newoptions = get_option('widget_pages');
  612. if ( isset($_POST['pages-submit']) ) {
  613. $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));
  614. $sortby = stripslashes( $_POST['pages-sortby'] );
  615. if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
  616. $newoptions['sortby'] = $sortby;
  617. } else {
  618. $newoptions['sortby'] = 'menu_order';
  619. }
  620. $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
  621. }
  622. if ( $options != $newoptions ) {
  623. $options = $newoptions;
  624. update_option('widget_pages', $options);
  625. }
  626. $title = attribute_escape($options['title']);
  627. $exclude = attribute_escape( $options['exclude'] );
  628. ?>
  629. <p><label for="pages-title"><?php _e('Title:'); ?> <input class="widefat" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label></p>
  630. <p>
  631. <label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
  632. <select name="pages-sortby" id="pages-sortby" class="widefat">
  633. <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
  634. <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
  635. <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
  636. </select>
  637. </label>
  638. </p>
  639. <p>
  640. <label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
  641. <br />
  642. <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
  643. </p>
  644. <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
  645. <?php
  646. }
  647. /**
  648. * Links widget class
  649. *
  650. * @since 2.8.0
  651. */
  652. class WP_Widget_Links extends WP_Widgets {
  653. function widget( $args, $instance ) {
  654. extract($args, EXTR_SKIP);
  655. $show_description = isset($instance['description']) ? $instance['description'] : false;
  656. $show_name = isset($instance['name']) ? $instance['name'] : false;
  657. $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
  658. $show_images = isset($instance['images']) ? $instance['images'] : true;
  659. $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
  660. wp_list_bookmarks(apply_filters('widget_links_args', array(
  661. 'title_before' => $before_title, 'title_after' => $after_title,
  662. 'category_before' => $before_widget, 'category_after' => $after_widget,
  663. 'show_images' => $show_images, 'show_description' => $show_description,
  664. 'show_name' => $show_name, 'show_rating' => $show_rating,
  665. 'class' => 'linkcat widget'
  666. )));
  667. }
  668. function update( $new_instance, $old_instance ) {
  669. if( !isset($new_instance['submit']) ) // user clicked cancel?
  670. return false;
  671. return $new_instance;
  672. }
  673. function form( $instance ) {
  674. //Defaults
  675. $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false) );
  676. /*
  677. if ( isset($_POST['links-submit']) ) {
  678. $newoptions = array();
  679. $newoptions['description'] = isset($_POST['links-description']);
  680. $newoptions['name'] = isset($_POST['links-name']);
  681. $newoptions['rating'] = isset($_POST['links-rating']);
  682. $newoptions['images'] = isset($_POST['links-images']);
  683. if ( $instance != $newoptions ) {
  684. $instance = $newoptions;
  685. update_option('widget_links', $instance);
  686. }
  687. }
  688. */
  689. ?>
  690. <p>
  691. <label for="<?php echo $this->get_field_id('images'); ?>">
  692. <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" /> <?php _e('Show Link Image'); ?></label><br />
  693. <label for="<?php echo $this->get_field_id('name'); ?>">
  694. <input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" /> <?php _e('Show Link Name'); ?></label><br />
  695. <label for="<?php echo $this->get_field_id('description'); ?>">
  696. <input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" /> <?php _e('Show Link Description'); ?></label><br />
  697. <label for="<?php echo $this->get_field_id('rating'); ?>">
  698. <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" /> <?php _e('Show Link Rating'); ?></label>
  699. <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
  700. </p>
  701. <?php
  702. }
  703. }
  704. /**
  705. * Display search widget.
  706. *
  707. * @since 2.2.0
  708. *
  709. * @param array $args Widget arguments.
  710. */
  711. function wp_widget_search($args) {
  712. extract($args);
  713. echo $before_widget;
  714. // Use current theme search form if it exists
  715. get_search_form();
  716. echo $after_widget;
  717. }
  718. /**
  719. * Display archives widget.
  720. *
  721. * @since 2.2.0
  722. *
  723. * @param array $args Widget arguments.
  724. */
  725. function wp_widget_archives($args) {
  726. extract($args);
  727. $options = get_option('widget_archives');
  728. $c = $options['count'] ? '1' : '0';
  729. $d = $options['dropdown'] ? '1' : '0';
  730. $title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);
  731. echo $before_widget;
  732. echo $before_title . $title . $after_title;
  733. if($d) {
  734. ?>
  735. <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
  736. <?php
  737. } else {
  738. ?>
  739. <ul>
  740. <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
  741. </ul>
  742. <?php
  743. }
  744. echo $after_widget;
  745. }
  746. /**
  747. * Display and process archives widget options form.
  748. *
  749. * @since 2.2.0
  750. */
  751. function wp_widget_archives_control() {
  752. $options = $newoptions = get_option('widget_archives');
  753. if ( isset($_POST["archives-submit"]) ) {
  754. $newoptions['count'] = isset($_POST['archives-count']);
  755. $newoptions['dropdown'] = isset($_POST['archives-dropdown']);
  756. $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
  757. }
  758. if ( $options != $newoptions ) {
  759. $options = $newoptions;
  760. update_option('widget_archives', $options);
  761. }
  762. $count = $options['count'] ? 'checked="checked"' : '';
  763. $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
  764. $title = attribute_escape($options['title']);
  765. ?>
  766. <p><label for="archives-title"><?php _e('Title:'); ?> <input class="widefat" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
  767. <p>
  768. <label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
  769. <br />
  770. <label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
  771. </p>
  772. <input type="hidden" id="archives-submit" name="archives-submit" value="1" />
  773. <?php
  774. }
  775. /**
  776. * Display meta widget.
  777. *
  778. * Displays log in/out, RSS feed links, etc.
  779. *
  780. * @since 2.2.0
  781. *
  782. * @param array $args Widget arguments.
  783. */
  784. function wp_widget_meta($args) {
  785. extract($args);
  786. $options = get_option('widget_meta');
  787. $title = empty($options['title']) ? __('Meta') : apply_filters('widget_title', $options['title']);
  788. ?>
  789. <?php echo $before_widget; ?>
  790. <?php echo $before_title . $title . $after_title; ?>
  791. <ul>
  792. <?php wp_register(); ?>
  793. <li><?php wp_loginout(); ?></li>
  794. <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo attribute_escape(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
  795. <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo attribute_escape(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
  796. <li><a href="http://wordpress.org/" title="<?php echo attribute_escape(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>">WordPress.org</a></li>
  797. <?php wp_meta(); ?>
  798. </ul>
  799. <?php echo $after_widget; ?>
  800. <?php
  801. }
  802. /**
  803. * Display and process meta widget options form.
  804. *
  805. * @since 2.2.0
  806. */
  807. function wp_widget_meta_control() {
  808. $options = $newoptions = get_option('widget_meta');
  809. if ( isset($_POST["meta-submit"]) ) {
  810. $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"]));
  811. }
  812. if ( $options != $newoptions ) {
  813. $options = $newoptions;
  814. update_option('widget_meta', $options);
  815. }
  816. $title = attribute_escape($options['title']);
  817. ?>
  818. <p><label for="meta-title"><?php _e('Title:'); ?> <input class="widefat" id="meta-title" name="meta-title" type="text" value="<?php echo $title; ?>" /></label></p>
  819. <input type="hidden" id="meta-submit" name="meta-submit" value="1" />
  820. <?php
  821. }
  822. /**
  823. * Display calendar widget.
  824. *
  825. * @since 2.2.0
  826. *
  827. * @param array $args Widget arguments.
  828. */
  829. function wp_widget_calendar($args) {
  830. extract($args);
  831. $options = get_option('widget_calendar');
  832. $title = apply_filters('widget_title', $options['title']);
  833. if ( empty($title) )
  834. $title = '&nbsp;';
  835. echo $before_widget . $before_title . $title . $after_title;
  836. echo '<div id="calendar_wrap">';
  837. get_calendar();
  838. echo '</div>';
  839. echo $after_widget;
  840. }
  841. /**
  842. * Display and process calendar widget options form.
  843. *
  844. * @since 2.2.0
  845. */
  846. function wp_widget_calendar_control() {
  847. $options = $newoptions = get_option('widget_calendar');
  848. if ( isset($_POST["calendar-submit"]) ) {
  849. $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
  850. }
  851. if ( $options != $newoptions ) {
  852. $options = $newoptions;
  853. update_option('widget_calendar', $options);
  854. }
  855. $title = attribute_escape($options['title']);
  856. ?>
  857. <p><label for="calendar-title"><?php _e('Title:'); ?> <input class="widefat" id="calendar-title" name="calendar-title" type="text" value="<?php echo $title; ?>" /></label></p>
  858. <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" />
  859. <?php
  860. }
  861. /**
  862. * Display the Text widget, depending on the widget number.
  863. *
  864. * Supports multiple text widgets and keeps track of the widget number by using
  865. * the $widget_args parameter. The option 'widget_text' is used to store the
  866. * content for the widgets. The content and title are passed through the
  867. * 'widget_text' and 'widget_title' filters respectively.
  868. *
  869. * @since 2.2.0
  870. *
  871. * @param array $args Widget arguments.
  872. * @param int $number Widget number.
  873. */
  874. function wp_widget_text($args, $widget_args = 1) {
  875. extract( $args, EXTR_SKIP );
  876. if ( is_numeric($widget_args) )
  877. $widget_args = array( 'number' => $widget_args );
  878. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  879. extract( $widget_args, EXTR_SKIP );
  880. $options = get_option('widget_text');
  881. if ( !isset($options[$number]) )
  882. return;
  883. $title = apply_filters('widget_title', $options[$number]['title']);
  884. $text = apply_filters( 'widget_text', $options[$number]['text'] );
  885. ?>
  886. <?php echo $before_widget; ?>
  887. <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
  888. <div class="textwidget"><?php echo $text; ?></div>
  889. <?php echo $after_widget; ?>
  890. <?php
  891. }
  892. /**
  893. * Display and process text widget options form.
  894. *
  895. * @since 2.2.0
  896. *
  897. * @param int $widget_args Widget number.
  898. */
  899. function wp_widget_text_control($widget_args) {
  900. global $wp_registered_widgets;
  901. static $updated = false;
  902. if ( is_numeric($widget_args) )
  903. $widget_args = array( 'number' => $widget_args );
  904. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  905. extract( $widget_args, EXTR_SKIP );
  906. $options = get_option('widget_text');
  907. if ( !is_array($options) )
  908. $options = array();
  909. if ( !$updated && !empty($_POST['sidebar']) ) {
  910. $sidebar = (string) $_POST['sidebar'];
  911. $sidebars_widgets = wp_get_sidebars_widgets();
  912. if ( isset($sidebars_widgets[$sidebar]) )
  913. $this_sidebar =& $sidebars_widgets[$sidebar];
  914. else
  915. $this_sidebar = array();
  916. foreach ( (array) $this_sidebar as $_widget_id ) {
  917. if ( 'wp_widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
  918. $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
  919. if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
  920. unset($options[$widget_number]);
  921. }
  922. }
  923. foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
  924. if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
  925. continue;
  926. $title = strip_tags(stripslashes($widget_text['title']));
  927. if ( current_user_can('unfiltered_html') )
  928. $text = stripslashes( $widget_text['text'] );
  929. else
  930. $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
  931. $options[$widget_number] = compact( 'title', 'text' );
  932. }
  933. update_option('widget_text', $options);
  934. $updated = true;
  935. }
  936. if ( -1 == $number ) {
  937. $title = '';
  938. $text = '';
  939. $number = '%i%';
  940. } else {
  941. $title = attribute_escape($options[$number]['title']);
  942. $text = format_to_edit($options[$number]['text']);
  943. }
  944. ?>
  945. <p>
  946. <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
  947. <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
  948. <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
  949. </p>
  950. <?php
  951. }
  952. /**
  953. * Register text widget on startup.
  954. *
  955. * @since 2.2.0
  956. */
  957. function wp_widget_text_register() {
  958. if ( !$options = get_option('widget_text') )
  959. $options = array();
  960. $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
  961. $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
  962. $name = __('Text');
  963. $id = false;
  964. foreach ( (array) array_keys($options) as $o ) {
  965. // Old widgets can have null values for some reason
  966. if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
  967. continue;
  968. $id = "text-$o"; // Never never never translate an id
  969. wp_register_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
  970. wp_register_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
  971. }
  972. // If there are none, we register the widget's existance with a generic template
  973. if ( !$id ) {
  974. wp_register_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
  975. wp_register_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
  976. }
  977. }
  978. /**
  979. * Display categories widget.
  980. *
  981. * Allows multiple category widgets.
  982. *
  983. * @since 2.2.0
  984. *
  985. * @param array $args Widget arguments.
  986. * @param int $number Widget number.
  987. */
  988. function wp_widget_categories($args, $widget_args = 1) {
  989. extract($args, EXTR_SKIP);
  990. if ( is_numeric($widget_args) )
  991. $widget_args = array( 'number' => $widget_args );
  992. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  993. extract($widget_args, EXTR_SKIP);
  994. $options = get_option('widget_categories');
  995. if ( !isset($options[$number]) )
  996. return;
  997. $c = $options[$number]['count'] ? '1' : '0';
  998. $h = $options[$number]['hierarchical'] ? '1' : '0';
  999. $d = $options[$number]['dropdown'] ? '1' : '0';
  1000. $title = empty($options[$number]['title']) ? __('Categories') : apply_filters('widget_title', $options[$number]['title']);
  1001. echo $before_widget;
  1002. echo $before_title . $title . $after_title;
  1003. $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
  1004. if ( $d ) {
  1005. $cat_args['show_option_none'] = __('Select Category');
  1006. wp_dropdown_categories($cat_args);
  1007. ?>
  1008. <script type='text/javascript'>
  1009. /* <![CDATA[ */
  1010. var dropdown = document.getElementById("cat");
  1011. function onCatChange() {
  1012. if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
  1013. location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
  1014. }
  1015. }
  1016. dropdown.onchange = onCatChange;
  1017. /* ]]> */
  1018. </script>
  1019. <?php
  1020. } else {
  1021. ?>
  1022. <ul>
  1023. <?php
  1024. $cat_args['title_li'] = '';
  1025. wp_list_categories($cat_args);
  1026. ?>
  1027. </ul>
  1028. <?php
  1029. }
  1030. echo $after_widget;
  1031. }
  1032. /**
  1033. * Display and process categories widget options form.
  1034. *
  1035. * @since 2.2.0
  1036. *
  1037. * @param int $widget_args Widget number.
  1038. */
  1039. function wp_widget_categories_control( $widget_args ) {
  1040. global $wp_registered_widgets;
  1041. static $updated = false;
  1042. if ( is_numeric($widget_args) )
  1043. $widget_args = array( 'number' => $widget_args );
  1044. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  1045. extract($widget_args, EXTR_SKIP);
  1046. $options = get_option('widget_categories');
  1047. if ( !is_array( $options ) )
  1048. $options = array();
  1049. if ( !$updated && !empty($_POST['sidebar']) ) {
  1050. $sidebar = (string) $_POST['sidebar'];
  1051. $sidebars_widgets = wp_get_sidebars_widgets();
  1052. if ( isset($sidebars_widgets[$sidebar]) )
  1053. $this_sidebar =& $sidebars_widgets[$sidebar];
  1054. else
  1055. $this_sidebar = array();
  1056. foreach ( (array) $this_sidebar as $_widget_id ) {
  1057. if ( 'wp_widget_categories' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
  1058. $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
  1059. if ( !in_array( "categories-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
  1060. unset($options[$widget_number]);
  1061. }
  1062. }
  1063. foreach ( (array) $_POST['widget-categories'] as $widget_number => $widget_cat ) {
  1064. if ( !isset($widget_cat['title']) && isset($options[$widget_number]) ) // user clicked cancel
  1065. continue;
  1066. $title = trim(strip_tags(stripslashes($widget_cat['title'])));
  1067. $count = isset($widget_cat['count']);
  1068. $hierarchical = isset($widget_cat['hierarchical']);
  1069. $dropdown = isset($widget_cat['dropdown']);
  1070. $options[$widget_number] = compact( 'title', 'count', 'hierarchical', 'dropdown' );
  1071. }
  1072. update_option('widget_categories', $options);
  1073. $updated = true;
  1074. }
  1075. if ( -1 == $number ) {
  1076. $title = '';
  1077. $count = false;
  1078. $hierarchical = false;
  1079. $dropdown = false;
  1080. $number = '%i%';
  1081. } else {
  1082. $title = attribute_escape( $options[$number]['title'] );
  1083. $count = (bool) $options[$number]['count'];
  1084. $hierarchical = (bool) $options[$number]['hierarchical'];
  1085. $dropdown = (bool) $options[$number]['dropdown'];
  1086. }
  1087. ?>
  1088. <p>
  1089. <label for="categories-title-<?php echo $number; ?>">
  1090. <?php _e( 'Title:' ); ?>
  1091. <input class="widefat" id="categories-title-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
  1092. </label>
  1093. </p>
  1094. <p>
  1095. <label for="categories-dropdown-<?php echo $number; ?>">
  1096. <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][dropdown]"<?php checked( $dropdown ); ?> />
  1097. <?php _e( 'Show as dropdown' ); ?>
  1098. </label>
  1099. <br />
  1100. <label for="categories-count-<?php echo $number; ?>">
  1101. <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][count]"<?php checked( $count ); ?> />
  1102. <?php _e( 'Show post counts' ); ?>
  1103. </label>
  1104. <br />
  1105. <label for="categories-hierarchical-<?php echo $number; ?>">
  1106. <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][hierarchical]"<?php checked( $hierarchical ); ?> />
  1107. <?php _e( 'Show hierarchy' ); ?>
  1108. </label>
  1109. </p>
  1110. <input type="hidden" name="widget-categories[<?php echo $number; ?>][submit]" value="1" />
  1111. <?php
  1112. }
  1113. /**
  1114. * Register categories widget on startup.
  1115. *
  1116. * @since 2.3.0
  1117. */
  1118. function wp_widget_categories_register() {
  1119. if ( !$options = get_option( 'widget_categories' ) )
  1120. $options = array();
  1121. if ( isset($options['title']) )
  1122. $options = wp_widget_categories_upgrade();
  1123. $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
  1124. $name = __( 'Categories' );
  1125. $id = false;
  1126. foreach ( (array) array_keys($options) as $o ) {
  1127. // Old widgets can have null values for some reason
  1128. if ( !isset($options[$o]['title']) )
  1129. continue;
  1130. $id = "categories-$o";
  1131. wp_register_sidebar_widget( $id, $name, 'wp_widget_categories', $widget_ops, array( 'number' => $o ) );
  1132. wp_register_widget_control( $id, $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => $o ) );
  1133. }
  1134. // If there are none, we register the widget's existance with a generic template
  1135. if ( !$id ) {
  1136. wp_register_sidebar_widget( 'categories-1', $name, 'wp_widget_categories', $widget_ops, array( 'number' => -1 ) );
  1137. wp_register_widget_control( 'categories-1', $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => -1 ) );
  1138. }
  1139. }
  1140. /**
  1141. * Upgrade previous category widget to current version.
  1142. *
  1143. * @since 2.3.0
  1144. *
  1145. * @return array
  1146. */
  1147. function wp_widget_categories_upgrade() {
  1148. $options = get_option( 'widget_categories' );
  1149. if ( !isset( $options['title'] ) )
  1150. return $options;
  1151. $newoptions = array( 1 => $options );
  1152. update_option( 'widget_categories', $newoptions );
  1153. $sidebars_widgets = get_option( 'sidebars_widgets' );
  1154. if ( is_array( $sidebars_widgets ) ) {
  1155. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  1156. if ( is_array( $widgets ) ) {
  1157. foreach ( $widgets as $widget )
  1158. $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
  1159. } else {
  1160. $new_widgets[$sidebar] = $widgets;
  1161. }
  1162. }
  1163. if ( $new_widgets != $sidebars_widgets )
  1164. update_option( 'sidebars_widgets', $new_widgets );
  1165. }
  1166. return $newoptions;
  1167. }
  1168. /**
  1169. * Display recent entries widget.
  1170. *
  1171. * @since 2.2.0
  1172. *
  1173. * @param array $args Widget arguments.
  1174. * @return int Displayed cache.
  1175. */
  1176. function wp_widget_recent_entries($args) {
  1177. if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
  1178. if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
  1179. return print($output);
  1180. ob_start();
  1181. }
  1182. extract($args);
  1183. $options = get_option('widget_recent_entries');
  1184. $title = empty($options['title']) ? __('Recent Posts') : apply_filters('widget_title', $options['title']);
  1185. if ( !$number = (int) $options['number'] )
  1186. $number = 10;
  1187. else if ( $number < 1 )
  1188. $number = 1;
  1189. else if ( $number > 15 )
  1190. $number = 15;
  1191. $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
  1192. if ($r->have_posts()) :
  1193. ?>
  1194. <?php echo $before_widget; ?>
  1195. <?php echo $before_title . $title . $after_title; ?>
  1196. <ul>
  1197. <?php while ($r->have_posts()) : $r->the_post(); ?>
  1198. <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
  1199. <?php endwhile; ?>
  1200. </ul>
  1201. <?php echo $after_widget; ?>
  1202. <?php
  1203. wp_reset_query(); // Restore global post data stomped by the_post().
  1204. endif;
  1205. if ( '%BEG_OF_TITLE%' != $args['before_title'] )
  1206. wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
  1207. }
  1208. /**
  1209. * Remove recent entries widget items cache.
  1210. *
  1211. * @since 2.2.0
  1212. */
  1213. function wp_flush_widget_recent_entries() {
  1214. wp_cache_delete('widget_recent_entries', 'widget');
  1215. }
  1216. add_action('save_post', 'wp_flush_widget_recent_entries');
  1217. add_action('deleted_post', 'wp_flush_widget_recent_entries');
  1218. add_action('switch_theme', 'wp_flush_widget_recent_entries');
  1219. /**
  1220. * Display and process recent entries widget options form.
  1221. *
  1222. * @since 2.2.0
  1223. */
  1224. function wp_widget_recent_entries_control() {
  1225. $options = $newoptions = get_option('widget_recent_entries');
  1226. if ( isset($_POST["recent-entries-submit"]) ) {
  1227. $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
  1228. $newoptions['number'] = (int) $_POST["recent-entries-number"];
  1229. }
  1230. if ( $options != $newoptions ) {
  1231. $options = $newoptions;
  1232. update_option('widget_recent_entries', $options);
  1233. wp_flush_widget_recent_entries();
  1234. }
  1235. $title = attribute_escape($options['title']);
  1236. if ( !$number = (int) $options['number'] )
  1237. $number = 5;
  1238. ?>
  1239. <p><label for="recent-entries-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p>
  1240. <p>
  1241. <label for="recent-entries-number"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-number" name="recent-entries-number" type="text" value="<?php echo $number; ?>" /></label>
  1242. <br />
  1243. <small><?php _e('(at most 15)'); ?></small>
  1244. </p>
  1245. <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
  1246. <?php
  1247. }
  1248. /**
  1249. * Display recent comments widget.
  1250. *
  1251. * @since 2.2.0
  1252. *
  1253. * @param array $args Widget arguments.
  1254. */
  1255. function wp_widget_recent_comments($args) {
  1256. global $wpdb, $comments, $comment;
  1257. extract($args, EXTR_SKIP);
  1258. $options = get_option('widget_recent_comments');
  1259. $title = empty($options['title']) ? __('Recent Comments') : apply_filters('widget_title', $options['title']);
  1260. if ( !$number = (int) $options['number'] )
  1261. $number = 5;
  1262. else if ( $number < 1 )
  1263. $number = 1;
  1264. else if ( $number > 15 )
  1265. $number = 15;
  1266. if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
  1267. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
  1268. wp_cache_add( 'recent_comments', $comments, 'widget' );
  1269. }
  1270. ?>
  1271. <?php echo $before_widget; ?>
  1272. <?php echo $before_title . $title . $after_title; ?>
  1273. <ul id="recentcomments"><?php
  1274. if ( $comments ) : foreach ( (array) $comments as $comment) :
  1275. echo '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="' . clean_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
  1276. endforeach; endif;?></ul>
  1277. <?php echo $after_widget; ?>
  1278. <?php
  1279. }
  1280. /**
  1281. * Remove the cache for recent comments widget.
  1282. *
  1283. * @since 2.2.0
  1284. */
  1285. function wp_delete_recent_comments_cache() {
  1286. wp_cache_delete( 'recent_comments', 'widget' );
  1287. }
  1288. add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
  1289. add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
  1290. /**
  1291. * Display and process recent comments widget options form.
  1292. *
  1293. * @since 2.2.0
  1294. */
  1295. function wp_widget_recent_comments_control() {
  1296. $options = $newoptions = get_option('widget_recent_comments');
  1297. if ( isset($_POST["recent-comments-submit"]) ) {
  1298. $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
  1299. $newoptions['number'] = (int) $_POST["recent-comments-number"];
  1300. }
  1301. if ( $options != $newoptions ) {
  1302. $options = $newoptions;
  1303. update_option('widget_recent_comments', $options);
  1304. wp_delete_recent_comments_cache();
  1305. }
  1306. $title = attribute_escape($options['title']);
  1307. if ( !$number = (int) $options['number'] )
  1308. $number = 5;
  1309. ?>
  1310. <p><label for="recent-comments-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
  1311. <p>
  1312. <label for="recent-comments-number"><?php _e('Number of comments to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label>
  1313. <br />
  1314. <small><?php _e('(at most 15)'); ?></small>
  1315. </p>
  1316. <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
  1317. <?php
  1318. }
  1319. /**
  1320. * Display the style for recent comments widget.
  1321. *
  1322. * @since 2.2.0
  1323. */
  1324. function wp_widget_recent_comments_style() {
  1325. ?>
  1326. <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
  1327. <?php
  1328. }
  1329. /**
  1330. * Register recent comments with control and hook for 'wp_head' action.
  1331. *
  1332. * @since 2.2.0
  1333. */
  1334. function wp_widget_recent_comments_register() {
  1335. $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
  1336. wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops);
  1337. wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control');
  1338. if ( is_active_widget('wp_widget_recent_comments') )
  1339. add_action('wp_head', 'wp_widget_recent_comments_style');
  1340. }
  1341. /**
  1342. * Display RSS widget.
  1343. *
  1344. * Allows for multiple widgets to be displayed.
  1345. *
  1346. * @since 2.2.0
  1347. *
  1348. * @param array $args Widget arguments.
  1349. * @param int $number Widget number.
  1350. */
  1351. function wp_widget_rss($args, $widget_args = 1) {
  1352. extract($args, EXTR_SKIP);
  1353. if ( is_numeric($widget_args) )
  1354. $widget_args = array( 'number' => $widget_args );
  1355. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  1356. extract($widget_args, EXTR_SKIP);
  1357. $options = get_option('widget_rss');
  1358. if ( !isset($options[$number]) )
  1359. return;
  1360. if ( isset($options[$number]['error']) && $options[$number]['error'] )
  1361. return;
  1362. $url = $options[$number]['url'];
  1363. while ( stristr($url, 'http') != $url )
  1364. $url = substr($url, 1);
  1365. if ( empty($url) )
  1366. return;
  1367. $rss = fetch_feed($url);
  1368. $title = $options[$number]['title'];
  1369. $desc = '';
  1370. $link = '';
  1371. if ( ! is_wp_error($rss) ) {
  1372. $desc = attribute_escape(strip_tags(html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
  1373. if ( empty($title) )
  1374. $title = htmlentities(strip_tags($rss->get_title()));
  1375. $link = clean_url(strip_tags($rss->get_permalink()));
  1376. while ( stristr($link, 'http') != $link )
  1377. $link = substr($link, 1);
  1378. }
  1379. if ( empty($title) )
  1380. $title = $desc;
  1381. if ( empty($title) )
  1382. $title = __('Unknown Feed');
  1383. $title = apply_filters('widget_title', $title );
  1384. $url = clean_url(strip_tags($url));
  1385. $icon = includes_url('images/rss.png');
  1386. $title = "<a class='rsswidget' href='$url' title='" . attribute_escape(__('Syndicate this content')) ."'><img style='background:orange;color:white;border:none;' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
  1387. echo $before_widget;
  1388. echo $before_title . $title . $after_title;
  1389. wp_widget_rss_output( $rss, $options[$number] );
  1390. echo $after_widget;
  1391. }
  1392. /**
  1393. * Display the RSS entries in a list.
  1394. *
  1395. * @since 2.5.0
  1396. *
  1397. * @param string|array|object $rss RSS url.
  1398. * @param array $args Widget arguments.
  1399. */
  1400. function wp_widget_rss_output( $rss, $args = array() ) {
  1401. if ( is_string( $rss ) ) {
  1402. $rss = fetch_feed($rss);
  1403. } elseif ( is_array($rss) && isset($rss['url']) ) {
  1404. $args = $rss;
  1405. $rss = fetch_feed($rss['url']);
  1406. } elseif ( !is_object($rss) ) {
  1407. return;
  1408. }
  1409. if ( is_wp_error($rss) ) {
  1410. if ( is_admin() || current_user_can('manage_options') ) {
  1411. echo '<p>';
  1412. printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message());
  1413. echo '</p>';
  1414. }
  1415. return;
  1416. }
  1417. $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
  1418. $args = wp_parse_args( $args, $default_args );
  1419. extract( $args, EXTR_SKIP );
  1420. $items = (int) $items;
  1421. if ( $items < 1 || 20 < $items )
  1422. $items = 10;
  1423. $show_summary = (int) $show_summary;
  1424. $show_author = (int) $show_author;
  1425. $show_date = (int) $show_date;
  1426. if ( !$rss->get_item_quantity() ) {
  1427. echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
  1428. return;
  1429. }
  1430. echo '<ul>';
  1431. foreach ( $rss->get_items(0, $items) as $item ) {
  1432. $link = $item->get_link();
  1433. while ( stristr($link, 'http') != $link )
  1434. $link = substr($link, 1);
  1435. $link = clean_url(strip_tags($link));
  1436. $title = attribute_escape(strip_tags($item->get_title()));
  1437. if ( empty($title) )
  1438. $title = __('Untitled');
  1439. $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item->get_description(), ENT_QUOTES, get_option('blog_charset')))));
  1440. $desc = wp_html_excerpt( $desc, 360 ) . ' [&hellip;]';
  1441. $desc = wp_specialchars( $desc );
  1442. if ( $show_summary ) {
  1443. $summary = "<div class='rssSummary'>$desc</div>";
  1444. } else {
  1445. $summary = '';
  1446. }
  1447. $date = '';
  1448. if ( $show_date ) {
  1449. $date = $item->get_date();
  1450. if ( $date ) {
  1451. if ( $date_stamp = strtotime( $date ) )
  1452. $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
  1453. else
  1454. $date = '';
  1455. }
  1456. }
  1457. $author = '';
  1458. if ( $show_author ) {
  1459. $author = $item->get_author();
  1460. $author = $author->get_name();
  1461. $author = ' <cite>' . wp_specialchars( strip_tags( $author ) ) . '</cite>';
  1462. }
  1463. if ( $link == '' ) {
  1464. echo "<li>$title{$date}{$summary}{$author}</li>";
  1465. } else {
  1466. echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
  1467. }
  1468. }
  1469. echo '</ul>';
  1470. }
  1471. /**
  1472. * Display and process RSS widget control form.
  1473. *
  1474. * @since 2.2.0
  1475. *
  1476. * @param int $widget_args Widget number.
  1477. */
  1478. function wp_widget_rss_control($widget_args) {
  1479. global $wp_registered_widgets;
  1480. static $updated = false;
  1481. if ( is_numeric($widget_args) )
  1482. $widget_args = array( 'number' => $widget_args );
  1483. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  1484. extract($widget_args, EXTR_SKIP);
  1485. $options = get_option('widget_rss');
  1486. if ( !is_array($options) )
  1487. $options = array();
  1488. $urls = array();
  1489. foreach ( (array) $options as $option )
  1490. if ( isset($option['url']) )
  1491. $urls[$option['url']] = true;
  1492. if ( !$updated && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['sidebar']) ) {
  1493. $sidebar = (string) $_POST['sidebar'];
  1494. $sidebars_widgets = wp_get_sidebars_widgets();
  1495. if ( isset($sidebars_widgets[$sidebar]) )
  1496. $this_sidebar =& $sidebars_widgets[$sidebar];
  1497. else
  1498. $this_sidebar = array();
  1499. foreach ( (array) $this_sidebar as $_widget_id ) {
  1500. if ( 'wp_widget_rss' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
  1501. $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
  1502. if ( !in_array( "rss-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
  1503. unset($options[$widget_number]);
  1504. }
  1505. }
  1506. foreach( (array) $_POST['widget-rss'] as $widget_number => $widget_rss ) {
  1507. if ( !isset($widget_rss['url']) && isset($options[$widget_number]) ) // user clicked cancel
  1508. continue;
  1509. $widget_rss = stripslashes_deep( $widget_rss );
  1510. $url = sanitize_url(strip_tags($widget_rss['url']));
  1511. $options[$widget_number] = wp_widget_rss_process( $widget_rss, !isset($urls[$url]) );
  1512. }
  1513. update_option('widget_rss', $options);
  1514. $updated = true;
  1515. }
  1516. if ( -1 == $number ) {
  1517. $title = '';
  1518. $url = '';
  1519. $items = 10;
  1520. $error = false;
  1521. $number = '%i%';
  1522. $show_summary = 0;
  1523. $show_author = 0;
  1524. $show_date = 0;
  1525. } else {
  1526. extract( (array) $options[$number] );
  1527. }
  1528. wp_widget_rss_form( compact( 'number', 'title', 'url', 'items', 'error', 'show_summary', 'show_author', 'show_date' ) );
  1529. }
  1530. /**
  1531. * Display RSS widget options form.
  1532. *
  1533. * The options for what fields are displayed for the RSS form are all booleans
  1534. * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
  1535. * 'show_date'.
  1536. *
  1537. * @since 2.5.0
  1538. *
  1539. * @param array|string $args Values for input fields.
  1540. * @param array $inputs Override default display options.
  1541. */
  1542. function wp_widget_rss_form( $args, $inputs = null ) {
  1543. $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
  1544. $inputs = wp_parse_args( $inputs, $default_inputs );
  1545. extract( $args );
  1546. extract( $inputs, EXTR_SKIP);
  1547. $number = attribute_escape( $number );
  1548. $title = attribute_escape( $title );
  1549. $url = attribute_escape( $url );
  1550. $items = (int) $items;
  1551. if ( $items < 1 || 20 < $items )
  1552. $items = 10;
  1553. $show_summary = (int) $show_summary;
  1554. $show_author = (int) $show_author;
  1555. $show_date = (int) $show_date;
  1556. if ( !empty($error) ) {
  1557. $message = sprintf( __('Error in RSS Widget: %s'), $error);
  1558. echo "<div class='error'><p>$message</p></div>";
  1559. echo "<p class='hide-if-no-js'><strong>$message</strong></p>";
  1560. }
  1561. if ( $inputs['url'] ) :
  1562. ?>
  1563. <p>
  1564. <label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?>
  1565. <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" />
  1566. </label>
  1567. </p>
  1568. <?php endif; if ( $inputs['title'] ) : ?>
  1569. <p>
  1570. <label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?>
  1571. <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
  1572. </label>
  1573. </p>
  1574. <?php endif; if ( $inputs['items'] ) : ?>
  1575. <p>
  1576. <label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?>
  1577. <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
  1578. <?php
  1579. for ( $i = 1; $i <= 20; ++$i )
  1580. echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
  1581. ?>
  1582. </select>
  1583. </label>
  1584. </p>
  1585. <?php endif; if ( $inputs['show_summary'] ) : ?>
  1586. <p>
  1587. <label for="rss-show-summary-<?php echo $number; ?>">
  1588. <input id="rss-show-summary-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_summary]" type="checkbox" value="1" <?php if ( $show_summary ) echo 'checked="checked"'; ?>/>
  1589. <?php _e('Display item content?'); ?>
  1590. </label>
  1591. </p>
  1592. <?php endif; if ( $inputs['show_author'] ) : ?>
  1593. <p>
  1594. <label for="rss-show-author-<?php echo $number; ?>">
  1595. <input id="rss-show-author-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_author]" type="checkbox" value="1" <?php if ( $show_author ) echo 'checked="checked"'; ?>/>
  1596. <?php _e('Display item author if available?'); ?>
  1597. </label>
  1598. </p>
  1599. <?php endif; if ( $inputs['show_date'] ) : ?>
  1600. <p>
  1601. <label for="rss-show-date-<?php echo $number; ?>">
  1602. <input id="rss-show-date-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_date]" type="checkbox" value="1" <?php if ( $show_date ) echo 'checked="checked"'; ?>/>
  1603. <?php _e('Display item date?'); ?>
  1604. </label>
  1605. </p>
  1606. <input type="hidden" name="widget-rss[<?php echo $number; ?>][submit]" value="1" />
  1607. <?php
  1608. endif;
  1609. foreach ( array_keys($default_inputs) as $input ) :
  1610. if ( 'hidden' === $inputs[$input] ) :
  1611. $id = str_replace( '_', '-', $input );
  1612. ?>
  1613. <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
  1614. <?php
  1615. endif;
  1616. endforeach;
  1617. }
  1618. /**
  1619. * Process RSS feed widget data and optionally retrieve feed items.
  1620. *
  1621. * The feed widget can not have more than 20 items or it will reset back to the
  1622. * default, which is 10.
  1623. *
  1624. * The resulting array has the feed title, feed url, feed link (from channel),
  1625. * feed items, error (if any), and whether to show summary, author, and date.
  1626. * All respectively in the order of the array elements.
  1627. *
  1628. * @since 2.5.0
  1629. *
  1630. * @param array $widget_rss RSS widget feed data. Expects unescaped data.
  1631. * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
  1632. * @return array
  1633. */
  1634. function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
  1635. $items = (int) $widget_rss['items'];
  1636. if ( $items < 1 || 20 < $items )
  1637. $items = 10;
  1638. $url = sanitize_url(strip_tags( $widget_rss['url'] ));
  1639. $title = trim(strip_tags( $widget_rss['title'] ));
  1640. $show_summary = (int) $widget_rss['show_summary'];
  1641. $show_author = (int) $widget_rss['show_author'];
  1642. $show_date = (int) $widget_rss['show_date'];
  1643. if ( $check_feed ) {
  1644. $rss = fetch_feed($url);
  1645. $error = false;
  1646. $link = '';
  1647. if ( is_wp_error($rss) ) {
  1648. $error = $rss->get_error_message();
  1649. } else {
  1650. $link = clean_url(strip_tags($rss->get_permalink()));
  1651. while ( stristr($link, 'http') != $link )
  1652. $link = substr($link, 1);
  1653. }
  1654. }
  1655. return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
  1656. }
  1657. /**
  1658. * Register RSS widget to allow multiple RSS widgets on startup.
  1659. *
  1660. * @since 2.2.0
  1661. */
  1662. function wp_widget_rss_register() {
  1663. if ( !$options = get_option('widget_rss') )
  1664. $options = array();
  1665. $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
  1666. $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
  1667. $name = __('RSS');
  1668. $id = false;
  1669. foreach ( (array) array_keys($options) as $o ) {
  1670. // Old widgets can have null values for some reason
  1671. if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
  1672. continue;
  1673. $id = "rss-$o"; // Never never never translate an id
  1674. wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
  1675. wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
  1676. }
  1677. // If there are none, we register the widget's existance with a generic template
  1678. if ( !$id ) {
  1679. wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
  1680. wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
  1681. }
  1682. }
  1683. /**
  1684. * Display tag cloud widget.
  1685. *
  1686. * @since 2.3.0
  1687. *
  1688. * @param array $args Widget arguments.
  1689. */
  1690. function wp_widget_tag_cloud($args) {
  1691. extract($args);
  1692. $options = get_option('widget_tag_cloud');
  1693. $title = empty($options['title']) ? __('Tags') : apply_filters('widget_title', $options['title']);
  1694. echo $before_widget;
  1695. echo $before_title . $title . $after_title;
  1696. wp_tag_cloud();
  1697. echo $after_widget;
  1698. }
  1699. /**
  1700. * Manage WordPress Tag Cloud widget options.
  1701. *
  1702. * Displays management form for changing the tag cloud widget title.
  1703. *
  1704. * @since 2.3.0
  1705. */
  1706. function wp_widget_tag_cloud_control() {
  1707. $options = $newoptions = get_option('widget_tag_cloud');
  1708. if ( isset($_POST['tag-cloud-submit']) ) {
  1709. $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
  1710. }
  1711. if ( $options != $newoptions ) {
  1712. $options = $newoptions;
  1713. update_option('widget_tag_cloud', $options);
  1714. }
  1715. $title = attribute_escape( $options['title'] );
  1716. ?>
  1717. <p><label for="tag-cloud-title">
  1718. <?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
  1719. </p>
  1720. <input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
  1721. <?php
  1722. }
  1723. /**
  1724. * Register all of the default WordPress widgets on startup.
  1725. *
  1726. * Calls 'widgets_init' action after all of the WordPress widgets have been
  1727. * registered.
  1728. *
  1729. * @since 2.2.0
  1730. */
  1731. function wp_widgets_init() {
  1732. if ( !is_blog_installed() )
  1733. return;
  1734. $widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
  1735. wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
  1736. wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
  1737. $widget_ops = array('classname' => 'widget_calendar', 'description' => __( "A calendar of your blog's posts") );
  1738. wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
  1739. wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control' );
  1740. $widget_ops = array('classname' => 'widget_archive', 'description' => __( "A monthly archive of your blog's posts") );
  1741. wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $widget_ops);
  1742. wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
  1743. $widget_ops = array('description' => __( "Your blogroll" ) );
  1744. $wp_widget_links = new WP_Widget_Links('links', __('Links'), $widget_ops);
  1745. $wp_widget_links->register();
  1746. $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
  1747. wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $widget_ops);
  1748. wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control' );
  1749. $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your blog") );
  1750. wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $widget_ops);
  1751. $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
  1752. wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
  1753. wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
  1754. $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
  1755. wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);
  1756. wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
  1757. wp_widget_categories_register();
  1758. wp_widget_text_register();
  1759. wp_widget_rss_register();
  1760. wp_widget_recent_comments_register();
  1761. do_action('widgets_init');
  1762. }
  1763. add_action('init', 'wp_widgets_init', 1);
  1764. /*
  1765. * Pattern for multi-widget (allows multiple instances such as the text widget).
  1766. *
  1767. * Make sure to close the comments after copying.
  1768. /**
  1769. * Displays widget.
  1770. *
  1771. * Supports multiple widgets.
  1772. *
  1773. * @param array $args Widget arguments.
  1774. * @param array|int $widget_args Widget number. Which of the several widgets of this type do we mean.
  1775. * /
  1776. function widget_many( $args, $widget_args = 1 ) {
  1777. extract( $args, EXTR_SKIP );
  1778. if ( is_numeric($widget_args) )
  1779. $widget_args = array( 'number' => $widget_args );
  1780. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  1781. extract( $widget_args, EXTR_SKIP );
  1782. // Data should be stored as array: array( number => data for that instance of the widget, ... )
  1783. $options = get_option('widget_many');
  1784. if ( !isset($options[$number]) )
  1785. return;
  1786. echo $before_widget;
  1787. // Do stuff for this widget, drawing data from $options[$number]
  1788. echo $after_widget;
  1789. }
  1790. /**
  1791. * Displays form for a particular instance of the widget.
  1792. *
  1793. * Also updates the data after a POST submit.
  1794. *
  1795. * @param array|int $widget_args Widget number. Which of the several widgets of this type do we mean.
  1796. * /
  1797. function widget_many_control( $widget_args = 1 ) {
  1798. global $wp_registered_widgets;
  1799. static $updated = false; // Whether or not we have already updated the data after a POST submit
  1800. if ( is_numeric($widget_args) )
  1801. $widget_args = array( 'number' => $widget_args );
  1802. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  1803. extract( $widget_args, EXTR_SKIP );
  1804. // Data should be stored as array: array( number => data for that instance of the widget, ... )
  1805. $options = get_option('widget_many');
  1806. if ( !is_array($options) )
  1807. $options = array();
  1808. // We need to update the data
  1809. if ( !$updated && !empty($_POST['sidebar']) ) {
  1810. // Tells us what sidebar to put the data in
  1811. $sidebar = (string) $_POST['sidebar'];
  1812. $sidebars_widgets = wp_get_sidebars_widgets();
  1813. if ( isset($sidebars_widgets[$sidebar]) )
  1814. $this_sidebar =& $sidebars_widgets[$sidebar];
  1815. else
  1816. $this_sidebar = array();
  1817. foreach ( $this_sidebar as $_widget_id ) {
  1818. // Remove all widgets of this type from the sidebar. We'll add the new data in a second. This makes sure we don't get any duplicate data
  1819. // since widget ids aren't necessarily persistent across multiple updates
  1820. if ( 'widget_many' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
  1821. $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
  1822. if ( !in_array( "many-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
  1823. unset($options[$widget_number]);
  1824. }
  1825. }
  1826. foreach ( (array) $_POST['widget-many'] as $widget_number => $widget_many_instance ) {
  1827. // compile data from $widget_many_instance
  1828. if ( !isset($widget_many_instance['something']) && isset($options[$widget_number]) ) // user clicked cancel
  1829. continue;
  1830. $something = wp_specialchars( $widget_many_instance['something'] );
  1831. $options[$widget_number] = array( 'something' => $something ); // Even simple widgets should store stuff in array, rather than in scalar
  1832. }
  1833. update_option('widget_many', $options);
  1834. $updated = true; // So that we don't go through this more than once
  1835. }
  1836. // Here we echo out the form
  1837. if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
  1838. $something = '';
  1839. $number = '%i%';
  1840. } else {
  1841. $something = attribute_escape($options[$number]['something']);
  1842. }
  1843. // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
  1844. // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
  1845. ?>
  1846. <p>
  1847. <input class="widefat" id="widget-many-something-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][something]" type="text" value="<?php echo $data; ?>" />
  1848. <input type="hidden" id="widget-many-submit-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][submit]" value="1" />
  1849. </p>
  1850. <?php
  1851. }
  1852. /**
  1853. * Registers each instance of our widget on startup.
  1854. * /
  1855. function widget_many_register() {
  1856. if ( !$options = get_option('widget_many') )
  1857. $options = array();
  1858. $widget_ops = array('classname' => 'widget_many', 'description' => __('Widget which allows multiple instances'));
  1859. $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'many');
  1860. $name = __('Many');
  1861. $registered = false;
  1862. foreach ( array_keys($options) as $o ) {
  1863. // Old widgets can have null values for some reason
  1864. if ( !isset($options[$o]['something']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
  1865. continue;
  1866. // $id should look like {$id_base}-{$o}
  1867. $id = "many-$o"; // Never never never translate an id
  1868. $registered = true;
  1869. wp_register_sidebar_widget( $id, $name, 'widget_many', $widget_ops, array( 'number' => $o ) );
  1870. wp_register_widget_control( $id, $name, 'widget_many_control', $control_ops, array( 'number' => $o ) );
  1871. }
  1872. // If there are none, we register the widget's existance with a generic template
  1873. if ( !$registered ) {
  1874. wp_register_sidebar_widget( 'many-1', $name, 'widget_many', $widget_ops, array( 'number' => -1 ) );
  1875. wp_register_widget_control( 'many-1', $name, 'widget_many_control', $control_ops, array( 'number' => -1 ) );
  1876. }
  1877. }
  1878. // This is important
  1879. add_action( 'widgets_init', 'widget_many_register' );
  1880. */
  1881. ?>