You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1476 lines
47 KiB

  1. <?php
  2. /**
  3. * Core Widgets API
  4. *
  5. * This API is used for creating dynamic sidebar without hardcoding functionality into
  6. * themes
  7. *
  8. * Includes both internal WordPress routines and theme-use routines.
  9. *
  10. * This functionality was found in a plugin before the WordPress 2.2 release, which
  11. * included it in the core from that point on.
  12. *
  13. * @link https://codex.wordpress.org/Plugins/WordPress_Widgets WordPress Widgets
  14. * @link https://codex.wordpress.org/Plugins/WordPress_Widgets_Api Widgets API
  15. *
  16. * @package WordPress
  17. * @subpackage Widgets
  18. * @since 2.2.0
  19. */
  20. //
  21. // Global Variables
  22. //
  23. /** @ignore */
  24. global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
  25. /**
  26. * Stores the sidebars, since many themes can have more than one.
  27. *
  28. * @global array $wp_registered_sidebars
  29. * @since 2.2.0
  30. */
  31. $wp_registered_sidebars = array();
  32. /**
  33. * Stores the registered widgets.
  34. *
  35. * @global array $wp_registered_widgets
  36. * @since 2.2.0
  37. */
  38. $wp_registered_widgets = array();
  39. /**
  40. * Stores the registered widget control (options).
  41. *
  42. * @global array $wp_registered_widget_controls
  43. * @since 2.2.0
  44. */
  45. $wp_registered_widget_controls = array();
  46. /**
  47. * @global array $wp_registered_widget_updates
  48. */
  49. $wp_registered_widget_updates = array();
  50. /**
  51. * Private
  52. *
  53. * @global array $_wp_sidebars_widgets
  54. */
  55. $_wp_sidebars_widgets = array();
  56. /**
  57. * Private
  58. *
  59. * @global array $_wp_deprecated_widgets_callbacks
  60. */
  61. $GLOBALS['_wp_deprecated_widgets_callbacks'] = array(
  62. 'wp_widget_pages',
  63. 'wp_widget_pages_control',
  64. 'wp_widget_calendar',
  65. 'wp_widget_calendar_control',
  66. 'wp_widget_archives',
  67. 'wp_widget_archives_control',
  68. 'wp_widget_links',
  69. 'wp_widget_meta',
  70. 'wp_widget_meta_control',
  71. 'wp_widget_search',
  72. 'wp_widget_recent_entries',
  73. 'wp_widget_recent_entries_control',
  74. 'wp_widget_tag_cloud',
  75. 'wp_widget_tag_cloud_control',
  76. 'wp_widget_categories',
  77. 'wp_widget_categories_control',
  78. 'wp_widget_text',
  79. 'wp_widget_text_control',
  80. 'wp_widget_rss',
  81. 'wp_widget_rss_control',
  82. 'wp_widget_recent_comments',
  83. 'wp_widget_recent_comments_control'
  84. );
  85. //
  86. // Template tags & API functions
  87. //
  88. /**
  89. * Register a widget
  90. *
  91. * Registers a WP_Widget widget
  92. *
  93. * @since 2.8.0
  94. *
  95. * @see WP_Widget
  96. *
  97. * @global WP_Widget_Factory $wp_widget_factory
  98. *
  99. * @param string $widget_class The name of a class that extends WP_Widget
  100. */
  101. function register_widget($widget_class) {
  102. global $wp_widget_factory;
  103. $wp_widget_factory->register($widget_class);
  104. }
  105. /**
  106. * Unregisters a widget.
  107. *
  108. * Unregisters a WP_Widget widget. Useful for un-registering default widgets.
  109. * Run within a function hooked to the {@see 'widgets_init'} action.
  110. *
  111. * @since 2.8.0
  112. *
  113. * @see WP_Widget
  114. *
  115. * @global WP_Widget_Factory $wp_widget_factory
  116. *
  117. * @param string $widget_class The name of a class that extends WP_Widget.
  118. */
  119. function unregister_widget($widget_class) {
  120. global $wp_widget_factory;
  121. $wp_widget_factory->unregister($widget_class);
  122. }
  123. /**
  124. * Creates multiple sidebars.
  125. *
  126. * If you wanted to quickly create multiple sidebars for a theme or internally.
  127. * This function will allow you to do so. If you don't pass the 'name' and/or
  128. * 'id' in `$args`, then they will be built for you.
  129. *
  130. * @since 2.2.0
  131. *
  132. * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
  133. *
  134. * @global array $wp_registered_sidebars
  135. *
  136. * @param int $number Optional. Number of sidebars to create. Default 1.
  137. * @param array|string $args {
  138. * Optional. Array or string of arguments for building a sidebar.
  139. *
  140. * @type string $id The base string of the unique identifier for each sidebar. If provided, and multiple
  141. * sidebars are being defined, the id will have "-2" appended, and so on.
  142. * Default 'sidebar-' followed by the number the sidebar creation is currently at.
  143. * @type string $name The name or title for the sidebars displayed in the admin dashboard. If registering
  144. * more than one sidebar, include '%d' in the string as a placeholder for the uniquely
  145. * assigned number for each sidebar.
  146. * Default 'Sidebar' for the first sidebar, otherwise 'Sidebar %d'.
  147. * }
  148. */
  149. function register_sidebars( $number = 1, $args = array() ) {
  150. global $wp_registered_sidebars;
  151. $number = (int) $number;
  152. if ( is_string($args) )
  153. parse_str($args, $args);
  154. for ( $i = 1; $i <= $number; $i++ ) {
  155. $_args = $args;
  156. if ( $number > 1 )
  157. $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
  158. else
  159. $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
  160. // Custom specified ID's are suffixed if they exist already.
  161. // Automatically generated sidebar names need to be suffixed regardless starting at -0
  162. if ( isset($args['id']) ) {
  163. $_args['id'] = $args['id'];
  164. $n = 2; // Start at -2 for conflicting custom ID's
  165. while ( is_registered_sidebar( $_args['id'] ) ) {
  166. $_args['id'] = $args['id'] . '-' . $n++;
  167. }
  168. } else {
  169. $n = count( $wp_registered_sidebars );
  170. do {
  171. $_args['id'] = 'sidebar-' . ++$n;
  172. } while ( is_registered_sidebar( $_args['id'] ) );
  173. }
  174. register_sidebar($_args);
  175. }
  176. }
  177. /**
  178. * Builds the definition for a single sidebar and returns the ID.
  179. *
  180. * Accepts either a string or an array and then parses that against a set
  181. * of default arguments for the new sidebar. WordPress will automatically
  182. * generate a sidebar ID and name based on the current number of registered
  183. * sidebars if those arguments are not included.
  184. *
  185. * When allowing for automatic generation of the name and ID parameters, keep
  186. * in mind that the incrementor for your sidebar can change over time depending
  187. * on what other plugins and themes are installed.
  188. *
  189. * If theme support for 'widgets' has not yet been added when this function is
  190. * called, it will be automatically enabled through the use of add_theme_support()
  191. *
  192. * @since 2.2.0
  193. *
  194. * @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
  195. *
  196. * @param array|string $args {
  197. * Optional. Array or string of arguments for the sidebar being registered.
  198. *
  199. * @type string $name The name or title of the sidebar displayed in the Widgets
  200. * interface. Default 'Sidebar $instance'.
  201. * @type string $id The unique identifier by which the sidebar will be called.
  202. * Default 'sidebar-$instance'.
  203. * @type string $description Description of the sidebar, displayed in the Widgets interface.
  204. * Default empty string.
  205. * @type string $class Extra CSS class to assign to the sidebar in the Widgets interface.
  206. * Default empty.
  207. * @type string $before_widget HTML content to prepend to each widget's HTML output when
  208. * assigned to this sidebar. Default is an opening list item element.
  209. * @type string $after_widget HTML content to append to each widget's HTML output when
  210. * assigned to this sidebar. Default is a closing list item element.
  211. * @type string $before_title HTML content to prepend to the sidebar title when displayed.
  212. * Default is an opening h2 element.
  213. * @type string $after_title HTML content to append to the sidebar title when displayed.
  214. * Default is a closing h2 element.
  215. * }
  216. * @return string Sidebar ID added to $wp_registered_sidebars global.
  217. */
  218. function register_sidebar($args = array()) {
  219. global $wp_registered_sidebars;
  220. $i = count($wp_registered_sidebars) + 1;
  221. $id_is_empty = empty( $args['id'] );
  222. $defaults = array(
  223. 'name' => sprintf(__('Sidebar %d'), $i ),
  224. 'id' => "sidebar-$i",
  225. 'description' => '',
  226. 'class' => '',
  227. 'before_widget' => '<li id="%1$s" class="widget %2$s">',
  228. 'after_widget' => "</li>\n",
  229. 'before_title' => '<h2 class="widgettitle">',
  230. 'after_title' => "</h2>\n",
  231. );
  232. $sidebar = wp_parse_args( $args, $defaults );
  233. if ( $id_is_empty ) {
  234. /* translators: 1: the id argument, 2: sidebar name, 3: recommended id value */
  235. _doing_it_wrong( __FUNCTION__, sprintf( __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), '<code>id</code>', $sidebar['name'], $sidebar['id'] ), '4.2.0' );
  236. }
  237. $wp_registered_sidebars[$sidebar['id']] = $sidebar;
  238. add_theme_support('widgets');
  239. /**
  240. * Fires once a sidebar has been registered.
  241. *
  242. * @since 3.0.0
  243. *
  244. * @param array $sidebar Parsed arguments for the registered sidebar.
  245. */
  246. do_action( 'register_sidebar', $sidebar );
  247. return $sidebar['id'];
  248. }
  249. /**
  250. * Removes a sidebar from the list.
  251. *
  252. * @since 2.2.0
  253. *
  254. * @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
  255. *
  256. * @param string $name The ID of the sidebar when it was added.
  257. */
  258. function unregister_sidebar( $name ) {
  259. global $wp_registered_sidebars;
  260. unset( $wp_registered_sidebars[ $name ] );
  261. }
  262. /**
  263. * Checks if a sidebar is registered.
  264. *
  265. * @since 4.4.0
  266. *
  267. * @global array $wp_registered_sidebars Registered sidebars.
  268. *
  269. * @param string|int $sidebar_id The ID of the sidebar when it was registered.
  270. * @return bool True if the sidebar is registered, false otherwise.
  271. */
  272. function is_registered_sidebar( $sidebar_id ) {
  273. global $wp_registered_sidebars;
  274. return isset( $wp_registered_sidebars[ $sidebar_id ] );
  275. }
  276. /**
  277. * Register an instance of a widget.
  278. *
  279. * The default widget option is 'classname' that can be overridden.
  280. *
  281. * The function can also be used to un-register widgets when `$output_callback`
  282. * parameter is an empty string.
  283. *
  284. * @since 2.2.0
  285. *
  286. * @global array $wp_registered_widgets Uses stored registered widgets.
  287. * @global array $wp_register_widget_defaults Retrieves widget defaults.
  288. * @global array $wp_registered_widget_updates
  289. * @global array $_wp_deprecated_widgets_callbacks
  290. *
  291. * @param int|string $id Widget ID.
  292. * @param string $name Widget display title.
  293. * @param callable $output_callback Run when widget is called.
  294. * @param array $options {
  295. * Optional. An array of supplementary widget options for the instance.
  296. *
  297. * @type string $classname Class name for the widget's HTML container. Default is a shortened
  298. * version of the output callback name.
  299. * @type string $description Widget description for display in the widget administration
  300. * panel and/or theme.
  301. * }
  302. */
  303. function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) {
  304. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
  305. $id = strtolower($id);
  306. if ( empty($output_callback) ) {
  307. unset($wp_registered_widgets[$id]);
  308. return;
  309. }
  310. $id_base = _get_widget_id_base($id);
  311. if ( in_array($output_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($output_callback) ) {
  312. unset( $wp_registered_widget_controls[ $id ] );
  313. unset( $wp_registered_widget_updates[ $id_base ] );
  314. return;
  315. }
  316. $defaults = array('classname' => $output_callback);
  317. $options = wp_parse_args($options, $defaults);
  318. $widget = array(
  319. 'name' => $name,
  320. 'id' => $id,
  321. 'callback' => $output_callback,
  322. 'params' => array_slice(func_get_args(), 4)
  323. );
  324. $widget = array_merge($widget, $options);
  325. if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) {
  326. /**
  327. * Fires once for each registered widget.
  328. *
  329. * @since 3.0.0
  330. *
  331. * @param array $widget An array of default widget arguments.
  332. */
  333. do_action( 'wp_register_sidebar_widget', $widget );
  334. $wp_registered_widgets[$id] = $widget;
  335. }
  336. }
  337. /**
  338. * Retrieve description for widget.
  339. *
  340. * When registering widgets, the options can also include 'description' that
  341. * describes the widget for display on the widget administration panel or
  342. * in the theme.
  343. *
  344. * @since 2.5.0
  345. *
  346. * @global array $wp_registered_widgets
  347. *
  348. * @param int|string $id Widget ID.
  349. * @return string|void Widget description, if available.
  350. */
  351. function wp_widget_description( $id ) {
  352. if ( !is_scalar($id) )
  353. return;
  354. global $wp_registered_widgets;
  355. if ( isset($wp_registered_widgets[$id]['description']) )
  356. return esc_html( $wp_registered_widgets[$id]['description'] );
  357. }
  358. /**
  359. * Retrieve description for a sidebar.
  360. *
  361. * When registering sidebars a 'description' parameter can be included that
  362. * describes the sidebar for display on the widget administration panel.
  363. *
  364. * @since 2.9.0
  365. *
  366. * @global array $wp_registered_sidebars
  367. *
  368. * @param string $id sidebar ID.
  369. * @return string|void Sidebar description, if available.
  370. */
  371. function wp_sidebar_description( $id ) {
  372. if ( !is_scalar($id) )
  373. return;
  374. global $wp_registered_sidebars;
  375. if ( isset($wp_registered_sidebars[$id]['description']) )
  376. return esc_html( $wp_registered_sidebars[$id]['description'] );
  377. }
  378. /**
  379. * Remove widget from sidebar.
  380. *
  381. * @since 2.2.0
  382. *
  383. * @param int|string $id Widget ID.
  384. */
  385. function wp_unregister_sidebar_widget($id) {
  386. /**
  387. * Fires just before a widget is removed from a sidebar.
  388. *
  389. * @since 3.0.0
  390. *
  391. * @param int $id The widget ID.
  392. */
  393. do_action( 'wp_unregister_sidebar_widget', $id );
  394. wp_register_sidebar_widget($id, '', '');
  395. wp_unregister_widget_control($id);
  396. }
  397. /**
  398. * Registers widget control callback for customizing options.
  399. *
  400. * @since 2.2.0
  401. *
  402. * @todo `$params` parameter?
  403. *
  404. * @global array $wp_registered_widget_controls
  405. * @global array $wp_registered_widget_updates
  406. * @global array $wp_registered_widgets
  407. * @global array $_wp_deprecated_widgets_callbacks
  408. *
  409. * @param int|string $id Sidebar ID.
  410. * @param string $name Sidebar display name.
  411. * @param callable $control_callback Run when sidebar is displayed.
  412. * @param array $options {
  413. * Optional. Array or string of control options. Default empty array.
  414. *
  415. * @type int $height Never used. Default 200.
  416. * @type int $width Width of the fully expanded control form (but try hard to use the default width).
  417. * Default 250.
  418. * @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the
  419. * text widget. The widget id will end up looking like `{$id_base}-{$unique_number}`.
  420. * }
  421. */
  422. function wp_register_widget_control( $id, $name, $control_callback, $options = array() ) {
  423. global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
  424. $id = strtolower($id);
  425. $id_base = _get_widget_id_base($id);
  426. if ( empty($control_callback) ) {
  427. unset($wp_registered_widget_controls[$id]);
  428. unset($wp_registered_widget_updates[$id_base]);
  429. return;
  430. }
  431. if ( in_array($control_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($control_callback) ) {
  432. unset( $wp_registered_widgets[ $id ] );
  433. return;
  434. }
  435. if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
  436. return;
  437. $defaults = array('width' => 250, 'height' => 200 ); // height is never used
  438. $options = wp_parse_args($options, $defaults);
  439. $options['width'] = (int) $options['width'];
  440. $options['height'] = (int) $options['height'];
  441. $widget = array(
  442. 'name' => $name,
  443. 'id' => $id,
  444. 'callback' => $control_callback,
  445. 'params' => array_slice(func_get_args(), 4)
  446. );
  447. $widget = array_merge($widget, $options);
  448. $wp_registered_widget_controls[$id] = $widget;
  449. if ( isset($wp_registered_widget_updates[$id_base]) )
  450. return;
  451. if ( isset($widget['params'][0]['number']) )
  452. $widget['params'][0]['number'] = -1;
  453. unset($widget['width'], $widget['height'], $widget['name'], $widget['id']);
  454. $wp_registered_widget_updates[$id_base] = $widget;
  455. }
  456. /**
  457. * Registers the update callback for a widget.
  458. *
  459. * @since 2.8.0
  460. *
  461. * @global array $wp_registered_widget_updates
  462. *
  463. * @param string $id_base The base ID of a widget created by extending WP_Widget.
  464. * @param callable $update_callback Update callback method for the widget.
  465. * @param array $options Optional. Widget control options. See wp_register_widget_control().
  466. * Default empty array.
  467. */
  468. function _register_widget_update_callback( $id_base, $update_callback, $options = array() ) {
  469. global $wp_registered_widget_updates;
  470. if ( isset($wp_registered_widget_updates[$id_base]) ) {
  471. if ( empty($update_callback) )
  472. unset($wp_registered_widget_updates[$id_base]);
  473. return;
  474. }
  475. $widget = array(
  476. 'callback' => $update_callback,
  477. 'params' => array_slice(func_get_args(), 3)
  478. );
  479. $widget = array_merge($widget, $options);
  480. $wp_registered_widget_updates[$id_base] = $widget;
  481. }
  482. /**
  483. * Registers the form callback for a widget.
  484. *
  485. * @since 2.8.0
  486. *
  487. * @global array $wp_registered_widget_controls
  488. *
  489. * @param int|string $id Widget ID.
  490. * @param string $name Name attribute for the widget.
  491. * @param callable $form_callback Form callback.
  492. * @param array $options Optional. Widget control options. See wp_register_widget_control().
  493. * Default empty array.
  494. */
  495. function _register_widget_form_callback($id, $name, $form_callback, $options = array()) {
  496. global $wp_registered_widget_controls;
  497. $id = strtolower($id);
  498. if ( empty($form_callback) ) {
  499. unset($wp_registered_widget_controls[$id]);
  500. return;
  501. }
  502. if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
  503. return;
  504. $defaults = array('width' => 250, 'height' => 200 );
  505. $options = wp_parse_args($options, $defaults);
  506. $options['width'] = (int) $options['width'];
  507. $options['height'] = (int) $options['height'];
  508. $widget = array(
  509. 'name' => $name,
  510. 'id' => $id,
  511. 'callback' => $form_callback,
  512. 'params' => array_slice(func_get_args(), 4)
  513. );
  514. $widget = array_merge($widget, $options);
  515. $wp_registered_widget_controls[$id] = $widget;
  516. }
  517. /**
  518. * Remove control callback for widget.
  519. *
  520. * @since 2.2.0
  521. *
  522. * @param int|string $id Widget ID.
  523. */
  524. function wp_unregister_widget_control($id) {
  525. wp_register_widget_control( $id, '', '' );
  526. }
  527. /**
  528. * Display dynamic sidebar.
  529. *
  530. * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or
  531. * 'name' parameter for its registered sidebars you can pass an id or name as the $index parameter.
  532. * Otherwise, you can pass in a numerical index to display the sidebar at that index.
  533. *
  534. * @since 2.2.0
  535. *
  536. * @global array $wp_registered_sidebars
  537. * @global array $wp_registered_widgets
  538. *
  539. * @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar.
  540. * @return bool True, if widget sidebar was found and called. False if not found or not called.
  541. */
  542. function dynamic_sidebar( $index = 1 ) {
  543. global $wp_registered_sidebars, $wp_registered_widgets;
  544. if ( is_int( $index ) ) {
  545. $index = "sidebar-$index";
  546. } else {
  547. $index = sanitize_title( $index );
  548. foreach ( (array) $wp_registered_sidebars as $key => $value ) {
  549. if ( sanitize_title( $value['name'] ) == $index ) {
  550. $index = $key;
  551. break;
  552. }
  553. }
  554. }
  555. $sidebars_widgets = wp_get_sidebars_widgets();
  556. if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {
  557. /** This action is documented in wp-includes/widget.php */
  558. do_action( 'dynamic_sidebar_before', $index, false );
  559. /** This action is documented in wp-includes/widget.php */
  560. do_action( 'dynamic_sidebar_after', $index, false );
  561. /** This filter is documented in wp-includes/widget.php */
  562. return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );
  563. }
  564. /**
  565. * Fires before widgets are rendered in a dynamic sidebar.
  566. *
  567. * Note: The action also fires for empty sidebars, and on both the front end
  568. * and back end, including the Inactive Widgets sidebar on the Widgets screen.
  569. *
  570. * @since 3.9.0
  571. *
  572. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  573. * @param bool $has_widgets Whether the sidebar is populated with widgets.
  574. * Default true.
  575. */
  576. do_action( 'dynamic_sidebar_before', $index, true );
  577. $sidebar = $wp_registered_sidebars[$index];
  578. $did_one = false;
  579. foreach ( (array) $sidebars_widgets[$index] as $id ) {
  580. if ( !isset($wp_registered_widgets[$id]) ) continue;
  581. $params = array_merge(
  582. array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
  583. (array) $wp_registered_widgets[$id]['params']
  584. );
  585. // Substitute HTML id and class attributes into before_widget
  586. $classname_ = '';
  587. foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
  588. if ( is_string($cn) )
  589. $classname_ .= '_' . $cn;
  590. elseif ( is_object($cn) )
  591. $classname_ .= '_' . get_class($cn);
  592. }
  593. $classname_ = ltrim($classname_, '_');
  594. $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
  595. /**
  596. * Filters the parameters passed to a widget's display callback.
  597. *
  598. * Note: The filter is evaluated on both the front end and back end,
  599. * including for the Inactive Widgets sidebar on the Widgets screen.
  600. *
  601. * @since 2.5.0
  602. *
  603. * @see register_sidebar()
  604. *
  605. * @param array $params {
  606. * @type array $args {
  607. * An array of widget display arguments.
  608. *
  609. * @type string $name Name of the sidebar the widget is assigned to.
  610. * @type string $id ID of the sidebar the widget is assigned to.
  611. * @type string $description The sidebar description.
  612. * @type string $class CSS class applied to the sidebar container.
  613. * @type string $before_widget HTML markup to prepend to each widget in the sidebar.
  614. * @type string $after_widget HTML markup to append to each widget in the sidebar.
  615. * @type string $before_title HTML markup to prepend to the widget title when displayed.
  616. * @type string $after_title HTML markup to append to the widget title when displayed.
  617. * @type string $widget_id ID of the widget.
  618. * @type string $widget_name Name of the widget.
  619. * }
  620. * @type array $widget_args {
  621. * An array of multi-widget arguments.
  622. *
  623. * @type int $number Number increment used for multiples of the same widget.
  624. * }
  625. * }
  626. */
  627. $params = apply_filters( 'dynamic_sidebar_params', $params );
  628. $callback = $wp_registered_widgets[$id]['callback'];
  629. /**
  630. * Fires before a widget's display callback is called.
  631. *
  632. * Note: The action fires on both the front end and back end, including
  633. * for widgets in the Inactive Widgets sidebar on the Widgets screen.
  634. *
  635. * The action is not fired for empty sidebars.
  636. *
  637. * @since 3.0.0
  638. *
  639. * @param array $widget_id {
  640. * An associative array of widget arguments.
  641. *
  642. * @type string $name Name of the widget.
  643. * @type string $id Widget ID.
  644. * @type array|callable $callback When the hook is fired on the front end, $callback is an array
  645. * containing the widget object. Fired on the back end, $callback
  646. * is 'wp_widget_control', see $_callback.
  647. * @type array $params An associative array of multi-widget arguments.
  648. * @type string $classname CSS class applied to the widget container.
  649. * @type string $description The widget description.
  650. * @type array $_callback When the hook is fired on the back end, $_callback is populated
  651. * with an array containing the widget object, see $callback.
  652. * }
  653. */
  654. do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] );
  655. if ( is_callable($callback) ) {
  656. call_user_func_array($callback, $params);
  657. $did_one = true;
  658. }
  659. }
  660. /**
  661. * Fires after widgets are rendered in a dynamic sidebar.
  662. *
  663. * Note: The action also fires for empty sidebars, and on both the front end
  664. * and back end, including the Inactive Widgets sidebar on the Widgets screen.
  665. *
  666. * @since 3.9.0
  667. *
  668. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  669. * @param bool $has_widgets Whether the sidebar is populated with widgets.
  670. * Default true.
  671. */
  672. do_action( 'dynamic_sidebar_after', $index, true );
  673. /**
  674. * Filters whether a sidebar has widgets.
  675. *
  676. * Note: The filter is also evaluated for empty sidebars, and on both the front end
  677. * and back end, including the Inactive Widgets sidebar on the Widgets screen.
  678. *
  679. * @since 3.9.0
  680. *
  681. * @param bool $did_one Whether at least one widget was rendered in the sidebar.
  682. * Default false.
  683. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  684. */
  685. return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index );
  686. }
  687. /**
  688. * Whether widget is displayed on the front end.
  689. *
  690. * Either $callback or $id_base can be used
  691. * $id_base is the first argument when extending WP_Widget class
  692. * Without the optional $widget_id parameter, returns the ID of the first sidebar
  693. * in which the first instance of the widget with the given callback or $id_base is found.
  694. * With the $widget_id parameter, returns the ID of the sidebar where
  695. * the widget with that callback/$id_base AND that ID is found.
  696. *
  697. * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
  698. * this function has to run after widgets have initialized, at action {@see 'init'} or later.
  699. *
  700. * @since 2.2.0
  701. *
  702. * @global array $wp_registered_widgets
  703. *
  704. * @param string|false $callback Optional, Widget callback to check. Default false.
  705. * @param int|false $widget_id Optional. Widget ID. Optional, but needed for checking. Default false.
  706. * @param string|false $id_base Optional. The base ID of a widget created by extending WP_Widget. Default false.
  707. * @param bool $skip_inactive Optional. Whether to check in 'wp_inactive_widgets'. Default true.
  708. * @return string|false False if widget is not active or id of sidebar in which the widget is active.
  709. */
  710. function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) {
  711. global $wp_registered_widgets;
  712. $sidebars_widgets = wp_get_sidebars_widgets();
  713. if ( is_array($sidebars_widgets) ) {
  714. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  715. if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) {
  716. continue;
  717. }
  718. if ( is_array($widgets) ) {
  719. foreach ( $widgets as $widget ) {
  720. if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && _get_widget_id_base($widget) == $id_base ) ) {
  721. if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
  722. return $sidebar;
  723. }
  724. }
  725. }
  726. }
  727. }
  728. return false;
  729. }
  730. /**
  731. * Whether the dynamic sidebar is enabled and used by theme.
  732. *
  733. * @since 2.2.0
  734. *
  735. * @global array $wp_registered_widgets
  736. * @global array $wp_registered_sidebars
  737. *
  738. * @return bool True, if using widgets. False, if not using widgets.
  739. */
  740. function is_dynamic_sidebar() {
  741. global $wp_registered_widgets, $wp_registered_sidebars;
  742. $sidebars_widgets = get_option('sidebars_widgets');
  743. foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
  744. if ( ! empty( $sidebars_widgets[ $index ] ) ) {
  745. foreach ( (array) $sidebars_widgets[$index] as $widget )
  746. if ( array_key_exists($widget, $wp_registered_widgets) )
  747. return true;
  748. }
  749. }
  750. return false;
  751. }
  752. /**
  753. * Whether a sidebar is in use.
  754. *
  755. * @since 2.8.0
  756. *
  757. * @param string|int $index Sidebar name, id or number to check.
  758. * @return bool true if the sidebar is in use, false otherwise.
  759. */
  760. function is_active_sidebar( $index ) {
  761. $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
  762. $sidebars_widgets = wp_get_sidebars_widgets();
  763. $is_active_sidebar = ! empty( $sidebars_widgets[$index] );
  764. /**
  765. * Filters whether a dynamic sidebar is considered "active".
  766. *
  767. * @since 3.9.0
  768. *
  769. * @param bool $is_active_sidebar Whether or not the sidebar should be considered "active".
  770. * In other words, whether the sidebar contains any widgets.
  771. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  772. */
  773. return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index );
  774. }
  775. //
  776. // Internal Functions
  777. //
  778. /**
  779. * Retrieve full list of sidebars and their widget instance IDs.
  780. *
  781. * Will upgrade sidebar widget list, if needed. Will also save updated list, if
  782. * needed.
  783. *
  784. * @since 2.2.0
  785. * @access private
  786. *
  787. * @global array $_wp_sidebars_widgets
  788. * @global array $sidebars_widgets
  789. *
  790. * @param bool $deprecated Not used (argument deprecated).
  791. * @return array Upgraded list of widgets to version 3 array format when called from the admin.
  792. */
  793. function wp_get_sidebars_widgets( $deprecated = true ) {
  794. if ( $deprecated !== true )
  795. _deprecated_argument( __FUNCTION__, '2.8.1' );
  796. global $_wp_sidebars_widgets, $sidebars_widgets;
  797. // If loading from front page, consult $_wp_sidebars_widgets rather than options
  798. // to see if wp_convert_widget_settings() has made manipulations in memory.
  799. if ( !is_admin() ) {
  800. if ( empty($_wp_sidebars_widgets) )
  801. $_wp_sidebars_widgets = get_option('sidebars_widgets', array());
  802. $sidebars_widgets = $_wp_sidebars_widgets;
  803. } else {
  804. $sidebars_widgets = get_option('sidebars_widgets', array());
  805. }
  806. if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) )
  807. unset($sidebars_widgets['array_version']);
  808. /**
  809. * Filters the list of sidebars and their widgets.
  810. *
  811. * @since 2.7.0
  812. *
  813. * @param array $sidebars_widgets An associative array of sidebars and their widgets.
  814. */
  815. return apply_filters( 'sidebars_widgets', $sidebars_widgets );
  816. }
  817. /**
  818. * Set the sidebar widget option to update sidebars.
  819. *
  820. * @since 2.2.0
  821. * @access private
  822. *
  823. * @param array $sidebars_widgets Sidebar widgets and their settings.
  824. */
  825. function wp_set_sidebars_widgets( $sidebars_widgets ) {
  826. if ( !isset( $sidebars_widgets['array_version'] ) )
  827. $sidebars_widgets['array_version'] = 3;
  828. update_option( 'sidebars_widgets', $sidebars_widgets );
  829. }
  830. /**
  831. * Retrieve default registered sidebars list.
  832. *
  833. * @since 2.2.0
  834. * @access private
  835. *
  836. * @global array $wp_registered_sidebars
  837. *
  838. * @return array
  839. */
  840. function wp_get_widget_defaults() {
  841. global $wp_registered_sidebars;
  842. $defaults = array();
  843. foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
  844. $defaults[$index] = array();
  845. return $defaults;
  846. }
  847. /**
  848. * Convert the widget settings from single to multi-widget format.
  849. *
  850. * @since 2.8.0
  851. *
  852. * @global array $_wp_sidebars_widgets
  853. *
  854. * @param string $base_name
  855. * @param string $option_name
  856. * @param array $settings
  857. * @return array
  858. */
  859. function wp_convert_widget_settings($base_name, $option_name, $settings) {
  860. // This test may need expanding.
  861. $single = $changed = false;
  862. if ( empty($settings) ) {
  863. $single = true;
  864. } else {
  865. foreach ( array_keys($settings) as $number ) {
  866. if ( 'number' == $number )
  867. continue;
  868. if ( !is_numeric($number) ) {
  869. $single = true;
  870. break;
  871. }
  872. }
  873. }
  874. if ( $single ) {
  875. $settings = array( 2 => $settings );
  876. // If loading from the front page, update sidebar in memory but don't save to options
  877. if ( is_admin() ) {
  878. $sidebars_widgets = get_option('sidebars_widgets');
  879. } else {
  880. if ( empty($GLOBALS['_wp_sidebars_widgets']) )
  881. $GLOBALS['_wp_sidebars_widgets'] = get_option('sidebars_widgets', array());
  882. $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
  883. }
  884. foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
  885. if ( is_array($sidebar) ) {
  886. foreach ( $sidebar as $i => $name ) {
  887. if ( $base_name == $name ) {
  888. $sidebars_widgets[$index][$i] = "$name-2";
  889. $changed = true;
  890. break 2;
  891. }
  892. }
  893. }
  894. }
  895. if ( is_admin() && $changed )
  896. update_option('sidebars_widgets', $sidebars_widgets);
  897. }
  898. $settings['_multiwidget'] = 1;
  899. if ( is_admin() )
  900. update_option( $option_name, $settings );
  901. return $settings;
  902. }
  903. /**
  904. * Output an arbitrary widget as a template tag.
  905. *
  906. * @since 2.8.0
  907. *
  908. * @global WP_Widget_Factory $wp_widget_factory
  909. *
  910. * @param string $widget The widget's PHP class name (see class-wp-widget.php).
  911. * @param array $instance Optional. The widget's instance settings. Default empty array.
  912. * @param array $args {
  913. * Optional. Array of arguments to configure the display of the widget.
  914. *
  915. * @type string $before_widget HTML content that will be prepended to the widget's HTML output.
  916. * Default `<div class="widget %s">`, where `%s` is the widget's class name.
  917. * @type string $after_widget HTML content that will be appended to the widget's HTML output.
  918. * Default `</div>`.
  919. * @type string $before_title HTML content that will be prepended to the widget's title when displayed.
  920. * Default `<h2 class="widgettitle">`.
  921. * @type string $after_title HTML content that will be appended to the widget's title when displayed.
  922. * Default `</h2>`.
  923. * }
  924. */
  925. function the_widget( $widget, $instance = array(), $args = array() ) {
  926. global $wp_widget_factory;
  927. $widget_obj = $wp_widget_factory->widgets[$widget];
  928. if ( ! ( $widget_obj instanceof WP_Widget ) ) {
  929. return;
  930. }
  931. $default_args = array(
  932. 'before_widget' => '<div class="widget %s">',
  933. 'after_widget' => "</div>",
  934. 'before_title' => '<h2 class="widgettitle">',
  935. 'after_title' => '</h2>',
  936. );
  937. $args = wp_parse_args( $args, $default_args );
  938. $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] );
  939. $instance = wp_parse_args($instance);
  940. /**
  941. * Fires before rendering the requested widget.
  942. *
  943. * @since 3.0.0
  944. *
  945. * @param string $widget The widget's class name.
  946. * @param array $instance The current widget instance's settings.
  947. * @param array $args An array of the widget's sidebar arguments.
  948. */
  949. do_action( 'the_widget', $widget, $instance, $args );
  950. $widget_obj->_set(-1);
  951. $widget_obj->widget($args, $instance);
  952. }
  953. /**
  954. * Retrieves the widget ID base value.
  955. *
  956. * @since 2.8.0
  957. *
  958. * @param string $id Widget ID.
  959. * @return string Widget ID base.
  960. */
  961. function _get_widget_id_base( $id ) {
  962. return preg_replace( '/-[0-9]+$/', '', $id );
  963. }
  964. /**
  965. * Handle sidebars config after theme change
  966. *
  967. * @access private
  968. * @since 3.3.0
  969. *
  970. * @global array $sidebars_widgets
  971. */
  972. function _wp_sidebars_changed() {
  973. global $sidebars_widgets;
  974. if ( ! is_array( $sidebars_widgets ) )
  975. $sidebars_widgets = wp_get_sidebars_widgets();
  976. retrieve_widgets(true);
  977. }
  978. /**
  979. * Look for "lost" widgets, this has to run at least on each theme change.
  980. *
  981. * @since 2.8.0
  982. *
  983. * @global array $wp_registered_sidebars
  984. * @global array $sidebars_widgets
  985. * @global array $wp_registered_widgets
  986. *
  987. * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value
  988. * of 'customize' defers updates for the Customizer.
  989. * @return array|void
  990. */
  991. function retrieve_widgets( $theme_changed = false ) {
  992. global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
  993. $registered_sidebar_keys = array_keys( $wp_registered_sidebars );
  994. $orphaned = 0;
  995. $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
  996. if ( is_array( $old_sidebars_widgets ) ) {
  997. // time() that sidebars were stored is in $old_sidebars_widgets['time']
  998. $_sidebars_widgets = $old_sidebars_widgets['data'];
  999. if ( 'customize' !== $theme_changed ) {
  1000. remove_theme_mod( 'sidebars_widgets' );
  1001. }
  1002. foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
  1003. if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
  1004. continue;
  1005. }
  1006. if ( !in_array( $sidebar, $registered_sidebar_keys ) ) {
  1007. $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $widgets;
  1008. unset( $_sidebars_widgets[$sidebar] );
  1009. }
  1010. }
  1011. } else {
  1012. if ( empty( $sidebars_widgets ) )
  1013. return;
  1014. unset( $sidebars_widgets['array_version'] );
  1015. $old = array_keys($sidebars_widgets);
  1016. sort($old);
  1017. sort($registered_sidebar_keys);
  1018. if ( $old == $registered_sidebar_keys )
  1019. return;
  1020. $_sidebars_widgets = array(
  1021. 'wp_inactive_widgets' => !empty( $sidebars_widgets['wp_inactive_widgets'] ) ? $sidebars_widgets['wp_inactive_widgets'] : array()
  1022. );
  1023. unset( $sidebars_widgets['wp_inactive_widgets'] );
  1024. foreach ( $wp_registered_sidebars as $id => $settings ) {
  1025. if ( $theme_changed ) {
  1026. $_sidebars_widgets[$id] = array_shift( $sidebars_widgets );
  1027. } else {
  1028. // no theme change, grab only sidebars that are currently registered
  1029. if ( isset( $sidebars_widgets[$id] ) ) {
  1030. $_sidebars_widgets[$id] = $sidebars_widgets[$id];
  1031. unset( $sidebars_widgets[$id] );
  1032. }
  1033. }
  1034. }
  1035. foreach ( $sidebars_widgets as $val ) {
  1036. if ( is_array($val) && ! empty( $val ) )
  1037. $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $val;
  1038. }
  1039. }
  1040. // discard invalid, theme-specific widgets from sidebars
  1041. $shown_widgets = array();
  1042. foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
  1043. if ( !is_array($widgets) )
  1044. continue;
  1045. $_widgets = array();
  1046. foreach ( $widgets as $widget ) {
  1047. if ( isset($wp_registered_widgets[$widget]) )
  1048. $_widgets[] = $widget;
  1049. }
  1050. $_sidebars_widgets[$sidebar] = $_widgets;
  1051. $shown_widgets = array_merge($shown_widgets, $_widgets);
  1052. }
  1053. $sidebars_widgets = $_sidebars_widgets;
  1054. unset($_sidebars_widgets, $_widgets);
  1055. // find hidden/lost multi-widget instances
  1056. $lost_widgets = array();
  1057. foreach ( $wp_registered_widgets as $key => $val ) {
  1058. if ( in_array($key, $shown_widgets, true) )
  1059. continue;
  1060. $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
  1061. if ( 2 > (int) $number )
  1062. continue;
  1063. $lost_widgets[] = $key;
  1064. }
  1065. $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
  1066. if ( 'customize' !== $theme_changed ) {
  1067. wp_set_sidebars_widgets( $sidebars_widgets );
  1068. }
  1069. return $sidebars_widgets;
  1070. }
  1071. /**
  1072. * Display the RSS entries in a list.
  1073. *
  1074. * @since 2.5.0
  1075. *
  1076. * @param string|array|object $rss RSS url.
  1077. * @param array $args Widget arguments.
  1078. */
  1079. function wp_widget_rss_output( $rss, $args = array() ) {
  1080. if ( is_string( $rss ) ) {
  1081. $rss = fetch_feed($rss);
  1082. } elseif ( is_array($rss) && isset($rss['url']) ) {
  1083. $args = $rss;
  1084. $rss = fetch_feed($rss['url']);
  1085. } elseif ( !is_object($rss) ) {
  1086. return;
  1087. }
  1088. if ( is_wp_error($rss) ) {
  1089. if ( is_admin() || current_user_can('manage_options') )
  1090. echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</p>';
  1091. return;
  1092. }
  1093. $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0, 'items' => 0 );
  1094. $args = wp_parse_args( $args, $default_args );
  1095. $items = (int) $args['items'];
  1096. if ( $items < 1 || 20 < $items )
  1097. $items = 10;
  1098. $show_summary = (int) $args['show_summary'];
  1099. $show_author = (int) $args['show_author'];
  1100. $show_date = (int) $args['show_date'];
  1101. if ( !$rss->get_item_quantity() ) {
  1102. echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
  1103. $rss->__destruct();
  1104. unset($rss);
  1105. return;
  1106. }
  1107. echo '<ul>';
  1108. foreach ( $rss->get_items( 0, $items ) as $item ) {
  1109. $link = $item->get_link();
  1110. while ( stristr( $link, 'http' ) != $link ) {
  1111. $link = substr( $link, 1 );
  1112. }
  1113. $link = esc_url( strip_tags( $link ) );
  1114. $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
  1115. if ( empty( $title ) ) {
  1116. $title = __( 'Untitled' );
  1117. }
  1118. $desc = @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
  1119. $desc = esc_attr( wp_trim_words( $desc, 55, ' [&hellip;]' ) );
  1120. $summary = '';
  1121. if ( $show_summary ) {
  1122. $summary = $desc;
  1123. // Change existing [...] to [&hellip;].
  1124. if ( '[...]' == substr( $summary, -5 ) ) {
  1125. $summary = substr( $summary, 0, -5 ) . '[&hellip;]';
  1126. }
  1127. $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
  1128. }
  1129. $date = '';
  1130. if ( $show_date ) {
  1131. $date = $item->get_date( 'U' );
  1132. if ( $date ) {
  1133. $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
  1134. }
  1135. }
  1136. $author = '';
  1137. if ( $show_author ) {
  1138. $author = $item->get_author();
  1139. if ( is_object($author) ) {
  1140. $author = $author->get_name();
  1141. $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
  1142. }
  1143. }
  1144. if ( $link == '' ) {
  1145. echo "<li>$title{$date}{$summary}{$author}</li>";
  1146. } elseif ( $show_summary ) {
  1147. echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
  1148. } else {
  1149. echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
  1150. }
  1151. }
  1152. echo '</ul>';
  1153. $rss->__destruct();
  1154. unset($rss);
  1155. }
  1156. /**
  1157. * Display RSS widget options form.
  1158. *
  1159. * The options for what fields are displayed for the RSS form are all booleans
  1160. * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
  1161. * 'show_date'.
  1162. *
  1163. * @since 2.5.0
  1164. *
  1165. * @param array|string $args Values for input fields.
  1166. * @param array $inputs Override default display options.
  1167. */
  1168. function wp_widget_rss_form( $args, $inputs = null ) {
  1169. $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
  1170. $inputs = wp_parse_args( $inputs, $default_inputs );
  1171. $args['title'] = isset( $args['title'] ) ? $args['title'] : '';
  1172. $args['url'] = isset( $args['url'] ) ? $args['url'] : '';
  1173. $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
  1174. if ( $args['items'] < 1 || 20 < $args['items'] ) {
  1175. $args['items'] = 10;
  1176. }
  1177. $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
  1178. $args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
  1179. $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];
  1180. if ( ! empty( $args['error'] ) ) {
  1181. echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $args['error'] . '</p>';
  1182. }
  1183. $esc_number = esc_attr( $args['number'] );
  1184. if ( $inputs['url'] ) :
  1185. ?>
  1186. <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
  1187. <input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p>
  1188. <?php endif; if ( $inputs['title'] ) : ?>
  1189. <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
  1190. <input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p>
  1191. <?php endif; if ( $inputs['items'] ) : ?>
  1192. <p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
  1193. <select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
  1194. <?php
  1195. for ( $i = 1; $i <= 20; ++$i ) {
  1196. echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
  1197. }
  1198. ?>
  1199. </select></p>
  1200. <?php endif; if ( $inputs['show_summary'] ) : ?>
  1201. <p><input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> />
  1202. <label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label></p>
  1203. <?php endif; if ( $inputs['show_author'] ) : ?>
  1204. <p><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> />
  1205. <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label></p>
  1206. <?php endif; if ( $inputs['show_date'] ) : ?>
  1207. <p><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/>
  1208. <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label></p>
  1209. <?php
  1210. endif;
  1211. foreach ( array_keys($default_inputs) as $input ) :
  1212. if ( 'hidden' === $inputs[$input] ) :
  1213. $id = str_replace( '_', '-', $input );
  1214. ?>
  1215. <input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" />
  1216. <?php
  1217. endif;
  1218. endforeach;
  1219. }
  1220. /**
  1221. * Process RSS feed widget data and optionally retrieve feed items.
  1222. *
  1223. * The feed widget can not have more than 20 items or it will reset back to the
  1224. * default, which is 10.
  1225. *
  1226. * The resulting array has the feed title, feed url, feed link (from channel),
  1227. * feed items, error (if any), and whether to show summary, author, and date.
  1228. * All respectively in the order of the array elements.
  1229. *
  1230. * @since 2.5.0
  1231. *
  1232. * @param array $widget_rss RSS widget feed data. Expects unescaped data.
  1233. * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
  1234. * @return array
  1235. */
  1236. function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
  1237. $items = (int) $widget_rss['items'];
  1238. if ( $items < 1 || 20 < $items )
  1239. $items = 10;
  1240. $url = esc_url_raw( strip_tags( $widget_rss['url'] ) );
  1241. $title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
  1242. $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
  1243. $show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] :0;
  1244. $show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
  1245. if ( $check_feed ) {
  1246. $rss = fetch_feed($url);
  1247. $error = false;
  1248. $link = '';
  1249. if ( is_wp_error($rss) ) {
  1250. $error = $rss->get_error_message();
  1251. } else {
  1252. $link = esc_url(strip_tags($rss->get_permalink()));
  1253. while ( stristr($link, 'http') != $link )
  1254. $link = substr($link, 1);
  1255. $rss->__destruct();
  1256. unset($rss);
  1257. }
  1258. }
  1259. return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
  1260. }
  1261. /**
  1262. * Registers all of the default WordPress widgets on startup.
  1263. *
  1264. * Calls {@see 'widgets_init'} action after all of the WordPress widgets have been registered.
  1265. *
  1266. * @since 2.2.0
  1267. */
  1268. function wp_widgets_init() {
  1269. if ( !is_blog_installed() )
  1270. return;
  1271. register_widget('WP_Widget_Pages');
  1272. register_widget('WP_Widget_Calendar');
  1273. register_widget('WP_Widget_Archives');
  1274. if ( get_option( 'link_manager_enabled' ) )
  1275. register_widget('WP_Widget_Links');
  1276. register_widget('WP_Widget_Meta');
  1277. register_widget('WP_Widget_Search');
  1278. register_widget('WP_Widget_Text');
  1279. register_widget('WP_Widget_Categories');
  1280. register_widget('WP_Widget_Recent_Posts');
  1281. register_widget('WP_Widget_Recent_Comments');
  1282. register_widget('WP_Widget_RSS');
  1283. register_widget('WP_Widget_Tag_Cloud');
  1284. register_widget('WP_Nav_Menu_Widget');
  1285. /**
  1286. * Fires after all default WordPress widgets have been registered.
  1287. *
  1288. * @since 2.2.0
  1289. */
  1290. do_action( 'widgets_init' );
  1291. }