Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion admin/admin-menu-and-tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public static function get_post_type_label( string $post_type ): string {
*/
public static function get_settings(): array {
$defaults = [
'enabled' => false,
'enabled' => false,
'include_activity_log' => false,
// Legacy option; UI no longer exposes a single "mode". Capabilities API reports both channels.
'mode' => 'api',
'allowed_items' => [
Expand Down
24 changes: 24 additions & 0 deletions admin/class-dt-migration-import-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ public function handle_import_batch() : void {
$batch_result['errors'] = array_merge( $batch_result['errors'] ?? [], $pum_result['errors'] );
}

if ( ! empty( $settings['include_activity_log'] ) ) {
$activity_rows = $rbody['activity_log'] ?? [];
$act_result = Disciple_Tools_Migration_Import_Engine::import_activity_log_for_posts(
is_array( $activity_rows ) ? $activity_rows : [],
$batch_post_ids,
$post_type
);
if ( ! empty( $act_result['errors'] ) ) {
$batch_result['errors'] = array_merge( $batch_result['errors'] ?? [], $act_result['errors'] );
}
}

wp_send_json_success( [
'done' => ! $has_more,
'phase' => 'records',
Expand Down Expand Up @@ -486,6 +498,18 @@ private function handle_file_mode_batch( string $step, array $settings ) : void
$batch_result['errors'] = array_merge( $batch_result['errors'] ?? [], $pum_result['errors'] );
}

if ( ! empty( $settings['include_activity_log'] ) ) {
$activity_rows = $payload['activity_log'][ $post_type ] ?? [];
$act_result = Disciple_Tools_Migration_Import_Engine::import_activity_log_for_posts(
is_array( $activity_rows ) ? $activity_rows : [],
$slice_post_ids,
$post_type
);
if ( ! empty( $act_result['errors'] ) ) {
$batch_result['errors'] = array_merge( $batch_result['errors'] ?? [], $act_result['errors'] );
}
}

wp_send_json_success( [
'done' => ! $has_more,
'phase' => 'records',
Expand Down
115 changes: 113 additions & 2 deletions admin/class-dt-migration-tab-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class Disciple_Tools_Migration_Tab_Import {
*/
private $import_preview_user_count = 0;

/**
* Whether the previewed export includes activity log rows (source package / Server A).
*
* @var bool
*/
private $import_preview_export_include_activity_log = false;

/**
* Which import UI produced the current preview: 'api' or 'file'.
*
Expand Down Expand Up @@ -322,6 +329,7 @@ class="dt-migration-setting-checkbox"
<td><?php echo esc_html( $row['notes'] ); ?></td>
</tr>
<?php endforeach; ?>
<?php $this->render_import_preview_activity_settings_row(); ?>
</tbody>
</table>

Expand All @@ -336,6 +344,7 @@ class="dt-migration-setting-checkbox"
<th><?php esc_html_e( 'Tiles', 'disciple-tools-migration' ); ?></th>
<th><?php esc_html_e( 'Fields', 'disciple-tools-migration' ); ?></th>
<th><?php esc_html_e( 'Records', 'disciple-tools-migration' ); ?></th>
<th><?php esc_html_e( 'Activity log', 'disciple-tools-migration' ); ?></th>
</tr>
</thead>
<tbody>
Expand All @@ -360,12 +369,27 @@ class="dt-migration-record-checkbox"
<td><?php echo isset( $summary['tiles'] ) ? intval( $summary['tiles'] ) : 0; ?></td>
<td><?php echo isset( $summary['fields'] ) ? intval( $summary['fields'] ) : 0; ?></td>
<td><?php echo esc_html( (string) (int) $record_count ); ?></td>
<td>
<?php
if ( $this->import_preview_export_include_activity_log ) {
$ac = isset( $record_data['activity_log_count'] ) ? (int) $record_data['activity_log_count'] : 0;
echo esc_html( number_format_i18n( $ac ) );
} else {
echo '<span aria-hidden="true">&mdash;</span>';
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php if ( $this->import_preview_export_include_activity_log && empty( $settings['include_activity_log'] ) ) : ?>
<p class="description" style="margin-top: 8px;">
<?php esc_html_e( 'Activity log is disabled on this site (Migration → Settings), so historical entries will not be restored during import until you enable that option.', 'disciple-tools-migration' ); ?>
</p>
<?php endif; ?>

<p style="margin-top: 16px;">
<button type="button" class="button dt-migration-run-preflight" data-import-channel="api">
Expand Down Expand Up @@ -458,6 +482,7 @@ class="dt-migration-record-checkbox"
<td><?php echo esc_html( $row['notes'] ); ?></td>
</tr>
<?php endforeach; ?>
<?php $this->render_import_preview_activity_settings_row(); ?>
</tbody>
</table>
<table class="widefat striped dt-migration-records-table">
Expand All @@ -468,6 +493,7 @@ class="dt-migration-record-checkbox"
<th><?php esc_html_e( 'Tiles', 'disciple-tools-migration' ); ?></th>
<th><?php esc_html_e( 'Fields', 'disciple-tools-migration' ); ?></th>
<th><?php esc_html_e( 'Records', 'disciple-tools-migration' ); ?></th>
<th><?php esc_html_e( 'Activity log', 'disciple-tools-migration' ); ?></th>
</tr>
</thead>
<tbody>
Expand All @@ -486,10 +512,26 @@ class="dt-migration-record-checkbox"
<td><?php echo isset( $summary['tiles'] ) ? intval( $summary['tiles'] ) : 0; ?></td>
<td><?php echo isset( $summary['fields'] ) ? intval( $summary['fields'] ) : 0; ?></td>
<td><?php echo esc_html( (string) (int) $record_count ); ?></td>
<td>
<?php
if ( $this->import_preview_export_include_activity_log ) {
$ac = isset( $record_data['activity_log_count'] ) ? (int) $record_data['activity_log_count'] : 0;
echo esc_html( number_format_i18n( $ac ) );
} else {
echo '<span aria-hidden="true">&mdash;</span>';
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php if ( $this->import_preview_export_include_activity_log && empty( $settings['include_activity_log'] ) ) : ?>
<p class="description" style="margin-top: 8px;">
<?php esc_html_e( 'Activity log is disabled on this site (Migration → Settings), so historical entries will not be restored during import until you enable that option.', 'disciple-tools-migration' ); ?>
</p>
<?php endif; ?>

<p style="margin-top: 16px;">
<button type="button" class="button dt-migration-run-preflight" data-import-channel="file">
<?php esc_html_e( 'Run preflight', 'disciple-tools-migration' ); ?>
Expand Down Expand Up @@ -737,6 +779,7 @@ private function process_form_fields( array $settings ) : void {

// Store allowed_items from export response for use in preview tables.
$this->export_allowed_items = $export_body['settings']['allowed_items'] ?? [];
$this->import_preview_export_include_activity_log = ! empty( $export_body['settings']['include_activity_log'] );

$preview = [];
foreach ( $post_types as $post_type => $config ) {
Expand Down Expand Up @@ -850,6 +893,7 @@ private function process_form_fields( array $settings ) : void {
// Always clear previous preview when re-submitting the form.
$this->settings_preview = null;
$this->import_preview_user_count = 0;
$this->import_preview_export_include_activity_log = false;
$this->import_preview_channel = null;
$this->records_preview = null;
$this->export_allowed_items = null;
Expand Down Expand Up @@ -987,6 +1031,7 @@ private function build_file_preview_state_from_payload( array $payload ) : void
$tiles_all = $dt_settings['dt_tiles_settings']['values'] ?? [];
$fields_all = $dt_settings['dt_fields_settings']['values'] ?? [];
$this->export_allowed_items = $payload['settings']['allowed_items'] ?? [];
$this->import_preview_export_include_activity_log = ! empty( $payload['settings']['include_activity_log'] );
$sys_users = $payload['export']['system_users']['users'] ?? [];
$this->import_preview_user_count = is_array( $sys_users ) ? count( $sys_users ) : 0;

Expand All @@ -999,10 +1044,21 @@ private function build_file_preview_state_from_payload( array $payload ) : void
}
$this->settings_preview = $preview;

$records_raw = $payload['records'] ?? [];
$records_raw = $payload['records'] ?? [];
$activity_raw = $payload['activity_log'] ?? [];
$this->records_preview = [];
foreach ( $records_raw as $post_type => $recs ) {
$this->records_preview[ $post_type ] = [ 'count' => is_array( $recs ) ? count( $recs ) : 0 ];
$activity_n = 0;
if ( $this->import_preview_export_include_activity_log && isset( $activity_raw[ $post_type ] ) && is_array( $activity_raw[ $post_type ] ) ) {
$activity_n = count( $activity_raw[ $post_type ] );
}
$row = [
'count' => is_array( $recs ) ? count( $recs ) : 0,
];
if ( $this->import_preview_export_include_activity_log ) {
$row['activity_log_count'] = $activity_n;
}
$this->records_preview[ $post_type ] = $row;
}

$this->import_preview_channel = 'file';
Expand Down Expand Up @@ -1114,4 +1170,59 @@ private function render_past_file_jobs_table( array $settings ) : void {
</table>
<?php
}

/**
* Sums activity_log_count values from a records preview payload.
*
* @param array<string, array<string, mixed>> $records_preview
*/
private function sum_preview_activity_log_counts( array $records_preview ) : int {
$total = 0;
foreach ( $records_preview as $row ) {
if ( is_array( $row ) && isset( $row['activity_log_count'] ) ) {
$total += (int) $row['activity_log_count'];
}
}
return $total;
}

/**
* Extra settings-table row describing activity log presence in the previewed export.
*/
private function render_import_preview_activity_settings_row() : void {
$records_preview = is_array( $this->records_preview ) ? $this->records_preview : [];
$activity_total = $this->sum_preview_activity_log_counts( $records_preview );
?>
<tr class="dt-migration-activity-log-preview-row">
<td><span aria-hidden="true">&mdash;</span></td>
<td><?php esc_html_e( 'Activity log (with records)', 'disciple-tools-migration' ); ?></td>
<td>
<?php
echo $this->import_preview_export_include_activity_log
? esc_html__( 'In export', 'disciple-tools-migration' )
: esc_html__( 'Not included', 'disciple-tools-migration' );
?>
</td>
<td>
<?php
if ( $this->import_preview_export_include_activity_log ) {
if ( $activity_total > 0 ) {
echo esc_html(
sprintf(
/* translators: %s: formatted number of activity log entries */
__( '%s activity entries in this preview (counts per post type are below).', 'disciple-tools-migration' ),
number_format_i18n( $activity_total )
)
);
} else {
esc_html_e( 'The source included activity in exports, but there are no rows for the post types shown below.', 'disciple-tools-migration' );
}
} else {
esc_html_e( 'The source did not include activity history in this package.', 'disciple-tools-migration' );
}
?>
</td>
</tr>
<?php
}
}
16 changes: 16 additions & 0 deletions admin/class-dt-migration-tab-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public function main_column( array $settings ) {
</fieldset>
</td>
</tr>
<tr>
<td>
<?php esc_html_e( 'Activity log', 'disciple-tools-migration' ); ?>
</td>
<td>
<label>
<input type="checkbox" name="dt_migration_include_activity_log" value="1" <?php checked( ! empty( $settings['include_activity_log'] ) ); ?> />
<?php esc_html_e( 'Include activity history when exporting records, and restore it when importing (per batch).', 'disciple-tools-migration' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'When importing, existing activity rows for the affected records are removed and replaced with the export. Leave off for smaller exports or if you do not need historical activity on the target site.', 'disciple-tools-migration' ); ?>
</p>
</td>
</tr>
<tr>
<th colspan="2" scope="colgroup">
<?php esc_html_e( 'File import jobs', 'disciple-tools-migration' ); ?>
Expand Down Expand Up @@ -198,6 +212,8 @@ public function process_form_fields(): void {

$settings['enabled'] = isset( $post_vars['dt_migration_enabled'] ) && '1' === (string) $post_vars['dt_migration_enabled'];

$settings['include_activity_log'] = isset( $post_vars['dt_migration_include_activity_log'] ) && '1' === (string) $post_vars['dt_migration_include_activity_log'];

$allowed = $post_vars['dt_migration_allowed_items'] ?? [];

$settings['allowed_items']['general_settings'] = ! empty( $allowed['general_settings'] );
Expand Down
Loading
Loading