Skip to content

Commit

Permalink
Changed "diagnostics" tab to "utilities" to add export/import.
Browse files Browse the repository at this point in the history
Added export/import of Kanban database table data to v2.
  • Loading branch information
gelform committed Jul 29, 2018
1 parent 190bc3b commit 986238d
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 1 deletion.
176 changes: 176 additions & 0 deletions src/Kanban/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ static function init() {
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'add_deactivate_thickbox' ) );

add_action( 'wp_ajax_kanban_diagnostic_info', array( __CLASS__, 'get_diagnostic_info' ) );

add_action('admin_init', array(__CLASS__, 'download_export_file'), 10);
add_action('admin_init', array(__CLASS__, 'upload_import_file'), 10);

}


Expand Down Expand Up @@ -260,6 +264,69 @@ static function contact_page() {
}


static function import_page() {

if ( !isset($_GET[Kanban_Utils::get_nonce()]) || !wp_verify_nonce($_GET[Kanban_Utils::get_nonce()], 'import-process') || !isset($_GET['filename']) || !isset($_GET['current']) || !isset($_GET['max']) ) {
wp_redirect(add_query_arg(
array(
'page' => 'kanban_settings',
'message' => urlencode(__('Something went wrong with your import. Please try again.', 'kanban'))
),
admin_url('admin.php')
));
exit;
}

$done = false;
$backup = false;
$percentage = round($_GET['current'] == 0 ? 1 : ($_GET['current']/$_GET['max'])*100);
$template = Kanban_Template::find_template( 'admin/import' );

if ( isset($_GET['backup']) ) {
$backup = true;

$redirect = remove_query_arg('backup');

include_once $template;
exit;
} else if ( $_GET['current'] == $_GET['max'] ) {
$done = true;

$redirect = add_query_arg(array(
'page' => 'kanban_settings'
), admin_url('admin.php'));
} else {

$redirect = add_query_arg(array(
'current' => $_GET['current']+1
));
}

$file = self::exports_upload_dir() . '/' . $_GET['filename'] . '-' . $_GET['current'];

if ( !is_file($file) ) {
wp_redirect(add_query_arg(
array(
'page' => 'kanban_settings',
'message' => urlencode(__('Something went wrong with your import. Please try again.', 'kanban'))
),
admin_url('admin.php')
));
exit;
}

include $file;

if ( $_GET['current'] == $_GET['max'] ) {
// Cleanup chunk files AFTER running the last one.
foreach (glob(self::exports_upload_dir() . '/' . $_GET['filename'] . "-*") as $chfile) {
unlink($chfile);
}
}

include_once $template;
}


static function render_kanbanpro_page() {
$template = Kanban_Template::find_template( 'admin/kanbanpro' );
Expand Down Expand Up @@ -489,7 +556,107 @@ static function contact_support() {
}
}

public static function exports_upload_dir()
{
$upload_dir = wp_upload_dir();
$user_dirname = $upload_dir['basedir'] . '/kanban-exports';
if (!file_exists($user_dirname)) {
wp_mkdir_p($user_dirname);
}

return $user_dirname;
}

public static function upload_import_file()
{
if ( !isset($_GET['page']) || !isset($_GET[Kanban_Utils::get_nonce()]) || $_GET['page'] != 'kanban_settings' || !wp_verify_nonce($_GET[Kanban_Utils::get_nonce()], 'import') ) return;

if ( !isset($_FILES) || !isset($_FILES['kanban_import']) || empty($_FILES['kanban_import']['name']) ) return;

$data = json_decode(file_get_contents($_FILES['kanban_import']['tmp_name']));

$chunk_size = isset($_GET['size']) && is_numeric($_GET['size']) ? $_GET['size'] : 20;

$chunks = array_chunk($data, $chunk_size);

$filename = $_FILES['kanban_import']['name'];

foreach ($chunks as $i => $chunk) {
file_put_contents(
self::exports_upload_dir() . '/' . $filename . '-' . ($i+1),
'<?php ' . "\n"
. 'global $wpdb;' . "\n"
. implode($chunk, "\n")
);
}

wp_redirect(add_query_arg(
array(
'page' => 'kanban_import',
Kanban_Utils::get_nonce() => wp_create_nonce('import-process'),
'current' => 1,
'max' => count($chunks),
'filename' => urlencode($filename),
'backup' => 1
)
));
exit;

}


public static function download_export_file()
{
if ( !isset($_GET['page']) || !isset($_GET[Kanban_Utils::get_nonce()]) || $_GET['page'] != 'kanban_settings' || !wp_verify_nonce($_GET[Kanban_Utils::get_nonce()], 'export') ) return;

global $wpdb;

$tables = $wpdb->get_col(
$wpdb->prepare (
'show tables like %s;',
$wpdb->prefix . 'kanban%'
)
);

$return = array();

//cycle through
foreach ($tables as $table) {
$return[] = sprintf('$wpdb->query("TRUNCATE TABLE {$wpdb->prefix}%s;");',
str_replace($wpdb->prefix, '', $table)
);
;

$result = $wpdb->get_results('SELECT * FROM ' . $table, ARRAY_A);

foreach ($result as $row) {

$return[] = sprintf(
'$wpdb->insert(%s, %s);',
'$wpdb->prefix . "' . str_replace($wpdb->prefix, '', $table) . '"',
var_export($row, true)
);
}
}

$prefix = isset($_GET['prefix']) ? $_GET['prefix'] : 'export';

$filename = sprintf(
'kb-%s-%s.kanbanwp',
$prefix,
Date('Y-m-d_H-i-s')
);

file_put_contents(
self::exports_upload_dir() . '/' . $filename,
json_encode($return)
);

header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . $filename . '"');

exit(json_encode($return));
}

static function ajax_register_user() {
if ( ! wp_verify_nonce( $_POST[ Kanban_Utils::get_nonce() ], 'kanban-new-user' ) ) {
Expand Down Expand Up @@ -641,6 +808,15 @@ static function admin_menu() {
array( __CLASS__, 'v3_page' )
);

add_submenu_page(
null,
__( 'Import' ),
__( 'Import' ),
'manage_options',
'kanban_import',
array( __CLASS__, 'import_page' )
);

} // admin_menu


Expand Down
42 changes: 42 additions & 0 deletions templates/admin/import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<div class="wrap">
<h1>
<?php echo __( sprintf( '%s Import', Kanban::get_instance()->settings->pretty_name ), 'kanban' ); ?>
</h1>

<p style="font-size: 1.382em; font-style: italic; font-weight: bold;">
<?php if ( $backup ) : ?>
<?php echo __( 'Backing up the current data and starting the download... ', 'kanban' ); ?>
<iframe src="<?php echo add_query_arg(array(
'page' => 'kanban_settings',
'prefix' => 'backup',
Kanban_Utils::get_nonce() => wp_create_nonce( 'export' )
), admin_url( 'admin.php' )) ?>" style="height: 1px; width: 1px; visibility: hidden;"></iframe>
<?php else: // $backup ?>
<?php echo sprintf(
__( '%s%% imported...', 'kanban' ),
$percentage
); ?>
<?php endif // $backup ?>
</p>

<div style="background: white; padding: 2px; width: 90%;">
<div style="background: limegreen; height: 40px; width: <?php echo $percentage ?>%;">
&nbsp;
</div>
</div>

<?php if ( $done ) : ?>
<p>
<a href="<?php echo $redirect ?>" class="button">
<?php echo __( 'Continue to the settings page', 'kanban' ); ?>
</a>
</p>
<?php else : // $done ?>
<script>
setTimeout(function() {
document.location.replace('<?php echo $redirect ?>');
}, 1000);
</script>
<?php endif ?>

</div><!-- wrap -->
73 changes: 72 additions & 1 deletion templates/admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class="nav-tab nav-tab-active"><?php echo __( 'General', 'kanban' ); ?></a>
<?php
echo apply_filters( 'kanban_settings_tabs', '' );
?>
<a href="#tab-help" id="tab-help-tab" class="nav-tab"><?php echo __( 'Diagnostics', 'kanban' ); ?></a>
<a href="#tab-help" id="tab-help-tab" class="nav-tab"><?php echo __( 'Utilities', 'kanban' ); ?></a>
</h2>


Expand Down Expand Up @@ -505,6 +505,77 @@ class="regular-text">


<div class="tab" id="tab-help" style="display: none;">

<h3><?php echo __( 'Import', 'kanban' ) ?></h3>

<div id="kanban-import" style="display:none;">

<div>
<p style="font-weight: bold;">
<?php echo __( '<b style="color: red;">Caution!</b> This will overwrite all data in the Kanban database tables!', 'kanban' ) ?>
</p>

<ol>
<li>
<?php echo __( 'Upload a Kanban data file (file type .kanbanwp) you exported from a Kanban install.', 'kanban' ) ?>
</li>
<li>
<?php echo __( 'Before importing begins, your current data will be downloaded to your computer
(The file will also be saved to the "kanban-exports" folder in your "uploads" folder).
You can use this file to restore your data at a later date.', 'kanban' ) ?>
</li>
<li>
<?php echo __( 'Your current Kanban data will be deleted and replaced.
Please do not stop the importer or your import may be interrupted and some data may not be imported.', 'kanban' ) ?>
</li>
</ol>

<hr>

<p>
<label>
<?php echo __( 'Select your Kanban data file (file type .kanbanwp):', 'kanban' ) ?><br>
<input type="file" name="kanban_import">
</label>
</p>
<p>
<button type="submit" class="button">
<?php echo __( 'Begin your import', 'kanban' ) ?>
</button>
</p>
</div>
</div>

<script>
function kanban_import () {
tb_show("<?php echo __( 'Import Kanban data', 'kanban' ) ?><br>", "#TB_inline?width=600&height=400&inlineId=kanban-import", "");
jQuery('#TB_ajaxContent').wrapInner('<form action="<?php echo add_query_arg(array(Kanban_Utils::get_nonce() => wp_create_nonce( 'import' ))) ?>" method="post" enctype="multipart/form-data"></form>');
};
</script>

<a href="#" onclick="kanban_import(); return false;" class="button">
<?php echo __( 'Import Kanban data', 'kanban' ) ?>
</a>

<h3><?php echo __( 'Export', 'kanban' ) ?></h3>

<p>
<?php echo __( 'A Kanban data file (file type .kanbanwp) will download to your computer.
The file will also be saved to the "kanban-exports" folder in your "uploads" folder.
Uploaded files (images, documents, etc) are <b>not</b> copied.
Please copy them manually.', 'kanban' ) ?>
</p>

<p>
<a href="<?php echo add_query_arg(array(Kanban_Utils::get_nonce() => wp_create_nonce( 'export' ))) ?>" class="button">
<?php echo __( 'Export Kanban data', 'kanban' ) ?>
</a>
</p>

<hr>

<h3><?php echo __( 'Diagnostics', 'kanban' ) ?></h3>

<p>
<button type="button" class="button" id="button-load-diagnostic-info">
<?php echo __( 'Send diagnostic info to support', 'kanban' ) ?>
Expand Down

0 comments on commit 986238d

Please sign in to comment.