Skip to content

Commit

Permalink
create a core backup #44
Browse files Browse the repository at this point in the history
  • Loading branch information
markkap committed Jul 25, 2021
1 parent 6d18e3f commit 4bca6f9
Show file tree
Hide file tree
Showing 6 changed files with 688 additions and 46 deletions.
149 changes: 149 additions & 0 deletions src/wp-admin/backup-new.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Create Backup Administration Screen.
*
* @package calmPress
*/

/** WordPress Administration Bootstrap */
require_once __DIR__ . '/admin.php';

if ( ! current_user_can( 'backup' ) ) {
wp_die(
'<h1>' . __( 'You do not have permissions to create backup.' ) . '</h1>' .
403
);
}

if ( isset( $_POST['action'] ) && 'createbackup' === $_POST['action'] ) {
check_admin_referer( 'new-backup', '_wpnonce_new-backup' );

$description = wp_unslash( $_POST['description'] );

$storage = new \calmpress\backup\Local_Backup_Storage();
$storage->Backup( $description );
$redirect = add_query_arg( array( 'update' => 'new' ), 'backup-new.php' );
wp_redirect( $redirect );
die();
}

$title = __( 'Create Backup' );
$parent_file = 'backups.php';

$help = '<p>' . __( 'To create a new backup, fill in the form on this screen and click the Create button at the bottom.' ) . '</p>';

get_current_screen()->add_help_tab(
array(
'id' => 'overview',
'title' => __( 'Overview' ),
'content' => $help,
)
);

require_once ABSPATH . 'wp-admin/admin-header.php';

if ( isset( $_GET['update'] ) ) {
$messages = array();
if ( is_multisite() ) {
$edit_link = '';
if ( ( isset( $_GET['user_id'] ) ) ) {
$user_id_new = absint( $_GET['user_id'] );
if ( $user_id_new ) {
$edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_id_new ) ) );
}
}

switch ( $_GET['update'] ) {
case 'newuserconfirmation':
$messages[] = __( 'Invitation email sent to new user. A confirmation link must be clicked before their account is created.' );
break;
case 'add':
$messages[] = __( 'Invitation email sent to user. A confirmation link must be clicked for them to be added to your site.' );
break;
case 'addnoconfirmation':
$message = __( 'User has been added to your site.' );

if ( $edit_link ) {
$message .= sprintf( ' <a href="%s">%s</a>', $edit_link, __( 'Edit user' ) );
}

$messages[] = $message;
break;
case 'addexisting':
$messages[] = __( 'That user is already a member of this site.' );
break;
case 'could_not_add':
$add_user_errors = new WP_Error( 'could_not_add', __( 'That user could not be added to this site.' ) );
break;
case 'created_could_not_add':
$add_user_errors = new WP_Error( 'created_could_not_add', __( 'User has been created, but could not be added to this site.' ) );
break;
case 'does_not_exist':
$add_user_errors = new WP_Error( 'does_not_exist', __( 'The requested user does not exist.' ) );
break;
case 'enter_email':
$add_user_errors = new WP_Error( 'enter_email', __( 'Please enter a valid email address.' ) );
break;
}
} else {
if ( 'add' === $_GET['update'] ) {
$messages[] = __( 'User added.' );
}
}
}
?>
<div class="wrap">
<h1 id="create-backup">
<?php
esc_html_e( 'Create Backup' );
?>
</h1>

<?php if ( isset( $errors ) && is_wp_error( $errors ) ) : ?>
<div class="error">
<ul>
<?php
foreach ( $errors->get_error_messages() as $err ) {
echo "<li>$err</li>\n";
}
?>
</ul>
</div>
<?php
endif;

if ( ! empty( $messages ) ) {
foreach ( $messages as $msg ) {
echo '<div id="message" class="updated notice is-dismissible"><p>' . $msg . '</p></div>';
}
}
?>

<?php if ( isset( $add_user_errors ) && is_wp_error( $add_user_errors ) ) : ?>
<div class="error">
<?php
foreach ( $add_user_errors->get_error_messages() as $message ) {
echo "<p>$message</p>";
}
?>
</div>
<?php endif; ?>

<form method="post" name="newbackup" id="newbackup" class="validate" novalidate="novalidate">
<input name="action" type="hidden" value="createbackup" />
<?php wp_nonce_field( 'new-backup', '_wpnonce_new-backup' ); ?>

<table class="form-table" role="presentation">
<tr class="form-field">
<th scope="row"><label for="description"><?php esc_html_e( 'Description' ); ?></label></th>
<td>
<textarea name="description" id="description" rows="5" cols="50" class="large-text"></textarea>
<p class="description"><?php esc_html_e( 'A general description of the reason for the backup, that will be associated with the backup when displayed in the backups list' ); ?></p>
</td>
</tr>
</table>
<?php submit_button( __( 'Create' ), 'primary', 'newbackupsub', true, array( 'id' => 'newbackupsub' ) ); ?>
</form>
</div>
<?php
require_once ABSPATH . 'wp-admin/admin-footer.php';
6 changes: 3 additions & 3 deletions src/wp-admin/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@
$submenu['options-general.php'][45] = array( __( 'Privacy' ), 'manage_privacy_options', 'options-privacy.php' );

if ( ! is_multisite() ) {
$menu[85] = array( __( 'Backups' ), 'backup', 'backups.php', '', 'menu-top menu-icon-settings', 'menu-settings', 'dashicons-admin-settings' );
$submenu['backup.php'][10] = array( __( 'Backup' ), 'backup', 'backups.php' );
$submenu['new-backup.php'][10] = array( __( 'New' ), 'backup', 'backups.php' );
$menu[85] = array( __( 'Backups' ), 'backup', 'backups.php', '', 'menu-top menu-icon-settings', 'menu-settings', 'dashicons-database' );
$submenu['backups.php'][10] = array( __( 'Backups' ), 'backup', 'backups.php' );
$submenu['backups.php'][15] = array( __( 'Create New' ), 'backup', 'backup-new.php' );
}

$_wp_last_utility_menu = 85; // The index of the last top-level menu in the utility menu group.
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/calmpress/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'calmpress\backup\Backup' => __DIR__ . '/backup/class-backup.php',
'calmpress\backup\Backup_Storage' => __DIR__ . '/backup/class-backup-storage.php',
'calmpress\backup\Local_Backup_Storage' => __DIR__ . '/backup/class-local-backup-storage.php',
'calmpress\backup\Local_Backup' => __DIR__ . '/backup/class-local-backup.php',
];

spl_autoload_register(
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/calmpress/backup/class-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function time_created() : int;
public function description() : string;

/**
* The hstorage in which the backup is located.
* The storage in which the backup is located.
*
* @since 1.0.0
*
Expand Down
49 changes: 8 additions & 41 deletions src/wp-includes/calmpress/backup/class-local-backup-storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Local_Backup_Storage implements Backup_Storage {
*/
protected function backup_dir() : string {
$data = wp_upload_dir( null, false );
return $data['basedir'] . self::PATH_RELATIVE_TO_UPLOADS;
return $data['basedir'] . '/' . self::PATH_RELATIVE_TO_UPLOADS;
}

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ public function identifier() : string {
* @return Backup_Container A container which contains the backups.
*/
public function backups() : Backup_Container {
$container = new Backup_Container;
$container = new Backup_Container();

foreach ( glob( $this->backup_dir() . '*.meta' ) as $file ) {
try {
Expand All @@ -98,49 +98,16 @@ public function backups() : Backup_Container {
}

/**
* A utility function to recursively add file names to a zip.
* Do a local backup.
*
* @since 1.0.0
*
* @param ZipArchive $zip The zip file to add the files into, should be open.
* @param string $dirname The directory relative to calmPress root directory to zip.
*/
private static function zip_directory( ZipArchive $zip, string $dirname ) {

$root_path = ABSPATH . '/' . $dirname;
$zip->addEmptyDir( $dirname );

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( ABSPATH . '/' . $dirname ),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ( $files as $name => $file ) {
// Get file path relative to backup root.
$relative_path = substr( $file->getRealPath(), strlen( $root_path ) + 1);

if ( ! $file->isDir() ) {
$zip->addFile( $file->getRealPath(), $relative_path );
} else {
$zip->addEmptyDir( $relative_path );
}
}
}

/**
* Do a back up.
*
* @since 1.0.0
* @param string $description The description to be used for the backup.
*
* @return Local_Backup The backup object representing the backup.
* @throws \Exception if the backup fails.
*/
public static function backup() : Local_Backup {

// Use a temp directory to avoid possible collosions.
$tempdir = get_temp_dir();

$zipfile = ZipArchive::open( 'backup.zip', ZipArchive::CREATE );
self::zip_directory( $zipfile, 'wp-admin' );
self::zip_directory( $zipfile, 'wp-includes' );
public function Backup( string $description ) {
Local_Backup::create_backup( $description, $this->backup_dir() );
}

}
Loading

0 comments on commit 4bca6f9

Please sign in to comment.