PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/svbtle/vendor/UCF-Theme-Updater/assets.php

https://github.com/zigbe/wp-svbtle
PHP | 296 lines | 192 code | 54 blank | 50 comment | 52 complexity | bf334c6b935710b8d1cc9d03edc3beb2 MD5 | raw file
  1. <?php
  2. /******************************************************************************\
  3. http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/update.php?rev=17984#L264
  4. changes:
  5. - disables the iframe popup and uses a new window and makes a pop-up linking to the github project
  6. - calls 'upgrade-github-theme' vs 'upgrade-theme'
  7. \******************************************************************************/
  8. function github_theme_update_row( $theme_key, $theme ) {
  9. $current = get_site_transient( 'update_themes' );
  10. if (
  11. !isset( $current->response[ $theme_key ] ) and
  12. !isset( $current->up_to_date[ $theme_key ] )
  13. )
  14. return false;
  15. $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
  16. // custom additions
  17. if(isset($current->up_to_date[$theme_key])){
  18. $rollback = $current->up_to_date[$theme_key]['rollback'];
  19. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message-gtu update-ok">';
  20. echo 'Theme is up-to-date! ';
  21. if (current_user_can('update_themes') ){
  22. if(count($rollback) > 0){
  23. echo "<strong>Rollback to:</strong> ";
  24. // display last three tags
  25. for($i=0; $i<3 ; $i++){
  26. $tag = array_pop($rollback);
  27. if(empty($tag)) break;
  28. if($i>0) echo ", ";
  29. printf('<a href="%s%s">%s</a>',
  30. wp_nonce_url( self_admin_url('update.php?action=upgrade-github-theme&theme=') . $theme_key, 'upgrade-theme_' . $theme_key),
  31. '&rollback=' . urlencode($tag),
  32. $tag);
  33. }
  34. } else {
  35. echo "No previous tags to rollback to.";
  36. }
  37. }
  38. } else {
  39. $r = $current->response[ $theme_key ];
  40. if( isset($r['error']) ){
  41. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message-gtu update-error">';
  42. printf('Error with Github Theme Updater. %1$s', $r['error']);
  43. } else {
  44. $themes_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  45. $theme_name = wp_kses( $theme['Name'], $themes_allowedtags );
  46. $github_url = esc_url($r['url']);
  47. $diff_url = esc_url($r['url'] . '/compare/' . $theme['Version'] . '...' . $r['new_version']);
  48. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message-gtu">';
  49. printf('Github has as a new version of <a href="%s" target="blank">%s</a>. ', $github_url, $theme_name);
  50. printf('View <a href="%s" target="blank">version diff</a> with %s. ', $diff_url, $r['new_version']);
  51. if (current_user_can('update_themes')){
  52. if(empty($r['package'])){
  53. echo '<em>Automatic update is unavailable for this plugin.</em>';
  54. } else {
  55. printf('<a href="%s">Update automatically</a>.', wp_nonce_url( self_admin_url('update.php?action=upgrade-github-theme&theme=') . $theme_key, 'upgrade-theme_' . $theme_key));
  56. }
  57. }
  58. }
  59. do_action( "in_theme_update_message-$theme_key", $theme, $r );
  60. }
  61. echo '</div></td></tr>';
  62. }
  63. // http://codex.wordpress.org/Function_Reference/wp_enqueue_style
  64. add_action( 'admin_init', 'theme_upgrader_stylesheet' );
  65. function theme_upgrader_stylesheet() {
  66. $style_url = WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/admin-style.css';
  67. wp_register_style('theme_updater_style', $style_url);
  68. wp_enqueue_style( 'theme_updater_style');
  69. }
  70. /******************************************************************************\
  71. Most of this code is pulled directly from the WP source
  72. modifications are noted.
  73. \******************************************************************************/
  74. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  75. class Github_Theme_Upgrader extends Theme_Upgrader {
  76. function download_url( $url ) {
  77. /*
  78. http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/file.php?rev=17928#L467
  79. changes:
  80. - wanted a timeout < 5 min
  81. - SSL fails when trying to access github
  82. */
  83. if ( ! $url )
  84. return new WP_Error('http_no_url', __('Invalid URL Provided.'));
  85. $tmpfname = wp_tempnam($url);
  86. if ( ! $tmpfname )
  87. return new WP_Error('http_no_file', __('Could not create Temporary file.'));
  88. $handle = @fopen($tmpfname, 'wb');
  89. if ( ! $handle )
  90. return new WP_Error('http_no_file', __('Could not create Temporary file.'));
  91. // This! is the one line I wanted to get at
  92. $response = wp_remote_get($url , array('sslverify' => false, 'timeout' => 30));
  93. if ( is_wp_error($response) ) {
  94. fclose($handle);
  95. unlink($tmpfname);
  96. return $response;
  97. }
  98. if ( $response['response']['code'] != '200' ){
  99. fclose($handle);
  100. unlink($tmpfname);
  101. return new WP_Error('http_404', trim($response['response']['message']));
  102. }
  103. fwrite($handle, $response['body']);
  104. fclose($handle);
  105. return $tmpfname;
  106. }
  107. function download_package($package) {
  108. /*
  109. http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/class-wp-upgrader.php?rev=17771#L108
  110. changes:
  111. - use customized download_url
  112. */
  113. if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
  114. return $package; //must be a local file..
  115. if ( empty($package) )
  116. return new WP_Error('no_package', $this->strings['no_package']);
  117. $this->skin->feedback('downloading_package', $package);
  118. // This! is the one line I wanted to get at
  119. $download_file = $this->download_url($package);
  120. if ( is_wp_error($download_file) )
  121. return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
  122. return $download_file;
  123. }
  124. function install_package($args = array()) {
  125. /*
  126. This funciton can go away once my patch has been applied:
  127. http://core.trac.wordpress.org/ticket/17680
  128. */
  129. global $wp_filesystem;
  130. $defaults = array( 'source' => '', 'destination' => '', //Please always pass these
  131. 'clear_destination' => false, 'clear_working' => false,
  132. 'hook_extra' => array());
  133. $args = wp_parse_args($args, $defaults);
  134. extract($args);
  135. @set_time_limit( 300 );
  136. if ( empty($source) || empty($destination) )
  137. return new WP_Error('bad_request', $this->strings['bad_request']);
  138. $this->skin->feedback('installing_package');
  139. $res = apply_filters('upgrader_pre_install', true, $hook_extra);
  140. if ( is_wp_error($res) )
  141. return $res;
  142. //Retain the Original source and destinations
  143. $remote_source = $source;
  144. $local_destination = $destination;
  145. $source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
  146. $remote_destination = $wp_filesystem->find_folder($local_destination);
  147. //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
  148. if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
  149. $source = trailingslashit($source) . trailingslashit($source_files[0]);
  150. elseif ( count($source_files) == 0 )
  151. return new WP_Error('bad_package', $this->strings['bad_package']); //There are no files?
  152. //else //Its only a single file, The upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
  153. //Hook ability to change the source file location..
  154. $source = apply_filters('upgrader_source_selection', $source, $remote_source, $this);
  155. if ( is_wp_error($source) )
  156. return $source;
  157. //Has the source location changed? If so, we need a new source_files list.
  158. if ( $source !== $remote_source )
  159. $source_files = array_keys( $wp_filesystem->dirlist($source) );
  160. //Protection against deleting files in any important base directories.
  161. if ( in_array( $destination, array(ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes') ) ) {
  162. $remote_destination = trailingslashit($remote_destination) . trailingslashit(basename($source));
  163. $destination = trailingslashit($destination) . trailingslashit(basename($source));
  164. }
  165. $tempdir = untrailingslashit($remote_destination) . ".tmp-" . time() . "/";
  166. if ( $wp_filesystem->exists($remote_destination) ) {
  167. if ( $clear_destination ) {
  168. //Try to rename original theme (also works as a backup)
  169. $moved = @rename($remote_destination, $tempdir);
  170. if ( ! $moved )
  171. return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
  172. //We're going to clear the destination if theres something there
  173. $this->skin->feedback('remove_old');
  174. $removed = $wp_filesystem->delete($remote_destination, true);
  175. $removed = apply_filters('upgrader_clear_destination', $removed, $local_destination, $remote_destination, $hook_extra);
  176. if ( is_wp_error($removed) )
  177. return $removed;
  178. else if ( ! $removed )
  179. return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
  180. } else {
  181. //If we're not clearing the destination folder and something exists there allready, Bail.
  182. //But first check to see if there are actually any files in the folder.
  183. $_files = $wp_filesystem->dirlist($remote_destination);
  184. if ( ! empty($_files) ) {
  185. $wp_filesystem->delete($remote_source, true); //Clear out the source files.
  186. return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
  187. }
  188. }
  189. }
  190. //Create destination if needed
  191. if ( !$wp_filesystem->exists($remote_destination) )
  192. if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
  193. return new WP_Error('mkdir_failed', $this->strings['mkdir_failed'], $remote_destination);
  194. // Copy new version of item into place.
  195. $result = copy_dir($source, $remote_destination);
  196. if ( is_wp_error($result) ) {
  197. if ( $clear_working )
  198. $wp_filesystem->delete($remote_source, true);
  199. return $result;
  200. }
  201. //Clear the Working folder?
  202. if ( $clear_working )
  203. $wp_filesystem->delete($remote_source, true);
  204. $destination_name = basename( str_replace($local_destination, '', $destination) );
  205. if ( '.' == $destination_name )
  206. $destination_name = '';
  207. $this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
  208. $res = apply_filters('upgrader_post_install', true, $hook_extra, $this->result);
  209. if ( is_wp_error($res) ) {
  210. $this->result = $res;
  211. return $res;
  212. }
  213. // Remove temporary backup
  214. $removed = $wp_filesystem->delete($tempdir, true);
  215. if( !$removed ) $this->skin->feedback("Could not remove the temporary theme directory.");
  216. //Bombard the calling function will all the info which we've just used.
  217. return $this->result;
  218. }
  219. }
  220. add_action('update-custom_upgrade-github-theme', 'github_theme_updater', 10, 2);
  221. function github_theme_updater(){
  222. /*
  223. http://core.trac.wordpress.org/browser/trunk/wp-admin/update.php?rev=17632#L145
  224. changes:
  225. - use customized theme upgrader
  226. */
  227. if ( ! current_user_can('update_themes') )
  228. wp_die(__('You do not have sufficient permissions to update themes for this site.'));
  229. $theme = isset($_REQUEST['theme']) ? urldecode($_REQUEST['theme']) : '';
  230. check_admin_referer('upgrade-theme_' . $theme);
  231. add_thickbox();
  232. wp_enqueue_script('theme-preview');
  233. $title = __('Update Theme');
  234. $parent_file = 'themes.php';
  235. $submenu_file = 'themes.php';
  236. require_once(ABSPATH . 'wp-admin/admin-header.php');
  237. $nonce = 'upgrade-theme_' . $theme;
  238. $url = 'update.php?action=upgrade-theme&theme=' . $theme;
  239. $upgrader = new Github_Theme_Upgrader( new Theme_Upgrader_Skin( compact('title', 'nonce', 'url', 'theme') ) );
  240. $upgrader->upgrade($theme);
  241. include(ABSPATH . 'wp-admin/admin-footer.php');
  242. }