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

Remember preview url query params #129

Merged
merged 22 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
31096b4
Save history preview url params in changeset meta
mohdsayed Mar 3, 2017
2b29f14
Add preview history url params to edit in preview and customize link
mohdsayed Mar 3, 2017
a9fd390
Add support for 4.6 and check plugin active using function_exists
mohdsayed Mar 6, 2017
6a44a47
Resolve conflict with develop
mohdsayed Mar 6, 2017
95e139b
Get snapshot post id from current post
mohdsayed Mar 6, 2017
108c865
Get post type dynamically for save_post_ action
mohdsayed Mar 6, 2017
d5542c1
Remove customizer history plugin dependency and review changes.
mohdsayed Mar 8, 2017
1774137
Fix JSHint issue, remove unused CustomizerBrowserHistory
mohdsayed Mar 8, 2017
2c4b599
Update regex for validating id and fix get_preview_url_query_vars
mohdsayed Mar 9, 2017
34246cc
Add missing url query param and re-factor save_customize_preview_url_…
westonruter Mar 9, 2017
ab61743
Ensure frontend preview links use the previewed URL when the changese…
westonruter Mar 9, 2017
65c042b
Rename Preview Snapshot to Preview Changeset
westonruter Mar 9, 2017
e78ae0a
Fix passing of integer scroll
westonruter Mar 9, 2017
d3438ad
Remove dependency on Customizer Browser History
westonruter Mar 9, 2017
3534389
Re-use get_frontend_view_link in filter for getting permalink
westonruter Mar 9, 2017
beaf92a
Update wp-dev-lib 85f9cf4...8b767f9: Add workaround for phpunit failu…
westonruter Mar 10, 2017
f12a74a
Update .travis.yml with latest from dev-lib
westonruter Mar 10, 2017
4caa963
Update dev-lib with https://github.com/xwp/wp-dev-lib/pull/221
westonruter Mar 10, 2017
1aec717
Add unit tests and improve method naming
westonruter Mar 10, 2017
f5b55cd
Ensure the current URL being previewed on the frontend is always used…
westonruter Mar 10, 2017
6d9e9a9
Fix phpunit test for 4.6.1
mohdsayed Mar 10, 2017
43bee5b
Fix phpunit test for 4.6.1
mohdsayed Mar 10, 2017
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
39 changes: 36 additions & 3 deletions js/compat/customize-snapshots.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global jQuery, wp, _customizeSnapshotsCompatSettings */
/* eslint consistent-this: ["error", "snapshot"] */
/* global jQuery, wp, _customizeSnapshotsCompatSettings, JSON */
/* eslint consistent-this: ["error", "snapshot"], no-magic-numbers: [ "error", { "ignore": [0,1,2] } ] */

( function( api, $ ) {
'use strict';
Expand Down Expand Up @@ -222,10 +222,16 @@
* @return {void}
*/
extendPreviewerQuery: function extendPreviewerQuery() {
var snapshot = this, originalQuery = api.previewer.query;
var snapshot = this, originalQuery = api.previewer.query, previewURLQueryParams;

api.previewer.query = function() {
var retval = originalQuery.apply( this, arguments );

previewURLQueryParams = location.search.substr( 1 );
if ( previewURLQueryParams ) {
retval.customize_preview_url_query_vars = JSON.stringify( snapshot.parseQueryString( previewURLQueryParams ) );
}

if ( api.state( 'snapshot-exists' ).get() ) {
retval.customize_snapshot_uuid = snapshot.data.uuid;
if ( snapshot.snapshotTitle && snapshot.snapshotTitle.val() ) {
Expand Down Expand Up @@ -485,6 +491,33 @@

snapshot.updateCountdown();
snapshot.editContainer.find( '.reset-time' ).toggle( scheduled );
},

/**
* Parse query string.
* Taken from WP 4.7.0
*
* @param {string} queryString Query string.
* @returns {object} Parsed query string.
*/
parseQueryString: function parseQueryString( queryString ) {
var queryParams = {};
_.each( queryString.split( '&' ), function( pair ) {
var parts, key, value;
parts = pair.split( '=', 2 );
if ( ! parts[0] ) {
return;
}
key = decodeURIComponent( parts[0].replace( /\+/g, ' ' ) );
key = key.replace( / /g, '_' ); // What PHP does.
if ( _.isUndefined( parts[1] ) ) {
value = null;
} else {
value = decodeURIComponent( parts[1].replace( /\+/g, ' ' ) );
}
queryParams[ key ] = value;
} );
return queryParams;
}
} );

Expand Down
10 changes: 8 additions & 2 deletions js/customize-snapshots.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global jQuery, wp, _customizeSnapshotsSettings */
/* global jQuery, wp, JSON, _customizeSnapshotsSettings */
/* eslint no-magic-numbers: [ "error", { "ignore": [0,1,-1] } ], consistent-this: [ "error", "snapshot" ] */

(function( api, $ ) {
Expand Down Expand Up @@ -962,10 +962,16 @@
* @return {void}
*/
extendPreviewerQuery: function extendPreviewerQuery() {
var snapshot = this, originalQuery = api.previewer.query;
var snapshot = this, originalQuery = api.previewer.query, previewURLQueryParams;

api.previewer.query = function() {
var retval = originalQuery.apply( this, arguments );

previewURLQueryParams = location.search.substr( 1 );
if ( previewURLQueryParams ) {
retval.customize_preview_url_query_vars = JSON.stringify( api.utils.parseQueryString( previewURLQueryParams ) );
}

if ( snapshot.editControlSettings( 'title' ).get() ) {
retval.customize_changeset_title = snapshot.editControlSettings( 'title' ).get();
}
Expand Down
88 changes: 81 additions & 7 deletions php/class-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

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' ) );
}

Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex seems to be missing the initial ^( and ending )$ if it is doing what I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The panel/sections/control ids can be like
post[post][1437]
title_tagline
blogname
nav_menu-61
postmeta[posttype][27450][keywords]

It doesn't follow any initial or ending pattern?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the purpose to validate that the $param is a valid ID string? If you want to ensure that only the valid characters are included then you should do:

preg_match( '/^[a-z|\[|\]|_|-|0-9]+$/', $value )

Adding the ^ and $ anchors, as otherwise a user could supply an ID like: !@#$%^&*()title_tagline⁄€‹›fifl‡°·‚

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably be \[a-z|\[|\]|_|\-|0-9]+\ ( escaped - sign ) but /^[a-z|\[|\]|_|-|0-9]+$/ doesn't match anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sayedtaqui from that example, you can see that it is matching all of those strings, the last one incorrect.

Compare:

image

and

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 );
}
}
}
}
29 changes: 29 additions & 0 deletions php/class-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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> ',
Expand Down Expand Up @@ -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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no postmeta on the changeset post, where get_post_meta() returns false, this will result in $preview_url_query_vars === array( false ).

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;
}
}