Ei kuvausta

script-loader.php 127KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530
  1. <?php
  2. /**
  3. * WordPress scripts and styles default loader.
  4. *
  5. * Several constants are used to manage the loading, concatenating and compression of scripts and CSS:
  6. * define('SCRIPT_DEBUG', true); loads the development (non-minified) versions of all scripts and CSS, and disables compression and concatenation,
  7. * define('CONCATENATE_SCRIPTS', false); disables compression and concatenation of scripts and CSS,
  8. * define('COMPRESS_SCRIPTS', false); disables compression of scripts,
  9. * define('COMPRESS_CSS', false); disables compression of CSS,
  10. * define('ENFORCE_GZIP', true); forces gzip for compression (default is deflate).
  11. *
  12. * The globals $concatenate_scripts, $compress_scripts and $compress_css can be set by plugins
  13. * to temporarily override the above settings. Also a compression test is run once and the result is saved
  14. * as option 'can_compress_scripts' (0/1). The test will run again if that option is deleted.
  15. *
  16. * @package WordPress
  17. */
  18. /** WordPress Dependency Class */
  19. require ABSPATH . WPINC . '/class-wp-dependency.php';
  20. /** WordPress Dependencies Class */
  21. require ABSPATH . WPINC . '/class.wp-dependencies.php';
  22. /** WordPress Scripts Class */
  23. require ABSPATH . WPINC . '/class.wp-scripts.php';
  24. /** WordPress Scripts Functions */
  25. require ABSPATH . WPINC . '/functions.wp-scripts.php';
  26. /** WordPress Styles Class */
  27. require ABSPATH . WPINC . '/class.wp-styles.php';
  28. /** WordPress Styles Functions */
  29. require ABSPATH . WPINC . '/functions.wp-styles.php';
  30. /**
  31. * Registers TinyMCE scripts.
  32. *
  33. * @since 5.0.0
  34. *
  35. * @global string $tinymce_version
  36. * @global bool $concatenate_scripts
  37. * @global bool $compress_scripts
  38. *
  39. * @param WP_Scripts $scripts WP_Scripts object.
  40. * @param bool $force_uncompressed Whether to forcibly prevent gzip compression. Default false.
  41. */
  42. function wp_register_tinymce_scripts( $scripts, $force_uncompressed = false ) {
  43. global $tinymce_version, $concatenate_scripts, $compress_scripts;
  44. $suffix = wp_scripts_get_suffix();
  45. $dev_suffix = wp_scripts_get_suffix( 'dev' );
  46. script_concat_settings();
  47. $compressed = $compress_scripts && $concatenate_scripts && isset( $_SERVER['HTTP_ACCEPT_ENCODING'] )
  48. && false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) && ! $force_uncompressed;
  49. // Load tinymce.js when running from /src, otherwise load wp-tinymce.js.gz (in production)
  50. // or tinymce.min.js (when SCRIPT_DEBUG is true).
  51. if ( $compressed ) {
  52. $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . 'wp-tinymce.js', array(), $tinymce_version );
  53. } else {
  54. $scripts->add( 'wp-tinymce-root', includes_url( 'js/tinymce/' ) . "tinymce$dev_suffix.js", array(), $tinymce_version );
  55. $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . "plugins/compat3x/plugin$dev_suffix.js", array( 'wp-tinymce-root' ), $tinymce_version );
  56. }
  57. $scripts->add( 'wp-tinymce-lists', includes_url( "js/tinymce/plugins/lists/plugin$suffix.js" ), array( 'wp-tinymce' ), $tinymce_version );
  58. }
  59. /**
  60. * Registers all the WordPress vendor scripts that are in the standardized
  61. * `js/dist/vendor/` location.
  62. *
  63. * For the order of `$scripts->add` see `wp_default_scripts`.
  64. *
  65. * @since 5.0.0
  66. *
  67. * @global WP_Locale $wp_locale WordPress date and time locale object.
  68. *
  69. * @param WP_Scripts $scripts WP_Scripts object.
  70. */
  71. function wp_default_packages_vendor( $scripts ) {
  72. global $wp_locale;
  73. $suffix = wp_scripts_get_suffix();
  74. $vendor_scripts = array(
  75. 'react' => array( 'wp-polyfill' ),
  76. 'react-dom' => array( 'react' ),
  77. 'regenerator-runtime',
  78. 'moment',
  79. 'lodash',
  80. 'wp-polyfill-fetch',
  81. 'wp-polyfill-formdata',
  82. 'wp-polyfill-node-contains',
  83. 'wp-polyfill-url',
  84. 'wp-polyfill-dom-rect',
  85. 'wp-polyfill-element-closest',
  86. 'wp-polyfill-object-fit',
  87. 'wp-polyfill' => array( 'regenerator-runtime' ),
  88. );
  89. $vendor_scripts_versions = array(
  90. 'react' => '17.0.1',
  91. 'react-dom' => '17.0.1',
  92. 'regenerator-runtime' => '0.13.9',
  93. 'moment' => '2.29.2',
  94. 'lodash' => '4.17.19',
  95. 'wp-polyfill-fetch' => '3.6.2',
  96. 'wp-polyfill-formdata' => '4.0.10',
  97. 'wp-polyfill-node-contains' => '4.0.0',
  98. 'wp-polyfill-url' => '3.6.4',
  99. 'wp-polyfill-dom-rect' => '4.0.0',
  100. 'wp-polyfill-element-closest' => '2.0.2',
  101. 'wp-polyfill-object-fit' => '2.3.5',
  102. 'wp-polyfill' => '3.15.0',
  103. );
  104. foreach ( $vendor_scripts as $handle => $dependencies ) {
  105. if ( is_string( $dependencies ) ) {
  106. $handle = $dependencies;
  107. $dependencies = array();
  108. }
  109. $path = "/wp-includes/js/dist/vendor/$handle$suffix.js";
  110. $version = $vendor_scripts_versions[ $handle ];
  111. $scripts->add( $handle, $path, $dependencies, $version, 1 );
  112. }
  113. did_action( 'init' ) && $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );
  114. did_action( 'init' ) && $scripts->add_inline_script(
  115. 'moment',
  116. sprintf(
  117. "moment.updateLocale( '%s', %s );",
  118. get_user_locale(),
  119. wp_json_encode(
  120. array(
  121. 'months' => array_values( $wp_locale->month ),
  122. 'monthsShort' => array_values( $wp_locale->month_abbrev ),
  123. 'weekdays' => array_values( $wp_locale->weekday ),
  124. 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
  125. 'week' => array(
  126. 'dow' => (int) get_option( 'start_of_week', 0 ),
  127. ),
  128. 'longDateFormat' => array(
  129. 'LT' => get_option( 'time_format', __( 'g:i a' ) ),
  130. 'LTS' => null,
  131. 'L' => null,
  132. 'LL' => get_option( 'date_format', __( 'F j, Y' ) ),
  133. 'LLL' => __( 'F j, Y g:i a' ),
  134. 'LLLL' => null,
  135. ),
  136. )
  137. )
  138. ),
  139. 'after'
  140. );
  141. }
  142. /**
  143. * Returns contents of an inline script used in appending polyfill scripts for
  144. * browsers which fail the provided tests. The provided array is a mapping from
  145. * a condition to verify feature support to its polyfill script handle.
  146. *
  147. * @since 5.0.0
  148. *
  149. * @param WP_Scripts $scripts WP_Scripts object.
  150. * @param array $tests Features to detect.
  151. * @return string Conditional polyfill inline script.
  152. */
  153. function wp_get_script_polyfill( $scripts, $tests ) {
  154. $polyfill = '';
  155. foreach ( $tests as $test => $handle ) {
  156. if ( ! array_key_exists( $handle, $scripts->registered ) ) {
  157. continue;
  158. }
  159. $src = $scripts->registered[ $handle ]->src;
  160. $ver = $scripts->registered[ $handle ]->ver;
  161. if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && 0 === strpos( $src, $scripts->content_url ) ) ) {
  162. $src = $scripts->base_url . $src;
  163. }
  164. if ( ! empty( $ver ) ) {
  165. $src = add_query_arg( 'ver', $ver, $src );
  166. }
  167. /** This filter is documented in wp-includes/class.wp-scripts.php */
  168. $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
  169. if ( ! $src ) {
  170. continue;
  171. }
  172. $polyfill .= (
  173. // Test presence of feature...
  174. '( ' . $test . ' ) || ' .
  175. /*
  176. * ...appending polyfill on any failures. Cautious viewers may balk
  177. * at the `document.write`. Its caveat of synchronous mid-stream
  178. * blocking write is exactly the behavior we need though.
  179. */
  180. 'document.write( \'<script src="' .
  181. $src .
  182. '"></scr\' + \'ipt>\' );'
  183. );
  184. }
  185. return $polyfill;
  186. }
  187. /**
  188. * Registers development scripts that integrate with `@wordpress/scripts`.
  189. *
  190. * @see https://github.com/WordPress/gutenberg/tree/trunk/packages/scripts#start
  191. *
  192. * @since 6.0.0
  193. *
  194. * @param WP_Scripts $scripts WP_Scripts object.
  195. */
  196. function wp_register_development_scripts( $scripts ) {
  197. if (
  198. ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG
  199. || empty( $scripts->registered['react'] )
  200. ) {
  201. return;
  202. }
  203. $development_scripts = array(
  204. 'react-refresh-entry',
  205. 'react-refresh-runtime',
  206. );
  207. foreach ( $development_scripts as $script_name ) {
  208. $assets = include ABSPATH . WPINC . '/assets/script-loader-' . $script_name . '.php';
  209. if ( ! is_array( $assets ) ) {
  210. return;
  211. }
  212. $scripts->add(
  213. 'wp-' . $script_name,
  214. '/wp-includes/js/dist/development/' . $script_name . '.js',
  215. $assets['dependencies'],
  216. $assets['version']
  217. );
  218. }
  219. // See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
  220. $scripts->registered['react']->deps[] = 'wp-react-refresh-entry';
  221. }
  222. /**
  223. * Registers all the WordPress packages scripts that are in the standardized
  224. * `js/dist/` location.
  225. *
  226. * For the order of `$scripts->add` see `wp_default_scripts`.
  227. *
  228. * @since 5.0.0
  229. *
  230. * @param WP_Scripts $scripts WP_Scripts object.
  231. */
  232. function wp_default_packages_scripts( $scripts ) {
  233. $suffix = wp_scripts_get_suffix();
  234. /*
  235. * Expects multidimensional array like:
  236. *
  237. * 'a11y.js' => array('dependencies' => array(...), 'version' => '...'),
  238. * 'annotations.js' => array('dependencies' => array(...), 'version' => '...'),
  239. * 'api-fetch.js' => array(...
  240. */
  241. $assets = include ABSPATH . WPINC . '/assets/script-loader-packages.php';
  242. foreach ( $assets as $package_name => $package_data ) {
  243. $basename = basename( $package_name, '.js' );
  244. $handle = 'wp-' . $basename;
  245. $path = "/wp-includes/js/dist/{$basename}{$suffix}.js";
  246. if ( ! empty( $package_data['dependencies'] ) ) {
  247. $dependencies = $package_data['dependencies'];
  248. } else {
  249. $dependencies = array();
  250. }
  251. // Add dependencies that cannot be detected and generated by build tools.
  252. switch ( $handle ) {
  253. case 'wp-block-library':
  254. array_push( $dependencies, 'editor' );
  255. break;
  256. case 'wp-edit-post':
  257. array_push( $dependencies, 'media-models', 'media-views', 'postbox', 'wp-dom-ready' );
  258. break;
  259. }
  260. $scripts->add( $handle, $path, $dependencies, $package_data['version'], 1 );
  261. if ( in_array( 'wp-i18n', $dependencies, true ) ) {
  262. $scripts->set_translations( $handle );
  263. }
  264. /*
  265. * Manually set the text direction localization after wp-i18n is printed.
  266. * This ensures that wp.i18n.isRTL() returns true in RTL languages.
  267. * We cannot use $scripts->set_translations( 'wp-i18n' ) to do this
  268. * because WordPress prints a script's translations *before* the script,
  269. * which means, in the case of wp-i18n, that wp.i18n.setLocaleData()
  270. * is called before wp.i18n is defined.
  271. */
  272. if ( 'wp-i18n' === $handle ) {
  273. $ltr = _x( 'ltr', 'text direction' );
  274. $script = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] } );", $ltr );
  275. $scripts->add_inline_script( $handle, $script, 'after' );
  276. }
  277. }
  278. }
  279. /**
  280. * Adds inline scripts required for the WordPress JavaScript packages.
  281. *
  282. * @since 5.0.0
  283. *
  284. * @global WP_Locale $wp_locale WordPress date and time locale object.
  285. *
  286. * @param WP_Scripts $scripts WP_Scripts object.
  287. */
  288. function wp_default_packages_inline_scripts( $scripts ) {
  289. global $wp_locale;
  290. if ( isset( $scripts->registered['wp-api-fetch'] ) ) {
  291. $scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
  292. }
  293. $scripts->add_inline_script(
  294. 'wp-api-fetch',
  295. sprintf(
  296. 'wp.apiFetch.use( wp.apiFetch.createRootURLMiddleware( "%s" ) );',
  297. esc_url_raw( get_rest_url() )
  298. ),
  299. 'after'
  300. );
  301. $scripts->add_inline_script(
  302. 'wp-api-fetch',
  303. implode(
  304. "\n",
  305. array(
  306. sprintf(
  307. 'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
  308. wp_installing() ? '' : wp_create_nonce( 'wp_rest' )
  309. ),
  310. 'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',
  311. 'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
  312. sprintf(
  313. 'wp.apiFetch.nonceEndpoint = "%s";',
  314. admin_url( 'admin-ajax.php?action=rest-nonce' )
  315. ),
  316. )
  317. ),
  318. 'after'
  319. );
  320. $scripts->add_inline_script(
  321. 'wp-data',
  322. implode(
  323. "\n",
  324. array(
  325. '( function() {',
  326. ' var userId = ' . get_current_user_ID() . ';',
  327. ' var storageKey = "WP_DATA_USER_" + userId;',
  328. ' wp.data',
  329. ' .use( wp.data.plugins.persistence, { storageKey: storageKey } );',
  330. ' wp.data.plugins.persistence.__unstableMigrate( { storageKey: storageKey } );',
  331. '} )();',
  332. )
  333. )
  334. );
  335. // Calculate the timezone abbr (EDT, PST) if possible.
  336. $timezone_string = get_option( 'timezone_string', 'UTC' );
  337. $timezone_abbr = '';
  338. if ( ! empty( $timezone_string ) ) {
  339. $timezone_date = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
  340. $timezone_abbr = $timezone_date->format( 'T' );
  341. }
  342. $scripts->add_inline_script(
  343. 'wp-date',
  344. sprintf(
  345. 'wp.date.setSettings( %s );',
  346. wp_json_encode(
  347. array(
  348. 'l10n' => array(
  349. 'locale' => get_user_locale(),
  350. 'months' => array_values( $wp_locale->month ),
  351. 'monthsShort' => array_values( $wp_locale->month_abbrev ),
  352. 'weekdays' => array_values( $wp_locale->weekday ),
  353. 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
  354. 'meridiem' => (object) $wp_locale->meridiem,
  355. 'relative' => array(
  356. /* translators: %s: Duration. */
  357. 'future' => __( '%s from now' ),
  358. /* translators: %s: Duration. */
  359. 'past' => __( '%s ago' ),
  360. ),
  361. ),
  362. 'formats' => array(
  363. /* translators: Time format, see https://www.php.net/manual/datetime.format.php */
  364. 'time' => get_option( 'time_format', __( 'g:i a' ) ),
  365. /* translators: Date format, see https://www.php.net/manual/datetime.format.php */
  366. 'date' => get_option( 'date_format', __( 'F j, Y' ) ),
  367. /* translators: Date/Time format, see https://www.php.net/manual/datetime.format.php */
  368. 'datetime' => __( 'F j, Y g:i a' ),
  369. /* translators: Abbreviated date/time format, see https://www.php.net/manual/datetime.format.php */
  370. 'datetimeAbbreviated' => __( 'M j, Y g:i a' ),
  371. ),
  372. 'timezone' => array(
  373. 'offset' => get_option( 'gmt_offset', 0 ),
  374. 'string' => $timezone_string,
  375. 'abbr' => $timezone_abbr,
  376. ),
  377. )
  378. )
  379. ),
  380. 'after'
  381. );
  382. // Loading the old editor and its config to ensure the classic block works as expected.
  383. $scripts->add_inline_script(
  384. 'editor',
  385. 'window.wp.oldEditor = window.wp.editor;',
  386. 'after'
  387. );
  388. /*
  389. * wp-editor module is exposed as window.wp.editor.
  390. * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor.
  391. * Solution: fuse the two objects together to maintain backward compatibility.
  392. * For more context, see https://github.com/WordPress/gutenberg/issues/33203.
  393. */
  394. $scripts->add_inline_script(
  395. 'wp-editor',
  396. 'Object.assign( window.wp.editor, window.wp.oldEditor );',
  397. 'after'
  398. );
  399. }
  400. /**
  401. * Adds inline scripts required for the TinyMCE in the block editor.
  402. *
  403. * These TinyMCE init settings are used to extend and override the default settings
  404. * from `_WP_Editors::default_settings()` for the Classic block.
  405. *
  406. * @since 5.0.0
  407. *
  408. * @global WP_Scripts $wp_scripts
  409. */
  410. function wp_tinymce_inline_scripts() {
  411. global $wp_scripts;
  412. /** This filter is documented in wp-includes/class-wp-editor.php */
  413. $editor_settings = apply_filters( 'wp_editor_settings', array( 'tinymce' => true ), 'classic-block' );
  414. $tinymce_plugins = array(
  415. 'charmap',
  416. 'colorpicker',
  417. 'hr',
  418. 'lists',
  419. 'media',
  420. 'paste',
  421. 'tabfocus',
  422. 'textcolor',
  423. 'fullscreen',
  424. 'wordpress',
  425. 'wpautoresize',
  426. 'wpeditimage',
  427. 'wpemoji',
  428. 'wpgallery',
  429. 'wplink',
  430. 'wpdialogs',
  431. 'wptextpattern',
  432. 'wpview',
  433. );
  434. /** This filter is documented in wp-includes/class-wp-editor.php */
  435. $tinymce_plugins = apply_filters( 'tiny_mce_plugins', $tinymce_plugins, 'classic-block' );
  436. $tinymce_plugins = array_unique( $tinymce_plugins );
  437. $disable_captions = false;
  438. // Runs after `tiny_mce_plugins` but before `mce_buttons`.
  439. /** This filter is documented in wp-admin/includes/media.php */
  440. if ( apply_filters( 'disable_captions', '' ) ) {
  441. $disable_captions = true;
  442. }
  443. $toolbar1 = array(
  444. 'formatselect',
  445. 'bold',
  446. 'italic',
  447. 'bullist',
  448. 'numlist',
  449. 'blockquote',
  450. 'alignleft',
  451. 'aligncenter',
  452. 'alignright',
  453. 'link',
  454. 'unlink',
  455. 'wp_more',
  456. 'spellchecker',
  457. 'wp_add_media',
  458. 'wp_adv',
  459. );
  460. /** This filter is documented in wp-includes/class-wp-editor.php */
  461. $toolbar1 = apply_filters( 'mce_buttons', $toolbar1, 'classic-block' );
  462. $toolbar2 = array(
  463. 'strikethrough',
  464. 'hr',
  465. 'forecolor',
  466. 'pastetext',
  467. 'removeformat',
  468. 'charmap',
  469. 'outdent',
  470. 'indent',
  471. 'undo',
  472. 'redo',
  473. 'wp_help',
  474. );
  475. /** This filter is documented in wp-includes/class-wp-editor.php */
  476. $toolbar2 = apply_filters( 'mce_buttons_2', $toolbar2, 'classic-block' );
  477. /** This filter is documented in wp-includes/class-wp-editor.php */
  478. $toolbar3 = apply_filters( 'mce_buttons_3', array(), 'classic-block' );
  479. /** This filter is documented in wp-includes/class-wp-editor.php */
  480. $toolbar4 = apply_filters( 'mce_buttons_4', array(), 'classic-block' );
  481. /** This filter is documented in wp-includes/class-wp-editor.php */
  482. $external_plugins = apply_filters( 'mce_external_plugins', array(), 'classic-block' );
  483. $tinymce_settings = array(
  484. 'plugins' => implode( ',', $tinymce_plugins ),
  485. 'toolbar1' => implode( ',', $toolbar1 ),
  486. 'toolbar2' => implode( ',', $toolbar2 ),
  487. 'toolbar3' => implode( ',', $toolbar3 ),
  488. 'toolbar4' => implode( ',', $toolbar4 ),
  489. 'external_plugins' => wp_json_encode( $external_plugins ),
  490. 'classic_block_editor' => true,
  491. );
  492. if ( $disable_captions ) {
  493. $tinymce_settings['wpeditimage_disable_captions'] = true;
  494. }
  495. if ( ! empty( $editor_settings['tinymce'] ) && is_array( $editor_settings['tinymce'] ) ) {
  496. array_merge( $tinymce_settings, $editor_settings['tinymce'] );
  497. }
  498. /** This filter is documented in wp-includes/class-wp-editor.php */
  499. $tinymce_settings = apply_filters( 'tiny_mce_before_init', $tinymce_settings, 'classic-block' );
  500. // Do "by hand" translation from PHP array to js object.
  501. // Prevents breakage in some custom settings.
  502. $init_obj = '';
  503. foreach ( $tinymce_settings as $key => $value ) {
  504. if ( is_bool( $value ) ) {
  505. $val = $value ? 'true' : 'false';
  506. $init_obj .= $key . ':' . $val . ',';
  507. continue;
  508. } elseif ( ! empty( $value ) && is_string( $value ) && (
  509. ( '{' === $value[0] && '}' === $value[ strlen( $value ) - 1 ] ) ||
  510. ( '[' === $value[0] && ']' === $value[ strlen( $value ) - 1 ] ) ||
  511. preg_match( '/^\(?function ?\(/', $value ) ) ) {
  512. $init_obj .= $key . ':' . $value . ',';
  513. continue;
  514. }
  515. $init_obj .= $key . ':"' . $value . '",';
  516. }
  517. $init_obj = '{' . trim( $init_obj, ' ,' ) . '}';
  518. $script = 'window.wpEditorL10n = {
  519. tinymce: {
  520. baseURL: ' . wp_json_encode( includes_url( 'js/tinymce' ) ) . ',
  521. suffix: ' . ( SCRIPT_DEBUG ? '""' : '".min"' ) . ',
  522. settings: ' . $init_obj . ',
  523. }
  524. }';
  525. $wp_scripts->add_inline_script( 'wp-block-library', $script, 'before' );
  526. }
  527. /**
  528. * Registers all the WordPress packages scripts.
  529. *
  530. * @since 5.0.0
  531. *
  532. * @param WP_Scripts $scripts WP_Scripts object.
  533. */
  534. function wp_default_packages( $scripts ) {
  535. wp_default_packages_vendor( $scripts );
  536. wp_register_development_scripts( $scripts );
  537. wp_register_tinymce_scripts( $scripts );
  538. wp_default_packages_scripts( $scripts );
  539. if ( did_action( 'init' ) ) {
  540. wp_default_packages_inline_scripts( $scripts );
  541. }
  542. }
  543. /**
  544. * Returns the suffix that can be used for the scripts.
  545. *
  546. * There are two suffix types, the normal one and the dev suffix.
  547. *
  548. * @since 5.0.0
  549. *
  550. * @param string $type The type of suffix to retrieve.
  551. * @return string The script suffix.
  552. */
  553. function wp_scripts_get_suffix( $type = '' ) {
  554. static $suffixes;
  555. if ( null === $suffixes ) {
  556. // Include an unmodified $wp_version.
  557. require ABSPATH . WPINC . '/version.php';
  558. $develop_src = false !== strpos( $wp_version, '-src' );
  559. if ( ! defined( 'SCRIPT_DEBUG' ) ) {
  560. define( 'SCRIPT_DEBUG', $develop_src );
  561. }
  562. $suffix = SCRIPT_DEBUG ? '' : '.min';
  563. $dev_suffix = $develop_src ? '' : '.min';
  564. $suffixes = array(
  565. 'suffix' => $suffix,
  566. 'dev_suffix' => $dev_suffix,
  567. );
  568. }
  569. if ( 'dev' === $type ) {
  570. return $suffixes['dev_suffix'];
  571. }
  572. return $suffixes['suffix'];
  573. }
  574. /**
  575. * Registers all WordPress scripts.
  576. *
  577. * Localizes some of them.
  578. * args order: `$scripts->add( 'handle', 'url', 'dependencies', 'query-string', 1 );`
  579. * when last arg === 1 queues the script for the footer
  580. *
  581. * @since 2.6.0
  582. *
  583. * @param WP_Scripts $scripts WP_Scripts object.
  584. */
  585. function wp_default_scripts( $scripts ) {
  586. $suffix = wp_scripts_get_suffix();
  587. $dev_suffix = wp_scripts_get_suffix( 'dev' );
  588. $guessurl = site_url();
  589. if ( ! $guessurl ) {
  590. $guessed_url = true;
  591. $guessurl = wp_guess_url();
  592. }
  593. $scripts->base_url = $guessurl;
  594. $scripts->content_url = defined( 'WP_CONTENT_URL' ) ? WP_CONTENT_URL : '';
  595. $scripts->default_version = get_bloginfo( 'version' );
  596. $scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' );
  597. $scripts->add( 'utils', "/wp-includes/js/utils$suffix.js" );
  598. did_action( 'init' ) && $scripts->localize(
  599. 'utils',
  600. 'userSettings',
  601. array(
  602. 'url' => (string) SITECOOKIEPATH,
  603. 'uid' => (string) get_current_user_id(),
  604. 'time' => (string) time(),
  605. 'secure' => (string) ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) ),
  606. )
  607. );
  608. $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array( 'jquery', 'hoverIntent', 'utils' ), false, 1 );
  609. $scripts->set_translations( 'common' );
  610. $scripts->add( 'wp-sanitize', "/wp-includes/js/wp-sanitize$suffix.js", array(), false, 1 );
  611. $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", array(), '1.6.1', 1 );
  612. $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", array(), false, 1 );
  613. did_action( 'init' ) && $scripts->localize(
  614. 'quicktags',
  615. 'quicktagsL10n',
  616. array(
  617. 'closeAllOpenTags' => __( 'Close all open tags' ),
  618. 'closeTags' => __( 'close tags' ),
  619. 'enterURL' => __( 'Enter the URL' ),
  620. 'enterImageURL' => __( 'Enter the URL of the image' ),
  621. 'enterImageDescription' => __( 'Enter a description of the image' ),
  622. 'textdirection' => __( 'text direction' ),
  623. 'toggleTextdirection' => __( 'Toggle Editor Text Direction' ),
  624. 'dfw' => __( 'Distraction-free writing mode' ),
  625. 'strong' => __( 'Bold' ),
  626. 'strongClose' => __( 'Close bold tag' ),
  627. 'em' => __( 'Italic' ),
  628. 'emClose' => __( 'Close italic tag' ),
  629. 'link' => __( 'Insert link' ),
  630. 'blockquote' => __( 'Blockquote' ),
  631. 'blockquoteClose' => __( 'Close blockquote tag' ),
  632. 'del' => __( 'Deleted text (strikethrough)' ),
  633. 'delClose' => __( 'Close deleted text tag' ),
  634. 'ins' => __( 'Inserted text' ),
  635. 'insClose' => __( 'Close inserted text tag' ),
  636. 'image' => __( 'Insert image' ),
  637. 'ul' => __( 'Bulleted list' ),
  638. 'ulClose' => __( 'Close bulleted list tag' ),
  639. 'ol' => __( 'Numbered list' ),
  640. 'olClose' => __( 'Close numbered list tag' ),
  641. 'li' => __( 'List item' ),
  642. 'liClose' => __( 'Close list item tag' ),
  643. 'code' => __( 'Code' ),
  644. 'codeClose' => __( 'Close code tag' ),
  645. 'more' => __( 'Insert Read More tag' ),
  646. )
  647. );
  648. $scripts->add( 'colorpicker', "/wp-includes/js/colorpicker$suffix.js", array( 'prototype' ), '3517m' );
  649. $scripts->add( 'editor', "/wp-admin/js/editor$suffix.js", array( 'utils', 'jquery' ), false, 1 );
  650. $scripts->add( 'clipboard', "/wp-includes/js/clipboard$suffix.js", array(), '2.0.10', 1 );
  651. $scripts->add( 'wp-ajax-response', "/wp-includes/js/wp-ajax-response$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
  652. did_action( 'init' ) && $scripts->localize(
  653. 'wp-ajax-response',
  654. 'wpAjax',
  655. array(
  656. 'noPerm' => __( 'Sorry, you are not allowed to do that.' ),
  657. 'broken' => __( 'Something went wrong.' ),
  658. )
  659. );
  660. $scripts->add( 'wp-api-request', "/wp-includes/js/api-request$suffix.js", array( 'jquery' ), false, 1 );
  661. // `wpApiSettings` is also used by `wp-api`, which depends on this script.
  662. did_action( 'init' ) && $scripts->localize(
  663. 'wp-api-request',
  664. 'wpApiSettings',
  665. array(
  666. 'root' => esc_url_raw( get_rest_url() ),
  667. 'nonce' => wp_installing() ? '' : wp_create_nonce( 'wp_rest' ),
  668. 'versionString' => 'wp/v2/',
  669. )
  670. );
  671. $scripts->add( 'wp-pointer', "/wp-includes/js/wp-pointer$suffix.js", array( 'jquery-ui-core' ), false, 1 );
  672. $scripts->set_translations( 'wp-pointer' );
  673. $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array( 'heartbeat' ), false, 1 );
  674. $scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array( 'jquery', 'wp-hooks' ), false, 1 );
  675. did_action( 'init' ) && $scripts->localize(
  676. 'heartbeat',
  677. 'heartbeatSettings',
  678. /**
  679. * Filters the Heartbeat settings.
  680. *
  681. * @since 3.6.0
  682. *
  683. * @param array $settings Heartbeat settings array.
  684. */
  685. apply_filters( 'heartbeat_settings', array() )
  686. );
  687. $scripts->add( 'wp-auth-check', "/wp-includes/js/wp-auth-check$suffix.js", array( 'heartbeat' ), false, 1 );
  688. $scripts->set_translations( 'wp-auth-check' );
  689. $scripts->add( 'wp-lists', "/wp-includes/js/wp-lists$suffix.js", array( 'wp-ajax-response', 'jquery-color' ), false, 1 );
  690. // WordPress no longer uses or bundles Prototype or script.aculo.us. These are now pulled from an external source.
  691. $scripts->add( 'prototype', 'https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js', array(), '1.7.1' );
  692. $scripts->add( 'scriptaculous-root', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/scriptaculous.js', array( 'prototype' ), '1.9.0' );
  693. $scripts->add( 'scriptaculous-builder', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/builder.js', array( 'scriptaculous-root' ), '1.9.0' );
  694. $scripts->add( 'scriptaculous-dragdrop', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/dragdrop.js', array( 'scriptaculous-builder', 'scriptaculous-effects' ), '1.9.0' );
  695. $scripts->add( 'scriptaculous-effects', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/effects.js', array( 'scriptaculous-root' ), '1.9.0' );
  696. $scripts->add( 'scriptaculous-slider', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/slider.js', array( 'scriptaculous-effects' ), '1.9.0' );
  697. $scripts->add( 'scriptaculous-sound', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/sound.js', array( 'scriptaculous-root' ), '1.9.0' );
  698. $scripts->add( 'scriptaculous-controls', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/controls.js', array( 'scriptaculous-root' ), '1.9.0' );
  699. $scripts->add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );
  700. // Not used in core, replaced by Jcrop.js.
  701. $scripts->add( 'cropper', '/wp-includes/js/crop/cropper.js', array( 'scriptaculous-dragdrop' ) );
  702. // jQuery.
  703. // The unminified jquery.js and jquery-migrate.js are included to facilitate debugging.
  704. $scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '3.6.0' );
  705. $scripts->add( 'jquery-core', "/wp-includes/js/jquery/jquery$suffix.js", array(), '3.6.0' );
  706. $scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '3.3.2' );
  707. // Full jQuery UI.
  708. // The build process in 1.12.1 has changed significantly.
  709. // In order to keep backwards compatibility, and to keep the optimized loading,
  710. // the source files were flattened and included with some modifications for AMD loading.
  711. // A notable change is that 'jquery-ui-core' now contains 'jquery-ui-position' and 'jquery-ui-widget'.
  712. $scripts->add( 'jquery-ui-core', "/wp-includes/js/jquery/ui/core$suffix.js", array( 'jquery' ), '1.13.1', 1 );
  713. $scripts->add( 'jquery-effects-core', "/wp-includes/js/jquery/ui/effect$suffix.js", array( 'jquery' ), '1.13.1', 1 );
  714. $scripts->add( 'jquery-effects-blind', "/wp-includes/js/jquery/ui/effect-blind$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  715. $scripts->add( 'jquery-effects-bounce', "/wp-includes/js/jquery/ui/effect-bounce$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  716. $scripts->add( 'jquery-effects-clip', "/wp-includes/js/jquery/ui/effect-clip$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  717. $scripts->add( 'jquery-effects-drop', "/wp-includes/js/jquery/ui/effect-drop$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  718. $scripts->add( 'jquery-effects-explode', "/wp-includes/js/jquery/ui/effect-explode$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  719. $scripts->add( 'jquery-effects-fade', "/wp-includes/js/jquery/ui/effect-fade$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  720. $scripts->add( 'jquery-effects-fold', "/wp-includes/js/jquery/ui/effect-fold$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  721. $scripts->add( 'jquery-effects-highlight', "/wp-includes/js/jquery/ui/effect-highlight$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  722. $scripts->add( 'jquery-effects-puff', "/wp-includes/js/jquery/ui/effect-puff$suffix.js", array( 'jquery-effects-core', 'jquery-effects-scale' ), '1.13.1', 1 );
  723. $scripts->add( 'jquery-effects-pulsate', "/wp-includes/js/jquery/ui/effect-pulsate$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  724. $scripts->add( 'jquery-effects-scale', "/wp-includes/js/jquery/ui/effect-scale$suffix.js", array( 'jquery-effects-core', 'jquery-effects-size' ), '1.13.1', 1 );
  725. $scripts->add( 'jquery-effects-shake', "/wp-includes/js/jquery/ui/effect-shake$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  726. $scripts->add( 'jquery-effects-size', "/wp-includes/js/jquery/ui/effect-size$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  727. $scripts->add( 'jquery-effects-slide', "/wp-includes/js/jquery/ui/effect-slide$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  728. $scripts->add( 'jquery-effects-transfer', "/wp-includes/js/jquery/ui/effect-transfer$suffix.js", array( 'jquery-effects-core' ), '1.13.1', 1 );
  729. // Widgets
  730. $scripts->add( 'jquery-ui-accordion', "/wp-includes/js/jquery/ui/accordion$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  731. $scripts->add( 'jquery-ui-autocomplete', "/wp-includes/js/jquery/ui/autocomplete$suffix.js", array( 'jquery-ui-menu', 'wp-a11y' ), '1.13.1', 1 );
  732. $scripts->add( 'jquery-ui-button', "/wp-includes/js/jquery/ui/button$suffix.js", array( 'jquery-ui-core', 'jquery-ui-controlgroup', 'jquery-ui-checkboxradio' ), '1.13.1', 1 );
  733. $scripts->add( 'jquery-ui-datepicker', "/wp-includes/js/jquery/ui/datepicker$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  734. $scripts->add( 'jquery-ui-dialog', "/wp-includes/js/jquery/ui/dialog$suffix.js", array( 'jquery-ui-resizable', 'jquery-ui-draggable', 'jquery-ui-button' ), '1.13.1', 1 );
  735. $scripts->add( 'jquery-ui-menu', "/wp-includes/js/jquery/ui/menu$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  736. $scripts->add( 'jquery-ui-mouse', "/wp-includes/js/jquery/ui/mouse$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  737. $scripts->add( 'jquery-ui-progressbar', "/wp-includes/js/jquery/ui/progressbar$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  738. $scripts->add( 'jquery-ui-selectmenu', "/wp-includes/js/jquery/ui/selectmenu$suffix.js", array( 'jquery-ui-menu' ), '1.13.1', 1 );
  739. $scripts->add( 'jquery-ui-slider', "/wp-includes/js/jquery/ui/slider$suffix.js", array( 'jquery-ui-mouse' ), '1.13.1', 1 );
  740. $scripts->add( 'jquery-ui-spinner', "/wp-includes/js/jquery/ui/spinner$suffix.js", array( 'jquery-ui-button' ), '1.13.1', 1 );
  741. $scripts->add( 'jquery-ui-tabs', "/wp-includes/js/jquery/ui/tabs$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  742. $scripts->add( 'jquery-ui-tooltip', "/wp-includes/js/jquery/ui/tooltip$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  743. // New in 1.12.1
  744. $scripts->add( 'jquery-ui-checkboxradio', "/wp-includes/js/jquery/ui/checkboxradio$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  745. $scripts->add( 'jquery-ui-controlgroup', "/wp-includes/js/jquery/ui/controlgroup$suffix.js", array( 'jquery-ui-core' ), '1.13.1', 1 );
  746. // Interactions
  747. $scripts->add( 'jquery-ui-draggable', "/wp-includes/js/jquery/ui/draggable$suffix.js", array( 'jquery-ui-mouse' ), '1.13.1', 1 );
  748. $scripts->add( 'jquery-ui-droppable', "/wp-includes/js/jquery/ui/droppable$suffix.js", array( 'jquery-ui-draggable' ), '1.13.1', 1 );
  749. $scripts->add( 'jquery-ui-resizable', "/wp-includes/js/jquery/ui/resizable$suffix.js", array( 'jquery-ui-mouse' ), '1.13.1', 1 );
  750. $scripts->add( 'jquery-ui-selectable', "/wp-includes/js/jquery/ui/selectable$suffix.js", array( 'jquery-ui-mouse' ), '1.13.1', 1 );
  751. $scripts->add( 'jquery-ui-sortable', "/wp-includes/js/jquery/ui/sortable$suffix.js", array( 'jquery-ui-mouse' ), '1.13.1', 1 );
  752. // As of 1.12.1 `jquery-ui-position` and `jquery-ui-widget` are part of `jquery-ui-core`.
  753. // Listed here for back-compat.
  754. $scripts->add( 'jquery-ui-position', false, array( 'jquery-ui-core' ), '1.13.1', 1 );
  755. $scripts->add( 'jquery-ui-widget', false, array( 'jquery-ui-core' ), '1.13.1', 1 );
  756. // Strings for 'jquery-ui-autocomplete' live region messages.
  757. did_action( 'init' ) && $scripts->localize(
  758. 'jquery-ui-autocomplete',
  759. 'uiAutocompleteL10n',
  760. array(
  761. 'noResults' => __( 'No results found.' ),
  762. /* translators: Number of results found when using jQuery UI Autocomplete. */
  763. 'oneResult' => __( '1 result found. Use up and down arrow keys to navigate.' ),
  764. /* translators: %d: Number of results found when using jQuery UI Autocomplete. */
  765. 'manyResults' => __( '%d results found. Use up and down arrow keys to navigate.' ),
  766. 'itemSelected' => __( 'Item selected.' ),
  767. )
  768. );
  769. // Deprecated, not used in core, most functionality is included in jQuery 1.3.
  770. $scripts->add( 'jquery-form', "/wp-includes/js/jquery/jquery.form$suffix.js", array( 'jquery' ), '4.3.0', 1 );
  771. // jQuery plugins.
  772. $scripts->add( 'jquery-color', '/wp-includes/js/jquery/jquery.color.min.js', array( 'jquery' ), '2.2.0', 1 );
  773. $scripts->add( 'schedule', '/wp-includes/js/jquery/jquery.schedule.js', array( 'jquery' ), '20m', 1 );
  774. $scripts->add( 'jquery-query', '/wp-includes/js/jquery/jquery.query.js', array( 'jquery' ), '2.2.3', 1 );
  775. $scripts->add( 'jquery-serialize-object', '/wp-includes/js/jquery/jquery.serialize-object.js', array( 'jquery' ), '0.2-wp', 1 );
  776. $scripts->add( 'jquery-hotkeys', "/wp-includes/js/jquery/jquery.hotkeys$suffix.js", array( 'jquery' ), '0.0.2m', 1 );
  777. $scripts->add( 'jquery-table-hotkeys', "/wp-includes/js/jquery/jquery.table-hotkeys$suffix.js", array( 'jquery', 'jquery-hotkeys' ), false, 1 );
  778. $scripts->add( 'jquery-touch-punch', '/wp-includes/js/jquery/jquery.ui.touch-punch.js', array( 'jquery-ui-core', 'jquery-ui-mouse' ), '0.2.2', 1 );
  779. // Not used any more, registered for backward compatibility.
  780. $scripts->add( 'suggest', "/wp-includes/js/jquery/suggest$suffix.js", array( 'jquery' ), '1.1-20110113', 1 );
  781. // Masonry v2 depended on jQuery. v3 does not. The older jquery-masonry handle is a shiv.
  782. // It sets jQuery as a dependency, as the theme may have been implicitly loading it this way.
  783. $scripts->add( 'imagesloaded', '/wp-includes/js/imagesloaded.min.js', array(), '4.1.4', 1 );
  784. $scripts->add( 'masonry', '/wp-includes/js/masonry.min.js', array( 'imagesloaded' ), '4.2.2', 1 );
  785. $scripts->add( 'jquery-masonry', '/wp-includes/js/jquery/jquery.masonry.min.js', array( 'jquery', 'masonry' ), '3.1.2b', 1 );
  786. $scripts->add( 'thickbox', '/wp-includes/js/thickbox/thickbox.js', array( 'jquery' ), '3.1-20121105', 1 );
  787. did_action( 'init' ) && $scripts->localize(
  788. 'thickbox',
  789. 'thickboxL10n',
  790. array(
  791. 'next' => __( 'Next &gt;' ),
  792. 'prev' => __( '&lt; Prev' ),
  793. 'image' => __( 'Image' ),
  794. 'of' => __( 'of' ),
  795. 'close' => __( 'Close' ),
  796. 'noiframes' => __( 'This feature requires inline frames. You have iframes disabled or your browser does not support them.' ),
  797. 'loadingAnimation' => includes_url( 'js/thickbox/loadingAnimation.gif' ),
  798. )
  799. );
  800. // Not used in core, replaced by imgAreaSelect.
  801. $scripts->add( 'jcrop', '/wp-includes/js/jcrop/jquery.Jcrop.min.js', array( 'jquery' ), '0.9.15' );
  802. $scripts->add( 'swfobject', '/wp-includes/js/swfobject.js', array(), '2.2-20120417' );
  803. // Error messages for Plupload.
  804. $uploader_l10n = array(
  805. 'queue_limit_exceeded' => __( 'You have attempted to queue too many files.' ),
  806. /* translators: %s: File name. */
  807. 'file_exceeds_size_limit' => __( '%s exceeds the maximum upload size for this site.' ),
  808. 'zero_byte_file' => __( 'This file is empty. Please try another.' ),
  809. 'invalid_filetype' => __( 'Sorry, you are not allowed to upload this file type.' ),
  810. 'not_an_image' => __( 'This file is not an image. Please try another.' ),
  811. 'image_memory_exceeded' => __( 'Memory exceeded. Please try another smaller file.' ),
  812. 'image_dimensions_exceeded' => __( 'This is larger than the maximum size. Please try another.' ),
  813. 'default_error' => __( 'An error occurred in the upload. Please try again later.' ),
  814. 'missing_upload_url' => __( 'There was a configuration error. Please contact the server administrator.' ),
  815. 'upload_limit_exceeded' => __( 'You may only upload 1 file.' ),
  816. 'http_error' => __( 'Unexpected response from the server. The file may have been uploaded successfully. Check in the Media Library or reload the page.' ),
  817. 'http_error_image' => __( 'The server cannot process the image. This can happen if the server is busy or does not have enough resources to complete the task. Uploading a smaller image may help. Suggested maximum size is 2560 pixels.' ),
  818. 'upload_failed' => __( 'Upload failed.' ),
  819. /* translators: 1: Opening link tag, 2: Closing link tag. */
  820. 'big_upload_failed' => __( 'Please try uploading this file with the %1$sbrowser uploader%2$s.' ),
  821. /* translators: %s: File name. */
  822. 'big_upload_queued' => __( '%s exceeds the maximum upload size for the multi-file uploader when used in your browser.' ),
  823. 'io_error' => __( 'IO error.' ),
  824. 'security_error' => __( 'Security error.' ),
  825. 'file_cancelled' => __( 'File canceled.' ),
  826. 'upload_stopped' => __( 'Upload stopped.' ),
  827. 'dismiss' => __( 'Dismiss' ),
  828. 'crunching' => __( 'Crunching&hellip;' ),
  829. 'deleted' => __( 'moved to the Trash.' ),
  830. /* translators: %s: File name. */
  831. 'error_uploading' => __( '&#8220;%s&#8221; has failed to upload.' ),
  832. 'unsupported_image' => __( 'This image cannot be displayed in a web browser. For best results convert it to JPEG before uploading.' ),
  833. 'noneditable_image' => __( 'This image cannot be processed by the web server. Convert it to JPEG or PNG before uploading.' ),
  834. 'file_url_copied' => __( 'The file URL has been copied to your clipboard' ),
  835. );
  836. $scripts->add( 'moxiejs', "/wp-includes/js/plupload/moxie$suffix.js", array(), '1.3.5' );
  837. $scripts->add( 'plupload', "/wp-includes/js/plupload/plupload$suffix.js", array( 'moxiejs' ), '2.1.9' );
  838. // Back compat handles:
  839. foreach ( array( 'all', 'html5', 'flash', 'silverlight', 'html4' ) as $handle ) {
  840. $scripts->add( "plupload-$handle", false, array( 'plupload' ), '2.1.1' );
  841. }
  842. $scripts->add( 'plupload-handlers', "/wp-includes/js/plupload/handlers$suffix.js", array( 'clipboard', 'jquery', 'plupload', 'underscore', 'wp-a11y', 'wp-i18n' ) );
  843. did_action( 'init' ) && $scripts->localize( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
  844. $scripts->add( 'wp-plupload', "/wp-includes/js/plupload/wp-plupload$suffix.js", array( 'plupload', 'jquery', 'json2', 'media-models' ), false, 1 );
  845. did_action( 'init' ) && $scripts->localize( 'wp-plupload', 'pluploadL10n', $uploader_l10n );
  846. // Keep 'swfupload' for back-compat.
  847. $scripts->add( 'swfupload', '/wp-includes/js/swfupload/swfupload.js', array(), '2201-20110113' );
  848. $scripts->add( 'swfupload-all', false, array( 'swfupload' ), '2201' );
  849. $scripts->add( 'swfupload-handlers', "/wp-includes/js/swfupload/handlers$suffix.js", array( 'swfupload-all', 'jquery' ), '2201-20110524' );
  850. did_action( 'init' ) && $scripts->localize( 'swfupload-handlers', 'swfuploadL10n', $uploader_l10n );
  851. $scripts->add( 'comment-reply', "/wp-includes/js/comment-reply$suffix.js", array(), false, 1 );
  852. $scripts->add( 'json2', "/wp-includes/js/json2$suffix.js", array(), '2015-05-03' );
  853. did_action( 'init' ) && $scripts->add_data( 'json2', 'conditional', 'lt IE 8' );
  854. $scripts->add( 'underscore', "/wp-includes/js/underscore$dev_suffix.js", array(), '1.13.3', 1 );
  855. $scripts->add( 'backbone', "/wp-includes/js/backbone$dev_suffix.js", array( 'underscore', 'jquery' ), '1.4.1', 1 );
  856. $scripts->add( 'wp-util', "/wp-includes/js/wp-util$suffix.js", array( 'underscore', 'jquery' ), false, 1 );
  857. did_action( 'init' ) && $scripts->localize(
  858. 'wp-util',
  859. '_wpUtilSettings',
  860. array(
  861. 'ajax' => array(
  862. 'url' => admin_url( 'admin-ajax.php', 'relative' ),
  863. ),
  864. )
  865. );
  866. $scripts->add( 'wp-backbone', "/wp-includes/js/wp-backbone$suffix.js", array( 'backbone', 'wp-util' ), false, 1 );
  867. $scripts->add( 'revisions', "/wp-admin/js/revisions$suffix.js", array( 'wp-backbone', 'jquery-ui-slider', 'hoverIntent' ), false, 1 );
  868. $scripts->add( 'imgareaselect', "/wp-includes/js/imgareaselect/jquery.imgareaselect$suffix.js", array( 'jquery' ), false, 1 );
  869. $scripts->add( 'mediaelement', false, array( 'jquery', 'mediaelement-core', 'mediaelement-migrate' ), '4.2.16', 1 );
  870. $scripts->add( 'mediaelement-core', "/wp-includes/js/mediaelement/mediaelement-and-player$suffix.js", array(), '4.2.16', 1 );
  871. $scripts->add( 'mediaelement-migrate', "/wp-includes/js/mediaelement/mediaelement-migrate$suffix.js", array(), false, 1 );
  872. did_action( 'init' ) && $scripts->add_inline_script(
  873. 'mediaelement-core',
  874. sprintf(
  875. 'var mejsL10n = %s;',
  876. wp_json_encode(
  877. array(
  878. 'language' => strtolower( strtok( determine_locale(), '_-' ) ),
  879. 'strings' => array(
  880. 'mejs.download-file' => __( 'Download File' ),
  881. 'mejs.install-flash' => __( 'You are using a browser that does not have Flash player enabled or installed. Please turn on your Flash player plugin or download the latest version from https://get.adobe.com/flashplayer/' ),
  882. 'mejs.fullscreen' => __( 'Fullscreen' ),
  883. 'mejs.play' => __( 'Play' ),
  884. 'mejs.pause' => __( 'Pause' ),
  885. 'mejs.time-slider' => __( 'Time Slider' ),
  886. 'mejs.time-help-text' => __( 'Use Left/Right Arrow keys to advance one second, Up/Down arrows to advance ten seconds.' ),
  887. 'mejs.live-broadcast' => __( 'Live Broadcast' ),
  888. 'mejs.volume-help-text' => __( 'Use Up/Down Arrow keys to increase or decrease volume.' ),
  889. 'mejs.unmute' => __( 'Unmute' ),
  890. 'mejs.mute' => __( 'Mute' ),
  891. 'mejs.volume-slider' => __( 'Volume Slider' ),
  892. 'mejs.video-player' => __( 'Video Player' ),
  893. 'mejs.audio-player' => __( 'Audio Player' ),
  894. 'mejs.captions-subtitles' => __( 'Captions/Subtitles' ),
  895. 'mejs.captions-chapters' => __( 'Chapters' ),
  896. 'mejs.none' => __( 'None' ),
  897. 'mejs.afrikaans' => __( 'Afrikaans' ),
  898. 'mejs.albanian' => __( 'Albanian' ),
  899. 'mejs.arabic' => __( 'Arabic' ),
  900. 'mejs.belarusian' => __( 'Belarusian' ),
  901. 'mejs.bulgarian' => __( 'Bulgarian' ),
  902. 'mejs.catalan' => __( 'Catalan' ),
  903. 'mejs.chinese' => __( 'Chinese' ),
  904. 'mejs.chinese-simplified' => __( 'Chinese (Simplified)' ),
  905. 'mejs.chinese-traditional' => __( 'Chinese (Traditional)' ),
  906. 'mejs.croatian' => __( 'Croatian' ),
  907. 'mejs.czech' => __( 'Czech' ),
  908. 'mejs.danish' => __( 'Danish' ),
  909. 'mejs.dutch' => __( 'Dutch' ),
  910. 'mejs.english' => __( 'English' ),
  911. 'mejs.estonian' => __( 'Estonian' ),
  912. 'mejs.filipino' => __( 'Filipino' ),
  913. 'mejs.finnish' => __( 'Finnish' ),
  914. 'mejs.french' => __( 'French' ),
  915. 'mejs.galician' => __( 'Galician' ),
  916. 'mejs.german' => __( 'German' ),
  917. 'mejs.greek' => __( 'Greek' ),
  918. 'mejs.haitian-creole' => __( 'Haitian Creole' ),
  919. 'mejs.hebrew' => __( 'Hebrew' ),
  920. 'mejs.hindi' => __( 'Hindi' ),
  921. 'mejs.hungarian' => __( 'Hungarian' ),
  922. 'mejs.icelandic' => __( 'Icelandic' ),
  923. 'mejs.indonesian' => __( 'Indonesian' ),
  924. 'mejs.irish' => __( 'Irish' ),
  925. 'mejs.italian' => __( 'Italian' ),
  926. 'mejs.japanese' => __( 'Japanese' ),
  927. 'mejs.korean' => __( 'Korean' ),
  928. 'mejs.latvian' => __( 'Latvian' ),
  929. 'mejs.lithuanian' => __( 'Lithuanian' ),
  930. 'mejs.macedonian' => __( 'Macedonian' ),
  931. 'mejs.malay' => __( 'Malay' ),
  932. 'mejs.maltese' => __( 'Maltese' ),
  933. 'mejs.norwegian' => __( 'Norwegian' ),
  934. 'mejs.persian' => __( 'Persian' ),
  935. 'mejs.polish' => __( 'Polish' ),
  936. 'mejs.portuguese' => __( 'Portuguese' ),
  937. 'mejs.romanian' => __( 'Romanian' ),
  938. 'mejs.russian' => __( 'Russian' ),
  939. 'mejs.serbian' => __( 'Serbian' ),
  940. 'mejs.slovak' => __( 'Slovak' ),
  941. 'mejs.slovenian' => __( 'Slovenian' ),
  942. 'mejs.spanish' => __( 'Spanish' ),
  943. 'mejs.swahili' => __( 'Swahili' ),
  944. 'mejs.swedish' => __( 'Swedish' ),
  945. 'mejs.tagalog' => __( 'Tagalog' ),
  946. 'mejs.thai' => __( 'Thai' ),
  947. 'mejs.turkish' => __( 'Turkish' ),
  948. 'mejs.ukrainian' => __( 'Ukrainian' ),
  949. 'mejs.vietnamese' => __( 'Vietnamese' ),
  950. 'mejs.welsh' => __( 'Welsh' ),
  951. 'mejs.yiddish' => __( 'Yiddish' ),
  952. ),
  953. )
  954. )
  955. ),
  956. 'before'
  957. );
  958. $scripts->add( 'mediaelement-vimeo', '/wp-includes/js/mediaelement/renderers/vimeo.min.js', array( 'mediaelement' ), '4.2.16', 1 );
  959. $scripts->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.js", array( 'mediaelement' ), false, 1 );
  960. $mejs_settings = array(
  961. 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ),
  962. 'classPrefix' => 'mejs-',
  963. 'stretching' => 'responsive',
  964. );
  965. did_action( 'init' ) && $scripts->localize(
  966. 'mediaelement',
  967. '_wpmejsSettings',
  968. /**
  969. * Filters the MediaElement configuration settings.
  970. *
  971. * @since 4.4.0
  972. *
  973. * @param array $mejs_settings MediaElement settings array.
  974. */
  975. apply_filters( 'mejs_settings', $mejs_settings )
  976. );
  977. $scripts->add( 'wp-codemirror', '/wp-includes/js/codemirror/codemirror.min.js', array(), '5.29.1-alpha-ee20357' );
  978. $scripts->add( 'csslint', '/wp-includes/js/codemirror/csslint.js', array(), '1.0.5' );
  979. $scripts->add( 'esprima', '/wp-includes/js/codemirror/esprima.js', array(), '4.0.0' );
  980. $scripts->add( 'jshint', '/wp-includes/js/codemirror/fakejshint.js', array( 'esprima' ), '2.9.5' );
  981. $scripts->add( 'jsonlint', '/wp-includes/js/codemirror/jsonlint.js', array(), '1.6.2' );
  982. $scripts->add( 'htmlhint', '/wp-includes/js/codemirror/htmlhint.js', array(), '0.9.14-xwp' );
  983. $scripts->add( 'htmlhint-kses', '/wp-includes/js/codemirror/htmlhint-kses.js', array( 'htmlhint' ) );
  984. $scripts->add( 'code-editor', "/wp-admin/js/code-editor$suffix.js", array( 'jquery', 'wp-codemirror', 'underscore' ) );
  985. $scripts->add( 'wp-theme-plugin-editor', "/wp-admin/js/theme-plugin-editor$suffix.js", array( 'common', 'wp-util', 'wp-sanitize', 'jquery', 'jquery-ui-core', 'wp-a11y', 'underscore' ) );
  986. $scripts->set_translations( 'wp-theme-plugin-editor' );
  987. $scripts->add( 'wp-playlist', "/wp-includes/js/mediaelement/wp-playlist$suffix.js", array( 'wp-util', 'backbone', 'mediaelement' ), false, 1 );
  988. $scripts->add( 'zxcvbn-async', "/wp-includes/js/zxcvbn-async$suffix.js", array(), '1.0' );
  989. did_action( 'init' ) && $scripts->localize(
  990. 'zxcvbn-async',
  991. '_zxcvbnSettings',
  992. array(
  993. 'src' => empty( $guessed_url ) ? includes_url( '/js/zxcvbn.min.js' ) : $scripts->base_url . '/wp-includes/js/zxcvbn.min.js',
  994. )
  995. );
  996. $scripts->add( 'password-strength-meter', "/wp-admin/js/password-strength-meter$suffix.js", array( 'jquery', 'zxcvbn-async' ), false, 1 );
  997. did_action( 'init' ) && $scripts->localize(
  998. 'password-strength-meter',
  999. 'pwsL10n',
  1000. array(
  1001. 'unknown' => _x( 'Password strength unknown', 'password strength' ),
  1002. 'short' => _x( 'Very weak', 'password strength' ),
  1003. 'bad' => _x( 'Weak', 'password strength' ),
  1004. 'good' => _x( 'Medium', 'password strength' ),
  1005. 'strong' => _x( 'Strong', 'password strength' ),
  1006. 'mismatch' => _x( 'Mismatch', 'password mismatch' ),
  1007. )
  1008. );
  1009. $scripts->set_translations( 'password-strength-meter' );
  1010. $scripts->add( 'application-passwords', "/wp-admin/js/application-passwords$suffix.js", array( 'jquery', 'wp-util', 'wp-api-request', 'wp-date', 'wp-i18n', 'wp-hooks' ), false, 1 );
  1011. $scripts->set_translations( 'application-passwords' );
  1012. $scripts->add( 'auth-app', "/wp-admin/js/auth-app$suffix.js", array( 'jquery', 'wp-api-request', 'wp-i18n', 'wp-hooks' ), false, 1 );
  1013. $scripts->set_translations( 'auth-app' );
  1014. $scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'jquery', 'password-strength-meter', 'wp-util' ), false, 1 );
  1015. $scripts->set_translations( 'user-profile' );
  1016. $user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : 0;
  1017. did_action( 'init' ) && $scripts->localize(
  1018. 'user-profile',
  1019. 'userProfileL10n',
  1020. array(
  1021. 'user_id' => $user_id,
  1022. 'nonce' => wp_installing() ? '' : wp_create_nonce( 'reset-password-for-' . $user_id ),
  1023. )
  1024. );
  1025. $scripts->add( 'language-chooser', "/wp-admin/js/language-chooser$suffix.js", array( 'jquery' ), false, 1 );
  1026. $scripts->add( 'user-suggest', "/wp-admin/js/user-suggest$suffix.js", array( 'jquery-ui-autocomplete' ), false, 1 );
  1027. $scripts->add( 'admin-bar', "/wp-includes/js/admin-bar$suffix.js", array( 'hoverintent-js' ), false, 1 );
  1028. $scripts->add( 'wplink', "/wp-includes/js/wplink$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
  1029. did_action( 'init' ) && $scripts->localize(
  1030. 'wplink',
  1031. 'wpLinkL10n',
  1032. array(
  1033. 'title' => __( 'Insert/edit link' ),
  1034. 'update' => __( 'Update' ),
  1035. 'save' => __( 'Add Link' ),
  1036. 'noTitle' => __( '(no title)' ),
  1037. 'noMatchesFound' => __( 'No results found.' ),
  1038. 'linkSelected' => __( 'Link selected.' ),
  1039. 'linkInserted' => __( 'Link inserted.' ),
  1040. /* translators: Minimum input length in characters to start searching posts in the "Insert/edit link" modal. */
  1041. 'minInputLength' => (int) _x( '3', 'minimum input length for searching post links' ),
  1042. )
  1043. );
  1044. $scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 );
  1045. $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 );
  1046. $scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 );
  1047. $scripts->add( 'hoverIntent', "/wp-includes/js/hoverIntent$suffix.js", array( 'jquery' ), '1.10.2', 1 );
  1048. // JS-only version of hoverintent (no dependencies).
  1049. $scripts->add( 'hoverintent-js', '/wp-includes/js/hoverintent-js.min.js', array(), '2.2.1', 1 );
  1050. $scripts->add( 'customize-base', "/wp-includes/js/customize-base$suffix.js", array( 'jquery', 'json2', 'underscore' ), false, 1 );
  1051. $scripts->add( 'customize-loader', "/wp-includes/js/customize-loader$suffix.js", array( 'customize-base' ), false, 1 );
  1052. $scripts->add( 'customize-preview', "/wp-includes/js/customize-preview$suffix.js", array( 'wp-a11y', 'customize-base' ), false, 1 );
  1053. $scripts->add( 'customize-models', '/wp-includes/js/customize-models.js', array( 'underscore', 'backbone' ), false, 1 );
  1054. $scripts->add( 'customize-views', '/wp-includes/js/customize-views.js', array( 'jquery', 'underscore', 'imgareaselect', 'customize-models', 'media-editor', 'media-views' ), false, 1 );
  1055. $scripts->add( 'customize-controls', "/wp-admin/js/customize-controls$suffix.js", array( 'customize-base', 'wp-a11y', 'wp-util', 'jquery-ui-core' ), false, 1 );
  1056. did_action( 'init' ) && $scripts->localize(
  1057. 'customize-controls',
  1058. '_wpCustomizeControlsL10n',
  1059. array(
  1060. 'activate' => __( 'Activate &amp; Publish' ),
  1061. 'save' => __( 'Save &amp; Publish' ), // @todo Remove as not required.
  1062. 'publish' => __( 'Publish' ),
  1063. 'published' => __( 'Published' ),
  1064. 'saveDraft' => __( 'Save Draft' ),
  1065. 'draftSaved' => __( 'Draft Saved' ),
  1066. 'updating' => __( 'Updating' ),
  1067. 'schedule' => _x( 'Schedule', 'customizer changeset action/button label' ),
  1068. 'scheduled' => _x( 'Scheduled', 'customizer changeset status' ),
  1069. 'invalid' => __( 'Invalid' ),
  1070. 'saveBeforeShare' => __( 'Please save your changes in order to share the preview.' ),
  1071. 'futureDateError' => __( 'You must supply a future date to schedule.' ),
  1072. 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),
  1073. 'saved' => __( 'Saved' ),
  1074. 'cancel' => __( 'Cancel' ),
  1075. 'close' => __( 'Close' ),
  1076. 'action' => __( 'Action' ),
  1077. 'discardChanges' => __( 'Discard changes' ),
  1078. 'cheatin' => __( 'Something went wrong.' ),
  1079. 'notAllowedHeading' => __( 'You need a higher level of permission.' ),
  1080. 'notAllowed' => __( 'Sorry, you are not allowed to customize this site.' ),
  1081. 'previewIframeTitle' => __( 'Site Preview' ),
  1082. 'loginIframeTitle' => __( 'Session expired' ),
  1083. 'collapseSidebar' => _x( 'Hide Controls', 'label for hide controls button without length constraints' ),
  1084. 'expandSidebar' => _x( 'Show Controls', 'label for hide controls button without length constraints' ),
  1085. 'untitledBlogName' => __( '(Untitled)' ),
  1086. 'unknownRequestFail' => __( 'Looks like something&#8217;s gone wrong. Wait a couple seconds, and then try again.' ),
  1087. 'themeDownloading' => __( 'Downloading your new theme&hellip;' ),
  1088. 'themePreviewWait' => __( 'Setting up your live preview. This may take a bit.' ),
  1089. 'revertingChanges' => __( 'Reverting unpublished changes&hellip;' ),
  1090. 'trashConfirm' => __( 'Are you sure you want to discard your unpublished changes?' ),
  1091. /* translators: %s: Display name of the user who has taken over the changeset in customizer. */
  1092. 'takenOverMessage' => __( '%s has taken over and is currently customizing.' ),
  1093. /* translators: %s: URL to the Customizer to load the autosaved version. */
  1094. 'autosaveNotice' => __( 'There is a more recent autosave of your changes than the one you are previewing. <a href="%s">Restore the autosave</a>' ),
  1095. 'videoHeaderNotice' => __( 'This theme does not support video headers on this page. Navigate to the front page or another page that supports video headers.' ),
  1096. // Used for overriding the file types allowed in Plupload.
  1097. 'allowedFiles' => __( 'Allowed Files' ),
  1098. 'customCssError' => array(
  1099. /* translators: %d: Error count. */
  1100. 'singular' => _n( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.', 1 ),
  1101. /* translators: %d: Error count. */
  1102. 'plural' => _n( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.', 2 ),
  1103. // @todo This is lacking, as some languages have a dedicated dual form. For proper handling of plurals in JS, see #20491.
  1104. ),
  1105. 'pageOnFrontError' => __( 'Homepage and posts page must be different.' ),
  1106. 'saveBlockedError' => array(
  1107. /* translators: %s: Number of invalid settings. */
  1108. 'singular' => _n( 'Unable to save due to %s invalid setting.', 'Unable to save due to %s invalid settings.', 1 ),
  1109. /* translators: %s: Number of invalid settings. */
  1110. 'plural' => _n( 'Unable to save due to %s invalid setting.', 'Unable to save due to %s invalid settings.', 2 ),
  1111. // @todo This is lacking, as some languages have a dedicated dual form. For proper handling of plurals in JS, see #20491.
  1112. ),
  1113. 'scheduleDescription' => __( 'Schedule your customization changes to publish ("go live") at a future date.' ),
  1114. 'themePreviewUnavailable' => __( 'Sorry, you cannot preview new themes when you have changes scheduled or saved as a draft. Please publish your changes, or wait until they publish to preview new themes.' ),
  1115. 'themeInstallUnavailable' => sprintf(
  1116. /* translators: %s: URL to Add Themes admin screen. */
  1117. __( 'You will not be able to install new themes from here yet since your install requires SFTP credentials. For now, please <a href="%s">add themes in the admin</a>.' ),
  1118. esc_url( admin_url( 'theme-install.php' ) )
  1119. ),
  1120. 'publishSettings' => __( 'Publish Settings' ),
  1121. 'invalidDate' => __( 'Invalid date.' ),
  1122. 'invalidValue' => __( 'Invalid value.' ),
  1123. 'blockThemeNotification' => sprintf(
  1124. /* translators: 1: Link to Site Editor documentation on HelpHub, 2: HTML button. */
  1125. __( 'Hurray! Your theme supports Full Site Editing with blocks. <a href="%1$s">Tell me more</a>. %2$s' ),
  1126. __( 'https://wordpress.org/support/article/site-editor/' ),
  1127. sprintf(
  1128. '<button type="button" data-action="%1$s" class="button switch-to-editor">%2$s</button>',
  1129. esc_url( admin_url( 'site-editor.php' ) ),
  1130. __( 'Use Site Editor' )
  1131. )
  1132. ),
  1133. )
  1134. );
  1135. $scripts->add( 'customize-selective-refresh', "/wp-includes/js/customize-selective-refresh$suffix.js", array( 'jquery', 'wp-util', 'customize-preview' ), false, 1 );
  1136. $scripts->add( 'customize-widgets', "/wp-admin/js/customize-widgets$suffix.js", array( 'jquery', 'jquery-ui-sortable', 'jquery-ui-droppable', 'wp-backbone', 'customize-controls' ), false, 1 );
  1137. $scripts->add( 'customize-preview-widgets', "/wp-includes/js/customize-preview-widgets$suffix.js", array( 'jquery', 'wp-util', 'customize-preview', 'customize-selective-refresh' ), false, 1 );
  1138. $scripts->add( 'customize-nav-menus', "/wp-admin/js/customize-nav-menus$suffix.js", array( 'jquery', 'wp-backbone', 'customize-controls', 'accordion', 'nav-menu', 'wp-sanitize' ), false, 1 );
  1139. $scripts->add( 'customize-preview-nav-menus', "/wp-includes/js/customize-preview-nav-menus$suffix.js", array( 'jquery', 'wp-util', 'customize-preview', 'customize-selective-refresh' ), false, 1 );
  1140. $scripts->add( 'wp-custom-header', "/wp-includes/js/wp-custom-header$suffix.js", array( 'wp-a11y' ), false, 1 );
  1141. $scripts->add( 'accordion', "/wp-admin/js/accordion$suffix.js", array( 'jquery' ), false, 1 );
  1142. $scripts->add( 'shortcode', "/wp-includes/js/shortcode$suffix.js", array( 'underscore' ), false, 1 );
  1143. $scripts->add( 'media-models', "/wp-includes/js/media-models$suffix.js", array( 'wp-backbone' ), false, 1 );
  1144. did_action( 'init' ) && $scripts->localize(
  1145. 'media-models',
  1146. '_wpMediaModelsL10n',
  1147. array(
  1148. 'settings' => array(
  1149. 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
  1150. 'post' => array( 'id' => 0 ),
  1151. ),
  1152. )
  1153. );
  1154. $scripts->add( 'wp-embed', "/wp-includes/js/wp-embed$suffix.js", array(), false, 1 );
  1155. // To enqueue media-views or media-editor, call wp_enqueue_media().
  1156. // Both rely on numerous settings, styles, and templates to operate correctly.
  1157. $scripts->add( 'media-views', "/wp-includes/js/media-views$suffix.js", array( 'utils', 'media-models', 'wp-plupload', 'jquery-ui-sortable', 'wp-mediaelement', 'wp-api-request', 'wp-a11y', 'clipboard' ), false, 1 );
  1158. $scripts->set_translations( 'media-views' );
  1159. $scripts->add( 'media-editor', "/wp-includes/js/media-editor$suffix.js", array( 'shortcode', 'media-views' ), false, 1 );
  1160. $scripts->set_translations( 'media-editor' );
  1161. $scripts->add( 'media-audiovideo', "/wp-includes/js/media-audiovideo$suffix.js", array( 'media-editor' ), false, 1 );
  1162. $scripts->add( 'mce-view', "/wp-includes/js/mce-view$suffix.js", array( 'shortcode', 'jquery', 'media-views', 'media-audiovideo' ), false, 1 );
  1163. $scripts->add( 'wp-api', "/wp-includes/js/wp-api$suffix.js", array( 'jquery', 'backbone', 'underscore', 'wp-api-request' ), false, 1 );
  1164. if ( is_admin() ) {
  1165. $scripts->add( 'admin-tags', "/wp-admin/js/tags$suffix.js", array( 'jquery', 'wp-ajax-response' ), false, 1 );
  1166. $scripts->set_translations( 'admin-tags' );
  1167. $scripts->add( 'admin-comments', "/wp-admin/js/edit-comments$suffix.js", array( 'wp-lists', 'quicktags', 'jquery-query' ), false, 1 );
  1168. $scripts->set_translations( 'admin-comments' );
  1169. did_action( 'init' ) && $scripts->localize(
  1170. 'admin-comments',
  1171. 'adminCommentsSettings',
  1172. array(
  1173. 'hotkeys_highlight_first' => isset( $_GET['hotkeys_highlight_first'] ),
  1174. 'hotkeys_highlight_last' => isset( $_GET['hotkeys_highlight_last'] ),
  1175. )
  1176. );
  1177. $scripts->add( 'xfn', "/wp-admin/js/xfn$suffix.js", array( 'jquery' ), false, 1 );
  1178. $scripts->add( 'postbox', "/wp-admin/js/postbox$suffix.js", array( 'jquery-ui-sortable', 'wp-a11y' ), false, 1 );
  1179. $scripts->set_translations( 'postbox' );
  1180. $scripts->add( 'tags-box', "/wp-admin/js/tags-box$suffix.js", array( 'jquery', 'tags-suggest' ), false, 1 );
  1181. $scripts->set_translations( 'tags-box' );
  1182. $scripts->add( 'tags-suggest', "/wp-admin/js/tags-suggest$suffix.js", array( 'jquery-ui-autocomplete', 'wp-a11y' ), false, 1 );
  1183. $scripts->set_translations( 'tags-suggest' );
  1184. $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count', 'wp-a11y', 'wp-sanitize', 'clipboard' ), false, 1 );
  1185. $scripts->set_translations( 'post' );
  1186. $scripts->add( 'editor-expand', "/wp-admin/js/editor-expand$suffix.js", array( 'jquery', 'underscore' ), false, 1 );
  1187. $scripts->add( 'link', "/wp-admin/js/link$suffix.js", array( 'wp-lists', 'postbox' ), false, 1 );
  1188. $scripts->add( 'comment', "/wp-admin/js/comment$suffix.js", array( 'jquery', 'postbox' ), false, 1 );
  1189. $scripts->set_translations( 'comment' );
  1190. $scripts->add( 'admin-gallery', "/wp-admin/js/gallery$suffix.js", array( 'jquery-ui-sortable' ) );
  1191. $scripts->add( 'admin-widgets', "/wp-admin/js/widgets$suffix.js", array( 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'wp-a11y' ), false, 1 );
  1192. $scripts->set_translations( 'admin-widgets' );
  1193. $scripts->add( 'media-widgets', "/wp-admin/js/widgets/media-widgets$suffix.js", array( 'jquery', 'media-models', 'media-views', 'wp-api-request' ) );
  1194. $scripts->add_inline_script( 'media-widgets', 'wp.mediaWidgets.init();', 'after' );
  1195. $scripts->add( 'media-audio-widget', "/wp-admin/js/widgets/media-audio-widget$suffix.js", array( 'media-widgets', 'media-audiovideo' ) );
  1196. $scripts->add( 'media-image-widget', "/wp-admin/js/widgets/media-image-widget$suffix.js", array( 'media-widgets' ) );
  1197. $scripts->add( 'media-gallery-widget', "/wp-admin/js/widgets/media-gallery-widget$suffix.js", array( 'media-widgets' ) );
  1198. $scripts->add( 'media-video-widget', "/wp-admin/js/widgets/media-video-widget$suffix.js", array( 'media-widgets', 'media-audiovideo', 'wp-api-request' ) );
  1199. $scripts->add( 'text-widgets', "/wp-admin/js/widgets/text-widgets$suffix.js", array( 'jquery', 'backbone', 'editor', 'wp-util', 'wp-a11y' ) );
  1200. $scripts->add( 'custom-html-widgets', "/wp-admin/js/widgets/custom-html-widgets$suffix.js", array( 'jquery', 'backbone', 'wp-util', 'jquery-ui-core', 'wp-a11y' ) );
  1201. $scripts->add( 'theme', "/wp-admin/js/theme$suffix.js", array( 'wp-backbone', 'wp-a11y', 'customize-base' ), false, 1 );
  1202. $scripts->add( 'inline-edit-post', "/wp-admin/js/inline-edit-post$suffix.js", array( 'jquery', 'tags-suggest', 'wp-a11y' ), false, 1 );
  1203. $scripts->set_translations( 'inline-edit-post' );
  1204. $scripts->add( 'inline-edit-tax', "/wp-admin/js/inline-edit-tax$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
  1205. $scripts->set_translations( 'inline-edit-tax' );
  1206. $scripts->add( 'plugin-install', "/wp-admin/js/plugin-install$suffix.js", array( 'jquery', 'jquery-ui-core', 'thickbox' ), false, 1 );
  1207. $scripts->set_translations( 'plugin-install' );
  1208. $scripts->add( 'site-health', "/wp-admin/js/site-health$suffix.js", array( 'clipboard', 'jquery', 'wp-util', 'wp-a11y', 'wp-api-request', 'wp-url', 'wp-i18n', 'wp-hooks' ), false, 1 );
  1209. $scripts->set_translations( 'site-health' );
  1210. $scripts->add( 'privacy-tools', "/wp-admin/js/privacy-tools$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
  1211. $scripts->set_translations( 'privacy-tools' );
  1212. $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'common', 'jquery', 'wp-util', 'wp-a11y', 'wp-sanitize', 'wp-i18n' ), false, 1 );
  1213. $scripts->set_translations( 'updates' );
  1214. did_action( 'init' ) && $scripts->localize(
  1215. 'updates',
  1216. '_wpUpdatesSettings',
  1217. array(
  1218. 'ajax_nonce' => wp_installing() ? '' : wp_create_nonce( 'updates' ),
  1219. )
  1220. );
  1221. $scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array( 'jquery' ), '1.2' );
  1222. $scripts->add( 'iris', '/wp-admin/js/iris.min.js', array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), '1.1.1', 1 );
  1223. $scripts->add( 'wp-color-picker', "/wp-admin/js/color-picker$suffix.js", array( 'iris' ), false, 1 );
  1224. $scripts->set_translations( 'wp-color-picker' );
  1225. $scripts->add( 'dashboard', "/wp-admin/js/dashboard$suffix.js", array( 'jquery', 'admin-comments', 'postbox', 'wp-util', 'wp-a11y', 'wp-date' ), false, 1 );
  1226. $scripts->set_translations( 'dashboard' );
  1227. $scripts->add( 'list-revisions', "/wp-includes/js/wp-list-revisions$suffix.js" );
  1228. $scripts->add( 'media-grid', "/wp-includes/js/media-grid$suffix.js", array( 'media-editor' ), false, 1 );
  1229. $scripts->add( 'media', "/wp-admin/js/media$suffix.js", array( 'jquery', 'clipboard', 'wp-i18n', 'wp-a11y' ), false, 1 );
  1230. $scripts->set_translations( 'media' );
  1231. $scripts->add( 'image-edit', "/wp-admin/js/image-edit$suffix.js", array( 'jquery', 'jquery-ui-core', 'json2', 'imgareaselect', 'wp-a11y' ), false, 1 );
  1232. $scripts->set_translations( 'image-edit' );
  1233. $scripts->add( 'set-post-thumbnail', "/wp-admin/js/set-post-thumbnail$suffix.js", array( 'jquery' ), false, 1 );
  1234. $scripts->set_translations( 'set-post-thumbnail' );
  1235. /*
  1236. * Navigation Menus: Adding underscore as a dependency to utilize _.debounce
  1237. * see https://core.trac.wordpress.org/ticket/42321
  1238. */
  1239. $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", array( 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'wp-lists', 'postbox', 'json2', 'underscore' ) );
  1240. $scripts->set_translations( 'nav-menu' );
  1241. $scripts->add( 'custom-header', '/wp-admin/js/custom-header.js', array( 'jquery-masonry' ), false, 1 );
  1242. $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array( 'wp-color-picker', 'media-views' ), false, 1 );
  1243. $scripts->add( 'media-gallery', "/wp-admin/js/media-gallery$suffix.js", array( 'jquery' ), false, 1 );
  1244. $scripts->add( 'svg-painter', '/wp-admin/js/svg-painter.js', array( 'jquery' ), false, 1 );
  1245. }
  1246. }
  1247. /**
  1248. * Assigns default styles to $styles object.
  1249. *
  1250. * Nothing is returned, because the $styles parameter is passed by reference.
  1251. * Meaning that whatever object is passed will be updated without having to
  1252. * reassign the variable that was passed back to the same value. This saves
  1253. * memory.
  1254. *
  1255. * Adding default styles is not the only task, it also assigns the base_url
  1256. * property, the default version, and text direction for the object.
  1257. *
  1258. * @since 2.6.0
  1259. *
  1260. * @global array $editor_styles
  1261. *
  1262. * @param WP_Styles $styles
  1263. */
  1264. function wp_default_styles( $styles ) {
  1265. global $editor_styles;
  1266. // Include an unmodified $wp_version.
  1267. require ABSPATH . WPINC . '/version.php';
  1268. if ( ! defined( 'SCRIPT_DEBUG' ) ) {
  1269. define( 'SCRIPT_DEBUG', false !== strpos( $wp_version, '-src' ) );
  1270. }
  1271. $guessurl = site_url();
  1272. if ( ! $guessurl ) {
  1273. $guessurl = wp_guess_url();
  1274. }
  1275. $styles->base_url = $guessurl;
  1276. $styles->content_url = defined( 'WP_CONTENT_URL' ) ? WP_CONTENT_URL : '';
  1277. $styles->default_version = get_bloginfo( 'version' );
  1278. $styles->text_direction = function_exists( 'is_rtl' ) && is_rtl() ? 'rtl' : 'ltr';
  1279. $styles->default_dirs = array( '/wp-admin/', '/wp-includes/css/' );
  1280. // Open Sans is no longer used by core, but may be relied upon by themes and plugins.
  1281. $open_sans_font_url = '';
  1282. /*
  1283. * translators: If there are characters in your language that are not supported
  1284. * by Open Sans, translate this to 'off'. Do not translate into your own language.
  1285. */
  1286. if ( 'off' !== _x( 'on', 'Open Sans font: on or off' ) ) {
  1287. $subsets = 'latin,latin-ext';
  1288. /*
  1289. * translators: To add an additional Open Sans character subset specific to your language,
  1290. * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
  1291. */
  1292. $subset = _x( 'no-subset', 'Open Sans font: add new subset (greek, cyrillic, vietnamese)' );
  1293. if ( 'cyrillic' === $subset ) {
  1294. $subsets .= ',cyrillic,cyrillic-ext';
  1295. } elseif ( 'greek' === $subset ) {
  1296. $subsets .= ',greek,greek-ext';
  1297. } elseif ( 'vietnamese' === $subset ) {
  1298. $subsets .= ',vietnamese';
  1299. }
  1300. // Hotlink Open Sans, for now.
  1301. $open_sans_font_url = "https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,300,400,600&subset=$subsets&display=fallback";
  1302. }
  1303. // Register a stylesheet for the selected admin color scheme.
  1304. $styles->add( 'colors', true, array( 'wp-admin', 'buttons' ) );
  1305. $suffix = SCRIPT_DEBUG ? '' : '.min';
  1306. // Admin CSS.
  1307. $styles->add( 'common', "/wp-admin/css/common$suffix.css" );
  1308. $styles->add( 'forms', "/wp-admin/css/forms$suffix.css" );
  1309. $styles->add( 'admin-menu', "/wp-admin/css/admin-menu$suffix.css" );
  1310. $styles->add( 'dashboard', "/wp-admin/css/dashboard$suffix.css" );
  1311. $styles->add( 'list-tables', "/wp-admin/css/list-tables$suffix.css" );
  1312. $styles->add( 'edit', "/wp-admin/css/edit$suffix.css" );
  1313. $styles->add( 'revisions', "/wp-admin/css/revisions$suffix.css" );
  1314. $styles->add( 'media', "/wp-admin/css/media$suffix.css" );
  1315. $styles->add( 'themes', "/wp-admin/css/themes$suffix.css" );
  1316. $styles->add( 'about', "/wp-admin/css/about$suffix.css" );
  1317. $styles->add( 'nav-menus', "/wp-admin/css/nav-menus$suffix.css" );
  1318. $styles->add( 'widgets', "/wp-admin/css/widgets$suffix.css", array( 'wp-pointer' ) );
  1319. $styles->add( 'site-icon', "/wp-admin/css/site-icon$suffix.css" );
  1320. $styles->add( 'l10n', "/wp-admin/css/l10n$suffix.css" );
  1321. $styles->add( 'code-editor', "/wp-admin/css/code-editor$suffix.css", array( 'wp-codemirror' ) );
  1322. $styles->add( 'site-health', "/wp-admin/css/site-health$suffix.css" );
  1323. $styles->add( 'wp-admin', false, array( 'dashicons', 'common', 'forms', 'admin-menu', 'dashboard', 'list-tables', 'edit', 'revisions', 'media', 'themes', 'about', 'nav-menus', 'widgets', 'site-icon', 'l10n' ) );
  1324. $styles->add( 'login', "/wp-admin/css/login$suffix.css", array( 'dashicons', 'buttons', 'forms', 'l10n' ) );
  1325. $styles->add( 'install', "/wp-admin/css/install$suffix.css", array( 'dashicons', 'buttons', 'forms', 'l10n' ) );
  1326. $styles->add( 'wp-color-picker', "/wp-admin/css/color-picker$suffix.css" );
  1327. $styles->add( 'customize-controls', "/wp-admin/css/customize-controls$suffix.css", array( 'wp-admin', 'colors', 'imgareaselect' ) );
  1328. $styles->add( 'customize-widgets', "/wp-admin/css/customize-widgets$suffix.css", array( 'wp-admin', 'colors' ) );
  1329. $styles->add( 'customize-nav-menus', "/wp-admin/css/customize-nav-menus$suffix.css", array( 'wp-admin', 'colors' ) );
  1330. // Common dependencies.
  1331. $styles->add( 'buttons', "/wp-includes/css/buttons$suffix.css" );
  1332. $styles->add( 'dashicons', "/wp-includes/css/dashicons$suffix.css" );
  1333. // Includes CSS.
  1334. $styles->add( 'admin-bar', "/wp-includes/css/admin-bar$suffix.css", array( 'dashicons' ) );
  1335. $styles->add( 'wp-auth-check', "/wp-includes/css/wp-auth-check$suffix.css", array( 'dashicons' ) );
  1336. $styles->add( 'editor-buttons', "/wp-includes/css/editor$suffix.css", array( 'dashicons' ) );
  1337. $styles->add( 'media-views', "/wp-includes/css/media-views$suffix.css", array( 'buttons', 'dashicons', 'wp-mediaelement' ) );
  1338. $styles->add( 'wp-pointer', "/wp-includes/css/wp-pointer$suffix.css", array( 'dashicons' ) );
  1339. $styles->add( 'customize-preview', "/wp-includes/css/customize-preview$suffix.css", array( 'dashicons' ) );
  1340. $styles->add( 'wp-embed-template-ie', "/wp-includes/css/wp-embed-template-ie$suffix.css" );
  1341. $styles->add_data( 'wp-embed-template-ie', 'conditional', 'lte IE 8' );
  1342. // External libraries and friends.
  1343. $styles->add( 'imgareaselect', '/wp-includes/js/imgareaselect/imgareaselect.css', array(), '0.9.8' );
  1344. $styles->add( 'wp-jquery-ui-dialog', "/wp-includes/css/jquery-ui-dialog$suffix.css", array( 'dashicons' ) );
  1345. $styles->add( 'mediaelement', '/wp-includes/js/mediaelement/mediaelementplayer-legacy.min.css', array(), '4.2.16' );
  1346. $styles->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.css", array( 'mediaelement' ) );
  1347. $styles->add( 'thickbox', '/wp-includes/js/thickbox/thickbox.css', array( 'dashicons' ) );
  1348. $styles->add( 'wp-codemirror', '/wp-includes/js/codemirror/codemirror.min.css', array(), '5.29.1-alpha-ee20357' );
  1349. // Deprecated CSS.
  1350. $styles->add( 'deprecated-media', "/wp-admin/css/deprecated-media$suffix.css" );
  1351. $styles->add( 'farbtastic', "/wp-admin/css/farbtastic$suffix.css", array(), '1.3u1' );
  1352. $styles->add( 'jcrop', '/wp-includes/js/jcrop/jquery.Jcrop.min.css', array(), '0.9.15' );
  1353. $styles->add( 'colors-fresh', false, array( 'wp-admin', 'buttons' ) ); // Old handle.
  1354. $styles->add( 'open-sans', $open_sans_font_url ); // No longer used in core as of 4.6.
  1355. // Noto Serif is no longer used by core, but may be relied upon by themes and plugins.
  1356. $fonts_url = '';
  1357. /*
  1358. * translators: Use this to specify the proper Google Font name and variants
  1359. * to load that is supported by your language. Do not translate.
  1360. * Set to 'off' to disable loading.
  1361. */
  1362. $font_family = _x( 'Noto Serif:400,400i,700,700i', 'Google Font Name and Variants' );
  1363. if ( 'off' !== $font_family ) {
  1364. $fonts_url = 'https://fonts.googleapis.com/css?family=' . urlencode( $font_family );
  1365. }
  1366. $styles->add( 'wp-editor-font', $fonts_url ); // No longer used in core as of 5.7.
  1367. $block_library_theme_path = WPINC . "/css/dist/block-library/theme$suffix.css";
  1368. $styles->add( 'wp-block-library-theme', "/$block_library_theme_path" );
  1369. $styles->add_data( 'wp-block-library-theme', 'path', ABSPATH . $block_library_theme_path );
  1370. $styles->add(
  1371. 'wp-reset-editor-styles',
  1372. "/wp-includes/css/dist/block-library/reset$suffix.css",
  1373. array( 'common', 'forms' ) // Make sure the reset is loaded after the default WP Admin styles.
  1374. );
  1375. $styles->add(
  1376. 'wp-editor-classic-layout-styles',
  1377. "/wp-includes/css/dist/edit-post/classic$suffix.css",
  1378. array()
  1379. );
  1380. $wp_edit_blocks_dependencies = array(
  1381. 'wp-components',
  1382. 'wp-editor',
  1383. // This need to be added before the block library styles,
  1384. // The block library styles override the "reset" styles.
  1385. 'wp-reset-editor-styles',
  1386. 'wp-block-library',
  1387. 'wp-reusable-blocks',
  1388. );
  1389. // Only load the default layout and margin styles for themes without theme.json file.
  1390. if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
  1391. $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
  1392. }
  1393. if ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) {
  1394. // Include opinionated block styles if no $editor_styles are declared, so the editor never appears broken.
  1395. $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
  1396. }
  1397. $styles->add(
  1398. 'wp-edit-blocks',
  1399. "/wp-includes/css/dist/block-library/editor$suffix.css",
  1400. $wp_edit_blocks_dependencies
  1401. );
  1402. $package_styles = array(
  1403. 'block-editor' => array( 'wp-components' ),
  1404. 'block-library' => array(),
  1405. 'block-directory' => array(),
  1406. 'components' => array(),
  1407. 'edit-post' => array(
  1408. 'wp-components',
  1409. 'wp-block-editor',
  1410. 'wp-editor',
  1411. 'wp-edit-blocks',
  1412. 'wp-block-library',
  1413. 'wp-nux',
  1414. ),
  1415. 'editor' => array(
  1416. 'wp-components',
  1417. 'wp-block-editor',
  1418. 'wp-nux',
  1419. 'wp-reusable-blocks',
  1420. ),
  1421. 'format-library' => array(),
  1422. 'list-reusable-blocks' => array( 'wp-components' ),
  1423. 'reusable-blocks' => array( 'wp-components' ),
  1424. 'nux' => array( 'wp-components' ),
  1425. 'widgets' => array(
  1426. 'wp-components',
  1427. ),
  1428. 'edit-widgets' => array(
  1429. 'wp-widgets',
  1430. 'wp-block-editor',
  1431. 'wp-edit-blocks',
  1432. 'wp-block-library',
  1433. 'wp-reusable-blocks',
  1434. ),
  1435. 'customize-widgets' => array(
  1436. 'wp-widgets',
  1437. 'wp-block-editor',
  1438. 'wp-edit-blocks',
  1439. 'wp-block-library',
  1440. 'wp-reusable-blocks',
  1441. ),
  1442. 'edit-site' => array(
  1443. 'wp-components',
  1444. 'wp-block-editor',
  1445. 'wp-edit-blocks',
  1446. ),
  1447. );
  1448. foreach ( $package_styles as $package => $dependencies ) {
  1449. $handle = 'wp-' . $package;
  1450. $path = "/wp-includes/css/dist/$package/style$suffix.css";
  1451. if ( 'block-library' === $package && wp_should_load_separate_core_block_assets() ) {
  1452. $path = "/wp-includes/css/dist/$package/common$suffix.css";
  1453. }
  1454. $styles->add( $handle, $path, $dependencies );
  1455. $styles->add_data( $handle, 'path', ABSPATH . $path );
  1456. }
  1457. // RTL CSS.
  1458. $rtl_styles = array(
  1459. // Admin CSS.
  1460. 'common',
  1461. 'forms',
  1462. 'admin-menu',
  1463. 'dashboard',
  1464. 'list-tables',
  1465. 'edit',
  1466. 'revisions',
  1467. 'media',
  1468. 'themes',
  1469. 'about',
  1470. 'nav-menus',
  1471. 'widgets',
  1472. 'site-icon',
  1473. 'l10n',
  1474. 'install',
  1475. 'wp-color-picker',
  1476. 'customize-controls',
  1477. 'customize-widgets',
  1478. 'customize-nav-menus',
  1479. 'customize-preview',
  1480. 'login',
  1481. 'site-health',
  1482. // Includes CSS.
  1483. 'buttons',
  1484. 'admin-bar',
  1485. 'wp-auth-check',
  1486. 'editor-buttons',
  1487. 'media-views',
  1488. 'wp-pointer',
  1489. 'wp-jquery-ui-dialog',
  1490. // Package styles.
  1491. 'wp-reset-editor-styles',
  1492. 'wp-editor-classic-layout-styles',
  1493. 'wp-block-library-theme',
  1494. 'wp-edit-blocks',
  1495. 'wp-block-editor',
  1496. 'wp-block-library',
  1497. 'wp-block-directory',
  1498. 'wp-components',
  1499. 'wp-customize-widgets',
  1500. 'wp-edit-post',
  1501. 'wp-edit-site',
  1502. 'wp-edit-widgets',
  1503. 'wp-editor',
  1504. 'wp-format-library',
  1505. 'wp-list-reusable-blocks',
  1506. 'wp-reusable-blocks',
  1507. 'wp-nux',
  1508. 'wp-widgets',
  1509. // Deprecated CSS.
  1510. 'deprecated-media',
  1511. 'farbtastic',
  1512. );
  1513. foreach ( $rtl_styles as $rtl_style ) {
  1514. $styles->add_data( $rtl_style, 'rtl', 'replace' );
  1515. if ( $suffix ) {
  1516. $styles->add_data( $rtl_style, 'suffix', $suffix );
  1517. }
  1518. }
  1519. }
  1520. /**
  1521. * Reorders JavaScript scripts array to place prototype before jQuery.
  1522. *
  1523. * @since 2.3.1
  1524. *
  1525. * @param array $js_array JavaScript scripts array
  1526. * @return array Reordered array, if needed.
  1527. */
  1528. function wp_prototype_before_jquery( $js_array ) {
  1529. $prototype = array_search( 'prototype', $js_array, true );
  1530. if ( false === $prototype ) {
  1531. return $js_array;
  1532. }
  1533. $jquery = array_search( 'jquery', $js_array, true );
  1534. if ( false === $jquery ) {
  1535. return $js_array;
  1536. }
  1537. if ( $prototype < $jquery ) {
  1538. return $js_array;
  1539. }
  1540. unset( $js_array[ $prototype ] );
  1541. array_splice( $js_array, $jquery, 0, 'prototype' );
  1542. return $js_array;
  1543. }
  1544. /**
  1545. * Loads localized data on print rather than initialization.
  1546. *
  1547. * These localizations require information that may not be loaded even by init.
  1548. *
  1549. * @since 2.5.0
  1550. */
  1551. function wp_just_in_time_script_localization() {
  1552. wp_localize_script(
  1553. 'autosave',
  1554. 'autosaveL10n',
  1555. array(
  1556. 'autosaveInterval' => AUTOSAVE_INTERVAL,
  1557. 'blog_id' => get_current_blog_id(),
  1558. )
  1559. );
  1560. wp_localize_script(
  1561. 'mce-view',
  1562. 'mceViewL10n',
  1563. array(
  1564. 'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
  1565. )
  1566. );
  1567. wp_localize_script(
  1568. 'word-count',
  1569. 'wordCountL10n',
  1570. array(
  1571. /*
  1572. * translators: If your word count is based on single characters (e.g. East Asian characters),
  1573. * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
  1574. * Do not translate into your own language.
  1575. */
  1576. 'type' => _x( 'words', 'Word count type. Do not translate!' ),
  1577. 'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
  1578. )
  1579. );
  1580. }
  1581. /**
  1582. * Localizes the jQuery UI datepicker.
  1583. *
  1584. * @since 4.6.0
  1585. *
  1586. * @link https://api.jqueryui.com/datepicker/#options
  1587. *
  1588. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1589. */
  1590. function wp_localize_jquery_ui_datepicker() {
  1591. global $wp_locale;
  1592. if ( ! wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) ) {
  1593. return;
  1594. }
  1595. // Convert the PHP date format into jQuery UI's format.
  1596. $datepicker_date_format = str_replace(
  1597. array(
  1598. 'd',
  1599. 'j',
  1600. 'l',
  1601. 'z', // Day.
  1602. 'F',
  1603. 'M',
  1604. 'n',
  1605. 'm', // Month.
  1606. 'Y',
  1607. 'y', // Year.
  1608. ),
  1609. array(
  1610. 'dd',
  1611. 'd',
  1612. 'DD',
  1613. 'o',
  1614. 'MM',
  1615. 'M',
  1616. 'm',
  1617. 'mm',
  1618. 'yy',
  1619. 'y',
  1620. ),
  1621. get_option( 'date_format' )
  1622. );
  1623. $datepicker_defaults = wp_json_encode(
  1624. array(
  1625. 'closeText' => __( 'Close' ),
  1626. 'currentText' => __( 'Today' ),
  1627. 'monthNames' => array_values( $wp_locale->month ),
  1628. 'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
  1629. 'nextText' => __( 'Next' ),
  1630. 'prevText' => __( 'Previous' ),
  1631. 'dayNames' => array_values( $wp_locale->weekday ),
  1632. 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
  1633. 'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
  1634. 'dateFormat' => $datepicker_date_format,
  1635. 'firstDay' => absint( get_option( 'start_of_week' ) ),
  1636. 'isRTL' => $wp_locale->is_rtl(),
  1637. )
  1638. );
  1639. wp_add_inline_script( 'jquery-ui-datepicker', "jQuery(function(jQuery){jQuery.datepicker.setDefaults({$datepicker_defaults});});" );
  1640. }
  1641. /**
  1642. * Localizes community events data that needs to be passed to dashboard.js.
  1643. *
  1644. * @since 4.8.0
  1645. */
  1646. function wp_localize_community_events() {
  1647. if ( ! wp_script_is( 'dashboard' ) ) {
  1648. return;
  1649. }
  1650. require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
  1651. $user_id = get_current_user_id();
  1652. $saved_location = get_user_option( 'community-events-location', $user_id );
  1653. $saved_ip_address = isset( $saved_location['ip'] ) ? $saved_location['ip'] : false;
  1654. $current_ip_address = WP_Community_Events::get_unsafe_client_ip();
  1655. /*
  1656. * If the user's location is based on their IP address, then update their
  1657. * location when their IP address changes. This allows them to see events
  1658. * in their current city when travelling. Otherwise, they would always be
  1659. * shown events in the city where they were when they first loaded the
  1660. * Dashboard, which could have been months or years ago.
  1661. */
  1662. if ( $saved_ip_address && $current_ip_address && $current_ip_address !== $saved_ip_address ) {
  1663. $saved_location['ip'] = $current_ip_address;
  1664. update_user_meta( $user_id, 'community-events-location', $saved_location );
  1665. }
  1666. $events_client = new WP_Community_Events( $user_id, $saved_location );
  1667. wp_localize_script(
  1668. 'dashboard',
  1669. 'communityEventsData',
  1670. array(
  1671. 'nonce' => wp_create_nonce( 'community_events' ),
  1672. 'cache' => $events_client->get_cached_events(),
  1673. 'time_format' => get_option( 'time_format' ),
  1674. )
  1675. );
  1676. }
  1677. /**
  1678. * Administration Screen CSS for changing the styles.
  1679. *
  1680. * If installing the 'wp-admin/' directory will be replaced with './'.
  1681. *
  1682. * The $_wp_admin_css_colors global manages the Administration Screens CSS
  1683. * stylesheet that is loaded. The option that is set is 'admin_color' and is the
  1684. * color and key for the array. The value for the color key is an object with
  1685. * a 'url' parameter that has the URL path to the CSS file.
  1686. *
  1687. * The query from $src parameter will be appended to the URL that is given from
  1688. * the $_wp_admin_css_colors array value URL.
  1689. *
  1690. * @since 2.6.0
  1691. *
  1692. * @global array $_wp_admin_css_colors
  1693. *
  1694. * @param string $src Source URL.
  1695. * @param string $handle Either 'colors' or 'colors-rtl'.
  1696. * @return string|false URL path to CSS stylesheet for Administration Screens.
  1697. */
  1698. function wp_style_loader_src( $src, $handle ) {
  1699. global $_wp_admin_css_colors;
  1700. if ( wp_installing() ) {
  1701. return preg_replace( '#^wp-admin/#', './', $src );
  1702. }
  1703. if ( 'colors' === $handle ) {
  1704. $color = get_user_option( 'admin_color' );
  1705. if ( empty( $color ) || ! isset( $_wp_admin_css_colors[ $color ] ) ) {
  1706. $color = 'fresh';
  1707. }
  1708. $color = $_wp_admin_css_colors[ $color ];
  1709. $url = $color->url;
  1710. if ( ! $url ) {
  1711. return false;
  1712. }
  1713. $parsed = parse_url( $src );
  1714. if ( isset( $parsed['query'] ) && $parsed['query'] ) {
  1715. wp_parse_str( $parsed['query'], $qv );
  1716. $url = add_query_arg( $qv, $url );
  1717. }
  1718. return $url;
  1719. }
  1720. return $src;
  1721. }
  1722. /**
  1723. * Prints the script queue in the HTML head on admin pages.
  1724. *
  1725. * Postpones the scripts that were queued for the footer.
  1726. * print_footer_scripts() is called in the footer to print these scripts.
  1727. *
  1728. * @since 2.8.0
  1729. *
  1730. * @see wp_print_scripts()
  1731. *
  1732. * @global bool $concatenate_scripts
  1733. *
  1734. * @return array
  1735. */
  1736. function print_head_scripts() {
  1737. global $concatenate_scripts;
  1738. if ( ! did_action( 'wp_print_scripts' ) ) {
  1739. /** This action is documented in wp-includes/functions.wp-scripts.php */
  1740. do_action( 'wp_print_scripts' );
  1741. }
  1742. $wp_scripts = wp_scripts();
  1743. script_concat_settings();
  1744. $wp_scripts->do_concat = $concatenate_scripts;
  1745. $wp_scripts->do_head_items();
  1746. /**
  1747. * Filters whether to print the head scripts.
  1748. *
  1749. * @since 2.8.0
  1750. *
  1751. * @param bool $print Whether to print the head scripts. Default true.
  1752. */
  1753. if ( apply_filters( 'print_head_scripts', true ) ) {
  1754. _print_scripts();
  1755. }
  1756. $wp_scripts->reset();
  1757. return $wp_scripts->done;
  1758. }
  1759. /**
  1760. * Prints the scripts that were queued for the footer or too late for the HTML head.
  1761. *
  1762. * @since 2.8.0
  1763. *
  1764. * @global WP_Scripts $wp_scripts
  1765. * @global bool $concatenate_scripts
  1766. *
  1767. * @return array
  1768. */
  1769. function print_footer_scripts() {
  1770. global $wp_scripts, $concatenate_scripts;
  1771. if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  1772. return array(); // No need to run if not instantiated.
  1773. }
  1774. script_concat_settings();
  1775. $wp_scripts->do_concat = $concatenate_scripts;
  1776. $wp_scripts->do_footer_items();
  1777. /**
  1778. * Filters whether to print the footer scripts.
  1779. *
  1780. * @since 2.8.0
  1781. *
  1782. * @param bool $print Whether to print the footer scripts. Default true.
  1783. */
  1784. if ( apply_filters( 'print_footer_scripts', true ) ) {
  1785. _print_scripts();
  1786. }
  1787. $wp_scripts->reset();
  1788. return $wp_scripts->done;
  1789. }
  1790. /**
  1791. * Prints scripts (internal use only)
  1792. *
  1793. * @ignore
  1794. *
  1795. * @global WP_Scripts $wp_scripts
  1796. * @global bool $compress_scripts
  1797. */
  1798. function _print_scripts() {
  1799. global $wp_scripts, $compress_scripts;
  1800. $zip = $compress_scripts ? 1 : 0;
  1801. if ( $zip && defined( 'ENFORCE_GZIP' ) && ENFORCE_GZIP ) {
  1802. $zip = 'gzip';
  1803. }
  1804. $concat = trim( $wp_scripts->concat, ', ' );
  1805. $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : " type='text/javascript'";
  1806. if ( $concat ) {
  1807. if ( ! empty( $wp_scripts->print_code ) ) {
  1808. echo "\n<script{$type_attr}>\n";
  1809. echo "/* <![CDATA[ */\n"; // Not needed in HTML 5.
  1810. echo $wp_scripts->print_code;
  1811. echo "/* ]]> */\n";
  1812. echo "</script>\n";
  1813. }
  1814. $concat = str_split( $concat, 128 );
  1815. $concatenated = '';
  1816. foreach ( $concat as $key => $chunk ) {
  1817. $concatenated .= "&load%5Bchunk_{$key}%5D={$chunk}";
  1818. }
  1819. $src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}" . $concatenated . '&ver=' . $wp_scripts->default_version;
  1820. echo "<script{$type_attr} src='" . esc_attr( $src ) . "'></script>\n";
  1821. }
  1822. if ( ! empty( $wp_scripts->print_html ) ) {
  1823. echo $wp_scripts->print_html;
  1824. }
  1825. }
  1826. /**
  1827. * Prints the script queue in the HTML head on the front end.
  1828. *
  1829. * Postpones the scripts that were queued for the footer.
  1830. * wp_print_footer_scripts() is called in the footer to print these scripts.
  1831. *
  1832. * @since 2.8.0
  1833. *
  1834. * @global WP_Scripts $wp_scripts
  1835. *
  1836. * @return array
  1837. */
  1838. function wp_print_head_scripts() {
  1839. global $wp_scripts;
  1840. if ( ! did_action( 'wp_print_scripts' ) ) {
  1841. /** This action is documented in wp-includes/functions.wp-scripts.php */
  1842. do_action( 'wp_print_scripts' );
  1843. }
  1844. if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
  1845. return array(); // No need to run if nothing is queued.
  1846. }
  1847. return print_head_scripts();
  1848. }
  1849. /**
  1850. * Private, for use in *_footer_scripts hooks
  1851. *
  1852. * @since 3.3.0
  1853. */
  1854. function _wp_footer_scripts() {
  1855. print_late_styles();
  1856. print_footer_scripts();
  1857. }
  1858. /**
  1859. * Hooks to print the scripts and styles in the footer.
  1860. *
  1861. * @since 2.8.0
  1862. */
  1863. function wp_print_footer_scripts() {
  1864. /**
  1865. * Fires when footer scripts are printed.
  1866. *
  1867. * @since 2.8.0
  1868. */
  1869. do_action( 'wp_print_footer_scripts' );
  1870. }
  1871. /**
  1872. * Wrapper for do_action( 'wp_enqueue_scripts' ).
  1873. *
  1874. * Allows plugins to queue scripts for the front end using wp_enqueue_script().
  1875. * Runs first in wp_head() where all is_home(), is_page(), etc. functions are available.
  1876. *
  1877. * @since 2.8.0
  1878. */
  1879. function wp_enqueue_scripts() {
  1880. /**
  1881. * Fires when scripts and styles are enqueued.
  1882. *
  1883. * @since 2.8.0
  1884. */
  1885. do_action( 'wp_enqueue_scripts' );
  1886. }
  1887. /**
  1888. * Prints the styles queue in the HTML head on admin pages.
  1889. *
  1890. * @since 2.8.0
  1891. *
  1892. * @global bool $concatenate_scripts
  1893. *
  1894. * @return array
  1895. */
  1896. function print_admin_styles() {
  1897. global $concatenate_scripts;
  1898. $wp_styles = wp_styles();
  1899. script_concat_settings();
  1900. $wp_styles->do_concat = $concatenate_scripts;
  1901. $wp_styles->do_items( false );
  1902. /**
  1903. * Filters whether to print the admin styles.
  1904. *
  1905. * @since 2.8.0
  1906. *
  1907. * @param bool $print Whether to print the admin styles. Default true.
  1908. */
  1909. if ( apply_filters( 'print_admin_styles', true ) ) {
  1910. _print_styles();
  1911. }
  1912. $wp_styles->reset();
  1913. return $wp_styles->done;
  1914. }
  1915. /**
  1916. * Prints the styles that were queued too late for the HTML head.
  1917. *
  1918. * @since 3.3.0
  1919. *
  1920. * @global WP_Styles $wp_styles
  1921. * @global bool $concatenate_scripts
  1922. *
  1923. * @return array|void
  1924. */
  1925. function print_late_styles() {
  1926. global $wp_styles, $concatenate_scripts;
  1927. if ( ! ( $wp_styles instanceof WP_Styles ) ) {
  1928. return;
  1929. }
  1930. script_concat_settings();
  1931. $wp_styles->do_concat = $concatenate_scripts;
  1932. $wp_styles->do_footer_items();
  1933. /**
  1934. * Filters whether to print the styles queued too late for the HTML head.
  1935. *
  1936. * @since 3.3.0
  1937. *
  1938. * @param bool $print Whether to print the 'late' styles. Default true.
  1939. */
  1940. if ( apply_filters( 'print_late_styles', true ) ) {
  1941. _print_styles();
  1942. }
  1943. $wp_styles->reset();
  1944. return $wp_styles->done;
  1945. }
  1946. /**
  1947. * Prints styles (internal use only).
  1948. *
  1949. * @ignore
  1950. * @since 3.3.0
  1951. *
  1952. * @global bool $compress_css
  1953. */
  1954. function _print_styles() {
  1955. global $compress_css;
  1956. $wp_styles = wp_styles();
  1957. $zip = $compress_css ? 1 : 0;
  1958. if ( $zip && defined( 'ENFORCE_GZIP' ) && ENFORCE_GZIP ) {
  1959. $zip = 'gzip';
  1960. }
  1961. $concat = trim( $wp_styles->concat, ', ' );
  1962. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  1963. if ( $concat ) {
  1964. $dir = $wp_styles->text_direction;
  1965. $ver = $wp_styles->default_version;
  1966. $concat = str_split( $concat, 128 );
  1967. $concatenated = '';
  1968. foreach ( $concat as $key => $chunk ) {
  1969. $concatenated .= "&load%5Bchunk_{$key}%5D={$chunk}";
  1970. }
  1971. $href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&dir={$dir}" . $concatenated . '&ver=' . $ver;
  1972. echo "<link rel='stylesheet' href='" . esc_attr( $href ) . "'{$type_attr} media='all' />\n";
  1973. if ( ! empty( $wp_styles->print_code ) ) {
  1974. echo "<style{$type_attr}>\n";
  1975. echo $wp_styles->print_code;
  1976. echo "\n</style>\n";
  1977. }
  1978. }
  1979. if ( ! empty( $wp_styles->print_html ) ) {
  1980. echo $wp_styles->print_html;
  1981. }
  1982. }
  1983. /**
  1984. * Determines the concatenation and compression settings for scripts and styles.
  1985. *
  1986. * @since 2.8.0
  1987. *
  1988. * @global bool $concatenate_scripts
  1989. * @global bool $compress_scripts
  1990. * @global bool $compress_css
  1991. */
  1992. function script_concat_settings() {
  1993. global $concatenate_scripts, $compress_scripts, $compress_css;
  1994. $compressed_output = ( ini_get( 'zlib.output_compression' ) || 'ob_gzhandler' === ini_get( 'output_handler' ) );
  1995. $can_compress_scripts = ! wp_installing() && get_site_option( 'can_compress_scripts' );
  1996. if ( ! isset( $concatenate_scripts ) ) {
  1997. $concatenate_scripts = defined( 'CONCATENATE_SCRIPTS' ) ? CONCATENATE_SCRIPTS : true;
  1998. if ( ( ! is_admin() && ! did_action( 'login_init' ) ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) {
  1999. $concatenate_scripts = false;
  2000. }
  2001. }
  2002. if ( ! isset( $compress_scripts ) ) {
  2003. $compress_scripts = defined( 'COMPRESS_SCRIPTS' ) ? COMPRESS_SCRIPTS : true;
  2004. if ( $compress_scripts && ( ! $can_compress_scripts || $compressed_output ) ) {
  2005. $compress_scripts = false;
  2006. }
  2007. }
  2008. if ( ! isset( $compress_css ) ) {
  2009. $compress_css = defined( 'COMPRESS_CSS' ) ? COMPRESS_CSS : true;
  2010. if ( $compress_css && ( ! $can_compress_scripts || $compressed_output ) ) {
  2011. $compress_css = false;
  2012. }
  2013. }
  2014. }
  2015. /**
  2016. * Handles the enqueueing of block scripts and styles that are common to both
  2017. * the editor and the front-end.
  2018. *
  2019. * @since 5.0.0
  2020. */
  2021. function wp_common_block_scripts_and_styles() {
  2022. if ( is_admin() && ! wp_should_load_block_editor_scripts_and_styles() ) {
  2023. return;
  2024. }
  2025. wp_enqueue_style( 'wp-block-library' );
  2026. if ( current_theme_supports( 'wp-block-styles' ) ) {
  2027. if ( wp_should_load_separate_core_block_assets() ) {
  2028. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'css' : 'min.css';
  2029. $files = glob( __DIR__ . "/blocks/**/theme.$suffix" );
  2030. foreach ( $files as $path ) {
  2031. $block_name = basename( dirname( $path ) );
  2032. if ( is_rtl() && file_exists( __DIR__ . "/blocks/$block_name/theme-rtl.$suffix" ) ) {
  2033. $path = __DIR__ . "/blocks/$block_name/theme-rtl.$suffix";
  2034. }
  2035. wp_add_inline_style( "wp-block-{$block_name}", file_get_contents( $path ) );
  2036. }
  2037. } else {
  2038. wp_enqueue_style( 'wp-block-library-theme' );
  2039. }
  2040. }
  2041. /**
  2042. * Fires after enqueuing block assets for both editor and front-end.
  2043. *
  2044. * Call `add_action` on any hook before 'wp_enqueue_scripts'.
  2045. *
  2046. * In the function call you supply, simply use `wp_enqueue_script` and
  2047. * `wp_enqueue_style` to add your functionality to the Gutenberg editor.
  2048. *
  2049. * @since 5.0.0
  2050. */
  2051. do_action( 'enqueue_block_assets' );
  2052. }
  2053. /**
  2054. * Enqueues the global styles defined via theme.json.
  2055. *
  2056. * @since 5.8.0
  2057. */
  2058. function wp_enqueue_global_styles() {
  2059. $separate_assets = wp_should_load_separate_core_block_assets();
  2060. $is_block_theme = wp_is_block_theme();
  2061. $is_classic_theme = ! $is_block_theme;
  2062. /*
  2063. * Global styles should be printed in the head when loading all styles combined.
  2064. * The footer should only be used to print global styles for classic themes with separate core assets enabled.
  2065. *
  2066. * See https://core.trac.wordpress.org/ticket/53494.
  2067. */
  2068. if (
  2069. ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
  2070. ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) ||
  2071. ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets )
  2072. ) {
  2073. return;
  2074. }
  2075. $stylesheet = wp_get_global_stylesheet();
  2076. if ( empty( $stylesheet ) ) {
  2077. return;
  2078. }
  2079. wp_register_style( 'global-styles', false, array(), true, true );
  2080. wp_add_inline_style( 'global-styles', $stylesheet );
  2081. wp_enqueue_style( 'global-styles' );
  2082. }
  2083. /**
  2084. * Renders the SVG filters supplied by theme.json.
  2085. *
  2086. * Note that this doesn't render the per-block user-defined
  2087. * filters which are handled by wp_render_duotone_support,
  2088. * but it should be rendered before the filtered content
  2089. * in the body to satisfy Safari's rendering quirks.
  2090. *
  2091. * @since 5.9.1
  2092. */
  2093. function wp_global_styles_render_svg_filters() {
  2094. /*
  2095. * When calling via the in_admin_header action, we only want to render the
  2096. * SVGs on block editor pages.
  2097. */
  2098. if (
  2099. is_admin() &&
  2100. ! get_current_screen()->is_block_editor()
  2101. ) {
  2102. return;
  2103. }
  2104. $filters = wp_get_global_styles_svg_filters();
  2105. if ( ! empty( $filters ) ) {
  2106. echo $filters;
  2107. }
  2108. }
  2109. /**
  2110. * Checks if the editor scripts and styles for all registered block types
  2111. * should be enqueued on the current screen.
  2112. *
  2113. * @since 5.6.0
  2114. *
  2115. * @global WP_Screen $current_screen WordPress current screen object.
  2116. *
  2117. * @return bool Whether scripts and styles should be enqueued.
  2118. */
  2119. function wp_should_load_block_editor_scripts_and_styles() {
  2120. global $current_screen;
  2121. $is_block_editor_screen = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor();
  2122. /**
  2123. * Filters the flag that decides whether or not block editor scripts and styles
  2124. * are going to be enqueued on the current screen.
  2125. *
  2126. * @since 5.6.0
  2127. *
  2128. * @param bool $is_block_editor_screen Current value of the flag.
  2129. */
  2130. return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
  2131. }
  2132. /**
  2133. * Checks whether separate styles should be loaded for core blocks on-render.
  2134. *
  2135. * When this function returns true, other functions ensure that core blocks
  2136. * only load their assets on-render, and each block loads its own, individual
  2137. * assets. Third-party blocks only load their assets when rendered.
  2138. *
  2139. * When this function returns false, all core block assets are loaded regardless
  2140. * of whether they are rendered in a page or not, because they are all part of
  2141. * the `block-library/style.css` file. Assets for third-party blocks are always
  2142. * enqueued regardless of whether they are rendered or not.
  2143. *
  2144. * This only affects front end and not the block editor screens.
  2145. *
  2146. * @see wp_enqueue_registered_block_scripts_and_styles()
  2147. * @see register_block_style_handle()
  2148. *
  2149. * @since 5.8.0
  2150. *
  2151. * @return bool Whether separate assets will be loaded.
  2152. */
  2153. function wp_should_load_separate_core_block_assets() {
  2154. if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
  2155. return false;
  2156. }
  2157. /**
  2158. * Filters whether block styles should be loaded separately.
  2159. *
  2160. * Returning false loads all core block assets, regardless of whether they are rendered
  2161. * in a page or not. Returning true loads core block assets only when they are rendered.
  2162. *
  2163. * @since 5.8.0
  2164. *
  2165. * @param bool $load_separate_assets Whether separate assets will be loaded.
  2166. * Default false (all block assets are loaded, even when not used).
  2167. */
  2168. return apply_filters( 'should_load_separate_core_block_assets', false );
  2169. }
  2170. /**
  2171. * Enqueues registered block scripts and styles, depending on current rendered
  2172. * context (only enqueuing editor scripts while in context of the editor).
  2173. *
  2174. * @since 5.0.0
  2175. *
  2176. * @global WP_Screen $current_screen WordPress current screen object.
  2177. */
  2178. function wp_enqueue_registered_block_scripts_and_styles() {
  2179. global $current_screen;
  2180. if ( wp_should_load_separate_core_block_assets() ) {
  2181. return;
  2182. }
  2183. $load_editor_scripts = is_admin() && wp_should_load_block_editor_scripts_and_styles();
  2184. $block_registry = WP_Block_Type_Registry::get_instance();
  2185. foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
  2186. // Front-end styles.
  2187. if ( ! empty( $block_type->style ) ) {
  2188. wp_enqueue_style( $block_type->style );
  2189. }
  2190. // Front-end script.
  2191. if ( ! empty( $block_type->script ) ) {
  2192. wp_enqueue_script( $block_type->script );
  2193. }
  2194. // Editor styles.
  2195. if ( $load_editor_scripts && ! empty( $block_type->editor_style ) ) {
  2196. wp_enqueue_style( $block_type->editor_style );
  2197. }
  2198. // Editor script.
  2199. if ( $load_editor_scripts && ! empty( $block_type->editor_script ) ) {
  2200. wp_enqueue_script( $block_type->editor_script );
  2201. }
  2202. }
  2203. }
  2204. /**
  2205. * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
  2206. *
  2207. * @since 5.3.0
  2208. *
  2209. * @global WP_Styles $wp_styles
  2210. */
  2211. function enqueue_block_styles_assets() {
  2212. global $wp_styles;
  2213. $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
  2214. foreach ( $block_styles as $block_name => $styles ) {
  2215. foreach ( $styles as $style_properties ) {
  2216. if ( isset( $style_properties['style_handle'] ) ) {
  2217. // If the site loads separate styles per-block, enqueue the stylesheet on render.
  2218. if ( wp_should_load_separate_core_block_assets() ) {
  2219. add_filter(
  2220. 'render_block',
  2221. function( $html, $block ) use ( $block_name, $style_properties ) {
  2222. if ( $block['blockName'] === $block_name ) {
  2223. wp_enqueue_style( $style_properties['style_handle'] );
  2224. }
  2225. return $html;
  2226. },
  2227. 10,
  2228. 2
  2229. );
  2230. } else {
  2231. wp_enqueue_style( $style_properties['style_handle'] );
  2232. }
  2233. }
  2234. if ( isset( $style_properties['inline_style'] ) ) {
  2235. // Default to "wp-block-library".
  2236. $handle = 'wp-block-library';
  2237. // If the site loads separate styles per-block, check if the block has a stylesheet registered.
  2238. if ( wp_should_load_separate_core_block_assets() ) {
  2239. $block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' );
  2240. if ( isset( $wp_styles->registered[ $block_stylesheet_handle ] ) ) {
  2241. $handle = $block_stylesheet_handle;
  2242. }
  2243. }
  2244. // Add inline styles to the calculated handle.
  2245. wp_add_inline_style( $handle, $style_properties['inline_style'] );
  2246. }
  2247. }
  2248. }
  2249. }
  2250. /**
  2251. * Function responsible for enqueuing the assets required for block styles functionality on the editor.
  2252. *
  2253. * @since 5.3.0
  2254. */
  2255. function enqueue_editor_block_styles_assets() {
  2256. $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
  2257. $register_script_lines = array( '( function() {' );
  2258. foreach ( $block_styles as $block_name => $styles ) {
  2259. foreach ( $styles as $style_properties ) {
  2260. $block_style = array(
  2261. 'name' => $style_properties['name'],
  2262. 'label' => $style_properties['label'],
  2263. );
  2264. if ( isset( $style_properties['is_default'] ) ) {
  2265. $block_style['isDefault'] = $style_properties['is_default'];
  2266. }
  2267. $register_script_lines[] = sprintf(
  2268. ' wp.blocks.registerBlockStyle( \'%s\', %s );',
  2269. $block_name,
  2270. wp_json_encode( $block_style )
  2271. );
  2272. }
  2273. }
  2274. $register_script_lines[] = '} )();';
  2275. $inline_script = implode( "\n", $register_script_lines );
  2276. wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
  2277. wp_add_inline_script( 'wp-block-styles', $inline_script );
  2278. wp_enqueue_script( 'wp-block-styles' );
  2279. }
  2280. /**
  2281. * Enqueues the assets required for the block directory within the block editor.
  2282. *
  2283. * @since 5.5.0
  2284. */
  2285. function wp_enqueue_editor_block_directory_assets() {
  2286. wp_enqueue_script( 'wp-block-directory' );
  2287. wp_enqueue_style( 'wp-block-directory' );
  2288. }
  2289. /**
  2290. * Enqueues the assets required for the format library within the block editor.
  2291. *
  2292. * @since 5.8.0
  2293. */
  2294. function wp_enqueue_editor_format_library_assets() {
  2295. wp_enqueue_script( 'wp-format-library' );
  2296. wp_enqueue_style( 'wp-format-library' );
  2297. }
  2298. /**
  2299. * Sanitizes an attributes array into an attributes string to be placed inside a `<script>` tag.
  2300. *
  2301. * Automatically injects type attribute if needed.
  2302. * Used by {@see wp_get_script_tag()} and {@see wp_get_inline_script_tag()}.
  2303. *
  2304. * @since 5.7.0
  2305. *
  2306. * @param array $attributes Key-value pairs representing `<script>` tag attributes.
  2307. * @return string String made of sanitized `<script>` tag attributes.
  2308. */
  2309. function wp_sanitize_script_attributes( $attributes ) {
  2310. $html5_script_support = ! is_admin() && ! current_theme_supports( 'html5', 'script' );
  2311. $attributes_string = '';
  2312. // If HTML5 script tag is supported, only the attribute name is added
  2313. // to $attributes_string for entries with a boolean value, and that are true.
  2314. foreach ( $attributes as $attribute_name => $attribute_value ) {
  2315. if ( is_bool( $attribute_value ) ) {
  2316. if ( $attribute_value ) {
  2317. $attributes_string .= $html5_script_support ? sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_name ) ) : ' ' . esc_attr( $attribute_name );
  2318. }
  2319. } else {
  2320. $attributes_string .= sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
  2321. }
  2322. }
  2323. return $attributes_string;
  2324. }
  2325. /**
  2326. * Formats `<script>` loader tags.
  2327. *
  2328. * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
  2329. * Automatically injects type attribute if needed.
  2330. *
  2331. * @since 5.7.0
  2332. *
  2333. * @param array $attributes Key-value pairs representing `<script>` tag attributes.
  2334. * @return string String containing `<script>` opening and closing tags.
  2335. */
  2336. function wp_get_script_tag( $attributes ) {
  2337. if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
  2338. $attributes['type'] = 'text/javascript';
  2339. }
  2340. /**
  2341. * Filters attributes to be added to a script tag.
  2342. *
  2343. * @since 5.7.0
  2344. *
  2345. * @param array $attributes Key-value pairs representing `<script>` tag attributes.
  2346. * Only the attribute name is added to the `<script>` tag for
  2347. * entries with a boolean value, and that are true.
  2348. */
  2349. $attributes = apply_filters( 'wp_script_attributes', $attributes );
  2350. return sprintf( "<script%s></script>\n", wp_sanitize_script_attributes( $attributes ) );
  2351. }
  2352. /**
  2353. * Prints formatted `<script>` loader tag.
  2354. *
  2355. * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
  2356. * Automatically injects type attribute if needed.
  2357. *
  2358. * @since 5.7.0
  2359. *
  2360. * @param array $attributes Key-value pairs representing `<script>` tag attributes.
  2361. */
  2362. function wp_print_script_tag( $attributes ) {
  2363. echo wp_get_script_tag( $attributes );
  2364. }
  2365. /**
  2366. * Wraps inline JavaScript in `<script>` tag.
  2367. *
  2368. * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
  2369. * Automatically injects type attribute if needed.
  2370. *
  2371. * @since 5.7.0
  2372. *
  2373. * @param string $javascript Inline JavaScript code.
  2374. * @param array $attributes Optional. Key-value pairs representing `<script>` tag attributes.
  2375. * @return string String containing inline JavaScript code wrapped around `<script>` tag.
  2376. */
  2377. function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
  2378. if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
  2379. $attributes['type'] = 'text/javascript';
  2380. }
  2381. /**
  2382. * Filters attributes to be added to a script tag.
  2383. *
  2384. * @since 5.7.0
  2385. *
  2386. * @param array $attributes Key-value pairs representing `<script>` tag attributes.
  2387. * Only the attribute name is added to the `<script>` tag for
  2388. * entries with a boolean value, and that are true.
  2389. * @param string $javascript Inline JavaScript code.
  2390. */
  2391. $attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $javascript );
  2392. $javascript = "\n" . trim( $javascript, "\n\r " ) . "\n";
  2393. return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $javascript );
  2394. }
  2395. /**
  2396. * Prints inline JavaScript wrapped in `<script>` tag.
  2397. *
  2398. * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
  2399. * Automatically injects type attribute if needed.
  2400. *
  2401. * @since 5.7.0
  2402. *
  2403. * @param string $javascript Inline JavaScript code.
  2404. * @param array $attributes Optional. Key-value pairs representing `<script>` tag attributes.
  2405. */
  2406. function wp_print_inline_script_tag( $javascript, $attributes = array() ) {
  2407. echo wp_get_inline_script_tag( $javascript, $attributes );
  2408. }
  2409. /**
  2410. * Allows small styles to be inlined.
  2411. *
  2412. * This improves performance and sustainability, and is opt-in. Stylesheets can opt in
  2413. * by adding `path` data using `wp_style_add_data`, and defining the file's absolute path:
  2414. *
  2415. * wp_style_add_data( $style_handle, 'path', $file_path );
  2416. *
  2417. * @since 5.8.0
  2418. *
  2419. * @global WP_Styles $wp_styles
  2420. */
  2421. function wp_maybe_inline_styles() {
  2422. global $wp_styles;
  2423. $total_inline_limit = 20000;
  2424. /**
  2425. * The maximum size of inlined styles in bytes.
  2426. *
  2427. * @since 5.8.0
  2428. *
  2429. * @param int $total_inline_limit The file-size threshold, in bytes. Default 20000.
  2430. */
  2431. $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit );
  2432. $styles = array();
  2433. // Build an array of styles that have a path defined.
  2434. foreach ( $wp_styles->queue as $handle ) {
  2435. if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) {
  2436. $styles[] = array(
  2437. 'handle' => $handle,
  2438. 'src' => $wp_styles->registered[ $handle ]->src,
  2439. 'path' => $wp_styles->registered[ $handle ]->extra['path'],
  2440. 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ),
  2441. );
  2442. }
  2443. }
  2444. if ( ! empty( $styles ) ) {
  2445. // Reorder styles array based on size.
  2446. usort(
  2447. $styles,
  2448. static function( $a, $b ) {
  2449. return ( $a['size'] <= $b['size'] ) ? -1 : 1;
  2450. }
  2451. );
  2452. /*
  2453. * The total inlined size.
  2454. *
  2455. * On each iteration of the loop, if a style gets added inline the value of this var increases
  2456. * to reflect the total size of inlined styles.
  2457. */
  2458. $total_inline_size = 0;
  2459. // Loop styles.
  2460. foreach ( $styles as $style ) {
  2461. // Size check. Since styles are ordered by size, we can break the loop.
  2462. if ( $total_inline_size + $style['size'] > $total_inline_limit ) {
  2463. break;
  2464. }
  2465. // Get the styles if we don't already have them.
  2466. $style['css'] = file_get_contents( $style['path'] );
  2467. // Check if the style contains relative URLs that need to be modified.
  2468. // URLs relative to the stylesheet's path should be converted to relative to the site's root.
  2469. $style['css'] = _wp_normalize_relative_css_links( $style['css'], $style['src'] );
  2470. // Set `src` to `false` and add styles inline.
  2471. $wp_styles->registered[ $style['handle'] ]->src = false;
  2472. if ( empty( $wp_styles->registered[ $style['handle'] ]->extra['after'] ) ) {
  2473. $wp_styles->registered[ $style['handle'] ]->extra['after'] = array();
  2474. }
  2475. array_unshift( $wp_styles->registered[ $style['handle'] ]->extra['after'], $style['css'] );
  2476. // Add the styles size to the $total_inline_size var.
  2477. $total_inline_size += (int) $style['size'];
  2478. }
  2479. }
  2480. }
  2481. /**
  2482. * Makes URLs relative to the WordPress installation.
  2483. *
  2484. * @since 5.9.0
  2485. * @access private
  2486. *
  2487. * @param string $css The CSS to make URLs relative to the WordPress installation.
  2488. * @param string $stylesheet_url The URL to the stylesheet.
  2489. *
  2490. * @return string The CSS with URLs made relative to the WordPress installation.
  2491. */
  2492. function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
  2493. $has_src_results = preg_match_all( '#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results );
  2494. if ( $has_src_results ) {
  2495. // Loop through the URLs to find relative ones.
  2496. foreach ( $src_results[1] as $src_index => $src_result ) {
  2497. // Skip if this is an absolute URL.
  2498. if ( 0 === strpos( $src_result, 'http' ) || 0 === strpos( $src_result, '//' ) ) {
  2499. continue;
  2500. }
  2501. // Skip if the URL is an HTML ID.
  2502. if ( str_starts_with( $src_result, '#' ) ) {
  2503. continue;
  2504. }
  2505. // Skip if the URL is a data URI.
  2506. if ( str_starts_with( $src_result, 'data:' ) ) {
  2507. continue;
  2508. }
  2509. // Build the absolute URL.
  2510. $absolute_url = dirname( $stylesheet_url ) . '/' . $src_result;
  2511. $absolute_url = str_replace( '/./', '/', $absolute_url );
  2512. // Convert to URL related to the site root.
  2513. $relative_url = wp_make_link_relative( $absolute_url );
  2514. // Replace the URL in the CSS.
  2515. $css = str_replace(
  2516. $src_results[0][ $src_index ],
  2517. str_replace( $src_result, $relative_url, $src_results[0][ $src_index ] ),
  2518. $css
  2519. );
  2520. }
  2521. }
  2522. return $css;
  2523. }
  2524. /**
  2525. * Function that enqueues the CSS Custom Properties coming from theme.json.
  2526. *
  2527. * @since 5.9.0
  2528. */
  2529. function wp_enqueue_global_styles_css_custom_properties() {
  2530. wp_register_style( 'global-styles-css-custom-properties', false, array(), true, true );
  2531. wp_add_inline_style( 'global-styles-css-custom-properties', wp_get_global_stylesheet( array( 'variables' ) ) );
  2532. wp_enqueue_style( 'global-styles-css-custom-properties' );
  2533. }
  2534. /**
  2535. * This function takes care of adding inline styles
  2536. * in the proper place, depending on the theme in use.
  2537. *
  2538. * @since 5.9.1
  2539. *
  2540. * For block themes, it's loaded in the head.
  2541. * For classic ones, it's loaded in the body
  2542. * because the wp_head action happens before
  2543. * the render_block.
  2544. *
  2545. * @link https://core.trac.wordpress.org/ticket/53494.
  2546. *
  2547. * @param string $style String containing the CSS styles to be added.
  2548. */
  2549. function wp_enqueue_block_support_styles( $style ) {
  2550. $action_hook_name = 'wp_footer';
  2551. if ( wp_is_block_theme() ) {
  2552. $action_hook_name = 'wp_head';
  2553. }
  2554. add_action(
  2555. $action_hook_name,
  2556. static function () use ( $style ) {
  2557. echo "<style>$style</style>\n";
  2558. }
  2559. );
  2560. }
  2561. /**
  2562. * Enqueues a stylesheet for a specific block.
  2563. *
  2564. * If the theme has opted-in to separate-styles loading,
  2565. * then the stylesheet will be enqueued on-render,
  2566. * otherwise when the block inits.
  2567. *
  2568. * @since 5.9.0
  2569. *
  2570. * @param string $block_name The block-name, including namespace.
  2571. * @param array $args An array of arguments [handle,src,deps,ver,media].
  2572. */
  2573. function wp_enqueue_block_style( $block_name, $args ) {
  2574. $args = wp_parse_args(
  2575. $args,
  2576. array(
  2577. 'handle' => '',
  2578. 'src' => '',
  2579. 'deps' => array(),
  2580. 'ver' => false,
  2581. 'media' => 'all',
  2582. )
  2583. );
  2584. /**
  2585. * Callback function to register and enqueue styles.
  2586. *
  2587. * @param string $content When the callback is used for the render_block filter,
  2588. * the content needs to be returned so the function parameter
  2589. * is to ensure the content exists.
  2590. * @return string Block content.
  2591. */
  2592. $callback = static function( $content ) use ( $args ) {
  2593. // Register the stylesheet.
  2594. if ( ! empty( $args['src'] ) ) {
  2595. wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );
  2596. }
  2597. // Add `path` data if provided.
  2598. if ( isset( $args['path'] ) ) {
  2599. wp_style_add_data( $args['handle'], 'path', $args['path'] );
  2600. // Get the RTL file path.
  2601. $rtl_file_path = str_replace( '.css', '-rtl.css', $args['path'] );
  2602. // Add RTL stylesheet.
  2603. if ( file_exists( $rtl_file_path ) ) {
  2604. wp_style_add_data( $args['handle'], 'rtl', 'replace' );
  2605. if ( is_rtl() ) {
  2606. wp_style_add_data( $args['handle'], 'path', $rtl_file_path );
  2607. }
  2608. }
  2609. }
  2610. // Enqueue the stylesheet.
  2611. wp_enqueue_style( $args['handle'] );
  2612. return $content;
  2613. };
  2614. $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
  2615. if ( wp_should_load_separate_core_block_assets() ) {
  2616. /**
  2617. * Callback function to register and enqueue styles.
  2618. *
  2619. * @param string $content The block content.
  2620. * @param array $block The full block, including name and attributes.
  2621. * @return string Block content.
  2622. */
  2623. $callback_separate = static function( $content, $block ) use ( $block_name, $callback ) {
  2624. if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {
  2625. return $callback( $content );
  2626. }
  2627. return $content;
  2628. };
  2629. /*
  2630. * The filter's callback here is an anonymous function because
  2631. * using a named function in this case is not possible.
  2632. *
  2633. * The function cannot be unhooked, however, users are still able
  2634. * to dequeue the stylesheets registered/enqueued by the callback
  2635. * which is why in this case, using an anonymous function
  2636. * was deemed acceptable.
  2637. */
  2638. add_filter( 'render_block', $callback_separate, 10, 2 );
  2639. return;
  2640. }
  2641. /*
  2642. * The filter's callback here is an anonymous function because
  2643. * using a named function in this case is not possible.
  2644. *
  2645. * The function cannot be unhooked, however, users are still able
  2646. * to dequeue the stylesheets registered/enqueued by the callback
  2647. * which is why in this case, using an anonymous function
  2648. * was deemed acceptable.
  2649. */
  2650. add_filter( $hook, $callback );
  2651. // Enqueue assets in the editor.
  2652. add_action( 'enqueue_block_assets', $callback );
  2653. }
  2654. /**
  2655. * Runs the theme.json webfonts handler.
  2656. *
  2657. * Using `WP_Theme_JSON_Resolver`, it gets the fonts defined
  2658. * in the `theme.json` for the current selection and style
  2659. * variations, validates the font-face properties, generates
  2660. * the '@font-face' style declarations, and then enqueues the
  2661. * styles for both the editor and front-end.
  2662. *
  2663. * Design Notes:
  2664. * This is not a public API, but rather an internal handler.
  2665. * A future public Webfonts API will replace this stopgap code.
  2666. *
  2667. * This code design is intentional.
  2668. * a. It hides the inner-workings.
  2669. * b. It does not expose API ins or outs for consumption.
  2670. * c. It only works with a theme's `theme.json`.
  2671. *
  2672. * Why?
  2673. * a. To avoid backwards-compatibility issues when
  2674. * the Webfonts API is introduced in Core.
  2675. * b. To make `fontFace` declarations in `theme.json` work.
  2676. *
  2677. * @link https://github.com/WordPress/gutenberg/issues/40472
  2678. *
  2679. * @since 6.0.0
  2680. * @access private
  2681. */
  2682. function _wp_theme_json_webfonts_handler() {
  2683. // Block themes are unavailable during installation.
  2684. if ( wp_installing() ) {
  2685. return;
  2686. }
  2687. // Webfonts to be processed.
  2688. $registered_webfonts = array();
  2689. /**
  2690. * Gets the webfonts from theme.json.
  2691. *
  2692. * @since 6.0.0
  2693. *
  2694. * @return array Array of defined webfonts.
  2695. */
  2696. $fn_get_webfonts_from_theme_json = static function() {
  2697. // Get settings from theme.json.
  2698. $settings = WP_Theme_JSON_Resolver::get_merged_data()->get_settings();
  2699. // If in the editor, add webfonts defined in variations.
  2700. if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
  2701. $variations = WP_Theme_JSON_Resolver::get_style_variations();
  2702. foreach ( $variations as $variation ) {
  2703. // Skip if fontFamilies are not defined in the variation.
  2704. if ( empty( $variation['settings']['typography']['fontFamilies'] ) ) {
  2705. continue;
  2706. }
  2707. // Initialize the array structure.
  2708. if ( empty( $settings['typography'] ) ) {
  2709. $settings['typography'] = array();
  2710. }
  2711. if ( empty( $settings['typography']['fontFamilies'] ) ) {
  2712. $settings['typography']['fontFamilies'] = array();
  2713. }
  2714. if ( empty( $settings['typography']['fontFamilies']['theme'] ) ) {
  2715. $settings['typography']['fontFamilies']['theme'] = array();
  2716. }
  2717. // Combine variations with settings. Remove duplicates.
  2718. $settings['typography']['fontFamilies']['theme'] = array_merge( $settings['typography']['fontFamilies']['theme'], $variation['settings']['typography']['fontFamilies']['theme'] );
  2719. $settings['typography']['fontFamilies'] = array_unique( $settings['typography']['fontFamilies'] );
  2720. }
  2721. }
  2722. // Bail out early if there are no settings for webfonts.
  2723. if ( empty( $settings['typography']['fontFamilies'] ) ) {
  2724. return array();
  2725. }
  2726. $webfonts = array();
  2727. // Look for fontFamilies.
  2728. foreach ( $settings['typography']['fontFamilies'] as $font_families ) {
  2729. foreach ( $font_families as $font_family ) {
  2730. // Skip if fontFace is not defined.
  2731. if ( empty( $font_family['fontFace'] ) ) {
  2732. continue;
  2733. }
  2734. // Skip if fontFace is not an array of webfonts.
  2735. if ( ! is_array( $font_family['fontFace'] ) ) {
  2736. continue;
  2737. }
  2738. $webfonts = array_merge( $webfonts, $font_family['fontFace'] );
  2739. }
  2740. }
  2741. return $webfonts;
  2742. };
  2743. /**
  2744. * Transforms each 'src' into an URI by replacing 'file:./'
  2745. * placeholder from theme.json.
  2746. *
  2747. * The absolute path to the webfont file(s) cannot be defined in
  2748. * theme.json. `file:./` is the placeholder which is replaced by
  2749. * the theme's URL path to the theme's root.
  2750. *
  2751. * @since 6.0.0
  2752. *
  2753. * @param array $src Webfont file(s) `src`.
  2754. * @return array Webfont's `src` in URI.
  2755. */
  2756. $fn_transform_src_into_uri = static function( array $src ) {
  2757. foreach ( $src as $key => $url ) {
  2758. // Tweak the URL to be relative to the theme root.
  2759. if ( ! str_starts_with( $url, 'file:./' ) ) {
  2760. continue;
  2761. }
  2762. $src[ $key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) );
  2763. }
  2764. return $src;
  2765. };
  2766. /**
  2767. * Converts the font-face properties (i.e. keys) into kebab-case.
  2768. *
  2769. * @since 6.0.0
  2770. *
  2771. * @param array $font_face Font face to convert.
  2772. * @return array Font faces with each property in kebab-case format.
  2773. */
  2774. $fn_convert_keys_to_kebab_case = static function( array $font_face ) {
  2775. foreach ( $font_face as $property => $value ) {
  2776. $kebab_case = _wp_to_kebab_case( $property );
  2777. $font_face[ $kebab_case ] = $value;
  2778. if ( $kebab_case !== $property ) {
  2779. unset( $font_face[ $property ] );
  2780. }
  2781. }
  2782. return $font_face;
  2783. };
  2784. /**
  2785. * Validates a webfont.
  2786. *
  2787. * @since 6.0.0
  2788. *
  2789. * @param array $webfont The webfont arguments.
  2790. * @return array|false The validated webfont arguments, or false if the webfont is invalid.
  2791. */
  2792. $fn_validate_webfont = static function( $webfont ) {
  2793. $webfont = wp_parse_args(
  2794. $webfont,
  2795. array(
  2796. 'font-family' => '',
  2797. 'font-style' => 'normal',
  2798. 'font-weight' => '400',
  2799. 'font-display' => 'fallback',
  2800. 'src' => array(),
  2801. )
  2802. );
  2803. // Check the font-family.
  2804. if ( empty( $webfont['font-family'] ) || ! is_string( $webfont['font-family'] ) ) {
  2805. trigger_error( __( 'Webfont font family must be a non-empty string.' ) );
  2806. return false;
  2807. }
  2808. // Check that the `src` property is defined and a valid type.
  2809. if ( empty( $webfont['src'] ) || ( ! is_string( $webfont['src'] ) && ! is_array( $webfont['src'] ) ) ) {
  2810. trigger_error( __( 'Webfont src must be a non-empty string or an array of strings.' ) );
  2811. return false;
  2812. }
  2813. // Validate the `src` property.
  2814. foreach ( (array) $webfont['src'] as $src ) {
  2815. if ( ! is_string( $src ) || '' === trim( $src ) ) {
  2816. trigger_error( __( 'Each webfont src must be a non-empty string.' ) );
  2817. return false;
  2818. }
  2819. }
  2820. // Check the font-weight.
  2821. if ( ! is_string( $webfont['font-weight'] ) && ! is_int( $webfont['font-weight'] ) ) {
  2822. trigger_error( __( 'Webfont font weight must be a properly formatted string or integer.' ) );
  2823. return false;
  2824. }
  2825. // Check the font-display.
  2826. if ( ! in_array( $webfont['font-display'], array( 'auto', 'block', 'fallback', 'swap' ), true ) ) {
  2827. $webfont['font-display'] = 'fallback';
  2828. }
  2829. $valid_props = array(
  2830. 'ascend-override',
  2831. 'descend-override',
  2832. 'font-display',
  2833. 'font-family',
  2834. 'font-stretch',
  2835. 'font-style',
  2836. 'font-weight',
  2837. 'font-variant',
  2838. 'font-feature-settings',
  2839. 'font-variation-settings',
  2840. 'line-gap-override',
  2841. 'size-adjust',
  2842. 'src',
  2843. 'unicode-range',
  2844. );
  2845. foreach ( $webfont as $prop => $value ) {
  2846. if ( ! in_array( $prop, $valid_props, true ) ) {
  2847. unset( $webfont[ $prop ] );
  2848. }
  2849. }
  2850. return $webfont;
  2851. };
  2852. /**
  2853. * Registers webfonts declared in theme.json.
  2854. *
  2855. * @since 6.0.0
  2856. *
  2857. * @uses $registered_webfonts To access and update the registered webfonts registry (passed by reference).
  2858. * @uses $fn_get_webfonts_from_theme_json To run the function that gets the webfonts from theme.json.
  2859. * @uses $fn_convert_keys_to_kebab_case To run the function that converts keys into kebab-case.
  2860. * @uses $fn_validate_webfont To run the function that validates each font-face (webfont) from theme.json.
  2861. */
  2862. $fn_register_webfonts = static function() use ( &$registered_webfonts, $fn_get_webfonts_from_theme_json, $fn_convert_keys_to_kebab_case, $fn_validate_webfont, $fn_transform_src_into_uri ) {
  2863. $registered_webfonts = array();
  2864. foreach ( $fn_get_webfonts_from_theme_json() as $webfont ) {
  2865. if ( ! is_array( $webfont ) ) {
  2866. continue;
  2867. }
  2868. $webfont = $fn_convert_keys_to_kebab_case( $webfont );
  2869. $webfont = $fn_validate_webfont( $webfont );
  2870. $webfont['src'] = $fn_transform_src_into_uri( (array) $webfont['src'] );
  2871. // Skip if not valid.
  2872. if ( empty( $webfont ) ) {
  2873. continue;
  2874. }
  2875. $registered_webfonts[] = $webfont;
  2876. }
  2877. };
  2878. /**
  2879. * Orders 'src' items to optimize for browser support.
  2880. *
  2881. * @since 6.0.0
  2882. *
  2883. * @param array $webfont Webfont to process.
  2884. * @return array Ordered `src` items.
  2885. */
  2886. $fn_order_src = static function( array $webfont ) {
  2887. $src = array();
  2888. $src_ordered = array();
  2889. foreach ( $webfont['src'] as $url ) {
  2890. // Add data URIs first.
  2891. if ( str_starts_with( trim( $url ), 'data:' ) ) {
  2892. $src_ordered[] = array(
  2893. 'url' => $url,
  2894. 'format' => 'data',
  2895. );
  2896. continue;
  2897. }
  2898. $format = pathinfo( $url, PATHINFO_EXTENSION );
  2899. $src[ $format ] = $url;
  2900. }
  2901. // Add woff2.
  2902. if ( ! empty( $src['woff2'] ) ) {
  2903. $src_ordered[] = array(
  2904. 'url' => sanitize_url( $src['woff2'] ),
  2905. 'format' => 'woff2',
  2906. );
  2907. }
  2908. // Add woff.
  2909. if ( ! empty( $src['woff'] ) ) {
  2910. $src_ordered[] = array(
  2911. 'url' => sanitize_url( $src['woff'] ),
  2912. 'format' => 'woff',
  2913. );
  2914. }
  2915. // Add ttf.
  2916. if ( ! empty( $src['ttf'] ) ) {
  2917. $src_ordered[] = array(
  2918. 'url' => sanitize_url( $src['ttf'] ),
  2919. 'format' => 'truetype',
  2920. );
  2921. }
  2922. // Add eot.
  2923. if ( ! empty( $src['eot'] ) ) {
  2924. $src_ordered[] = array(
  2925. 'url' => sanitize_url( $src['eot'] ),
  2926. 'format' => 'embedded-opentype',
  2927. );
  2928. }
  2929. // Add otf.
  2930. if ( ! empty( $src['otf'] ) ) {
  2931. $src_ordered[] = array(
  2932. 'url' => sanitize_url( $src['otf'] ),
  2933. 'format' => 'opentype',
  2934. );
  2935. }
  2936. $webfont['src'] = $src_ordered;
  2937. return $webfont;
  2938. };
  2939. /**
  2940. * Compiles the 'src' into valid CSS.
  2941. *
  2942. * @since 6.0.0
  2943. *
  2944. * @param string $font_family Font family.
  2945. * @param array $value Value to process.
  2946. * @return string The CSS.
  2947. */
  2948. $fn_compile_src = static function( $font_family, array $value ) {
  2949. $src = "local($font_family)";
  2950. foreach ( $value as $item ) {
  2951. if (
  2952. str_starts_with( $item['url'], site_url() ) ||
  2953. str_starts_with( $item['url'], home_url() )
  2954. ) {
  2955. $item['url'] = wp_make_link_relative( $item['url'] );
  2956. }
  2957. $src .= ( 'data' === $item['format'] )
  2958. ? ", url({$item['url']})"
  2959. : ", url('{$item['url']}') format('{$item['format']}')";
  2960. }
  2961. return $src;
  2962. };
  2963. /**
  2964. * Compiles the font variation settings.
  2965. *
  2966. * @since 6.0.0
  2967. *
  2968. * @param array $font_variation_settings Array of font variation settings.
  2969. * @return string The CSS.
  2970. */
  2971. $fn_compile_variations = static function( array $font_variation_settings ) {
  2972. $variations = '';
  2973. foreach ( $font_variation_settings as $key => $value ) {
  2974. $variations .= "$key $value";
  2975. }
  2976. return $variations;
  2977. };
  2978. /**
  2979. * Builds the font-family's CSS.
  2980. *
  2981. * @since 6.0.0
  2982. *
  2983. * @uses $fn_compile_src To run the function that compiles the src.
  2984. * @uses $fn_compile_variations To run the function that compiles the variations.
  2985. *
  2986. * @param array $webfont Webfont to process.
  2987. * @return string This font-family's CSS.
  2988. */
  2989. $fn_build_font_face_css = static function( array $webfont ) use ( $fn_compile_src, $fn_compile_variations ) {
  2990. $css = '';
  2991. // Wrap font-family in quotes if it contains spaces.
  2992. if (
  2993. str_contains( $webfont['font-family'], ' ' ) &&
  2994. ! str_contains( $webfont['font-family'], '"' ) &&
  2995. ! str_contains( $webfont['font-family'], "'" )
  2996. ) {
  2997. $webfont['font-family'] = '"' . $webfont['font-family'] . '"';
  2998. }
  2999. foreach ( $webfont as $key => $value ) {
  3000. /*
  3001. * Skip "provider", since it's for internal API use,
  3002. * and not a valid CSS property.
  3003. */
  3004. if ( 'provider' === $key ) {
  3005. continue;
  3006. }
  3007. // Compile the "src" parameter.
  3008. if ( 'src' === $key ) {
  3009. $value = $fn_compile_src( $webfont['font-family'], $value );
  3010. }
  3011. // If font-variation-settings is an array, convert it to a string.
  3012. if ( 'font-variation-settings' === $key && is_array( $value ) ) {
  3013. $value = $fn_compile_variations( $value );
  3014. }
  3015. if ( ! empty( $value ) ) {
  3016. $css .= "$key:$value;";
  3017. }
  3018. }
  3019. return $css;
  3020. };
  3021. /**
  3022. * Gets the '@font-face' CSS styles for locally-hosted font files.
  3023. *
  3024. * @since 6.0.0
  3025. *
  3026. * @uses $registered_webfonts To access and update the registered webfonts registry (passed by reference).
  3027. * @uses $fn_order_src To run the function that orders the src.
  3028. * @uses $fn_build_font_face_css To run the function that builds the font-face CSS.
  3029. *
  3030. * @return string The `@font-face` CSS.
  3031. */
  3032. $fn_get_css = static function() use ( &$registered_webfonts, $fn_order_src, $fn_build_font_face_css ) {
  3033. $css = '';
  3034. foreach ( $registered_webfonts as $webfont ) {
  3035. // Order the webfont's `src` items to optimize for browser support.
  3036. $webfont = $fn_order_src( $webfont );
  3037. // Build the @font-face CSS for this webfont.
  3038. $css .= '@font-face{' . $fn_build_font_face_css( $webfont ) . '}';
  3039. }
  3040. return $css;
  3041. };
  3042. /**
  3043. * Generates and enqueues webfonts styles.
  3044. *
  3045. * @since 6.0.0
  3046. *
  3047. * @uses $fn_get_css To run the function that gets the CSS.
  3048. */
  3049. $fn_generate_and_enqueue_styles = static function() use ( $fn_get_css ) {
  3050. // Generate the styles.
  3051. $styles = $fn_get_css();
  3052. // Bail out if there are no styles to enqueue.
  3053. if ( '' === $styles ) {
  3054. return;
  3055. }
  3056. // Enqueue the stylesheet.
  3057. wp_register_style( 'wp-webfonts', '' );
  3058. wp_enqueue_style( 'wp-webfonts' );
  3059. // Add the styles to the stylesheet.
  3060. wp_add_inline_style( 'wp-webfonts', $styles );
  3061. };
  3062. /**
  3063. * Generates and enqueues editor styles.
  3064. *
  3065. * @since 6.0.0
  3066. *
  3067. * @uses $fn_get_css To run the function that gets the CSS.
  3068. */
  3069. $fn_generate_and_enqueue_editor_styles = static function() use ( $fn_get_css ) {
  3070. // Generate the styles.
  3071. $styles = $fn_get_css();
  3072. // Bail out if there are no styles to enqueue.
  3073. if ( '' === $styles ) {
  3074. return;
  3075. }
  3076. wp_add_inline_style( 'wp-block-library', $styles );
  3077. };
  3078. add_action( 'wp_loaded', $fn_register_webfonts );
  3079. add_action( 'wp_enqueue_scripts', $fn_generate_and_enqueue_styles );
  3080. add_action( 'admin_init', $fn_generate_and_enqueue_editor_styles );
  3081. }