widgets.php 54 KB

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