Нема описа

widgets.php 66KB

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