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 all 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
42 changes: 25 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
sudo: false

notifications:
email:
on_success: never
on_failure: change

cache:
directories:
- node_modules
- vendor
- $HOME/phpunit-bin

language:
- php
- node_js
- php
- node_js

php:
- 5.3
- 7.0

node_js:
- stable
- 5.3
- 7.0

env:
- WP_VERSION=trunk WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=4.6.1 WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=1
- WP_VERSION=trunk WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=4.6.1 WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=1

install:
- nvm install 4 && nvm use 4
- export DEV_LIB_PATH=dev-lib
- if [ ! -e "$DEV_LIB_PATH" ] && [ -L .travis.yml ]; then export DEV_LIB_PATH=$( dirname $( readlink .travis.yml ) ); fi
- source $DEV_LIB_PATH/travis.install.sh
- nvm install 6 && nvm use 6
- export DEV_LIB_PATH=dev-lib
- if [ ! -e "$DEV_LIB_PATH" ] && [ -L .travis.yml ]; then export DEV_LIB_PATH=$( dirname $( readlink .travis.yml ) ); fi
- source $DEV_LIB_PATH/travis.install.sh

script:
- source $DEV_LIB_PATH/travis.script.sh
- source $DEV_LIB_PATH/travis.script.sh

after_script:
- source $DEV_LIB_PATH/travis.after_script.sh
- source $DEV_LIB_PATH/travis.after_script.sh
2 changes: 1 addition & 1 deletion dev-lib
6 changes: 4 additions & 2 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 @@ -226,6 +226,8 @@

api.previewer.query = function() {
var retval = originalQuery.apply( this, arguments );
retval.customizer_state_query_vars = JSON.stringify( snapshot.getStateQueryVars() );

if ( api.state( 'snapshot-exists' ).get() ) {
retval.customize_snapshot_uuid = snapshot.data.uuid;
if ( snapshot.snapshotTitle && snapshot.snapshotTitle.val() ) {
Expand Down
34 changes: 33 additions & 1 deletion 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 @@ -98,6 +98,35 @@
} );
},

/**
* Get state query vars.
*
* @return {{}} Query vars for scroll, device, url, and autofocus.
*/
getStateQueryVars: function() {
var queryVars = {
'autofocus[control]': null,
'autofocus[section]': null,
'autofocus[panel]': null
};
queryVars.scroll = parseInt( api.previewer.scroll, 10 ) || 0;
queryVars.device = api.previewedDevice.get();
queryVars.url = api.previewer.previewUrl.get();

_.find( [ 'control', 'section', 'panel' ], function( constructType ) {
var found = false;
api[ constructType ].each( function( construct ) { // @todo Core needs to support more Backbone methods on wp.customize.Values().
if ( ! found && construct.expanded && construct.expanded.get() ) {
queryVars[ 'autofocus[' + constructType + ']' ] = construct.id;
found = true;
}
} );
return found;
} );

return queryVars;
},

/**
* Update snapshot.
*
Expand Down Expand Up @@ -966,6 +995,9 @@

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

retval.customizer_state_query_vars = JSON.stringify( snapshot.getStateQueryVars() );

if ( snapshot.editControlSettings( 'title' ).get() ) {
retval.customize_changeset_title = snapshot.editControlSettings( 'title' ).get();
}
Expand Down
43 changes: 35 additions & 8 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_customizer_state_query_vars' ) );
add_filter( 'wp_insert_post_data', array( $this, 'prepare_snapshot_post_content_for_publish' ) );
}

Expand Down Expand Up @@ -516,22 +517,29 @@ public function replace_customize_link( $wp_admin_bar ) {
$preview_url_parsed = wp_parse_url( $customize_node->href );
parse_str( $preview_url_parsed['query'], $preview_url_query_params );
if ( ! empty( $preview_url_query_params['url'] ) ) {
$preview_url_query_params['url'] = remove_query_arg( array( $this->get_front_uuid_param() ), $preview_url_query_params['url'] );
$preview_url_query_params['url'] = rawurlencode( remove_query_arg( array( $this->get_front_uuid_param() ), $preview_url_query_params['url'] ) );
$customize_node->href = preg_replace(
'/(?<=\?).*?(?=#|$)/',
build_query( $preview_url_query_params ),
$customize_node->href
);
}

// 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 ) {
$customizer_state_query_vars = $this->post_type->get_customizer_state_query_vars( $post->ID );
unset( $customizer_state_query_vars['url'] );
$args = array_merge( $args, $customizer_state_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 +942,23 @@ 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_customizer_state_query_vars( $post_id ) {
if ( ! isset( $_POST['customizer_state_query_vars'] ) ) {
return;
}

$original_query_vars = json_decode( wp_unslash( $_POST['customizer_state_query_vars'] ), true );

if ( empty( $original_query_vars ) || ! is_array( $original_query_vars ) ) {
return;
}

$this->post_type->set_customizer_state_query_vars( $post_id, $original_query_vars );
}
}
105 changes: 90 additions & 15 deletions php/class-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ public function add_admin_menu_item() {
*/
public function filter_post_type_link( $url, $post ) {
if ( static::SLUG === $post->post_type ) {
$url = add_query_arg(
array(
static::FRONT_UUID_PARAM_NAME => $post->post_name,
),
home_url( '/' )
);
$url = $this->get_frontend_view_link( $post );
}
return $url;
}
Expand Down Expand Up @@ -286,9 +281,13 @@ public function filter_post_row_actions( $actions, $post ) {

$post_type_obj = get_post_type_object( static::SLUG );
if ( 'publish' !== $post->post_status && current_user_can( $post_type_obj->cap->edit_post, $post->ID ) ) {
$args = array(
static::CUSTOMIZE_UUID_PARAM_NAME => $post->post_name,
$args = array_merge(
$this->get_customizer_state_query_vars( $post->ID ),
array(
static::CUSTOMIZE_UUID_PARAM_NAME => $post->post_name,
)
);

$customize_url = add_query_arg( array_map( 'rawurlencode', $args ), wp_customize_url() );
$actions = array_merge(
array(
Expand Down Expand Up @@ -340,14 +339,18 @@ public function render_data_metabox( $post ) {
$snapshot_theme = get_post_meta( $post->ID, '_snapshot_theme', true );
if ( ! empty( $snapshot_theme ) && get_stylesheet() !== $snapshot_theme ) {
echo '<p>';
/* translators: 1 is the theme the snapshot was created for */
echo sprintf( esc_html__( 'This snapshot was made when a different theme was active (%1$s), so currently it cannot be edited.', 'customize-snapshots' ), esc_html( $snapshot_theme ) );
/* translators: 1 is the theme the changeset was created for */
echo sprintf( esc_html__( 'This changeset was made when a different theme was active (%1$s), so currently it cannot be edited.', 'customize-snapshots' ), esc_html( $snapshot_theme ) );
echo '</p>';
} elseif ( 'publish' !== $post->post_status ) {
echo '<p>';
$args = array(
static::CUSTOMIZE_UUID_PARAM_NAME => $post->post_name,
$args = array_merge(
$this->get_customizer_state_query_vars( $post->ID ),
array(
static::CUSTOMIZE_UUID_PARAM_NAME => $post->post_name,
)
);

$customize_url = add_query_arg( array_map( 'rawurlencode', $args ), wp_customize_url() );
echo sprintf(
'<a href="%s" class="button button-secondary">%s</a> ',
Expand All @@ -359,7 +362,7 @@ public function render_data_metabox( $post ) {
echo sprintf(
'<a href="%s" class="button button-secondary">%s</a>',
esc_url( $frontend_view_url ),
esc_html__( 'Preview Snapshot', 'customize-snapshots' )
esc_html__( 'Preview Changeset', 'customize-snapshots' )
);
echo '</p>';
}
Expand Down Expand Up @@ -681,7 +684,7 @@ public function hide_add_new_changeset_button() {
* @return mixed
*/
public function add_snapshot_bulk_actions( $bulk_actions ) {
$bulk_actions['merge_snapshot'] = __( 'Merge Snapshot', 'customize-snapshots' );
$bulk_actions['merge_snapshot'] = __( 'Merge Changeset', 'customize-snapshots' );
return $bulk_actions;
}

Expand Down Expand Up @@ -791,7 +794,7 @@ public function admin_show_merge_error() {
return;
}
$error = array(
1 => __( 'At-least two snapshot required for merge.', 'customize-snapshots' ),
1 => __( 'At-least two changesets required for merge.', 'customize-snapshots' ),
);
$error_code = intval( $_REQUEST['merge-error'] ); // WPCS: input var ok.
if ( ! isset( $error[ $error_code ] ) ) {
Expand Down Expand Up @@ -849,4 +852,76 @@ public function filter_out_settings_if_removed_in_metabox( $content ) {

return $content;
}

/**
* Get customizer session state query vars.
*
* @param int $post_id Post id.
* @return array $preview_url_query_vars Preview url query vars.
*/
public function get_customizer_state_query_vars( $post_id ) {
$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;
}

/**
* Set customizer session state query vars.
*
* Supplied query vars are validated and sanitized.
*
* @param int $post_id Post id.
* @param array $query_vars Post id.
* @return array Sanitized query vars.
*/
public function set_customizer_state_query_vars( $post_id, $query_vars ) {
$stored_query_vars = array();
$autofocus_query_vars = array( 'autofocus[panel]', 'autofocus[section]', 'autofocus[control]' );

$this->snapshot_manager->ensure_customize_manager();

foreach ( wp_array_slice_assoc( $query_vars, $autofocus_query_vars ) as $key => $value ) {
if ( preg_match( '/^[a-z|\[|\]|_|\-|0-9]+$/', $value ) ) {
$stored_query_vars[ $key ] = $value;
}
}
if ( ! empty( $query_vars['url'] ) && wp_validate_redirect( $query_vars['url'] ) ) {
$stored_query_vars['url'] = esc_url_raw( $query_vars['url'] );
}
if ( isset( $query_vars['device'] ) && in_array( $query_vars['device'], array_keys( $this->snapshot_manager->customize_manager->get_previewable_devices() ), true ) ) {
$stored_query_vars['device'] = $query_vars['device'];
}
if ( isset( $query_vars['scroll'] ) && is_int( $query_vars['scroll'] ) ) {
$stored_query_vars['scroll'] = $query_vars['scroll'];
}
update_post_meta( $post_id, '_preview_url_query_vars', $stored_query_vars );
return $stored_query_vars;
}

/**
* Get frontend view link.
*
* Returns URL to frontend with customize_changeset_uuid param supplied.
* If the changeset was saved in the customizer then the URL being previewed
* will serve as the base URL as opposed to the home URL as normally.
*
* @see Post_Type::filter_post_type_link()
* @param int|\WP_Post $post Changeset post.
* @return string URL.
*/
public function get_frontend_view_link( $post ) {
$post = get_post( $post );
$preview_url_query_vars = $this->get_customizer_state_query_vars( $post->ID );
$base_url = isset( $preview_url_query_vars['url'] ) ? $preview_url_query_vars['url'] : home_url( '/' );
return add_query_arg(
array(
static::FRONT_UUID_PARAM_NAME => $post->post_name,
),
$base_url
);
}
}
Loading