Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Merge 35184c8 into 68e9990
Browse files Browse the repository at this point in the history
  • Loading branch information
PatelUtkarsh committed Dec 12, 2017
2 parents 68e9990 + 35184c8 commit 8694120
Show file tree
Hide file tree
Showing 4 changed files with 507 additions and 43 deletions.
10 changes: 7 additions & 3 deletions css/customize-snapshots-admin.css
@@ -1,3 +1,7 @@
details .snapshot-setting-actions{
display: inline-block;
float: right;
}
details.snapshot-setting-removed summary {
text-decoration: line-through;
}
Expand All @@ -7,8 +11,8 @@ details:not(.snapshot-setting-removed) .snapshot-toggle-setting-removal {
details:not(.snapshot-setting-removed) .snapshot-toggle-setting-removal:hover {
color: #f00
}
details .snapshot-toggle-setting-removal {
float: right;
.snapshot-merged-list a{
vertical-align: bottom;
}

a.snapshot-thickbox {
Expand Down Expand Up @@ -44,7 +48,7 @@ a.snapshot-thickbox {
visibility: hidden;
}

.snapshot-conflict-details article {
.snapshot-conflict-details article, .snapshot-conflict-details div {
padding: 0 10px;
background: #ffffff;
border: 1px solid #e5e5e5;
Expand Down
15 changes: 14 additions & 1 deletion js/customize-snapshots-admin.js
Expand Up @@ -18,6 +18,7 @@ var CustomizeSnapshotsAdmin = (function( $ ) {
component.data = _.extend( component.data, args );
$( function() {
component.deleteSetting();
component.conflictSetting();
} );
};

Expand Down Expand Up @@ -110,5 +111,17 @@ var CustomizeSnapshotsAdmin = (function( $ ) {
} );
};

/**
* Change setting value on change of conflict setting.
*
* @return {void}
*/
component.conflictSetting = function() {
$( 'input.snapshot-resolved-settings' ).change( function() {
var $this = $( this ),
selector = '#' + $this.data( 'settingValueSelector' );
$( selector ).html( $this.parents( 'details' ).find( '.snapshot-conflict-setting-data' ).html() );
} );
};
return component;
})( jQuery );
} )( jQuery );
210 changes: 204 additions & 6 deletions php/class-post-type.php
Expand Up @@ -85,6 +85,7 @@ public function hooks() {
add_filter( 'post_row_actions', array( $this, 'filter_post_row_actions' ), 10, 2 );
add_filter( 'user_has_cap', array( $this, 'filter_user_has_cap' ), 10, 2 );
add_action( 'post_submitbox_minor_actions', array( $this, 'hide_disabled_publishing_actions' ) );
add_filter( 'content_save_pre', array( $this, 'filter_selected_conflict_setting' ), 11 ); // 11 because remove customize setting is set on 10.
add_filter( 'content_save_pre', array( $this, 'filter_out_settings_if_removed_in_metabox' ), 10 );
add_action( 'admin_print_scripts-revision.php', array( $this, 'disable_revision_ui_for_published_posts' ) );
add_action( 'admin_notices', array( $this, 'admin_show_merge_error' ) );
Expand Down Expand Up @@ -384,6 +385,34 @@ public function render_data_metabox( $post ) {
echo sprintf( '%1$s %2$s %3$s', esc_html__( 'Modified:', 'customize-snapshots' ), esc_html( get_the_modified_date( '' ) ), esc_html( get_the_modified_time( '' ) ) ) . '<br>';
echo '</p>';

$merged_uuid = $this->get_snapshot_merged_uuid( $snapshot_content );
if ( ! empty( $merged_uuid ) ) {
echo '<p>';
echo '<strong>';
esc_html_e( 'These changes are merged from:', 'customize-snapshots' );
echo '</strong>';
echo '<ul class="ul-disc snapshot-merged-list">';
foreach ( $merged_uuid as $uuid ) {
$uuid_post_id = $this->find_post( $uuid );
$uuid_post = get_post( $uuid_post_id );
echo '<li>';
if ( $uuid_post->post_name === $uuid_post->post_title ) {
echo get_the_title( $uuid_post );
} else {
$title = get_the_title( $uuid_post );
if ( empty( $title ) ) {
echo esc_html( $uuid_post->post_name );
} else {
echo get_the_title( $uuid_post ) . ' - ' . esc_html( $uuid_post->post_name );
}
}
echo ( 'future' === $uuid_post->post_status ) ? ' (Scheduled)' : '';
echo '<a href="' . esc_url( get_edit_post_link( $uuid_post_id ) ) . '" class="dashicons dashicons-external"></a></li>';
}
echo '</ul>';
echo '</p>';
}

$snapshot_theme = get_post_meta( $post->ID, '_snapshot_theme', true );
if ( ! empty( $snapshot_theme ) && get_stylesheet() !== $snapshot_theme ) {
echo '<p>';
Expand Down Expand Up @@ -426,10 +455,13 @@ public function render_data_metabox( $post ) {
}
$value = isset( $setting_params['value'] ) ? $setting_params['value'] : '';
echo '<li>';
echo '<details open>';
echo '<details open class="snapshot-setting-value">';
echo '<summary><code>' . esc_html( $setting_id ) . '</code> ';
if ( 'publish' !== get_post_status( $post ) ) {
echo '<span class="snapshot-setting-actions">';
$this->resolve_conflict_markup( $setting_id, $setting_params, $snapshot_content, $post );
echo '<a href="#" id="' . esc_attr( $setting_id ) . '" data-text-restore="' . esc_attr__( 'Restore setting', 'customize-snapshots' ) . '" class="snapshot-toggle-setting-removal remove">' . esc_html__( 'Remove setting', 'customize-snapshots' ) . '</a>';
echo '</span>';
}

// Show error message when there was a publishing error.
Expand Down Expand Up @@ -457,6 +489,7 @@ public function render_data_metabox( $post ) {

/* translators: %s: Setting id which has potential conflict. */
$title_text = sprintf( __( '%s has potential conflicts (click to expand)', 'customize-snapshots' ), $setting_id );

echo '<a href="#TB_inline?width=600&height=550&inlineId=snapshot-' . esc_attr( $setting_id_key ) . '" class="dashicons dashicons-warning thickbox snapshot-thickbox" title="' . esc_attr( $title_text ) . '"></a>'; ?>
<div id="snapshot-<?php echo esc_attr( $setting_id ); ?>" style="display:none;">
<?php foreach ( $conflicts_settings[ $setting_id ] as $data ) { ?>
Expand All @@ -483,7 +516,9 @@ public function render_data_metabox( $post ) {
echo '</summary>';

$preview = $this->get_printable_setting_value( $value, $setting_id, $setting_params, $post );
echo '<div id="snapshot-setting-preview-' . esc_attr( $setting_id ) . '">';
echo $preview; // WPCS: xss ok.
echo '</div>';
echo '</details>';
echo '</li>';
} // End foreach().
Expand Down Expand Up @@ -877,12 +912,11 @@ public function handle_snapshot_merge( $redirect_to, $do_action, $post_ids ) {
/**
* Merge two or more snapshots
*
* @param array $post_ids post id array.
* @param array $posts post array.
*
* @return int Changeset post id.
*/
public function merge_snapshots( $post_ids ) {
$posts = array_map( 'get_post', $post_ids );
public function merge_snapshots( $posts ) {
usort( $posts, function( $a, $b ) {
$compare_a = $a->post_modified;
$compare_b = $b->post_modified;
Expand Down Expand Up @@ -919,18 +953,52 @@ public function merge_snapshots( $post_ids ) {
}
$conflict_keys = array_flip( $conflict_keys );
$merged_snapshot_data = call_user_func_array( 'array_merge', $snapshots_data );
$merged_keys = array_keys( array_diff_key( $merged_snapshot_data, $conflict_keys ) );

foreach ( $merged_keys as $key ) {
// Store contributed snapshot id in setting.
$uuid = array();
foreach ( $snapshot_post_data as $post_data ) {
if ( isset( $post_data['data'][ $key ]['merged_uuid'] ) && is_array( $post_data['data'][ $key ]['merged_uuid'] ) ) {
$uuid = array_merge( $uuid, $post_data['data'][ $key ]['merged_uuid'] );
} elseif ( isset( $post_data['data'][ $key ] ) ) {
$uuid[] = $post_data['uuid'];
}
}
$merged_snapshot_data[ $key ]['merged_uuid'] = array_values( array_unique( $uuid ) );
}

foreach ( $conflict_keys as $key => $i ) {
$original_values = array();
$uuid = array();
foreach ( $snapshot_post_data as $post_data ) {
if ( isset( $post_data['data'][ $key ] ) ) {
if ( isset( $post_data['data'][ $key ]['merge_conflict'] ) && is_array( $post_data['data'][ $key ]['merge_conflict'] ) ) {
$original_values = array_merge( $original_values, $post_data['data'][ $key ]['merge_conflict'] );
} elseif ( isset( $post_data['data'][ $key ] ) ) {
$original_values[] = array(
'uuid' => $post_data['uuid'],
'value' => $post_data['data'][ $key ]['value'],
);
}
if ( isset( $post_data['data'][ $key ]['merged_uuid'] ) ) {
$uuid = array_merge( $uuid, $post_data['data'][ $key ]['merged_uuid'] );
} else {
$uuid[] = $post_data['uuid'];
}
}
$values = wp_list_pluck( $original_values, 'value' );
$values = array_unique( $values, SORT_REGULAR );
if ( 1 === count( $values ) ) {
// If all values are same there is no conflict so store normally as contributed snapshot ids.
$merged_snapshot_data[ $key ]['merged_uuid'] = ! empty( $uuid ) ? $uuid : wp_list_pluck( $original_values, 'uuid' );
$merged_snapshot_data[ $key ]['merged_uuid'] = array_values( array_unique( $merged_snapshot_data[ $key ]['merged_uuid'] ) ); // Array_values to reset keys.
unset( $merged_snapshot_data[ $key ]['merge_conflict'], $merged_snapshot_data[ $key ]['selected_uuid'] );
} else {
// If we have more than one unique value means it is conflicting.
$merged_snapshot_data[ $key ]['merge_conflict'] = $original_values;
$last_value = end( $original_values );
$merged_snapshot_data[ $key ]['selected_uuid'] = $last_value['uuid'];
}
$merged_snapshot_data[ $key ]['merge_conflict'] = $original_values;
}
$post_id = $this->save( array(
'uuid' => wp_generate_uuid4(),
Expand Down Expand Up @@ -1198,4 +1266,134 @@ public function get_conflicted_settings( $post, $settings = array() ) {
}
return $conflicted_settings;
}

/**
* Generate resolve conflict markup.
* This will add thickbox with radio button to select between conflicted setting values.
*
* @param string $setting_id setting id.
* @param array $value setting value.
* @param array $snapshot_content snapshot post-content.
* @param \WP_Post $post Post object.
*/
public function resolve_conflict_markup( $setting_id, $value, $snapshot_content, $post ) {
if ( isset( $value['merge_conflict'] ) ) {
$setting_id_key = str_replace( ']', '\\]', str_replace( '[', '\\[', $setting_id ) );
switch ( $post->post_status ) {
case 'draft':
$save_as_string = __( 'Save draft', 'default' );
break;
case 'pending':
$save_as_string = __( 'Save as Pending', 'default' );
break;
default:
$save_as_string = __( 'Save', 'default' );
break;
}
/* translators: %s: save as draft or pending */
$title_text = sprintf( __( 'Pick to change selection and press \'%s\' button', 'customize-snapshots' ), $save_as_string );
echo '<a href="#TB_inline?width=600&height=550&inlineId=snapshot-resolve-' . esc_attr( $setting_id_key ) . '" id="' . esc_attr( $setting_id ) . '" class="snapshot-resolve-setting-conflict remove thickbox" title="' . esc_attr( $title_text ) . '">' . esc_html__( 'Change merge selection', 'customize-snapshots' ) . '</a> ';
echo '<div id="snapshot-resolve-' . esc_attr( $setting_id ) . '" style="display:none;">';
echo '<ul>';
foreach ( $value['merge_conflict'] as $conflicted_data ) {
echo '<li>';
echo '<details open class="snapshot-conflict-details">';
echo '<summary>';
$input = '<input type="radio" class="snapshot-resolved-settings" data-setting-value-selector="snapshot-setting-preview-' . $setting_id_key . '"';
$json = wp_json_encode( array(
'setting_id' => $setting_id,
'uuid' => $conflicted_data['uuid'],
) );
$input .= sprintf( 'name="%s_resolve_conflict_uuid[%s]" value=%s', static::SLUG, array_search( $setting_id, array_keys( $snapshot_content ), true ), $json );
$input .= checked( $value['selected_uuid'], $conflicted_data['uuid'], false ) . '>';
echo $input; // WPCS: xss ok.
echo '<code> ' . esc_html( $conflicted_data['uuid'] ) . ' </code></summary>';

$preview = $this->get_printable_setting_value( $conflicted_data['value'] );

echo '<div class="snapshot-conflict-setting-data">';
echo $preview; // WPCS: xss ok.
echo '</div>';
echo '</details></input></li>';
}
echo '</ul>';
echo '</div>';
} // End if().
}

/**
* Change conflicted value in post_content, if they were changed in the meta box.
*
* @param String $content Post content to filter.
* @return String $content Post content, possibly filtered.
*/
public function filter_selected_conflict_setting( $content ) {
global $post;
$key_for_selected_resolved_setting = static::SLUG . '_resolve_conflict_uuid';
$post_type_object = get_post_type_object( static::SLUG );

$should_filter_for_resolve_conflict = (
isset( $post->post_status )
&&
( 'publish' !== $post->post_status )
&&
current_user_can( $post_type_object->cap->edit_post, $post->ID )
&&
( static::SLUG === $post->post_type )
&&
! empty( $_REQUEST[ $key_for_selected_resolved_setting ] )
&&
is_array( $_REQUEST[ $key_for_selected_resolved_setting ] )
&&
isset( $_REQUEST[ static::SLUG ] )
&&
wp_verify_nonce( $_REQUEST[ static::SLUG ], static::SLUG . '_settings' )
&&
! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
);

if ( ! $should_filter_for_resolve_conflict ) {
return $content;
}
$setting_ids_to_unset = wp_unslash( $_REQUEST[ $key_for_selected_resolved_setting ] );
$snapshot_data = json_decode( wp_unslash( $content ), true );
foreach ( $setting_ids_to_unset as $i => $data ) {
$data = json_decode( $data, true );
if ( isset( $data['setting_id'], $data['uuid'], $snapshot_data[ $data['setting_id'] ], $snapshot_data[ $data['setting_id'] ]['merge_conflict'] ) ) {
$setting_key = $data['setting_id'];
if ( isset( $snapshot_data[ $setting_key ]['selected_uuid'] ) && $snapshot_data[ $setting_key ]['selected_uuid'] === $data['uuid'] ) {
// No change.
continue;
}
$snapshot_data[ $setting_key ]['selected_uuid'] = $data['uuid'];
foreach ( $snapshot_data[ $setting_key ]['merge_conflict'] as $conflict_data ) {
if ( isset( $conflict_data['uuid'], $conflict_data['value'] ) && $conflict_data['uuid'] === $data['uuid'] ) {
$snapshot_data[ $setting_key ]['value'] = $conflict_data['value'];
break;
}
}
}
}
$content = Customize_Snapshot_Manager::encode_json( $snapshot_data );
return $content;
}

/**
* Get snapshot merged uuid from post_content
*
* @param array $post_content snapshot data.
* @return array merged snapshot.
*/
public function get_snapshot_merged_uuid( $post_content ) {
$snapshot_merged_uuid = array();
foreach ( $post_content as $key => $value ) {
if ( isset( $value['merged_uuid'] ) ) {
$snapshot_merged_uuid = array_merge( $snapshot_merged_uuid, $value['merged_uuid'] );
} elseif ( isset( $value['merge_conflict'] ) ) {
$temp_uuid = wp_list_pluck( $value['merge_conflict'], 'uuid' );
$snapshot_merged_uuid = array_merge( $snapshot_merged_uuid, $temp_uuid );
}
}
return array_unique( $snapshot_merged_uuid );
}
}

0 comments on commit 8694120

Please sign in to comment.