Brak opisu

theme.php 122KB

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