widgets.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  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://wordpress.org/support/article/wordpress-widgets/
  14. * @link https://developer.wordpress.org/themes/functionality/widgets/
  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 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 controls (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 The new sidebars are stored in this array by sidebar ID.
  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. }
  159. for ( $i = 1; $i <= $number; $i++ ) {
  160. $_args = $args;
  161. if ( $number > 1 ) {
  162. if ( isset( $args['name'] ) ) {
  163. $_args['name'] = sprintf( $args['name'], $i );
  164. } else {
  165. /* translators: %d: Sidebar number. */
  166. $_args['name'] = sprintf( __( 'Sidebar %d' ), $i );
  167. }
  168. } else {
  169. $_args['name'] = isset( $args['name'] ) ? $args['name'] : __( 'Sidebar' );
  170. }
  171. // Custom specified ID's are suffixed if they exist already.
  172. // Automatically generated sidebar names need to be suffixed regardless starting at -0.
  173. if ( isset( $args['id'] ) ) {
  174. $_args['id'] = $args['id'];
  175. $n = 2; // Start at -2 for conflicting custom IDs.
  176. while ( is_registered_sidebar( $_args['id'] ) ) {
  177. $_args['id'] = $args['id'] . '-' . $n++;
  178. }
  179. } else {
  180. $n = count( $wp_registered_sidebars );
  181. do {
  182. $_args['id'] = 'sidebar-' . ++$n;
  183. } while ( is_registered_sidebar( $_args['id'] ) );
  184. }
  185. register_sidebar( $_args );
  186. }
  187. }
  188. /**
  189. * Builds the definition for a single sidebar and returns the ID.
  190. *
  191. * Accepts either a string or an array and then parses that against a set
  192. * of default arguments for the new sidebar. WordPress will automatically
  193. * generate a sidebar ID and name based on the current number of registered
  194. * sidebars if those arguments are not included.
  195. *
  196. * When allowing for automatic generation of the name and ID parameters, keep
  197. * in mind that the incrementor for your sidebar can change over time depending
  198. * on what other plugins and themes are installed.
  199. *
  200. * If theme support for 'widgets' has not yet been added when this function is
  201. * called, it will be automatically enabled through the use of add_theme_support()
  202. *
  203. * @since 2.2.0
  204. * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments.
  205. * @since 5.9.0 Added the `show_in_rest` argument.
  206. *
  207. * @global array $wp_registered_sidebars Registered sidebars.
  208. *
  209. * @param array|string $args {
  210. * Optional. Array or string of arguments for the sidebar being registered.
  211. *
  212. * @type string $name The name or title of the sidebar displayed in the Widgets
  213. * interface. Default 'Sidebar $instance'.
  214. * @type string $id The unique identifier by which the sidebar will be called.
  215. * Default 'sidebar-$instance'.
  216. * @type string $description Description of the sidebar, displayed in the Widgets interface.
  217. * Default empty string.
  218. * @type string $class Extra CSS class to assign to the sidebar in the Widgets interface.
  219. * Default empty.
  220. * @type string $before_widget HTML content to prepend to each widget's HTML output when assigned
  221. * to this sidebar. Receives the widget's ID attribute as `%1$s`
  222. * and class name as `%2$s`. Default is an opening list item element.
  223. * @type string $after_widget HTML content to append to each widget's HTML output when assigned
  224. * to this sidebar. Default is a closing list item element.
  225. * @type string $before_title HTML content to prepend to the sidebar title when displayed.
  226. * Default is an opening h2 element.
  227. * @type string $after_title HTML content to append to the sidebar title when displayed.
  228. * Default is a closing h2 element.
  229. * @type string $before_sidebar HTML content to prepend to the sidebar when displayed.
  230. * Receives the `$id` argument as `%1$s` and `$class` as `%2$s`.
  231. * Outputs after the {@see 'dynamic_sidebar_before'} action.
  232. * Default empty string.
  233. * @type string $after_sidebar HTML content to append to the sidebar when displayed.
  234. * Outputs before the {@see 'dynamic_sidebar_after'} action.
  235. * Default empty string.
  236. * @type bool $show_in_rest Whether to show this sidebar publicly in the REST API.
  237. * Defaults to only showing the sidebar to administrator users.
  238. * }
  239. * @return string Sidebar ID added to $wp_registered_sidebars global.
  240. */
  241. function register_sidebar( $args = array() ) {
  242. global $wp_registered_sidebars;
  243. $i = count( $wp_registered_sidebars ) + 1;
  244. $id_is_empty = empty( $args['id'] );
  245. $defaults = array(
  246. /* translators: %d: Sidebar number. */
  247. 'name' => sprintf( __( 'Sidebar %d' ), $i ),
  248. 'id' => "sidebar-$i",
  249. 'description' => '',
  250. 'class' => '',
  251. 'before_widget' => '<li id="%1$s" class="widget %2$s">',
  252. 'after_widget' => "</li>\n",
  253. 'before_title' => '<h2 class="widgettitle">',
  254. 'after_title' => "</h2>\n",
  255. 'before_sidebar' => '',
  256. 'after_sidebar' => '',
  257. 'show_in_rest' => false,
  258. );
  259. /**
  260. * Filters the sidebar default arguments.
  261. *
  262. * @since 5.3.0
  263. *
  264. * @see register_sidebar()
  265. *
  266. * @param array $defaults The default sidebar arguments.
  267. */
  268. $sidebar = wp_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) );
  269. if ( $id_is_empty ) {
  270. _doing_it_wrong(
  271. __FUNCTION__,
  272. sprintf(
  273. /* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */
  274. __( '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.' ),
  275. '<code>id</code>',
  276. $sidebar['name'],
  277. $sidebar['id']
  278. ),
  279. '4.2.0'
  280. );
  281. }
  282. $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
  283. add_theme_support( 'widgets' );
  284. /**
  285. * Fires once a sidebar has been registered.
  286. *
  287. * @since 3.0.0
  288. *
  289. * @param array $sidebar Parsed arguments for the registered sidebar.
  290. */
  291. do_action( 'register_sidebar', $sidebar );
  292. return $sidebar['id'];
  293. }
  294. /**
  295. * Removes a sidebar from the list.
  296. *
  297. * @since 2.2.0
  298. *
  299. * @global array $wp_registered_sidebars Registered sidebars.
  300. *
  301. * @param string|int $sidebar_id The ID of the sidebar when it was registered.
  302. */
  303. function unregister_sidebar( $sidebar_id ) {
  304. global $wp_registered_sidebars;
  305. unset( $wp_registered_sidebars[ $sidebar_id ] );
  306. }
  307. /**
  308. * Checks if a sidebar is registered.
  309. *
  310. * @since 4.4.0
  311. *
  312. * @global array $wp_registered_sidebars Registered sidebars.
  313. *
  314. * @param string|int $sidebar_id The ID of the sidebar when it was registered.
  315. * @return bool True if the sidebar is registered, false otherwise.
  316. */
  317. function is_registered_sidebar( $sidebar_id ) {
  318. global $wp_registered_sidebars;
  319. return isset( $wp_registered_sidebars[ $sidebar_id ] );
  320. }
  321. /**
  322. * Register an instance of a widget.
  323. *
  324. * The default widget option is 'classname' that can be overridden.
  325. *
  326. * The function can also be used to un-register widgets when `$output_callback`
  327. * parameter is an empty string.
  328. *
  329. * @since 2.2.0
  330. * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
  331. * by adding it to the function signature.
  332. * @since 5.8.0 Added show_instance_in_rest option.
  333. *
  334. * @global array $wp_registered_widgets Uses stored registered widgets.
  335. * @global array $wp_registered_widget_controls Stores the registered widget controls (options).
  336. * @global array $wp_registered_widget_updates
  337. * @global array $_wp_deprecated_widgets_callbacks
  338. *
  339. * @param int|string $id Widget ID.
  340. * @param string $name Widget display title.
  341. * @param callable $output_callback Run when widget is called.
  342. * @param array $options {
  343. * Optional. An array of supplementary widget options for the instance.
  344. *
  345. * @type string $classname Class name for the widget's HTML container. Default is a shortened
  346. * version of the output callback name.
  347. * @type string $description Widget description for display in the widget administration
  348. * panel and/or theme.
  349. * @type bool $show_instance_in_rest Whether to show the widget's instance settings in the REST API.
  350. * Only available for WP_Widget based widgets.
  351. * }
  352. * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
  353. */
  354. function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array(), ...$params ) {
  355. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
  356. $id = strtolower( $id );
  357. if ( empty( $output_callback ) ) {
  358. unset( $wp_registered_widgets[ $id ] );
  359. return;
  360. }
  361. $id_base = _get_widget_id_base( $id );
  362. if ( in_array( $output_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $output_callback ) ) {
  363. unset( $wp_registered_widget_controls[ $id ] );
  364. unset( $wp_registered_widget_updates[ $id_base ] );
  365. return;
  366. }
  367. $defaults = array( 'classname' => $output_callback );
  368. $options = wp_parse_args( $options, $defaults );
  369. $widget = array(
  370. 'name' => $name,
  371. 'id' => $id,
  372. 'callback' => $output_callback,
  373. 'params' => $params,
  374. );
  375. $widget = array_merge( $widget, $options );
  376. if ( is_callable( $output_callback ) && ( ! isset( $wp_registered_widgets[ $id ] ) || did_action( 'widgets_init' ) ) ) {
  377. /**
  378. * Fires once for each registered widget.
  379. *
  380. * @since 3.0.0
  381. *
  382. * @param array $widget An array of default widget arguments.
  383. */
  384. do_action( 'wp_register_sidebar_widget', $widget );
  385. $wp_registered_widgets[ $id ] = $widget;
  386. }
  387. }
  388. /**
  389. * Retrieve description for widget.
  390. *
  391. * When registering widgets, the options can also include 'description' that
  392. * describes the widget for display on the widget administration panel or
  393. * in the theme.
  394. *
  395. * @since 2.5.0
  396. *
  397. * @global array $wp_registered_widgets
  398. *
  399. * @param int|string $id Widget ID.
  400. * @return string|void Widget description, if available.
  401. */
  402. function wp_widget_description( $id ) {
  403. if ( ! is_scalar( $id ) ) {
  404. return;
  405. }
  406. global $wp_registered_widgets;
  407. if ( isset( $wp_registered_widgets[ $id ]['description'] ) ) {
  408. return esc_html( $wp_registered_widgets[ $id ]['description'] );
  409. }
  410. }
  411. /**
  412. * Retrieve description for a sidebar.
  413. *
  414. * When registering sidebars a 'description' parameter can be included that
  415. * describes the sidebar for display on the widget administration panel.
  416. *
  417. * @since 2.9.0
  418. *
  419. * @global array $wp_registered_sidebars Registered sidebars.
  420. *
  421. * @param string $id sidebar ID.
  422. * @return string|void Sidebar description, if available.
  423. */
  424. function wp_sidebar_description( $id ) {
  425. if ( ! is_scalar( $id ) ) {
  426. return;
  427. }
  428. global $wp_registered_sidebars;
  429. if ( isset( $wp_registered_sidebars[ $id ]['description'] ) ) {
  430. return wp_kses( $wp_registered_sidebars[ $id ]['description'], 'sidebar_description' );
  431. }
  432. }
  433. /**
  434. * Remove widget from sidebar.
  435. *
  436. * @since 2.2.0
  437. *
  438. * @param int|string $id Widget ID.
  439. */
  440. function wp_unregister_sidebar_widget( $id ) {
  441. /**
  442. * Fires just before a widget is removed from a sidebar.
  443. *
  444. * @since 3.0.0
  445. *
  446. * @param int|string $id The widget ID.
  447. */
  448. do_action( 'wp_unregister_sidebar_widget', $id );
  449. wp_register_sidebar_widget( $id, '', '' );
  450. wp_unregister_widget_control( $id );
  451. }
  452. /**
  453. * Registers widget control callback for customizing options.
  454. *
  455. * @since 2.2.0
  456. * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
  457. * by adding it to the function signature.
  458. *
  459. * @global array $wp_registered_widget_controls
  460. * @global array $wp_registered_widget_updates
  461. * @global array $wp_registered_widgets
  462. * @global array $_wp_deprecated_widgets_callbacks
  463. *
  464. * @param int|string $id Sidebar ID.
  465. * @param string $name Sidebar display name.
  466. * @param callable $control_callback Run when sidebar is displayed.
  467. * @param array $options {
  468. * Optional. Array or string of control options. Default empty array.
  469. *
  470. * @type int $height Never used. Default 200.
  471. * @type int $width Width of the fully expanded control form (but try hard to use the default width).
  472. * Default 250.
  473. * @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the
  474. * text widget. The widget ID will end up looking like `{$id_base}-{$unique_number}`.
  475. * }
  476. * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
  477. */
  478. function wp_register_widget_control( $id, $name, $control_callback, $options = array(), ...$params ) {
  479. global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
  480. $id = strtolower( $id );
  481. $id_base = _get_widget_id_base( $id );
  482. if ( empty( $control_callback ) ) {
  483. unset( $wp_registered_widget_controls[ $id ] );
  484. unset( $wp_registered_widget_updates[ $id_base ] );
  485. return;
  486. }
  487. if ( in_array( $control_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $control_callback ) ) {
  488. unset( $wp_registered_widgets[ $id ] );
  489. return;
  490. }
  491. if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) {
  492. return;
  493. }
  494. $defaults = array(
  495. 'width' => 250,
  496. 'height' => 200,
  497. ); // Height is never used.
  498. $options = wp_parse_args( $options, $defaults );
  499. $options['width'] = (int) $options['width'];
  500. $options['height'] = (int) $options['height'];
  501. $widget = array(
  502. 'name' => $name,
  503. 'id' => $id,
  504. 'callback' => $control_callback,
  505. 'params' => $params,
  506. );
  507. $widget = array_merge( $widget, $options );
  508. $wp_registered_widget_controls[ $id ] = $widget;
  509. if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) {
  510. return;
  511. }
  512. if ( isset( $widget['params'][0]['number'] ) ) {
  513. $widget['params'][0]['number'] = -1;
  514. }
  515. unset( $widget['width'], $widget['height'], $widget['name'], $widget['id'] );
  516. $wp_registered_widget_updates[ $id_base ] = $widget;
  517. }
  518. /**
  519. * Registers the update callback for a widget.
  520. *
  521. * @since 2.8.0
  522. * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
  523. * by adding it to the function signature.
  524. *
  525. * @global array $wp_registered_widget_updates
  526. *
  527. * @param string $id_base The base ID of a widget created by extending WP_Widget.
  528. * @param callable $update_callback Update callback method for the widget.
  529. * @param array $options Optional. Widget control options. See wp_register_widget_control().
  530. * Default empty array.
  531. * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
  532. */
  533. function _register_widget_update_callback( $id_base, $update_callback, $options = array(), ...$params ) {
  534. global $wp_registered_widget_updates;
  535. if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) {
  536. if ( empty( $update_callback ) ) {
  537. unset( $wp_registered_widget_updates[ $id_base ] );
  538. }
  539. return;
  540. }
  541. $widget = array(
  542. 'callback' => $update_callback,
  543. 'params' => $params,
  544. );
  545. $widget = array_merge( $widget, $options );
  546. $wp_registered_widget_updates[ $id_base ] = $widget;
  547. }
  548. /**
  549. * Registers the form callback for a widget.
  550. *
  551. * @since 2.8.0
  552. * @since 5.3.0 Formalized the existing and already documented `...$params` parameter
  553. * by adding it to the function signature.
  554. *
  555. * @global array $wp_registered_widget_controls
  556. *
  557. * @param int|string $id Widget ID.
  558. * @param string $name Name attribute for the widget.
  559. * @param callable $form_callback Form callback.
  560. * @param array $options Optional. Widget control options. See wp_register_widget_control().
  561. * Default empty array.
  562. * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called.
  563. */
  564. function _register_widget_form_callback( $id, $name, $form_callback, $options = array(), ...$params ) {
  565. global $wp_registered_widget_controls;
  566. $id = strtolower( $id );
  567. if ( empty( $form_callback ) ) {
  568. unset( $wp_registered_widget_controls[ $id ] );
  569. return;
  570. }
  571. if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) {
  572. return;
  573. }
  574. $defaults = array(
  575. 'width' => 250,
  576. 'height' => 200,
  577. );
  578. $options = wp_parse_args( $options, $defaults );
  579. $options['width'] = (int) $options['width'];
  580. $options['height'] = (int) $options['height'];
  581. $widget = array(
  582. 'name' => $name,
  583. 'id' => $id,
  584. 'callback' => $form_callback,
  585. 'params' => $params,
  586. );
  587. $widget = array_merge( $widget, $options );
  588. $wp_registered_widget_controls[ $id ] = $widget;
  589. }
  590. /**
  591. * Remove control callback for widget.
  592. *
  593. * @since 2.2.0
  594. *
  595. * @param int|string $id Widget ID.
  596. */
  597. function wp_unregister_widget_control( $id ) {
  598. wp_register_widget_control( $id, '', '' );
  599. }
  600. /**
  601. * Display dynamic sidebar.
  602. *
  603. * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or
  604. * 'name' parameter for its registered sidebars you can pass an ID or name as the $index parameter.
  605. * Otherwise, you can pass in a numerical index to display the sidebar at that index.
  606. *
  607. * @since 2.2.0
  608. *
  609. * @global array $wp_registered_sidebars Registered sidebars.
  610. * @global array $wp_registered_widgets Registered widgets.
  611. *
  612. * @param int|string $index Optional. Index, name or ID of dynamic sidebar. Default 1.
  613. * @return bool True, if widget sidebar was found and called. False if not found or not called.
  614. */
  615. function dynamic_sidebar( $index = 1 ) {
  616. global $wp_registered_sidebars, $wp_registered_widgets;
  617. if ( is_int( $index ) ) {
  618. $index = "sidebar-$index";
  619. } else {
  620. $index = sanitize_title( $index );
  621. foreach ( (array) $wp_registered_sidebars as $key => $value ) {
  622. if ( sanitize_title( $value['name'] ) === $index ) {
  623. $index = $key;
  624. break;
  625. }
  626. }
  627. }
  628. $sidebars_widgets = wp_get_sidebars_widgets();
  629. if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {
  630. /** This action is documented in wp-includes/widget.php */
  631. do_action( 'dynamic_sidebar_before', $index, false );
  632. /** This action is documented in wp-includes/widget.php */
  633. do_action( 'dynamic_sidebar_after', $index, false );
  634. /** This filter is documented in wp-includes/widget.php */
  635. return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );
  636. }
  637. $sidebar = $wp_registered_sidebars[ $index ];
  638. $sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] );
  639. /**
  640. * Fires before widgets are rendered in a dynamic sidebar.
  641. *
  642. * Note: The action also fires for empty sidebars, and on both the front end
  643. * and back end, including the Inactive Widgets sidebar on the Widgets screen.
  644. *
  645. * @since 3.9.0
  646. *
  647. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  648. * @param bool $has_widgets Whether the sidebar is populated with widgets.
  649. * Default true.
  650. */
  651. do_action( 'dynamic_sidebar_before', $index, true );
  652. if ( ! is_admin() && ! empty( $sidebar['before_sidebar'] ) ) {
  653. echo $sidebar['before_sidebar'];
  654. }
  655. $did_one = false;
  656. foreach ( (array) $sidebars_widgets[ $index ] as $id ) {
  657. if ( ! isset( $wp_registered_widgets[ $id ] ) ) {
  658. continue;
  659. }
  660. $params = array_merge(
  661. array(
  662. array_merge(
  663. $sidebar,
  664. array(
  665. 'widget_id' => $id,
  666. 'widget_name' => $wp_registered_widgets[ $id ]['name'],
  667. )
  668. ),
  669. ),
  670. (array) $wp_registered_widgets[ $id ]['params']
  671. );
  672. // Substitute HTML `id` and `class` attributes into `before_widget`.
  673. $classname_ = '';
  674. foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) {
  675. if ( is_string( $cn ) ) {
  676. $classname_ .= '_' . $cn;
  677. } elseif ( is_object( $cn ) ) {
  678. $classname_ .= '_' . get_class( $cn );
  679. }
  680. }
  681. $classname_ = ltrim( $classname_, '_' );
  682. $params[0]['before_widget'] = sprintf(
  683. $params[0]['before_widget'],
  684. str_replace( '\\', '_', $id ),
  685. $classname_
  686. );
  687. /**
  688. * Filters the parameters passed to a widget's display callback.
  689. *
  690. * Note: The filter is evaluated on both the front end and back end,
  691. * including for the Inactive Widgets sidebar on the Widgets screen.
  692. *
  693. * @since 2.5.0
  694. *
  695. * @see register_sidebar()
  696. *
  697. * @param array $params {
  698. * @type array $args {
  699. * An array of widget display arguments.
  700. *
  701. * @type string $name Name of the sidebar the widget is assigned to.
  702. * @type string $id ID of the sidebar the widget is assigned to.
  703. * @type string $description The sidebar description.
  704. * @type string $class CSS class applied to the sidebar container.
  705. * @type string $before_widget HTML markup to prepend to each widget in the sidebar.
  706. * @type string $after_widget HTML markup to append to each widget in the sidebar.
  707. * @type string $before_title HTML markup to prepend to the widget title when displayed.
  708. * @type string $after_title HTML markup to append to the widget title when displayed.
  709. * @type string $widget_id ID of the widget.
  710. * @type string $widget_name Name of the widget.
  711. * }
  712. * @type array $widget_args {
  713. * An array of multi-widget arguments.
  714. *
  715. * @type int $number Number increment used for multiples of the same widget.
  716. * }
  717. * }
  718. */
  719. $params = apply_filters( 'dynamic_sidebar_params', $params );
  720. $callback = $wp_registered_widgets[ $id ]['callback'];
  721. /**
  722. * Fires before a widget's display callback is called.
  723. *
  724. * Note: The action fires on both the front end and back end, including
  725. * for widgets in the Inactive Widgets sidebar on the Widgets screen.
  726. *
  727. * The action is not fired for empty sidebars.
  728. *
  729. * @since 3.0.0
  730. *
  731. * @param array $widget {
  732. * An associative array of widget arguments.
  733. *
  734. * @type string $name Name of the widget.
  735. * @type string $id Widget ID.
  736. * @type callable $callback When the hook is fired on the front end, `$callback` is an array
  737. * containing the widget object. Fired on the back end, `$callback`
  738. * is 'wp_widget_control', see `$_callback`.
  739. * @type array $params An associative array of multi-widget arguments.
  740. * @type string $classname CSS class applied to the widget container.
  741. * @type string $description The widget description.
  742. * @type array $_callback When the hook is fired on the back end, `$_callback` is populated
  743. * with an array containing the widget object, see `$callback`.
  744. * }
  745. */
  746. do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] );
  747. if ( is_callable( $callback ) ) {
  748. call_user_func_array( $callback, $params );
  749. $did_one = true;
  750. }
  751. }
  752. if ( ! is_admin() && ! empty( $sidebar['after_sidebar'] ) ) {
  753. echo $sidebar['after_sidebar'];
  754. }
  755. /**
  756. * Fires after widgets are rendered in a dynamic sidebar.
  757. *
  758. * Note: The action also fires for empty sidebars, and on both the front end
  759. * and back end, including the Inactive Widgets sidebar on the Widgets screen.
  760. *
  761. * @since 3.9.0
  762. *
  763. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  764. * @param bool $has_widgets Whether the sidebar is populated with widgets.
  765. * Default true.
  766. */
  767. do_action( 'dynamic_sidebar_after', $index, true );
  768. /**
  769. * Filters whether a sidebar has widgets.
  770. *
  771. * Note: The filter is also evaluated for empty sidebars, and on both the front end
  772. * and back end, including the Inactive Widgets sidebar on the Widgets screen.
  773. *
  774. * @since 3.9.0
  775. *
  776. * @param bool $did_one Whether at least one widget was rendered in the sidebar.
  777. * Default false.
  778. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  779. */
  780. return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index );
  781. }
  782. /**
  783. * Determines whether a given widget is displayed on the front end.
  784. *
  785. * Either $callback or $id_base can be used
  786. * $id_base is the first argument when extending WP_Widget class
  787. * Without the optional $widget_id parameter, returns the ID of the first sidebar
  788. * in which the first instance of the widget with the given callback or $id_base is found.
  789. * With the $widget_id parameter, returns the ID of the sidebar where
  790. * the widget with that callback/$id_base AND that ID is found.
  791. *
  792. * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
  793. * this function has to run after widgets have initialized, at action {@see 'init'} or later.
  794. *
  795. * For more information on this and similar theme functions, check out
  796. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  797. * Conditional Tags} article in the Theme Developer Handbook.
  798. *
  799. * @since 2.2.0
  800. *
  801. * @global array $wp_registered_widgets
  802. *
  803. * @param callable|false $callback Optional. Widget callback to check. Default false.
  804. * @param string|false $widget_id Optional. Widget ID. Optional, but needed for checking.
  805. * Default false.
  806. * @param string|false $id_base Optional. The base ID of a widget created by extending WP_Widget.
  807. * Default false.
  808. * @param bool $skip_inactive Optional. Whether to check in 'wp_inactive_widgets'.
  809. * Default true.
  810. * @return string|false ID of the sidebar in which the widget is active,
  811. * false if the widget is not active.
  812. */
  813. function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) {
  814. global $wp_registered_widgets;
  815. $sidebars_widgets = wp_get_sidebars_widgets();
  816. if ( is_array( $sidebars_widgets ) ) {
  817. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  818. if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) {
  819. continue;
  820. }
  821. if ( is_array( $widgets ) ) {
  822. foreach ( $widgets as $widget ) {
  823. if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] === $callback ) || ( $id_base && _get_widget_id_base( $widget ) === $id_base ) ) {
  824. if ( ! $widget_id || $widget_id === $wp_registered_widgets[ $widget ]['id'] ) {
  825. return $sidebar;
  826. }
  827. }
  828. }
  829. }
  830. }
  831. }
  832. return false;
  833. }
  834. /**
  835. * Determines whether the dynamic sidebar is enabled and used by the theme.
  836. *
  837. * For more information on this and similar theme functions, check out
  838. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  839. * Conditional Tags} article in the Theme Developer Handbook.
  840. *
  841. * @since 2.2.0
  842. *
  843. * @global array $wp_registered_widgets Registered widgets.
  844. * @global array $wp_registered_sidebars Registered sidebars.
  845. *
  846. * @return bool True if using widgets, false otherwise.
  847. */
  848. function is_dynamic_sidebar() {
  849. global $wp_registered_widgets, $wp_registered_sidebars;
  850. $sidebars_widgets = get_option( 'sidebars_widgets' );
  851. foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
  852. if ( ! empty( $sidebars_widgets[ $index ] ) ) {
  853. foreach ( (array) $sidebars_widgets[ $index ] as $widget ) {
  854. if ( array_key_exists( $widget, $wp_registered_widgets ) ) {
  855. return true;
  856. }
  857. }
  858. }
  859. }
  860. return false;
  861. }
  862. /**
  863. * Determines whether a sidebar contains widgets.
  864. *
  865. * For more information on this and similar theme functions, check out
  866. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  867. * Conditional Tags} article in the Theme Developer Handbook.
  868. *
  869. * @since 2.8.0
  870. *
  871. * @param string|int $index Sidebar name, id or number to check.
  872. * @return bool True if the sidebar has widgets, false otherwise.
  873. */
  874. function is_active_sidebar( $index ) {
  875. $index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index );
  876. $sidebars_widgets = wp_get_sidebars_widgets();
  877. $is_active_sidebar = ! empty( $sidebars_widgets[ $index ] );
  878. /**
  879. * Filters whether a dynamic sidebar is considered "active".
  880. *
  881. * @since 3.9.0
  882. *
  883. * @param bool $is_active_sidebar Whether or not the sidebar should be considered "active".
  884. * In other words, whether the sidebar contains any widgets.
  885. * @param int|string $index Index, name, or ID of the dynamic sidebar.
  886. */
  887. return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index );
  888. }
  889. //
  890. // Internal Functions.
  891. //
  892. /**
  893. * Retrieve full list of sidebars and their widget instance IDs.
  894. *
  895. * Will upgrade sidebar widget list, if needed. Will also save updated list, if
  896. * needed.
  897. *
  898. * @since 2.2.0
  899. * @access private
  900. *
  901. * @global array $_wp_sidebars_widgets
  902. * @global array $sidebars_widgets
  903. *
  904. * @param bool $deprecated Not used (argument deprecated).
  905. * @return array Upgraded list of widgets to version 3 array format when called from the admin.
  906. */
  907. function wp_get_sidebars_widgets( $deprecated = true ) {
  908. if ( true !== $deprecated ) {
  909. _deprecated_argument( __FUNCTION__, '2.8.1' );
  910. }
  911. global $_wp_sidebars_widgets, $sidebars_widgets;
  912. // If loading from front page, consult $_wp_sidebars_widgets rather than options
  913. // to see if wp_convert_widget_settings() has made manipulations in memory.
  914. if ( ! is_admin() ) {
  915. if ( empty( $_wp_sidebars_widgets ) ) {
  916. $_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() );
  917. }
  918. $sidebars_widgets = $_wp_sidebars_widgets;
  919. } else {
  920. $sidebars_widgets = get_option( 'sidebars_widgets', array() );
  921. }
  922. if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) {
  923. unset( $sidebars_widgets['array_version'] );
  924. }
  925. /**
  926. * Filters the list of sidebars and their widgets.
  927. *
  928. * @since 2.7.0
  929. *
  930. * @param array $sidebars_widgets An associative array of sidebars and their widgets.
  931. */
  932. return apply_filters( 'sidebars_widgets', $sidebars_widgets );
  933. }
  934. /**
  935. * Retrieves the registered sidebar with the given ID.
  936. *
  937. * @since 5.9.0
  938. *
  939. * @global array $wp_registered_sidebars The registered sidebars.
  940. *
  941. * @param string $id The sidebar ID.
  942. * @return array|null The discovered sidebar, or null if it is not registered.
  943. */
  944. function wp_get_sidebar( $id ) {
  945. global $wp_registered_sidebars;
  946. foreach ( (array) $wp_registered_sidebars as $sidebar ) {
  947. if ( $sidebar['id'] === $id ) {
  948. return $sidebar;
  949. }
  950. }
  951. if ( 'wp_inactive_widgets' === $id ) {
  952. return array(
  953. 'id' => 'wp_inactive_widgets',
  954. 'name' => __( 'Inactive widgets' ),
  955. );
  956. }
  957. return null;
  958. }
  959. /**
  960. * Set the sidebar widget option to update sidebars.
  961. *
  962. * @since 2.2.0
  963. * @access private
  964. *
  965. * @global array $_wp_sidebars_widgets
  966. * @param array $sidebars_widgets Sidebar widgets and their settings.
  967. */
  968. function wp_set_sidebars_widgets( $sidebars_widgets ) {
  969. global $_wp_sidebars_widgets;
  970. // Clear cached value used in wp_get_sidebars_widgets().
  971. $_wp_sidebars_widgets = null;
  972. if ( ! isset( $sidebars_widgets['array_version'] ) ) {
  973. $sidebars_widgets['array_version'] = 3;
  974. }
  975. update_option( 'sidebars_widgets', $sidebars_widgets );
  976. }
  977. /**
  978. * Retrieve default registered sidebars list.
  979. *
  980. * @since 2.2.0
  981. * @access private
  982. *
  983. * @global array $wp_registered_sidebars Registered sidebars.
  984. *
  985. * @return array
  986. */
  987. function wp_get_widget_defaults() {
  988. global $wp_registered_sidebars;
  989. $defaults = array();
  990. foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
  991. $defaults[ $index ] = array();
  992. }
  993. return $defaults;
  994. }
  995. /**
  996. * Converts the widget settings from single to multi-widget format.
  997. *
  998. * @since 2.8.0
  999. *
  1000. * @global array $_wp_sidebars_widgets
  1001. *
  1002. * @param string $base_name Root ID for all widgets of this type.
  1003. * @param string $option_name Option name for this widget type.
  1004. * @param array $settings The array of widget instance settings.
  1005. * @return array The array of widget settings converted to multi-widget format.
  1006. */
  1007. function wp_convert_widget_settings( $base_name, $option_name, $settings ) {
  1008. // This test may need expanding.
  1009. $single = false;
  1010. $changed = false;
  1011. if ( empty( $settings ) ) {
  1012. $single = true;
  1013. } else {
  1014. foreach ( array_keys( $settings ) as $number ) {
  1015. if ( 'number' === $number ) {
  1016. continue;
  1017. }
  1018. if ( ! is_numeric( $number ) ) {
  1019. $single = true;
  1020. break;
  1021. }
  1022. }
  1023. }
  1024. if ( $single ) {
  1025. $settings = array( 2 => $settings );
  1026. // If loading from the front page, update sidebar in memory but don't save to options.
  1027. if ( is_admin() ) {
  1028. $sidebars_widgets = get_option( 'sidebars_widgets' );
  1029. } else {
  1030. if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) {
  1031. $GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() );
  1032. }
  1033. $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
  1034. }
  1035. foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
  1036. if ( is_array( $sidebar ) ) {
  1037. foreach ( $sidebar as $i => $name ) {
  1038. if ( $base_name === $name ) {
  1039. $sidebars_widgets[ $index ][ $i ] = "$name-2";
  1040. $changed = true;
  1041. break 2;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. if ( is_admin() && $changed ) {
  1047. update_option( 'sidebars_widgets', $sidebars_widgets );
  1048. }
  1049. }
  1050. $settings['_multiwidget'] = 1;
  1051. if ( is_admin() ) {
  1052. update_option( $option_name, $settings );
  1053. }
  1054. return $settings;
  1055. }
  1056. /**
  1057. * Output an arbitrary widget as a template tag.
  1058. *
  1059. * @since 2.8.0
  1060. *
  1061. * @global WP_Widget_Factory $wp_widget_factory
  1062. *
  1063. * @param string $widget The widget's PHP class name (see class-wp-widget.php).
  1064. * @param array $instance Optional. The widget's instance settings. Default empty array.
  1065. * @param array $args {
  1066. * Optional. Array of arguments to configure the display of the widget.
  1067. *
  1068. * @type string $before_widget HTML content that will be prepended to the widget's HTML output.
  1069. * Default `<div class="widget %s">`, where `%s` is the widget's class name.
  1070. * @type string $after_widget HTML content that will be appended to the widget's HTML output.
  1071. * Default `</div>`.
  1072. * @type string $before_title HTML content that will be prepended to the widget's title when displayed.
  1073. * Default `<h2 class="widgettitle">`.
  1074. * @type string $after_title HTML content that will be appended to the widget's title when displayed.
  1075. * Default `</h2>`.
  1076. * }
  1077. */
  1078. function the_widget( $widget, $instance = array(), $args = array() ) {
  1079. global $wp_widget_factory;
  1080. if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
  1081. _doing_it_wrong(
  1082. __FUNCTION__,
  1083. sprintf(
  1084. /* translators: %s: register_widget() */
  1085. __( 'Widgets need to be registered using %s, before they can be displayed.' ),
  1086. '<code>register_widget()</code>'
  1087. ),
  1088. '4.9.0'
  1089. );
  1090. return;
  1091. }
  1092. $widget_obj = $wp_widget_factory->widgets[ $widget ];
  1093. if ( ! ( $widget_obj instanceof WP_Widget ) ) {
  1094. return;
  1095. }
  1096. $default_args = array(
  1097. 'before_widget' => '<div class="widget %s">',
  1098. 'after_widget' => '</div>',
  1099. 'before_title' => '<h2 class="widgettitle">',
  1100. 'after_title' => '</h2>',
  1101. );
  1102. $args = wp_parse_args( $args, $default_args );
  1103. $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] );
  1104. $instance = wp_parse_args( $instance );
  1105. /** This filter is documented in wp-includes/class-wp-widget.php */
  1106. $instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args );
  1107. if ( false === $instance ) {
  1108. return;
  1109. }
  1110. /**
  1111. * Fires before rendering the requested widget.
  1112. *
  1113. * @since 3.0.0
  1114. *
  1115. * @param string $widget The widget's class name.
  1116. * @param array $instance The current widget instance's settings.
  1117. * @param array $args An array of the widget's sidebar arguments.
  1118. */
  1119. do_action( 'the_widget', $widget, $instance, $args );
  1120. $widget_obj->_set( -1 );
  1121. $widget_obj->widget( $args, $instance );
  1122. }
  1123. /**
  1124. * Retrieves the widget ID base value.
  1125. *
  1126. * @since 2.8.0
  1127. *
  1128. * @param string $id Widget ID.
  1129. * @return string Widget ID base.
  1130. */
  1131. function _get_widget_id_base( $id ) {
  1132. return preg_replace( '/-[0-9]+$/', '', $id );
  1133. }
  1134. /**
  1135. * Handle sidebars config after theme change
  1136. *
  1137. * @access private
  1138. * @since 3.3.0
  1139. *
  1140. * @global array $sidebars_widgets
  1141. */
  1142. function _wp_sidebars_changed() {
  1143. global $sidebars_widgets;
  1144. if ( ! is_array( $sidebars_widgets ) ) {
  1145. $sidebars_widgets = wp_get_sidebars_widgets();
  1146. }
  1147. retrieve_widgets( true );
  1148. }
  1149. /**
  1150. * Validates and remaps any "orphaned" widgets to wp_inactive_widgets sidebar,
  1151. * and saves the widget settings. This has to run at least on each theme change.
  1152. *
  1153. * For example, let's say theme A has a "footer" sidebar, and theme B doesn't have one.
  1154. * After switching from theme A to theme B, all the widgets previously assigned
  1155. * to the footer would be inaccessible. This function detects this scenario, and
  1156. * moves all the widgets previously assigned to the footer under wp_inactive_widgets.
  1157. *
  1158. * Despite the word "retrieve" in the name, this function actually updates the database
  1159. * and the global `$sidebars_widgets`. For that reason it should not be run on front end,
  1160. * unless the `$theme_changed` value is 'customize' (to bypass the database write).
  1161. *
  1162. * @since 2.8.0
  1163. *
  1164. * @global array $wp_registered_sidebars Registered sidebars.
  1165. * @global array $sidebars_widgets
  1166. * @global array $wp_registered_widgets Registered widgets.
  1167. *
  1168. * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value
  1169. * of 'customize' defers updates for the Customizer.
  1170. * @return array Updated sidebars widgets.
  1171. */
  1172. function retrieve_widgets( $theme_changed = false ) {
  1173. global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
  1174. $registered_sidebars_keys = array_keys( $wp_registered_sidebars );
  1175. $registered_widgets_ids = array_keys( $wp_registered_widgets );
  1176. if ( ! is_array( get_theme_mod( 'sidebars_widgets' ) ) ) {
  1177. if ( empty( $sidebars_widgets ) ) {
  1178. return array();
  1179. }
  1180. unset( $sidebars_widgets['array_version'] );
  1181. $sidebars_widgets_keys = array_keys( $sidebars_widgets );
  1182. sort( $sidebars_widgets_keys );
  1183. sort( $registered_sidebars_keys );
  1184. if ( $sidebars_widgets_keys === $registered_sidebars_keys ) {
  1185. $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
  1186. return $sidebars_widgets;
  1187. }
  1188. }
  1189. // Discard invalid, theme-specific widgets from sidebars.
  1190. $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
  1191. $sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets );
  1192. // Find hidden/lost multi-widget instances.
  1193. $shown_widgets = array_merge( ...array_values( $sidebars_widgets ) );
  1194. $lost_widgets = array_diff( $registered_widgets_ids, $shown_widgets );
  1195. foreach ( $lost_widgets as $key => $widget_id ) {
  1196. $number = preg_replace( '/.+?-([0-9]+)$/', '$1', $widget_id );
  1197. // Only keep active and default widgets.
  1198. if ( is_numeric( $number ) && (int) $number < 2 ) {
  1199. unset( $lost_widgets[ $key ] );
  1200. }
  1201. }
  1202. $sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] );
  1203. if ( 'customize' !== $theme_changed ) {
  1204. // Update the widgets settings in the database.
  1205. wp_set_sidebars_widgets( $sidebars_widgets );
  1206. }
  1207. return $sidebars_widgets;
  1208. }
  1209. /**
  1210. * Compares a list of sidebars with their widgets against an allowed list.
  1211. *
  1212. * @since 4.9.0
  1213. * @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping.
  1214. *
  1215. * @param array $existing_sidebars_widgets List of sidebars and their widget instance IDs.
  1216. * @return array Mapped sidebars widgets.
  1217. */
  1218. function wp_map_sidebars_widgets( $existing_sidebars_widgets ) {
  1219. global $wp_registered_sidebars;
  1220. $new_sidebars_widgets = array(
  1221. 'wp_inactive_widgets' => array(),
  1222. );
  1223. // Short-circuit if there are no sidebars to map.
  1224. if ( ! is_array( $existing_sidebars_widgets ) || empty( $existing_sidebars_widgets ) ) {
  1225. return $new_sidebars_widgets;
  1226. }
  1227. foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
  1228. if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
  1229. $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], (array) $widgets );
  1230. unset( $existing_sidebars_widgets[ $sidebar ] );
  1231. }
  1232. }
  1233. // If old and new theme have just one sidebar, map it and we're done.
  1234. if ( 1 === count( $existing_sidebars_widgets ) && 1 === count( $wp_registered_sidebars ) ) {
  1235. $new_sidebars_widgets[ key( $wp_registered_sidebars ) ] = array_pop( $existing_sidebars_widgets );
  1236. return $new_sidebars_widgets;
  1237. }
  1238. // Map locations with the same slug.
  1239. $existing_sidebars = array_keys( $existing_sidebars_widgets );
  1240. foreach ( $wp_registered_sidebars as $sidebar => $name ) {
  1241. if ( in_array( $sidebar, $existing_sidebars, true ) ) {
  1242. $new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ];
  1243. unset( $existing_sidebars_widgets[ $sidebar ] );
  1244. } elseif ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) {
  1245. $new_sidebars_widgets[ $sidebar ] = array();
  1246. }
  1247. }
  1248. // If there are more sidebars, try to map them.
  1249. if ( ! empty( $existing_sidebars_widgets ) ) {
  1250. /*
  1251. * If old and new theme both have sidebars that contain phrases
  1252. * from within the same group, make an educated guess and map it.
  1253. */
  1254. $common_slug_groups = array(
  1255. array( 'sidebar', 'primary', 'main', 'right' ),
  1256. array( 'second', 'left' ),
  1257. array( 'sidebar-2', 'footer', 'bottom' ),
  1258. array( 'header', 'top' ),
  1259. );
  1260. // Go through each group...
  1261. foreach ( $common_slug_groups as $slug_group ) {
  1262. // ...and see if any of these slugs...
  1263. foreach ( $slug_group as $slug ) {
  1264. // ...and any of the new sidebars...
  1265. foreach ( $wp_registered_sidebars as $new_sidebar => $args ) {
  1266. // ...actually match!
  1267. if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {
  1268. continue;
  1269. }
  1270. // Then see if any of the existing sidebars...
  1271. foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
  1272. // ...and any slug in the same group...
  1273. foreach ( $slug_group as $slug ) {
  1274. // ... have a match as well.
  1275. if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {
  1276. continue;
  1277. }
  1278. // Make sure this sidebar wasn't mapped and removed previously.
  1279. if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {
  1280. // We have a match that can be mapped!
  1281. $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] );
  1282. // Remove the mapped sidebar so it can't be mapped again.
  1283. unset( $existing_sidebars_widgets[ $sidebar ] );
  1284. // Go back and check the next new sidebar.
  1285. continue 3;
  1286. }
  1287. } // End foreach ( $slug_group as $slug ).
  1288. } // End foreach ( $existing_sidebars_widgets as $sidebar => $widgets ).
  1289. } // End foreach ( $wp_registered_sidebars as $new_sidebar => $args ).
  1290. } // End foreach ( $slug_group as $slug ).
  1291. } // End foreach ( $common_slug_groups as $slug_group ).
  1292. }
  1293. // Move any left over widgets to inactive sidebar.
  1294. foreach ( $existing_sidebars_widgets as $widgets ) {
  1295. if ( is_array( $widgets ) && ! empty( $widgets ) ) {
  1296. $new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], $widgets );
  1297. }
  1298. }
  1299. // Sidebars_widgets settings from when this theme was previously active.
  1300. $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
  1301. $old_sidebars_widgets = isset( $old_sidebars_widgets['data'] ) ? $old_sidebars_widgets['data'] : false;
  1302. if ( is_array( $old_sidebars_widgets ) ) {
  1303. // Remove empty sidebars, no need to map those.
  1304. $old_sidebars_widgets = array_filter( $old_sidebars_widgets );
  1305. // Only check sidebars that are empty or have not been mapped to yet.
  1306. foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) {
  1307. if ( array_key_exists( $new_sidebar, $old_sidebars_widgets ) && ! empty( $new_widgets ) ) {
  1308. unset( $old_sidebars_widgets[ $new_sidebar ] );
  1309. }
  1310. }
  1311. // Remove orphaned widgets, we're only interested in previously active sidebars.
  1312. foreach ( $old_sidebars_widgets as $sidebar => $widgets ) {
  1313. if ( 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
  1314. unset( $old_sidebars_widgets[ $sidebar ] );
  1315. }
  1316. }
  1317. $old_sidebars_widgets = _wp_remove_unregistered_widgets( $old_sidebars_widgets );
  1318. if ( ! empty( $old_sidebars_widgets ) ) {
  1319. // Go through each remaining sidebar...
  1320. foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) {
  1321. // ...and check every new sidebar...
  1322. foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) {
  1323. // ...for every widget we're trying to revive.
  1324. foreach ( $old_widgets as $key => $widget_id ) {
  1325. $active_key = array_search( $widget_id, $new_widgets, true );
  1326. // If the widget is used elsewhere...
  1327. if ( false !== $active_key ) {
  1328. // ...and that elsewhere is inactive widgets...
  1329. if ( 'wp_inactive_widgets' === $new_sidebar ) {
  1330. // ...remove it from there and keep the active version...
  1331. unset( $new_sidebars_widgets['wp_inactive_widgets'][ $active_key ] );
  1332. } else {
  1333. // ...otherwise remove it from the old sidebar and keep it in the new one.
  1334. unset( $old_sidebars_widgets[ $old_sidebar ][ $key ] );
  1335. }
  1336. } // End if ( $active_key ).
  1337. } // End foreach ( $old_widgets as $key => $widget_id ).
  1338. } // End foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ).
  1339. } // End foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ).
  1340. } // End if ( ! empty( $old_sidebars_widgets ) ).
  1341. // Restore widget settings from when theme was previously active.
  1342. $new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets );
  1343. }
  1344. return $new_sidebars_widgets;
  1345. }
  1346. /**
  1347. * Compares a list of sidebars with their widgets against an allowed list.
  1348. *
  1349. * @since 4.9.0
  1350. *
  1351. * @param array $sidebars_widgets List of sidebars and their widget instance IDs.
  1352. * @param array $allowed_widget_ids Optional. List of widget IDs to compare against. Default: Registered widgets.
  1353. * @return array Sidebars with allowed widgets.
  1354. */
  1355. function _wp_remove_unregistered_widgets( $sidebars_widgets, $allowed_widget_ids = array() ) {
  1356. if ( empty( $allowed_widget_ids ) ) {
  1357. $allowed_widget_ids = array_keys( $GLOBALS['wp_registered_widgets'] );
  1358. }
  1359. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  1360. if ( is_array( $widgets ) ) {
  1361. $sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $allowed_widget_ids );
  1362. }
  1363. }
  1364. return $sidebars_widgets;
  1365. }
  1366. /**
  1367. * Display the RSS entries in a list.
  1368. *
  1369. * @since 2.5.0
  1370. *
  1371. * @param string|array|object $rss RSS url.
  1372. * @param array $args Widget arguments.
  1373. */
  1374. function wp_widget_rss_output( $rss, $args = array() ) {
  1375. if ( is_string( $rss ) ) {
  1376. $rss = fetch_feed( $rss );
  1377. } elseif ( is_array( $rss ) && isset( $rss['url'] ) ) {
  1378. $args = $rss;
  1379. $rss = fetch_feed( $rss['url'] );
  1380. } elseif ( ! is_object( $rss ) ) {
  1381. return;
  1382. }
  1383. if ( is_wp_error( $rss ) ) {
  1384. if ( is_admin() || current_user_can( 'manage_options' ) ) {
  1385. echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</p>';
  1386. }
  1387. return;
  1388. }
  1389. $default_args = array(
  1390. 'show_author' => 0,
  1391. 'show_date' => 0,
  1392. 'show_summary' => 0,
  1393. 'items' => 0,
  1394. );
  1395. $args = wp_parse_args( $args, $default_args );
  1396. $items = (int) $args['items'];
  1397. if ( $items < 1 || 20 < $items ) {
  1398. $items = 10;
  1399. }
  1400. $show_summary = (int) $args['show_summary'];
  1401. $show_author = (int) $args['show_author'];
  1402. $show_date = (int) $args['show_date'];
  1403. if ( ! $rss->get_item_quantity() ) {
  1404. echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
  1405. $rss->__destruct();
  1406. unset( $rss );
  1407. return;
  1408. }
  1409. echo '<ul>';
  1410. foreach ( $rss->get_items( 0, $items ) as $item ) {
  1411. $link = $item->get_link();
  1412. while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
  1413. $link = substr( $link, 1 );
  1414. }
  1415. $link = esc_url( strip_tags( $link ) );
  1416. $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
  1417. if ( empty( $title ) ) {
  1418. $title = __( 'Untitled' );
  1419. }
  1420. $desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
  1421. $desc = esc_attr( wp_trim_words( $desc, 55, ' [&hellip;]' ) );
  1422. $summary = '';
  1423. if ( $show_summary ) {
  1424. $summary = $desc;
  1425. // Change existing [...] to [&hellip;].
  1426. if ( '[...]' === substr( $summary, -5 ) ) {
  1427. $summary = substr( $summary, 0, -5 ) . '[&hellip;]';
  1428. }
  1429. $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
  1430. }
  1431. $date = '';
  1432. if ( $show_date ) {
  1433. $date = $item->get_date( 'U' );
  1434. if ( $date ) {
  1435. $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
  1436. }
  1437. }
  1438. $author = '';
  1439. if ( $show_author ) {
  1440. $author = $item->get_author();
  1441. if ( is_object( $author ) ) {
  1442. $author = $author->get_name();
  1443. $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
  1444. }
  1445. }
  1446. if ( '' === $link ) {
  1447. echo "<li>$title{$date}{$summary}{$author}</li>";
  1448. } elseif ( $show_summary ) {
  1449. echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
  1450. } else {
  1451. echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
  1452. }
  1453. }
  1454. echo '</ul>';
  1455. $rss->__destruct();
  1456. unset( $rss );
  1457. }
  1458. /**
  1459. * Display RSS widget options form.
  1460. *
  1461. * The options for what fields are displayed for the RSS form are all booleans
  1462. * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
  1463. * 'show_date'.
  1464. *
  1465. * @since 2.5.0
  1466. *
  1467. * @param array|string $args Values for input fields.
  1468. * @param array $inputs Override default display options.
  1469. */
  1470. function wp_widget_rss_form( $args, $inputs = null ) {
  1471. $default_inputs = array(
  1472. 'url' => true,
  1473. 'title' => true,
  1474. 'items' => true,
  1475. 'show_summary' => true,
  1476. 'show_author' => true,
  1477. 'show_date' => true,
  1478. );
  1479. $inputs = wp_parse_args( $inputs, $default_inputs );
  1480. $args['title'] = isset( $args['title'] ) ? $args['title'] : '';
  1481. $args['url'] = isset( $args['url'] ) ? $args['url'] : '';
  1482. $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
  1483. if ( $args['items'] < 1 || 20 < $args['items'] ) {
  1484. $args['items'] = 10;
  1485. }
  1486. $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
  1487. $args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
  1488. $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];
  1489. if ( ! empty( $args['error'] ) ) {
  1490. echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $args['error'] ) . '</p>';
  1491. }
  1492. $esc_number = esc_attr( $args['number'] );
  1493. if ( $inputs['url'] ) :
  1494. ?>
  1495. <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
  1496. <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>
  1497. <?php endif; if ( $inputs['title'] ) : ?>
  1498. <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
  1499. <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>
  1500. <?php endif; if ( $inputs['items'] ) : ?>
  1501. <p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
  1502. <select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
  1503. <?php
  1504. for ( $i = 1; $i <= 20; ++$i ) {
  1505. echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
  1506. }
  1507. ?>
  1508. </select></p>
  1509. <?php endif; if ( $inputs['show_summary'] || $inputs['show_author'] || $inputs['show_date'] ) : ?>
  1510. <p>
  1511. <?php if ( $inputs['show_summary'] ) : ?>
  1512. <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'] ); ?> />
  1513. <label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label><br />
  1514. <?php endif; if ( $inputs['show_author'] ) : ?>
  1515. <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'] ); ?> />
  1516. <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label><br />
  1517. <?php endif; if ( $inputs['show_date'] ) : ?>
  1518. <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'] ); ?>/>
  1519. <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label><br />
  1520. <?php endif; ?>
  1521. </p>
  1522. <?php
  1523. endif; // End of display options.
  1524. foreach ( array_keys( $default_inputs ) as $input ) :
  1525. if ( 'hidden' === $inputs[ $input ] ) :
  1526. $id = str_replace( '_', '-', $input );
  1527. ?>
  1528. <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 ] ); ?>" />
  1529. <?php
  1530. endif;
  1531. endforeach;
  1532. }
  1533. /**
  1534. * Process RSS feed widget data and optionally retrieve feed items.
  1535. *
  1536. * The feed widget can not have more than 20 items or it will reset back to the
  1537. * default, which is 10.
  1538. *
  1539. * The resulting array has the feed title, feed url, feed link (from channel),
  1540. * feed items, error (if any), and whether to show summary, author, and date.
  1541. * All respectively in the order of the array elements.
  1542. *
  1543. * @since 2.5.0
  1544. *
  1545. * @param array $widget_rss RSS widget feed data. Expects unescaped data.
  1546. * @param bool $check_feed Optional. Whether to check feed for errors. Default true.
  1547. * @return array
  1548. */
  1549. function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
  1550. $items = (int) $widget_rss['items'];
  1551. if ( $items < 1 || 20 < $items ) {
  1552. $items = 10;
  1553. }
  1554. $url = sanitize_url( strip_tags( $widget_rss['url'] ) );
  1555. $title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
  1556. $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
  1557. $show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
  1558. $show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
  1559. $error = false;
  1560. $link = '';
  1561. if ( $check_feed ) {
  1562. $rss = fetch_feed( $url );
  1563. if ( is_wp_error( $rss ) ) {
  1564. $error = $rss->get_error_message();
  1565. } else {
  1566. $link = esc_url( strip_tags( $rss->get_permalink() ) );
  1567. while ( stristr( $link, 'http' ) !== $link ) {
  1568. $link = substr( $link, 1 );
  1569. }
  1570. $rss->__destruct();
  1571. unset( $rss );
  1572. }
  1573. }
  1574. return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
  1575. }
  1576. /**
  1577. * Registers all of the default WordPress widgets on startup.
  1578. *
  1579. * Calls {@see 'widgets_init'} action after all of the WordPress widgets have been registered.
  1580. *
  1581. * @since 2.2.0
  1582. */
  1583. function wp_widgets_init() {
  1584. if ( ! is_blog_installed() ) {
  1585. return;
  1586. }
  1587. register_widget( 'WP_Widget_Pages' );
  1588. register_widget( 'WP_Widget_Calendar' );
  1589. register_widget( 'WP_Widget_Archives' );
  1590. if ( get_option( 'link_manager_enabled' ) ) {
  1591. register_widget( 'WP_Widget_Links' );
  1592. }
  1593. register_widget( 'WP_Widget_Media_Audio' );
  1594. register_widget( 'WP_Widget_Media_Image' );
  1595. register_widget( 'WP_Widget_Media_Gallery' );
  1596. register_widget( 'WP_Widget_Media_Video' );
  1597. register_widget( 'WP_Widget_Meta' );
  1598. register_widget( 'WP_Widget_Search' );
  1599. register_widget( 'WP_Widget_Text' );
  1600. register_widget( 'WP_Widget_Categories' );
  1601. register_widget( 'WP_Widget_Recent_Posts' );
  1602. register_widget( 'WP_Widget_Recent_Comments' );
  1603. register_widget( 'WP_Widget_RSS' );
  1604. register_widget( 'WP_Widget_Tag_Cloud' );
  1605. register_widget( 'WP_Nav_Menu_Widget' );
  1606. register_widget( 'WP_Widget_Custom_HTML' );
  1607. register_widget( 'WP_Widget_Block' );
  1608. /**
  1609. * Fires after all default WordPress widgets have been registered.
  1610. *
  1611. * @since 2.2.0
  1612. */
  1613. do_action( 'widgets_init' );
  1614. }
  1615. /**
  1616. * Enables the widgets block editor. This is hooked into 'after_setup_theme' so
  1617. * that the block editor is enabled by default but can be disabled by themes.
  1618. *
  1619. * @since 5.8.0
  1620. *
  1621. * @access private
  1622. */
  1623. function wp_setup_widgets_block_editor() {
  1624. add_theme_support( 'widgets-block-editor' );
  1625. }
  1626. /**
  1627. * Whether or not to use the block editor to manage widgets. Defaults to true
  1628. * unless a theme has removed support for widgets-block-editor or a plugin has
  1629. * filtered the return value of this function.
  1630. *
  1631. * @since 5.8.0
  1632. *
  1633. * @return bool Whether to use the block editor to manage widgets.
  1634. */
  1635. function wp_use_widgets_block_editor() {
  1636. /**
  1637. * Filters whether to use the block editor to manage widgets.
  1638. *
  1639. * @since 5.8.0
  1640. *
  1641. * @param bool $use_widgets_block_editor Whether to use the block editor to manage widgets.
  1642. */
  1643. return apply_filters(
  1644. 'use_widgets_block_editor',
  1645. get_theme_support( 'widgets-block-editor' )
  1646. );
  1647. }
  1648. /**
  1649. * Converts a widget ID into its id_base and number components.
  1650. *
  1651. * @since 5.8.0
  1652. *
  1653. * @param string $id Widget ID.
  1654. * @return array Array containing a widget's id_base and number components.
  1655. */
  1656. function wp_parse_widget_id( $id ) {
  1657. $parsed = array();
  1658. if ( preg_match( '/^(.+)-(\d+)$/', $id, $matches ) ) {
  1659. $parsed['id_base'] = $matches[1];
  1660. $parsed['number'] = (int) $matches[2];
  1661. } else {
  1662. // Likely an old single widget.
  1663. $parsed['id_base'] = $id;
  1664. }
  1665. return $parsed;
  1666. }
  1667. /**
  1668. * Finds the sidebar that a given widget belongs to.
  1669. *
  1670. * @since 5.8.0
  1671. *
  1672. * @param string $widget_id The widget ID to look for.
  1673. * @return string|null The found sidebar's ID, or null if it was not found.
  1674. */
  1675. function wp_find_widgets_sidebar( $widget_id ) {
  1676. foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
  1677. foreach ( $widget_ids as $maybe_widget_id ) {
  1678. if ( $maybe_widget_id === $widget_id ) {
  1679. return (string) $sidebar_id;
  1680. }
  1681. }
  1682. }
  1683. return null;
  1684. }
  1685. /**
  1686. * Assigns a widget to the given sidebar.
  1687. *
  1688. * @since 5.8.0
  1689. *
  1690. * @param string $widget_id The widget ID to assign.
  1691. * @param string $sidebar_id The sidebar ID to assign to. If empty, the widget won't be added to any sidebar.
  1692. */
  1693. function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
  1694. $sidebars = wp_get_sidebars_widgets();
  1695. foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
  1696. foreach ( $widgets as $i => $maybe_widget_id ) {
  1697. if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
  1698. unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
  1699. // We could technically break 2 here, but continue looping in case the ID is duplicated.
  1700. continue 2;
  1701. }
  1702. }
  1703. }
  1704. if ( $sidebar_id ) {
  1705. $sidebars[ $sidebar_id ][] = $widget_id;
  1706. }
  1707. wp_set_sidebars_widgets( $sidebars );
  1708. }
  1709. /**
  1710. * Calls the render callback of a widget and returns the output.
  1711. *
  1712. * @since 5.8.0
  1713. *
  1714. * @param string $widget_id Widget ID.
  1715. * @param string $sidebar_id Sidebar ID.
  1716. * @return string
  1717. */
  1718. function wp_render_widget( $widget_id, $sidebar_id ) {
  1719. global $wp_registered_widgets, $wp_registered_sidebars;
  1720. if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
  1721. return '';
  1722. }
  1723. if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
  1724. $sidebar = $wp_registered_sidebars[ $sidebar_id ];
  1725. } elseif ( 'wp_inactive_widgets' === $sidebar_id ) {
  1726. $sidebar = array();
  1727. } else {
  1728. return '';
  1729. }
  1730. $params = array_merge(
  1731. array(
  1732. array_merge(
  1733. $sidebar,
  1734. array(
  1735. 'widget_id' => $widget_id,
  1736. 'widget_name' => $wp_registered_widgets[ $widget_id ]['name'],
  1737. )
  1738. ),
  1739. ),
  1740. (array) $wp_registered_widgets[ $widget_id ]['params']
  1741. );
  1742. // Substitute HTML `id` and `class` attributes into `before_widget`.
  1743. $classname_ = '';
  1744. foreach ( (array) $wp_registered_widgets[ $widget_id ]['classname'] as $cn ) {
  1745. if ( is_string( $cn ) ) {
  1746. $classname_ .= '_' . $cn;
  1747. } elseif ( is_object( $cn ) ) {
  1748. $classname_ .= '_' . get_class( $cn );
  1749. }
  1750. }
  1751. $classname_ = ltrim( $classname_, '_' );
  1752. $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ );
  1753. /** This filter is documented in wp-includes/widgets.php */
  1754. $params = apply_filters( 'dynamic_sidebar_params', $params );
  1755. $callback = $wp_registered_widgets[ $widget_id ]['callback'];
  1756. ob_start();
  1757. /** This filter is documented in wp-includes/widgets.php */
  1758. do_action( 'dynamic_sidebar', $wp_registered_widgets[ $widget_id ] );
  1759. if ( is_callable( $callback ) ) {
  1760. call_user_func_array( $callback, $params );
  1761. }
  1762. return ob_get_clean();
  1763. }
  1764. /**
  1765. * Calls the control callback of a widget and returns the output.
  1766. *
  1767. * @since 5.8.0
  1768. *
  1769. * @param string $id Widget ID.
  1770. * @return string|null
  1771. */
  1772. function wp_render_widget_control( $id ) {
  1773. global $wp_registered_widget_controls;
  1774. if ( ! isset( $wp_registered_widget_controls[ $id ]['callback'] ) ) {
  1775. return null;
  1776. }
  1777. $callback = $wp_registered_widget_controls[ $id ]['callback'];
  1778. $params = $wp_registered_widget_controls[ $id ]['params'];
  1779. ob_start();
  1780. if ( is_callable( $callback ) ) {
  1781. call_user_func_array( $callback, $params );
  1782. }
  1783. return ob_get_clean();
  1784. }
  1785. /**
  1786. * Displays a _doing_it_wrong() message for conflicting widget editor scripts.
  1787. *
  1788. * The 'wp-editor' script module is exposed as window.wp.editor. This overrides
  1789. * the legacy TinyMCE editor module which is required by the widgets editor.
  1790. * Because of that conflict, these two shouldn't be enqueued together.
  1791. * See https://core.trac.wordpress.org/ticket/53569.
  1792. *
  1793. * There is also another conflict related to styles where the block widgets
  1794. * editor is hidden if a block enqueues 'wp-edit-post' stylesheet.
  1795. * See https://core.trac.wordpress.org/ticket/53569.
  1796. *
  1797. * @since 5.8.0
  1798. * @access private
  1799. *
  1800. * @global WP_Scripts $wp_scripts
  1801. * @global WP_Styles $wp_styles
  1802. */
  1803. function wp_check_widget_editor_deps() {
  1804. global $wp_scripts, $wp_styles;
  1805. if (
  1806. $wp_scripts->query( 'wp-edit-widgets', 'enqueued' ) ||
  1807. $wp_scripts->query( 'wp-customize-widgets', 'enqueued' )
  1808. ) {
  1809. if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) {
  1810. _doing_it_wrong(
  1811. 'wp_enqueue_script()',
  1812. sprintf(
  1813. /* translators: 1: 'wp-editor', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
  1814. __( '"%1$s" script should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
  1815. 'wp-editor',
  1816. 'wp-edit-widgets',
  1817. 'wp-customize-widgets'
  1818. ),
  1819. '5.8.0'
  1820. );
  1821. }
  1822. if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) {
  1823. _doing_it_wrong(
  1824. 'wp_enqueue_style()',
  1825. sprintf(
  1826. /* translators: 1: 'wp-edit-post', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
  1827. __( '"%1$s" style should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
  1828. 'wp-edit-post',
  1829. 'wp-edit-widgets',
  1830. 'wp-customize-widgets'
  1831. ),
  1832. '5.8.0'
  1833. );
  1834. }
  1835. }
  1836. }