$max_files ){ asort( $files ); //Make sure they are sorted alphabetically (which is chronologically, due to the date stamp) //uberp( $files ); while( count( $files ) > $max_files ){ unlink( $files[0] ); //Delete the file from the server unset( $files[0] ); //This is critical, otherwise we loop infinitely } } } } /* * Creates a directory if it doesn't already exist */ function ubermenu_make_backup_dir( $dir ){ if( !file_exists( $dir ) ){ if( !wp_mkdir_p( $dir ) ){ return false; //Couldn't create directory } } return true; } /** * RESTORE ON PLUGIN ACTIVATION */ /* * When the plugin is activated, restore the custom assets * (Plugins are re-activated after update) */ register_activation_hook( UBERMENU_FILE , 'ubermenu_restore_backups' ); function ubermenu_restore_backups(){ ubermenu_restore_custom_assets(); } /* * Restores custom.css and custom.css - if they exist */ function ubermenu_restore_custom_assets(){ $custom_dir = trailingslashit( UBERMENU_DIR ).'custom/'; //UberMenu's /custom directory //Find the Backups directory $uploads = wp_upload_dir(); $uploads_dir = trailingslashit( $uploads['basedir'] ); $backups_dir = $uploads_dir . 'ubermenu_backups/'; //Restore CSS backup - if one exists and the custom.css does not exist in the plugin $custom_css = $custom_dir.'custom.css'; $backup_css = $backups_dir.'custom.css'; if( !file_exists( $custom_css ) && file_exists( $backup_css ) ){ $result = ubermenu_restore_file( $backup_css , $custom_css , $custom_dir ); if( is_wp_error( $result ) ){ add_action( 'admin_notices', 'ubermenu_restore_admin_notice_fail_css' ); } } //Restore JS backup - if one exists and the custom.js does not exist in the plugin $custom_js = $custom_dir.'custom.js'; $backup_js = $backups_dir.'custom.js'; if( !file_exists( $custom_js ) && file_exists( $backup_js ) ){ ubermenu_restore_file( $backup_js , $custom_js , $custom_dir ); } } /* * Copies the file from the source to the destination * $dest_file is a full path */ function ubermenu_restore_file( $source_file , $dest_file , $dest_dir ){ //echo 'restore '.$source_file .' to ' .$dest_file; //If the directory is writable if( is_writable( $dest_dir ) ){ copy( $source_file , $dest_file ); } else{ error_log( 'UberMenu: could not restore (not writable) ' . $source_file . ' to ' . $dest_file ); //Not really important, since the directory had to be writable in order to run the update //and delete the file in the first place return new WP_Error( 'ubermenu_restore_failed' , 'File not writable' ); } } function ubermenu_restore_admin_notice_fail_css(){ ubermenu_restore_admin_notice( __( 'Could not restore custom.css file, as the directory is not writable. You can manually restore the file from the wp-content/uploads/ubermenu_backups directory' , 'ubermenu' ) , 'error' ); } function ubermenu_restore_admin_notice( $notice , $type = 'udpated' ) { ?>