theme.php 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279
  1. <?php
  2. /**
  3. * Theme, template, and stylesheet functions.
  4. *
  5. * @package WordPress
  6. * @subpackage Theme
  7. */
  8. /**
  9. * Returns an array of WP_Theme objects based on the arguments.
  10. *
  11. * Despite advances over get_themes(), this function is quite expensive, and grows
  12. * linearly with additional themes. Stick to wp_get_theme() if possible.
  13. *
  14. * @since 3.4.0
  15. *
  16. * @global array $wp_theme_directories
  17. *
  18. * @param array $args {
  19. * Optional. The search arguments.
  20. *
  21. * @type mixed $errors True to return themes with errors, false to return
  22. * themes without errors, null to return all themes.
  23. * Default false.
  24. * @type mixed $allowed (Multisite) True to return only allowed themes for a site.
  25. * False to return only disallowed themes for a site.
  26. * 'site' to return only site-allowed themes.
  27. * 'network' to return only network-allowed themes.
  28. * Null to return all themes. Default null.
  29. * @type int $blog_id (Multisite) The blog ID used to calculate which themes
  30. * are allowed. Default 0, synonymous for the current blog.
  31. * }
  32. * @return WP_Theme[] Array of WP_Theme objects.
  33. */
  34. function wp_get_themes( $args = array() ) {
  35. global $wp_theme_directories;
  36. $defaults = array(
  37. 'errors' => false,
  38. 'allowed' => null,
  39. 'blog_id' => 0,
  40. );
  41. $args = wp_parse_args( $args, $defaults );
  42. $theme_directories = search_theme_directories();
  43. if ( is_array( $wp_theme_directories ) && count( $wp_theme_directories ) > 1 ) {
  44. // Make sure the active theme wins out, in case search_theme_directories() picks the wrong
  45. // one in the case of a conflict. (Normally, last registered theme root wins.)
  46. $current_theme = get_stylesheet();
  47. if ( isset( $theme_directories[ $current_theme ] ) ) {
  48. $root_of_current_theme = get_raw_theme_root( $current_theme );
  49. if ( ! in_array( $root_of_current_theme, $wp_theme_directories, true ) ) {
  50. $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme;
  51. }
  52. $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme;
  53. }
  54. }
  55. if ( empty( $theme_directories ) ) {
  56. return array();
  57. }
  58. if ( is_multisite() && null !== $args['allowed'] ) {
  59. $allowed = $args['allowed'];
  60. if ( 'network' === $allowed ) {
  61. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() );
  62. } elseif ( 'site' === $allowed ) {
  63. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) );
  64. } elseif ( $allowed ) {
  65. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
  66. } else {
  67. $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
  68. }
  69. }
  70. $themes = array();
  71. static $_themes = array();
  72. foreach ( $theme_directories as $theme => $theme_root ) {
  73. if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) ) {
  74. $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ];
  75. } else {
  76. $themes[ $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] );
  77. $_themes[ $theme_root['theme_root'] . '/' . $theme ] = $themes[ $theme ];
  78. }
  79. }
  80. if ( null !== $args['errors'] ) {
  81. foreach ( $themes as $theme => $wp_theme ) {
  82. if ( $wp_theme->errors() != $args['errors'] ) {
  83. unset( $themes[ $theme ] );
  84. }
  85. }
  86. }
  87. return $themes;
  88. }
  89. /**
  90. * Gets a WP_Theme object for a theme.
  91. *
  92. * @since 3.4.0
  93. *
  94. * @global array $wp_theme_directories
  95. *
  96. * @param string $stylesheet Optional. Directory name for the theme. Defaults to active theme.
  97. * @param string $theme_root Optional. Absolute path of the theme root to look in.
  98. * If not specified, get_raw_theme_root() is used to calculate
  99. * the theme root for the $stylesheet provided (or active theme).
  100. * @return WP_Theme Theme object. Be sure to check the object's exists() method
  101. * if you need to confirm the theme's existence.
  102. */
  103. function wp_get_theme( $stylesheet = '', $theme_root = '' ) {
  104. global $wp_theme_directories;
  105. if ( empty( $stylesheet ) ) {
  106. $stylesheet = get_stylesheet();
  107. }
  108. if ( empty( $theme_root ) ) {
  109. $theme_root = get_raw_theme_root( $stylesheet );
  110. if ( false === $theme_root ) {
  111. $theme_root = WP_CONTENT_DIR . '/themes';
  112. } elseif ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
  113. $theme_root = WP_CONTENT_DIR . $theme_root;
  114. }
  115. }
  116. return new WP_Theme( $stylesheet, $theme_root );
  117. }
  118. /**
  119. * Clears the cache held by get_theme_roots() and WP_Theme.
  120. *
  121. * @since 3.5.0
  122. * @param bool $clear_update_cache Whether to clear the theme updates cache.
  123. */
  124. function wp_clean_themes_cache( $clear_update_cache = true ) {
  125. if ( $clear_update_cache ) {
  126. delete_site_transient( 'update_themes' );
  127. }
  128. search_theme_directories( true );
  129. foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme ) {
  130. $theme->cache_delete();
  131. }
  132. }
  133. /**
  134. * Whether a child theme is in use.
  135. *
  136. * @since 3.0.0
  137. *
  138. * @return bool True if a child theme is in use, false otherwise.
  139. */
  140. function is_child_theme() {
  141. return ( TEMPLATEPATH !== STYLESHEETPATH );
  142. }
  143. /**
  144. * Retrieves name of the current stylesheet.
  145. *
  146. * The theme name that is currently set as the front end theme.
  147. *
  148. * For all intents and purposes, the template name and the stylesheet name
  149. * are going to be the same for most cases.
  150. *
  151. * @since 1.5.0
  152. *
  153. * @return string Stylesheet name.
  154. */
  155. function get_stylesheet() {
  156. /**
  157. * Filters the name of current stylesheet.
  158. *
  159. * @since 1.5.0
  160. *
  161. * @param string $stylesheet Name of the current stylesheet.
  162. */
  163. return apply_filters( 'stylesheet', get_option( 'stylesheet' ) );
  164. }
  165. /**
  166. * Retrieves stylesheet directory path for the active theme.
  167. *
  168. * @since 1.5.0
  169. *
  170. * @return string Path to active theme's stylesheet directory.
  171. */
  172. function get_stylesheet_directory() {
  173. $stylesheet = get_stylesheet();
  174. $theme_root = get_theme_root( $stylesheet );
  175. $stylesheet_dir = "$theme_root/$stylesheet";
  176. /**
  177. * Filters the stylesheet directory path for the active theme.
  178. *
  179. * @since 1.5.0
  180. *
  181. * @param string $stylesheet_dir Absolute path to the active theme.
  182. * @param string $stylesheet Directory name of the active theme.
  183. * @param string $theme_root Absolute path to themes directory.
  184. */
  185. return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
  186. }
  187. /**
  188. * Retrieves stylesheet directory URI for the active theme.
  189. *
  190. * @since 1.5.0
  191. *
  192. * @return string URI to active theme's stylesheet directory.
  193. */
  194. function get_stylesheet_directory_uri() {
  195. $stylesheet = str_replace( '%2F', '/', rawurlencode( get_stylesheet() ) );
  196. $theme_root_uri = get_theme_root_uri( $stylesheet );
  197. $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
  198. /**
  199. * Filters the stylesheet directory URI.
  200. *
  201. * @since 1.5.0
  202. *
  203. * @param string $stylesheet_dir_uri Stylesheet directory URI.
  204. * @param string $stylesheet Name of the activated theme's directory.
  205. * @param string $theme_root_uri Themes root URI.
  206. */
  207. return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
  208. }
  209. /**
  210. * Retrieves stylesheet URI for the active theme.
  211. *
  212. * The stylesheet file name is 'style.css' which is appended to the stylesheet directory URI path.
  213. * See get_stylesheet_directory_uri().
  214. *
  215. * @since 1.5.0
  216. *
  217. * @return string URI to active theme's stylesheet.
  218. */
  219. function get_stylesheet_uri() {
  220. $stylesheet_dir_uri = get_stylesheet_directory_uri();
  221. $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
  222. /**
  223. * Filters the URI of the active theme stylesheet.
  224. *
  225. * @since 1.5.0
  226. *
  227. * @param string $stylesheet_uri Stylesheet URI for the active theme/child theme.
  228. * @param string $stylesheet_dir_uri Stylesheet directory URI for the active theme/child theme.
  229. */
  230. return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
  231. }
  232. /**
  233. * Retrieves the localized stylesheet URI.
  234. *
  235. * The stylesheet directory for the localized stylesheet files are located, by
  236. * default, in the base theme directory. The name of the locale file will be the
  237. * locale followed by '.css'. If that does not exist, then the text direction
  238. * stylesheet will be checked for existence, for example 'ltr.css'.
  239. *
  240. * The theme may change the location of the stylesheet directory by either using
  241. * the {@see 'stylesheet_directory_uri'} or {@see 'locale_stylesheet_uri'} filters.
  242. *
  243. * If you want to change the location of the stylesheet files for the entire
  244. * WordPress workflow, then change the former. If you just have the locale in a
  245. * separate folder, then change the latter.
  246. *
  247. * @since 2.1.0
  248. *
  249. * @global WP_Locale $wp_locale WordPress date and time locale object.
  250. *
  251. * @return string URI to active theme's localized stylesheet.
  252. */
  253. function get_locale_stylesheet_uri() {
  254. global $wp_locale;
  255. $stylesheet_dir_uri = get_stylesheet_directory_uri();
  256. $dir = get_stylesheet_directory();
  257. $locale = get_locale();
  258. if ( file_exists( "$dir/$locale.css" ) ) {
  259. $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
  260. } elseif ( ! empty( $wp_locale->text_direction ) && file_exists( "$dir/{$wp_locale->text_direction}.css" ) ) {
  261. $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
  262. } else {
  263. $stylesheet_uri = '';
  264. }
  265. /**
  266. * Filters the localized stylesheet URI.
  267. *
  268. * @since 2.1.0
  269. *
  270. * @param string $stylesheet_uri Localized stylesheet URI.
  271. * @param string $stylesheet_dir_uri Stylesheet directory URI.
  272. */
  273. return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
  274. }
  275. /**
  276. * Retrieves name of the active theme.
  277. *
  278. * @since 1.5.0
  279. *
  280. * @return string Template name.
  281. */
  282. function get_template() {
  283. /**
  284. * Filters the name of the active theme.
  285. *
  286. * @since 1.5.0
  287. *
  288. * @param string $template active theme's directory name.
  289. */
  290. return apply_filters( 'template', get_option( 'template' ) );
  291. }
  292. /**
  293. * Retrieves template directory path for the active theme.
  294. *
  295. * @since 1.5.0
  296. *
  297. * @return string Path to active theme's template directory.
  298. */
  299. function get_template_directory() {
  300. $template = get_template();
  301. $theme_root = get_theme_root( $template );
  302. $template_dir = "$theme_root/$template";
  303. /**
  304. * Filters the active theme directory path.
  305. *
  306. * @since 1.5.0
  307. *
  308. * @param string $template_dir The path of the active theme directory.
  309. * @param string $template Directory name of the active theme.
  310. * @param string $theme_root Absolute path to the themes directory.
  311. */
  312. return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
  313. }
  314. /**
  315. * Retrieves template directory URI for the active theme.
  316. *
  317. * @since 1.5.0
  318. *
  319. * @return string URI to active theme's template directory.
  320. */
  321. function get_template_directory_uri() {
  322. $template = str_replace( '%2F', '/', rawurlencode( get_template() ) );
  323. $theme_root_uri = get_theme_root_uri( $template );
  324. $template_dir_uri = "$theme_root_uri/$template";
  325. /**
  326. * Filters the active theme directory URI.
  327. *
  328. * @since 1.5.0
  329. *
  330. * @param string $template_dir_uri The URI of the active theme directory.
  331. * @param string $template Directory name of the active theme.
  332. * @param string $theme_root_uri The themes root URI.
  333. */
  334. return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
  335. }
  336. /**
  337. * Retrieves theme roots.
  338. *
  339. * @since 2.9.0
  340. *
  341. * @global array $wp_theme_directories
  342. *
  343. * @return array|string An array of theme roots keyed by template/stylesheet
  344. * or a single theme root if all themes have the same root.
  345. */
  346. function get_theme_roots() {
  347. global $wp_theme_directories;
  348. if ( ! is_array( $wp_theme_directories ) || count( $wp_theme_directories ) <= 1 ) {
  349. return '/themes';
  350. }
  351. $theme_roots = get_site_transient( 'theme_roots' );
  352. if ( false === $theme_roots ) {
  353. search_theme_directories( true ); // Regenerate the transient.
  354. $theme_roots = get_site_transient( 'theme_roots' );
  355. }
  356. return $theme_roots;
  357. }
  358. /**
  359. * Registers a directory that contains themes.
  360. *
  361. * @since 2.9.0
  362. *
  363. * @global array $wp_theme_directories
  364. *
  365. * @param string $directory Either the full filesystem path to a theme folder
  366. * or a folder within WP_CONTENT_DIR.
  367. * @return bool True if successfully registered a directory that contains themes,
  368. * false if the directory does not exist.
  369. */
  370. function register_theme_directory( $directory ) {
  371. global $wp_theme_directories;
  372. if ( ! file_exists( $directory ) ) {
  373. // Try prepending as the theme directory could be relative to the content directory.
  374. $directory = WP_CONTENT_DIR . '/' . $directory;
  375. // If this directory does not exist, return and do not register.
  376. if ( ! file_exists( $directory ) ) {
  377. return false;
  378. }
  379. }
  380. if ( ! is_array( $wp_theme_directories ) ) {
  381. $wp_theme_directories = array();
  382. }
  383. $untrailed = untrailingslashit( $directory );
  384. if ( ! empty( $untrailed ) && ! in_array( $untrailed, $wp_theme_directories, true ) ) {
  385. $wp_theme_directories[] = $untrailed;
  386. }
  387. return true;
  388. }
  389. /**
  390. * Searches all registered theme directories for complete and valid themes.
  391. *
  392. * @since 2.9.0
  393. *
  394. * @global array $wp_theme_directories
  395. *
  396. * @param bool $force Optional. Whether to force a new directory scan. Default false.
  397. * @return array|false Valid themes found on success, false on failure.
  398. */
  399. function search_theme_directories( $force = false ) {
  400. global $wp_theme_directories;
  401. static $found_themes = null;
  402. if ( empty( $wp_theme_directories ) ) {
  403. return false;
  404. }
  405. if ( ! $force && isset( $found_themes ) ) {
  406. return $found_themes;
  407. }
  408. $found_themes = array();
  409. $wp_theme_directories = (array) $wp_theme_directories;
  410. $relative_theme_roots = array();
  411. /*
  412. * Set up maybe-relative, maybe-absolute array of theme directories.
  413. * We always want to return absolute, but we need to cache relative
  414. * to use in get_theme_root().
  415. */
  416. foreach ( $wp_theme_directories as $theme_root ) {
  417. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) {
  418. $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
  419. } else {
  420. $relative_theme_roots[ $theme_root ] = $theme_root;
  421. }
  422. }
  423. /**
  424. * Filters whether to get the cache of the registered theme directories.
  425. *
  426. * @since 3.4.0
  427. *
  428. * @param bool $cache_expiration Whether to get the cache of the theme directories. Default false.
  429. * @param string $context The class or function name calling the filter.
  430. */
  431. $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' );
  432. if ( $cache_expiration ) {
  433. $cached_roots = get_site_transient( 'theme_roots' );
  434. if ( is_array( $cached_roots ) ) {
  435. foreach ( $cached_roots as $theme_dir => $theme_root ) {
  436. // A cached theme root is no longer around, so skip it.
  437. if ( ! isset( $relative_theme_roots[ $theme_root ] ) ) {
  438. continue;
  439. }
  440. $found_themes[ $theme_dir ] = array(
  441. 'theme_file' => $theme_dir . '/style.css',
  442. 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
  443. );
  444. }
  445. return $found_themes;
  446. }
  447. if ( ! is_int( $cache_expiration ) ) {
  448. $cache_expiration = 30 * MINUTE_IN_SECONDS;
  449. }
  450. } else {
  451. $cache_expiration = 30 * MINUTE_IN_SECONDS;
  452. }
  453. /* Loop the registered theme directories and extract all themes */
  454. foreach ( $wp_theme_directories as $theme_root ) {
  455. // Start with directories in the root of the active theme directory.
  456. $dirs = @ scandir( $theme_root );
  457. if ( ! $dirs ) {
  458. trigger_error( "$theme_root is not readable", E_USER_NOTICE );
  459. continue;
  460. }
  461. foreach ( $dirs as $dir ) {
  462. if ( ! is_dir( $theme_root . '/' . $dir ) || '.' === $dir[0] || 'CVS' === $dir ) {
  463. continue;
  464. }
  465. if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
  466. // wp-content/themes/a-single-theme
  467. // wp-content/themes is $theme_root, a-single-theme is $dir.
  468. $found_themes[ $dir ] = array(
  469. 'theme_file' => $dir . '/style.css',
  470. 'theme_root' => $theme_root,
  471. );
  472. } else {
  473. $found_theme = false;
  474. // wp-content/themes/a-folder-of-themes/*
  475. // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs.
  476. $sub_dirs = @ scandir( $theme_root . '/' . $dir );
  477. if ( ! $sub_dirs ) {
  478. trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE );
  479. continue;
  480. }
  481. foreach ( $sub_dirs as $sub_dir ) {
  482. if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || '.' === $dir[0] || 'CVS' === $dir ) {
  483. continue;
  484. }
  485. if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) ) {
  486. continue;
  487. }
  488. $found_themes[ $dir . '/' . $sub_dir ] = array(
  489. 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
  490. 'theme_root' => $theme_root,
  491. );
  492. $found_theme = true;
  493. }
  494. // Never mind the above, it's just a theme missing a style.css.
  495. // Return it; WP_Theme will catch the error.
  496. if ( ! $found_theme ) {
  497. $found_themes[ $dir ] = array(
  498. 'theme_file' => $dir . '/style.css',
  499. 'theme_root' => $theme_root,
  500. );
  501. }
  502. }
  503. }
  504. }
  505. asort( $found_themes );
  506. $theme_roots = array();
  507. $relative_theme_roots = array_flip( $relative_theme_roots );
  508. foreach ( $found_themes as $theme_dir => $theme_data ) {
  509. $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
  510. }
  511. if ( get_site_transient( 'theme_roots' ) != $theme_roots ) {
  512. set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
  513. }
  514. return $found_themes;
  515. }
  516. /**
  517. * Retrieves path to themes directory.
  518. *
  519. * Does not have trailing slash.
  520. *
  521. * @since 1.5.0
  522. *
  523. * @global array $wp_theme_directories
  524. *
  525. * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
  526. * Default is to leverage the main theme root.
  527. * @return string Themes directory path.
  528. */
  529. function get_theme_root( $stylesheet_or_template = '' ) {
  530. global $wp_theme_directories;
  531. $theme_root = '';
  532. if ( $stylesheet_or_template ) {
  533. $theme_root = get_raw_theme_root( $stylesheet_or_template );
  534. if ( $theme_root ) {
  535. // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
  536. // This gives relative theme roots the benefit of the doubt when things go haywire.
  537. if ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
  538. $theme_root = WP_CONTENT_DIR . $theme_root;
  539. }
  540. }
  541. }
  542. if ( ! $theme_root ) {
  543. $theme_root = WP_CONTENT_DIR . '/themes';
  544. }
  545. /**
  546. * Filters the absolute path to the themes directory.
  547. *
  548. * @since 1.5.0
  549. *
  550. * @param string $theme_root Absolute path to themes directory.
  551. */
  552. return apply_filters( 'theme_root', $theme_root );
  553. }
  554. /**
  555. * Retrieves URI for themes directory.
  556. *
  557. * Does not have trailing slash.
  558. *
  559. * @since 1.5.0
  560. *
  561. * @global array $wp_theme_directories
  562. *
  563. * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
  564. * Default is to leverage the main theme root.
  565. * @param string $theme_root Optional. The theme root for which calculations will be based,
  566. * preventing the need for a get_raw_theme_root() call. Default empty.
  567. * @return string Themes directory URI.
  568. */
  569. function get_theme_root_uri( $stylesheet_or_template = '', $theme_root = '' ) {
  570. global $wp_theme_directories;
  571. if ( $stylesheet_or_template && ! $theme_root ) {
  572. $theme_root = get_raw_theme_root( $stylesheet_or_template );
  573. }
  574. if ( $stylesheet_or_template && $theme_root ) {
  575. if ( in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
  576. // Absolute path. Make an educated guess. YMMV -- but note the filter below.
  577. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) {
  578. $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
  579. } elseif ( 0 === strpos( $theme_root, ABSPATH ) ) {
  580. $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
  581. } elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) ) {
  582. $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
  583. } else {
  584. $theme_root_uri = $theme_root;
  585. }
  586. } else {
  587. $theme_root_uri = content_url( $theme_root );
  588. }
  589. } else {
  590. $theme_root_uri = content_url( 'themes' );
  591. }
  592. /**
  593. * Filters the URI for themes directory.
  594. *
  595. * @since 1.5.0
  596. *
  597. * @param string $theme_root_uri The URI for themes directory.
  598. * @param string $siteurl WordPress web address which is set in General Options.
  599. * @param string $stylesheet_or_template The stylesheet or template name of the theme.
  600. */
  601. return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
  602. }
  603. /**
  604. * Gets the raw theme root relative to the content directory with no filters applied.
  605. *
  606. * @since 3.1.0
  607. *
  608. * @global array $wp_theme_directories
  609. *
  610. * @param string $stylesheet_or_template The stylesheet or template name of the theme.
  611. * @param bool $skip_cache Optional. Whether to skip the cache.
  612. * Defaults to false, meaning the cache is used.
  613. * @return string Theme root.
  614. */
  615. function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
  616. global $wp_theme_directories;
  617. if ( ! is_array( $wp_theme_directories ) || count( $wp_theme_directories ) <= 1 ) {
  618. return '/themes';
  619. }
  620. $theme_root = false;
  621. // If requesting the root for the active theme, consult options to avoid calling get_theme_roots().
  622. if ( ! $skip_cache ) {
  623. if ( get_option( 'stylesheet' ) == $stylesheet_or_template ) {
  624. $theme_root = get_option( 'stylesheet_root' );
  625. } elseif ( get_option( 'template' ) == $stylesheet_or_template ) {
  626. $theme_root = get_option( 'template_root' );
  627. }
  628. }
  629. if ( empty( $theme_root ) ) {
  630. $theme_roots = get_theme_roots();
  631. if ( ! empty( $theme_roots[ $stylesheet_or_template ] ) ) {
  632. $theme_root = $theme_roots[ $stylesheet_or_template ];
  633. }
  634. }
  635. return $theme_root;
  636. }
  637. /**
  638. * Displays localized stylesheet link element.
  639. *
  640. * @since 2.1.0
  641. */
  642. function locale_stylesheet() {
  643. $stylesheet = get_locale_stylesheet_uri();
  644. if ( empty( $stylesheet ) ) {
  645. return;
  646. }
  647. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  648. printf(
  649. '<link rel="stylesheet" href="%s"%s media="screen" />',
  650. $stylesheet,
  651. $type_attr
  652. );
  653. }
  654. /**
  655. * Switches the theme.
  656. *
  657. * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
  658. * of two arguments: $template then $stylesheet. This is for backward compatibility.
  659. *
  660. * @since 2.5.0
  661. *
  662. * @global array $wp_theme_directories
  663. * @global WP_Customize_Manager $wp_customize
  664. * @global array $sidebars_widgets
  665. *
  666. * @param string $stylesheet Stylesheet name.
  667. */
  668. function switch_theme( $stylesheet ) {
  669. global $wp_theme_directories, $wp_customize, $sidebars_widgets;
  670. $requirements = validate_theme_requirements( $stylesheet );
  671. if ( is_wp_error( $requirements ) ) {
  672. wp_die( $requirements );
  673. }
  674. $_sidebars_widgets = null;
  675. if ( 'wp_ajax_customize_save' === current_action() ) {
  676. $old_sidebars_widgets_data_setting = $wp_customize->get_setting( 'old_sidebars_widgets_data' );
  677. if ( $old_sidebars_widgets_data_setting ) {
  678. $_sidebars_widgets = $wp_customize->post_value( $old_sidebars_widgets_data_setting );
  679. }
  680. } elseif ( is_array( $sidebars_widgets ) ) {
  681. $_sidebars_widgets = $sidebars_widgets;
  682. }
  683. if ( is_array( $_sidebars_widgets ) ) {
  684. set_theme_mod(
  685. 'sidebars_widgets',
  686. array(
  687. 'time' => time(),
  688. 'data' => $_sidebars_widgets,
  689. )
  690. );
  691. }
  692. $nav_menu_locations = get_theme_mod( 'nav_menu_locations' );
  693. update_option( 'theme_switch_menu_locations', $nav_menu_locations );
  694. if ( func_num_args() > 1 ) {
  695. $stylesheet = func_get_arg( 1 );
  696. }
  697. $old_theme = wp_get_theme();
  698. $new_theme = wp_get_theme( $stylesheet );
  699. $template = $new_theme->get_template();
  700. if ( wp_is_recovery_mode() ) {
  701. $paused_themes = wp_paused_themes();
  702. $paused_themes->delete( $old_theme->get_stylesheet() );
  703. $paused_themes->delete( $old_theme->get_template() );
  704. }
  705. update_option( 'template', $template );
  706. update_option( 'stylesheet', $stylesheet );
  707. if ( count( $wp_theme_directories ) > 1 ) {
  708. update_option( 'template_root', get_raw_theme_root( $template, true ) );
  709. update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
  710. } else {
  711. delete_option( 'template_root' );
  712. delete_option( 'stylesheet_root' );
  713. }
  714. $new_name = $new_theme->get( 'Name' );
  715. update_option( 'current_theme', $new_name );
  716. // Migrate from the old mods_{name} option to theme_mods_{slug}.
  717. if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
  718. $default_theme_mods = (array) get_option( 'mods_' . $new_name );
  719. if ( ! empty( $nav_menu_locations ) && empty( $default_theme_mods['nav_menu_locations'] ) ) {
  720. $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
  721. }
  722. add_option( "theme_mods_$stylesheet", $default_theme_mods );
  723. } else {
  724. /*
  725. * Since retrieve_widgets() is called when initializing a theme in the Customizer,
  726. * we need to remove the theme mods to avoid overwriting changes made via
  727. * the Customizer when accessing wp-admin/widgets.php.
  728. */
  729. if ( 'wp_ajax_customize_save' === current_action() ) {
  730. remove_theme_mod( 'sidebars_widgets' );
  731. }
  732. }
  733. update_option( 'theme_switched', $old_theme->get_stylesheet() );
  734. /**
  735. * Fires after the theme is switched.
  736. *
  737. * @since 1.5.0
  738. * @since 4.5.0 Introduced the `$old_theme` parameter.
  739. *
  740. * @param string $new_name Name of the new theme.
  741. * @param WP_Theme $new_theme WP_Theme instance of the new theme.
  742. * @param WP_Theme $old_theme WP_Theme instance of the old theme.
  743. */
  744. do_action( 'switch_theme', $new_name, $new_theme, $old_theme );
  745. }
  746. /**
  747. * Checks that the active theme has the required files.
  748. *
  749. * Standalone themes need to have a `templates/index.html` or `index.php` template file.
  750. * Child themes need to have a `Template` header in the `style.css` stylesheet.
  751. *
  752. * Does not initially check the default theme, which is the fallback and should always exist.
  753. * But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
  754. * Will switch theme to the fallback theme if active theme does not validate.
  755. *
  756. * You can use the {@see 'validate_current_theme'} filter to return false to disable
  757. * this functionality.
  758. *
  759. * @since 1.5.0
  760. * @since 6.0.0 Removed the requirement for block themes to have an `index.php` template.
  761. *
  762. * @see WP_DEFAULT_THEME
  763. *
  764. * @return bool
  765. */
  766. function validate_current_theme() {
  767. /**
  768. * Filters whether to validate the active theme.
  769. *
  770. * @since 2.7.0
  771. *
  772. * @param bool $validate Whether to validate the active theme. Default true.
  773. */
  774. if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) ) {
  775. return true;
  776. }
  777. if (
  778. ! file_exists( get_template_directory() . '/templates/index.html' )
  779. && ! file_exists( get_template_directory() . '/block-templates/index.html' ) // Deprecated path support since 5.9.0.
  780. && ! file_exists( get_template_directory() . '/index.php' )
  781. ) {
  782. // Invalid.
  783. } elseif ( ! file_exists( get_template_directory() . '/style.css' ) ) {
  784. // Invalid.
  785. } elseif ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
  786. // Invalid.
  787. } else {
  788. // Valid.
  789. return true;
  790. }
  791. $default = wp_get_theme( WP_DEFAULT_THEME );
  792. if ( $default->exists() ) {
  793. switch_theme( WP_DEFAULT_THEME );
  794. return false;
  795. }
  796. /**
  797. * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
  798. * switch to the latest core default theme that's installed.
  799. *
  800. * If it turns out that this latest core default theme is our current
  801. * theme, then there's nothing we can do about that, so we have to bail,
  802. * rather than going into an infinite loop. (This is why there are
  803. * checks against WP_DEFAULT_THEME above, also.) We also can't do anything
  804. * if it turns out there is no default theme installed. (That's `false`.)
  805. */
  806. $default = WP_Theme::get_core_default_theme();
  807. if ( false === $default || get_stylesheet() == $default->get_stylesheet() ) {
  808. return true;
  809. }
  810. switch_theme( $default->get_stylesheet() );
  811. return false;
  812. }
  813. /**
  814. * Validates the theme requirements for WordPress version and PHP version.
  815. *
  816. * Uses the information from `Requires at least` and `Requires PHP` headers
  817. * defined in the theme's `style.css` file.
  818. *
  819. * @since 5.5.0
  820. * @since 5.8.0 Removed support for using `readme.txt` as a fallback.
  821. *
  822. * @param string $stylesheet Directory name for the theme.
  823. * @return true|WP_Error True if requirements are met, WP_Error on failure.
  824. */
  825. function validate_theme_requirements( $stylesheet ) {
  826. $theme = wp_get_theme( $stylesheet );
  827. $requirements = array(
  828. 'requires' => ! empty( $theme->get( 'RequiresWP' ) ) ? $theme->get( 'RequiresWP' ) : '',
  829. 'requires_php' => ! empty( $theme->get( 'RequiresPHP' ) ) ? $theme->get( 'RequiresPHP' ) : '',
  830. );
  831. $compatible_wp = is_wp_version_compatible( $requirements['requires'] );
  832. $compatible_php = is_php_version_compatible( $requirements['requires_php'] );
  833. if ( ! $compatible_wp && ! $compatible_php ) {
  834. return new WP_Error(
  835. 'theme_wp_php_incompatible',
  836. sprintf(
  837. /* translators: %s: Theme name. */
  838. _x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'theme' ),
  839. $theme->display( 'Name' )
  840. )
  841. );
  842. } elseif ( ! $compatible_php ) {
  843. return new WP_Error(
  844. 'theme_php_incompatible',
  845. sprintf(
  846. /* translators: %s: Theme name. */
  847. _x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'theme' ),
  848. $theme->display( 'Name' )
  849. )
  850. );
  851. } elseif ( ! $compatible_wp ) {
  852. return new WP_Error(
  853. 'theme_wp_incompatible',
  854. sprintf(
  855. /* translators: %s: Theme name. */
  856. _x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'theme' ),
  857. $theme->display( 'Name' )
  858. )
  859. );
  860. }
  861. return true;
  862. }
  863. /**
  864. * Retrieves all theme modifications.
  865. *
  866. * @since 3.1.0
  867. * @since 5.9.0 The return value is always an array.
  868. *
  869. * @return array Theme modifications.
  870. */
  871. function get_theme_mods() {
  872. $theme_slug = get_option( 'stylesheet' );
  873. $mods = get_option( "theme_mods_$theme_slug" );
  874. if ( false === $mods ) {
  875. $theme_name = get_option( 'current_theme' );
  876. if ( false === $theme_name ) {
  877. $theme_name = wp_get_theme()->get( 'Name' );
  878. }
  879. $mods = get_option( "mods_$theme_name" ); // Deprecated location.
  880. if ( is_admin() && false !== $mods ) {
  881. update_option( "theme_mods_$theme_slug", $mods );
  882. delete_option( "mods_$theme_name" );
  883. }
  884. }
  885. if ( ! is_array( $mods ) ) {
  886. $mods = array();
  887. }
  888. return $mods;
  889. }
  890. /**
  891. * Retrieves theme modification value for the active theme.
  892. *
  893. * If the modification name does not exist and `$default` is a string, then the
  894. * default will be passed through the {@link https://www.php.net/sprintf sprintf()}
  895. * PHP function with the template directory URI as the first value and the
  896. * stylesheet directory URI as the second value.
  897. *
  898. * @since 2.1.0
  899. *
  900. * @param string $name Theme modification name.
  901. * @param mixed $default Optional. Theme modification default value. Default false.
  902. * @return mixed Theme modification value.
  903. */
  904. function get_theme_mod( $name, $default = false ) {
  905. $mods = get_theme_mods();
  906. if ( isset( $mods[ $name ] ) ) {
  907. /**
  908. * Filters the theme modification, or 'theme_mod', value.
  909. *
  910. * The dynamic portion of the hook name, `$name`, refers to the key name
  911. * of the modification array. For example, 'header_textcolor', 'header_image',
  912. * and so on depending on the theme options.
  913. *
  914. * @since 2.2.0
  915. *
  916. * @param mixed $current_mod The value of the active theme modification.
  917. */
  918. return apply_filters( "theme_mod_{$name}", $mods[ $name ] );
  919. }
  920. if ( is_string( $default ) ) {
  921. // Only run the replacement if an sprintf() string format pattern was found.
  922. if ( preg_match( '#(?<!%)%(?:\d+\$?)?s#', $default ) ) {
  923. // Remove a single trailing percent sign.
  924. $default = preg_replace( '#(?<!%)%$#', '', $default );
  925. $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  926. }
  927. }
  928. /** This filter is documented in wp-includes/theme.php */
  929. return apply_filters( "theme_mod_{$name}", $default );
  930. }
  931. /**
  932. * Updates theme modification value for the active theme.
  933. *
  934. * @since 2.1.0
  935. * @since 5.6.0 A return value was added.
  936. *
  937. * @param string $name Theme modification name.
  938. * @param mixed $value Theme modification value.
  939. * @return bool True if the value was updated, false otherwise.
  940. */
  941. function set_theme_mod( $name, $value ) {
  942. $mods = get_theme_mods();
  943. $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
  944. /**
  945. * Filters the theme modification, or 'theme_mod', value on save.
  946. *
  947. * The dynamic portion of the hook name, `$name`, refers to the key name
  948. * of the modification array. For example, 'header_textcolor', 'header_image',
  949. * and so on depending on the theme options.
  950. *
  951. * @since 3.9.0
  952. *
  953. * @param mixed $value The new value of the theme modification.
  954. * @param mixed $old_value The current value of the theme modification.
  955. */
  956. $mods[ $name ] = apply_filters( "pre_set_theme_mod_{$name}", $value, $old_value );
  957. $theme = get_option( 'stylesheet' );
  958. return update_option( "theme_mods_$theme", $mods );
  959. }
  960. /**
  961. * Removes theme modification name from active theme list.
  962. *
  963. * If removing the name also removes all elements, then the entire option
  964. * will be removed.
  965. *
  966. * @since 2.1.0
  967. *
  968. * @param string $name Theme modification name.
  969. */
  970. function remove_theme_mod( $name ) {
  971. $mods = get_theme_mods();
  972. if ( ! isset( $mods[ $name ] ) ) {
  973. return;
  974. }
  975. unset( $mods[ $name ] );
  976. if ( empty( $mods ) ) {
  977. remove_theme_mods();
  978. return;
  979. }
  980. $theme = get_option( 'stylesheet' );
  981. update_option( "theme_mods_$theme", $mods );
  982. }
  983. /**
  984. * Removes theme modifications option for the active theme.
  985. *
  986. * @since 2.1.0
  987. */
  988. function remove_theme_mods() {
  989. delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
  990. // Old style.
  991. $theme_name = get_option( 'current_theme' );
  992. if ( false === $theme_name ) {
  993. $theme_name = wp_get_theme()->get( 'Name' );
  994. }
  995. delete_option( 'mods_' . $theme_name );
  996. }
  997. /**
  998. * Retrieves the custom header text color in 3- or 6-digit hexadecimal form.
  999. *
  1000. * @since 2.1.0
  1001. *
  1002. * @return string Header text color in 3- or 6-digit hexadecimal form (minus the hash symbol).
  1003. */
  1004. function get_header_textcolor() {
  1005. return get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
  1006. }
  1007. /**
  1008. * Displays the custom header text color in 3- or 6-digit hexadecimal form (minus the hash symbol).
  1009. *
  1010. * @since 2.1.0
  1011. */
  1012. function header_textcolor() {
  1013. echo get_header_textcolor();
  1014. }
  1015. /**
  1016. * Whether to display the header text.
  1017. *
  1018. * @since 3.4.0
  1019. *
  1020. * @return bool
  1021. */
  1022. function display_header_text() {
  1023. if ( ! current_theme_supports( 'custom-header', 'header-text' ) ) {
  1024. return false;
  1025. }
  1026. $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
  1027. return 'blank' !== $text_color;
  1028. }
  1029. /**
  1030. * Checks whether a header image is set or not.
  1031. *
  1032. * @since 4.2.0
  1033. *
  1034. * @see get_header_image()
  1035. *
  1036. * @return bool Whether a header image is set or not.
  1037. */
  1038. function has_header_image() {
  1039. return (bool) get_header_image();
  1040. }
  1041. /**
  1042. * Retrieves header image for custom header.
  1043. *
  1044. * @since 2.1.0
  1045. *
  1046. * @return string|false
  1047. */
  1048. function get_header_image() {
  1049. $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
  1050. if ( 'remove-header' === $url ) {
  1051. return false;
  1052. }
  1053. if ( is_random_header_image() ) {
  1054. $url = get_random_header_image();
  1055. }
  1056. /**
  1057. * Filters the header image URL.
  1058. *
  1059. * @since 6.1.0
  1060. *
  1061. * @param string $url Header image URL.
  1062. */
  1063. $url = apply_filters( 'get_header_image', $url );
  1064. if ( ! is_string( $url ) ) {
  1065. return false;
  1066. }
  1067. $url = trim( $url );
  1068. return sanitize_url( set_url_scheme( $url ) );
  1069. }
  1070. /**
  1071. * Creates image tag markup for a custom header image.
  1072. *
  1073. * @since 4.4.0
  1074. *
  1075. * @param array $attr Optional. Additional attributes for the image tag. Can be used
  1076. * to override the default attributes. Default empty.
  1077. * @return string HTML image element markup or empty string on failure.
  1078. */
  1079. function get_header_image_tag( $attr = array() ) {
  1080. $header = get_custom_header();
  1081. $header->url = get_header_image();
  1082. if ( ! $header->url ) {
  1083. return '';
  1084. }
  1085. $width = absint( $header->width );
  1086. $height = absint( $header->height );
  1087. $alt = '';
  1088. // Use alternative text assigned to the image, if available. Otherwise, leave it empty.
  1089. if ( ! empty( $header->attachment_id ) ) {
  1090. $image_alt = get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true );
  1091. if ( is_string( $image_alt ) ) {
  1092. $alt = $image_alt;
  1093. }
  1094. }
  1095. $attr = wp_parse_args(
  1096. $attr,
  1097. array(
  1098. 'src' => $header->url,
  1099. 'width' => $width,
  1100. 'height' => $height,
  1101. 'alt' => $alt,
  1102. )
  1103. );
  1104. // Generate 'srcset' and 'sizes' if not already present.
  1105. if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) {
  1106. $image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true );
  1107. $size_array = array( $width, $height );
  1108. if ( is_array( $image_meta ) ) {
  1109. $srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id );
  1110. if ( ! empty( $attr['sizes'] ) ) {
  1111. $sizes = $attr['sizes'];
  1112. } else {
  1113. $sizes = wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );
  1114. }
  1115. if ( $srcset && $sizes ) {
  1116. $attr['srcset'] = $srcset;
  1117. $attr['sizes'] = $sizes;
  1118. }
  1119. }
  1120. }
  1121. /**
  1122. * Filters the list of header image attributes.
  1123. *
  1124. * @since 5.9.0
  1125. *
  1126. * @param array $attr Array of the attributes for the image tag.
  1127. * @param object $header The custom header object returned by 'get_custom_header()'.
  1128. */
  1129. $attr = apply_filters( 'get_header_image_tag_attributes', $attr, $header );
  1130. $attr = array_map( 'esc_attr', $attr );
  1131. $html = '<img';
  1132. foreach ( $attr as $name => $value ) {
  1133. $html .= ' ' . $name . '="' . $value . '"';
  1134. }
  1135. $html .= ' />';
  1136. /**
  1137. * Filters the markup of header images.
  1138. *
  1139. * @since 4.4.0
  1140. *
  1141. * @param string $html The HTML image tag markup being filtered.
  1142. * @param object $header The custom header object returned by 'get_custom_header()'.
  1143. * @param array $attr Array of the attributes for the image tag.
  1144. */
  1145. return apply_filters( 'get_header_image_tag', $html, $header, $attr );
  1146. }
  1147. /**
  1148. * Displays the image markup for a custom header image.
  1149. *
  1150. * @since 4.4.0
  1151. *
  1152. * @param array $attr Optional. Attributes for the image markup. Default empty.
  1153. */
  1154. function the_header_image_tag( $attr = array() ) {
  1155. echo get_header_image_tag( $attr );
  1156. }
  1157. /**
  1158. * Gets random header image data from registered images in theme.
  1159. *
  1160. * @since 3.4.0
  1161. *
  1162. * @access private
  1163. *
  1164. * @global array $_wp_default_headers
  1165. *
  1166. * @return object
  1167. */
  1168. function _get_random_header_data() {
  1169. global $_wp_default_headers;
  1170. static $_wp_random_header = null;
  1171. if ( empty( $_wp_random_header ) ) {
  1172. $header_image_mod = get_theme_mod( 'header_image', '' );
  1173. $headers = array();
  1174. if ( 'random-uploaded-image' === $header_image_mod ) {
  1175. $headers = get_uploaded_header_images();
  1176. } elseif ( ! empty( $_wp_default_headers ) ) {
  1177. if ( 'random-default-image' === $header_image_mod ) {
  1178. $headers = $_wp_default_headers;
  1179. } else {
  1180. if ( current_theme_supports( 'custom-header', 'random-default' ) ) {
  1181. $headers = $_wp_default_headers;
  1182. }
  1183. }
  1184. }
  1185. if ( empty( $headers ) ) {
  1186. return new stdClass;
  1187. }
  1188. $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
  1189. $_wp_random_header->url = sprintf(
  1190. $_wp_random_header->url,
  1191. get_template_directory_uri(),
  1192. get_stylesheet_directory_uri()
  1193. );
  1194. $_wp_random_header->thumbnail_url = sprintf(
  1195. $_wp_random_header->thumbnail_url,
  1196. get_template_directory_uri(),
  1197. get_stylesheet_directory_uri()
  1198. );
  1199. }
  1200. return $_wp_random_header;
  1201. }
  1202. /**
  1203. * Gets random header image URL from registered images in theme.
  1204. *
  1205. * @since 3.2.0
  1206. *
  1207. * @return string Path to header image.
  1208. */
  1209. function get_random_header_image() {
  1210. $random_image = _get_random_header_data();
  1211. if ( empty( $random_image->url ) ) {
  1212. return '';
  1213. }
  1214. return $random_image->url;
  1215. }
  1216. /**
  1217. * Checks if random header image is in use.
  1218. *
  1219. * Always true if user expressly chooses the option in Appearance > Header.
  1220. * Also true if theme has multiple header images registered, no specific header image
  1221. * is chosen, and theme turns on random headers with add_theme_support().
  1222. *
  1223. * @since 3.2.0
  1224. *
  1225. * @param string $type The random pool to use. Possible values include 'any',
  1226. * 'default', 'uploaded'. Default 'any'.
  1227. * @return bool
  1228. */
  1229. function is_random_header_image( $type = 'any' ) {
  1230. $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
  1231. if ( 'any' === $type ) {
  1232. if ( 'random-default-image' === $header_image_mod
  1233. || 'random-uploaded-image' === $header_image_mod
  1234. || ( '' !== get_random_header_image() && empty( $header_image_mod ) )
  1235. ) {
  1236. return true;
  1237. }
  1238. } else {
  1239. if ( "random-$type-image" === $header_image_mod ) {
  1240. return true;
  1241. } elseif ( 'default' === $type && empty( $header_image_mod ) && '' !== get_random_header_image() ) {
  1242. return true;
  1243. }
  1244. }
  1245. return false;
  1246. }
  1247. /**
  1248. * Displays header image URL.
  1249. *
  1250. * @since 2.1.0
  1251. */
  1252. function header_image() {
  1253. $image = get_header_image();
  1254. if ( $image ) {
  1255. echo esc_url( $image );
  1256. }
  1257. }
  1258. /**
  1259. * Gets the header images uploaded for the active theme.
  1260. *
  1261. * @since 3.2.0
  1262. *
  1263. * @return array
  1264. */
  1265. function get_uploaded_header_images() {
  1266. $header_images = array();
  1267. // @todo Caching.
  1268. $headers = get_posts(
  1269. array(
  1270. 'post_type' => 'attachment',
  1271. 'meta_key' => '_wp_attachment_is_custom_header',
  1272. 'meta_value' => get_option( 'stylesheet' ),
  1273. 'orderby' => 'none',
  1274. 'nopaging' => true,
  1275. )
  1276. );
  1277. if ( empty( $headers ) ) {
  1278. return array();
  1279. }
  1280. foreach ( (array) $headers as $header ) {
  1281. $url = sanitize_url( wp_get_attachment_url( $header->ID ) );
  1282. $header_data = wp_get_attachment_metadata( $header->ID );
  1283. $header_index = $header->ID;
  1284. $header_images[ $header_index ] = array();
  1285. $header_images[ $header_index ]['attachment_id'] = $header->ID;
  1286. $header_images[ $header_index ]['url'] = $url;
  1287. $header_images[ $header_index ]['thumbnail_url'] = $url;
  1288. $header_images[ $header_index ]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
  1289. if ( isset( $header_data['attachment_parent'] ) ) {
  1290. $header_images[ $header_index ]['attachment_parent'] = $header_data['attachment_parent'];
  1291. } else {
  1292. $header_images[ $header_index ]['attachment_parent'] = '';
  1293. }
  1294. if ( isset( $header_data['width'] ) ) {
  1295. $header_images[ $header_index ]['width'] = $header_data['width'];
  1296. }
  1297. if ( isset( $header_data['height'] ) ) {
  1298. $header_images[ $header_index ]['height'] = $header_data['height'];
  1299. }
  1300. }
  1301. return $header_images;
  1302. }
  1303. /**
  1304. * Gets the header image data.
  1305. *
  1306. * @since 3.4.0
  1307. *
  1308. * @global array $_wp_default_headers
  1309. *
  1310. * @return object
  1311. */
  1312. function get_custom_header() {
  1313. global $_wp_default_headers;
  1314. if ( is_random_header_image() ) {
  1315. $data = _get_random_header_data();
  1316. } else {
  1317. $data = get_theme_mod( 'header_image_data' );
  1318. if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
  1319. $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
  1320. $data = array();
  1321. $data['url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
  1322. $data['thumbnail_url'] = $data['url'];
  1323. if ( ! empty( $_wp_default_headers ) ) {
  1324. foreach ( (array) $_wp_default_headers as $default_header ) {
  1325. $url = vsprintf( $default_header['url'], $directory_args );
  1326. if ( $data['url'] == $url ) {
  1327. $data = $default_header;
  1328. $data['url'] = $url;
  1329. $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. }
  1336. $default = array(
  1337. 'url' => '',
  1338. 'thumbnail_url' => '',
  1339. 'width' => get_theme_support( 'custom-header', 'width' ),
  1340. 'height' => get_theme_support( 'custom-header', 'height' ),
  1341. 'video' => get_theme_support( 'custom-header', 'video' ),
  1342. );
  1343. return (object) wp_parse_args( $data, $default );
  1344. }
  1345. /**
  1346. * Registers a selection of default headers to be displayed by the custom header admin UI.
  1347. *
  1348. * @since 3.0.0
  1349. *
  1350. * @global array $_wp_default_headers
  1351. *
  1352. * @param array $headers Array of headers keyed by a string ID. The IDs point to arrays
  1353. * containing 'url', 'thumbnail_url', and 'description' keys.
  1354. */
  1355. function register_default_headers( $headers ) {
  1356. global $_wp_default_headers;
  1357. $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
  1358. }
  1359. /**
  1360. * Unregisters default headers.
  1361. *
  1362. * This function must be called after register_default_headers() has already added the
  1363. * header you want to remove.
  1364. *
  1365. * @see register_default_headers()
  1366. * @since 3.0.0
  1367. *
  1368. * @global array $_wp_default_headers
  1369. *
  1370. * @param string|array $header The header string id (key of array) to remove, or an array thereof.
  1371. * @return bool|void A single header returns true on success, false on failure.
  1372. * There is currently no return value for multiple headers.
  1373. */
  1374. function unregister_default_headers( $header ) {
  1375. global $_wp_default_headers;
  1376. if ( is_array( $header ) ) {
  1377. array_map( 'unregister_default_headers', $header );
  1378. } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
  1379. unset( $_wp_default_headers[ $header ] );
  1380. return true;
  1381. } else {
  1382. return false;
  1383. }
  1384. }
  1385. /**
  1386. * Checks whether a header video is set or not.
  1387. *
  1388. * @since 4.7.0
  1389. *
  1390. * @see get_header_video_url()
  1391. *
  1392. * @return bool Whether a header video is set or not.
  1393. */
  1394. function has_header_video() {
  1395. return (bool) get_header_video_url();
  1396. }
  1397. /**
  1398. * Retrieves header video URL for custom header.
  1399. *
  1400. * Uses a local video if present, or falls back to an external video.
  1401. *
  1402. * @since 4.7.0
  1403. *
  1404. * @return string|false Header video URL or false if there is no video.
  1405. */
  1406. function get_header_video_url() {
  1407. $id = absint( get_theme_mod( 'header_video' ) );
  1408. if ( $id ) {
  1409. // Get the file URL from the attachment ID.
  1410. $url = wp_get_attachment_url( $id );
  1411. } else {
  1412. $url = get_theme_mod( 'external_header_video' );
  1413. }
  1414. /**
  1415. * Filters the header video URL.
  1416. *
  1417. * @since 4.7.3
  1418. *
  1419. * @param string $url Header video URL, if available.
  1420. */
  1421. $url = apply_filters( 'get_header_video_url', $url );
  1422. if ( ! $id && ! $url ) {
  1423. return false;
  1424. }
  1425. return sanitize_url( set_url_scheme( $url ) );
  1426. }
  1427. /**
  1428. * Displays header video URL.
  1429. *
  1430. * @since 4.7.0
  1431. */
  1432. function the_header_video_url() {
  1433. $video = get_header_video_url();
  1434. if ( $video ) {
  1435. echo esc_url( $video );
  1436. }
  1437. }
  1438. /**
  1439. * Retrieves header video settings.
  1440. *
  1441. * @since 4.7.0
  1442. *
  1443. * @return array
  1444. */
  1445. function get_header_video_settings() {
  1446. $header = get_custom_header();
  1447. $video_url = get_header_video_url();
  1448. $video_type = wp_check_filetype( $video_url, wp_get_mime_types() );
  1449. $settings = array(
  1450. 'mimeType' => '',
  1451. 'posterUrl' => get_header_image(),
  1452. 'videoUrl' => $video_url,
  1453. 'width' => absint( $header->width ),
  1454. 'height' => absint( $header->height ),
  1455. 'minWidth' => 900,
  1456. 'minHeight' => 500,
  1457. 'l10n' => array(
  1458. 'pause' => __( 'Pause' ),
  1459. 'play' => __( 'Play' ),
  1460. 'pauseSpeak' => __( 'Video is paused.' ),
  1461. 'playSpeak' => __( 'Video is playing.' ),
  1462. ),
  1463. );
  1464. if ( preg_match( '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#', $video_url ) ) {
  1465. $settings['mimeType'] = 'video/x-youtube';
  1466. } elseif ( ! empty( $video_type['type'] ) ) {
  1467. $settings['mimeType'] = $video_type['type'];
  1468. }
  1469. /**
  1470. * Filters header video settings.
  1471. *
  1472. * @since 4.7.0
  1473. *
  1474. * @param array $settings An array of header video settings.
  1475. */
  1476. return apply_filters( 'header_video_settings', $settings );
  1477. }
  1478. /**
  1479. * Checks whether a custom header is set or not.
  1480. *
  1481. * @since 4.7.0
  1482. *
  1483. * @return bool True if a custom header is set. False if not.
  1484. */
  1485. function has_custom_header() {
  1486. if ( has_header_image() || ( has_header_video() && is_header_video_active() ) ) {
  1487. return true;
  1488. }
  1489. return false;
  1490. }
  1491. /**
  1492. * Checks whether the custom header video is eligible to show on the current page.
  1493. *
  1494. * @since 4.7.0
  1495. *
  1496. * @return bool True if the custom header video should be shown. False if not.
  1497. */
  1498. function is_header_video_active() {
  1499. if ( ! get_theme_support( 'custom-header', 'video' ) ) {
  1500. return false;
  1501. }
  1502. $video_active_cb = get_theme_support( 'custom-header', 'video-active-callback' );
  1503. if ( empty( $video_active_cb ) || ! is_callable( $video_active_cb ) ) {
  1504. $show_video = true;
  1505. } else {
  1506. $show_video = call_user_func( $video_active_cb );
  1507. }
  1508. /**
  1509. * Filters whether the custom header video is eligible to show on the current page.
  1510. *
  1511. * @since 4.7.0
  1512. *
  1513. * @param bool $show_video Whether the custom header video should be shown. Returns the value
  1514. * of the theme setting for the `custom-header`'s `video-active-callback`.
  1515. * If no callback is set, the default value is that of `is_front_page()`.
  1516. */
  1517. return apply_filters( 'is_header_video_active', $show_video );
  1518. }
  1519. /**
  1520. * Retrieves the markup for a custom header.
  1521. *
  1522. * The container div will always be returned in the Customizer preview.
  1523. *
  1524. * @since 4.7.0
  1525. *
  1526. * @return string The markup for a custom header on success.
  1527. */
  1528. function get_custom_header_markup() {
  1529. if ( ! has_custom_header() && ! is_customize_preview() ) {
  1530. return '';
  1531. }
  1532. return sprintf(
  1533. '<div id="wp-custom-header" class="wp-custom-header">%s</div>',
  1534. get_header_image_tag()
  1535. );
  1536. }
  1537. /**
  1538. * Prints the markup for a custom header.
  1539. *
  1540. * A container div will always be printed in the Customizer preview.
  1541. *
  1542. * @since 4.7.0
  1543. */
  1544. function the_custom_header_markup() {
  1545. $custom_header = get_custom_header_markup();
  1546. if ( empty( $custom_header ) ) {
  1547. return;
  1548. }
  1549. echo $custom_header;
  1550. if ( is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) {
  1551. wp_enqueue_script( 'wp-custom-header' );
  1552. wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings() );
  1553. }
  1554. }
  1555. /**
  1556. * Retrieves background image for custom background.
  1557. *
  1558. * @since 3.0.0
  1559. *
  1560. * @return string
  1561. */
  1562. function get_background_image() {
  1563. return get_theme_mod( 'background_image', get_theme_support( 'custom-background', 'default-image' ) );
  1564. }
  1565. /**
  1566. * Displays background image path.
  1567. *
  1568. * @since 3.0.0
  1569. */
  1570. function background_image() {
  1571. echo get_background_image();
  1572. }
  1573. /**
  1574. * Retrieves value for custom background color.
  1575. *
  1576. * @since 3.0.0
  1577. *
  1578. * @return string
  1579. */
  1580. function get_background_color() {
  1581. return get_theme_mod( 'background_color', get_theme_support( 'custom-background', 'default-color' ) );
  1582. }
  1583. /**
  1584. * Displays background color value.
  1585. *
  1586. * @since 3.0.0
  1587. */
  1588. function background_color() {
  1589. echo get_background_color();
  1590. }
  1591. /**
  1592. * Default custom background callback.
  1593. *
  1594. * @since 3.0.0
  1595. */
  1596. function _custom_background_cb() {
  1597. // $background is the saved custom image, or the default image.
  1598. $background = set_url_scheme( get_background_image() );
  1599. // $color is the saved custom color.
  1600. // A default has to be specified in style.css. It will not be printed here.
  1601. $color = get_background_color();
  1602. if ( get_theme_support( 'custom-background', 'default-color' ) === $color ) {
  1603. $color = false;
  1604. }
  1605. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  1606. if ( ! $background && ! $color ) {
  1607. if ( is_customize_preview() ) {
  1608. printf( '<style%s id="custom-background-css"></style>', $type_attr );
  1609. }
  1610. return;
  1611. }
  1612. $style = $color ? "background-color: #$color;" : '';
  1613. if ( $background ) {
  1614. $image = ' background-image: url("' . sanitize_url( $background ) . '");';
  1615. // Background Position.
  1616. $position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
  1617. $position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
  1618. if ( ! in_array( $position_x, array( 'left', 'center', 'right' ), true ) ) {
  1619. $position_x = 'left';
  1620. }
  1621. if ( ! in_array( $position_y, array( 'top', 'center', 'bottom' ), true ) ) {
  1622. $position_y = 'top';
  1623. }
  1624. $position = " background-position: $position_x $position_y;";
  1625. // Background Size.
  1626. $size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
  1627. if ( ! in_array( $size, array( 'auto', 'contain', 'cover' ), true ) ) {
  1628. $size = 'auto';
  1629. }
  1630. $size = " background-size: $size;";
  1631. // Background Repeat.
  1632. $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
  1633. if ( ! in_array( $repeat, array( 'repeat-x', 'repeat-y', 'repeat', 'no-repeat' ), true ) ) {
  1634. $repeat = 'repeat';
  1635. }
  1636. $repeat = " background-repeat: $repeat;";
  1637. // Background Scroll.
  1638. $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
  1639. if ( 'fixed' !== $attachment ) {
  1640. $attachment = 'scroll';
  1641. }
  1642. $attachment = " background-attachment: $attachment;";
  1643. $style .= $image . $position . $size . $repeat . $attachment;
  1644. }
  1645. ?>
  1646. <style<?php echo $type_attr; ?> id="custom-background-css">
  1647. body.custom-background { <?php echo trim( $style ); ?> }
  1648. </style>
  1649. <?php
  1650. }
  1651. /**
  1652. * Renders the Custom CSS style element.
  1653. *
  1654. * @since 4.7.0
  1655. */
  1656. function wp_custom_css_cb() {
  1657. $styles = wp_get_custom_css();
  1658. if ( $styles || is_customize_preview() ) :
  1659. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  1660. ?>
  1661. <style<?php echo $type_attr; ?> id="wp-custom-css">
  1662. <?php
  1663. // Note that esc_html() cannot be used because `div &gt; span` is not interpreted properly.
  1664. echo strip_tags( $styles );
  1665. ?>
  1666. </style>
  1667. <?php
  1668. endif;
  1669. }
  1670. /**
  1671. * Fetches the `custom_css` post for a given theme.
  1672. *
  1673. * @since 4.7.0
  1674. *
  1675. * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the active theme.
  1676. * @return WP_Post|null The custom_css post or null if none exists.
  1677. */
  1678. function wp_get_custom_css_post( $stylesheet = '' ) {
  1679. if ( empty( $stylesheet ) ) {
  1680. $stylesheet = get_stylesheet();
  1681. }
  1682. $custom_css_query_vars = array(
  1683. 'post_type' => 'custom_css',
  1684. 'post_status' => get_post_stati(),
  1685. 'name' => sanitize_title( $stylesheet ),
  1686. 'posts_per_page' => 1,
  1687. 'no_found_rows' => true,
  1688. 'cache_results' => true,
  1689. 'update_post_meta_cache' => false,
  1690. 'update_post_term_cache' => false,
  1691. 'lazy_load_term_meta' => false,
  1692. );
  1693. $post = null;
  1694. if ( get_stylesheet() === $stylesheet ) {
  1695. $post_id = get_theme_mod( 'custom_css_post_id' );
  1696. if ( $post_id > 0 && get_post( $post_id ) ) {
  1697. $post = get_post( $post_id );
  1698. }
  1699. // `-1` indicates no post exists; no query necessary.
  1700. if ( ! $post && -1 !== $post_id ) {
  1701. $query = new WP_Query( $custom_css_query_vars );
  1702. $post = $query->post;
  1703. /*
  1704. * Cache the lookup. See wp_update_custom_css_post().
  1705. * @todo This should get cleared if a custom_css post is added/removed.
  1706. */
  1707. set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 );
  1708. }
  1709. } else {
  1710. $query = new WP_Query( $custom_css_query_vars );
  1711. $post = $query->post;
  1712. }
  1713. return $post;
  1714. }
  1715. /**
  1716. * Fetches the saved Custom CSS content for rendering.
  1717. *
  1718. * @since 4.7.0
  1719. *
  1720. * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the active theme.
  1721. * @return string The Custom CSS Post content.
  1722. */
  1723. function wp_get_custom_css( $stylesheet = '' ) {
  1724. $css = '';
  1725. if ( empty( $stylesheet ) ) {
  1726. $stylesheet = get_stylesheet();
  1727. }
  1728. $post = wp_get_custom_css_post( $stylesheet );
  1729. if ( $post ) {
  1730. $css = $post->post_content;
  1731. }
  1732. /**
  1733. * Filters the custom CSS output into the head element.
  1734. *
  1735. * @since 4.7.0
  1736. *
  1737. * @param string $css CSS pulled in from the Custom CSS post type.
  1738. * @param string $stylesheet The theme stylesheet name.
  1739. */
  1740. $css = apply_filters( 'wp_get_custom_css', $css, $stylesheet );
  1741. return $css;
  1742. }
  1743. /**
  1744. * Updates the `custom_css` post for a given theme.
  1745. *
  1746. * Inserts a `custom_css` post when one doesn't yet exist.
  1747. *
  1748. * @since 4.7.0
  1749. *
  1750. * @param string $css CSS, stored in `post_content`.
  1751. * @param array $args {
  1752. * Args.
  1753. *
  1754. * @type string $preprocessed Optional. Pre-processed CSS, stored in `post_content_filtered`.
  1755. * Normally empty string.
  1756. * @type string $stylesheet Optional. Stylesheet (child theme) to update.
  1757. * Defaults to active theme/stylesheet.
  1758. * }
  1759. * @return WP_Post|WP_Error Post on success, error on failure.
  1760. */
  1761. function wp_update_custom_css_post( $css, $args = array() ) {
  1762. $args = wp_parse_args(
  1763. $args,
  1764. array(
  1765. 'preprocessed' => '',
  1766. 'stylesheet' => get_stylesheet(),
  1767. )
  1768. );
  1769. $data = array(
  1770. 'css' => $css,
  1771. 'preprocessed' => $args['preprocessed'],
  1772. );
  1773. /**
  1774. * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args
  1775. * for a `custom_css` post being updated.
  1776. *
  1777. * This filter can be used by plugin that offer CSS pre-processors, to store the original
  1778. * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
  1779. * When used in this way, the `post_content_filtered` should be supplied as the setting value
  1780. * instead of `post_content` via a the `customize_value_custom_css` filter, for example:
  1781. *
  1782. * <code>
  1783. * add_filter( 'customize_value_custom_css', function( $value, $setting ) {
  1784. * $post = wp_get_custom_css_post( $setting->stylesheet );
  1785. * if ( $post && ! empty( $post->post_content_filtered ) ) {
  1786. * $css = $post->post_content_filtered;
  1787. * }
  1788. * return $css;
  1789. * }, 10, 2 );
  1790. * </code>
  1791. *
  1792. * @since 4.7.0
  1793. * @param array $data {
  1794. * Custom CSS data.
  1795. *
  1796. * @type string $css CSS stored in `post_content`.
  1797. * @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`.
  1798. * Normally empty string.
  1799. * }
  1800. * @param array $args {
  1801. * The args passed into `wp_update_custom_css_post()` merged with defaults.
  1802. *
  1803. * @type string $css The original CSS passed in to be updated.
  1804. * @type string $preprocessed The original preprocessed CSS passed in to be updated.
  1805. * @type string $stylesheet The stylesheet (theme) being updated.
  1806. * }
  1807. */
  1808. $data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) );
  1809. $post_data = array(
  1810. 'post_title' => $args['stylesheet'],
  1811. 'post_name' => sanitize_title( $args['stylesheet'] ),
  1812. 'post_type' => 'custom_css',
  1813. 'post_status' => 'publish',
  1814. 'post_content' => $data['css'],
  1815. 'post_content_filtered' => $data['preprocessed'],
  1816. );
  1817. // Update post if it already exists, otherwise create a new one.
  1818. $post = wp_get_custom_css_post( $args['stylesheet'] );
  1819. if ( $post ) {
  1820. $post_data['ID'] = $post->ID;
  1821. $r = wp_update_post( wp_slash( $post_data ), true );
  1822. } else {
  1823. $r = wp_insert_post( wp_slash( $post_data ), true );
  1824. if ( ! is_wp_error( $r ) ) {
  1825. if ( get_stylesheet() === $args['stylesheet'] ) {
  1826. set_theme_mod( 'custom_css_post_id', $r );
  1827. }
  1828. // Trigger creation of a revision. This should be removed once #30854 is resolved.
  1829. $revisions = wp_get_latest_revision_id_and_total_count( $r );
  1830. if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) {
  1831. wp_save_post_revision( $r );
  1832. }
  1833. }
  1834. }
  1835. if ( is_wp_error( $r ) ) {
  1836. return $r;
  1837. }
  1838. return get_post( $r );
  1839. }
  1840. /**
  1841. * Adds callback for custom TinyMCE editor stylesheets.
  1842. *
  1843. * The parameter $stylesheet is the name of the stylesheet, relative to
  1844. * the theme root. It also accepts an array of stylesheets.
  1845. * It is optional and defaults to 'editor-style.css'.
  1846. *
  1847. * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
  1848. * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
  1849. * If an array of stylesheets is passed to add_editor_style(),
  1850. * RTL is only added for the first stylesheet.
  1851. *
  1852. * Since version 3.4 the TinyMCE body has .rtl CSS class.
  1853. * It is a better option to use that class and add any RTL styles to the main stylesheet.
  1854. *
  1855. * @since 3.0.0
  1856. *
  1857. * @global array $editor_styles
  1858. *
  1859. * @param array|string $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
  1860. * Defaults to 'editor-style.css'
  1861. */
  1862. function add_editor_style( $stylesheet = 'editor-style.css' ) {
  1863. global $editor_styles;
  1864. add_theme_support( 'editor-style' );
  1865. $editor_styles = (array) $editor_styles;
  1866. $stylesheet = (array) $stylesheet;
  1867. if ( is_rtl() ) {
  1868. $rtl_stylesheet = str_replace( '.css', '-rtl.css', $stylesheet[0] );
  1869. $stylesheet[] = $rtl_stylesheet;
  1870. }
  1871. $editor_styles = array_merge( $editor_styles, $stylesheet );
  1872. }
  1873. /**
  1874. * Removes all visual editor stylesheets.
  1875. *
  1876. * @since 3.1.0
  1877. *
  1878. * @global array $editor_styles
  1879. *
  1880. * @return bool True on success, false if there were no stylesheets to remove.
  1881. */
  1882. function remove_editor_styles() {
  1883. if ( ! current_theme_supports( 'editor-style' ) ) {
  1884. return false;
  1885. }
  1886. _remove_theme_support( 'editor-style' );
  1887. if ( is_admin() ) {
  1888. $GLOBALS['editor_styles'] = array();
  1889. }
  1890. return true;
  1891. }
  1892. /**
  1893. * Retrieves any registered editor stylesheet URLs.
  1894. *
  1895. * @since 4.0.0
  1896. *
  1897. * @global array $editor_styles Registered editor stylesheets
  1898. *
  1899. * @return string[] If registered, a list of editor stylesheet URLs.
  1900. */
  1901. function get_editor_stylesheets() {
  1902. $stylesheets = array();
  1903. // Load editor_style.css if the active theme supports it.
  1904. if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
  1905. $editor_styles = $GLOBALS['editor_styles'];
  1906. $editor_styles = array_unique( array_filter( $editor_styles ) );
  1907. $style_uri = get_stylesheet_directory_uri();
  1908. $style_dir = get_stylesheet_directory();
  1909. // Support externally referenced styles (like, say, fonts).
  1910. foreach ( $editor_styles as $key => $file ) {
  1911. if ( preg_match( '~^(https?:)?//~', $file ) ) {
  1912. $stylesheets[] = sanitize_url( $file );
  1913. unset( $editor_styles[ $key ] );
  1914. }
  1915. }
  1916. // Look in a parent theme first, that way child theme CSS overrides.
  1917. if ( is_child_theme() ) {
  1918. $template_uri = get_template_directory_uri();
  1919. $template_dir = get_template_directory();
  1920. foreach ( $editor_styles as $key => $file ) {
  1921. if ( $file && file_exists( "$template_dir/$file" ) ) {
  1922. $stylesheets[] = "$template_uri/$file";
  1923. }
  1924. }
  1925. }
  1926. foreach ( $editor_styles as $file ) {
  1927. if ( $file && file_exists( "$style_dir/$file" ) ) {
  1928. $stylesheets[] = "$style_uri/$file";
  1929. }
  1930. }
  1931. }
  1932. /**
  1933. * Filters the array of URLs of stylesheets applied to the editor.
  1934. *
  1935. * @since 4.3.0
  1936. *
  1937. * @param string[] $stylesheets Array of URLs of stylesheets to be applied to the editor.
  1938. */
  1939. return apply_filters( 'editor_stylesheets', $stylesheets );
  1940. }
  1941. /**
  1942. * Expands a theme's starter content configuration using core-provided data.
  1943. *
  1944. * @since 4.7.0
  1945. *
  1946. * @return array Array of starter content.
  1947. */
  1948. function get_theme_starter_content() {
  1949. $theme_support = get_theme_support( 'starter-content' );
  1950. if ( is_array( $theme_support ) && ! empty( $theme_support[0] ) && is_array( $theme_support[0] ) ) {
  1951. $config = $theme_support[0];
  1952. } else {
  1953. $config = array();
  1954. }
  1955. $core_content = array(
  1956. 'widgets' => array(
  1957. 'text_business_info' => array(
  1958. 'text',
  1959. array(
  1960. 'title' => _x( 'Find Us', 'Theme starter content' ),
  1961. 'text' => implode(
  1962. '',
  1963. array(
  1964. '<strong>' . _x( 'Address', 'Theme starter content' ) . "</strong>\n",
  1965. _x( '123 Main Street', 'Theme starter content' ) . "\n",
  1966. _x( 'New York, NY 10001', 'Theme starter content' ) . "\n\n",
  1967. '<strong>' . _x( 'Hours', 'Theme starter content' ) . "</strong>\n",
  1968. _x( 'Monday&ndash;Friday: 9:00AM&ndash;5:00PM', 'Theme starter content' ) . "\n",
  1969. _x( 'Saturday &amp; Sunday: 11:00AM&ndash;3:00PM', 'Theme starter content' ),
  1970. )
  1971. ),
  1972. 'filter' => true,
  1973. 'visual' => true,
  1974. ),
  1975. ),
  1976. 'text_about' => array(
  1977. 'text',
  1978. array(
  1979. 'title' => _x( 'About This Site', 'Theme starter content' ),
  1980. 'text' => _x( 'This may be a good place to introduce yourself and your site or include some credits.', 'Theme starter content' ),
  1981. 'filter' => true,
  1982. 'visual' => true,
  1983. ),
  1984. ),
  1985. 'archives' => array(
  1986. 'archives',
  1987. array(
  1988. 'title' => _x( 'Archives', 'Theme starter content' ),
  1989. ),
  1990. ),
  1991. 'calendar' => array(
  1992. 'calendar',
  1993. array(
  1994. 'title' => _x( 'Calendar', 'Theme starter content' ),
  1995. ),
  1996. ),
  1997. 'categories' => array(
  1998. 'categories',
  1999. array(
  2000. 'title' => _x( 'Categories', 'Theme starter content' ),
  2001. ),
  2002. ),
  2003. 'meta' => array(
  2004. 'meta',
  2005. array(
  2006. 'title' => _x( 'Meta', 'Theme starter content' ),
  2007. ),
  2008. ),
  2009. 'recent-comments' => array(
  2010. 'recent-comments',
  2011. array(
  2012. 'title' => _x( 'Recent Comments', 'Theme starter content' ),
  2013. ),
  2014. ),
  2015. 'recent-posts' => array(
  2016. 'recent-posts',
  2017. array(
  2018. 'title' => _x( 'Recent Posts', 'Theme starter content' ),
  2019. ),
  2020. ),
  2021. 'search' => array(
  2022. 'search',
  2023. array(
  2024. 'title' => _x( 'Search', 'Theme starter content' ),
  2025. ),
  2026. ),
  2027. ),
  2028. 'nav_menus' => array(
  2029. 'link_home' => array(
  2030. 'type' => 'custom',
  2031. 'title' => _x( 'Home', 'Theme starter content' ),
  2032. 'url' => home_url( '/' ),
  2033. ),
  2034. 'page_home' => array( // Deprecated in favor of 'link_home'.
  2035. 'type' => 'post_type',
  2036. 'object' => 'page',
  2037. 'object_id' => '{{home}}',
  2038. ),
  2039. 'page_about' => array(
  2040. 'type' => 'post_type',
  2041. 'object' => 'page',
  2042. 'object_id' => '{{about}}',
  2043. ),
  2044. 'page_blog' => array(
  2045. 'type' => 'post_type',
  2046. 'object' => 'page',
  2047. 'object_id' => '{{blog}}',
  2048. ),
  2049. 'page_news' => array(
  2050. 'type' => 'post_type',
  2051. 'object' => 'page',
  2052. 'object_id' => '{{news}}',
  2053. ),
  2054. 'page_contact' => array(
  2055. 'type' => 'post_type',
  2056. 'object' => 'page',
  2057. 'object_id' => '{{contact}}',
  2058. ),
  2059. 'link_email' => array(
  2060. 'title' => _x( 'Email', 'Theme starter content' ),
  2061. 'url' => 'mailto:wordpress@example.com',
  2062. ),
  2063. 'link_facebook' => array(
  2064. 'title' => _x( 'Facebook', 'Theme starter content' ),
  2065. 'url' => 'https://www.facebook.com/wordpress',
  2066. ),
  2067. 'link_foursquare' => array(
  2068. 'title' => _x( 'Foursquare', 'Theme starter content' ),
  2069. 'url' => 'https://foursquare.com/',
  2070. ),
  2071. 'link_github' => array(
  2072. 'title' => _x( 'GitHub', 'Theme starter content' ),
  2073. 'url' => 'https://github.com/wordpress/',
  2074. ),
  2075. 'link_instagram' => array(
  2076. 'title' => _x( 'Instagram', 'Theme starter content' ),
  2077. 'url' => 'https://www.instagram.com/explore/tags/wordcamp/',
  2078. ),
  2079. 'link_linkedin' => array(
  2080. 'title' => _x( 'LinkedIn', 'Theme starter content' ),
  2081. 'url' => 'https://www.linkedin.com/company/1089783',
  2082. ),
  2083. 'link_pinterest' => array(
  2084. 'title' => _x( 'Pinterest', 'Theme starter content' ),
  2085. 'url' => 'https://www.pinterest.com/',
  2086. ),
  2087. 'link_twitter' => array(
  2088. 'title' => _x( 'Twitter', 'Theme starter content' ),
  2089. 'url' => 'https://twitter.com/wordpress',
  2090. ),
  2091. 'link_yelp' => array(
  2092. 'title' => _x( 'Yelp', 'Theme starter content' ),
  2093. 'url' => 'https://www.yelp.com',
  2094. ),
  2095. 'link_youtube' => array(
  2096. 'title' => _x( 'YouTube', 'Theme starter content' ),
  2097. 'url' => 'https://www.youtube.com/channel/UCdof4Ju7amm1chz1gi1T2ZA',
  2098. ),
  2099. ),
  2100. 'posts' => array(
  2101. 'home' => array(
  2102. 'post_type' => 'page',
  2103. 'post_title' => _x( 'Home', 'Theme starter content' ),
  2104. 'post_content' => sprintf(
  2105. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2106. _x( 'Welcome to your site! This is your homepage, which is what most visitors will see when they come to your site for the first time.', 'Theme starter content' )
  2107. ),
  2108. ),
  2109. 'about' => array(
  2110. 'post_type' => 'page',
  2111. 'post_title' => _x( 'About', 'Theme starter content' ),
  2112. 'post_content' => sprintf(
  2113. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2114. _x( 'You might be an artist who would like to introduce yourself and your work here or maybe you&rsquo;re a business with a mission to describe.', 'Theme starter content' )
  2115. ),
  2116. ),
  2117. 'contact' => array(
  2118. 'post_type' => 'page',
  2119. 'post_title' => _x( 'Contact', 'Theme starter content' ),
  2120. 'post_content' => sprintf(
  2121. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2122. _x( 'This is a page with some basic contact information, such as an address and phone number. You might also try a plugin to add a contact form.', 'Theme starter content' )
  2123. ),
  2124. ),
  2125. 'blog' => array(
  2126. 'post_type' => 'page',
  2127. 'post_title' => _x( 'Blog', 'Theme starter content' ),
  2128. ),
  2129. 'news' => array(
  2130. 'post_type' => 'page',
  2131. 'post_title' => _x( 'News', 'Theme starter content' ),
  2132. ),
  2133. 'homepage-section' => array(
  2134. 'post_type' => 'page',
  2135. 'post_title' => _x( 'A homepage section', 'Theme starter content' ),
  2136. 'post_content' => sprintf(
  2137. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2138. _x( 'This is an example of a homepage section. Homepage sections can be any page other than the homepage itself, including the page that shows your latest blog posts.', 'Theme starter content' )
  2139. ),
  2140. ),
  2141. ),
  2142. );
  2143. $content = array();
  2144. foreach ( $config as $type => $args ) {
  2145. switch ( $type ) {
  2146. // Use options and theme_mods as-is.
  2147. case 'options':
  2148. case 'theme_mods':
  2149. $content[ $type ] = $config[ $type ];
  2150. break;
  2151. // Widgets are grouped into sidebars.
  2152. case 'widgets':
  2153. foreach ( $config[ $type ] as $sidebar_id => $widgets ) {
  2154. foreach ( $widgets as $id => $widget ) {
  2155. if ( is_array( $widget ) ) {
  2156. // Item extends core content.
  2157. if ( ! empty( $core_content[ $type ][ $id ] ) ) {
  2158. $widget = array(
  2159. $core_content[ $type ][ $id ][0],
  2160. array_merge( $core_content[ $type ][ $id ][1], $widget ),
  2161. );
  2162. }
  2163. $content[ $type ][ $sidebar_id ][] = $widget;
  2164. } elseif ( is_string( $widget )
  2165. && ! empty( $core_content[ $type ] )
  2166. && ! empty( $core_content[ $type ][ $widget ] )
  2167. ) {
  2168. $content[ $type ][ $sidebar_id ][] = $core_content[ $type ][ $widget ];
  2169. }
  2170. }
  2171. }
  2172. break;
  2173. // And nav menu items are grouped into nav menus.
  2174. case 'nav_menus':
  2175. foreach ( $config[ $type ] as $nav_menu_location => $nav_menu ) {
  2176. // Ensure nav menus get a name.
  2177. if ( empty( $nav_menu['name'] ) ) {
  2178. $nav_menu['name'] = $nav_menu_location;
  2179. }
  2180. $content[ $type ][ $nav_menu_location ]['name'] = $nav_menu['name'];
  2181. foreach ( $nav_menu['items'] as $id => $nav_menu_item ) {
  2182. if ( is_array( $nav_menu_item ) ) {
  2183. // Item extends core content.
  2184. if ( ! empty( $core_content[ $type ][ $id ] ) ) {
  2185. $nav_menu_item = array_merge( $core_content[ $type ][ $id ], $nav_menu_item );
  2186. }
  2187. $content[ $type ][ $nav_menu_location ]['items'][] = $nav_menu_item;
  2188. } elseif ( is_string( $nav_menu_item )
  2189. && ! empty( $core_content[ $type ] )
  2190. && ! empty( $core_content[ $type ][ $nav_menu_item ] )
  2191. ) {
  2192. $content[ $type ][ $nav_menu_location ]['items'][] = $core_content[ $type ][ $nav_menu_item ];
  2193. }
  2194. }
  2195. }
  2196. break;
  2197. // Attachments are posts but have special treatment.
  2198. case 'attachments':
  2199. foreach ( $config[ $type ] as $id => $item ) {
  2200. if ( ! empty( $item['file'] ) ) {
  2201. $content[ $type ][ $id ] = $item;
  2202. }
  2203. }
  2204. break;
  2205. // All that's left now are posts (besides attachments).
  2206. // Not a default case for the sake of clarity and future work.
  2207. case 'posts':
  2208. foreach ( $config[ $type ] as $id => $item ) {
  2209. if ( is_array( $item ) ) {
  2210. // Item extends core content.
  2211. if ( ! empty( $core_content[ $type ][ $id ] ) ) {
  2212. $item = array_merge( $core_content[ $type ][ $id ], $item );
  2213. }
  2214. // Enforce a subset of fields.
  2215. $content[ $type ][ $id ] = wp_array_slice_assoc(
  2216. $item,
  2217. array(
  2218. 'post_type',
  2219. 'post_title',
  2220. 'post_excerpt',
  2221. 'post_name',
  2222. 'post_content',
  2223. 'menu_order',
  2224. 'comment_status',
  2225. 'thumbnail',
  2226. 'template',
  2227. )
  2228. );
  2229. } elseif ( is_string( $item ) && ! empty( $core_content[ $type ][ $item ] ) ) {
  2230. $content[ $type ][ $item ] = $core_content[ $type ][ $item ];
  2231. }
  2232. }
  2233. break;
  2234. }
  2235. }
  2236. /**
  2237. * Filters the expanded array of starter content.
  2238. *
  2239. * @since 4.7.0
  2240. *
  2241. * @param array $content Array of starter content.
  2242. * @param array $config Array of theme-specific starter content configuration.
  2243. */
  2244. return apply_filters( 'get_theme_starter_content', $content, $config );
  2245. }
  2246. /**
  2247. * Registers theme support for a given feature.
  2248. *
  2249. * Must be called in the theme's functions.php file to work.
  2250. * If attached to a hook, it must be {@see 'after_setup_theme'}.
  2251. * The {@see 'init'} hook may be too late for some features.
  2252. *
  2253. * Example usage:
  2254. *
  2255. * add_theme_support( 'title-tag' );
  2256. * add_theme_support( 'custom-logo', array(
  2257. * 'height' => 480,
  2258. * 'width' => 720,
  2259. * ) );
  2260. *
  2261. * @since 2.9.0
  2262. * @since 3.4.0 The `custom-header-uploads` feature was deprecated.
  2263. * @since 3.6.0 The `html5` feature was added.
  2264. * @since 3.6.1 The `html5` feature requires an array of types to be passed. Defaults to
  2265. * 'comment-list', 'comment-form', 'search-form' for backward compatibility.
  2266. * @since 3.9.0 The `html5` feature now also accepts 'gallery' and 'caption'.
  2267. * @since 4.1.0 The `title-tag` feature was added.
  2268. * @since 4.5.0 The `customize-selective-refresh-widgets` feature was added.
  2269. * @since 4.7.0 The `starter-content` feature was added.
  2270. * @since 5.0.0 The `responsive-embeds`, `align-wide`, `dark-editor-style`, `disable-custom-colors`,
  2271. * `disable-custom-font-sizes`, `editor-color-palette`, `editor-font-sizes`,
  2272. * `editor-styles`, and `wp-block-styles` features were added.
  2273. * @since 5.3.0 The `html5` feature now also accepts 'script' and 'style'.
  2274. * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  2275. * by adding it to the function signature.
  2276. * @since 5.5.0 The `core-block-patterns` feature was added and is enabled by default.
  2277. * @since 5.5.0 The `custom-logo` feature now also accepts 'unlink-homepage-logo'.
  2278. * @since 5.6.0 The `post-formats` feature warns if no array is passed as the second parameter.
  2279. * @since 5.8.0 The `widgets-block-editor` feature enables the Widgets block editor.
  2280. * @since 6.0.0 The `html5` feature warns if no array is passed as the second parameter.
  2281. *
  2282. * @global array $_wp_theme_features
  2283. *
  2284. * @param string $feature The feature being added. Likely core values include:
  2285. * - 'admin-bar'
  2286. * - 'align-wide'
  2287. * - 'automatic-feed-links'
  2288. * - 'core-block-patterns'
  2289. * - 'custom-background'
  2290. * - 'custom-header'
  2291. * - 'custom-line-height'
  2292. * - 'custom-logo'
  2293. * - 'customize-selective-refresh-widgets'
  2294. * - 'custom-spacing'
  2295. * - 'custom-units'
  2296. * - 'dark-editor-style'
  2297. * - 'disable-custom-colors'
  2298. * - 'disable-custom-font-sizes'
  2299. * - 'editor-color-palette'
  2300. * - 'editor-gradient-presets'
  2301. * - 'editor-font-sizes'
  2302. * - 'editor-styles'
  2303. * - 'featured-content'
  2304. * - 'html5'
  2305. * - 'menus'
  2306. * - 'post-formats'
  2307. * - 'post-thumbnails'
  2308. * - 'responsive-embeds'
  2309. * - 'starter-content'
  2310. * - 'title-tag'
  2311. * - 'wp-block-styles'
  2312. * - 'widgets'
  2313. * - 'widgets-block-editor'
  2314. * @param mixed ...$args Optional extra arguments to pass along with certain features.
  2315. * @return void|false Void on success, false on failure.
  2316. */
  2317. function add_theme_support( $feature, ...$args ) {
  2318. global $_wp_theme_features;
  2319. if ( ! $args ) {
  2320. $args = true;
  2321. }
  2322. switch ( $feature ) {
  2323. case 'post-thumbnails':
  2324. // All post types are already supported.
  2325. if ( true === get_theme_support( 'post-thumbnails' ) ) {
  2326. return;
  2327. }
  2328. /*
  2329. * Merge post types with any that already declared their support
  2330. * for post thumbnails.
  2331. */
  2332. if ( isset( $args[0] ) && is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) {
  2333. $args[0] = array_unique( array_merge( $_wp_theme_features['post-thumbnails'][0], $args[0] ) );
  2334. }
  2335. break;
  2336. case 'post-formats':
  2337. if ( isset( $args[0] ) && is_array( $args[0] ) ) {
  2338. $post_formats = get_post_format_slugs();
  2339. unset( $post_formats['standard'] );
  2340. $args[0] = array_intersect( $args[0], array_keys( $post_formats ) );
  2341. } else {
  2342. _doing_it_wrong(
  2343. "add_theme_support( 'post-formats' )",
  2344. __( 'You need to pass an array of post formats.' ),
  2345. '5.6.0'
  2346. );
  2347. return false;
  2348. }
  2349. break;
  2350. case 'html5':
  2351. // You can't just pass 'html5', you need to pass an array of types.
  2352. if ( empty( $args[0] ) || ! is_array( $args[0] ) ) {
  2353. _doing_it_wrong(
  2354. "add_theme_support( 'html5' )",
  2355. __( 'You need to pass an array of types.' ),
  2356. '3.6.1'
  2357. );
  2358. if ( ! empty( $args[0] ) && ! is_array( $args[0] ) ) {
  2359. return false;
  2360. }
  2361. // Build an array of types for back-compat.
  2362. $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
  2363. }
  2364. // Calling 'html5' again merges, rather than overwrites.
  2365. if ( isset( $_wp_theme_features['html5'] ) ) {
  2366. $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
  2367. }
  2368. break;
  2369. case 'custom-logo':
  2370. if ( true === $args ) {
  2371. $args = array( 0 => array() );
  2372. }
  2373. $defaults = array(
  2374. 'width' => null,
  2375. 'height' => null,
  2376. 'flex-width' => false,
  2377. 'flex-height' => false,
  2378. 'header-text' => '',
  2379. 'unlink-homepage-logo' => false,
  2380. );
  2381. $args[0] = wp_parse_args( array_intersect_key( $args[0], $defaults ), $defaults );
  2382. // Allow full flexibility if no size is specified.
  2383. if ( is_null( $args[0]['width'] ) && is_null( $args[0]['height'] ) ) {
  2384. $args[0]['flex-width'] = true;
  2385. $args[0]['flex-height'] = true;
  2386. }
  2387. break;
  2388. case 'custom-header-uploads':
  2389. return add_theme_support( 'custom-header', array( 'uploads' => true ) );
  2390. case 'custom-header':
  2391. if ( true === $args ) {
  2392. $args = array( 0 => array() );
  2393. }
  2394. $defaults = array(
  2395. 'default-image' => '',
  2396. 'random-default' => false,
  2397. 'width' => 0,
  2398. 'height' => 0,
  2399. 'flex-height' => false,
  2400. 'flex-width' => false,
  2401. 'default-text-color' => '',
  2402. 'header-text' => true,
  2403. 'uploads' => true,
  2404. 'wp-head-callback' => '',
  2405. 'admin-head-callback' => '',
  2406. 'admin-preview-callback' => '',
  2407. 'video' => false,
  2408. 'video-active-callback' => 'is_front_page',
  2409. );
  2410. $jit = isset( $args[0]['__jit'] );
  2411. unset( $args[0]['__jit'] );
  2412. // Merge in data from previous add_theme_support() calls.
  2413. // The first value registered wins. (A child theme is set up first.)
  2414. if ( isset( $_wp_theme_features['custom-header'] ) ) {
  2415. $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
  2416. }
  2417. // Load in the defaults at the end, as we need to insure first one wins.
  2418. // This will cause all constants to be defined, as each arg will then be set to the default.
  2419. if ( $jit ) {
  2420. $args[0] = wp_parse_args( $args[0], $defaults );
  2421. }
  2422. /*
  2423. * If a constant was defined, use that value. Otherwise, define the constant to ensure
  2424. * the constant is always accurate (and is not defined later, overriding our value).
  2425. * As stated above, the first value wins.
  2426. * Once we get to wp_loaded (just-in-time), define any constants we haven't already.
  2427. * Constants are lame. Don't reference them. This is just for backward compatibility.
  2428. */
  2429. if ( defined( 'NO_HEADER_TEXT' ) ) {
  2430. $args[0]['header-text'] = ! NO_HEADER_TEXT;
  2431. } elseif ( isset( $args[0]['header-text'] ) ) {
  2432. define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
  2433. }
  2434. if ( defined( 'HEADER_IMAGE_WIDTH' ) ) {
  2435. $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
  2436. } elseif ( isset( $args[0]['width'] ) ) {
  2437. define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
  2438. }
  2439. if ( defined( 'HEADER_IMAGE_HEIGHT' ) ) {
  2440. $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
  2441. } elseif ( isset( $args[0]['height'] ) ) {
  2442. define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
  2443. }
  2444. if ( defined( 'HEADER_TEXTCOLOR' ) ) {
  2445. $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
  2446. } elseif ( isset( $args[0]['default-text-color'] ) ) {
  2447. define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
  2448. }
  2449. if ( defined( 'HEADER_IMAGE' ) ) {
  2450. $args[0]['default-image'] = HEADER_IMAGE;
  2451. } elseif ( isset( $args[0]['default-image'] ) ) {
  2452. define( 'HEADER_IMAGE', $args[0]['default-image'] );
  2453. }
  2454. if ( $jit && ! empty( $args[0]['default-image'] ) ) {
  2455. $args[0]['random-default'] = false;
  2456. }
  2457. // If headers are supported, and we still don't have a defined width or height,
  2458. // we have implicit flex sizes.
  2459. if ( $jit ) {
  2460. if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) ) {
  2461. $args[0]['flex-width'] = true;
  2462. }
  2463. if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) ) {
  2464. $args[0]['flex-height'] = true;
  2465. }
  2466. }
  2467. break;
  2468. case 'custom-background':
  2469. if ( true === $args ) {
  2470. $args = array( 0 => array() );
  2471. }
  2472. $defaults = array(
  2473. 'default-image' => '',
  2474. 'default-preset' => 'default',
  2475. 'default-position-x' => 'left',
  2476. 'default-position-y' => 'top',
  2477. 'default-size' => 'auto',
  2478. 'default-repeat' => 'repeat',
  2479. 'default-attachment' => 'scroll',
  2480. 'default-color' => '',
  2481. 'wp-head-callback' => '_custom_background_cb',
  2482. 'admin-head-callback' => '',
  2483. 'admin-preview-callback' => '',
  2484. );
  2485. $jit = isset( $args[0]['__jit'] );
  2486. unset( $args[0]['__jit'] );
  2487. // Merge in data from previous add_theme_support() calls. The first value registered wins.
  2488. if ( isset( $_wp_theme_features['custom-background'] ) ) {
  2489. $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
  2490. }
  2491. if ( $jit ) {
  2492. $args[0] = wp_parse_args( $args[0], $defaults );
  2493. }
  2494. if ( defined( 'BACKGROUND_COLOR' ) ) {
  2495. $args[0]['default-color'] = BACKGROUND_COLOR;
  2496. } elseif ( isset( $args[0]['default-color'] ) || $jit ) {
  2497. define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
  2498. }
  2499. if ( defined( 'BACKGROUND_IMAGE' ) ) {
  2500. $args[0]['default-image'] = BACKGROUND_IMAGE;
  2501. } elseif ( isset( $args[0]['default-image'] ) || $jit ) {
  2502. define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
  2503. }
  2504. break;
  2505. // Ensure that 'title-tag' is accessible in the admin.
  2506. case 'title-tag':
  2507. // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php.
  2508. if ( did_action( 'wp_loaded' ) ) {
  2509. _doing_it_wrong(
  2510. "add_theme_support( 'title-tag' )",
  2511. sprintf(
  2512. /* translators: 1: title-tag, 2: wp_loaded */
  2513. __( 'Theme support for %1$s should be registered before the %2$s hook.' ),
  2514. '<code>title-tag</code>',
  2515. '<code>wp_loaded</code>'
  2516. ),
  2517. '4.1.0'
  2518. );
  2519. return false;
  2520. }
  2521. }
  2522. $_wp_theme_features[ $feature ] = $args;
  2523. }
  2524. /**
  2525. * Registers the internal custom header and background routines.
  2526. *
  2527. * @since 3.4.0
  2528. * @access private
  2529. *
  2530. * @global Custom_Image_Header $custom_image_header
  2531. * @global Custom_Background $custom_background
  2532. */
  2533. function _custom_header_background_just_in_time() {
  2534. global $custom_image_header, $custom_background;
  2535. if ( current_theme_supports( 'custom-header' ) ) {
  2536. // In case any constants were defined after an add_custom_image_header() call, re-run.
  2537. add_theme_support( 'custom-header', array( '__jit' => true ) );
  2538. $args = get_theme_support( 'custom-header' );
  2539. if ( $args[0]['wp-head-callback'] ) {
  2540. add_action( 'wp_head', $args[0]['wp-head-callback'] );
  2541. }
  2542. if ( is_admin() ) {
  2543. require_once ABSPATH . 'wp-admin/includes/class-custom-image-header.php';
  2544. $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
  2545. }
  2546. }
  2547. if ( current_theme_supports( 'custom-background' ) ) {
  2548. // In case any constants were defined after an add_custom_background() call, re-run.
  2549. add_theme_support( 'custom-background', array( '__jit' => true ) );
  2550. $args = get_theme_support( 'custom-background' );
  2551. add_action( 'wp_head', $args[0]['wp-head-callback'] );
  2552. if ( is_admin() ) {
  2553. require_once ABSPATH . 'wp-admin/includes/class-custom-background.php';
  2554. $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
  2555. }
  2556. }
  2557. }
  2558. /**
  2559. * Adds CSS to hide header text for custom logo, based on Customizer setting.
  2560. *
  2561. * @since 4.5.0
  2562. * @access private
  2563. */
  2564. function _custom_logo_header_styles() {
  2565. if ( ! current_theme_supports( 'custom-header', 'header-text' )
  2566. && get_theme_support( 'custom-logo', 'header-text' )
  2567. && ! get_theme_mod( 'header_text', true )
  2568. ) {
  2569. $classes = (array) get_theme_support( 'custom-logo', 'header-text' );
  2570. $classes = array_map( 'sanitize_html_class', $classes );
  2571. $classes = '.' . implode( ', .', $classes );
  2572. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  2573. ?>
  2574. <!-- Custom Logo: hide header text -->
  2575. <style id="custom-logo-css"<?php echo $type_attr; ?>>
  2576. <?php echo $classes; ?> {
  2577. position: absolute;
  2578. clip: rect(1px, 1px, 1px, 1px);
  2579. }
  2580. </style>
  2581. <?php
  2582. }
  2583. }
  2584. /**
  2585. * Gets the theme support arguments passed when registering that support.
  2586. *
  2587. * Example usage:
  2588. *
  2589. * get_theme_support( 'custom-logo' );
  2590. * get_theme_support( 'custom-header', 'width' );
  2591. *
  2592. * @since 3.1.0
  2593. * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  2594. * by adding it to the function signature.
  2595. *
  2596. * @global array $_wp_theme_features
  2597. *
  2598. * @param string $feature The feature to check. See add_theme_support() for the list
  2599. * of possible values.
  2600. * @param mixed ...$args Optional extra arguments to be checked against certain features.
  2601. * @return mixed The array of extra arguments or the value for the registered feature.
  2602. */
  2603. function get_theme_support( $feature, ...$args ) {
  2604. global $_wp_theme_features;
  2605. if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
  2606. return false;
  2607. }
  2608. if ( ! $args ) {
  2609. return $_wp_theme_features[ $feature ];
  2610. }
  2611. switch ( $feature ) {
  2612. case 'custom-logo':
  2613. case 'custom-header':
  2614. case 'custom-background':
  2615. if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) ) {
  2616. return $_wp_theme_features[ $feature ][0][ $args[0] ];
  2617. }
  2618. return false;
  2619. default:
  2620. return $_wp_theme_features[ $feature ];
  2621. }
  2622. }
  2623. /**
  2624. * Allows a theme to de-register its support of a certain feature
  2625. *
  2626. * Should be called in the theme's functions.php file. Generally would
  2627. * be used for child themes to override support from the parent theme.
  2628. *
  2629. * @since 3.0.0
  2630. *
  2631. * @see add_theme_support()
  2632. *
  2633. * @param string $feature The feature being removed. See add_theme_support() for the list
  2634. * of possible values.
  2635. * @return bool|void Whether feature was removed.
  2636. */
  2637. function remove_theme_support( $feature ) {
  2638. // Do not remove internal registrations that are not used directly by themes.
  2639. if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ), true ) ) {
  2640. return false;
  2641. }
  2642. return _remove_theme_support( $feature );
  2643. }
  2644. /**
  2645. * Do not use. Removes theme support internally without knowledge of those not used
  2646. * by themes directly.
  2647. *
  2648. * @access private
  2649. * @since 3.1.0
  2650. * @global array $_wp_theme_features
  2651. * @global Custom_Image_Header $custom_image_header
  2652. * @global Custom_Background $custom_background
  2653. *
  2654. * @param string $feature The feature being removed. See add_theme_support() for the list
  2655. * of possible values.
  2656. * @return bool True if support was removed, false if the feature was not registered.
  2657. */
  2658. function _remove_theme_support( $feature ) {
  2659. global $_wp_theme_features;
  2660. switch ( $feature ) {
  2661. case 'custom-header-uploads':
  2662. if ( ! isset( $_wp_theme_features['custom-header'] ) ) {
  2663. return false;
  2664. }
  2665. add_theme_support( 'custom-header', array( 'uploads' => false ) );
  2666. return; // Do not continue - custom-header-uploads no longer exists.
  2667. }
  2668. if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
  2669. return false;
  2670. }
  2671. switch ( $feature ) {
  2672. case 'custom-header':
  2673. if ( ! did_action( 'wp_loaded' ) ) {
  2674. break;
  2675. }
  2676. $support = get_theme_support( 'custom-header' );
  2677. if ( isset( $support[0]['wp-head-callback'] ) ) {
  2678. remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  2679. }
  2680. if ( isset( $GLOBALS['custom_image_header'] ) ) {
  2681. remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
  2682. unset( $GLOBALS['custom_image_header'] );
  2683. }
  2684. break;
  2685. case 'custom-background':
  2686. if ( ! did_action( 'wp_loaded' ) ) {
  2687. break;
  2688. }
  2689. $support = get_theme_support( 'custom-background' );
  2690. if ( isset( $support[0]['wp-head-callback'] ) ) {
  2691. remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  2692. }
  2693. remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
  2694. unset( $GLOBALS['custom_background'] );
  2695. break;
  2696. }
  2697. unset( $_wp_theme_features[ $feature ] );
  2698. return true;
  2699. }
  2700. /**
  2701. * Checks a theme's support for a given feature.
  2702. *
  2703. * Example usage:
  2704. *
  2705. * current_theme_supports( 'custom-logo' );
  2706. * current_theme_supports( 'html5', 'comment-form' );
  2707. *
  2708. * @since 2.9.0
  2709. * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  2710. * by adding it to the function signature.
  2711. *
  2712. * @global array $_wp_theme_features
  2713. *
  2714. * @param string $feature The feature being checked. See add_theme_support() for the list
  2715. * of possible values.
  2716. * @param mixed ...$args Optional extra arguments to be checked against certain features.
  2717. * @return bool True if the active theme supports the feature, false otherwise.
  2718. */
  2719. function current_theme_supports( $feature, ...$args ) {
  2720. global $_wp_theme_features;
  2721. if ( 'custom-header-uploads' === $feature ) {
  2722. return current_theme_supports( 'custom-header', 'uploads' );
  2723. }
  2724. if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
  2725. return false;
  2726. }
  2727. // If no args passed then no extra checks need to be performed.
  2728. if ( ! $args ) {
  2729. /** This filter is documented in wp-includes/theme.php */
  2730. return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  2731. }
  2732. switch ( $feature ) {
  2733. case 'post-thumbnails':
  2734. /*
  2735. * post-thumbnails can be registered for only certain content/post types
  2736. * by passing an array of types to add_theme_support().
  2737. * If no array was passed, then any type is accepted.
  2738. */
  2739. if ( true === $_wp_theme_features[ $feature ] ) { // Registered for all types.
  2740. return true;
  2741. }
  2742. $content_type = $args[0];
  2743. return in_array( $content_type, $_wp_theme_features[ $feature ][0], true );
  2744. case 'html5':
  2745. case 'post-formats':
  2746. /*
  2747. * Specific post formats can be registered by passing an array of types
  2748. * to add_theme_support().
  2749. *
  2750. * Specific areas of HTML5 support *must* be passed via an array to add_theme_support().
  2751. */
  2752. $type = $args[0];
  2753. return in_array( $type, $_wp_theme_features[ $feature ][0], true );
  2754. case 'custom-logo':
  2755. case 'custom-header':
  2756. case 'custom-background':
  2757. // Specific capabilities can be registered by passing an array to add_theme_support().
  2758. return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] );
  2759. }
  2760. /**
  2761. * Filters whether the active theme supports a specific feature.
  2762. *
  2763. * The dynamic portion of the hook name, `$feature`, refers to the specific
  2764. * theme feature. See add_theme_support() for the list of possible values.
  2765. *
  2766. * @since 3.4.0
  2767. *
  2768. * @param bool $supports Whether the active theme supports the given feature. Default true.
  2769. * @param array $args Array of arguments for the feature.
  2770. * @param string $feature The theme feature.
  2771. */
  2772. return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  2773. }
  2774. /**
  2775. * Checks a theme's support for a given feature before loading the functions which implement it.
  2776. *
  2777. * @since 2.9.0
  2778. *
  2779. * @param string $feature The feature being checked. See add_theme_support() for the list
  2780. * of possible values.
  2781. * @param string $include Path to the file.
  2782. * @return bool True if the active theme supports the supplied feature, false otherwise.
  2783. */
  2784. function require_if_theme_supports( $feature, $include ) {
  2785. if ( current_theme_supports( $feature ) ) {
  2786. require $include;
  2787. return true;
  2788. }
  2789. return false;
  2790. }
  2791. /**
  2792. * Registers a theme feature for use in add_theme_support().
  2793. *
  2794. * This does not indicate that the active theme supports the feature, it only describes
  2795. * the feature's supported options.
  2796. *
  2797. * @since 5.5.0
  2798. *
  2799. * @see add_theme_support()
  2800. *
  2801. * @global array $_wp_registered_theme_features
  2802. *
  2803. * @param string $feature The name uniquely identifying the feature. See add_theme_support()
  2804. * for the list of possible values.
  2805. * @param array $args {
  2806. * Data used to describe the theme.
  2807. *
  2808. * @type string $type The type of data associated with this feature.
  2809. * Valid values are 'string', 'boolean', 'integer',
  2810. * 'number', 'array', and 'object'. Defaults to 'boolean'.
  2811. * @type bool $variadic Does this feature utilize the variadic support
  2812. * of add_theme_support(), or are all arguments specified
  2813. * as the second parameter. Must be used with the "array" type.
  2814. * @type string $description A short description of the feature. Included in
  2815. * the Themes REST API schema. Intended for developers.
  2816. * @type bool|array $show_in_rest {
  2817. * Whether this feature should be included in the Themes REST API endpoint.
  2818. * Defaults to not being included. When registering an 'array' or 'object' type,
  2819. * this argument must be an array with the 'schema' key.
  2820. *
  2821. * @type array $schema Specifies the JSON Schema definition describing
  2822. * the feature. If any objects in the schema do not include
  2823. * the 'additionalProperties' keyword, it is set to false.
  2824. * @type string $name An alternate name to be used as the property name
  2825. * in the REST API.
  2826. * @type callable $prepare_callback A function used to format the theme support in the REST API.
  2827. * Receives the raw theme support value.
  2828. * }
  2829. * }
  2830. * @return true|WP_Error True if the theme feature was successfully registered, a WP_Error object if not.
  2831. */
  2832. function register_theme_feature( $feature, $args = array() ) {
  2833. global $_wp_registered_theme_features;
  2834. if ( ! is_array( $_wp_registered_theme_features ) ) {
  2835. $_wp_registered_theme_features = array();
  2836. }
  2837. $defaults = array(
  2838. 'type' => 'boolean',
  2839. 'variadic' => false,
  2840. 'description' => '',
  2841. 'show_in_rest' => false,
  2842. );
  2843. $args = wp_parse_args( $args, $defaults );
  2844. if ( true === $args['show_in_rest'] ) {
  2845. $args['show_in_rest'] = array();
  2846. }
  2847. if ( is_array( $args['show_in_rest'] ) ) {
  2848. $args['show_in_rest'] = wp_parse_args(
  2849. $args['show_in_rest'],
  2850. array(
  2851. 'schema' => array(),
  2852. 'name' => $feature,
  2853. 'prepare_callback' => null,
  2854. )
  2855. );
  2856. }
  2857. if ( ! in_array( $args['type'], array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ), true ) ) {
  2858. return new WP_Error(
  2859. 'invalid_type',
  2860. __( 'The feature "type" is not valid JSON Schema type.' )
  2861. );
  2862. }
  2863. if ( true === $args['variadic'] && 'array' !== $args['type'] ) {
  2864. return new WP_Error(
  2865. 'variadic_must_be_array',
  2866. __( 'When registering a "variadic" theme feature, the "type" must be an "array".' )
  2867. );
  2868. }
  2869. if ( false !== $args['show_in_rest'] && in_array( $args['type'], array( 'array', 'object' ), true ) ) {
  2870. if ( ! is_array( $args['show_in_rest'] ) || empty( $args['show_in_rest']['schema'] ) ) {
  2871. return new WP_Error(
  2872. 'missing_schema',
  2873. __( 'When registering an "array" or "object" feature to show in the REST API, the feature\'s schema must also be defined.' )
  2874. );
  2875. }
  2876. if ( 'array' === $args['type'] && ! isset( $args['show_in_rest']['schema']['items'] ) ) {
  2877. return new WP_Error(
  2878. 'missing_schema_items',
  2879. __( 'When registering an "array" feature, the feature\'s schema must include the "items" keyword.' )
  2880. );
  2881. }
  2882. if ( 'object' === $args['type'] && ! isset( $args['show_in_rest']['schema']['properties'] ) ) {
  2883. return new WP_Error(
  2884. 'missing_schema_properties',
  2885. __( 'When registering an "object" feature, the feature\'s schema must include the "properties" keyword.' )
  2886. );
  2887. }
  2888. }
  2889. if ( is_array( $args['show_in_rest'] ) ) {
  2890. if ( isset( $args['show_in_rest']['prepare_callback'] )
  2891. && ! is_callable( $args['show_in_rest']['prepare_callback'] )
  2892. ) {
  2893. return new WP_Error(
  2894. 'invalid_rest_prepare_callback',
  2895. sprintf(
  2896. /* translators: %s: prepare_callback */
  2897. __( 'The "%s" must be a callable function.' ),
  2898. 'prepare_callback'
  2899. )
  2900. );
  2901. }
  2902. $args['show_in_rest']['schema'] = wp_parse_args(
  2903. $args['show_in_rest']['schema'],
  2904. array(
  2905. 'description' => $args['description'],
  2906. 'type' => $args['type'],
  2907. 'default' => false,
  2908. )
  2909. );
  2910. if ( is_bool( $args['show_in_rest']['schema']['default'] )
  2911. && ! in_array( 'boolean', (array) $args['show_in_rest']['schema']['type'], true )
  2912. ) {
  2913. // Automatically include the "boolean" type when the default value is a boolean.
  2914. $args['show_in_rest']['schema']['type'] = (array) $args['show_in_rest']['schema']['type'];
  2915. array_unshift( $args['show_in_rest']['schema']['type'], 'boolean' );
  2916. }
  2917. $args['show_in_rest']['schema'] = rest_default_additional_properties_to_false( $args['show_in_rest']['schema'] );
  2918. }
  2919. $_wp_registered_theme_features[ $feature ] = $args;
  2920. return true;
  2921. }
  2922. /**
  2923. * Gets the list of registered theme features.
  2924. *
  2925. * @since 5.5.0
  2926. *
  2927. * @global array $_wp_registered_theme_features
  2928. *
  2929. * @return array[] List of theme features, keyed by their name.
  2930. */
  2931. function get_registered_theme_features() {
  2932. global $_wp_registered_theme_features;
  2933. if ( ! is_array( $_wp_registered_theme_features ) ) {
  2934. return array();
  2935. }
  2936. return $_wp_registered_theme_features;
  2937. }
  2938. /**
  2939. * Gets the registration config for a theme feature.
  2940. *
  2941. * @since 5.5.0
  2942. *
  2943. * @global array $_wp_registered_theme_features
  2944. *
  2945. * @param string $feature The feature name. See add_theme_support() for the list
  2946. * of possible values.
  2947. * @return array|null The registration args, or null if the feature was not registered.
  2948. */
  2949. function get_registered_theme_feature( $feature ) {
  2950. global $_wp_registered_theme_features;
  2951. if ( ! is_array( $_wp_registered_theme_features ) ) {
  2952. return null;
  2953. }
  2954. return isset( $_wp_registered_theme_features[ $feature ] ) ? $_wp_registered_theme_features[ $feature ] : null;
  2955. }
  2956. /**
  2957. * Checks an attachment being deleted to see if it's a header or background image.
  2958. *
  2959. * If true it removes the theme modification which would be pointing at the deleted
  2960. * attachment.
  2961. *
  2962. * @access private
  2963. * @since 3.0.0
  2964. * @since 4.3.0 Also removes `header_image_data`.
  2965. * @since 4.5.0 Also removes custom logo theme mods.
  2966. *
  2967. * @param int $id The attachment ID.
  2968. */
  2969. function _delete_attachment_theme_mod( $id ) {
  2970. $attachment_image = wp_get_attachment_url( $id );
  2971. $header_image = get_header_image();
  2972. $background_image = get_background_image();
  2973. $custom_logo_id = get_theme_mod( 'custom_logo' );
  2974. if ( $custom_logo_id && $custom_logo_id == $id ) {
  2975. remove_theme_mod( 'custom_logo' );
  2976. remove_theme_mod( 'header_text' );
  2977. }
  2978. if ( $header_image && $header_image == $attachment_image ) {
  2979. remove_theme_mod( 'header_image' );
  2980. remove_theme_mod( 'header_image_data' );
  2981. }
  2982. if ( $background_image && $background_image == $attachment_image ) {
  2983. remove_theme_mod( 'background_image' );
  2984. }
  2985. }
  2986. /**
  2987. * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
  2988. *
  2989. * See {@see 'after_switch_theme'}.
  2990. *
  2991. * @since 3.3.0
  2992. */
  2993. function check_theme_switched() {
  2994. $stylesheet = get_option( 'theme_switched' );
  2995. if ( $stylesheet ) {
  2996. $old_theme = wp_get_theme( $stylesheet );
  2997. // Prevent widget & menu mapping from running since Customizer already called it up front.
  2998. if ( get_option( 'theme_switched_via_customizer' ) ) {
  2999. remove_action( 'after_switch_theme', '_wp_menus_changed' );
  3000. remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
  3001. update_option( 'theme_switched_via_customizer', false );
  3002. }
  3003. if ( $old_theme->exists() ) {
  3004. /**
  3005. * Fires on the first WP load after a theme switch if the old theme still exists.
  3006. *
  3007. * This action fires multiple times and the parameters differs
  3008. * according to the context, if the old theme exists or not.
  3009. * If the old theme is missing, the parameter will be the slug
  3010. * of the old theme.
  3011. *
  3012. * @since 3.3.0
  3013. *
  3014. * @param string $old_name Old theme name.
  3015. * @param WP_Theme $old_theme WP_Theme instance of the old theme.
  3016. */
  3017. do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
  3018. } else {
  3019. /** This action is documented in wp-includes/theme.php */
  3020. do_action( 'after_switch_theme', $stylesheet, $old_theme );
  3021. }
  3022. flush_rewrite_rules();
  3023. update_option( 'theme_switched', false );
  3024. }
  3025. }
  3026. /**
  3027. * Includes and instantiates the WP_Customize_Manager class.
  3028. *
  3029. * Loads the Customizer at plugins_loaded when accessing the customize.php admin
  3030. * page or when any request includes a wp_customize=on param or a customize_changeset
  3031. * param (a UUID). This param is a signal for whether to bootstrap the Customizer when
  3032. * WordPress is loading, especially in the Customizer preview
  3033. * or when making Customizer Ajax requests for widgets or menus.
  3034. *
  3035. * @since 3.4.0
  3036. *
  3037. * @global WP_Customize_Manager $wp_customize
  3038. */
  3039. function _wp_customize_include() {
  3040. $is_customize_admin_page = ( is_admin() && 'customize.php' === basename( $_SERVER['PHP_SELF'] ) );
  3041. $should_include = (
  3042. $is_customize_admin_page
  3043. ||
  3044. ( isset( $_REQUEST['wp_customize'] ) && 'on' === $_REQUEST['wp_customize'] )
  3045. ||
  3046. ( ! empty( $_GET['customize_changeset_uuid'] ) || ! empty( $_POST['customize_changeset_uuid'] ) )
  3047. );
  3048. if ( ! $should_include ) {
  3049. return;
  3050. }
  3051. /*
  3052. * Note that wp_unslash() is not being used on the input vars because it is
  3053. * called before wp_magic_quotes() gets called. Besides this fact, none of
  3054. * the values should contain any characters needing slashes anyway.
  3055. */
  3056. $keys = array(
  3057. 'changeset_uuid',
  3058. 'customize_changeset_uuid',
  3059. 'customize_theme',
  3060. 'theme',
  3061. 'customize_messenger_channel',
  3062. 'customize_autosaved',
  3063. );
  3064. $input_vars = array_merge(
  3065. wp_array_slice_assoc( $_GET, $keys ),
  3066. wp_array_slice_assoc( $_POST, $keys )
  3067. );
  3068. $theme = null;
  3069. $autosaved = null;
  3070. $messenger_channel = null;
  3071. // Value false indicates UUID should be determined after_setup_theme
  3072. // to either re-use existing saved changeset or else generate a new UUID if none exists.
  3073. $changeset_uuid = false;
  3074. // Set initially fo false since defaults to true for back-compat;
  3075. // can be overridden via the customize_changeset_branching filter.
  3076. $branching = false;
  3077. if ( $is_customize_admin_page && isset( $input_vars['changeset_uuid'] ) ) {
  3078. $changeset_uuid = sanitize_key( $input_vars['changeset_uuid'] );
  3079. } elseif ( ! empty( $input_vars['customize_changeset_uuid'] ) ) {
  3080. $changeset_uuid = sanitize_key( $input_vars['customize_changeset_uuid'] );
  3081. }
  3082. // Note that theme will be sanitized via WP_Theme.
  3083. if ( $is_customize_admin_page && isset( $input_vars['theme'] ) ) {
  3084. $theme = $input_vars['theme'];
  3085. } elseif ( isset( $input_vars['customize_theme'] ) ) {
  3086. $theme = $input_vars['customize_theme'];
  3087. }
  3088. if ( ! empty( $input_vars['customize_autosaved'] ) ) {
  3089. $autosaved = true;
  3090. }
  3091. if ( isset( $input_vars['customize_messenger_channel'] ) ) {
  3092. $messenger_channel = sanitize_key( $input_vars['customize_messenger_channel'] );
  3093. }
  3094. /*
  3095. * Note that settings must be previewed even outside the customizer preview
  3096. * and also in the customizer pane itself. This is to enable loading an existing
  3097. * changeset into the customizer. Previewing the settings only has to be prevented
  3098. * here in the case of a customize_save action because this will cause WP to think
  3099. * there is nothing changed that needs to be saved.
  3100. */
  3101. $is_customize_save_action = (
  3102. wp_doing_ajax()
  3103. &&
  3104. isset( $_REQUEST['action'] )
  3105. &&
  3106. 'customize_save' === wp_unslash( $_REQUEST['action'] )
  3107. );
  3108. $settings_previewed = ! $is_customize_save_action;
  3109. require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
  3110. $GLOBALS['wp_customize'] = new WP_Customize_Manager(
  3111. compact(
  3112. 'changeset_uuid',
  3113. 'theme',
  3114. 'messenger_channel',
  3115. 'settings_previewed',
  3116. 'autosaved',
  3117. 'branching'
  3118. )
  3119. );
  3120. }
  3121. /**
  3122. * Publishes a snapshot's changes.
  3123. *
  3124. * @since 4.7.0
  3125. * @access private
  3126. *
  3127. * @global wpdb $wpdb WordPress database abstraction object.
  3128. * @global WP_Customize_Manager $wp_customize Customizer instance.
  3129. *
  3130. * @param string $new_status New post status.
  3131. * @param string $old_status Old post status.
  3132. * @param WP_Post $changeset_post Changeset post object.
  3133. */
  3134. function _wp_customize_publish_changeset( $new_status, $old_status, $changeset_post ) {
  3135. global $wp_customize, $wpdb;
  3136. $is_publishing_changeset = (
  3137. 'customize_changeset' === $changeset_post->post_type
  3138. &&
  3139. 'publish' === $new_status
  3140. &&
  3141. 'publish' !== $old_status
  3142. );
  3143. if ( ! $is_publishing_changeset ) {
  3144. return;
  3145. }
  3146. if ( empty( $wp_customize ) ) {
  3147. require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
  3148. $wp_customize = new WP_Customize_Manager(
  3149. array(
  3150. 'changeset_uuid' => $changeset_post->post_name,
  3151. 'settings_previewed' => false,
  3152. )
  3153. );
  3154. }
  3155. if ( ! did_action( 'customize_register' ) ) {
  3156. /*
  3157. * When running from CLI or Cron, the customize_register action will need
  3158. * to be triggered in order for core, themes, and plugins to register their
  3159. * settings. Normally core will add_action( 'customize_register' ) at
  3160. * priority 10 to register the core settings, and if any themes/plugins
  3161. * also add_action( 'customize_register' ) at the same priority, they
  3162. * will have a $wp_customize with those settings registered since they
  3163. * call add_action() afterward, normally. However, when manually doing
  3164. * the customize_register action after the setup_theme, then the order
  3165. * will be reversed for two actions added at priority 10, resulting in
  3166. * the core settings no longer being available as expected to themes/plugins.
  3167. * So the following manually calls the method that registers the core
  3168. * settings up front before doing the action.
  3169. */
  3170. remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) );
  3171. $wp_customize->register_controls();
  3172. /** This filter is documented in /wp-includes/class-wp-customize-manager.php */
  3173. do_action( 'customize_register', $wp_customize );
  3174. }
  3175. $wp_customize->_publish_changeset_values( $changeset_post->ID );
  3176. /*
  3177. * Trash the changeset post if revisions are not enabled. Unpublished
  3178. * changesets by default get garbage collected due to the auto-draft status.
  3179. * When a changeset post is published, however, it would no longer get cleaned
  3180. * out. This is a problem when the changeset posts are never displayed anywhere,
  3181. * since they would just be endlessly piling up. So here we use the revisions
  3182. * feature to indicate whether or not a published changeset should get trashed
  3183. * and thus garbage collected.
  3184. */
  3185. if ( ! wp_revisions_enabled( $changeset_post ) ) {
  3186. $wp_customize->trash_changeset_post( $changeset_post->ID );
  3187. }
  3188. }
  3189. /**
  3190. * Filters changeset post data upon insert to ensure post_name is intact.
  3191. *
  3192. * This is needed to prevent the post_name from being dropped when the post is
  3193. * transitioned into pending status by a contributor.
  3194. *
  3195. * @since 4.7.0
  3196. *
  3197. * @see wp_insert_post()
  3198. *
  3199. * @param array $post_data An array of slashed post data.
  3200. * @param array $supplied_post_data An array of sanitized, but otherwise unmodified post data.
  3201. * @return array Filtered data.
  3202. */
  3203. function _wp_customize_changeset_filter_insert_post_data( $post_data, $supplied_post_data ) {
  3204. if ( isset( $post_data['post_type'] ) && 'customize_changeset' === $post_data['post_type'] ) {
  3205. // Prevent post_name from being dropped, such as when contributor saves a changeset post as pending.
  3206. if ( empty( $post_data['post_name'] ) && ! empty( $supplied_post_data['post_name'] ) ) {
  3207. $post_data['post_name'] = $supplied_post_data['post_name'];
  3208. }
  3209. }
  3210. return $post_data;
  3211. }
  3212. /**
  3213. * Adds settings for the customize-loader script.
  3214. *
  3215. * @since 3.4.0
  3216. */
  3217. function _wp_customize_loader_settings() {
  3218. $admin_origin = parse_url( admin_url() );
  3219. $home_origin = parse_url( home_url() );
  3220. $cross_domain = ( strtolower( $admin_origin['host'] ) != strtolower( $home_origin['host'] ) );
  3221. $browser = array(
  3222. 'mobile' => wp_is_mobile(),
  3223. 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
  3224. );
  3225. $settings = array(
  3226. 'url' => esc_url( admin_url( 'customize.php' ) ),
  3227. 'isCrossDomain' => $cross_domain,
  3228. 'browser' => $browser,
  3229. 'l10n' => array(
  3230. 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),
  3231. 'mainIframeTitle' => __( 'Customizer' ),
  3232. ),
  3233. );
  3234. $script = 'var _wpCustomizeLoaderSettings = ' . wp_json_encode( $settings ) . ';';
  3235. $wp_scripts = wp_scripts();
  3236. $data = $wp_scripts->get_data( 'customize-loader', 'data' );
  3237. if ( $data ) {
  3238. $script = "$data\n$script";
  3239. }
  3240. $wp_scripts->add_data( 'customize-loader', 'data', $script );
  3241. }
  3242. /**
  3243. * Returns a URL to load the Customizer.
  3244. *
  3245. * @since 3.4.0
  3246. *
  3247. * @param string $stylesheet Optional. Theme to customize. Defaults to active theme.
  3248. * The theme's stylesheet will be urlencoded if necessary.
  3249. * @return string
  3250. */
  3251. function wp_customize_url( $stylesheet = '' ) {
  3252. $url = admin_url( 'customize.php' );
  3253. if ( $stylesheet ) {
  3254. $url .= '?theme=' . urlencode( $stylesheet );
  3255. }
  3256. return esc_url( $url );
  3257. }
  3258. /**
  3259. * Prints a script to check whether or not the Customizer is supported,
  3260. * and apply either the no-customize-support or customize-support class
  3261. * to the body.
  3262. *
  3263. * This function MUST be called inside the body tag.
  3264. *
  3265. * Ideally, call this function immediately after the body tag is opened.
  3266. * This prevents a flash of unstyled content.
  3267. *
  3268. * It is also recommended that you add the "no-customize-support" class
  3269. * to the body tag by default.
  3270. *
  3271. * @since 3.4.0
  3272. * @since 4.7.0 Support for IE8 and below is explicitly removed via conditional comments.
  3273. * @since 5.5.0 IE8 and older are no longer supported.
  3274. */
  3275. function wp_customize_support_script() {
  3276. $admin_origin = parse_url( admin_url() );
  3277. $home_origin = parse_url( home_url() );
  3278. $cross_domain = ( strtolower( $admin_origin['host'] ) != strtolower( $home_origin['host'] ) );
  3279. $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  3280. ?>
  3281. <script<?php echo $type_attr; ?>>
  3282. (function() {
  3283. var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
  3284. <?php if ( $cross_domain ) : ?>
  3285. request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
  3286. <?php else : ?>
  3287. request = true;
  3288. <?php endif; ?>
  3289. b[c] = b[c].replace( rcs, ' ' );
  3290. // The customizer requires postMessage and CORS (if the site is cross domain).
  3291. b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
  3292. }());
  3293. </script>
  3294. <?php
  3295. }
  3296. /**
  3297. * Whether the site is being previewed in the Customizer.
  3298. *
  3299. * @since 4.0.0
  3300. *
  3301. * @global WP_Customize_Manager $wp_customize Customizer instance.
  3302. *
  3303. * @return bool True if the site is being previewed in the Customizer, false otherwise.
  3304. */
  3305. function is_customize_preview() {
  3306. global $wp_customize;
  3307. return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
  3308. }
  3309. /**
  3310. * Makes sure that auto-draft posts get their post_date bumped or status changed
  3311. * to draft to prevent premature garbage-collection.
  3312. *
  3313. * When a changeset is updated but remains an auto-draft, ensure the post_date
  3314. * for the auto-draft posts remains the same so that it will be
  3315. * garbage-collected at the same time by `wp_delete_auto_drafts()`. Otherwise,
  3316. * if the changeset is updated to be a draft then update the posts
  3317. * to have a far-future post_date so that they will never be garbage collected
  3318. * unless the changeset post itself is deleted.
  3319. *
  3320. * When a changeset is updated to be a persistent draft or to be scheduled for
  3321. * publishing, then transition any dependent auto-drafts to a draft status so
  3322. * that they likewise will not be garbage-collected but also so that they can
  3323. * be edited in the admin before publishing since there is not yet a post/page
  3324. * editing flow in the Customizer. See #39752.
  3325. *
  3326. * @link https://core.trac.wordpress.org/ticket/39752
  3327. *
  3328. * @since 4.8.0
  3329. * @access private
  3330. * @see wp_delete_auto_drafts()
  3331. *
  3332. * @global wpdb $wpdb WordPress database abstraction object.
  3333. *
  3334. * @param string $new_status Transition to this post status.
  3335. * @param string $old_status Previous post status.
  3336. * @param \WP_Post $post Post data.
  3337. */
  3338. function _wp_keep_alive_customize_changeset_dependent_auto_drafts( $new_status, $old_status, $post ) {
  3339. global $wpdb;
  3340. unset( $old_status );
  3341. // Short-circuit if not a changeset or if the changeset was published.
  3342. if ( 'customize_changeset' !== $post->post_type || 'publish' === $new_status ) {
  3343. return;
  3344. }
  3345. $data = json_decode( $post->post_content, true );
  3346. if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
  3347. return;
  3348. }
  3349. /*
  3350. * Actually, in lieu of keeping alive, trash any customization drafts here if the changeset itself is
  3351. * getting trashed. This is needed because when a changeset transitions to a draft, then any of the
  3352. * dependent auto-draft post/page stubs will also get transitioned to customization drafts which
  3353. * are then visible in the WP Admin. We cannot wait for the deletion of the changeset in which
  3354. * _wp_delete_customize_changeset_dependent_auto_drafts() will be called, since they need to be
  3355. * trashed to remove from visibility immediately.
  3356. */
  3357. if ( 'trash' === $new_status ) {
  3358. foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
  3359. if ( ! empty( $post_id ) && 'draft' === get_post_status( $post_id ) ) {
  3360. wp_trash_post( $post_id );
  3361. }
  3362. }
  3363. return;
  3364. }
  3365. $post_args = array();
  3366. if ( 'auto-draft' === $new_status ) {
  3367. /*
  3368. * Keep the post date for the post matching the changeset
  3369. * so that it will not be garbage-collected before the changeset.
  3370. */
  3371. $post_args['post_date'] = $post->post_date; // Note wp_delete_auto_drafts() only looks at this date.
  3372. } else {
  3373. /*
  3374. * Since the changeset no longer has an auto-draft (and it is not published)
  3375. * it is now a persistent changeset, a long-lived draft, and so any
  3376. * associated auto-draft posts should likewise transition into having a draft
  3377. * status. These drafts will be treated differently than regular drafts in
  3378. * that they will be tied to the given changeset. The publish meta box is
  3379. * replaced with a notice about how the post is part of a set of customized changes
  3380. * which will be published when the changeset is published.
  3381. */
  3382. $post_args['post_status'] = 'draft';
  3383. }
  3384. foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
  3385. if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) {
  3386. continue;
  3387. }
  3388. $wpdb->update(
  3389. $wpdb->posts,
  3390. $post_args,
  3391. array( 'ID' => $post_id )
  3392. );
  3393. clean_post_cache( $post_id );
  3394. }
  3395. }
  3396. /**
  3397. * Creates the initial theme features when the 'setup_theme' action is fired.
  3398. *
  3399. * See {@see 'setup_theme'}.
  3400. *
  3401. * @since 5.5.0
  3402. * @since 6.0.1 The `block-templates` feature was added.
  3403. */
  3404. function create_initial_theme_features() {
  3405. register_theme_feature(
  3406. 'align-wide',
  3407. array(
  3408. 'description' => __( 'Whether theme opts in to wide alignment CSS class.' ),
  3409. 'show_in_rest' => true,
  3410. )
  3411. );
  3412. register_theme_feature(
  3413. 'automatic-feed-links',
  3414. array(
  3415. 'description' => __( 'Whether posts and comments RSS feed links are added to head.' ),
  3416. 'show_in_rest' => true,
  3417. )
  3418. );
  3419. register_theme_feature(
  3420. 'block-templates',
  3421. array(
  3422. 'description' => __( 'Whether a theme uses block-based templates.' ),
  3423. 'show_in_rest' => true,
  3424. )
  3425. );
  3426. register_theme_feature(
  3427. 'block-template-parts',
  3428. array(
  3429. 'description' => __( 'Whether a theme uses block-based template parts.' ),
  3430. 'show_in_rest' => true,
  3431. )
  3432. );
  3433. register_theme_feature(
  3434. 'custom-background',
  3435. array(
  3436. 'description' => __( 'Custom background if defined by the theme.' ),
  3437. 'type' => 'object',
  3438. 'show_in_rest' => array(
  3439. 'schema' => array(
  3440. 'properties' => array(
  3441. 'default-image' => array(
  3442. 'type' => 'string',
  3443. 'format' => 'uri',
  3444. ),
  3445. 'default-preset' => array(
  3446. 'type' => 'string',
  3447. 'enum' => array(
  3448. 'default',
  3449. 'fill',
  3450. 'fit',
  3451. 'repeat',
  3452. 'custom',
  3453. ),
  3454. ),
  3455. 'default-position-x' => array(
  3456. 'type' => 'string',
  3457. 'enum' => array(
  3458. 'left',
  3459. 'center',
  3460. 'right',
  3461. ),
  3462. ),
  3463. 'default-position-y' => array(
  3464. 'type' => 'string',
  3465. 'enum' => array(
  3466. 'left',
  3467. 'center',
  3468. 'right',
  3469. ),
  3470. ),
  3471. 'default-size' => array(
  3472. 'type' => 'string',
  3473. 'enum' => array(
  3474. 'auto',
  3475. 'contain',
  3476. 'cover',
  3477. ),
  3478. ),
  3479. 'default-repeat' => array(
  3480. 'type' => 'string',
  3481. 'enum' => array(
  3482. 'repeat-x',
  3483. 'repeat-y',
  3484. 'repeat',
  3485. 'no-repeat',
  3486. ),
  3487. ),
  3488. 'default-attachment' => array(
  3489. 'type' => 'string',
  3490. 'enum' => array(
  3491. 'scroll',
  3492. 'fixed',
  3493. ),
  3494. ),
  3495. 'default-color' => array(
  3496. 'type' => 'string',
  3497. ),
  3498. ),
  3499. ),
  3500. ),
  3501. )
  3502. );
  3503. register_theme_feature(
  3504. 'custom-header',
  3505. array(
  3506. 'description' => __( 'Custom header if defined by the theme.' ),
  3507. 'type' => 'object',
  3508. 'show_in_rest' => array(
  3509. 'schema' => array(
  3510. 'properties' => array(
  3511. 'default-image' => array(
  3512. 'type' => 'string',
  3513. 'format' => 'uri',
  3514. ),
  3515. 'random-default' => array(
  3516. 'type' => 'boolean',
  3517. ),
  3518. 'width' => array(
  3519. 'type' => 'integer',
  3520. ),
  3521. 'height' => array(
  3522. 'type' => 'integer',
  3523. ),
  3524. 'flex-height' => array(
  3525. 'type' => 'boolean',
  3526. ),
  3527. 'flex-width' => array(
  3528. 'type' => 'boolean',
  3529. ),
  3530. 'default-text-color' => array(
  3531. 'type' => 'string',
  3532. ),
  3533. 'header-text' => array(
  3534. 'type' => 'boolean',
  3535. ),
  3536. 'uploads' => array(
  3537. 'type' => 'boolean',
  3538. ),
  3539. 'video' => array(
  3540. 'type' => 'boolean',
  3541. ),
  3542. ),
  3543. ),
  3544. ),
  3545. )
  3546. );
  3547. register_theme_feature(
  3548. 'custom-logo',
  3549. array(
  3550. 'type' => 'object',
  3551. 'description' => __( 'Custom logo if defined by the theme.' ),
  3552. 'show_in_rest' => array(
  3553. 'schema' => array(
  3554. 'properties' => array(
  3555. 'width' => array(
  3556. 'type' => 'integer',
  3557. ),
  3558. 'height' => array(
  3559. 'type' => 'integer',
  3560. ),
  3561. 'flex-width' => array(
  3562. 'type' => 'boolean',
  3563. ),
  3564. 'flex-height' => array(
  3565. 'type' => 'boolean',
  3566. ),
  3567. 'header-text' => array(
  3568. 'type' => 'array',
  3569. 'items' => array(
  3570. 'type' => 'string',
  3571. ),
  3572. ),
  3573. 'unlink-homepage-logo' => array(
  3574. 'type' => 'boolean',
  3575. ),
  3576. ),
  3577. ),
  3578. ),
  3579. )
  3580. );
  3581. register_theme_feature(
  3582. 'customize-selective-refresh-widgets',
  3583. array(
  3584. 'description' => __( 'Whether the theme enables Selective Refresh for Widgets being managed with the Customizer.' ),
  3585. 'show_in_rest' => true,
  3586. )
  3587. );
  3588. register_theme_feature(
  3589. 'dark-editor-style',
  3590. array(
  3591. 'description' => __( 'Whether theme opts in to the dark editor style UI.' ),
  3592. 'show_in_rest' => true,
  3593. )
  3594. );
  3595. register_theme_feature(
  3596. 'disable-custom-colors',
  3597. array(
  3598. 'description' => __( 'Whether the theme disables custom colors.' ),
  3599. 'show_in_rest' => true,
  3600. )
  3601. );
  3602. register_theme_feature(
  3603. 'disable-custom-font-sizes',
  3604. array(
  3605. 'description' => __( 'Whether the theme disables custom font sizes.' ),
  3606. 'show_in_rest' => true,
  3607. )
  3608. );
  3609. register_theme_feature(
  3610. 'disable-custom-gradients',
  3611. array(
  3612. 'description' => __( 'Whether the theme disables custom gradients.' ),
  3613. 'show_in_rest' => true,
  3614. )
  3615. );
  3616. register_theme_feature(
  3617. 'disable-layout-styles',
  3618. array(
  3619. 'description' => __( 'Whether the theme disables generated layout styles.' ),
  3620. 'show_in_rest' => true,
  3621. )
  3622. );
  3623. register_theme_feature(
  3624. 'editor-color-palette',
  3625. array(
  3626. 'type' => 'array',
  3627. 'description' => __( 'Custom color palette if defined by the theme.' ),
  3628. 'show_in_rest' => array(
  3629. 'schema' => array(
  3630. 'items' => array(
  3631. 'type' => 'object',
  3632. 'properties' => array(
  3633. 'name' => array(
  3634. 'type' => 'string',
  3635. ),
  3636. 'slug' => array(
  3637. 'type' => 'string',
  3638. ),
  3639. 'color' => array(
  3640. 'type' => 'string',
  3641. ),
  3642. ),
  3643. ),
  3644. ),
  3645. ),
  3646. )
  3647. );
  3648. register_theme_feature(
  3649. 'editor-font-sizes',
  3650. array(
  3651. 'type' => 'array',
  3652. 'description' => __( 'Custom font sizes if defined by the theme.' ),
  3653. 'show_in_rest' => array(
  3654. 'schema' => array(
  3655. 'items' => array(
  3656. 'type' => 'object',
  3657. 'properties' => array(
  3658. 'name' => array(
  3659. 'type' => 'string',
  3660. ),
  3661. 'size' => array(
  3662. 'type' => 'number',
  3663. ),
  3664. 'slug' => array(
  3665. 'type' => 'string',
  3666. ),
  3667. ),
  3668. ),
  3669. ),
  3670. ),
  3671. )
  3672. );
  3673. register_theme_feature(
  3674. 'editor-gradient-presets',
  3675. array(
  3676. 'type' => 'array',
  3677. 'description' => __( 'Custom gradient presets if defined by the theme.' ),
  3678. 'show_in_rest' => array(
  3679. 'schema' => array(
  3680. 'items' => array(
  3681. 'type' => 'object',
  3682. 'properties' => array(
  3683. 'name' => array(
  3684. 'type' => 'string',
  3685. ),
  3686. 'gradient' => array(
  3687. 'type' => 'string',
  3688. ),
  3689. 'slug' => array(
  3690. 'type' => 'string',
  3691. ),
  3692. ),
  3693. ),
  3694. ),
  3695. ),
  3696. )
  3697. );
  3698. register_theme_feature(
  3699. 'editor-styles',
  3700. array(
  3701. 'description' => __( 'Whether theme opts in to the editor styles CSS wrapper.' ),
  3702. 'show_in_rest' => true,
  3703. )
  3704. );
  3705. register_theme_feature(
  3706. 'html5',
  3707. array(
  3708. 'type' => 'array',
  3709. 'description' => __( 'Allows use of HTML5 markup for search forms, comment forms, comment lists, gallery, and caption.' ),
  3710. 'show_in_rest' => array(
  3711. 'schema' => array(
  3712. 'items' => array(
  3713. 'type' => 'string',
  3714. 'enum' => array(
  3715. 'search-form',
  3716. 'comment-form',
  3717. 'comment-list',
  3718. 'gallery',
  3719. 'caption',
  3720. 'script',
  3721. 'style',
  3722. ),
  3723. ),
  3724. ),
  3725. ),
  3726. )
  3727. );
  3728. register_theme_feature(
  3729. 'post-formats',
  3730. array(
  3731. 'type' => 'array',
  3732. 'description' => __( 'Post formats supported.' ),
  3733. 'show_in_rest' => array(
  3734. 'name' => 'formats',
  3735. 'schema' => array(
  3736. 'items' => array(
  3737. 'type' => 'string',
  3738. 'enum' => get_post_format_slugs(),
  3739. ),
  3740. 'default' => array( 'standard' ),
  3741. ),
  3742. 'prepare_callback' => static function ( $formats ) {
  3743. $formats = is_array( $formats ) ? array_values( $formats[0] ) : array();
  3744. $formats = array_merge( array( 'standard' ), $formats );
  3745. return $formats;
  3746. },
  3747. ),
  3748. )
  3749. );
  3750. register_theme_feature(
  3751. 'post-thumbnails',
  3752. array(
  3753. 'type' => 'array',
  3754. 'description' => __( 'The post types that support thumbnails or true if all post types are supported.' ),
  3755. 'show_in_rest' => array(
  3756. 'type' => array( 'boolean', 'array' ),
  3757. 'schema' => array(
  3758. 'items' => array(
  3759. 'type' => 'string',
  3760. ),
  3761. ),
  3762. ),
  3763. )
  3764. );
  3765. register_theme_feature(
  3766. 'responsive-embeds',
  3767. array(
  3768. 'description' => __( 'Whether the theme supports responsive embedded content.' ),
  3769. 'show_in_rest' => true,
  3770. )
  3771. );
  3772. register_theme_feature(
  3773. 'title-tag',
  3774. array(
  3775. 'description' => __( 'Whether the theme can manage the document title tag.' ),
  3776. 'show_in_rest' => true,
  3777. )
  3778. );
  3779. register_theme_feature(
  3780. 'wp-block-styles',
  3781. array(
  3782. 'description' => __( 'Whether theme opts in to default WordPress block styles for viewing.' ),
  3783. 'show_in_rest' => true,
  3784. )
  3785. );
  3786. }
  3787. /**
  3788. * Returns whether the active theme is a block-based theme or not.
  3789. *
  3790. * @since 5.9.0
  3791. *
  3792. * @return boolean Whether the active theme is a block-based theme or not.
  3793. */
  3794. function wp_is_block_theme() {
  3795. return wp_get_theme()->is_block_theme();
  3796. }
  3797. /**
  3798. * Given an element name, returns a class name.
  3799. *
  3800. * Alias of WP_Theme_JSON::get_element_class_name.
  3801. *
  3802. * @since 6.1.0
  3803. *
  3804. * @param string $element The name of the element.
  3805. *
  3806. * @return string The name of the class.
  3807. */
  3808. function wp_theme_get_element_class_name( $element ) {
  3809. return WP_Theme_JSON::get_element_class_name( $element );
  3810. }
  3811. /**
  3812. * Adds default theme supports for block themes when the 'setup_theme' action fires.
  3813. *
  3814. * See {@see 'setup_theme'}.
  3815. *
  3816. * @since 5.9.0
  3817. * @access private
  3818. */
  3819. function _add_default_theme_supports() {
  3820. if ( ! wp_is_block_theme() ) {
  3821. return;
  3822. }
  3823. add_theme_support( 'post-thumbnails' );
  3824. add_theme_support( 'responsive-embeds' );
  3825. add_theme_support( 'editor-styles' );
  3826. /*
  3827. * Makes block themes support HTML5 by default for the comment block and search form
  3828. * (which use default template functions) and `[caption]` and `[gallery]` shortcodes.
  3829. * Other blocks contain their own HTML5 markup.
  3830. */
  3831. add_theme_support( 'html5', array( 'comment-form', 'comment-list', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
  3832. add_theme_support( 'automatic-feed-links' );
  3833. add_filter( 'should_load_separate_core_block_assets', '__return_true' );
  3834. /*
  3835. * Remove the Customizer's Menus panel when block theme is active.
  3836. */
  3837. add_filter(
  3838. 'customize_panel_active',
  3839. static function ( $active, WP_Customize_Panel $panel ) {
  3840. if (
  3841. 'nav_menus' === $panel->id &&
  3842. ! current_theme_supports( 'menus' ) &&
  3843. ! current_theme_supports( 'widgets' )
  3844. ) {
  3845. $active = false;
  3846. }
  3847. return $active;
  3848. },
  3849. 10,
  3850. 2
  3851. );
  3852. }