theme.php 102 KB

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