PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress3.4.2/wp-admin/includes/theme.php

https://bitbucket.org/ch3tag/mothers
PHP | 295 lines | 181 code | 40 blank | 74 comment | 34 complexity | b317af467b16bafb02f409fa12ec07d9 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Theme Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Remove a theme
  10. *
  11. * @since 2.8.0
  12. *
  13. * @param string $stylesheet Stylesheet of the theme to delete
  14. * @param string $redirect Redirect to page when complete.
  15. * @return mixed
  16. */
  17. function delete_theme($stylesheet, $redirect = '') {
  18. global $wp_filesystem;
  19. if ( empty($stylesheet) )
  20. return false;
  21. ob_start();
  22. if ( empty( $redirect ) )
  23. $redirect = wp_nonce_url('themes.php?action=delete&stylesheet=' . $stylesheet, 'delete-theme_' . $stylesheet);
  24. if ( false === ($credentials = request_filesystem_credentials($redirect)) ) {
  25. $data = ob_get_contents();
  26. ob_end_clean();
  27. if ( ! empty($data) ){
  28. include_once( ABSPATH . 'wp-admin/admin-header.php');
  29. echo $data;
  30. include( ABSPATH . 'wp-admin/admin-footer.php');
  31. exit;
  32. }
  33. return;
  34. }
  35. if ( ! WP_Filesystem($credentials) ) {
  36. request_filesystem_credentials($url, '', true); // Failed to connect, Error and request again
  37. $data = ob_get_contents();
  38. ob_end_clean();
  39. if ( ! empty($data) ) {
  40. include_once( ABSPATH . 'wp-admin/admin-header.php');
  41. echo $data;
  42. include( ABSPATH . 'wp-admin/admin-footer.php');
  43. exit;
  44. }
  45. return;
  46. }
  47. if ( ! is_object($wp_filesystem) )
  48. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  49. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  50. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
  51. //Get the base plugin folder
  52. $themes_dir = $wp_filesystem->wp_themes_dir();
  53. if ( empty($themes_dir) )
  54. return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.'));
  55. $themes_dir = trailingslashit( $themes_dir );
  56. $theme_dir = trailingslashit($themes_dir . $stylesheet);
  57. $deleted = $wp_filesystem->delete($theme_dir, true);
  58. if ( ! $deleted )
  59. return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $stylesheet) );
  60. // Force refresh of theme update information
  61. delete_site_transient('update_themes');
  62. return true;
  63. }
  64. /**
  65. * Get the Page Templates available in this theme
  66. *
  67. * @since 1.5.0
  68. *
  69. * @return array Key is the template name, value is the filename of the template
  70. */
  71. function get_page_templates() {
  72. return array_flip( wp_get_theme()->get_page_templates() );
  73. }
  74. /**
  75. * Tidies a filename for url display by the theme editor.
  76. *
  77. * @since 2.9.0
  78. * @access private
  79. *
  80. * @param string $fullpath Full path to the theme file
  81. * @param string $containingfolder Path of the theme parent folder
  82. * @return string
  83. */
  84. function _get_template_edit_filename($fullpath, $containingfolder) {
  85. return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
  86. }
  87. /**
  88. * Check if there is an update for a theme available.
  89. *
  90. * Will display link, if there is an update available.
  91. *
  92. * @since 2.7.0
  93. *
  94. * @param object $theme Theme data object.
  95. * @return bool False if no valid info was passed.
  96. */
  97. function theme_update_available( $theme ) {
  98. static $themes_update;
  99. if ( !current_user_can('update_themes' ) )
  100. return;
  101. if ( !isset($themes_update) )
  102. $themes_update = get_site_transient('update_themes');
  103. if ( ! is_a( $theme, 'WP_Theme' ) )
  104. return;
  105. $stylesheet = $theme->get_stylesheet();
  106. if ( isset($themes_update->response[ $stylesheet ]) ) {
  107. $update = $themes_update->response[ $stylesheet ];
  108. $theme_name = $theme->display('Name');
  109. $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.
  110. $update_url = wp_nonce_url('update.php?action=upgrade-theme&amp;theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
  111. $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;"';
  112. if ( !is_multisite() ) {
  113. if ( ! current_user_can('update_themes') )
  114. 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']);
  115. else if ( empty($update['package']) )
  116. 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']);
  117. else
  118. 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 now</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version'], $update_url, $update_onclick );
  119. }
  120. }
  121. }
  122. /**
  123. * Retrieve list of WordPress theme features (aka theme tags)
  124. *
  125. * @since 3.1.0
  126. *
  127. * @param bool $api Optional. Whether try to fetch tags from the WP.org API. Defaults to true.
  128. * @return array Array of features keyed by category with translations keyed by slug.
  129. */
  130. function get_theme_feature_list( $api = true ) {
  131. // Hard-coded list is used if api not accessible.
  132. $features = array(
  133. __('Colors') => array(
  134. 'black' => __( 'Black' ),
  135. 'blue' => __( 'Blue' ),
  136. 'brown' => __( 'Brown' ),
  137. 'gray' => __( 'Gray' ),
  138. 'green' => __( 'Green' ),
  139. 'orange' => __( 'Orange' ),
  140. 'pink' => __( 'Pink' ),
  141. 'purple' => __( 'Purple' ),
  142. 'red' => __( 'Red' ),
  143. 'silver' => __( 'Silver' ),
  144. 'tan' => __( 'Tan' ),
  145. 'white' => __( 'White' ),
  146. 'yellow' => __( 'Yellow' ),
  147. 'dark' => __( 'Dark' ),
  148. 'light' => __( 'Light' ),
  149. ),
  150. __('Columns') => array(
  151. 'one-column' => __( 'One Column' ),
  152. 'two-columns' => __( 'Two Columns' ),
  153. 'three-columns' => __( 'Three Columns' ),
  154. 'four-columns' => __( 'Four Columns' ),
  155. 'left-sidebar' => __( 'Left Sidebar' ),
  156. 'right-sidebar' => __( 'Right Sidebar' ),
  157. ),
  158. __('Width') => array(
  159. 'fixed-width' => __( 'Fixed Width' ),
  160. 'flexible-width' => __( 'Flexible Width' ),
  161. ),
  162. __( 'Features' ) => array(
  163. 'blavatar' => __( 'Blavatar' ),
  164. 'buddypress' => __( 'BuddyPress' ),
  165. 'custom-background' => __( 'Custom Background' ),
  166. 'custom-colors' => __( 'Custom Colors' ),
  167. 'custom-header' => __( 'Custom Header' ),
  168. 'custom-menu' => __( 'Custom Menu' ),
  169. 'editor-style' => __( 'Editor Style' ),
  170. 'featured-image-header' => __( 'Featured Image Header' ),
  171. 'featured-images' => __( 'Featured Images' ),
  172. 'front-page-post-form' => __( 'Front Page Posting' ),
  173. 'full-width-template' => __( 'Full Width Template' ),
  174. 'microformats' => __( 'Microformats' ),
  175. 'post-formats' => __( 'Post Formats' ),
  176. 'rtl-language-support' => __( 'RTL Language Support' ),
  177. 'sticky-post' => __( 'Sticky Post' ),
  178. 'theme-options' => __( 'Theme Options' ),
  179. 'threaded-comments' => __( 'Threaded Comments' ),
  180. 'translation-ready' => __( 'Translation Ready' ),
  181. ),
  182. __( 'Subject' ) => array(
  183. 'holiday' => __( 'Holiday' ),
  184. 'photoblogging' => __( 'Photoblogging' ),
  185. 'seasonal' => __( 'Seasonal' ),
  186. )
  187. );
  188. if ( ! $api || ! current_user_can( 'install_themes' ) )
  189. return $features;
  190. if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
  191. set_site_transient( 'wporg_theme_feature_list', array( ), 10800);
  192. if ( !$feature_list ) {
  193. $feature_list = themes_api( 'feature_list', array( ) );
  194. if ( is_wp_error( $feature_list ) )
  195. return $features;
  196. }
  197. if ( !$feature_list )
  198. return $features;
  199. set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
  200. $category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
  201. 'Features' => __('Features'), 'Subject' => __('Subject') );
  202. // Loop over the wporg canonical list and apply translations
  203. $wporg_features = array();
  204. foreach ( (array) $feature_list as $feature_category => $feature_items ) {
  205. if ( isset($category_translations[$feature_category]) )
  206. $feature_category = $category_translations[$feature_category];
  207. $wporg_features[$feature_category] = array();
  208. foreach ( $feature_items as $feature ) {
  209. if ( isset($features[$feature_category][$feature]) )
  210. $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
  211. else
  212. $wporg_features[$feature_category][$feature] = $feature;
  213. }
  214. }
  215. return $wporg_features;
  216. }
  217. /**
  218. * Retrieve theme installer pages from WordPress Themes API.
  219. *
  220. * It is possible for a theme to override the Themes API result with three
  221. * filters. Assume this is for themes, which can extend on the Theme Info to
  222. * offer more choices. This is very powerful and must be used with care, when
  223. * overridding the filters.
  224. *
  225. * The first filter, 'themes_api_args', is for the args and gives the action as
  226. * the second parameter. The hook for 'themes_api_args' must ensure that an
  227. * object is returned.
  228. *
  229. * The second filter, 'themes_api', is the result that would be returned.
  230. *
  231. * @since 2.8.0
  232. *
  233. * @param string $action
  234. * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
  235. * @return mixed
  236. */
  237. function themes_api($action, $args = null) {
  238. if ( is_array($args) )
  239. $args = (object)$args;
  240. if ( !isset($args->per_page) )
  241. $args->per_page = 24;
  242. $args = apply_filters('themes_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
  243. $res = apply_filters('themes_api', false, $action, $args); //NOTE: Allows a theme to completely override the builtin WordPress.org API.
  244. if ( ! $res ) {
  245. $request = wp_remote_post('http://api.wordpress.org/themes/info/1.0/', array( 'body' => array('action' => $action, 'request' => serialize($args))) );
  246. if ( is_wp_error($request) ) {
  247. $res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
  248. } else {
  249. $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
  250. if ( ! is_object( $res ) && ! is_array( $res ) )
  251. $res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
  252. }
  253. }
  254. return apply_filters('themes_api_result', $res, $action, $args);
  255. }