Plugin Directory

Changeset 332191


Ignore:
Timestamp:
01/13/2011 07:22:12 PM (15 years ago)
Author:
thesify
Message:

<3 PHPStorm's refactor code feature

File:
1 edited

Legend:

Unmodified
Added
Removed
  • thesis-restore-points/trunk/thesis-restore-points.php

    r332187 r332191  
    88Author URI: http://thesify.com
    99*/
    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(
     10define('TRP_DIR', WP_CONTENT_DIR . '/uploads/thesis-restore-points');
     11define('TRP_URL', WP_CONTENT_URL . '/uploads/thesis-restore-points');
     12abstract 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(
    1917            'public' => false,
    2018            'publicly_queryable' => false,
    21             'show_ui' => false, 
     19            'show_ui' => false,
    2220            'query_var' => false,
    2321            'rewrite' => false,
    2422            'capability_type' => 'post',
    2523            '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        ?>
    3935    <script type="text/javascript">
    40         (function($){
    41             $(document).ready(function(){
     36        (function($) {
     37            $(document).ready(function() {
    4238                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() {
    4440                    var $this = $(this);
    4541                    var id = $this.closest("tr").attr('data-id');
    4642                    //alert(id);
    4743                    var method = $this.hasClass('delete') ? 'delete' : ($this.hasClass('restore') ? 'restore' : ($this.hasClass('email') ? 'email' : ('')));
    48                     if( method == '') return;
     44                    if (method == '') return;
    4945                    $.get(ajaxurl, {
    5046                        'action' : 'trp',
     
    5349                        'r': Math.random(),
    5450                        '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                    });
    6661
    6762                    return false;
    6863                });
    6964
    70                 var create_backup = function(){
     65                var create_backup = function() {
    7166                    var name = $("#name").val();
    7267                    $.get(ajaxurl, {
     
    7570                        'nonce': nonce,
    7671                        'name' : name
    77                     }, function(data) { $(data).insertAfter('#current-backups tr:first'); } );
     72                    }, function(data) {
     73                        $(data).insertAfter('#current-backups tr:first');
     74                    });
    7875
    7976                    return false;
    8077                }
    8178                $('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                });
    8382            });
    8483        })(jQuery);
    8584    </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)
    120120        {
    121121            case 'backup':
     
    124124                $uploads_dir = wp_upload_dir();
    125125                $uploads_dir = $uploads_dir['path'];
    126                
     126
    127127                $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
    132132                $files = array();
    133133                $files[] = TRP_DIR . '/thesis_options.dat';
    134134                $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)
    147146                        return 0;
    148147                    return 1;
    149148                }
    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));
    156155                } 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)));
    167166                break;
    168                
     167
    169168            case 'restore':
    170            
    171            
     169
     170
    172171                break;
    173            
     172
    174173            case 'email':
    175                 if( !isset( $_GET['id'] ) ) die( '0' );
     174                if (!isset($_GET['id'])) die('0');
    176175                $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');
    183182                $message = 'Attached';
    184183                $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);
    188187                break;
    189188
    190189            case 'delete':
    191190                // unlink file
    192                 if( !isset( $_GET['id'] ) ) die( '0' );
     191                if (!isset($_GET['id'])) die('0');
    193192                $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);
    202201        }
    203202        die;
    204203    }
    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', '');
    214211        }
    215212    }
    216213
    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 . '>';
    220216        echo '<td>' . $post->post_date . '</td>';
    221217        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> | ';
    223219        echo '<a class="restore" href="#">Restore</a> | ';
    224220        echo '<a class="email" href="">Email me</a> | ';
     
    227223        echo '</tr>';
    228224    }
    229    
     225
    230226}
    231227
Note: See TracChangeset for help on using the changeset viewer.