PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/theme.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 1730 lines | 825 code | 214 blank | 691 comment | 175 complexity | 310dc738d1b8bb76f35a82f873ac9fd3 MD5 | raw file
  1. <?php
  2. /**
  3. * Theme, template, and stylesheet functions.
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Whether a child theme is in use.
  10. *
  11. * @since 3.0.0
  12. *
  13. * @return bool true if a child theme is in use, false otherwise.
  14. **/
  15. function is_child_theme() {
  16. return ( TEMPLATEPATH !== STYLESHEETPATH );
  17. }
  18. /**
  19. * Retrieve name of the current stylesheet.
  20. *
  21. * The theme name that the administrator has currently set the front end theme
  22. * as.
  23. *
  24. * For all extensive purposes, the template name and the stylesheet name are
  25. * going to be the same for most cases.
  26. *
  27. * @since 1.5.0
  28. * @uses apply_filters() Calls 'stylesheet' filter on stylesheet name.
  29. *
  30. * @return string Stylesheet name.
  31. */
  32. function get_stylesheet() {
  33. return apply_filters('stylesheet', get_option('stylesheet'));
  34. }
  35. /**
  36. * Retrieve stylesheet directory path for current theme.
  37. *
  38. * @since 1.5.0
  39. * @uses apply_filters() Calls 'stylesheet_directory' filter on stylesheet directory and theme name.
  40. *
  41. * @return string Path to current theme directory.
  42. */
  43. function get_stylesheet_directory() {
  44. $stylesheet = get_stylesheet();
  45. $theme_root = get_theme_root( $stylesheet );
  46. $stylesheet_dir = "$theme_root/$stylesheet";
  47. return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
  48. }
  49. /**
  50. * Retrieve stylesheet directory URI.
  51. *
  52. * @since 1.5.0
  53. *
  54. * @return string
  55. */
  56. function get_stylesheet_directory_uri() {
  57. $stylesheet = get_stylesheet();
  58. $theme_root_uri = get_theme_root_uri( $stylesheet );
  59. $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
  60. return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
  61. }
  62. /**
  63. * Retrieve URI of current theme stylesheet.
  64. *
  65. * The stylesheet file name is 'style.css' which is appended to {@link
  66. * get_stylesheet_directory_uri() stylesheet directory URI} path.
  67. *
  68. * @since 1.5.0
  69. * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
  70. *
  71. * @return string
  72. */
  73. function get_stylesheet_uri() {
  74. $stylesheet_dir_uri = get_stylesheet_directory_uri();
  75. $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
  76. return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
  77. }
  78. /**
  79. * Retrieve localized stylesheet URI.
  80. *
  81. * The stylesheet directory for the localized stylesheet files are located, by
  82. * default, in the base theme directory. The name of the locale file will be the
  83. * locale followed by '.css'. If that does not exist, then the text direction
  84. * stylesheet will be checked for existence, for example 'ltr.css'.
  85. *
  86. * The theme may change the location of the stylesheet directory by either using
  87. * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
  88. * If you want to change the location of the stylesheet files for the entire
  89. * WordPress workflow, then change the former. If you just have the locale in a
  90. * separate folder, then change the latter.
  91. *
  92. * @since 2.1.0
  93. * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
  94. *
  95. * @return string
  96. */
  97. function get_locale_stylesheet_uri() {
  98. global $wp_locale;
  99. $stylesheet_dir_uri = get_stylesheet_directory_uri();
  100. $dir = get_stylesheet_directory();
  101. $locale = get_locale();
  102. if ( file_exists("$dir/$locale.css") )
  103. $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
  104. elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
  105. $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
  106. else
  107. $stylesheet_uri = '';
  108. return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
  109. }
  110. /**
  111. * Retrieve name of the current theme.
  112. *
  113. * @since 1.5.0
  114. * @uses apply_filters() Calls 'template' filter on template option.
  115. *
  116. * @return string Template name.
  117. */
  118. function get_template() {
  119. return apply_filters('template', get_option('template'));
  120. }
  121. /**
  122. * Retrieve current theme directory.
  123. *
  124. * @since 1.5.0
  125. * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
  126. *
  127. * @return string Template directory path.
  128. */
  129. function get_template_directory() {
  130. $template = get_template();
  131. $theme_root = get_theme_root( $template );
  132. $template_dir = "$theme_root/$template";
  133. return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
  134. }
  135. /**
  136. * Retrieve theme directory URI.
  137. *
  138. * @since 1.5.0
  139. * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
  140. *
  141. * @return string Template directory URI.
  142. */
  143. function get_template_directory_uri() {
  144. $template = get_template();
  145. $theme_root_uri = get_theme_root_uri( $template );
  146. $template_dir_uri = "$theme_root_uri/$template";
  147. return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
  148. }
  149. /**
  150. * Retrieve theme data from parsed theme file.
  151. *
  152. * The description will have the tags filtered with the following HTML elements
  153. * whitelisted. The <b>'a'</b> element with the <em>href</em> and <em>title</em>
  154. * attributes. The <b>abbr</b> element with the <em>title</em> attribute. The
  155. * <b>acronym<b> element with the <em>title</em> attribute allowed. The
  156. * <b>code</b>, <b>em</b>, and <b>strong</b> elements also allowed.
  157. *
  158. * The style.css file must contain theme name, theme URI, and description. The
  159. * data can also contain author URI, author, template (parent template),
  160. * version, status, and finally tags. Some of these are not used by WordPress
  161. * administration panels, but are used by theme directory web sites which list
  162. * the theme.
  163. *
  164. * @since 1.5.0
  165. *
  166. * @param string $theme_file Theme file path.
  167. * @return array Theme data.
  168. */
  169. function get_theme_data( $theme_file ) {
  170. $default_headers = array(
  171. 'Name' => 'Theme Name',
  172. 'URI' => 'Theme URI',
  173. 'Description' => 'Description',
  174. 'Author' => 'Author',
  175. 'AuthorURI' => 'Author URI',
  176. 'Version' => 'Version',
  177. 'Template' => 'Template',
  178. 'Status' => 'Status',
  179. 'Tags' => 'Tags'
  180. );
  181. $themes_allowed_tags = array(
  182. 'a' => array(
  183. 'href' => array(),'title' => array()
  184. ),
  185. 'abbr' => array(
  186. 'title' => array()
  187. ),
  188. 'acronym' => array(
  189. 'title' => array()
  190. ),
  191. 'code' => array(),
  192. 'em' => array(),
  193. 'strong' => array()
  194. );
  195. $theme_data = get_file_data( $theme_file, $default_headers, 'theme' );
  196. $theme_data['Name'] = $theme_data['Title'] = wp_kses( $theme_data['Name'], $themes_allowed_tags );
  197. $theme_data['URI'] = esc_url( $theme_data['URI'] );
  198. $theme_data['Description'] = wptexturize( wp_kses( $theme_data['Description'], $themes_allowed_tags ) );
  199. $theme_data['AuthorURI'] = esc_url( $theme_data['AuthorURI'] );
  200. $theme_data['Template'] = wp_kses( $theme_data['Template'], $themes_allowed_tags );
  201. $theme_data['Version'] = wp_kses( $theme_data['Version'], $themes_allowed_tags );
  202. if ( $theme_data['Status'] == '' )
  203. $theme_data['Status'] = 'publish';
  204. else
  205. $theme_data['Status'] = wp_kses( $theme_data['Status'], $themes_allowed_tags );
  206. if ( $theme_data['Tags'] == '' )
  207. $theme_data['Tags'] = array();
  208. else
  209. $theme_data['Tags'] = array_map( 'trim', explode( ',', wp_kses( $theme_data['Tags'], array() ) ) );
  210. if ( $theme_data['Author'] == '' ) {
  211. $theme_data['Author'] = $theme_data['AuthorName'] = __('Anonymous');
  212. } else {
  213. $theme_data['AuthorName'] = wp_kses( $theme_data['Author'], $themes_allowed_tags );
  214. if ( empty( $theme_data['AuthorURI'] ) ) {
  215. $theme_data['Author'] = $theme_data['AuthorName'];
  216. } else {
  217. $theme_data['Author'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $theme_data['AuthorURI'], __( 'Visit author homepage' ), $theme_data['AuthorName'] );
  218. }
  219. }
  220. return $theme_data;
  221. }
  222. /**
  223. * Retrieve list of themes with theme data in theme directory.
  224. *
  225. * The theme is broken, if it doesn't have a parent theme and is missing either
  226. * style.css and, or index.php. If the theme has a parent theme then it is
  227. * broken, if it is missing style.css; index.php is optional. The broken theme
  228. * list is saved in the {@link $wp_broken_themes} global, which is displayed on
  229. * the theme list in the administration panels.
  230. *
  231. * @since 1.5.0
  232. * @global array $wp_broken_themes Stores the broken themes.
  233. * @global array $wp_themes Stores the working themes.
  234. *
  235. * @return array Theme list with theme data.
  236. */
  237. function get_themes() {
  238. global $wp_themes, $wp_broken_themes;
  239. if ( isset($wp_themes) )
  240. return $wp_themes;
  241. /* Register the default root as a theme directory */
  242. register_theme_directory( get_theme_root() );
  243. if ( !$theme_files = search_theme_directories() )
  244. return false;
  245. asort( $theme_files );
  246. $wp_themes = array();
  247. foreach ( (array) $theme_files as $theme_file ) {
  248. $theme_root = $theme_file['theme_root'];
  249. $theme_file = $theme_file['theme_file'];
  250. if ( !is_readable("$theme_root/$theme_file") ) {
  251. $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
  252. continue;
  253. }
  254. $theme_data = get_theme_data("$theme_root/$theme_file");
  255. $name = $theme_data['Name'];
  256. $title = $theme_data['Title'];
  257. $description = wptexturize($theme_data['Description']);
  258. $version = $theme_data['Version'];
  259. $author = $theme_data['Author'];
  260. $template = $theme_data['Template'];
  261. $stylesheet = dirname($theme_file);
  262. $screenshot = false;
  263. foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
  264. if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
  265. $screenshot = "screenshot.$ext";
  266. break;
  267. }
  268. }
  269. if ( empty($name) ) {
  270. $name = dirname($theme_file);
  271. $title = $name;
  272. }
  273. $parent_template = $template;
  274. if ( empty($template) ) {
  275. if ( file_exists("$theme_root/$stylesheet/index.php") )
  276. $template = $stylesheet;
  277. else
  278. continue;
  279. }
  280. $template = trim( $template );
  281. if ( !file_exists("$theme_root/$template/index.php") ) {
  282. $parent_dir = dirname(dirname($theme_file));
  283. if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
  284. $template = "$parent_dir/$template";
  285. $template_directory = "$theme_root/$template";
  286. } else {
  287. /**
  288. * The parent theme doesn't exist in the current theme's folder or sub folder
  289. * so lets use the theme root for the parent template.
  290. */
  291. if ( isset($theme_files[$template]) && file_exists( $theme_files[$template]['theme_root'] . "/$template/index.php" ) ) {
  292. $template_directory = $theme_files[$template]['theme_root'] . "/$template";
  293. } else {
  294. if ( empty( $parent_template) )
  295. $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'), 'error' => 'no_template');
  296. else
  297. $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => sprintf( __('The parent theme is missing. Please install the "%s" parent theme.'), $parent_template ), 'error' => 'no_parent', 'parent' => $parent_template );
  298. continue;
  299. }
  300. }
  301. } else {
  302. $template_directory = trim( $theme_root . '/' . $template );
  303. }
  304. $stylesheet_files = array();
  305. $template_files = array();
  306. $stylesheet_dir = @ dir("$theme_root/$stylesheet");
  307. if ( $stylesheet_dir ) {
  308. while ( ($file = $stylesheet_dir->read()) !== false ) {
  309. if ( !preg_match('|^\.+$|', $file) ) {
  310. if ( preg_match('|\.css$|', $file) )
  311. $stylesheet_files[] = "$theme_root/$stylesheet/$file";
  312. elseif ( preg_match('|\.php$|', $file) )
  313. $template_files[] = "$theme_root/$stylesheet/$file";
  314. }
  315. }
  316. @ $stylesheet_dir->close();
  317. }
  318. $template_dir = @ dir("$template_directory");
  319. if ( $template_dir ) {
  320. while ( ($file = $template_dir->read()) !== false ) {
  321. if ( preg_match('|^\.+$|', $file) )
  322. continue;
  323. if ( preg_match('|\.php$|', $file) ) {
  324. $template_files[] = "$template_directory/$file";
  325. } elseif ( is_dir("$template_directory/$file") ) {
  326. $template_subdir = @ dir("$template_directory/$file");
  327. if ( !$template_subdir )
  328. continue;
  329. while ( ($subfile = $template_subdir->read()) !== false ) {
  330. if ( preg_match('|^\.+$|', $subfile) )
  331. continue;
  332. if ( preg_match('|\.php$|', $subfile) )
  333. $template_files[] = "$template_directory/$file/$subfile";
  334. }
  335. @ $template_subdir->close();
  336. }
  337. }
  338. @ $template_dir->close();
  339. }
  340. //Make unique and remove duplicates when stylesheet and template are the same i.e. most themes
  341. $template_files = array_unique($template_files);
  342. $stylesheet_files = array_unique($stylesheet_files);
  343. $template_dir = dirname($template_files[0]);
  344. $stylesheet_dir = dirname($stylesheet_files[0]);
  345. if ( empty($template_dir) )
  346. $template_dir = '/';
  347. if ( empty($stylesheet_dir) )
  348. $stylesheet_dir = '/';
  349. // Check for theme name collision. This occurs if a theme is copied to
  350. // a new theme directory and the theme header is not updated. Whichever
  351. // theme is first keeps the name. Subsequent themes get a suffix applied.
  352. // The Default and Classic themes always trump their pretenders.
  353. if ( isset($wp_themes[$name]) ) {
  354. if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
  355. ('default' == $stylesheet || 'classic' == $stylesheet) ) {
  356. // If another theme has claimed to be one of our default themes, move
  357. // them aside.
  358. $suffix = $wp_themes[$name]['Stylesheet'];
  359. $new_name = "$name/$suffix";
  360. $wp_themes[$new_name] = $wp_themes[$name];
  361. $wp_themes[$new_name]['Name'] = $new_name;
  362. } else {
  363. $name = "$name/$stylesheet";
  364. }
  365. }
  366. $theme_roots[$stylesheet] = str_replace( WP_CONTENT_DIR, '', $theme_root );
  367. $wp_themes[$name] = array(
  368. 'Name' => $name,
  369. 'Title' => $title,
  370. 'Description' => $description,
  371. 'Author' => $author,
  372. 'Author Name' => $theme_data['AuthorName'],
  373. 'Author URI' => $theme_data['AuthorURI'],
  374. 'Version' => $version,
  375. 'Template' => $template,
  376. 'Stylesheet' => $stylesheet,
  377. 'Template Files' => $template_files,
  378. 'Stylesheet Files' => $stylesheet_files,
  379. 'Template Dir' => $template_dir,
  380. 'Stylesheet Dir' => $stylesheet_dir,
  381. 'Status' => $theme_data['Status'],
  382. 'Screenshot' => $screenshot,
  383. 'Tags' => $theme_data['Tags'],
  384. 'Theme Root' => $theme_root,
  385. 'Theme Root URI' => str_replace( WP_CONTENT_DIR, content_url(), $theme_root ),
  386. );
  387. }
  388. unset($theme_files);
  389. /* Store theme roots in the DB */
  390. if ( get_site_transient( 'theme_roots' ) != $theme_roots )
  391. set_site_transient( 'theme_roots', $theme_roots, 7200 ); // cache for two hours
  392. unset($theme_roots);
  393. /* Resolve theme dependencies. */
  394. $theme_names = array_keys( $wp_themes );
  395. foreach ( (array) $theme_names as $theme_name ) {
  396. $wp_themes[$theme_name]['Parent Theme'] = '';
  397. if ( $wp_themes[$theme_name]['Stylesheet'] != $wp_themes[$theme_name]['Template'] ) {
  398. foreach ( (array) $theme_names as $parent_theme_name ) {
  399. if ( ($wp_themes[$parent_theme_name]['Stylesheet'] == $wp_themes[$parent_theme_name]['Template']) && ($wp_themes[$parent_theme_name]['Template'] == $wp_themes[$theme_name]['Template']) ) {
  400. $wp_themes[$theme_name]['Parent Theme'] = $wp_themes[$parent_theme_name]['Name'];
  401. break;
  402. }
  403. }
  404. }
  405. }
  406. return $wp_themes;
  407. }
  408. /**
  409. * Retrieve theme roots.
  410. *
  411. * @since 2.9.0
  412. *
  413. * @return array Theme roots
  414. */
  415. function get_theme_roots() {
  416. $theme_roots = get_site_transient( 'theme_roots' );
  417. if ( false === $theme_roots ) {
  418. get_themes();
  419. $theme_roots = get_site_transient( 'theme_roots' ); // this is set in get_theme()
  420. }
  421. return $theme_roots;
  422. }
  423. /**
  424. * Retrieve theme data.
  425. *
  426. * @since 1.5.0
  427. *
  428. * @param string $theme Theme name.
  429. * @return array|null Null, if theme name does not exist. Theme data, if exists.
  430. */
  431. function get_theme($theme) {
  432. $themes = get_themes();
  433. if ( array_key_exists($theme, $themes) )
  434. return $themes[$theme];
  435. return null;
  436. }
  437. /**
  438. * Retrieve current theme display name.
  439. *
  440. * If the 'current_theme' option has already been set, then it will be returned
  441. * instead. If it is not set, then each theme will be iterated over until both
  442. * the current stylesheet and current template name.
  443. *
  444. * @since 1.5.0
  445. *
  446. * @return string
  447. */
  448. function get_current_theme() {
  449. if ( $theme = get_option('current_theme') )
  450. return $theme;
  451. $themes = get_themes();
  452. $theme_names = array_keys($themes);
  453. $current_template = get_option('template');
  454. $current_stylesheet = get_option('stylesheet');
  455. $current_theme = 'WordPress Default';
  456. if ( $themes ) {
  457. foreach ( (array) $theme_names as $theme_name ) {
  458. if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
  459. $themes[$theme_name]['Template'] == $current_template ) {
  460. $current_theme = $themes[$theme_name]['Name'];
  461. break;
  462. }
  463. }
  464. }
  465. update_option('current_theme', $current_theme);
  466. return $current_theme;
  467. }
  468. /**
  469. * Register a directory that contains themes.
  470. *
  471. * @since 2.9.0
  472. *
  473. * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
  474. * @return bool
  475. */
  476. function register_theme_directory( $directory) {
  477. global $wp_theme_directories;
  478. /* If this folder does not exist, return and do not register */
  479. if ( !file_exists( $directory ) )
  480. /* Try prepending as the theme directory could be relative to the content directory */
  481. $registered_directory = WP_CONTENT_DIR . '/' . $directory;
  482. else
  483. $registered_directory = $directory;
  484. /* If this folder does not exist, return and do not register */
  485. if ( !file_exists( $registered_directory ) )
  486. return false;
  487. $wp_theme_directories[] = $registered_directory;
  488. return true;
  489. }
  490. /**
  491. * Search all registered theme directories for complete and valid themes.
  492. *
  493. * @since 2.9.0
  494. *
  495. * @return array Valid themes found
  496. */
  497. function search_theme_directories() {
  498. global $wp_theme_directories, $wp_broken_themes;
  499. if ( empty( $wp_theme_directories ) )
  500. return false;
  501. $theme_files = array();
  502. $wp_broken_themes = array();
  503. /* Loop the registered theme directories and extract all themes */
  504. foreach ( (array) $wp_theme_directories as $theme_root ) {
  505. $theme_loc = $theme_root;
  506. /* We don't want to replace all forward slashes, see Trac #4541 */
  507. if ( '/' != WP_CONTENT_DIR )
  508. $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
  509. /* Files in the root of the current theme directory and one subdir down */
  510. $themes_dir = @ opendir($theme_root);
  511. if ( !$themes_dir )
  512. return false;
  513. while ( ($theme_dir = readdir($themes_dir)) !== false ) {
  514. if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
  515. if ( $theme_dir{0} == '.' || $theme_dir == 'CVS' )
  516. continue;
  517. $stylish_dir = @opendir($theme_root . '/' . $theme_dir);
  518. $found_stylesheet = false;
  519. while ( ($theme_file = readdir($stylish_dir)) !== false ) {
  520. if ( $theme_file == 'style.css' ) {
  521. $theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
  522. $found_stylesheet = true;
  523. break;
  524. }
  525. }
  526. @closedir($stylish_dir);
  527. if ( !$found_stylesheet ) { // look for themes in that dir
  528. $subdir = "$theme_root/$theme_dir";
  529. $subdir_name = $theme_dir;
  530. $theme_subdirs = @opendir( $subdir );
  531. $found_subdir_themes = false;
  532. while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
  533. if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
  534. if ( $theme_subdir{0} == '.' || $theme_subdir == 'CVS' )
  535. continue;
  536. $stylish_dir = @opendir($subdir . '/' . $theme_subdir);
  537. $found_stylesheet = false;
  538. while ( ($theme_file = readdir($stylish_dir)) !== false ) {
  539. if ( $theme_file == 'style.css' ) {
  540. $theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
  541. $found_stylesheet = true;
  542. $found_subdir_themes = true;
  543. break;
  544. }
  545. }
  546. @closedir($stylish_dir);
  547. }
  548. }
  549. @closedir($theme_subdir);
  550. if ( !$found_subdir_themes )
  551. $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
  552. }
  553. }
  554. }
  555. if ( is_dir( $theme_dir ) )
  556. @closedir( $theme_dir );
  557. }
  558. return $theme_files;
  559. }
  560. /**
  561. * Retrieve path to themes directory.
  562. *
  563. * Does not have trailing slash.
  564. *
  565. * @since 1.5.0
  566. * @param $stylesheet_or_template The stylesheet or template name of the theme
  567. * @uses apply_filters() Calls 'theme_root' filter on path.
  568. *
  569. * @return string Theme path.
  570. */
  571. function get_theme_root( $stylesheet_or_template = false ) {
  572. if ($stylesheet_or_template) {
  573. $theme_roots = get_theme_roots();
  574. if ( ! empty( $theme_roots[$stylesheet_or_template] ) )
  575. $theme_root = WP_CONTENT_DIR . $theme_roots[$stylesheet_or_template];
  576. else
  577. $theme_root = WP_CONTENT_DIR . '/themes';
  578. } else {
  579. $theme_root = WP_CONTENT_DIR . '/themes';
  580. }
  581. return apply_filters( 'theme_root', $theme_root );
  582. }
  583. /**
  584. * Retrieve URI for themes directory.
  585. *
  586. * Does not have trailing slash.
  587. *
  588. * @since 1.5.0
  589. * @param $stylesheet_or_template The stylesheet or template name of the theme
  590. *
  591. * @return string Themes URI.
  592. */
  593. function get_theme_root_uri( $stylesheet_or_template = false ) {
  594. $theme_roots = get_theme_roots();
  595. if ( isset( $theme_roots[$stylesheet_or_template] ) && $theme_roots[$stylesheet_or_template] )
  596. $theme_root_uri = content_url( $theme_roots[$stylesheet_or_template] );
  597. else
  598. $theme_root_uri = content_url( 'themes' );
  599. return apply_filters( 'theme_root_uri', $theme_root_uri, get_option('siteurl'), $stylesheet_or_template );
  600. }
  601. /**
  602. * Retrieve path to file without the use of extension.
  603. *
  604. * Used to quickly retrieve the path of file without including the file
  605. * extension. It will also check the parent template, if the file exists, with
  606. * the use of {@link locate_template()}. Allows for more generic file location
  607. * without the use of the other get_*_template() functions.
  608. *
  609. * Can be used with include() or require() to retrieve path.
  610. * <code>
  611. * if( '' != get_query_template( '404' ) )
  612. * include( get_query_template( '404' ) );
  613. * </code>
  614. * or the same can be accomplished with
  615. * <code>
  616. * if( '' != get_404_template() )
  617. * include( get_404_template() );
  618. * </code>
  619. *
  620. * @since 1.5.0
  621. *
  622. * @param string $type Filename without extension.
  623. * @return string Full path to file.
  624. */
  625. function get_query_template($type) {
  626. $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
  627. return apply_filters("{$type}_template", locate_template(array("{$type}.php")));
  628. }
  629. /**
  630. * Retrieve path of index template in current or parent template.
  631. *
  632. * @since 3.0.0
  633. *
  634. * @return string
  635. */
  636. function get_index_template() {
  637. return get_query_template('index');
  638. }
  639. /**
  640. * Retrieve path of 404 template in current or parent template.
  641. *
  642. * @since 1.5.0
  643. *
  644. * @return string
  645. */
  646. function get_404_template() {
  647. return get_query_template('404');
  648. }
  649. /**
  650. * Retrieve path of archive template in current or parent template.
  651. *
  652. * @since 1.5.0
  653. *
  654. * @return string
  655. */
  656. function get_archive_template() {
  657. return get_query_template('archive');
  658. }
  659. /**
  660. * Retrieve path of author template in current or parent template.
  661. *
  662. * @since 1.5.0
  663. *
  664. * @return string
  665. */
  666. function get_author_template() {
  667. $author_id = absint( get_query_var( 'author' ) );
  668. $author = get_user_by( 'id', $author_id );
  669. $author = $author->user_nicename;
  670. $templates = array();
  671. if ( $author )
  672. $templates[] = "author-{$author}.php";
  673. if ( $author_id )
  674. $templates[] = "author-{$author_id}.php";
  675. $templates[] = 'author.php';
  676. $template = locate_template( $templates );
  677. return apply_filters( 'author_template', $template );
  678. }
  679. /**
  680. * Retrieve path of category template in current or parent template.
  681. *
  682. * Works by first retrieving the current slug for example 'category-default.php' and then
  683. * trying category ID, for example 'category-1.php' and will finally fallback to category.php
  684. * template, if those files don't exist.
  685. *
  686. * @since 1.5.0
  687. * @uses apply_filters() Calls 'category_template' on file path of category template.
  688. *
  689. * @return string
  690. */
  691. function get_category_template() {
  692. $cat_ID = absint( get_query_var('cat') );
  693. $category = get_category( $cat_ID );
  694. $templates = array();
  695. if ( !is_wp_error($category) )
  696. $templates[] = "category-{$category->slug}.php";
  697. $templates[] = "category-$cat_ID.php";
  698. $templates[] = "category.php";
  699. $template = locate_template($templates);
  700. return apply_filters('category_template', $template);
  701. }
  702. /**
  703. * Retrieve path of tag template in current or parent template.
  704. *
  705. * Works by first retrieving the current tag name, for example 'tag-wordpress.php' and then
  706. * trying tag ID, for example 'tag-1.php' and will finally fallback to tag.php
  707. * template, if those files don't exist.
  708. *
  709. * @since 2.3.0
  710. * @uses apply_filters() Calls 'tag_template' on file path of tag template.
  711. *
  712. * @return string
  713. */
  714. function get_tag_template() {
  715. $tag_id = absint( get_query_var('tag_id') );
  716. $tag_name = get_query_var('tag');
  717. $templates = array();
  718. if ( $tag_name )
  719. $templates[] = "tag-$tag_name.php";
  720. if ( $tag_id )
  721. $templates[] = "tag-$tag_id.php";
  722. $templates[] = "tag.php";
  723. $template = locate_template($templates);
  724. return apply_filters('tag_template', $template);
  725. }
  726. /**
  727. * Retrieve path of taxonomy template in current or parent template.
  728. *
  729. * Retrieves the taxonomy and term, if term is available. The template is
  730. * prepended with 'taxonomy-' and followed by both the taxonomy string and
  731. * the taxonomy string followed by a dash and then followed by the term.
  732. *
  733. * The taxonomy and term template is checked and used first, if it exists.
  734. * Second, just the taxonomy template is checked, and then finally, taxonomy.php
  735. * template is used. If none of the files exist, then it will fall back on to
  736. * index.php.
  737. *
  738. * @since unknown (2.6.0 most likely)
  739. * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
  740. *
  741. * @return string
  742. */
  743. function get_taxonomy_template() {
  744. $taxonomy = get_query_var('taxonomy');
  745. $term = get_query_var('term');
  746. $templates = array();
  747. if ( $taxonomy && $term )
  748. $templates[] = "taxonomy-$taxonomy-$term.php";
  749. if ( $taxonomy )
  750. $templates[] = "taxonomy-$taxonomy.php";
  751. $templates[] = "taxonomy.php";
  752. $template = locate_template($templates);
  753. return apply_filters('taxonomy_template', $template);
  754. }
  755. /**
  756. * Retrieve path of date template in current or parent template.
  757. *
  758. * @since 1.5.0
  759. *
  760. * @return string
  761. */
  762. function get_date_template() {
  763. return get_query_template('date');
  764. }
  765. /**
  766. * Retrieve path of home template in current or parent template.
  767. *
  768. * This is the template used for the page containing the blog posts
  769. *
  770. * Attempts to locate 'home.php' first before falling back to 'index.php'.
  771. *
  772. * @since 1.5.0
  773. * @uses apply_filters() Calls 'home_template' on file path of home template.
  774. *
  775. * @return string
  776. */
  777. function get_home_template() {
  778. $template = locate_template(array('home.php', 'index.php'));
  779. return apply_filters('home_template', $template);
  780. }
  781. /**
  782. * Retrieve path of front-page template in current or parent template.
  783. *
  784. * Looks for 'front-page.php'.
  785. *
  786. * @since 3.0.0
  787. * @uses apply_filters() Calls 'front_page_template' on file path of template.
  788. *
  789. * @return string
  790. */
  791. function get_front_page_template() {
  792. return apply_filters( 'front_page_template', locate_template( array('front-page.php') ) );
  793. }
  794. /**
  795. * Retrieve path of page template in current or parent template.
  796. *
  797. * Will first look for the specifically assigned page template
  798. * The will search for 'page-{slug}.php' followed by 'page-id.php'
  799. * and finally 'page.php'
  800. *
  801. * @since 1.5.0
  802. *
  803. * @return string
  804. */
  805. function get_page_template() {
  806. global $wp_query;
  807. $id = (int) $wp_query->get_queried_object_id();
  808. $template = get_post_meta($id, '_wp_page_template', true);
  809. $pagename = get_query_var('pagename');
  810. if ( !$pagename && $id > 0 ) {
  811. // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
  812. $post = $wp_query->get_queried_object();
  813. $pagename = $post->post_name;
  814. }
  815. if ( 'default' == $template )
  816. $template = '';
  817. $templates = array();
  818. if ( !empty($template) && !validate_file($template) )
  819. $templates[] = $template;
  820. if ( $pagename )
  821. $templates[] = "page-$pagename.php";
  822. if ( $id )
  823. $templates[] = "page-$id.php";
  824. $templates[] = "page.php";
  825. return apply_filters('page_template', locate_template($templates));
  826. }
  827. /**
  828. * Retrieve path of paged template in current or parent template.
  829. *
  830. * @since 1.5.0
  831. *
  832. * @return string
  833. */
  834. function get_paged_template() {
  835. return get_query_template('paged');
  836. }
  837. /**
  838. * Retrieve path of search template in current or parent template.
  839. *
  840. * @since 1.5.0
  841. *
  842. * @return string
  843. */
  844. function get_search_template() {
  845. return get_query_template('search');
  846. }
  847. /**
  848. * Retrieve path of single template in current or parent template.
  849. *
  850. * @since 1.5.0
  851. *
  852. * @return string
  853. */
  854. function get_single_template() {
  855. global $wp_query;
  856. $object = $wp_query->get_queried_object();
  857. $templates = array('single-' . $object->post_type . '.php', 'single.php');
  858. return apply_filters('single_template', locate_template($templates));
  859. }
  860. /**
  861. * Retrieve path of attachment template in current or parent template.
  862. *
  863. * The attachment path first checks if the first part of the mime type exists.
  864. * The second check is for the second part of the mime type. The last check is
  865. * for both types separated by an underscore. If neither are found then the file
  866. * 'attachment.php' is checked and returned.
  867. *
  868. * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
  869. * finally 'text_plain.php'.
  870. *
  871. * @since 2.0.0
  872. *
  873. * @return string
  874. */
  875. function get_attachment_template() {
  876. global $posts;
  877. $type = explode('/', $posts[0]->post_mime_type);
  878. if ( $template = get_query_template($type[0]) )
  879. return $template;
  880. elseif ( $template = get_query_template($type[1]) )
  881. return $template;
  882. elseif ( $template = get_query_template("$type[0]_$type[1]") )
  883. return $template;
  884. else
  885. return get_query_template('attachment');
  886. }
  887. /**
  888. * Retrieve path of comment popup template in current or parent template.
  889. *
  890. * Checks for comment popup template in current template, if it exists or in the
  891. * parent template.
  892. *
  893. * @since 1.5.0
  894. * @uses apply_filters() Calls 'comments_popup_template' filter on path.
  895. *
  896. * @return string
  897. */
  898. function get_comments_popup_template() {
  899. $template = locate_template(array("comments-popup.php"));
  900. // Backward compat code will be removed in a future release
  901. if ('' == $template)
  902. $template = WPINC . '/theme-compat/comments-popup.php';
  903. return apply_filters('comments_popup_template', $template);
  904. }
  905. /**
  906. * Retrieve the name of the highest priority template file that exists.
  907. *
  908. * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
  909. * inherit from a parent theme can just overload one file.
  910. *
  911. * @since 2.7.0
  912. *
  913. * @param array $template_names Array of template files to search for in priority order.
  914. * @param bool $load If true the template file will be loaded if it is found.
  915. * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
  916. * @return string The template filename if one is located.
  917. */
  918. function locate_template($template_names, $load = false, $require_once = true ) {
  919. if ( !is_array($template_names) )
  920. return '';
  921. $located = '';
  922. foreach ( $template_names as $template_name ) {
  923. if ( !$template_name )
  924. continue;
  925. if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
  926. $located = STYLESHEETPATH . '/' . $template_name;
  927. break;
  928. } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
  929. $located = TEMPLATEPATH . '/' . $template_name;
  930. break;
  931. }
  932. }
  933. if ( $load && '' != $located )
  934. load_template( $located, $require_once );
  935. return $located;
  936. }
  937. /**
  938. * Require the template file with WordPress environment.
  939. *
  940. * The globals are set up for the template file to ensure that the WordPress
  941. * environment is available from within the function. The query variables are
  942. * also available.
  943. *
  944. * @since 1.5.0
  945. *
  946. * @param string $_template_file Path to template file.
  947. * @param bool $require_once Whether to require_once or require. Default true.
  948. */
  949. function load_template( $_template_file, $require_once = true ) {
  950. global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
  951. if ( is_array( $wp_query->query_vars ) )
  952. extract( $wp_query->query_vars, EXTR_SKIP );
  953. if ( $require_once )
  954. require_once( $_template_file );
  955. else
  956. require( $_template_file );
  957. }
  958. /**
  959. * Display localized stylesheet link element.
  960. *
  961. * @since 2.1.0
  962. */
  963. function locale_stylesheet() {
  964. $stylesheet = get_locale_stylesheet_uri();
  965. if ( empty($stylesheet) )
  966. return;
  967. echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
  968. }
  969. /**
  970. * Start preview theme output buffer.
  971. *
  972. * Will only preform task if the user has permissions and template and preview
  973. * query variables exist.
  974. *
  975. * @since 2.6.0
  976. */
  977. function preview_theme() {
  978. if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
  979. return;
  980. if ( !current_user_can( 'switch_themes' ) )
  981. return;
  982. $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
  983. if ( validate_file($_GET['template']) )
  984. return;
  985. add_filter( 'template', '_preview_theme_template_filter' );
  986. if ( isset($_GET['stylesheet']) ) {
  987. $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
  988. if ( validate_file($_GET['stylesheet']) )
  989. return;
  990. add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
  991. }
  992. // Prevent theme mods to current theme being used on theme being previewed
  993. add_filter( 'pre_option_mods_' . get_current_theme(), '__return_empty_array' );
  994. ob_start( 'preview_theme_ob_filter' );
  995. }
  996. add_action('setup_theme', 'preview_theme');
  997. /**
  998. * Private function to modify the current template when previewing a theme
  999. *
  1000. * @since 2.9.0
  1001. * @access private
  1002. *
  1003. * @return string
  1004. */
  1005. function _preview_theme_template_filter() {
  1006. return isset($_GET['template']) ? $_GET['template'] : '';
  1007. }
  1008. /**
  1009. * Private function to modify the current stylesheet when previewing a theme
  1010. *
  1011. * @since 2.9.0
  1012. * @access private
  1013. *
  1014. * @return string
  1015. */
  1016. function _preview_theme_stylesheet_filter() {
  1017. return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
  1018. }
  1019. /**
  1020. * Callback function for ob_start() to capture all links in the theme.
  1021. *
  1022. * @since 2.6.0
  1023. * @access private
  1024. *
  1025. * @param string $content
  1026. * @return string
  1027. */
  1028. function preview_theme_ob_filter( $content ) {
  1029. return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
  1030. }
  1031. /**
  1032. * Manipulates preview theme links in order to control and maintain location.
  1033. *
  1034. * Callback function for preg_replace_callback() to accept and filter matches.
  1035. *
  1036. * @since 2.6.0
  1037. * @access private
  1038. *
  1039. * @param array $matches
  1040. * @return string
  1041. */
  1042. function preview_theme_ob_filter_callback( $matches ) {
  1043. if ( strpos($matches[4], 'onclick') !== false )
  1044. $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if its escaped by \ to prevent breaking mid-attribute.
  1045. if (
  1046. ( false !== strpos($matches[3], '/wp-admin/') )
  1047. ||
  1048. ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
  1049. ||
  1050. ( false !== strpos($matches[3], '/feed/') )
  1051. ||
  1052. ( false !== strpos($matches[3], '/trackback/') )
  1053. )
  1054. return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
  1055. $link = add_query_arg( array('preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'] ), $matches[3] );
  1056. if ( 0 === strpos($link, 'preview=1') )
  1057. $link = "?$link";
  1058. return $matches[1] . esc_attr( $link ) . $matches[4];
  1059. }
  1060. /**
  1061. * Switches current theme to new template and stylesheet names.
  1062. *
  1063. * @since unknown
  1064. * @uses do_action() Calls 'switch_theme' action on updated theme display name.
  1065. *
  1066. * @param string $template Template name
  1067. * @param string $stylesheet Stylesheet name.
  1068. */
  1069. function switch_theme($template, $stylesheet) {
  1070. update_option('template', $template);
  1071. update_option('stylesheet', $stylesheet);
  1072. delete_option('current_theme');
  1073. $theme = get_current_theme();
  1074. do_action('switch_theme', $theme);
  1075. }
  1076. /**
  1077. * Checks that current theme files 'index.php' and 'style.css' exists.
  1078. *
  1079. * Does not check the default theme, which is the fallback and should always exist.
  1080. * Will switch theme to the fallback theme if current theme does not validate.
  1081. * You can use the 'validate_current_theme' filter to return FALSE to
  1082. * disable this functionality.
  1083. *
  1084. * @since 1.5.0
  1085. * @see WP_DEFAULT_THEME
  1086. *
  1087. * @return bool
  1088. */
  1089. function validate_current_theme() {
  1090. // Don't validate during an install/upgrade.
  1091. if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
  1092. return true;
  1093. if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
  1094. switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
  1095. return false;
  1096. }
  1097. if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
  1098. switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
  1099. return false;
  1100. }
  1101. return true;
  1102. }
  1103. /**
  1104. * Retrieve theme modification value for the current theme.
  1105. *
  1106. * If the modification name does not exist, then the $default will be passed
  1107. * through {@link http://php.net/sprintf sprintf()} PHP function with the first
  1108. * string the template directory URI and the second string the stylesheet
  1109. * directory URI.
  1110. *
  1111. * @since 2.1.0
  1112. * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
  1113. *
  1114. * @param string $name Theme modification name.
  1115. * @param bool|string $default
  1116. * @return string
  1117. */
  1118. function get_theme_mod($name, $default = false) {
  1119. $theme = get_current_theme();
  1120. $mods = get_option( "mods_$theme" );
  1121. if ( isset($mods[$name]) )
  1122. return apply_filters( "theme_mod_$name", $mods[$name] );
  1123. return apply_filters( "theme_mod_$name", sprintf($default, get_template_directory_uri(), get_stylesheet_directory_uri()) );
  1124. }
  1125. /**
  1126. * Update theme modification value for the current theme.
  1127. *
  1128. * @since 2.1.0
  1129. *
  1130. * @param string $name Theme modification name.
  1131. * @param string $value theme modification value.
  1132. */
  1133. function set_theme_mod($name, $value) {
  1134. $theme = get_current_theme();
  1135. $mods = get_option("mods_$theme");
  1136. $mods[$name] = $value;
  1137. update_option("mods_$theme", $mods);
  1138. wp_cache_delete("mods_$theme", 'options');
  1139. }
  1140. /**
  1141. * Remove theme modification name from current theme list.
  1142. *
  1143. * If removing the name also removes all elements, then the entire option will
  1144. * be removed.
  1145. *
  1146. * @since 2.1.0
  1147. *
  1148. * @param string $name Theme modification name.
  1149. * @return null
  1150. */
  1151. function remove_theme_mod( $name ) {
  1152. $theme = get_current_theme();
  1153. $mods = get_option("mods_$theme");
  1154. if ( !isset($mods[$name]) )
  1155. return;
  1156. unset($mods[$name]);
  1157. if ( empty($mods) )
  1158. return remove_theme_mods();
  1159. update_option("mods_$theme", $mods);
  1160. wp_cache_delete("mods_$theme", 'options');
  1161. }
  1162. /**
  1163. * Remove theme modifications option for current theme.
  1164. *
  1165. * @since 2.1.0
  1166. */
  1167. function remove_theme_mods() {
  1168. $theme = get_current_theme();
  1169. delete_option("mods_$theme");
  1170. }
  1171. /**
  1172. * Retrieve text color for custom header.
  1173. *
  1174. * @since 2.1.0
  1175. * @uses HEADER_TEXTCOLOR
  1176. *
  1177. * @return string
  1178. */
  1179. function get_header_textcolor() {
  1180. $default = defined('HEADER_TEXTCOLOR') ? HEADER_TEXTCOLOR : '';
  1181. return get_theme_mod('header_textcolor', $default);
  1182. }
  1183. /**
  1184. * Display text color for custom header.
  1185. *
  1186. * @since 2.1.0
  1187. */
  1188. function header_textcolor() {
  1189. echo get_header_textcolor();
  1190. }
  1191. /**
  1192. * Retrieve header image for custom header.
  1193. *
  1194. * @since 2.1.0
  1195. * @uses HEADER_IMAGE
  1196. *
  1197. * @return string
  1198. */
  1199. function get_header_image() {
  1200. $default = defined('HEADER_IMAGE') ? HEADER_IMAGE : '';
  1201. return get_theme_mod('header_image', $default);
  1202. }
  1203. /**
  1204. * Display header image path.
  1205. *
  1206. * @since 2.1.0
  1207. */
  1208. function header_image() {
  1209. echo get_header_image();
  1210. }
  1211. /**
  1212. * Add callbacks for image header display.
  1213. *
  1214. * The parameter $header_callback callback will be required to display the
  1215. * content for the 'wp_head' action. The parameter $admin_header_callback
  1216. * callback will be added to Custom_Image_Header class and that will be added
  1217. * to the 'admin_menu' action.
  1218. *
  1219. * @since 2.1.0
  1220. * @uses Custom_Image_Header Sets up for $admin_header_callback for administration panel display.
  1221. *
  1222. * @param callback $header_callback Call on 'wp_head' action.
  1223. * @param callback $admin_header_callback Call on custom header administration screen.
  1224. * @param callback $admin_image_div_callback Output a custom header image div on the custom header administration screen. Optional.
  1225. */
  1226. function add_custom_image_header($header_callback, $admin_header_callback, $admin_image_div_callback = '') {
  1227. if ( ! empty($header_callback) )
  1228. add_action('wp_head', $header_callback);
  1229. add_theme_support( 'custom-header' );
  1230. if ( ! is_admin() )
  1231. return;
  1232. require_once(ABSPATH . 'wp-admin/custom-header.php');
  1233. $GLOBALS['custom_image_header'] =& new Custom_Image_Header($admin_header_callback, $admin_image_div_callback);
  1234. add_action('admin_menu', array(&$GLOBALS['custom_image_header'], 'init'));
  1235. }
  1236. /**
  1237. * Register a selection of default headers to be displayed by the custom header admin UI.
  1238. *
  1239. * @since 3.0.0
  1240. *
  1241. * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
  1242. */
  1243. function register_default_headers( $headers ) {
  1244. global $_wp_default_headers;
  1245. $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
  1246. }
  1247. /**
  1248. * Unregister default headers.
  1249. *
  1250. * This function must be called after register_default_headers() has already added the
  1251. * header you want to remove.
  1252. *
  1253. * @see register_default_headers()
  1254. * @since 3.0.0
  1255. *
  1256. * @param string|array The header string id (key of array) to remove, or an array thereof.
  1257. * @return True on success, false on failure.
  1258. */
  1259. function unregister_default_headers( $header ) {
  1260. global $_wp_default_headers;
  1261. if ( is_array( $header ) ) {
  1262. array_map( 'unregister_default_headers', $header );
  1263. } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
  1264. unset( $_wp_default_headers[ $header ] );
  1265. return true;
  1266. } else {
  1267. return false;
  1268. }
  1269. }
  1270. /**
  1271. * Retrieve background image for custom background.
  1272. *
  1273. * @since 3.0.0
  1274. *
  1275. * @return string
  1276. */
  1277. function get_background_image() {
  1278. $default = defined('BACKGROUND_IMAGE') ? BACKGROUND_IMAGE : '';
  1279. return get_theme_mod('background_image', $default);
  1280. }
  1281. /**
  1282. * Display background image path.
  1283. *
  1284. * @since 3.0.0
  1285. */
  1286. function background_image() {
  1287. echo get_background_image();
  1288. }
  1289. /**
  1290. * Retrieve value for custom background color.
  1291. *
  1292. * @since 3.0.0
  1293. * @uses BACKGROUND_COLOR
  1294. *
  1295. * @return string
  1296. */
  1297. function get_background_color() {
  1298. $default = defined('BACKGROUND_COLOR') ? BACKGROUND_COLOR : '';
  1299. return get_theme_mod('background_color', $default);
  1300. }
  1301. /**
  1302. * Display background color value.
  1303. *
  1304. * @since 3.0.0
  1305. */
  1306. function background_color() {
  1307. echo get_background_color();
  1308. }
  1309. /**
  1310. * Add callbacks for background image display.
  1311. *
  1312. * The parameter $header_callback callback will be required to display the
  1313. * content for the 'wp_head' action. The parameter $admin_header_callback
  1314. * callback will be added to Custom_Background class and that will be added
  1315. * to the 'admin_menu' action.
  1316. *
  1317. * @since 3.0.0
  1318. * @uses Custom_Background Sets up for $admin_header_callback for administration panel display.
  1319. *
  1320. * @param callback $header_callback Call on 'wp_head' action.
  1321. * @param callback $admin_header_callback Call on custom background administration screen.
  1322. * @param callback $admin_image_div_callback Output a custom background image div on the custom background administration screen. Optional.
  1323. */
  1324. function add_custom_background($header_callback = '', $admin_header_callback = '', $admin_image_div_callback = '') {
  1325. if ( isset($GLOBALS['custom_background']) )
  1326. return;
  1327. if ( empty($header_callback) )
  1328. $header_callback = '_custom_background_cb';
  1329. add_action('wp_head', $header_callback);
  1330. add_theme_support( 'custom-background' );
  1331. if ( ! is_admin() )
  1332. return;
  1333. require_once(ABSPATH . 'wp-admin/custom-background.php');
  1334. $GLOBALS['custom_background'] =& new Custom_Background($admin_header_callback, $admin_image_div_callback);
  1335. add_action('admin_menu', array(&$GLOBALS['custom_background'], 'init'));
  1336. }
  1337. /**
  1338. * Default custom background callback.
  1339. *
  1340. * @since 3.0.0
  1341. * @see add_custom_background()
  1342. * @access protected
  1343. */
  1344. function _custom_background_cb() {
  1345. $background = get_background_image();
  1346. $color = get_background_color();
  1347. if ( !$background && !$color )
  1348. return;
  1349. switch ( get_theme_mod('background_repeat', 'repeat') ) {
  1350. case 'no-repeat':
  1351. $repeat = 'background-repeat: no-repeat;';
  1352. break;
  1353. case 'repeat-x':
  1354. $repeat = 'background-repeat: repeat-x;';
  1355. break;
  1356. case 'repeat-y':
  1357. $repeat = 'background-repeat: repeat-y;';
  1358. break;
  1359. default:
  1360. $repeat = 'background-repeat: repeat;';
  1361. }
  1362. switch ( get_theme_mod('background_position_x', 'left') ) {
  1363. case 'center':
  1364. $position = 'background-position: top center;';
  1365. break;
  1366. case 'right':
  1367. $position = 'background-position: top right;';
  1368. break;
  1369. default:
  1370. $position = 'background-position: top left;';
  1371. }
  1372. if ( 'scroll' == get_theme_mod('background_attachment', 'fixed') )
  1373. $attachment = 'background-attachment: scroll;';
  1374. else
  1375. $attachment = 'background-attachment: fixed;';
  1376. if ( !empty($background ) )
  1377. $image = "background-image: url('$background');";
  1378. else
  1379. $image = '';
  1380. if ( !empty($color) )
  1381. $color = "background-color: #$color;";
  1382. else
  1383. $color = '';
  1384. ?>
  1385. <style type="text/css">
  1386. body {
  1387. <?php echo $image; ?>
  1388. <?php echo $color; ?>
  1389. <?php echo $repeat; ?>
  1390. <?php echo $position; ?>
  1391. <?php echo $attachment; ?>
  1392. }
  1393. </style>
  1394. <?php
  1395. }
  1396. /**
  1397. * Add callback for custom TinyMCE editor stylesheets.
  1398. *
  1399. * The parameter $stylesheet is the name of the stylesheet, relative to
  1400. * the theme root. It also accepts an array of stylesheets.
  1401. * It is optional and defaults to 'editor-style.css'.
  1402. *
  1403. * @since 3.0.0
  1404. *
  1405. * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
  1406. * Defaults to 'editor-style.css'
  1407. */
  1408. function add_editor_style( $stylesheet = 'editor-style.css' ) {
  1409. add_theme_support( 'editor-style' );
  1410. if ( ! is_admin() )
  1411. return;
  1412. global $editor_styles;
  1413. $editor_styles = (array) $editor_styles;
  1414. $stylesheet = (array) $stylesheet;
  1415. if ( is_rtl() ) {
  1416. $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
  1417. $stylesheet[] = $rtl_stylesheet;
  1418. }
  1419. $editor_styles = array_merge( $editor_styles, $stylesheet );
  1420. }
  1421. /**
  1422. * Allows a theme to register its support of a certain feature
  1423. *
  1424. * Must be called in the theme's functions.php file to work.
  1425. * If attached to a hook, it must be after_setup_theme.
  1426. * The init hook may be too late for some features.
  1427. *
  1428. * @since 2.9.0
  1429. * @param string $feature the feature being added
  1430. */
  1431. function add_theme_support( $feature ) {
  1432. global $_wp_theme_features;
  1433. if ( func_num_args() == 1 )
  1434. $_wp_theme_features[$feature] = true;
  1435. else
  1436. $_wp_theme_features[$feature] = array_slice( func_get_args(), 1 );
  1437. }
  1438. /**
  1439. * Allows a theme to de-register its support of a certain feature
  1440. *
  1441. * Should be called in the theme's functions.php file. Generally would
  1442. * be used for child themes to override support from the parent theme.
  1443. *
  1444. * @since 3.0.0
  1445. * @see add_theme_support()
  1446. * @param string $feature the feature being added
  1447. * @return bool Whether feature was removed.
  1448. */
  1449. function remove_theme_support( $feature ) {
  1450. // Blacklist: for internal registrations not used directly by themes.
  1451. if ( in_array( $feature, array( 'custom-background', 'custom-header', 'editor-style', 'widgets' ) ) )
  1452. return false;
  1453. global $_wp_theme_features;
  1454. if ( ! isset( $_wp_theme_features[$feature] ) )
  1455. return false;
  1456. unset( $_wp_theme_features[$feature] );
  1457. return true;
  1458. }
  1459. /**
  1460. * Checks a theme's support for a given feature
  1461. *
  1462. * @since 2.9.0
  1463. * @param string $feature the feature being checked
  1464. * @return boolean
  1465. */
  1466. function current_theme_supports( $feature ) {
  1467. global $_wp_theme_features;
  1468. if ( !isset( $_wp_theme_features[$feature] ) )
  1469. return false;
  1470. // If no args passed then no extra checks need be performed
  1471. if ( func_num_args() <= 1 )
  1472. return true;
  1473. $args = array_slice( func_get_args(), 1 );
  1474. // @todo Allow pluggable arg checking
  1475. switch ( $feature ) {
  1476. case 'post-thumbnails':
  1477. // post-thumbnails can be registered for only certain content/post types by passing
  1478. // an array of types to add_theme_support(). If no array was passed, then
  1479. // any type is accepted
  1480. if ( true === $_wp_theme_features[$feature] ) // Registered for all types
  1481. return true;
  1482. $content_type = $args[0];
  1483. if ( in_array($content_type, $_wp_theme_features[$feature][0]) )
  1484. return true;
  1485. else
  1486. return false;
  1487. break;
  1488. }
  1489. return true;
  1490. }
  1491. /**
  1492. * Checks a theme's support for a given feature before loading the functions which implement it.
  1493. *
  1494. * @since 2.9.0
  1495. * @param string $feature the feature being checked
  1496. * @param string $include the file containing the functions that implement the feature
  1497. */
  1498. function require_if_theme_supports( $feature, $include) {
  1499. if ( current_theme_supports( $feature ) )
  1500. require ( $include );
  1501. }
  1502. /**
  1503. * Checks an attachment being deleted to see if it's a header or background image.
  1504. *
  1505. * If true it removes the theme modification which would be pointing at the deleted
  1506. * attachment
  1507. *
  1508. * @access private
  1509. * @since 3.0.0
  1510. * @param int $id the attachment id
  1511. */
  1512. function _delete_attachment_theme_mod( $id ) {
  1513. $attachment_image = wp_get_attachment_url( $id );
  1514. $header_image = get_header_image();
  1515. $backg