PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/theme.php

https://bitbucket.org/aqge/deptandashboard
PHP | 422 lines | 261 code | 60 blank | 101 comment | 50 complexity | 749e504bf6f4afc37929f59312c4f758 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress Theme Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * {@internal Missing Short Description}}
  10. *
  11. * @since 2.0.0
  12. *
  13. * @return unknown
  14. */
  15. function current_theme_info() {
  16. $themes = get_themes();
  17. $current_theme = get_current_theme();
  18. if ( ! $themes ) {
  19. $ct = new stdClass;
  20. $ct->name = $current_theme;
  21. return $ct;
  22. }
  23. if ( ! isset( $themes[$current_theme] ) ) {
  24. delete_option( 'current_theme' );
  25. $current_theme = get_current_theme();
  26. }
  27. $ct = new stdClass;
  28. $ct->name = $current_theme;
  29. $ct->title = $themes[$current_theme]['Title'];
  30. $ct->version = $themes[$current_theme]['Version'];
  31. $ct->parent_theme = $themes[$current_theme]['Parent Theme'];
  32. $ct->template_dir = $themes[$current_theme]['Template Dir'];
  33. $ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
  34. $ct->template = $themes[$current_theme]['Template'];
  35. $ct->stylesheet = $themes[$current_theme]['Stylesheet'];
  36. $ct->screenshot = $themes[$current_theme]['Screenshot'];
  37. $ct->description = $themes[$current_theme]['Description'];
  38. $ct->author = $themes[$current_theme]['Author'];
  39. $ct->tags = $themes[$current_theme]['Tags'];
  40. $ct->theme_root = $themes[$current_theme]['Theme Root'];
  41. $ct->theme_root_uri = $themes[$current_theme]['Theme Root URI'];
  42. return $ct;
  43. }
  44. /**
  45. * Remove a theme
  46. *
  47. * @since 2.8.0
  48. *
  49. * @param string $template Template directory of the theme to delete
  50. * @param string $redirect Redirect to page when complete.
  51. * @return mixed
  52. */
  53. function delete_theme($template, $redirect = '') {
  54. global $wp_filesystem;
  55. if ( empty($template) )
  56. return false;
  57. ob_start();
  58. if ( empty( $redirect ) )
  59. $redirect = wp_nonce_url('themes.php?action=delete&template=' . $template, 'delete-theme_' . $template);
  60. if ( false === ($credentials = request_filesystem_credentials($redirect)) ) {
  61. $data = ob_get_contents();
  62. ob_end_clean();
  63. if ( ! empty($data) ){
  64. include_once( ABSPATH . 'wp-admin/admin-header.php');
  65. echo $data;
  66. include( ABSPATH . 'wp-admin/admin-footer.php');
  67. exit;
  68. }
  69. return;
  70. }
  71. if ( ! WP_Filesystem($credentials) ) {
  72. request_filesystem_credentials($url, '', true); // Failed to connect, Error and request again
  73. $data = ob_get_contents();
  74. ob_end_clean();
  75. if ( ! empty($data) ) {
  76. include_once( ABSPATH . 'wp-admin/admin-header.php');
  77. echo $data;
  78. include( ABSPATH . 'wp-admin/admin-footer.php');
  79. exit;
  80. }
  81. return;
  82. }
  83. if ( ! is_object($wp_filesystem) )
  84. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  85. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  86. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
  87. //Get the base plugin folder
  88. $themes_dir = $wp_filesystem->wp_themes_dir();
  89. if ( empty($themes_dir) )
  90. return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.'));
  91. $themes_dir = trailingslashit( $themes_dir );
  92. $theme_dir = trailingslashit($themes_dir . $template);
  93. $deleted = $wp_filesystem->delete($theme_dir, true);
  94. if ( ! $deleted )
  95. return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $template) );
  96. // Force refresh of theme update information
  97. delete_site_transient('update_themes');
  98. return true;
  99. }
  100. /**
  101. * {@internal Missing Short Description}}
  102. *
  103. * @since 1.5.0
  104. *
  105. * @return unknown
  106. */
  107. function get_broken_themes() {
  108. global $wp_broken_themes;
  109. get_themes();
  110. return $wp_broken_themes;
  111. }
  112. /**
  113. * Get the allowed themes for the current blog.
  114. *
  115. * @since 3.0.0
  116. *
  117. * @uses get_themes()
  118. * @uses current_theme_info()
  119. * @uses get_site_allowed_themes()
  120. * @uses wpmu_get_blog_allowedthemes
  121. *
  122. * @return array $themes Array of allowed themes.
  123. */
  124. function get_allowed_themes() {
  125. if ( !is_multisite() )
  126. return get_themes();
  127. $themes = get_themes();
  128. $ct = current_theme_info();
  129. $allowed_themes = apply_filters("allowed_themes", get_site_allowed_themes() );
  130. if ( $allowed_themes == false )
  131. $allowed_themes = array();
  132. $blog_allowed_themes = wpmu_get_blog_allowedthemes();
  133. if ( is_array( $blog_allowed_themes ) )
  134. $allowed_themes = array_merge( $allowed_themes, $blog_allowed_themes );
  135. if ( isset( $allowed_themes[ esc_html( $ct->stylesheet ) ] ) == false )
  136. $allowed_themes[ esc_html( $ct->stylesheet ) ] = true;
  137. reset( $themes );
  138. foreach ( $themes as $key => $theme ) {
  139. if ( isset( $allowed_themes[ esc_html( $theme[ 'Stylesheet' ] ) ] ) == false )
  140. unset( $themes[ $key ] );
  141. }
  142. reset( $themes );
  143. return $themes;
  144. }
  145. /**
  146. * Get the Page Templates available in this theme
  147. *
  148. * @since 1.5.0
  149. *
  150. * @return array Key is the template name, value is the filename of the template
  151. */
  152. function get_page_templates() {
  153. $themes = get_themes();
  154. $theme = get_current_theme();
  155. $templates = $themes[$theme]['Template Files'];
  156. $page_templates = array();
  157. if ( is_array( $templates ) ) {
  158. $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
  159. foreach ( $templates as $template ) {
  160. $basename = str_replace($base, '', $template);
  161. // don't allow template files in subdirectories
  162. if ( false !== strpos($basename, '/') )
  163. continue;
  164. if ( 'functions.php' == $basename )
  165. continue;
  166. $template_data = implode( '', file( $template ));
  167. $name = '';
  168. if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
  169. $name = _cleanup_header_comment($name[1]);
  170. if ( !empty( $name ) ) {
  171. $page_templates[trim( $name )] = $basename;
  172. }
  173. }
  174. }
  175. return $page_templates;
  176. }
  177. /**
  178. * Tidies a filename for url display by the theme editor.
  179. *
  180. * @since 2.9.0
  181. * @access private
  182. *
  183. * @param string $fullpath Full path to the theme file
  184. * @param string $containingfolder Path of the theme parent folder
  185. * @return string
  186. */
  187. function _get_template_edit_filename($fullpath, $containingfolder) {
  188. return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
  189. }
  190. /**
  191. * Check if there is an update for a theme available.
  192. *
  193. * Will display link, if there is an update available.
  194. *
  195. * @since 2.7.0
  196. *
  197. * @param object $theme Theme data object.
  198. * @return bool False if no valid info was passed.
  199. */
  200. function theme_update_available( $theme ) {
  201. static $themes_update;
  202. if ( !current_user_can('update_themes' ) )
  203. return;
  204. if ( !isset($themes_update) )
  205. $themes_update = get_site_transient('update_themes');
  206. if ( is_object($theme) && isset($theme->stylesheet) )
  207. $stylesheet = $theme->stylesheet;
  208. elseif ( is_array($theme) && isset($theme['Stylesheet']) )
  209. $stylesheet = $theme['Stylesheet'];
  210. else
  211. return false; //No valid info passed.
  212. if ( isset($themes_update->response[ $stylesheet ]) ) {
  213. $update = $themes_update->response[ $stylesheet ];
  214. $theme_name = is_object($theme) ? $theme->name : (is_array($theme) ? $theme['Name'] : '');
  215. $details_url = add_query_arg(array('TB_iframe' => 'true', 'width' => 1024, 'height' => 800), $update['url']); //Theme browser inside WP? replace this, Also, theme preview JS will override this on the available list.
  216. $update_url = wp_nonce_url('update.php?action=upgrade-theme&amp;theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
  217. $update_onclick = 'onclick="if ( confirm(\'' . esc_js( __("Updating this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to update.") ) . '\') ) {return true;}return false;"';
  218. if ( !is_multisite() ) {
  219. if ( ! current_user_can('update_themes') )
  220. printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version']);
  221. else if ( empty($update['package']) )
  222. printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a>. <em>Automatic update is unavailable for this theme.</em>') . '</strong></p>', $theme_name, $details_url, $update['new_version']);
  223. else
  224. printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a> or <a href="%4$s" %5$s>update automatically</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version'], $update_url, $update_onclick );
  225. }
  226. }
  227. }
  228. /**
  229. * Retrieve list of WordPress theme features (aka theme tags)
  230. *
  231. * @since 3.1.0
  232. *
  233. * @return array Array of features keyed by category with translations keyed by slug.
  234. */
  235. function get_theme_feature_list() {
  236. // Hard-coded list is used if api not accessible.
  237. $features = array(
  238. __('Colors') => array(
  239. 'black' => __( 'Black' ),
  240. 'blue' => __( 'Blue' ),
  241. 'brown' => __( 'Brown' ),
  242. 'gray' => __( 'Gray' ),
  243. 'green' => __( 'Green' ),
  244. 'orange' => __( 'Orange' ),
  245. 'pink' => __( 'Pink' ),
  246. 'purple' => __( 'Purple' ),
  247. 'red' => __( 'Red' ),
  248. 'silver' => __( 'Silver' ),
  249. 'tan' => __( 'Tan' ),
  250. 'white' => __( 'White' ),
  251. 'yellow' => __( 'Yellow' ),
  252. 'dark' => __( 'Dark' ),
  253. 'light' => __( 'Light ')
  254. ),
  255. __('Columns') => array(
  256. 'one-column' => __( 'One Column' ),
  257. 'two-columns' => __( 'Two Columns' ),
  258. 'three-columns' => __( 'Three Columns' ),
  259. 'four-columns' => __( 'Four Columns' ),
  260. 'left-sidebar' => __( 'Left Sidebar' ),
  261. 'right-sidebar' => __( 'Right Sidebar' )
  262. ),
  263. __('Width') => array(
  264. 'fixed-width' => __( 'Fixed Width' ),
  265. 'flexible-width' => __( 'Flexible Width' )
  266. ),
  267. __( 'Features' ) => array(
  268. 'blavatar' => __( 'Blavatar' ),
  269. 'buddypress' => __( 'BuddyPress' ),
  270. 'custom-background' => __( 'Custom Background' ),
  271. 'custom-colors' => __( 'Custom Colors' ),
  272. 'custom-header' => __( 'Custom Header' ),
  273. 'custom-menu' => __( 'Custom Menu' ),
  274. 'editor-style' => __( 'Editor Style' ),
  275. 'featured-image-header' => __( 'Featured Image Header' ),
  276. 'featured-images' => __( 'Featured Images' ),
  277. 'front-page-post-form' => __( 'Front Page Posting' ),
  278. 'full-width-template' => __( 'Full Width Template' ),
  279. 'microformats' => __( 'Microformats' ),
  280. 'post-formats' => __( 'Post Formats' ),
  281. 'rtl-language-support' => __( 'RTL Language Support' ),
  282. 'sticky-post' => __( 'Sticky Post' ),
  283. 'theme-options' => __( 'Theme Options' ),
  284. 'threaded-comments' => __( 'Threaded Comments' ),
  285. 'translation-ready' => __( 'Translation Ready' )
  286. ),
  287. __( 'Subject' ) => array(
  288. 'holiday' => __( 'Holiday' ),
  289. 'photoblogging' => __( 'Photoblogging' ),
  290. 'seasonal' => __( 'Seasonal' )
  291. )
  292. );
  293. if ( !current_user_can('install_themes') )
  294. return $features;
  295. if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
  296. set_site_transient( 'wporg_theme_feature_list', array( ), 10800);
  297. if ( !$feature_list ) {
  298. $feature_list = themes_api( 'feature_list', array( ) );
  299. if ( is_wp_error( $feature_list ) )
  300. return $features;
  301. }
  302. if ( !$feature_list )
  303. return $features;
  304. set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
  305. $category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
  306. 'Features' => __('Features'), 'Subject' => __('Subject') );
  307. // Loop over the wporg canonical list and apply translations
  308. $wporg_features = array();
  309. foreach ( (array) $feature_list as $feature_category => $feature_items ) {
  310. if ( isset($category_translations[$feature_category]) )
  311. $feature_category = $category_translations[$feature_category];
  312. $wporg_features[$feature_category] = array();
  313. foreach ( $feature_items as $feature ) {
  314. if ( isset($features[$feature_category][$feature]) )
  315. $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
  316. else
  317. $wporg_features[$feature_category][$feature] = $feature;
  318. }
  319. }
  320. return $wporg_features;
  321. }
  322. /**
  323. * Retrieve theme installer pages from WordPress Themes API.
  324. *
  325. * It is possible for a theme to override the Themes API result with three
  326. * filters. Assume this is for themes, which can extend on the Theme Info to
  327. * offer more choices. This is very powerful and must be used with care, when
  328. * overridding the filters.
  329. *
  330. * The first filter, 'themes_api_args', is for the args and gives the action as
  331. * the second parameter. The hook for 'themes_api_args' must ensure that an
  332. * object is returned.
  333. *
  334. * The second filter, 'themes_api', is the result that would be returned.
  335. *
  336. * @since 2.8.0
  337. *
  338. * @param string $action
  339. * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
  340. * @return mixed
  341. */
  342. function themes_api($action, $args = null) {
  343. if ( is_array($args) )
  344. $args = (object)$args;
  345. if ( !isset($args->per_page) )
  346. $args->per_page = 24;
  347. $args = apply_filters('themes_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
  348. $res = apply_filters('themes_api', false, $action, $args); //NOTE: Allows a theme to completely override the builtin WordPress.org API.
  349. if ( ! $res ) {
  350. $request = wp_remote_post('http://api.wordpress.org/themes/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
  351. if ( is_wp_error($request) ) {
  352. $res = new WP_Error('themes_api_failed', __('An Unexpected HTTP Error occurred during the API request.'), $request->get_error_message() );
  353. } else {
  354. $res = unserialize( wp_remote_retrieve_body( $request ) );
  355. if ( ! $res )
  356. $res = new WP_Error('themes_api_failed', __('An unknown error occurred.'), wp_remote_retrieve_body( $request ) );
  357. }
  358. }
  359. //var_dump(array($args, $res));
  360. return apply_filters('themes_api_result', $res, $action, $args);
  361. }
  362. ?>