Plugin Directory

Changeset 547958


Ignore:
Timestamp:
05/23/2012 10:57:07 AM (13 years ago)
Author:
hel.io
Message:

Fixed some more frequency problems

Location:
backup/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • backup/trunk/backup.php

    r546966 r547958  
    22/*
    33Plugin Name: Backup
    4 Version: 1.1.1
     4Version: 1.1.2
    55Plugin URI: http://hel.io/wordpress/backup/
    66Description: Backup your WordPress website to Google Drive.
     
    6868        'client_secret' => '',
    6969        'last_backup' => '',
    70         'number_of_backups' => 10,
     70        'local_number' => 10,
     71        'drive_number' => 10,
    7172        'local_files' => array(),
    7273        'drive_files' => array(),
     
    7778   
    7879}
     80
     81/**
     82 * Backup Deactivation
     83 *
     84 * This function is called whenever the plugin is being deactivated and removes
     85 * all files and directories it created as well as the options stored in the database.
     86 * It also revokes access to the Google Account associated with it and removes all scheduled events created.
     87 */
     88function backup_deactivation() {
     89    $options = get_option( 'backup_options' );
     90    if ( ! empty( $options['token'] ) )
     91        @file_get_contents( 'https://accounts.google.com/o/oauth2/revoke?token=' . $options['token'] );
     92    delete_option( 'backup_options' );
     93    if ( wp_next_scheduled( 'backup_schedule' ) ) {
     94        wp_clear_scheduled_hook('backup_schedule');
     95    }
     96    rrmdir( WP_CONTENT_DIR . '/backup' );
     97    if ( file_exists( WP_CONTENT_DIR . '/backup.sql' ) )
     98        unlink( WP_CONTENT_DIR . '/backup.sql' );
     99}
     100register_deactivation_hook( __FILE__, 'backup_deactivation' );
    79101
    80102// Add custom schedule intervals
     
    198220                        <th scope="row"><?php _e( 'Store a maximum of', 'backup' ); ?></th>
    199221                        <td>
    200                             <input name='number' type='text' class='small-text' value='<?php echo $options['number_of_backups']; ?>' /> <?php _e( 'backups.', 'backup' ); ?>
    201                             <span class="description"><?php _e( 'Oldest backups will be deleted first.', 'backup' ) ?></span>
    202 
     222                            <input name='local_number' type='text' class='small-text' value='<?php echo $options['local_number']; ?>' /> <?php _e( 'backups locally and ', 'backup' ); ?>
     223                            <input name='drive_number' type='text' class='small-text' value='<?php echo $options['drive_number']; ?>' /> <?php _e( 'backups on Google Drive.', 'backup' ); ?>
    203224                        </td>
    204225                    </tr>
     
    206227                        <th scope="row"><?php _e( 'When to backup', 'backup' ); ?></th>
    207228                        <td>
    208                             <select name='frequency'>   
     229                            <select name='frequency'>
    209230                                <option value='never' <?php selected( 'never', $options['frequency'] ); ?> ><?php _e( 'Never', 'backup' ); ?></option>
    210231                                <option value='daily' <?php selected( 'daily', $options['frequency'] ); ?> ><?php _e( 'Daily', 'backup' ); ?></option>
     
    236257
    237258        // If number isn't an integer stop function execution
    238         if ( ! (int) $_POST['number'] )
     259        $local_number = intval( $_POST['local_number'] );
     260        $drive_number = intval( $_POST['drive_number'] );
     261        if ( $local_number < 0 || $drive_number < 0 )
    239262            return false;
    240263       
    241264        $options['folder'] = $_POST['folder'];
    242265        $options['frequency'] = $_POST['frequency'];
    243         $options['number_of_backups'] = $_POST['number'];
     266        $options['local_number'] = $local_number;
     267        $options['drive_number'] = $drive_number;
    244268
    245269        update_option( 'backup_options', $options );
     270
     271        // If we have already scheduled a backup before, clear it first
     272        if ( wp_next_scheduled( 'backup_schedule' ) ) {
     273            wp_clear_scheduled_hook('backup_schedule');
     274        }
    246275
    247276        // Schedule backup if frequency is something else than never
    248277        if ( $options['frequency'] != 'never' ) {
    249 
    250             // If we have already scheduled a backup before, clear it first
    251             if ( wp_next_scheduled( 'backup_schedule' ) ) {
    252                 wp_clear_scheduled_hook('backup_schedule');
    253             }
    254 
    255             // Schedule backups
    256278            wp_schedule_event( current_time( 'timestamp' ), $options['frequency'], 'backup_schedule');
    257279        }   
     
    294316        $options['last_backup'] = $backup_time;
    295317        $options['local_files'][] = $file_path;
    296         if ( count( $options['local_files'] ) > $options['number_of_backups'] )
     318        if ( count( $options['local_files'] ) > $options['local_number'] )
    297319            if ( unlink( $f = array_shift( $options['local_files'] ) ) )
    298320                backup_log( 'NOTICE', 'Deleted old backup file ' . $f );
     
    300322                backup_log( 'WARNING', 'Could not delete file ' . $f );
    301323        if ( $options['token'] && $options['client_id'] && $options['client_secret'] ) {
    302             $access = access_token( $options['token'], $options['client_id'], $options['client_secret'] );
    303             backup_log( 'NOTICE', 'Attempting to upload archive to Google Drive' );
    304             $timer_start = microtime( true );
    305             if ( ! $id = upload_file( $file_path, $file_name, $options['folder'], $access ) )
    306                 backup_log( 'ERROR', 'Failed to upload archive to Google Drive!' );
     324            if ( $access = access_token( $options['token'], $options['client_id'], $options['client_secret'] ) ) {
     325                backup_log( 'NOTICE', 'Attempting to upload archive to Google Drive' );
     326                $timer_start = microtime( true );
     327                if ( ! $id = upload_file( $file_path, $file_name, $options['folder'], $access ) )
     328                    backup_log( 'ERROR', 'Failed to upload archive to Google Drive!' );
     329                else {
     330                    backup_log( 'NOTICE', 'Archive ' . $file_name . ' uploaded to Google Drive in ' . ( microtime( true ) - $timer_start ) . ' seconds' );
     331                    $options['drive_files'][] = $id;
     332                    if ( count( $options['drive_files'] ) > $options['drive_number'] )
     333                        if ( delete_file( $r = array_shift( $options['drive_files'] ), $access ) )
     334                            backup_log( 'NOTICE', 'Deleted Google Drive file ' . $r );
     335                        else   
     336                            backup_log( 'WARNING', 'Could not delete Google Drive file ' . $r );
     337                }
     338            }
    307339            else {
    308                 backup_log( 'NOTICE', 'Archive ' . $file_name . ' uploaded to Google Drive in ' . ( microtime( true ) - $timer_start ) . ' seconds' );
    309                 $options['drive_files'][] = $id;
    310                 if ( count( $options['drive_files'] ) > $options['number_of_backups'] )
    311                     if ( delete_file( $r = array_shift( $options['drive_files'] ), $access ) )
    312                         backup_log( 'NOTICE', 'Deleted Google Drive file ' . $r );
    313                     else   
    314                         backup_log( 'WARNING', 'Could not delete Google Drive file ' . $r );
    315             }
     340                backup_log( 'ERROR', 'Did not receive an access token from Google!', __FILE__, __LINE__ );
     341            }   
    316342        }
    317343        update_option( 'backup_options', $options );
     
    362388   
    363389    $result = @file_get_contents( $url, false, stream_context_create( $context ) );
    364     if ( strpos( array_shift( $http_response_header ), '200' ) ) {
    365         $response_header = array();
    366         foreach ( $http_response_header as $header_line ) {
    367             list( $key, $value ) = explode( ':', $header_line, 2 );
    368             $response_header[trim( $key )] = trim( $value );
    369         }
    370         if ( isset( $response_header['Location'] ) ) {
    371             $next_location = $response_header['Location'];
    372             $pointer = 0;
    373             $max_chunk_size = 524288;
    374             while ( $pointer < $size - 1 ) {
    375                 $chunk = file_get_contents( $file, false, NULL, $pointer, $max_chunk_size );
    376                 $next_location = upload_chunk( $next_location, $chunk, $pointer, $size, $token );
    377                 if( $next_location === false ) {
    378                     return false;
    379                 }   
    380                 // if object it means we have our simpleXMLElement response
    381                 if ( is_object( $next_location ) ) {
    382                     // return resource Id
    383                     return substr( $next_location->children( "http://schemas.google.com/g/2005" )->resourceId, 5 );
     390    if ( $result ) {
     391        if ( strpos( $response = array_shift( $http_response_header ), '200' ) ) {
     392            $response_header = array();
     393            foreach ( $http_response_header as $header_line ) {
     394                list( $key, $value ) = explode( ':', $header_line, 2 );
     395                $response_header[trim( $key )] = trim( $value );
     396            }
     397            if ( isset( $response_header['Location'] ) ) {
     398                $next_location = $response_header['Location'];
     399                $pointer = 0;
     400                $max_chunk_size = 524288;
     401                while ( $pointer < $size - 1 ) {
     402                    $chunk = file_get_contents( $file, false, NULL, $pointer, $max_chunk_size );
     403                    $next_location = upload_chunk( $next_location, $chunk, $pointer, $size, $token );
     404                    if( $next_location === false ) {
     405                        return false;
     406                    }   
     407                    // if object it means we have our simpleXMLElement response
     408                    if ( is_object( $next_location ) ) {
     409                        // return resource Id
     410                        return substr( $next_location->children( "http://schemas.google.com/g/2005" )->resourceId, 5 );
     411                    }
     412                    $pointer += strlen( $chunk );
     413                   
    384414                }
    385                 $pointer += strlen( $chunk );
    386                
    387             }
    388         }
     415            }
     416        }
     417        else {
     418            backup_log( 'ERROR', 'Bad response: ' . $response, __FILE__, __LINE__ );
     419            return false;
     420        }
     421    }
     422    else {
     423        backup_log( 'ERROR', 'Unable to request file from ' . $url, __FILE__, __LINE__ );
    389424    }   
    390     return false;
    391425}
    392426/**
     
    440474        }
    441475        else {
    442             backup_log( 'ERROR', 'Bad Response: ' . $response );
     476            backup_log( 'ERROR', 'Bad response: ' . $response, __FILE__, __LINE__ );
    443477            return false;
    444478        }
     
    768802
    769803/**
     804 * Recursively remove a directory
     805 *
     806 * @param string $dir The path to the directory to be removed
     807 */
     808function rrmdir( $dir ) {
     809    foreach ( glob( $dir . '/*' ) as $file ) {
     810        if ( is_dir( $file ) )
     811            rrmdir( $file );
     812        else
     813            unlink( $file );
     814    }
     815    rmdir( $dir );
     816}
     817
     818/**
    770819 * Custom logging function for the backup plugin
    771820 *
  • backup/trunk/readme.txt

    r546967 r547958  
    55Requires at least: 3.0
    66Tested up to: 3.3.2
    7 Stable tag: 1.1.1
     7Stable tag: 1.1.2
    88
    99Make backups of your Wordpress site to Google Drive.
     
    2727== Changelog ==
    2828
     29= 1.1.2 =
     30* Added the ability to store a different number of backups locally then on Google Drive
     31* On deactivation the plugin deletes all traces of itself (backups stored locally, options) and revokes access to the Google Account
     32* Fixed some more frequency issues
     33
    2934= 1.1.1 =
    3035* Fixed mothly backup frequency
Note: See TracChangeset for help on using the changeset viewer.