-
Notifications
You must be signed in to change notification settings - Fork 11
Remember preview url query params #129
Changes from 8 commits
31096b4
2b29f14
a9fd390
6a44a47
95e139b
108c865
d5542c1
1774137
2c4b599
34246cc
ab61743
65c042b
e78ae0a
d3438ad
3534389
beaf92a
f12a74a
4caa963
1aec717
f5b55cd
6d9e9a9
43bee5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,7 +104,8 @@ function hooks() { | |
add_action( 'admin_bar_menu', array( $this, 'remove_all_non_snapshot_admin_bar_links' ), 100000 ); | ||
add_action( 'wp_before_admin_bar_render', array( $this, 'print_admin_bar_styles' ) ); | ||
add_filter( 'removable_query_args', array( $this, 'filter_removable_query_args' ) ); | ||
add_action( 'save_post_customize_changeset', array( $this, 'create_initial_changeset_revision' ) ); | ||
add_action( 'save_post_' . $this->get_post_type(), array( $this, 'create_initial_changeset_revision' ) ); | ||
add_action( 'save_post_' . $this->get_post_type(), array( $this, 'save_customize_preview_url_query_vars' ) ); | ||
add_filter( 'wp_insert_post_data', array( $this, 'prepare_snapshot_post_content_for_publish' ) ); | ||
} | ||
|
||
|
@@ -524,14 +525,22 @@ public function replace_customize_link( $wp_admin_bar ) { | |
); | ||
} | ||
|
||
// Add customize_snapshot_uuid param as param to customize.php itself. | ||
$customize_node->href = add_query_arg( | ||
array( | ||
$this->get_customize_uuid_param() => $this->current_snapshot_uuid, | ||
), | ||
$customize_node->href | ||
$args = array( | ||
$this->get_customize_uuid_param() => $this->current_snapshot_uuid, | ||
); | ||
|
||
$post = $this->snapshot->post(); | ||
|
||
if ( $post ) { | ||
$preview_url_query_vars = $this->post_type->get_preview_url_query_vars( $post->ID ); | ||
if ( ! empty( $preview_url_query_vars ) ) { | ||
$args = array_merge( $args, $preview_url_query_vars ); | ||
} | ||
} | ||
|
||
// Add customize_snapshot_uuid and preview url params to customize.php itself. | ||
$customize_node->href = add_query_arg( $args, $customize_node->href ); | ||
|
||
$customize_node->meta['class'] .= ' ab-customize-snapshots-item'; | ||
$wp_admin_bar->add_menu( (array) $customize_node ); | ||
} | ||
|
@@ -934,4 +943,69 @@ public function get_front_uuid_param() { | |
public function get_customize_uuid_param() { | ||
return constant( get_class( $this->post_type ) . '::CUSTOMIZE_UUID_PARAM_NAME' ); | ||
} | ||
|
||
/** | ||
* Save the preview url query vars in changeset meta. | ||
* | ||
* @param int $post_id Post id. | ||
*/ | ||
public function save_customize_preview_url_query_vars( $post_id ) { | ||
if ( ! isset( $_POST['customize_preview_url_query_vars'] ) ) { | ||
return; | ||
} | ||
|
||
$original_query_vars = (array) json_decode( wp_unslash( $_POST['customize_preview_url_query_vars'] ) ); | ||
|
||
if ( empty( $original_query_vars ) ) { | ||
return; | ||
} | ||
|
||
$allowed_panel_section_control_params = array( | ||
'autofocus[panel]', | ||
'autofocus[section]', | ||
'autofocus[control]', | ||
); | ||
|
||
$allowed_query_params = array_merge( $allowed_panel_section_control_params, array( | ||
'device', | ||
'scroll', | ||
) ); | ||
|
||
$allowed_devices = array_keys( $this->customize_manager->get_previewable_devices() ); | ||
|
||
$preview_url_query_vars = array(); | ||
$params = wp_array_slice_assoc( $original_query_vars, $allowed_query_params ); | ||
|
||
if ( ! empty( $params ) ) { | ||
foreach ( $params as $param => $value ) { | ||
$is_valid_var = ( | ||
( | ||
in_array( $param, $allowed_panel_section_control_params ) | ||
&& | ||
preg_match( '/[a-z|\[|\]|_|-|0-9]+/', $value ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This regex seems to be missing the initial There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The panel/sections/control ids can be like It doesn't follow any initial or ending pattern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't the purpose to validate that the preg_match( '/^[a-z|\[|\]|_|-|0-9]+$/', $value ) Adding the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @westonruter I think I am not able to get it, Can you see this http://regexr.com/3ffv4 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should probably be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. |
||
) | ||
|| | ||
( | ||
'device' === $param | ||
&& | ||
in_array( $value, $allowed_devices ) | ||
) | ||
|| | ||
( | ||
'scroll' === $param | ||
&& | ||
is_int( $value ) | ||
) | ||
); | ||
|
||
if ( $is_valid_var ) { | ||
$preview_url_query_vars[ $param ] = $value; | ||
} | ||
} | ||
|
||
if ( ! empty( $preview_url_query_vars ) ) { | ||
update_post_meta( $post_id, '_preview_url_query_vars', $preview_url_query_vars ); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,6 +289,13 @@ public function filter_post_row_actions( $actions, $post ) { | |
$args = array( | ||
static::CUSTOMIZE_UUID_PARAM_NAME => $post->post_name, | ||
); | ||
|
||
$preview_url_query_vars = $this->get_preview_url_query_vars( $post->ID ); | ||
|
||
if ( ! empty( $preview_url_query_vars ) ) { | ||
$args = array_merge( $args , $preview_url_query_vars ); | ||
} | ||
|
||
$customize_url = add_query_arg( array_map( 'rawurlencode', $args ), wp_customize_url() ); | ||
$actions = array_merge( | ||
array( | ||
|
@@ -345,9 +352,15 @@ public function render_data_metabox( $post ) { | |
echo '</p>'; | ||
} elseif ( 'publish' !== $post->post_status ) { | ||
echo '<p>'; | ||
$preview_url_query_vars = $this->get_preview_url_query_vars( $post->ID ); | ||
$args = array( | ||
static::CUSTOMIZE_UUID_PARAM_NAME => $post->post_name, | ||
); | ||
|
||
if ( ! empty( $preview_url_query_vars ) ) { | ||
$args = array_merge( $args, $preview_url_query_vars ); | ||
} | ||
|
||
$customize_url = add_query_arg( array_map( 'rawurlencode', $args ), wp_customize_url() ); | ||
echo sprintf( | ||
'<a href="%s" class="button button-secondary">%s</a> ', | ||
|
@@ -849,4 +862,20 @@ public function filter_out_settings_if_removed_in_metabox( $content ) { | |
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Get preview url query vars. | ||
* | ||
* @param int $post_id Post id. | ||
* @return array $preview_url_query_vars Preview url query vars. | ||
*/ | ||
public function get_preview_url_query_vars( $post_id ) { | ||
$preview_url_query_vars = array(); | ||
|
||
if ( $post_id ) { | ||
$preview_url_query_vars = (array) get_post_meta( $post_id, '_preview_url_query_vars', true ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is no postmeta on the changeset post, where So this should probably be: $preview_url_query_vars = get_post_meta( $post_id, '_preview_url_query_vars', true );
if ( ! is_array( $preview_url_query_vars ) ) {
$preview_url_query_vars = array();
} |
||
} | ||
|
||
return $preview_url_query_vars; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!