Brak opisu

theme.php 125KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206
  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. return esc_url_raw( set_url_scheme( $url ) );
  1057. }
  1058. /**
  1059. * Creates image tag markup for a custom header image.
  1060. *
  1061. * @since 4.4.0
  1062. *
  1063. * @param array $attr Optional. Additional attributes for the image tag. Can be used
  1064. * to override the default attributes. Default empty.
  1065. * @return string HTML image element markup or empty string on failure.
  1066. */
  1067. function get_header_image_tag( $attr = array() ) {
  1068. $header = get_custom_header();
  1069. $header->url = get_header_image();
  1070. if ( ! $header->url ) {
  1071. return '';
  1072. }
  1073. $width = absint( $header->width );
  1074. $height = absint( $header->height );
  1075. $alt = '';
  1076. // Use alternative text assigned to the image, if available. Otherwise, leave it empty.
  1077. if ( ! empty( $header->attachment_id ) ) {
  1078. $image_alt = get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true );
  1079. if ( is_string( $image_alt ) ) {
  1080. $alt = $image_alt;
  1081. }
  1082. }
  1083. $attr = wp_parse_args(
  1084. $attr,
  1085. array(
  1086. 'src' => $header->url,
  1087. 'width' => $width,
  1088. 'height' => $height,
  1089. 'alt' => $alt,
  1090. )
  1091. );
  1092. // Generate 'srcset' and 'sizes' if not already present.
  1093. if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) {
  1094. $image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true );
  1095. $size_array = array( $width, $height );
  1096. if ( is_array( $image_meta ) ) {
  1097. $srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id );
  1098. if ( ! empty( $attr['sizes'] ) ) {
  1099. $sizes = $attr['sizes'];
  1100. } else {
  1101. $sizes = wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );
  1102. }
  1103. if ( $srcset && $sizes ) {
  1104. $attr['srcset'] = $srcset;
  1105. $attr['sizes'] = $sizes;
  1106. }
  1107. }
  1108. }
  1109. /**
  1110. * Filters the list of header image attributes.
  1111. *
  1112. * @since 5.9.0
  1113. *
  1114. * @param array $attr Array of the attributes for the image tag.
  1115. * @param object $header The custom header object returned by 'get_custom_header()'.
  1116. */
  1117. $attr = apply_filters( 'get_header_image_tag_attributes', $attr, $header );
  1118. $attr = array_map( 'esc_attr', $attr );
  1119. $html = '<img';
  1120. foreach ( $attr as $name => $value ) {
  1121. $html .= ' ' . $name . '="' . $value . '"';
  1122. }
  1123. $html .= ' />';
  1124. /**
  1125. * Filters the markup of header images.
  1126. *
  1127. * @since 4.4.0
  1128. *
  1129. * @param string $html The HTML image tag markup being filtered.
  1130. * @param object $header The custom header object returned by 'get_custom_header()'.
  1131. * @param array $attr Array of the attributes for the image tag.
  1132. */
  1133. return apply_filters( 'get_header_image_tag', $html, $header, $attr );
  1134. }
  1135. /**
  1136. * Displays the image markup for a custom header image.
  1137. *
  1138. * @since 4.4.0
  1139. *
  1140. * @param array $attr Optional. Attributes for the image markup. Default empty.
  1141. */
  1142. function the_header_image_tag( $attr = array() ) {
  1143. echo get_header_image_tag( $attr );
  1144. }
  1145. /**
  1146. * Gets random header image data from registered images in theme.
  1147. *
  1148. * @since 3.4.0
  1149. *
  1150. * @access private
  1151. *
  1152. * @global array $_wp_default_headers
  1153. *
  1154. * @return object
  1155. */
  1156. function _get_random_header_data() {
  1157. global $_wp_default_headers;
  1158. static $_wp_random_header = null;
  1159. if ( empty( $_wp_random_header ) ) {
  1160. $header_image_mod = get_theme_mod( 'header_image', '' );
  1161. $headers = array();
  1162. if ( 'random-uploaded-image' === $header_image_mod ) {
  1163. $headers = get_uploaded_header_images();
  1164. } elseif ( ! empty( $_wp_default_headers ) ) {
  1165. if ( 'random-default-image' === $header_image_mod ) {
  1166. $headers = $_wp_default_headers;
  1167. } else {
  1168. if ( current_theme_supports( 'custom-header', 'random-default' ) ) {
  1169. $headers = $_wp_default_headers;
  1170. }
  1171. }
  1172. }
  1173. if ( empty( $headers ) ) {
  1174. return new stdClass;
  1175. }
  1176. $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
  1177. $_wp_random_header->url = sprintf(
  1178. $_wp_random_header->url,
  1179. get_template_directory_uri(),
  1180. get_stylesheet_directory_uri()
  1181. );
  1182. $_wp_random_header->thumbnail_url = sprintf(
  1183. $_wp_random_header->thumbnail_url,
  1184. get_template_directory_uri(),
  1185. get_stylesheet_directory_uri()
  1186. );
  1187. }
  1188. return $_wp_random_header;
  1189. }
  1190. /**
  1191. * Gets random header image URL from registered images in theme.
  1192. *
  1193. * @since 3.2.0
  1194. *
  1195. * @return string Path to header image.
  1196. */
  1197. function get_random_header_image() {
  1198. $random_image = _get_random_header_data();
  1199. if ( empty( $random_image->url ) ) {
  1200. return '';
  1201. }
  1202. return $random_image->url;
  1203. }
  1204. /**
  1205. * Checks if random header image is in use.
  1206. *
  1207. * Always true if user expressly chooses the option in Appearance > Header.
  1208. * Also true if theme has multiple header images registered, no specific header image
  1209. * is chosen, and theme turns on random headers with add_theme_support().
  1210. *
  1211. * @since 3.2.0
  1212. *
  1213. * @param string $type The random pool to use. Possible values include 'any',
  1214. * 'default', 'uploaded'. Default 'any'.
  1215. * @return bool
  1216. */
  1217. function is_random_header_image( $type = 'any' ) {
  1218. $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
  1219. if ( 'any' === $type ) {
  1220. if ( 'random-default-image' === $header_image_mod
  1221. || 'random-uploaded-image' === $header_image_mod
  1222. || ( '' !== get_random_header_image() && empty( $header_image_mod ) )
  1223. ) {
  1224. return true;
  1225. }
  1226. } else {
  1227. if ( "random-$type-image" === $header_image_mod ) {
  1228. return true;
  1229. } elseif ( 'default' === $type && empty( $header_image_mod ) && '' !== get_random_header_image() ) {
  1230. return true;
  1231. }
  1232. }
  1233. return false;
  1234. }
  1235. /**
  1236. * Displays header image URL.
  1237. *
  1238. * @since 2.1.0
  1239. */
  1240. function header_image() {
  1241. $image = get_header_image();
  1242. if ( $image ) {
  1243. echo esc_url( $image );
  1244. }
  1245. }
  1246. /**
  1247. * Gets the header images uploaded for the active theme.
  1248. *
  1249. * @since 3.2.0
  1250. *
  1251. * @return array
  1252. */
  1253. function get_uploaded_header_images() {
  1254. $header_images = array();
  1255. // @todo Caching.
  1256. $headers = get_posts(
  1257. array(
  1258. 'post_type' => 'attachment',
  1259. 'meta_key' => '_wp_attachment_is_custom_header',
  1260. 'meta_value' => get_option( 'stylesheet' ),
  1261. 'orderby' => 'none',
  1262. 'nopaging' => true,
  1263. )
  1264. );
  1265. if ( empty( $headers ) ) {
  1266. return array();
  1267. }
  1268. foreach ( (array) $headers as $header ) {
  1269. $url = esc_url_raw( wp_get_attachment_url( $header->ID ) );
  1270. $header_data = wp_get_attachment_metadata( $header->ID );
  1271. $header_index = $header->ID;
  1272. $header_images[ $header_index ] = array();
  1273. $header_images[ $header_index ]['attachment_id'] = $header->ID;
  1274. $header_images[ $header_index ]['url'] = $url;
  1275. $header_images[ $header_index ]['thumbnail_url'] = $url;
  1276. $header_images[ $header_index ]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
  1277. if ( isset( $header_data['attachment_parent'] ) ) {
  1278. $header_images[ $header_index ]['attachment_parent'] = $header_data['attachment_parent'];
  1279. } else {
  1280. $header_images[ $header_index ]['attachment_parent'] = '';
  1281. }
  1282. if ( isset( $header_data['width'] ) ) {
  1283. $header_images[ $header_index ]['width'] = $header_data['width'];
  1284. }
  1285. if ( isset( $header_data['height'] ) ) {
  1286. $header_images[ $header_index ]['height'] = $header_data['height'];
  1287. }
  1288. }
  1289. return $header_images;
  1290. }
  1291. /**
  1292. * Gets the header image data.
  1293. *
  1294. * @since 3.4.0
  1295. *
  1296. * @global array $_wp_default_headers
  1297. *
  1298. * @return object
  1299. */
  1300. function get_custom_header() {
  1301. global $_wp_default_headers;
  1302. if ( is_random_header_image() ) {
  1303. $data = _get_random_header_data();
  1304. } else {
  1305. $data = get_theme_mod( 'header_image_data' );
  1306. if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
  1307. $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
  1308. $data = array();
  1309. $data['url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
  1310. $data['thumbnail_url'] = $data['url'];
  1311. if ( ! empty( $_wp_default_headers ) ) {
  1312. foreach ( (array) $_wp_default_headers as $default_header ) {
  1313. $url = vsprintf( $default_header['url'], $directory_args );
  1314. if ( $data['url'] == $url ) {
  1315. $data = $default_header;
  1316. $data['url'] = $url;
  1317. $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
  1318. break;
  1319. }
  1320. }
  1321. }
  1322. }
  1323. }
  1324. $default = array(
  1325. 'url' => '',
  1326. 'thumbnail_url' => '',
  1327. 'width' => get_theme_support( 'custom-header', 'width' ),
  1328. 'height' => get_theme_support( 'custom-header', 'height' ),
  1329. 'video' => get_theme_support( 'custom-header', 'video' ),
  1330. );
  1331. return (object) wp_parse_args( $data, $default );
  1332. }
  1333. /**
  1334. * Registers a selection of default headers to be displayed by the custom header admin UI.
  1335. *
  1336. * @since 3.0.0
  1337. *
  1338. * @global array $_wp_default_headers
  1339. *
  1340. * @param array $headers Array of headers keyed by a string ID. The IDs point to arrays
  1341. * containing 'url', 'thumbnail_url', and 'description' keys.
  1342. */
  1343. function register_default_headers( $headers ) {
  1344. global $_wp_default_headers;
  1345. $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
  1346. }
  1347. /**
  1348. * Unregisters default headers.
  1349. *
  1350. * This function must be called after register_default_headers() has already added the
  1351. * header you want to remove.
  1352. *
  1353. * @see register_default_headers()
  1354. * @since 3.0.0
  1355. *
  1356. * @global array $_wp_default_headers
  1357. *
  1358. * @param string|array $header The header string id (key of array) to remove, or an array thereof.
  1359. * @return bool|void A single header returns true on success, false on failure.
  1360. * There is currently no return value for multiple headers.
  1361. */
  1362. function unregister_default_headers( $header ) {
  1363. global $_wp_default_headers;
  1364. if ( is_array( $header ) ) {
  1365. array_map( 'unregister_default_headers', $header );
  1366. } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
  1367. unset( $_wp_default_headers[ $header ] );
  1368. return true;
  1369. } else {
  1370. return false;
  1371. }
  1372. }
  1373. /**
  1374. * Checks whether a header video is set or not.
  1375. *
  1376. * @since 4.7.0
  1377. *
  1378. * @see get_header_video_url()
  1379. *
  1380. * @return bool Whether a header video is set or not.
  1381. */
  1382. function has_header_video() {
  1383. return (bool) get_header_video_url();
  1384. }
  1385. /**
  1386. * Retrieves header video URL for custom header.
  1387. *
  1388. * Uses a local video if present, or falls back to an external video.
  1389. *
  1390. * @since 4.7.0
  1391. *
  1392. * @return string|false Header video URL or false if there is no video.
  1393. */
  1394. function get_header_video_url() {
  1395. $id = absint( get_theme_mod( 'header_video' ) );
  1396. if ( $id ) {
  1397. // Get the file URL from the attachment ID.
  1398. $url = wp_get_attachment_url( $id );
  1399. } else {
  1400. $url = get_theme_mod( 'external_header_video' );
  1401. }
  1402. /**
  1403. * Filters the header video URL.
  1404. *
  1405. * @since 4.7.3
  1406. *
  1407. * @param string $url Header video URL, if available.
  1408. */
  1409. $url = apply_filters( 'get_header_video_url', $url );
  1410. if ( ! $id && ! $url ) {
  1411. return false;
  1412. }
  1413. return esc_url_raw( set_url_scheme( $url ) );
  1414. }
  1415. /**
  1416. * Displays header video URL.
  1417. *
  1418. * @since 4.7.0
  1419. */
  1420. function the_header_video_url() {
  1421. $video = get_header_video_url();
  1422. if ( $video ) {
  1423. echo esc_url( $video );
  1424. }
  1425. }
  1426. /**
  1427. * Retrieves header video settings.
  1428. *
  1429. * @since 4.7.0
  1430. *
  1431. * @return array
  1432. */
  1433. function get_header_video_settings() {
  1434. $header = get_custom_header();
  1435. $video_url = get_header_video_url();
  1436. $video_type = wp_check_filetype( $video_url, wp_get_mime_types() );
  1437. $settings = array(
  1438. 'mimeType' => '',
  1439. 'posterUrl' => get_header_image(),
  1440. 'videoUrl' => $video_url,
  1441. 'width' => absint( $header->width ),
  1442. 'height' => absint( $header->height ),
  1443. 'minWidth' => 900,
  1444. 'minHeight' => 500,
  1445. 'l10n' => array(
  1446. 'pause' => __( 'Pause' ),
  1447. 'play' => __( 'Play' ),
  1448. 'pauseSpeak' => __( 'Video is paused.' ),
  1449. 'playSpeak' => __( 'Video is playing.' ),
  1450. ),
  1451. );
  1452. if ( preg_match( '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#', $video_url ) ) {
  1453. $settings['mimeType'] = 'video/x-youtube';
  1454. } elseif ( ! empty( $video_type['type'] ) ) {
  1455. $settings['mimeType'] = $video_type['type'];
  1456. }
  1457. /**
  1458. * Filters header video settings.
  1459. *
  1460. * @since 4.7.0
  1461. *
  1462. * @param array $settings An array of header video settings.
  1463. */
  1464. return apply_filters( 'header_video_settings', $settings );
  1465. }
  1466. /**
  1467. * Checks whether a custom header is set or not.
  1468. *
  1469. * @since 4.7.0
  1470. *
  1471. * @return bool True if a custom header is set. False if not.
  1472. */
  1473. function has_custom_header() {
  1474. if ( has_header_image() || ( has_header_video() && is_header_video_active() ) ) {
  1475. return true;
  1476. }
  1477. return false;
  1478. }
  1479. /**
  1480. * Checks whether the custom header video is eligible to show on the current page.
  1481. *
  1482. * @since 4.7.0
  1483. *
  1484. * @return bool True if the custom header video should be shown. False if not.
  1485. */
  1486. function is_header_video_active() {
  1487. if ( ! get_theme_support( 'custom-header', 'video' ) ) {
  1488. return false;
  1489. }
  1490. $video_active_cb = get_theme_support( 'custom-header', 'video-active-callback' );
  1491. if ( empty( $video_active_cb ) || ! is_callable( $video_active_cb ) ) {
  1492. $show_video = true;
  1493. } else {
  1494. $show_video = call_user_func( $video_active_cb );
  1495. }
  1496. /**
  1497. * Filters whether the custom header video is eligible to show on the current page.
  1498. *
  1499. * @since 4.7.0
  1500. *
  1501. * @param bool $show_video Whether the custom header video should be shown. Returns the value
  1502. * of the theme setting for the `custom-header`'s `video-active-callback`.
  1503. * If no callback is set, the default value is that of `is_front_page()`.
  1504. */
  1505. return apply_filters( 'is_header_video_active', $show_video );
  1506. }
  1507. /**
  1508. * Retrieves the markup for a custom header.
  1509. *
  1510. * The container div will always be returned in the Customizer preview.
  1511. *
  1512. * @since 4.7.0
  1513. *
  1514. * @return string The markup for a custom header on success.
  1515. */
  1516. function get_custom_header_markup() {
  1517. if ( ! has_custom_header() && ! is_customize_preview() ) {
  1518. return '';
  1519. }
  1520. return sprintf(
  1521. '<div id="wp-custom-header" class="wp-custom-header">%s</div>',
  1522. get_header_image_tag()
  1523. );
  1524. }
  1525. /**
  1526. * Prints the markup for a custom header.
  1527. *
  1528. * A container div will always be printed in the Customizer preview.
  1529. *
  1530. * @since 4.7.0
  1531. */
  1532. function the_custom_header_markup() {
  1533. $custom_header = get_custom_header_markup();
  1534. if ( empty( $custom_header ) ) {
  1535. return;
  1536. }
  1537. echo $custom_header;
  1538. if ( is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) {
  1539. wp_enqueue_script( 'wp-custom-header' );
  1540. wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings() );
  1541. }
  1542. }
  1543. /**
  1544. * Retrieves background image for custom background.
  1545. *
  1546. * @since 3.0.0
  1547. *
  1548. * @return string
  1549. */
  1550. function get_background_image() {
  1551. return get_theme_mod( 'background_image', get_theme_support( 'custom-background', 'default-image' ) );
  1552. }
  1553. /**
  1554. * Displays background image path.
  1555. *
  1556. * @since 3.0.0
  1557. */
  1558. function background_image() {
  1559. echo get_background_image();
  1560. }
  1561. /**
  1562. * Retrieves value for custom background color.
  1563. *
  1564. * @since 3.0.0
  1565. *
  1566. * @return string
  1567. */
  1568. function get_background_color() {
  1569. return get_theme_mod( 'background_color', get_theme_support( 'custom-background', 'default-color' ) );
  1570. }
  1571. /**
  1572. * Displays background color value.
  1573. *
  1574. * @since 3.0.0
  1575. */
  1576. function background_color() {
  1577. echo get_background_color();
  1578. }
  1579. /**
  1580. * Default custom background callback.
  1581. *
  1582. * @since 3.0.0
  1583. */
  1584. function _custom_background_cb() {
  1585. // $background is the saved custom image, or the default image.
  1586. $background = set_url_scheme( get_background_image() );
  1587. // $color is the saved custom color.
  1588. // A default has to be specified in style.css. It will not be printed here.
  1589. $color = get_background_color();
  1590. if ( get_theme_support( 'custom-background', 'default-color' ) === $color ) {
  1591. $color = false;
  1592. }
  1593. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  1594. if ( ! $background && ! $color ) {
  1595. if ( is_customize_preview() ) {
  1596. printf( '<style%s id="custom-background-css"></style>', $type_attr );
  1597. }
  1598. return;
  1599. }
  1600. $style = $color ? "background-color: #$color;" : '';
  1601. if ( $background ) {
  1602. $image = ' background-image: url("' . esc_url_raw( $background ) . '");';
  1603. // Background Position.
  1604. $position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
  1605. $position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
  1606. if ( ! in_array( $position_x, array( 'left', 'center', 'right' ), true ) ) {
  1607. $position_x = 'left';
  1608. }
  1609. if ( ! in_array( $position_y, array( 'top', 'center', 'bottom' ), true ) ) {
  1610. $position_y = 'top';
  1611. }
  1612. $position = " background-position: $position_x $position_y;";
  1613. // Background Size.
  1614. $size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
  1615. if ( ! in_array( $size, array( 'auto', 'contain', 'cover' ), true ) ) {
  1616. $size = 'auto';
  1617. }
  1618. $size = " background-size: $size;";
  1619. // Background Repeat.
  1620. $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
  1621. if ( ! in_array( $repeat, array( 'repeat-x', 'repeat-y', 'repeat', 'no-repeat' ), true ) ) {
  1622. $repeat = 'repeat';
  1623. }
  1624. $repeat = " background-repeat: $repeat;";
  1625. // Background Scroll.
  1626. $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
  1627. if ( 'fixed' !== $attachment ) {
  1628. $attachment = 'scroll';
  1629. }
  1630. $attachment = " background-attachment: $attachment;";
  1631. $style .= $image . $position . $size . $repeat . $attachment;
  1632. }
  1633. ?>
  1634. <style<?php echo $type_attr; ?> id="custom-background-css">
  1635. body.custom-background { <?php echo trim( $style ); ?> }
  1636. </style>
  1637. <?php
  1638. }
  1639. /**
  1640. * Renders the Custom CSS style element.
  1641. *
  1642. * @since 4.7.0
  1643. */
  1644. function wp_custom_css_cb() {
  1645. $styles = wp_get_custom_css();
  1646. if ( $styles || is_customize_preview() ) :
  1647. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  1648. ?>
  1649. <style<?php echo $type_attr; ?> id="wp-custom-css">
  1650. <?php
  1651. // Note that esc_html() cannot be used because `div &gt; span` is not interpreted properly.
  1652. echo strip_tags( $styles );
  1653. ?>
  1654. </style>
  1655. <?php
  1656. endif;
  1657. }
  1658. /**
  1659. * Fetches the `custom_css` post for a given theme.
  1660. *
  1661. * @since 4.7.0
  1662. *
  1663. * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the active theme.
  1664. * @return WP_Post|null The custom_css post or null if none exists.
  1665. */
  1666. function wp_get_custom_css_post( $stylesheet = '' ) {
  1667. if ( empty( $stylesheet ) ) {
  1668. $stylesheet = get_stylesheet();
  1669. }
  1670. $custom_css_query_vars = array(
  1671. 'post_type' => 'custom_css',
  1672. 'post_status' => get_post_stati(),
  1673. 'name' => sanitize_title( $stylesheet ),
  1674. 'posts_per_page' => 1,
  1675. 'no_found_rows' => true,
  1676. 'cache_results' => true,
  1677. 'update_post_meta_cache' => false,
  1678. 'update_post_term_cache' => false,
  1679. 'lazy_load_term_meta' => false,
  1680. );
  1681. $post = null;
  1682. if ( get_stylesheet() === $stylesheet ) {
  1683. $post_id = get_theme_mod( 'custom_css_post_id' );
  1684. if ( $post_id > 0 && get_post( $post_id ) ) {
  1685. $post = get_post( $post_id );
  1686. }
  1687. // `-1` indicates no post exists; no query necessary.
  1688. if ( ! $post && -1 !== $post_id ) {
  1689. $query = new WP_Query( $custom_css_query_vars );
  1690. $post = $query->post;
  1691. /*
  1692. * Cache the lookup. See wp_update_custom_css_post().
  1693. * @todo This should get cleared if a custom_css post is added/removed.
  1694. */
  1695. set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 );
  1696. }
  1697. } else {
  1698. $query = new WP_Query( $custom_css_query_vars );
  1699. $post = $query->post;
  1700. }
  1701. return $post;
  1702. }
  1703. /**
  1704. * Fetches the saved Custom CSS content for rendering.
  1705. *
  1706. * @since 4.7.0
  1707. *
  1708. * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the active theme.
  1709. * @return string The Custom CSS Post content.
  1710. */
  1711. function wp_get_custom_css( $stylesheet = '' ) {
  1712. $css = '';
  1713. if ( empty( $stylesheet ) ) {
  1714. $stylesheet = get_stylesheet();
  1715. }
  1716. $post = wp_get_custom_css_post( $stylesheet );
  1717. if ( $post ) {
  1718. $css = $post->post_content;
  1719. }
  1720. /**
  1721. * Filters the custom CSS output into the head element.
  1722. *
  1723. * @since 4.7.0
  1724. *
  1725. * @param string $css CSS pulled in from the Custom CSS post type.
  1726. * @param string $stylesheet The theme stylesheet name.
  1727. */
  1728. $css = apply_filters( 'wp_get_custom_css', $css, $stylesheet );
  1729. return $css;
  1730. }
  1731. /**
  1732. * Updates the `custom_css` post for a given theme.
  1733. *
  1734. * Inserts a `custom_css` post when one doesn't yet exist.
  1735. *
  1736. * @since 4.7.0
  1737. *
  1738. * @param string $css CSS, stored in `post_content`.
  1739. * @param array $args {
  1740. * Args.
  1741. *
  1742. * @type string $preprocessed Optional. Pre-processed CSS, stored in `post_content_filtered`.
  1743. * Normally empty string.
  1744. * @type string $stylesheet Optional. Stylesheet (child theme) to update.
  1745. * Defaults to active theme/stylesheet.
  1746. * }
  1747. * @return WP_Post|WP_Error Post on success, error on failure.
  1748. */
  1749. function wp_update_custom_css_post( $css, $args = array() ) {
  1750. $args = wp_parse_args(
  1751. $args,
  1752. array(
  1753. 'preprocessed' => '',
  1754. 'stylesheet' => get_stylesheet(),
  1755. )
  1756. );
  1757. $data = array(
  1758. 'css' => $css,
  1759. 'preprocessed' => $args['preprocessed'],
  1760. );
  1761. /**
  1762. * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args
  1763. * for a `custom_css` post being updated.
  1764. *
  1765. * This filter can be used by plugin that offer CSS pre-processors, to store the original
  1766. * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
  1767. * When used in this way, the `post_content_filtered` should be supplied as the setting value
  1768. * instead of `post_content` via a the `customize_value_custom_css` filter, for example:
  1769. *
  1770. * <code>
  1771. * add_filter( 'customize_value_custom_css', function( $value, $setting ) {
  1772. * $post = wp_get_custom_css_post( $setting->stylesheet );
  1773. * if ( $post && ! empty( $post->post_content_filtered ) ) {
  1774. * $css = $post->post_content_filtered;
  1775. * }
  1776. * return $css;
  1777. * }, 10, 2 );
  1778. * </code>
  1779. *
  1780. * @since 4.7.0
  1781. * @param array $data {
  1782. * Custom CSS data.
  1783. *
  1784. * @type string $css CSS stored in `post_content`.
  1785. * @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`.
  1786. * Normally empty string.
  1787. * }
  1788. * @param array $args {
  1789. * The args passed into `wp_update_custom_css_post()` merged with defaults.
  1790. *
  1791. * @type string $css The original CSS passed in to be updated.
  1792. * @type string $preprocessed The original preprocessed CSS passed in to be updated.
  1793. * @type string $stylesheet The stylesheet (theme) being updated.
  1794. * }
  1795. */
  1796. $data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) );
  1797. $post_data = array(
  1798. 'post_title' => $args['stylesheet'],
  1799. 'post_name' => sanitize_title( $args['stylesheet'] ),
  1800. 'post_type' => 'custom_css',
  1801. 'post_status' => 'publish',
  1802. 'post_content' => $data['css'],
  1803. 'post_content_filtered' => $data['preprocessed'],
  1804. );
  1805. // Update post if it already exists, otherwise create a new one.
  1806. $post = wp_get_custom_css_post( $args['stylesheet'] );
  1807. if ( $post ) {
  1808. $post_data['ID'] = $post->ID;
  1809. $r = wp_update_post( wp_slash( $post_data ), true );
  1810. } else {
  1811. $r = wp_insert_post( wp_slash( $post_data ), true );
  1812. if ( ! is_wp_error( $r ) ) {
  1813. if ( get_stylesheet() === $args['stylesheet'] ) {
  1814. set_theme_mod( 'custom_css_post_id', $r );
  1815. }
  1816. // Trigger creation of a revision. This should be removed once #30854 is resolved.
  1817. if ( 0 === count( wp_get_post_revisions( $r ) ) ) {
  1818. wp_save_post_revision( $r );
  1819. }
  1820. }
  1821. }
  1822. if ( is_wp_error( $r ) ) {
  1823. return $r;
  1824. }
  1825. return get_post( $r );
  1826. }
  1827. /**
  1828. * Adds callback for custom TinyMCE editor stylesheets.
  1829. *
  1830. * The parameter $stylesheet is the name of the stylesheet, relative to
  1831. * the theme root. It also accepts an array of stylesheets.
  1832. * It is optional and defaults to 'editor-style.css'.
  1833. *
  1834. * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
  1835. * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
  1836. * If an array of stylesheets is passed to add_editor_style(),
  1837. * RTL is only added for the first stylesheet.
  1838. *
  1839. * Since version 3.4 the TinyMCE body has .rtl CSS class.
  1840. * It is a better option to use that class and add any RTL styles to the main stylesheet.
  1841. *
  1842. * @since 3.0.0
  1843. *
  1844. * @global array $editor_styles
  1845. *
  1846. * @param array|string $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
  1847. * Defaults to 'editor-style.css'
  1848. */
  1849. function add_editor_style( $stylesheet = 'editor-style.css' ) {
  1850. global $editor_styles;
  1851. add_theme_support( 'editor-style' );
  1852. $editor_styles = (array) $editor_styles;
  1853. $stylesheet = (array) $stylesheet;
  1854. if ( is_rtl() ) {
  1855. $rtl_stylesheet = str_replace( '.css', '-rtl.css', $stylesheet[0] );
  1856. $stylesheet[] = $rtl_stylesheet;
  1857. }
  1858. $editor_styles = array_merge( $editor_styles, $stylesheet );
  1859. }
  1860. /**
  1861. * Removes all visual editor stylesheets.
  1862. *
  1863. * @since 3.1.0
  1864. *
  1865. * @global array $editor_styles
  1866. *
  1867. * @return bool True on success, false if there were no stylesheets to remove.
  1868. */
  1869. function remove_editor_styles() {
  1870. if ( ! current_theme_supports( 'editor-style' ) ) {
  1871. return false;
  1872. }
  1873. _remove_theme_support( 'editor-style' );
  1874. if ( is_admin() ) {
  1875. $GLOBALS['editor_styles'] = array();
  1876. }
  1877. return true;
  1878. }
  1879. /**
  1880. * Retrieves any registered editor stylesheet URLs.
  1881. *
  1882. * @since 4.0.0
  1883. *
  1884. * @global array $editor_styles Registered editor stylesheets
  1885. *
  1886. * @return string[] If registered, a list of editor stylesheet URLs.
  1887. */
  1888. function get_editor_stylesheets() {
  1889. $stylesheets = array();
  1890. // Load editor_style.css if the active theme supports it.
  1891. if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
  1892. $editor_styles = $GLOBALS['editor_styles'];
  1893. $editor_styles = array_unique( array_filter( $editor_styles ) );
  1894. $style_uri = get_stylesheet_directory_uri();
  1895. $style_dir = get_stylesheet_directory();
  1896. // Support externally referenced styles (like, say, fonts).
  1897. foreach ( $editor_styles as $key => $file ) {
  1898. if ( preg_match( '~^(https?:)?//~', $file ) ) {
  1899. $stylesheets[] = esc_url_raw( $file );
  1900. unset( $editor_styles[ $key ] );
  1901. }
  1902. }
  1903. // Look in a parent theme first, that way child theme CSS overrides.
  1904. if ( is_child_theme() ) {
  1905. $template_uri = get_template_directory_uri();
  1906. $template_dir = get_template_directory();
  1907. foreach ( $editor_styles as $key => $file ) {
  1908. if ( $file && file_exists( "$template_dir/$file" ) ) {
  1909. $stylesheets[] = "$template_uri/$file";
  1910. }
  1911. }
  1912. }
  1913. foreach ( $editor_styles as $file ) {
  1914. if ( $file && file_exists( "$style_dir/$file" ) ) {
  1915. $stylesheets[] = "$style_uri/$file";
  1916. }
  1917. }
  1918. }
  1919. /**
  1920. * Filters the array of URLs of stylesheets applied to the editor.
  1921. *
  1922. * @since 4.3.0
  1923. *
  1924. * @param string[] $stylesheets Array of URLs of stylesheets to be applied to the editor.
  1925. */
  1926. return apply_filters( 'editor_stylesheets', $stylesheets );
  1927. }
  1928. /**
  1929. * Expands a theme's starter content configuration using core-provided data.
  1930. *
  1931. * @since 4.7.0
  1932. *
  1933. * @return array Array of starter content.
  1934. */
  1935. function get_theme_starter_content() {
  1936. $theme_support = get_theme_support( 'starter-content' );
  1937. if ( is_array( $theme_support ) && ! empty( $theme_support[0] ) && is_array( $theme_support[0] ) ) {
  1938. $config = $theme_support[0];
  1939. } else {
  1940. $config = array();
  1941. }
  1942. $core_content = array(
  1943. 'widgets' => array(
  1944. 'text_business_info' => array(
  1945. 'text',
  1946. array(
  1947. 'title' => _x( 'Find Us', 'Theme starter content' ),
  1948. 'text' => implode(
  1949. '',
  1950. array(
  1951. '<strong>' . _x( 'Address', 'Theme starter content' ) . "</strong>\n",
  1952. _x( '123 Main Street', 'Theme starter content' ) . "\n",
  1953. _x( 'New York, NY 10001', 'Theme starter content' ) . "\n\n",
  1954. '<strong>' . _x( 'Hours', 'Theme starter content' ) . "</strong>\n",
  1955. _x( 'Monday&ndash;Friday: 9:00AM&ndash;5:00PM', 'Theme starter content' ) . "\n",
  1956. _x( 'Saturday &amp; Sunday: 11:00AM&ndash;3:00PM', 'Theme starter content' ),
  1957. )
  1958. ),
  1959. 'filter' => true,
  1960. 'visual' => true,
  1961. ),
  1962. ),
  1963. 'text_about' => array(
  1964. 'text',
  1965. array(
  1966. 'title' => _x( 'About This Site', 'Theme starter content' ),
  1967. 'text' => _x( 'This may be a good place to introduce yourself and your site or include some credits.', 'Theme starter content' ),
  1968. 'filter' => true,
  1969. 'visual' => true,
  1970. ),
  1971. ),
  1972. 'archives' => array(
  1973. 'archives',
  1974. array(
  1975. 'title' => _x( 'Archives', 'Theme starter content' ),
  1976. ),
  1977. ),
  1978. 'calendar' => array(
  1979. 'calendar',
  1980. array(
  1981. 'title' => _x( 'Calendar', 'Theme starter content' ),
  1982. ),
  1983. ),
  1984. 'categories' => array(
  1985. 'categories',
  1986. array(
  1987. 'title' => _x( 'Categories', 'Theme starter content' ),
  1988. ),
  1989. ),
  1990. 'meta' => array(
  1991. 'meta',
  1992. array(
  1993. 'title' => _x( 'Meta', 'Theme starter content' ),
  1994. ),
  1995. ),
  1996. 'recent-comments' => array(
  1997. 'recent-comments',
  1998. array(
  1999. 'title' => _x( 'Recent Comments', 'Theme starter content' ),
  2000. ),
  2001. ),
  2002. 'recent-posts' => array(
  2003. 'recent-posts',
  2004. array(
  2005. 'title' => _x( 'Recent Posts', 'Theme starter content' ),
  2006. ),
  2007. ),
  2008. 'search' => array(
  2009. 'search',
  2010. array(
  2011. 'title' => _x( 'Search', 'Theme starter content' ),
  2012. ),
  2013. ),
  2014. ),
  2015. 'nav_menus' => array(
  2016. 'link_home' => array(
  2017. 'type' => 'custom',
  2018. 'title' => _x( 'Home', 'Theme starter content' ),
  2019. 'url' => home_url( '/' ),
  2020. ),
  2021. 'page_home' => array( // Deprecated in favor of 'link_home'.
  2022. 'type' => 'post_type',
  2023. 'object' => 'page',
  2024. 'object_id' => '{{home}}',
  2025. ),
  2026. 'page_about' => array(
  2027. 'type' => 'post_type',
  2028. 'object' => 'page',
  2029. 'object_id' => '{{about}}',
  2030. ),
  2031. 'page_blog' => array(
  2032. 'type' => 'post_type',
  2033. 'object' => 'page',
  2034. 'object_id' => '{{blog}}',
  2035. ),
  2036. 'page_news' => array(
  2037. 'type' => 'post_type',
  2038. 'object' => 'page',
  2039. 'object_id' => '{{news}}',
  2040. ),
  2041. 'page_contact' => array(
  2042. 'type' => 'post_type',
  2043. 'object' => 'page',
  2044. 'object_id' => '{{contact}}',
  2045. ),
  2046. 'link_email' => array(
  2047. 'title' => _x( 'Email', 'Theme starter content' ),
  2048. 'url' => 'mailto:wordpress@example.com',
  2049. ),
  2050. 'link_facebook' => array(
  2051. 'title' => _x( 'Facebook', 'Theme starter content' ),
  2052. 'url' => 'https://www.facebook.com/wordpress',
  2053. ),
  2054. 'link_foursquare' => array(
  2055. 'title' => _x( 'Foursquare', 'Theme starter content' ),
  2056. 'url' => 'https://foursquare.com/',
  2057. ),
  2058. 'link_github' => array(
  2059. 'title' => _x( 'GitHub', 'Theme starter content' ),
  2060. 'url' => 'https://github.com/wordpress/',
  2061. ),
  2062. 'link_instagram' => array(
  2063. 'title' => _x( 'Instagram', 'Theme starter content' ),
  2064. 'url' => 'https://www.instagram.com/explore/tags/wordcamp/',
  2065. ),
  2066. 'link_linkedin' => array(
  2067. 'title' => _x( 'LinkedIn', 'Theme starter content' ),
  2068. 'url' => 'https://www.linkedin.com/company/1089783',
  2069. ),
  2070. 'link_pinterest' => array(
  2071. 'title' => _x( 'Pinterest', 'Theme starter content' ),
  2072. 'url' => 'https://www.pinterest.com/',
  2073. ),
  2074. 'link_twitter' => array(
  2075. 'title' => _x( 'Twitter', 'Theme starter content' ),
  2076. 'url' => 'https://twitter.com/wordpress',
  2077. ),
  2078. 'link_yelp' => array(
  2079. 'title' => _x( 'Yelp', 'Theme starter content' ),
  2080. 'url' => 'https://www.yelp.com',
  2081. ),
  2082. 'link_youtube' => array(
  2083. 'title' => _x( 'YouTube', 'Theme starter content' ),
  2084. 'url' => 'https://www.youtube.com/channel/UCdof4Ju7amm1chz1gi1T2ZA',
  2085. ),
  2086. ),
  2087. 'posts' => array(
  2088. 'home' => array(
  2089. 'post_type' => 'page',
  2090. 'post_title' => _x( 'Home', 'Theme starter content' ),
  2091. 'post_content' => sprintf(
  2092. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2093. _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' )
  2094. ),
  2095. ),
  2096. 'about' => array(
  2097. 'post_type' => 'page',
  2098. 'post_title' => _x( 'About', 'Theme starter content' ),
  2099. 'post_content' => sprintf(
  2100. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2101. _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' )
  2102. ),
  2103. ),
  2104. 'contact' => array(
  2105. 'post_type' => 'page',
  2106. 'post_title' => _x( 'Contact', 'Theme starter content' ),
  2107. 'post_content' => sprintf(
  2108. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2109. _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' )
  2110. ),
  2111. ),
  2112. 'blog' => array(
  2113. 'post_type' => 'page',
  2114. 'post_title' => _x( 'Blog', 'Theme starter content' ),
  2115. ),
  2116. 'news' => array(
  2117. 'post_type' => 'page',
  2118. 'post_title' => _x( 'News', 'Theme starter content' ),
  2119. ),
  2120. 'homepage-section' => array(
  2121. 'post_type' => 'page',
  2122. 'post_title' => _x( 'A homepage section', 'Theme starter content' ),
  2123. 'post_content' => sprintf(
  2124. "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->",
  2125. _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' )
  2126. ),
  2127. ),
  2128. ),
  2129. );
  2130. $content = array();
  2131. foreach ( $config as $type => $args ) {
  2132. switch ( $type ) {
  2133. // Use options and theme_mods as-is.
  2134. case 'options':
  2135. case 'theme_mods':
  2136. $content[ $type ] = $config[ $type ];
  2137. break;
  2138. // Widgets are grouped into sidebars.
  2139. case 'widgets':
  2140. foreach ( $config[ $type ] as $sidebar_id => $widgets ) {
  2141. foreach ( $widgets as $id => $widget ) {
  2142. if ( is_array( $widget ) ) {
  2143. // Item extends core content.
  2144. if ( ! empty( $core_content[ $type ][ $id ] ) ) {
  2145. $widget = array(
  2146. $core_content[ $type ][ $id ][0],
  2147. array_merge( $core_content[ $type ][ $id ][1], $widget ),
  2148. );
  2149. }
  2150. $content[ $type ][ $sidebar_id ][] = $widget;
  2151. } elseif ( is_string( $widget )
  2152. && ! empty( $core_content[ $type ] )
  2153. && ! empty( $core_content[ $type ][ $widget ] )
  2154. ) {
  2155. $content[ $type ][ $sidebar_id ][] = $core_content[ $type ][ $widget ];
  2156. }
  2157. }
  2158. }
  2159. break;
  2160. // And nav menu items are grouped into nav menus.
  2161. case 'nav_menus':
  2162. foreach ( $config[ $type ] as $nav_menu_location => $nav_menu ) {
  2163. // Ensure nav menus get a name.
  2164. if ( empty( $nav_menu['name'] ) ) {
  2165. $nav_menu['name'] = $nav_menu_location;
  2166. }
  2167. $content[ $type ][ $nav_menu_location ]['name'] = $nav_menu['name'];
  2168. foreach ( $nav_menu['items'] as $id => $nav_menu_item ) {
  2169. if ( is_array( $nav_menu_item ) ) {
  2170. // Item extends core content.
  2171. if ( ! empty( $core_content[ $type ][ $id ] ) ) {
  2172. $nav_menu_item = array_merge( $core_content[ $type ][ $id ], $nav_menu_item );
  2173. }
  2174. $content[ $type ][ $nav_menu_location ]['items'][] = $nav_menu_item;
  2175. } elseif ( is_string( $nav_menu_item )
  2176. && ! empty( $core_content[ $type ] )
  2177. && ! empty( $core_content[ $type ][ $nav_menu_item ] )
  2178. ) {
  2179. $content[ $type ][ $nav_menu_location ]['items'][] = $core_content[ $type ][ $nav_menu_item ];
  2180. }
  2181. }
  2182. }
  2183. break;
  2184. // Attachments are posts but have special treatment.
  2185. case 'attachments':
  2186. foreach ( $config[ $type ] as $id => $item ) {
  2187. if ( ! empty( $item['file'] ) ) {
  2188. $content[ $type ][ $id ] = $item;
  2189. }
  2190. }
  2191. break;
  2192. // All that's left now are posts (besides attachments).
  2193. // Not a default case for the sake of clarity and future work.
  2194. case 'posts':
  2195. foreach ( $config[ $type ] as $id => $item ) {
  2196. if ( is_array( $item ) ) {
  2197. // Item extends core content.
  2198. if ( ! empty( $core_content[ $type ][ $id ] ) ) {
  2199. $item = array_merge( $core_content[ $type ][ $id ], $item );
  2200. }
  2201. // Enforce a subset of fields.
  2202. $content[ $type ][ $id ] = wp_array_slice_assoc(
  2203. $item,
  2204. array(
  2205. 'post_type',
  2206. 'post_title',
  2207. 'post_excerpt',
  2208. 'post_name',
  2209. 'post_content',
  2210. 'menu_order',
  2211. 'comment_status',
  2212. 'thumbnail',
  2213. 'template',
  2214. )
  2215. );
  2216. } elseif ( is_string( $item ) && ! empty( $core_content[ $type ][ $item ] ) ) {
  2217. $content[ $type ][ $item ] = $core_content[ $type ][ $item ];
  2218. }
  2219. }
  2220. break;
  2221. }
  2222. }
  2223. /**
  2224. * Filters the expanded array of starter content.
  2225. *
  2226. * @since 4.7.0
  2227. *
  2228. * @param array $content Array of starter content.
  2229. * @param array $config Array of theme-specific starter content configuration.
  2230. */
  2231. return apply_filters( 'get_theme_starter_content', $content, $config );
  2232. }
  2233. /**
  2234. * Registers theme support for a given feature.
  2235. *
  2236. * Must be called in the theme's functions.php file to work.
  2237. * If attached to a hook, it must be {@see 'after_setup_theme'}.
  2238. * The {@see 'init'} hook may be too late for some features.
  2239. *
  2240. * Example usage:
  2241. *
  2242. * add_theme_support( 'title-tag' );
  2243. * add_theme_support( 'custom-logo', array(
  2244. * 'height' => 480,
  2245. * 'width' => 720,
  2246. * ) );
  2247. *
  2248. * @since 2.9.0
  2249. * @since 3.4.0 The `custom-header-uploads` feature was deprecated.
  2250. * @since 3.6.0 The `html5` feature was added.
  2251. * @since 3.9.0 The `html5` feature now also accepts 'gallery' and 'caption'.
  2252. * @since 4.1.0 The `title-tag` feature was added.
  2253. * @since 4.5.0 The `customize-selective-refresh-widgets` feature was added.
  2254. * @since 4.7.0 The `starter-content` feature was added.
  2255. * @since 5.0.0 The `responsive-embeds`, `align-wide`, `dark-editor-style`, `disable-custom-colors`,
  2256. * `disable-custom-font-sizes`, `editor-color-palette`, `editor-font-sizes`,
  2257. * `editor-styles`, and `wp-block-styles` features were added.
  2258. * @since 5.3.0 The `html5` feature now also accepts 'script' and 'style'.
  2259. * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  2260. * by adding it to the function signature.
  2261. * @since 5.5.0 The `core-block-patterns` feature was added and is enabled by default.
  2262. * @since 5.5.0 The `custom-logo` feature now also accepts 'unlink-homepage-logo'.
  2263. * @since 5.6.0 The `post-formats` feature warns if no array is passed.
  2264. * @since 5.8.0 The `widgets-block-editor` feature enables the Widgets block editor.
  2265. *
  2266. * @global array $_wp_theme_features
  2267. *
  2268. * @param string $feature The feature being added. Likely core values include:
  2269. * - 'admin-bar'
  2270. * - 'align-wide'
  2271. * - 'automatic-feed-links'
  2272. * - 'core-block-patterns'
  2273. * - 'custom-background'
  2274. * - 'custom-header'
  2275. * - 'custom-line-height'
  2276. * - 'custom-logo'
  2277. * - 'customize-selective-refresh-widgets'
  2278. * - 'custom-spacing'
  2279. * - 'custom-units'
  2280. * - 'dark-editor-style'
  2281. * - 'disable-custom-colors'
  2282. * - 'disable-custom-font-sizes'
  2283. * - 'editor-color-palette'
  2284. * - 'editor-gradient-presets'
  2285. * - 'editor-font-sizes'
  2286. * - 'editor-styles'
  2287. * - 'featured-content'
  2288. * - 'html5'
  2289. * - 'menus'
  2290. * - 'post-formats'
  2291. * - 'post-thumbnails'
  2292. * - 'responsive-embeds'
  2293. * - 'starter-content'
  2294. * - 'title-tag'
  2295. * - 'wp-block-styles'
  2296. * - 'widgets'
  2297. * - 'widgets-block-editor'
  2298. * @param mixed ...$args Optional extra arguments to pass along with certain features.
  2299. * @return void|false Void on success, false on failure.
  2300. */
  2301. function add_theme_support( $feature, ...$args ) {
  2302. global $_wp_theme_features;
  2303. if ( ! $args ) {
  2304. $args = true;
  2305. }
  2306. switch ( $feature ) {
  2307. case 'post-thumbnails':
  2308. // All post types are already supported.
  2309. if ( true === get_theme_support( 'post-thumbnails' ) ) {
  2310. return;
  2311. }
  2312. /*
  2313. * Merge post types with any that already declared their support
  2314. * for post thumbnails.
  2315. */
  2316. if ( isset( $args[0] ) && is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) {
  2317. $args[0] = array_unique( array_merge( $_wp_theme_features['post-thumbnails'][0], $args[0] ) );
  2318. }
  2319. break;
  2320. case 'post-formats':
  2321. if ( isset( $args[0] ) && is_array( $args[0] ) ) {
  2322. $post_formats = get_post_format_slugs();
  2323. unset( $post_formats['standard'] );
  2324. $args[0] = array_intersect( $args[0], array_keys( $post_formats ) );
  2325. } else {
  2326. _doing_it_wrong(
  2327. "add_theme_support( 'post-formats' )",
  2328. __( 'You need to pass an array of post formats.' ),
  2329. '5.6.0'
  2330. );
  2331. return false;
  2332. }
  2333. break;
  2334. case 'html5':
  2335. // You can't just pass 'html5', you need to pass an array of types.
  2336. if ( empty( $args[0] ) || ! is_array( $args[0] ) ) {
  2337. _doing_it_wrong(
  2338. "add_theme_support( 'html5' )",
  2339. __( 'You need to pass an array of types.' ),
  2340. '3.6.1'
  2341. );
  2342. if ( ! empty( $args[0] ) && ! is_array( $args[0] ) ) {
  2343. return false;
  2344. }
  2345. // Build an array of types for back-compat.
  2346. $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
  2347. }
  2348. // Calling 'html5' again merges, rather than overwrites.
  2349. if ( isset( $_wp_theme_features['html5'] ) ) {
  2350. $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
  2351. }
  2352. break;
  2353. case 'custom-logo':
  2354. if ( true === $args ) {
  2355. $args = array( 0 => array() );
  2356. }
  2357. $defaults = array(
  2358. 'width' => null,
  2359. 'height' => null,
  2360. 'flex-width' => false,
  2361. 'flex-height' => false,
  2362. 'header-text' => '',
  2363. 'unlink-homepage-logo' => false,
  2364. );
  2365. $args[0] = wp_parse_args( array_intersect_key( $args[0], $defaults ), $defaults );
  2366. // Allow full flexibility if no size is specified.
  2367. if ( is_null( $args[0]['width'] ) && is_null( $args[0]['height'] ) ) {
  2368. $args[0]['flex-width'] = true;
  2369. $args[0]['flex-height'] = true;
  2370. }
  2371. break;
  2372. case 'custom-header-uploads':
  2373. return add_theme_support( 'custom-header', array( 'uploads' => true ) );
  2374. case 'custom-header':
  2375. if ( true === $args ) {
  2376. $args = array( 0 => array() );
  2377. }
  2378. $defaults = array(
  2379. 'default-image' => '',
  2380. 'random-default' => false,
  2381. 'width' => 0,
  2382. 'height' => 0,
  2383. 'flex-height' => false,
  2384. 'flex-width' => false,
  2385. 'default-text-color' => '',
  2386. 'header-text' => true,
  2387. 'uploads' => true,
  2388. 'wp-head-callback' => '',
  2389. 'admin-head-callback' => '',
  2390. 'admin-preview-callback' => '',
  2391. 'video' => false,
  2392. 'video-active-callback' => 'is_front_page',
  2393. );
  2394. $jit = isset( $args[0]['__jit'] );
  2395. unset( $args[0]['__jit'] );
  2396. // Merge in data from previous add_theme_support() calls.
  2397. // The first value registered wins. (A child theme is set up first.)
  2398. if ( isset( $_wp_theme_features['custom-header'] ) ) {
  2399. $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
  2400. }
  2401. // Load in the defaults at the end, as we need to insure first one wins.
  2402. // This will cause all constants to be defined, as each arg will then be set to the default.
  2403. if ( $jit ) {
  2404. $args[0] = wp_parse_args( $args[0], $defaults );
  2405. }
  2406. /*
  2407. * If a constant was defined, use that value. Otherwise, define the constant to ensure
  2408. * the constant is always accurate (and is not defined later, overriding our value).
  2409. * As stated above, the first value wins.
  2410. * Once we get to wp_loaded (just-in-time), define any constants we haven't already.
  2411. * Constants are lame. Don't reference them. This is just for backward compatibility.
  2412. */
  2413. if ( defined( 'NO_HEADER_TEXT' ) ) {
  2414. $args[0]['header-text'] = ! NO_HEADER_TEXT;
  2415. } elseif ( isset( $args[0]['header-text'] ) ) {
  2416. define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
  2417. }
  2418. if ( defined( 'HEADER_IMAGE_WIDTH' ) ) {
  2419. $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
  2420. } elseif ( isset( $args[0]['width'] ) ) {
  2421. define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
  2422. }
  2423. if ( defined( 'HEADER_IMAGE_HEIGHT' ) ) {
  2424. $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
  2425. } elseif ( isset( $args[0]['height'] ) ) {
  2426. define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
  2427. }
  2428. if ( defined( 'HEADER_TEXTCOLOR' ) ) {
  2429. $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
  2430. } elseif ( isset( $args[0]['default-text-color'] ) ) {
  2431. define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
  2432. }
  2433. if ( defined( 'HEADER_IMAGE' ) ) {
  2434. $args[0]['default-image'] = HEADER_IMAGE;
  2435. } elseif ( isset( $args[0]['default-image'] ) ) {
  2436. define( 'HEADER_IMAGE', $args[0]['default-image'] );
  2437. }
  2438. if ( $jit && ! empty( $args[0]['default-image'] ) ) {
  2439. $args[0]['random-default'] = false;
  2440. }
  2441. // If headers are supported, and we still don't have a defined width or height,
  2442. // we have implicit flex sizes.
  2443. if ( $jit ) {
  2444. if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) ) {
  2445. $args[0]['flex-width'] = true;
  2446. }
  2447. if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) ) {
  2448. $args[0]['flex-height'] = true;
  2449. }
  2450. }
  2451. break;
  2452. case 'custom-background':
  2453. if ( true === $args ) {
  2454. $args = array( 0 => array() );
  2455. }
  2456. $defaults = array(
  2457. 'default-image' => '',
  2458. 'default-preset' => 'default',
  2459. 'default-position-x' => 'left',
  2460. 'default-position-y' => 'top',
  2461. 'default-size' => 'auto',
  2462. 'default-repeat' => 'repeat',
  2463. 'default-attachment' => 'scroll',
  2464. 'default-color' => '',
  2465. 'wp-head-callback' => '_custom_background_cb',
  2466. 'admin-head-callback' => '',
  2467. 'admin-preview-callback' => '',
  2468. );
  2469. $jit = isset( $args[0]['__jit'] );
  2470. unset( $args[0]['__jit'] );
  2471. // Merge in data from previous add_theme_support() calls. The first value registered wins.
  2472. if ( isset( $_wp_theme_features['custom-background'] ) ) {
  2473. $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
  2474. }
  2475. if ( $jit ) {
  2476. $args[0] = wp_parse_args( $args[0], $defaults );
  2477. }
  2478. if ( defined( 'BACKGROUND_COLOR' ) ) {
  2479. $args[0]['default-color'] = BACKGROUND_COLOR;
  2480. } elseif ( isset( $args[0]['default-color'] ) || $jit ) {
  2481. define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
  2482. }
  2483. if ( defined( 'BACKGROUND_IMAGE' ) ) {
  2484. $args[0]['default-image'] = BACKGROUND_IMAGE;
  2485. } elseif ( isset( $args[0]['default-image'] ) || $jit ) {
  2486. define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
  2487. }
  2488. break;
  2489. // Ensure that 'title-tag' is accessible in the admin.
  2490. case 'title-tag':
  2491. // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php.
  2492. if ( did_action( 'wp_loaded' ) ) {
  2493. _doing_it_wrong(
  2494. "add_theme_support( 'title-tag' )",
  2495. sprintf(
  2496. /* translators: 1: title-tag, 2: wp_loaded */
  2497. __( 'Theme support for %1$s should be registered before the %2$s hook.' ),
  2498. '<code>title-tag</code>',
  2499. '<code>wp_loaded</code>'
  2500. ),
  2501. '4.1.0'
  2502. );
  2503. return false;
  2504. }
  2505. }
  2506. $_wp_theme_features[ $feature ] = $args;
  2507. }
  2508. /**
  2509. * Registers the internal custom header and background routines.
  2510. *
  2511. * @since 3.4.0
  2512. * @access private
  2513. *
  2514. * @global Custom_Image_Header $custom_image_header
  2515. * @global Custom_Background $custom_background
  2516. */
  2517. function _custom_header_background_just_in_time() {
  2518. global $custom_image_header, $custom_background;
  2519. if ( current_theme_supports( 'custom-header' ) ) {
  2520. // In case any constants were defined after an add_custom_image_header() call, re-run.
  2521. add_theme_support( 'custom-header', array( '__jit' => true ) );
  2522. $args = get_theme_support( 'custom-header' );
  2523. if ( $args[0]['wp-head-callback'] ) {
  2524. add_action( 'wp_head', $args[0]['wp-head-callback'] );
  2525. }
  2526. if ( is_admin() ) {
  2527. require_once ABSPATH . 'wp-admin/includes/class-custom-image-header.php';
  2528. $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
  2529. }
  2530. }
  2531. if ( current_theme_supports( 'custom-background' ) ) {
  2532. // In case any constants were defined after an add_custom_background() call, re-run.
  2533. add_theme_support( 'custom-background', array( '__jit' => true ) );
  2534. $args = get_theme_support( 'custom-background' );
  2535. add_action( 'wp_head', $args[0]['wp-head-callback'] );
  2536. if ( is_admin() ) {
  2537. require_once ABSPATH . 'wp-admin/includes/class-custom-background.php';
  2538. $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
  2539. }
  2540. }
  2541. }
  2542. /**
  2543. * Adds CSS to hide header text for custom logo, based on Customizer setting.
  2544. *
  2545. * @since 4.5.0
  2546. * @access private
  2547. */
  2548. function _custom_logo_header_styles() {
  2549. if ( ! current_theme_supports( 'custom-header', 'header-text' )
  2550. && get_theme_support( 'custom-logo', 'header-text' )
  2551. && ! get_theme_mod( 'header_text', true )
  2552. ) {
  2553. $classes = (array) get_theme_support( 'custom-logo', 'header-text' );
  2554. $classes = array_map( 'sanitize_html_class', $classes );
  2555. $classes = '.' . implode( ', .', $classes );
  2556. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  2557. ?>
  2558. <!-- Custom Logo: hide header text -->
  2559. <style id="custom-logo-css"<?php echo $type_attr; ?>>
  2560. <?php echo $classes; ?> {
  2561. position: absolute;
  2562. clip: rect(1px, 1px, 1px, 1px);
  2563. }
  2564. </style>
  2565. <?php
  2566. }
  2567. }
  2568. /**
  2569. * Gets the theme support arguments passed when registering that support.
  2570. *
  2571. * Example usage:
  2572. *
  2573. * get_theme_support( 'custom-logo' );
  2574. * get_theme_support( 'custom-header', 'width' );
  2575. *
  2576. * @since 3.1.0
  2577. * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  2578. * by adding it to the function signature.
  2579. *
  2580. * @global array $_wp_theme_features
  2581. *
  2582. * @param string $feature The feature to check. See add_theme_support() for the list
  2583. * of possible values.
  2584. * @param mixed ...$args Optional extra arguments to be checked against certain features.
  2585. * @return mixed The array of extra arguments or the value for the registered feature.
  2586. */
  2587. function get_theme_support( $feature, ...$args ) {
  2588. global $_wp_theme_features;
  2589. if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
  2590. return false;
  2591. }
  2592. if ( ! $args ) {
  2593. return $_wp_theme_features[ $feature ];
  2594. }
  2595. switch ( $feature ) {
  2596. case 'custom-logo':
  2597. case 'custom-header':
  2598. case 'custom-background':
  2599. if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) ) {
  2600. return $_wp_theme_features[ $feature ][0][ $args[0] ];
  2601. }
  2602. return false;
  2603. default:
  2604. return $_wp_theme_features[ $feature ];
  2605. }
  2606. }
  2607. /**
  2608. * Allows a theme to de-register its support of a certain feature
  2609. *
  2610. * Should be called in the theme's functions.php file. Generally would
  2611. * be used for child themes to override support from the parent theme.
  2612. *
  2613. * @since 3.0.0
  2614. *
  2615. * @see add_theme_support()
  2616. *
  2617. * @param string $feature The feature being removed. See add_theme_support() for the list
  2618. * of possible values.
  2619. * @return bool|void Whether feature was removed.
  2620. */
  2621. function remove_theme_support( $feature ) {
  2622. // Do not remove internal registrations that are not used directly by themes.
  2623. if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ), true ) ) {
  2624. return false;
  2625. }
  2626. return _remove_theme_support( $feature );
  2627. }
  2628. /**
  2629. * Do not use. Removes theme support internally without knowledge of those not used
  2630. * by themes directly.
  2631. *
  2632. * @access private
  2633. * @since 3.1.0
  2634. * @global array $_wp_theme_features
  2635. * @global Custom_Image_Header $custom_image_header
  2636. * @global Custom_Background $custom_background
  2637. *
  2638. * @param string $feature The feature being removed. See add_theme_support() for the list
  2639. * of possible values.
  2640. * @return bool True if support was removed, false if the feature was not registered.
  2641. */
  2642. function _remove_theme_support( $feature ) {
  2643. global $_wp_theme_features;
  2644. switch ( $feature ) {
  2645. case 'custom-header-uploads':
  2646. if ( ! isset( $_wp_theme_features['custom-header'] ) ) {
  2647. return false;
  2648. }
  2649. add_theme_support( 'custom-header', array( 'uploads' => false ) );
  2650. return; // Do not continue - custom-header-uploads no longer exists.
  2651. }
  2652. if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
  2653. return false;
  2654. }
  2655. switch ( $feature ) {
  2656. case 'custom-header':
  2657. if ( ! did_action( 'wp_loaded' ) ) {
  2658. break;
  2659. }
  2660. $support = get_theme_support( 'custom-header' );
  2661. if ( isset( $support[0]['wp-head-callback'] ) ) {
  2662. remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  2663. }
  2664. if ( isset( $GLOBALS['custom_image_header'] ) ) {
  2665. remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
  2666. unset( $GLOBALS['custom_image_header'] );
  2667. }
  2668. break;
  2669. case 'custom-background':
  2670. if ( ! did_action( 'wp_loaded' ) ) {
  2671. break;
  2672. }
  2673. $support = get_theme_support( 'custom-background' );
  2674. if ( isset( $support[0]['wp-head-callback'] ) ) {
  2675. remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  2676. }
  2677. remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
  2678. unset( $GLOBALS['custom_background'] );
  2679. break;
  2680. }
  2681. unset( $_wp_theme_features[ $feature ] );
  2682. return true;
  2683. }
  2684. /**
  2685. * Checks a theme's support for a given feature.
  2686. *
  2687. * Example usage:
  2688. *
  2689. * current_theme_supports( 'custom-logo' );
  2690. * current_theme_supports( 'html5', 'comment-form' );
  2691. *
  2692. * @since 2.9.0
  2693. * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  2694. * by adding it to the function signature.
  2695. *
  2696. * @global array $_wp_theme_features
  2697. *
  2698. * @param string $feature The feature being checked. See add_theme_support() for the list
  2699. * of possible values.
  2700. * @param mixed ...$args Optional extra arguments to be checked against certain features.
  2701. * @return bool True if the active theme supports the feature, false otherwise.
  2702. */
  2703. function current_theme_supports( $feature, ...$args ) {
  2704. global $_wp_theme_features;
  2705. if ( 'custom-header-uploads' === $feature ) {
  2706. return current_theme_supports( 'custom-header', 'uploads' );
  2707. }
  2708. if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
  2709. return false;
  2710. }
  2711. // If no args passed then no extra checks need to be performed.
  2712. if ( ! $args ) {
  2713. /** This filter is documented in wp-includes/theme.php */
  2714. return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  2715. }
  2716. switch ( $feature ) {
  2717. case 'post-thumbnails':
  2718. /*
  2719. * post-thumbnails can be registered for only certain content/post types
  2720. * by passing an array of types to add_theme_support().
  2721. * If no array was passed, then any type is accepted.
  2722. */
  2723. if ( true === $_wp_theme_features[ $feature ] ) { // Registered for all types.
  2724. return true;
  2725. }
  2726. $content_type = $args[0];
  2727. return in_array( $content_type, $_wp_theme_features[ $feature ][0], true );
  2728. case 'html5':
  2729. case 'post-formats':
  2730. /*
  2731. * Specific post formats can be registered by passing an array of types
  2732. * to add_theme_support().
  2733. *
  2734. * Specific areas of HTML5 support *must* be passed via an array to add_theme_support().
  2735. */
  2736. $type = $args[0];
  2737. return in_array( $type, $_wp_theme_features[ $feature ][0], true );
  2738. case 'custom-logo':
  2739. case 'custom-header':
  2740. case 'custom-background':
  2741. // Specific capabilities can be registered by passing an array to add_theme_support().
  2742. return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] );
  2743. }
  2744. /**
  2745. * Filters whether the active theme supports a specific feature.
  2746. *
  2747. * The dynamic portion of the hook name, `$feature`, refers to the specific
  2748. * theme feature. See add_theme_support() for the list of possible values.
  2749. *
  2750. * @since 3.4.0
  2751. *
  2752. * @param bool $supports Whether the active theme supports the given feature. Default true.
  2753. * @param array $args Array of arguments for the feature.
  2754. * @param string $feature The theme feature.
  2755. */
  2756. return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  2757. }
  2758. /**
  2759. * Checks a theme's support for a given feature before loading the functions which implement it.
  2760. *
  2761. * @since 2.9.0
  2762. *
  2763. * @param string $feature The feature being checked. See add_theme_support() for the list
  2764. * of possible values.
  2765. * @param string $include Path to the file.
  2766. * @return bool True if the active theme supports the supplied feature, false otherwise.
  2767. */
  2768. function require_if_theme_supports( $feature, $include ) {
  2769. if ( current_theme_supports( $feature ) ) {
  2770. require $include;
  2771. return true;
  2772. }
  2773. return false;
  2774. }
  2775. /**
  2776. * Registers a theme feature for use in add_theme_support().
  2777. *
  2778. * This does not indicate that the active theme supports the feature, it only describes
  2779. * the feature's supported options.
  2780. *
  2781. * @since 5.5.0
  2782. *
  2783. * @see add_theme_support()
  2784. *
  2785. * @global array $_wp_registered_theme_features
  2786. *
  2787. * @param string $feature The name uniquely identifying the feature. See add_theme_support()
  2788. * for the list of possible values.
  2789. * @param array $args {
  2790. * Data used to describe the theme.
  2791. *
  2792. * @type string $type The type of data associated with this feature.
  2793. * Valid values are 'string', 'boolean', 'integer',
  2794. * 'number', 'array', and 'object'. Defaults to 'boolean'.
  2795. * @type bool $variadic Does this feature utilize the variadic support
  2796. * of add_theme_support(), or are all arguments specified
  2797. * as the second parameter. Must be used with the "array" type.
  2798. * @type string $description A short description of the feature. Included in
  2799. * the Themes REST API schema. Intended for developers.
  2800. * @type bool|array $show_in_rest {
  2801. * Whether this feature should be included in the Themes REST API endpoint.
  2802. * Defaults to not being included. When registering an 'array' or 'object' type,
  2803. * this argument must be an array with the 'schema' key.
  2804. *
  2805. * @type array $schema Specifies the JSON Schema definition describing
  2806. * the feature. If any objects in the schema do not include
  2807. * the 'additionalProperties' keyword, it is set to false.
  2808. * @type string $name An alternate name to be used as the property name
  2809. * in the REST API.
  2810. * @type callable $prepare_callback A function used to format the theme support in the REST API.
  2811. * Receives the raw theme support value.
  2812. * }
  2813. * }
  2814. * @return true|WP_Error True if the theme feature was successfully registered, a WP_Error object if not.
  2815. */
  2816. function register_theme_feature( $feature, $args = array() ) {
  2817. global $_wp_registered_theme_features;
  2818. if ( ! is_array( $_wp_registered_theme_features ) ) {
  2819. $_wp_registered_theme_features = array();
  2820. }
  2821. $defaults = array(
  2822. 'type' => 'boolean',
  2823. 'variadic' => false,
  2824. 'description' => '',
  2825. 'show_in_rest' => false,
  2826. );
  2827. $args = wp_parse_args( $args, $defaults );
  2828. if ( true === $args['show_in_rest'] ) {
  2829. $args['show_in_rest'] = array();
  2830. }
  2831. if ( is_array( $args['show_in_rest'] ) ) {
  2832. $args['show_in_rest'] = wp_parse_args(
  2833. $args['show_in_rest'],
  2834. array(
  2835. 'schema' => array(),
  2836. 'name' => $feature,
  2837. 'prepare_callback' => null,
  2838. )
  2839. );
  2840. }
  2841. if ( ! in_array( $args['type'], array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ), true ) ) {
  2842. return new WP_Error(
  2843. 'invalid_type',
  2844. __( 'The feature "type" is not valid JSON Schema type.' )
  2845. );
  2846. }
  2847. if ( true === $args['variadic'] && 'array' !== $args['type'] ) {
  2848. return new WP_Error(
  2849. 'variadic_must_be_array',
  2850. __( 'When registering a "variadic" theme feature, the "type" must be an "array".' )
  2851. );
  2852. }
  2853. if ( false !== $args['show_in_rest'] && in_array( $args['type'], array( 'array', 'object' ), true ) ) {
  2854. if ( ! is_array( $args['show_in_rest'] ) || empty( $args['show_in_rest']['schema'] ) ) {
  2855. return new WP_Error(
  2856. 'missing_schema',
  2857. __( 'When registering an "array" or "object" feature to show in the REST API, the feature\'s schema must also be defined.' )
  2858. );
  2859. }
  2860. if ( 'array' === $args['type'] && ! isset( $args['show_in_rest']['schema']['items'] ) ) {
  2861. return new WP_Error(
  2862. 'missing_schema_items',
  2863. __( 'When registering an "array" feature, the feature\'s schema must include the "items" keyword.' )
  2864. );
  2865. }
  2866. if ( 'object' === $args['type'] && ! isset( $args['show_in_rest']['schema']['properties'] ) ) {
  2867. return new WP_Error(
  2868. 'missing_schema_properties',
  2869. __( 'When registering an "object" feature, the feature\'s schema must include the "properties" keyword.' )
  2870. );
  2871. }
  2872. }
  2873. if ( is_array( $args['show_in_rest'] ) ) {
  2874. if ( isset( $args['show_in_rest']['prepare_callback'] )
  2875. && ! is_callable( $args['show_in_rest']['prepare_callback'] )
  2876. ) {
  2877. return new WP_Error(
  2878. 'invalid_rest_prepare_callback',
  2879. sprintf(
  2880. /* translators: %s: prepare_callback */
  2881. __( 'The "%s" must be a callable function.' ),
  2882. 'prepare_callback'
  2883. )
  2884. );
  2885. }
  2886. $args['show_in_rest']['schema'] = wp_parse_args(
  2887. $args['show_in_rest']['schema'],
  2888. array(
  2889. 'description' => $args['description'],
  2890. 'type' => $args['type'],
  2891. 'default' => false,
  2892. )
  2893. );
  2894. if ( is_bool( $args['show_in_rest']['schema']['default'] )
  2895. && ! in_array( 'boolean', (array) $args['show_in_rest']['schema']['type'], true )
  2896. ) {
  2897. // Automatically include the "boolean" type when the default value is a boolean.
  2898. $args['show_in_rest']['schema']['type'] = (array) $args['show_in_rest']['schema']['type'];
  2899. array_unshift( $args['show_in_rest']['schema']['type'], 'boolean' );
  2900. }
  2901. $args['show_in_rest']['schema'] = rest_default_additional_properties_to_false( $args['show_in_rest']['schema'] );
  2902. }
  2903. $_wp_registered_theme_features[ $feature ] = $args;
  2904. return true;
  2905. }
  2906. /**
  2907. * Gets the list of registered theme features.
  2908. *
  2909. * @since 5.5.0
  2910. *
  2911. * @global array $_wp_registered_theme_features
  2912. *
  2913. * @return array[] List of theme features, keyed by their name.
  2914. */
  2915. function get_registered_theme_features() {
  2916. global $_wp_registered_theme_features;
  2917. if ( ! is_array( $_wp_registered_theme_features ) ) {
  2918. return array();
  2919. }
  2920. return $_wp_registered_theme_features;
  2921. }
  2922. /**
  2923. * Gets the registration config for a theme feature.
  2924. *
  2925. * @since 5.5.0
  2926. *
  2927. * @global array $_wp_registered_theme_features
  2928. *
  2929. * @param string $feature The feature name. See add_theme_support() for the list
  2930. * of possible values.
  2931. * @return array|null The registration args, or null if the feature was not registered.
  2932. */
  2933. function get_registered_theme_feature( $feature ) {
  2934. global $_wp_registered_theme_features;
  2935. if ( ! is_array( $_wp_registered_theme_features ) ) {
  2936. return null;
  2937. }
  2938. return isset( $_wp_registered_theme_features[ $feature ] ) ? $_wp_registered_theme_features[ $feature ] : null;
  2939. }
  2940. /**
  2941. * Checks an attachment being deleted to see if it's a header or background image.
  2942. *
  2943. * If true it removes the theme modification which would be pointing at the deleted
  2944. * attachment.
  2945. *
  2946. * @access private
  2947. * @since 3.0.0
  2948. * @since 4.3.0 Also removes `header_image_data`.
  2949. * @since 4.5.0 Also removes custom logo theme mods.
  2950. *
  2951. * @param int $id The attachment ID.
  2952. */
  2953. function _delete_attachment_theme_mod( $id ) {
  2954. $attachment_image = wp_get_attachment_url( $id );
  2955. $header_image = get_header_image();
  2956. $background_image = get_background_image();
  2957. $custom_logo_id = get_theme_mod( 'custom_logo' );
  2958. if ( $custom_logo_id && $custom_logo_id == $id ) {
  2959. remove_theme_mod( 'custom_logo' );
  2960. remove_theme_mod( 'header_text' );
  2961. }
  2962. if ( $header_image && $header_image == $attachment_image ) {
  2963. remove_theme_mod( 'header_image' );
  2964. remove_theme_mod( 'header_image_data' );
  2965. }
  2966. if ( $background_image && $background_image == $attachment_image ) {
  2967. remove_theme_mod( 'background_image' );
  2968. }
  2969. }
  2970. /**
  2971. * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load.
  2972. *
  2973. * See {@see 'after_switch_theme'}.
  2974. *
  2975. * @since 3.3.0
  2976. */
  2977. function check_theme_switched() {
  2978. $stylesheet = get_option( 'theme_switched' );
  2979. if ( $stylesheet ) {
  2980. $old_theme = wp_get_theme( $stylesheet );
  2981. // Prevent widget & menu mapping from running since Customizer already called it up front.
  2982. if ( get_option( 'theme_switched_via_customizer' ) ) {
  2983. remove_action( 'after_switch_theme', '_wp_menus_changed' );
  2984. remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
  2985. update_option( 'theme_switched_via_customizer', false );
  2986. }
  2987. if ( $old_theme->exists() ) {
  2988. /**
  2989. * Fires on the first WP load after a theme switch if the old theme still exists.
  2990. *
  2991. * This action fires multiple times and the parameters differs
  2992. * according to the context, if the old theme exists or not.
  2993. * If the old theme is missing, the parameter will be the slug
  2994. * of the old theme.
  2995. *
  2996. * @since 3.3.0
  2997. *
  2998. * @param string $old_name Old theme name.
  2999. * @param WP_Theme $old_theme WP_Theme instance of the old theme.
  3000. */
  3001. do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
  3002. } else {
  3003. /** This action is documented in wp-includes/theme.php */
  3004. do_action( 'after_switch_theme', $stylesheet, $old_theme );
  3005. }
  3006. flush_rewrite_rules();
  3007. update_option( 'theme_switched', false );
  3008. }
  3009. }
  3010. /**
  3011. * Includes and instantiates the WP_Customize_Manager class.
  3012. *
  3013. * Loads the Customizer at plugins_loaded when accessing the customize.php admin
  3014. * page or when any request includes a wp_customize=on param or a customize_changeset
  3015. * param (a UUID). This param is a signal for whether to bootstrap the Customizer when
  3016. * WordPress is loading, especially in the Customizer preview
  3017. * or when making Customizer Ajax requests for widgets or menus.
  3018. *
  3019. * @since 3.4.0
  3020. *
  3021. * @global WP_Customize_Manager $wp_customize
  3022. */
  3023. function _wp_customize_include() {
  3024. $is_customize_admin_page = ( is_admin() && 'customize.php' === basename( $_SERVER['PHP_SELF'] ) );
  3025. $should_include = (
  3026. $is_customize_admin_page
  3027. ||
  3028. ( isset( $_REQUEST['wp_customize'] ) && 'on' === $_REQUEST['wp_customize'] )
  3029. ||
  3030. ( ! empty( $_GET['customize_changeset_uuid'] ) || ! empty( $_POST['customize_changeset_uuid'] ) )
  3031. );
  3032. if ( ! $should_include ) {
  3033. return;
  3034. }
  3035. /*
  3036. * Note that wp_unslash() is not being used on the input vars because it is
  3037. * called before wp_magic_quotes() gets called. Besides this fact, none of
  3038. * the values should contain any characters needing slashes anyway.
  3039. */
  3040. $keys = array(
  3041. 'changeset_uuid',
  3042. 'customize_changeset_uuid',
  3043. 'customize_theme',
  3044. 'theme',
  3045. 'customize_messenger_channel',
  3046. 'customize_autosaved',
  3047. );
  3048. $input_vars = array_merge(
  3049. wp_array_slice_assoc( $_GET, $keys ),
  3050. wp_array_slice_assoc( $_POST, $keys )
  3051. );
  3052. $theme = null;
  3053. $autosaved = null;
  3054. $messenger_channel = null;
  3055. // Value false indicates UUID should be determined after_setup_theme
  3056. // to either re-use existing saved changeset or else generate a new UUID if none exists.
  3057. $changeset_uuid = false;
  3058. // Set initially fo false since defaults to true for back-compat;
  3059. // can be overridden via the customize_changeset_branching filter.
  3060. $branching = false;
  3061. if ( $is_customize_admin_page && isset( $input_vars['changeset_uuid'] ) ) {
  3062. $changeset_uuid = sanitize_key( $input_vars['changeset_uuid'] );
  3063. } elseif ( ! empty( $input_vars['customize_changeset_uuid'] ) ) {
  3064. $changeset_uuid = sanitize_key( $input_vars['customize_changeset_uuid'] );
  3065. }
  3066. // Note that theme will be sanitized via WP_Theme.
  3067. if ( $is_customize_admin_page && isset( $input_vars['theme'] ) ) {
  3068. $theme = $input_vars['theme'];
  3069. } elseif ( isset( $input_vars['customize_theme'] ) ) {
  3070. $theme = $input_vars['customize_theme'];
  3071. }
  3072. if ( ! empty( $input_vars['customize_autosaved'] ) ) {
  3073. $autosaved = true;
  3074. }
  3075. if ( isset( $input_vars['customize_messenger_channel'] ) ) {
  3076. $messenger_channel = sanitize_key( $input_vars['customize_messenger_channel'] );
  3077. }
  3078. /*
  3079. * Note that settings must be previewed even outside the customizer preview
  3080. * and also in the customizer pane itself. This is to enable loading an existing
  3081. * changeset into the customizer. Previewing the settings only has to be prevented
  3082. * here in the case of a customize_save action because this will cause WP to think
  3083. * there is nothing changed that needs to be saved.
  3084. */
  3085. $is_customize_save_action = (
  3086. wp_doing_ajax()
  3087. &&
  3088. isset( $_REQUEST['action'] )
  3089. &&
  3090. 'customize_save' === wp_unslash( $_REQUEST['action'] )
  3091. );
  3092. $settings_previewed = ! $is_customize_save_action;
  3093. require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
  3094. $GLOBALS['wp_customize'] = new WP_Customize_Manager(
  3095. compact(
  3096. 'changeset_uuid',
  3097. 'theme',
  3098. 'messenger_channel',
  3099. 'settings_previewed',
  3100. 'autosaved',
  3101. 'branching'
  3102. )
  3103. );
  3104. }
  3105. /**
  3106. * Publishes a snapshot's changes.
  3107. *
  3108. * @since 4.7.0
  3109. * @access private
  3110. *
  3111. * @global wpdb $wpdb WordPress database abstraction object.
  3112. * @global WP_Customize_Manager $wp_customize Customizer instance.
  3113. *
  3114. * @param string $new_status New post status.
  3115. * @param string $old_status Old post status.
  3116. * @param WP_Post $changeset_post Changeset post object.
  3117. */
  3118. function _wp_customize_publish_changeset( $new_status, $old_status, $changeset_post ) {
  3119. global $wp_customize, $wpdb;
  3120. $is_publishing_changeset = (
  3121. 'customize_changeset' === $changeset_post->post_type
  3122. &&
  3123. 'publish' === $new_status
  3124. &&
  3125. 'publish' !== $old_status
  3126. );
  3127. if ( ! $is_publishing_changeset ) {
  3128. return;
  3129. }
  3130. if ( empty( $wp_customize ) ) {
  3131. require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
  3132. $wp_customize = new WP_Customize_Manager(
  3133. array(
  3134. 'changeset_uuid' => $changeset_post->post_name,
  3135. 'settings_previewed' => false,
  3136. )
  3137. );
  3138. }
  3139. if ( ! did_action( 'customize_register' ) ) {
  3140. /*
  3141. * When running from CLI or Cron, the customize_register action will need
  3142. * to be triggered in order for core, themes, and plugins to register their
  3143. * settings. Normally core will add_action( 'customize_register' ) at
  3144. * priority 10 to register the core settings, and if any themes/plugins
  3145. * also add_action( 'customize_register' ) at the same priority, they
  3146. * will have a $wp_customize with those settings registered since they
  3147. * call add_action() afterward, normally. However, when manually doing
  3148. * the customize_register action after the setup_theme, then the order
  3149. * will be reversed for two actions added at priority 10, resulting in
  3150. * the core settings no longer being available as expected to themes/plugins.
  3151. * So the following manually calls the method that registers the core
  3152. * settings up front before doing the action.
  3153. */
  3154. remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) );
  3155. $wp_customize->register_controls();
  3156. /** This filter is documented in /wp-includes/class-wp-customize-manager.php */
  3157. do_action( 'customize_register', $wp_customize );
  3158. }
  3159. $wp_customize->_publish_changeset_values( $changeset_post->ID );
  3160. /*
  3161. * Trash the changeset post if revisions are not enabled. Unpublished
  3162. * changesets by default get garbage collected due to the auto-draft status.
  3163. * When a changeset post is published, however, it would no longer get cleaned
  3164. * out. This is a problem when the changeset posts are never displayed anywhere,
  3165. * since they would just be endlessly piling up. So here we use the revisions
  3166. * feature to indicate whether or not a published changeset should get trashed
  3167. * and thus garbage collected.
  3168. */
  3169. if ( ! wp_revisions_enabled( $changeset_post ) ) {
  3170. $wp_customize->trash_changeset_post( $changeset_post->ID );
  3171. }
  3172. }
  3173. /**
  3174. * Filters changeset post data upon insert to ensure post_name is intact.
  3175. *
  3176. * This is needed to prevent the post_name from being dropped when the post is
  3177. * transitioned into pending status by a contributor.
  3178. *
  3179. * @since 4.7.0
  3180. *
  3181. * @see wp_insert_post()
  3182. *
  3183. * @param array $post_data An array of slashed post data.
  3184. * @param array $supplied_post_data An array of sanitized, but otherwise unmodified post data.
  3185. * @return array Filtered data.
  3186. */
  3187. function _wp_customize_changeset_filter_insert_post_data( $post_data, $supplied_post_data ) {
  3188. if ( isset( $post_data['post_type'] ) && 'customize_changeset' === $post_data['post_type'] ) {
  3189. // Prevent post_name from being dropped, such as when contributor saves a changeset post as pending.
  3190. if ( empty( $post_data['post_name'] ) && ! empty( $supplied_post_data['post_name'] ) ) {
  3191. $post_data['post_name'] = $supplied_post_data['post_name'];
  3192. }
  3193. }
  3194. return $post_data;
  3195. }
  3196. /**
  3197. * Adds settings for the customize-loader script.
  3198. *
  3199. * @since 3.4.0
  3200. */
  3201. function _wp_customize_loader_settings() {
  3202. $admin_origin = parse_url( admin_url() );
  3203. $home_origin = parse_url( home_url() );
  3204. $cross_domain = ( strtolower( $admin_origin['host'] ) != strtolower( $home_origin['host'] ) );
  3205. $browser = array(
  3206. 'mobile' => wp_is_mobile(),
  3207. 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
  3208. );
  3209. $settings = array(
  3210. 'url' => esc_url( admin_url( 'customize.php' ) ),
  3211. 'isCrossDomain' => $cross_domain,
  3212. 'browser' => $browser,
  3213. 'l10n' => array(
  3214. 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),
  3215. 'mainIframeTitle' => __( 'Customizer' ),
  3216. ),
  3217. );
  3218. $script = 'var _wpCustomizeLoaderSettings = ' . wp_json_encode( $settings ) . ';';
  3219. $wp_scripts = wp_scripts();
  3220. $data = $wp_scripts->get_data( 'customize-loader', 'data' );
  3221. if ( $data ) {
  3222. $script = "$data\n$script";
  3223. }
  3224. $wp_scripts->add_data( 'customize-loader', 'data', $script );
  3225. }
  3226. /**
  3227. * Returns a URL to load the Customizer.
  3228. *
  3229. * @since 3.4.0
  3230. *
  3231. * @param string $stylesheet Optional. Theme to customize. Defaults to active theme.
  3232. * The theme's stylesheet will be urlencoded if necessary.
  3233. * @return string
  3234. */
  3235. function wp_customize_url( $stylesheet = '' ) {
  3236. $url = admin_url( 'customize.php' );
  3237. if ( $stylesheet ) {
  3238. $url .= '?theme=' . urlencode( $stylesheet );
  3239. }
  3240. return esc_url( $url );
  3241. }
  3242. /**
  3243. * Prints a script to check whether or not the Customizer is supported,
  3244. * and apply either the no-customize-support or customize-support class
  3245. * to the body.
  3246. *
  3247. * This function MUST be called inside the body tag.
  3248. *
  3249. * Ideally, call this function immediately after the body tag is opened.
  3250. * This prevents a flash of unstyled content.
  3251. *
  3252. * It is also recommended that you add the "no-customize-support" class
  3253. * to the body tag by default.
  3254. *
  3255. * @since 3.4.0
  3256. * @since 4.7.0 Support for IE8 and below is explicitly removed via conditional comments.
  3257. * @since 5.5.0 IE8 and older are no longer supported.
  3258. */
  3259. function wp_customize_support_script() {
  3260. $admin_origin = parse_url( admin_url() );
  3261. $home_origin = parse_url( home_url() );
  3262. $cross_domain = ( strtolower( $admin_origin['host'] ) != strtolower( $home_origin['host'] ) );
  3263. $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  3264. ?>
  3265. <script<?php echo $type_attr; ?>>
  3266. (function() {
  3267. var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
  3268. <?php if ( $cross_domain ) : ?>
  3269. request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
  3270. <?php else : ?>
  3271. request = true;
  3272. <?php endif; ?>
  3273. b[c] = b[c].replace( rcs, ' ' );
  3274. // The customizer requires postMessage and CORS (if the site is cross domain).
  3275. b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
  3276. }());
  3277. </script>
  3278. <?php
  3279. }
  3280. /**
  3281. * Whether the site is being previewed in the Customizer.
  3282. *
  3283. * @since 4.0.0
  3284. *
  3285. * @global WP_Customize_Manager $wp_customize Customizer instance.
  3286. *
  3287. * @return bool True if the site is being previewed in the Customizer, false otherwise.
  3288. */
  3289. function is_customize_preview() {
  3290. global $wp_customize;
  3291. return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
  3292. }
  3293. /**
  3294. * Makes sure that auto-draft posts get their post_date bumped or status changed
  3295. * to draft to prevent premature garbage-collection.
  3296. *
  3297. * When a changeset is updated but remains an auto-draft, ensure the post_date
  3298. * for the auto-draft posts remains the same so that it will be
  3299. * garbage-collected at the same time by `wp_delete_auto_drafts()`. Otherwise,
  3300. * if the changeset is updated to be a draft then update the posts
  3301. * to have a far-future post_date so that they will never be garbage collected
  3302. * unless the changeset post itself is deleted.
  3303. *
  3304. * When a changeset is updated to be a persistent draft or to be scheduled for
  3305. * publishing, then transition any dependent auto-drafts to a draft status so
  3306. * that they likewise will not be garbage-collected but also so that they can
  3307. * be edited in the admin before publishing since there is not yet a post/page
  3308. * editing flow in the Customizer. See #39752.
  3309. *
  3310. * @link https://core.trac.wordpress.org/ticket/39752
  3311. *
  3312. * @since 4.8.0
  3313. * @access private
  3314. * @see wp_delete_auto_drafts()
  3315. *
  3316. * @global wpdb $wpdb WordPress database abstraction object.
  3317. *
  3318. * @param string $new_status Transition to this post status.
  3319. * @param string $old_status Previous post status.
  3320. * @param \WP_Post $post Post data.
  3321. */
  3322. function _wp_keep_alive_customize_changeset_dependent_auto_drafts( $new_status, $old_status, $post ) {
  3323. global $wpdb;
  3324. unset( $old_status );
  3325. // Short-circuit if not a changeset or if the changeset was published.
  3326. if ( 'customize_changeset' !== $post->post_type || 'publish' === $new_status ) {
  3327. return;
  3328. }
  3329. $data = json_decode( $post->post_content, true );
  3330. if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
  3331. return;
  3332. }
  3333. /*
  3334. * Actually, in lieu of keeping alive, trash any customization drafts here if the changeset itself is
  3335. * getting trashed. This is needed because when a changeset transitions to a draft, then any of the
  3336. * dependent auto-draft post/page stubs will also get transitioned to customization drafts which
  3337. * are then visible in the WP Admin. We cannot wait for the deletion of the changeset in which
  3338. * _wp_delete_customize_changeset_dependent_auto_drafts() will be called, since they need to be
  3339. * trashed to remove from visibility immediately.
  3340. */
  3341. if ( 'trash' === $new_status ) {
  3342. foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
  3343. if ( ! empty( $post_id ) && 'draft' === get_post_status( $post_id ) ) {
  3344. wp_trash_post( $post_id );
  3345. }
  3346. }
  3347. return;
  3348. }
  3349. $post_args = array();
  3350. if ( 'auto-draft' === $new_status ) {
  3351. /*
  3352. * Keep the post date for the post matching the changeset
  3353. * so that it will not be garbage-collected before the changeset.
  3354. */
  3355. $post_args['post_date'] = $post->post_date; // Note wp_delete_auto_drafts() only looks at this date.
  3356. } else {
  3357. /*
  3358. * Since the changeset no longer has an auto-draft (and it is not published)
  3359. * it is now a persistent changeset, a long-lived draft, and so any
  3360. * associated auto-draft posts should likewise transition into having a draft
  3361. * status. These drafts will be treated differently than regular drafts in
  3362. * that they will be tied to the given changeset. The publish meta box is
  3363. * replaced with a notice about how the post is part of a set of customized changes
  3364. * which will be published when the changeset is published.
  3365. */
  3366. $post_args['post_status'] = 'draft';
  3367. }
  3368. foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
  3369. if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) {
  3370. continue;
  3371. }
  3372. $wpdb->update(
  3373. $wpdb->posts,
  3374. $post_args,
  3375. array( 'ID' => $post_id )
  3376. );
  3377. clean_post_cache( $post_id );
  3378. }
  3379. }
  3380. /**
  3381. * Creates the initial theme features when the 'setup_theme' action is fired.
  3382. *
  3383. * See {@see 'setup_theme'}.
  3384. *
  3385. * @since 5.5.0
  3386. */
  3387. function create_initial_theme_features() {
  3388. register_theme_feature(
  3389. 'align-wide',
  3390. array(
  3391. 'description' => __( 'Whether theme opts in to wide alignment CSS class.' ),
  3392. 'show_in_rest' => true,
  3393. )
  3394. );
  3395. register_theme_feature(
  3396. 'automatic-feed-links',
  3397. array(
  3398. 'description' => __( 'Whether posts and comments RSS feed links are added to head.' ),
  3399. 'show_in_rest' => true,
  3400. )
  3401. );
  3402. register_theme_feature(
  3403. 'custom-background',
  3404. array(
  3405. 'description' => __( 'Custom background if defined by the theme.' ),
  3406. 'type' => 'object',
  3407. 'show_in_rest' => array(
  3408. 'schema' => array(
  3409. 'properties' => array(
  3410. 'default-image' => array(
  3411. 'type' => 'string',
  3412. 'format' => 'uri',
  3413. ),
  3414. 'default-preset' => array(
  3415. 'type' => 'string',
  3416. 'enum' => array(
  3417. 'default',
  3418. 'fill',
  3419. 'fit',
  3420. 'repeat',
  3421. 'custom',
  3422. ),
  3423. ),
  3424. 'default-position-x' => array(
  3425. 'type' => 'string',
  3426. 'enum' => array(
  3427. 'left',
  3428. 'center',
  3429. 'right',
  3430. ),
  3431. ),
  3432. 'default-position-y' => array(
  3433. 'type' => 'string',
  3434. 'enum' => array(
  3435. 'left',
  3436. 'center',
  3437. 'right',
  3438. ),
  3439. ),
  3440. 'default-size' => array(
  3441. 'type' => 'string',
  3442. 'enum' => array(
  3443. 'auto',
  3444. 'contain',
  3445. 'cover',
  3446. ),
  3447. ),
  3448. 'default-repeat' => array(
  3449. 'type' => 'string',
  3450. 'enum' => array(
  3451. 'repeat-x',
  3452. 'repeat-y',
  3453. 'repeat',
  3454. 'no-repeat',
  3455. ),
  3456. ),
  3457. 'default-attachment' => array(
  3458. 'type' => 'string',
  3459. 'enum' => array(
  3460. 'scroll',
  3461. 'fixed',
  3462. ),
  3463. ),
  3464. 'default-color' => array(
  3465. 'type' => 'string',
  3466. ),
  3467. ),
  3468. ),
  3469. ),
  3470. )
  3471. );
  3472. register_theme_feature(
  3473. 'custom-header',
  3474. array(
  3475. 'description' => __( 'Custom header if defined by the theme.' ),
  3476. 'type' => 'object',
  3477. 'show_in_rest' => array(
  3478. 'schema' => array(
  3479. 'properties' => array(
  3480. 'default-image' => array(
  3481. 'type' => 'string',
  3482. 'format' => 'uri',
  3483. ),
  3484. 'random-default' => array(
  3485. 'type' => 'boolean',
  3486. ),
  3487. 'width' => array(
  3488. 'type' => 'integer',
  3489. ),
  3490. 'height' => array(
  3491. 'type' => 'integer',
  3492. ),
  3493. 'flex-height' => array(
  3494. 'type' => 'boolean',
  3495. ),
  3496. 'flex-width' => array(
  3497. 'type' => 'boolean',
  3498. ),
  3499. 'default-text-color' => array(
  3500. 'type' => 'string',
  3501. ),
  3502. 'header-text' => array(
  3503. 'type' => 'boolean',
  3504. ),
  3505. 'uploads' => array(
  3506. 'type' => 'boolean',
  3507. ),
  3508. 'video' => array(
  3509. 'type' => 'boolean',
  3510. ),
  3511. ),
  3512. ),
  3513. ),
  3514. )
  3515. );
  3516. register_theme_feature(
  3517. 'custom-logo',
  3518. array(
  3519. 'type' => 'object',
  3520. 'description' => __( 'Custom logo if defined by the theme.' ),
  3521. 'show_in_rest' => array(
  3522. 'schema' => array(
  3523. 'properties' => array(
  3524. 'width' => array(
  3525. 'type' => 'integer',
  3526. ),
  3527. 'height' => array(
  3528. 'type' => 'integer',
  3529. ),
  3530. 'flex-width' => array(
  3531. 'type' => 'boolean',
  3532. ),
  3533. 'flex-height' => array(
  3534. 'type' => 'boolean',
  3535. ),
  3536. 'header-text' => array(
  3537. 'type' => 'array',
  3538. 'items' => array(
  3539. 'type' => 'string',
  3540. ),
  3541. ),
  3542. 'unlink-homepage-logo' => array(
  3543. 'type' => 'boolean',
  3544. ),
  3545. ),
  3546. ),
  3547. ),
  3548. )
  3549. );
  3550. register_theme_feature(
  3551. 'customize-selective-refresh-widgets',
  3552. array(
  3553. 'description' => __( 'Whether the theme enables Selective Refresh for Widgets being managed with the Customizer.' ),
  3554. 'show_in_rest' => true,
  3555. )
  3556. );
  3557. register_theme_feature(
  3558. 'dark-editor-style',
  3559. array(
  3560. 'description' => __( 'Whether theme opts in to the dark editor style UI.' ),
  3561. 'show_in_rest' => true,
  3562. )
  3563. );
  3564. register_theme_feature(
  3565. 'disable-custom-colors',
  3566. array(
  3567. 'description' => __( 'Whether the theme disables custom colors.' ),
  3568. 'show_in_rest' => true,
  3569. )
  3570. );
  3571. register_theme_feature(
  3572. 'disable-custom-font-sizes',
  3573. array(
  3574. 'description' => __( 'Whether the theme disables custom font sizes.' ),
  3575. 'show_in_rest' => true,
  3576. )
  3577. );
  3578. register_theme_feature(
  3579. 'disable-custom-gradients',
  3580. array(
  3581. 'description' => __( 'Whether the theme disables custom gradients.' ),
  3582. 'show_in_rest' => true,
  3583. )
  3584. );
  3585. register_theme_feature(
  3586. 'editor-color-palette',
  3587. array(
  3588. 'type' => 'array',
  3589. 'description' => __( 'Custom color palette if defined by the theme.' ),
  3590. 'show_in_rest' => array(
  3591. 'schema' => array(
  3592. 'items' => array(
  3593. 'type' => 'object',
  3594. 'properties' => array(
  3595. 'name' => array(
  3596. 'type' => 'string',
  3597. ),
  3598. 'slug' => array(
  3599. 'type' => 'string',
  3600. ),
  3601. 'color' => array(
  3602. 'type' => 'string',
  3603. ),
  3604. ),
  3605. ),
  3606. ),
  3607. ),
  3608. )
  3609. );
  3610. register_theme_feature(
  3611. 'editor-font-sizes',
  3612. array(
  3613. 'type' => 'array',
  3614. 'description' => __( 'Custom font sizes if defined by the theme.' ),
  3615. 'show_in_rest' => array(
  3616. 'schema' => array(
  3617. 'items' => array(
  3618. 'type' => 'object',
  3619. 'properties' => array(
  3620. 'name' => array(
  3621. 'type' => 'string',
  3622. ),
  3623. 'size' => array(
  3624. 'type' => 'number',
  3625. ),
  3626. 'slug' => array(
  3627. 'type' => 'string',
  3628. ),
  3629. ),
  3630. ),
  3631. ),
  3632. ),
  3633. )
  3634. );
  3635. register_theme_feature(
  3636. 'editor-gradient-presets',
  3637. array(
  3638. 'type' => 'array',
  3639. 'description' => __( 'Custom gradient presets if defined by the theme.' ),
  3640. 'show_in_rest' => array(
  3641. 'schema' => array(
  3642. 'items' => array(
  3643. 'type' => 'object',
  3644. 'properties' => array(
  3645. 'name' => array(
  3646. 'type' => 'string',
  3647. ),
  3648. 'gradient' => array(
  3649. 'type' => 'string',
  3650. ),
  3651. 'slug' => array(
  3652. 'type' => 'string',
  3653. ),
  3654. ),
  3655. ),
  3656. ),
  3657. ),
  3658. )
  3659. );
  3660. register_theme_feature(
  3661. 'editor-styles',
  3662. array(
  3663. 'description' => __( 'Whether theme opts in to the editor styles CSS wrapper.' ),
  3664. 'show_in_rest' => true,
  3665. )
  3666. );
  3667. register_theme_feature(
  3668. 'html5',
  3669. array(
  3670. 'type' => 'array',
  3671. 'description' => __( 'Allows use of HTML5 markup for search forms, comment forms, comment lists, gallery, and caption.' ),
  3672. 'show_in_rest' => array(
  3673. 'schema' => array(
  3674. 'items' => array(
  3675. 'type' => 'string',
  3676. 'enum' => array(
  3677. 'search-form',
  3678. 'comment-form',
  3679. 'comment-list',
  3680. 'gallery',
  3681. 'caption',
  3682. 'script',
  3683. 'style',
  3684. ),
  3685. ),
  3686. ),
  3687. ),
  3688. )
  3689. );
  3690. register_theme_feature(
  3691. 'post-formats',
  3692. array(
  3693. 'type' => 'array',
  3694. 'description' => __( 'Post formats supported.' ),
  3695. 'show_in_rest' => array(
  3696. 'name' => 'formats',
  3697. 'schema' => array(
  3698. 'items' => array(
  3699. 'type' => 'string',
  3700. 'enum' => get_post_format_slugs(),
  3701. ),
  3702. 'default' => array( 'standard' ),
  3703. ),
  3704. 'prepare_callback' => static function ( $formats ) {
  3705. $formats = is_array( $formats ) ? array_values( $formats[0] ) : array();
  3706. $formats = array_merge( array( 'standard' ), $formats );
  3707. return $formats;
  3708. },
  3709. ),
  3710. )
  3711. );
  3712. register_theme_feature(
  3713. 'post-thumbnails',
  3714. array(
  3715. 'type' => 'array',
  3716. 'description' => __( 'The post types that support thumbnails or true if all post types are supported.' ),
  3717. 'show_in_rest' => array(
  3718. 'type' => array( 'boolean', 'array' ),
  3719. 'schema' => array(
  3720. 'items' => array(
  3721. 'type' => 'string',
  3722. ),
  3723. ),
  3724. ),
  3725. )
  3726. );
  3727. register_theme_feature(
  3728. 'responsive-embeds',
  3729. array(
  3730. 'description' => __( 'Whether the theme supports responsive embedded content.' ),
  3731. 'show_in_rest' => true,
  3732. )
  3733. );
  3734. register_theme_feature(
  3735. 'title-tag',
  3736. array(
  3737. 'description' => __( 'Whether the theme can manage the document title tag.' ),
  3738. 'show_in_rest' => true,
  3739. )
  3740. );
  3741. register_theme_feature(
  3742. 'wp-block-styles',
  3743. array(
  3744. 'description' => __( 'Whether theme opts in to default WordPress block styles for viewing.' ),
  3745. 'show_in_rest' => true,
  3746. )
  3747. );
  3748. }
  3749. /**
  3750. * Returns whether the active theme is a block-based theme or not.
  3751. *
  3752. * @since 5.9.0
  3753. *
  3754. * @return boolean Whether the active theme is a block-based theme or not.
  3755. */
  3756. function wp_is_block_theme() {
  3757. return wp_get_theme()->is_block_theme();
  3758. }
  3759. /**
  3760. * Adds default theme supports for block themes when the 'setup_theme' action fires.
  3761. *
  3762. * See {@see 'setup_theme'}.
  3763. *
  3764. * @since 5.9.0
  3765. * @access private
  3766. */
  3767. function _add_default_theme_supports() {
  3768. if ( ! wp_is_block_theme() ) {
  3769. return;
  3770. }
  3771. add_theme_support( 'post-thumbnails' );
  3772. add_theme_support( 'responsive-embeds' );
  3773. add_theme_support( 'editor-styles' );
  3774. /*
  3775. * Makes block themes support HTML5 by default for the comment block and search form
  3776. * (which use default template functions) and `[caption]` and `[gallery]` shortcodes.
  3777. * Other blocks contain their own HTML5 markup.
  3778. */
  3779. add_theme_support( 'html5', array( 'comment-form', 'comment-list', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
  3780. add_theme_support( 'automatic-feed-links' );
  3781. add_filter( 'should_load_separate_core_block_assets', '__return_true' );
  3782. }