PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/theme.php

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