PageRenderTime 76ms CodeModel.GetById 19ms RepoModel.GetById 1ms 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

Large files files are truncated, but you can click here to view the full 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_acti

Large files files are truncated, but you can click here to view the full file