Changeset 332191
- Timestamp:
- 01/13/2011 07:22:12 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
thesis-restore-points/trunk/thesis-restore-points.php
r332187 r332191 8 8 Author URI: http://thesify.com 9 9 */ 10 define( 'TRP_DIR', WP_CONTENT_DIR . '/uploads/thesis-restore-points' ); 11 define( 'TRP_URL', WP_CONTENT_URL . '/uploads/thesis-restore-points' ); 12 abstract class ThesisRestorePoints 13 { 14 static function init() 15 { 16 add_action( 'wp_ajax_trp', array( __CLASS__, 'ajax' ) ); 17 add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ), 10, 1 ); 18 register_post_type( 'thesis-restore-point', array( 10 define('TRP_DIR', WP_CONTENT_DIR . '/uploads/thesis-restore-points'); 11 define('TRP_URL', WP_CONTENT_URL . '/uploads/thesis-restore-points'); 12 abstract class ThesisRestorePoints { 13 static function init() { 14 add_action('wp_ajax_trp', array(__CLASS__, 'ajax')); 15 add_action('admin_menu', array(__CLASS__, 'admin_menu'), 10, 1); 16 register_post_type('thesis-restore-point', array( 19 17 'public' => false, 20 18 'publicly_queryable' => false, 21 'show_ui' => false, 19 'show_ui' => false, 22 20 'query_var' => false, 23 21 'rewrite' => false, 24 22 'capability_type' => 'post', 25 23 'hierarchical' => false, 26 'supports' => array( 'title', 'editor', 'author' ) 27 ) ); 28 29 } 30 31 static function admin_menu() 32 { 33 add_options_page( 'Thesis Restore Points Options', 'Thesis Restore Points', 'manage_options', 'thesis-restore-points', array( __CLASS__, 'plugin_options' ) ); 34 } 35 36 static function plugin_options() 37 { 38 ?> 24 'supports' => array('title', 'editor', 'author') 25 )); 26 27 } 28 29 static function admin_menu() { 30 add_options_page('Thesis Restore Points Options', 'Thesis Restore Points', 'manage_options', 'thesis-restore-points', array(__CLASS__, 'plugin_options')); 31 } 32 33 static function plugin_options() { 34 ?> 39 35 <script type="text/javascript"> 40 (function($) {41 $(document).ready(function() {36 (function($) { 37 $(document).ready(function() { 42 38 var nonce = '<?php echo wp_create_nonce('thesis-restore-points') ?>'; 43 $('a.delete, a.restore, a.email').live( 'click', function(){39 $('a.delete, a.restore, a.email').live('click', function() { 44 40 var $this = $(this); 45 41 var id = $this.closest("tr").attr('data-id'); 46 42 //alert(id); 47 43 var method = $this.hasClass('delete') ? 'delete' : ($this.hasClass('restore') ? 'restore' : ($this.hasClass('email') ? 'email' : (''))); 48 if (method == '') return;44 if (method == '') return; 49 45 $.get(ajaxurl, { 50 46 'action' : 'trp', … … 53 49 'r': Math.random(), 54 50 'id' : id 55 }, function(data) { 56 if(data == 1) 57 { 58 if(method == 'delete') 59 $this.closest('tr').hide(); 60 if(method == 'email') 61 alert('Email sent!'); 62 if(method == 'restore') 63 alert('Restored'); 64 } 65 } ); 51 }, function(data) { 52 if (data == 1) { 53 if (method == 'delete') 54 $this.closest('tr').hide(); 55 if (method == 'email') 56 alert('Email sent!'); 57 if (method == 'restore') 58 alert('Restored'); 59 } 60 }); 66 61 67 62 return false; 68 63 }); 69 64 70 var create_backup = function() {65 var create_backup = function() { 71 66 var name = $("#name").val(); 72 67 $.get(ajaxurl, { … … 75 70 'nonce': nonce, 76 71 'name' : name 77 }, function(data) { $(data).insertAfter('#current-backups tr:first'); } ); 72 }, function(data) { 73 $(data).insertAfter('#current-backups tr:first'); 74 }); 78 75 79 76 return false; 80 77 } 81 78 $('a.new').click(create_backup); 82 $('#name').keypress(function(e){if(e.which == 13 || e.which == 10) return create_backup(); }); 79 $('#name').keypress(function(e) { 80 if (e.which == 13 || e.which == 10) return create_backup(); 81 }); 83 82 }); 84 83 })(jQuery); 85 84 </script> 86 <div class="wrap"> 87 <h2>Thesis Restore Points</h2> 88 <h3>Create Backup</h3> 89 90 <input type="text" id="name" name="name" value="Backup Name" /> 91 <a class="button new">Go</a> 92 93 <h3>Current Backups</h3> 94 <table id="current-backups"class="widefat" cellspacing="0" style="width:700px"> 95 <thead> 96 <tr> 97 <th>Date created</th> 98 <th>Description</th> 99 <th></th> 100 </tr> 101 </thead> 102 <tbody> 103 <?php 104 $posts = get_posts( array( 'post_type' => 'thesis-restore-point', 'numberposts' => -1 ) ); 105 foreach ( $posts as $post ) { 106 self::render_row( $post ); 107 } 108 ?> 109 </tbody> 110 </table> 111 </div> 112 <?php 113 } 114 115 static function ajax() 116 { 117 isset( $_GET['method'] ) && current_user_can( 'manage_options' ) ? $method = $_GET['method'] : die; 118 if( ! wp_verify_nonce( $_GET['nonce'], 'thesis-restore-points' ) ) die; 119 switch( $method ) 85 <div class="wrap"> 86 <h2>Thesis Restore Points</h2> 87 88 <h3>Create Backup</h3> 89 90 <label for="name">Backup Name</label> <input type="text" id="name" name="name" value="first backup"/> 91 <a class="button new">Go</a> 92 93 <h3>Current Backups</h3> 94 <table id="current-backups" class="widefat" cellspacing="0" style="width:700px"> 95 <thead> 96 <tr> 97 <th>Date created</th> 98 <th>Description</th> 99 <th></th> 100 </tr> 101 </thead> 102 <tbody> 103 <?php 104 $posts = get_posts(array('post_type' => 'thesis-restore-point', 'numberposts' => -1)); 105 foreach ($posts as $post) { 106 self::render_row($post); 107 } 108 ?> 109 </tbody> 110 </table> 111 </div> 112 <?php 113 114 } 115 116 static function ajax() { 117 isset($_GET['method']) && current_user_can('manage_options') ? $method = $_GET['method'] : die; 118 if (!wp_verify_nonce($_GET['nonce'], 'thesis-restore-points')) die; 119 switch ($method) 120 120 { 121 121 case 'backup': … … 124 124 $uploads_dir = wp_upload_dir(); 125 125 $uploads_dir = $uploads_dir['path']; 126 126 127 127 $custom_folder = THESIS_CUSTOM; 128 129 $thesis_options = serialize( get_option( 'thesis_options' ));130 $thesis_design_options = serialize( get_option( 'thesis_design_options' ));131 128 129 $thesis_options = serialize(get_option('thesis_options')); 130 $thesis_design_options = serialize(get_option('thesis_design_options')); 131 132 132 $files = array(); 133 133 $files[] = TRP_DIR . '/thesis_options.dat'; 134 134 $files[] = TRP_DIR . '/thesis_design_options.dat'; 135 136 file_put_contents( $files[0], $thesis_options ); 137 file_put_contents( $files[1], $thesis_design_options ); 138 139 140 $backup_file = '/backup-' . md5( mt_rand() ) . '.zip'; 141 $archive = new PclZip( TRP_DIR . $backup_file ); 142 143 function exclude_cache( $p_event, &$p_header ) 144 { 145 $s = array( '/', '\\' ); 146 if( strpos( $p_header['filename'], str_replace( '\\', '/', THESIS_CUSTOM . '/cache' ) ) === 0 ) 135 136 file_put_contents($files[0], $thesis_options); 137 file_put_contents($files[1], $thesis_design_options); 138 139 140 $backup_file = '/backup-' . md5(mt_rand()) . '.zip'; 141 $archive = new PclZip(TRP_DIR . $backup_file); 142 143 function exclude_cache($p_event, &$p_header) { 144 $s = array('/', '\\'); 145 if (strpos($p_header['filename'], str_replace('\\', '/', THESIS_CUSTOM . '/cache')) === 0) 147 146 return 0; 148 147 return 1; 149 148 } 150 151 $o = $archive->add( THESIS_CUSTOM, PCLZIP_OPT_REMOVE_PATH, THESIS_CUSTOM, PCLZIP_OPT_ADD_PATH, 'custom', PCLZIP_CB_PRE_ADD, 'exclude_cache');152 $o = $archive->add( $files, PCLZIP_OPT_REMOVE_PATH, TRP_DIR);153 154 if ( $o == 0) {155 echo( "Error : ".$archive->errorInfo( true ));149 150 $o = $archive->add(THESIS_CUSTOM, PCLZIP_OPT_REMOVE_PATH, THESIS_CUSTOM, PCLZIP_OPT_ADD_PATH, 'custom', PCLZIP_CB_PRE_ADD, 'exclude_cache'); 151 $o = $archive->add($files, PCLZIP_OPT_REMOVE_PATH, TRP_DIR); 152 153 if ($o == 0) { 154 echo("Error : " . $archive->errorInfo(true)); 156 155 } else { 157 $title = isset( $_GET['name'] ) && strlen( trim( $_GET['name'] ) ) > 0 ? $_GET['name'] : 'Backup on ' . date( 'M D s');158 $post = array( 'post_title' => $title, 'post_status' => 'publish', 'post_type' => 'thesis-restore-point');159 $post_id = wp_insert_post( $post);160 update_post_meta( $post_id, 'url', TRP_URL . $backup_file);161 update_post_meta( $post_id, 'path', str_replace( '\\', '/', TRP_DIR ) . $backup_file);162 } 163 @unlink( $files[0]);164 @unlink( $files[1]);165 166 die( self::render_row( get_post( $post_id ) ));156 $title = isset($_GET['name']) && strlen(trim($_GET['name'])) > 0 ? $_GET['name'] : 'Backup on ' . date('M D s'); 157 $post = array('post_title' => $title, 'post_status' => 'publish', 'post_type' => 'thesis-restore-point'); 158 $post_id = wp_insert_post($post); 159 update_post_meta($post_id, 'url', TRP_URL . $backup_file); 160 update_post_meta($post_id, 'path', str_replace('\\', '/', TRP_DIR) . $backup_file); 161 } 162 @unlink($files[0]); 163 @unlink($files[1]); 164 165 die(self::render_row(get_post($post_id))); 167 166 break; 168 167 169 168 case 'restore': 170 171 169 170 172 171 break; 173 172 174 173 case 'email': 175 if ( !isset( $_GET['id'] ) ) die( '0');174 if (!isset($_GET['id'])) die('0'); 176 175 $post_id = $_GET['id']; 177 $post = get_post( $post_id);178 if ( !$post ) die( '0');179 180 181 $email = get_option( 'admin_email');182 $subject = 'Thesis Custom Folder Backup on ' . current_time( 'mysql');176 $post = get_post($post_id); 177 if (!$post) die('0'); 178 179 180 $email = get_option('admin_email'); 181 $subject = 'Thesis Custom Folder Backup on ' . current_time('mysql'); 183 182 $message = 'Attached'; 184 183 $headers = ''; 185 $attachments = get_post_meta( $post_id, 'path');186 187 wp_mail( $email, $subject, $message, $headers, $attachments);184 $attachments = get_post_meta($post_id, 'path'); 185 186 wp_mail($email, $subject, $message, $headers, $attachments); 188 187 break; 189 188 190 189 case 'delete': 191 190 // unlink file 192 if ( !isset( $_GET['id'] ) ) die( '0');191 if (!isset($_GET['id'])) die('0'); 193 192 $post_id = $_GET['id']; 194 $post = get_post( $post_id);195 if ( !$post ) die( '0');196 197 $file = get_post_meta( $post_id, 'path', true);198 199 @unlink( $file);200 201 echo ( wp_delete_post( $post_id ) !== false ? 1 : 0);193 $post = get_post($post_id); 194 if (!$post) die('0'); 195 196 $file = get_post_meta($post_id, 'path', true); 197 198 @unlink($file); 199 200 echo (wp_delete_post($post_id) !== false ? 1 : 0); 202 201 } 203 202 die; 204 203 } 205 206 static function init_backup() 207 { 208 require_once( ABSPATH . '/wp-admin/includes/class-pclzip.php' ); 209 if( !is_dir( TRP_DIR ) ) 210 wp_mkdir_p( TRP_DIR ); 211 if( !file_exists( TRP_DIR . '/index.html' ) ) 212 { 213 file_put_contents( TRP_DIR . '/index.html', '' ); 204 205 static function init_backup() { 206 require_once(ABSPATH . '/wp-admin/includes/class-pclzip.php'); 207 if (!is_dir(TRP_DIR)) 208 wp_mkdir_p(TRP_DIR); 209 if (!file_exists(TRP_DIR . '/index.html')) { 210 file_put_contents(TRP_DIR . '/index.html', ''); 214 211 } 215 212 } 216 213 217 private function render_row( $post ) 218 { 219 echo '<tr data-id='.$post->ID.'>'; 214 private static function render_row($post) { 215 echo '<tr data-id=' . $post->ID . '>'; 220 216 echo '<td>' . $post->post_date . '</td>'; 221 217 echo '<td>' . $post->post_title . '</td>'; 222 echo '<td>' . '<a href="' . get_post_meta( $post->ID, 'url', true) . '">Download</a> | ';218 echo '<td>' . '<a href="' . get_post_meta($post->ID, 'url', true) . '">Download</a> | '; 223 219 echo '<a class="restore" href="#">Restore</a> | '; 224 220 echo '<a class="email" href="">Email me</a> | '; … … 227 223 echo '</tr>'; 228 224 } 229 225 230 226 } 231 227
Note: See TracChangeset
for help on using the changeset viewer.