From 31096b4ea67ae6a111b9985bfa281a0a5f76764d Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Sat, 4 Mar 2017 02:37:12 +0530 Subject: [PATCH 01/21] Save history preview url params in changeset meta --- js/customize-snapshots.js | 7 ++- php/class-customize-snapshot-manager.php | 66 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index a1cf2dc1..619b15e1 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -1,4 +1,4 @@ -/* global jQuery, wp, _customizeSnapshotsSettings */ +/* global jQuery, wp, JSON, _customizeSnapshotsSettings, CustomizerBrowserHistory */ /* eslint no-magic-numbers: [ "error", { "ignore": [0,1,-1] } ], consistent-this: [ "error", "snapshot" ] */ (function( api, $ ) { @@ -966,6 +966,11 @@ api.previewer.query = function() { var retval = originalQuery.apply( this, arguments ); + + if ( ! _.isUndefined( CustomizerBrowserHistory ) ) { + retval.customize_preview_history_url_params = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); + } + if ( snapshot.editControlSettings( 'title' ).get() ) { retval.customize_changeset_title = snapshot.editControlSettings( 'title' ).get(); } diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index ee05b00a..24ce5d4d 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -105,6 +105,7 @@ function hooks() { 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_customize_changeset', array( $this, 'save_customize_preview_history_url_params' ) ); add_filter( 'wp_insert_post_data', array( $this, 'prepare_snapshot_post_content_for_publish' ) ); } @@ -928,4 +929,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 history url params in changeset meta. + * + * @param int $post_id Post id. + */ + public function save_customize_preview_history_url_params( $post_id ) { + if ( ! isset( $_POST['customize_preview_history_url_params'] ) ) { + return; + } + + $original_params = (array) json_decode( wp_unslash( $_POST['customize_preview_history_url_params'] ) ); + + if ( empty( $original_params ) ) { + return; + } + + $allowed_panel_section_control_keys = array( + 'autofocus[panel]', + 'autofocus[section]', + 'autofocus[control]', + ); + + $allowed_devices = array( + 'mobile', + 'desktop', + 'tablet', + ); + + $allowed_keys = array_merge( $allowed_panel_section_control_keys, array( + 'device', + 'scroll', + ) ); + + $history_params = array(); + $params = wp_array_slice_assoc( $original_params, $allowed_keys ); + + if ( ! empty( $params ) ) { + foreach ( $params as $key => $value ) { + + if ( in_array( $key, $allowed_panel_section_control_keys ) ) { + $is_valid_id = ( + preg_match( '/[a-z|\[|\]|_|0-9]+/', $value ) + && + count( $value ) < 100 + ); + if ( $is_valid_id ) { + $history_params[ $key ] = $value; + } + } + + if ( 'device' === $key && in_array( $value, $allowed_devices ) ) { + $history_params[ $key ] = $value; + } + + if ( 'scroll' === $key && is_int( $value ) && $value < 100000 ) { + $history_params[ $key ] = $value; + } + } + + if ( ! empty( $history_params ) ) { + update_post_meta( $post_id, 'customize_preview_history_url_params', $history_params ); + } + } + } } From 2b29f14b51980d41c9bd914977b2dcf94490c1ed Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Sat, 4 Mar 2017 03:25:27 +0530 Subject: [PATCH 02/21] Add preview history url params to edit in preview and customize link --- js/customize-snapshots.js | 2 +- php/class-customize-snapshot-manager.php | 92 ++++++++++++++---------- php/class-post-type.php | 30 ++++++++ 3 files changed, 84 insertions(+), 40 deletions(-) diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index 619b15e1..31bb6e53 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -968,7 +968,7 @@ var retval = originalQuery.apply( this, arguments ); if ( ! _.isUndefined( CustomizerBrowserHistory ) ) { - retval.customize_preview_history_url_params = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); + retval.customize_preview_url_query_vars = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); } if ( snapshot.editControlSettings( 'title' ).get() ) { diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index 24ce5d4d..42e5c970 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -105,7 +105,7 @@ function hooks() { 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_customize_changeset', array( $this, 'save_customize_preview_history_url_params' ) ); + add_action( 'save_post_customize_changeset', array( $this, 'save_customize_preview_url_query_vars' ) ); add_filter( 'wp_insert_post_data', array( $this, 'prepare_snapshot_post_content_for_publish' ) ); } @@ -523,12 +523,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 + $post_id = $this->customize_manager->changeset_post_id(); + + include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + $preview_url_query_vars = $this->post_type->get_preview_url_query_vars( $post_id ); + + $args = array( + $this->get_customize_uuid_param() => $this->current_snapshot_uuid, ); + 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 ); } @@ -931,66 +941,70 @@ public function get_customize_uuid_param() { } /** - * Save the preview history url params in changeset meta. + * Save the preview url query vars in changeset meta. * * @param int $post_id Post id. */ - public function save_customize_preview_history_url_params( $post_id ) { - if ( ! isset( $_POST['customize_preview_history_url_params'] ) ) { + public function save_customize_preview_url_query_vars( $post_id ) { + if ( ! isset( $_POST['customize_preview_url_query_vars'] ) ) { return; } - $original_params = (array) json_decode( wp_unslash( $_POST['customize_preview_history_url_params'] ) ); + $original_query_vars = (array) json_decode( wp_unslash( $_POST['customize_preview_url_query_vars'] ) ); - if ( empty( $original_params ) ) { + if ( empty( $original_query_vars ) ) { return; } - $allowed_panel_section_control_keys = array( + $allowed_panel_section_control_params = array( 'autofocus[panel]', 'autofocus[section]', 'autofocus[control]', ); - $allowed_devices = array( - 'mobile', - 'desktop', - 'tablet', - ); - - $allowed_keys = array_merge( $allowed_panel_section_control_keys, array( + $allowed_query_params = array_merge( $allowed_panel_section_control_params, array( 'device', 'scroll', ) ); - $history_params = array(); - $params = wp_array_slice_assoc( $original_params, $allowed_keys ); + $allowed_devices = array( + 'mobile', + 'desktop', + 'tablet', + ); + + $preview_url_query_vars = array(); + $params = wp_array_slice_assoc( $original_query_vars, $allowed_query_params ); if ( ! empty( $params ) ) { - foreach ( $params as $key => $value ) { - - if ( in_array( $key, $allowed_panel_section_control_keys ) ) { - $is_valid_id = ( - preg_match( '/[a-z|\[|\]|_|0-9]+/', $value ) + foreach ( $params as $param => $value ) { + $is_valid_var = ( + ( + in_array( $param, $allowed_panel_section_control_params ) && - count( $value ) < 100 - ); - if ( $is_valid_id ) { - $history_params[ $key ] = $value; - } - } - - if ( 'device' === $key && in_array( $value, $allowed_devices ) ) { - $history_params[ $key ] = $value; - } + preg_match( '/[a-z|\[|\]|_|-|0-9]+/', $value ) + ) + || + ( + 'device' === $param + && + in_array( $value, $allowed_devices ) + ) + || + ( + 'scroll' === $param + && + is_int( $value ) + ) + ); - if ( 'scroll' === $key && is_int( $value ) && $value < 100000 ) { - $history_params[ $key ] = $value; + if ( $is_valid_var ) { + $preview_url_query_vars[ $param ] = $value; } } - if ( ! empty( $history_params ) ) { - update_post_meta( $post_id, 'customize_preview_history_url_params', $history_params ); + if ( ! empty( $preview_url_query_vars ) ) { + update_post_meta( $post_id, '_preview_url_query_vars', $preview_url_query_vars ); } } } diff --git a/php/class-post-type.php b/php/class-post-type.php index 4b834f70..d22b7637 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -283,6 +283,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( @@ -339,9 +346,15 @@ public function render_data_metabox( $post ) { echo '

'; } elseif ( 'publish' !== $post->post_status ) { echo '

'; + $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( '%s ', @@ -840,4 +853,21 @@ 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(); + $wp_customize_browser_history = 'wp-customizer-browser-history/customizer-browser-history.php'; + + if ( is_plugin_active( $wp_customize_browser_history ) ) { + $preview_url_query_vars = (array) get_post_meta( $post_id, '_preview_url_query_vars', true ); + } + + return $preview_url_query_vars; + } } From a9fd3904be86686f670b21eec521ab9bcc5c7536 Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Mon, 6 Mar 2017 17:21:41 +0530 Subject: [PATCH 03/21] Add support for 4.6 and check plugin active using function_exists --- js/compat/customize-snapshots.js | 7 ++++++- php/class-customize-snapshot-manager-back-compat.php | 1 + php/class-customize-snapshot-manager.php | 4 +--- php/class-post-type.php | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/js/compat/customize-snapshots.js b/js/compat/customize-snapshots.js index 0e1a17f4..e46f6c5e 100644 --- a/js/compat/customize-snapshots.js +++ b/js/compat/customize-snapshots.js @@ -1,4 +1,4 @@ -/* global jQuery, wp, _customizeSnapshotsCompatSettings */ +/* global jQuery, wp, _customizeSnapshotsCompatSettings, CustomizerBrowserHistory, JSON */ /* eslint consistent-this: ["error", "snapshot"] */ ( function( api, $ ) { @@ -226,6 +226,11 @@ api.previewer.query = function() { var retval = originalQuery.apply( this, arguments ); + + if ( ! _.isUndefined( CustomizerBrowserHistory ) ) { + retval.customize_preview_url_query_vars = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); + } + if ( api.state( 'snapshot-exists' ).get() ) { retval.customize_snapshot_uuid = snapshot.data.uuid; if ( snapshot.snapshotTitle && snapshot.snapshotTitle.val() ) { diff --git a/php/class-customize-snapshot-manager-back-compat.php b/php/class-customize-snapshot-manager-back-compat.php index 219be0c4..ec2c19a1 100644 --- a/php/class-customize-snapshot-manager-back-compat.php +++ b/php/class-customize-snapshot-manager-back-compat.php @@ -30,6 +30,7 @@ function init() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) ); add_action( 'customize_save', array( $this, 'check_customize_publish_authorization' ), 10, 0 ); + add_action( 'save_post_customize_snapshot', array( $this, 'save_customize_preview_url_query_vars' ) ); $this->hooks(); if ( $this->read_current_snapshot_uuid() ) { $this->load_snapshot(); diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index 42e5c970..9a87751a 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -523,9 +523,7 @@ public function replace_customize_link( $wp_admin_bar ) { ); } - $post_id = $this->customize_manager->changeset_post_id(); - - include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + $post_id = $this->post_type->find_post( $this->current_snapshot_uuid ); $preview_url_query_vars = $this->post_type->get_preview_url_query_vars( $post_id ); $args = array( diff --git a/php/class-post-type.php b/php/class-post-type.php index d22b7637..66676526 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -862,9 +862,9 @@ public function filter_out_settings_if_removed_in_metabox( $content ) { */ public function get_preview_url_query_vars( $post_id ) { $preview_url_query_vars = array(); - $wp_customize_browser_history = 'wp-customizer-browser-history/customizer-browser-history.php'; - if ( is_plugin_active( $wp_customize_browser_history ) ) { + // If customizer browser history plugin is active. + if ( function_exists( 'customizer_browser_history_enqueue_scripts' ) && $post_id ) { $preview_url_query_vars = (array) get_post_meta( $post_id, '_preview_url_query_vars', true ); } From 95e139bf1b8b587639c9cc71734f092cea7abc4f Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Mon, 6 Mar 2017 18:32:51 +0530 Subject: [PATCH 04/21] Get snapshot post id from current post --- js/compat/customize-snapshots.js | 2 +- js/customize-snapshots.js | 2 +- php/class-customize-snapshot-manager.php | 12 +++++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/js/compat/customize-snapshots.js b/js/compat/customize-snapshots.js index e46f6c5e..24de0546 100644 --- a/js/compat/customize-snapshots.js +++ b/js/compat/customize-snapshots.js @@ -227,7 +227,7 @@ api.previewer.query = function() { var retval = originalQuery.apply( this, arguments ); - if ( ! _.isUndefined( CustomizerBrowserHistory ) ) { + if ( 'undefined' !== typeof CustomizerBrowserHistory ) { retval.customize_preview_url_query_vars = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); } diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index 7242a790..e7fbb6ae 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -967,7 +967,7 @@ api.previewer.query = function() { var retval = originalQuery.apply( this, arguments ); - if ( ! _.isUndefined( CustomizerBrowserHistory ) ) { + if ( 'undefined' !== typeof CustomizerBrowserHistory ) { retval.customize_preview_url_query_vars = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); } diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index ad23effc..5ee23305 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -525,15 +525,17 @@ public function replace_customize_link( $wp_admin_bar ) { ); } - $post_id = $this->post_type->find_post( $this->current_snapshot_uuid ); - $preview_url_query_vars = $this->post_type->get_preview_url_query_vars( $post_id ); - $args = array( $this->get_customize_uuid_param() => $this->current_snapshot_uuid, ); - if ( ! empty( $preview_url_query_vars ) ) { - $args = array_merge( $args, $preview_url_query_vars ); + $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. From 108c8658e9181c927bbb43ad6cd6c748469090e4 Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Mon, 6 Mar 2017 18:50:02 +0530 Subject: [PATCH 05/21] Get post type dynamically for save_post_ action --- php/class-customize-snapshot-manager-back-compat.php | 1 - php/class-customize-snapshot-manager.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/php/class-customize-snapshot-manager-back-compat.php b/php/class-customize-snapshot-manager-back-compat.php index 236d6cc5..43980075 100644 --- a/php/class-customize-snapshot-manager-back-compat.php +++ b/php/class-customize-snapshot-manager-back-compat.php @@ -30,7 +30,6 @@ function init() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) ); add_action( 'customize_save', array( $this, 'check_customize_publish_authorization' ), 10, 0 ); - add_action( 'save_post_customize_snapshot', array( $this, 'save_customize_preview_url_query_vars' ) ); $this->hooks(); if ( $this->read_current_snapshot_uuid() ) { $this->load_snapshot(); diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index 5ee23305..bc03a4ac 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -104,8 +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_customize_changeset', array( $this, 'save_customize_preview_url_query_vars' ) ); + 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' ) ); } From d5542c1863af0d3c16ade3e05014b939a9c0d855 Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Wed, 8 Mar 2017 17:11:23 +0530 Subject: [PATCH 06/21] Remove customizer history plugin dependency and review changes. --- js/compat/customize-snapshots.js | 36 +++++++++++++++++++++--- js/customize-snapshots.js | 7 +++-- php/class-customize-snapshot-manager.php | 8 ++---- php/class-post-type.php | 3 +- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/js/compat/customize-snapshots.js b/js/compat/customize-snapshots.js index 24de0546..4dae5ba1 100644 --- a/js/compat/customize-snapshots.js +++ b/js/compat/customize-snapshots.js @@ -1,5 +1,5 @@ /* global jQuery, wp, _customizeSnapshotsCompatSettings, CustomizerBrowserHistory, JSON */ -/* eslint consistent-this: ["error", "snapshot"] */ +/* eslint consistent-this: ["error", "snapshot"], no-magic-numbers: [ "error", { "ignore": [0,1,2] } ] */ ( function( api, $ ) { 'use strict'; @@ -222,13 +222,14 @@ * @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 ); - if ( 'undefined' !== typeof CustomizerBrowserHistory ) { - retval.customize_preview_url_query_vars = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); + previewURLQueryParams = location.search.substr( 1 ); + if ( previewURLQueryParams ) { + retval.customize_preview_url_query_vars = JSON.stringify( snapshot.parseQueryString( previewURLQueryParams ) ); } if ( api.state( 'snapshot-exists' ).get() ) { @@ -490,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; } } ); diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index e7fbb6ae..5517e854 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -962,13 +962,14 @@ * @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 ); - if ( 'undefined' !== typeof CustomizerBrowserHistory ) { - retval.customize_preview_url_query_vars = JSON.stringify( CustomizerBrowserHistory.getQueryParams( location.href ) ); + previewURLQueryParams = location.search.substr( 1 ); + if ( previewURLQueryParams ) { + retval.customize_preview_url_query_vars = JSON.stringify( api.utils.parseQueryString( previewURLQueryParams ) ); } if ( snapshot.editControlSettings( 'title' ).get() ) { diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index bc03a4ac..6d6dfbb0 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -105,7 +105,7 @@ function hooks() { 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_' . $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_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' ) ); } @@ -971,11 +971,7 @@ public function save_customize_preview_url_query_vars( $post_id ) { 'scroll', ) ); - $allowed_devices = array( - 'mobile', - 'desktop', - 'tablet', - ); + $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 ); diff --git a/php/class-post-type.php b/php/class-post-type.php index cb2e2b9c..a7a4995e 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -872,8 +872,7 @@ public function filter_out_settings_if_removed_in_metabox( $content ) { public function get_preview_url_query_vars( $post_id ) { $preview_url_query_vars = array(); - // If customizer browser history plugin is active. - if ( function_exists( 'customizer_browser_history_enqueue_scripts' ) && $post_id ) { + if ( $post_id ) { $preview_url_query_vars = (array) get_post_meta( $post_id, '_preview_url_query_vars', true ); } From 177413727daf0ff627cf1181b3a4deee128e3b2a Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Wed, 8 Mar 2017 18:32:36 +0530 Subject: [PATCH 07/21] Fix JSHint issue, remove unused CustomizerBrowserHistory --- js/compat/customize-snapshots.js | 2 +- js/customize-snapshots.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/compat/customize-snapshots.js b/js/compat/customize-snapshots.js index 4dae5ba1..a98436e3 100644 --- a/js/compat/customize-snapshots.js +++ b/js/compat/customize-snapshots.js @@ -1,4 +1,4 @@ -/* global jQuery, wp, _customizeSnapshotsCompatSettings, CustomizerBrowserHistory, JSON */ +/* global jQuery, wp, _customizeSnapshotsCompatSettings, JSON */ /* eslint consistent-this: ["error", "snapshot"], no-magic-numbers: [ "error", { "ignore": [0,1,2] } ] */ ( function( api, $ ) { diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index 5517e854..a82d296a 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -1,4 +1,4 @@ -/* global jQuery, wp, JSON, _customizeSnapshotsSettings, CustomizerBrowserHistory */ +/* global jQuery, wp, JSON, _customizeSnapshotsSettings */ /* eslint no-magic-numbers: [ "error", { "ignore": [0,1,-1] } ], consistent-this: [ "error", "snapshot" ] */ (function( api, $ ) { From 2c4b599073e7f3532f606d41f669f55c7141f817 Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Thu, 9 Mar 2017 06:21:49 +0530 Subject: [PATCH 08/21] Update regex for validating id and fix get_preview_url_query_vars --- php/class-customize-snapshot-manager.php | 2 +- php/class-post-type.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index 6d6dfbb0..6737b237 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -982,7 +982,7 @@ public function save_customize_preview_url_query_vars( $post_id ) { ( in_array( $param, $allowed_panel_section_control_params ) && - preg_match( '/[a-z|\[|\]|_|-|0-9]+/', $value ) + preg_match( '/^[a-z|\[|\]|_|\-|0-9]+$/', $value ) ) || ( diff --git a/php/class-post-type.php b/php/class-post-type.php index a7a4995e..46b5cb41 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -870,10 +870,10 @@ public function filter_out_settings_if_removed_in_metabox( $content ) { * @return array $preview_url_query_vars Preview url query vars. */ public function get_preview_url_query_vars( $post_id ) { - $preview_url_query_vars = array(); + $preview_url_query_vars = get_post_meta( $post_id, '_preview_url_query_vars', true ); - if ( $post_id ) { - $preview_url_query_vars = (array) 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; From 34246cc552ed7822c46f4c5db517489c461353f7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 09:15:27 -0800 Subject: [PATCH 09/21] Add missing url query param and re-factor save_customize_preview_url_query_vars --- php/class-customize-snapshot-manager.php | 59 +++++++----------------- 1 file changed, 16 insertions(+), 43 deletions(-) diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index 6737b237..3cf121a3 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -960,52 +960,25 @@ public function save_customize_preview_url_query_vars( $post_id ) { 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', - ) ); + $stored_query_vars = array(); - $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 ) - ) - || - ( - 'device' === $param - && - in_array( $value, $allowed_devices ) - ) - || - ( - 'scroll' === $param - && - is_int( $value ) - ) - ); - - if ( $is_valid_var ) { - $preview_url_query_vars[ $param ] = $value; - } + $autofocus_query_vars = array( 'autofocus[panel]', 'autofocus[section]', 'autofocus[control]' ); + foreach ( wp_array_slice_assoc( $original_query_vars, $autofocus_query_vars ) as $key => $value ) { + if ( preg_match( '/^[a-z|\[|\]|_|\-|0-9]+$/', $value ) ) { + $stored_query_vars[ $key ] = $value; } + } - if ( ! empty( $preview_url_query_vars ) ) { - update_post_meta( $post_id, '_preview_url_query_vars', $preview_url_query_vars ); - } + if ( ! empty( $original_query_vars['url'] ) && wp_validate_redirect( $original_query_vars['url'] ) ) { + $stored_query_vars['url'] = esc_url_raw( $original_query_vars['url'] ); + } + + if ( isset( $original_query_vars['device'] ) && in_array( $original_query_vars['device'], array_keys( $this->customize_manager->get_previewable_devices() ), true ) ) { + $stored_query_vars['device'] = $original_query_vars['device']; + } + if ( isset( $original_query_vars['scroll'] ) && is_int( $original_query_vars['scroll'] ) ) { + $stored_query_vars['scroll'] = $original_query_vars['scroll']; } + update_post_meta( $post_id, '_preview_url_query_vars', $stored_query_vars ); } } From ab61743afd2b5db787ad1c07bf0d692bd2aabaec Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 09:45:49 -0800 Subject: [PATCH 10/21] Ensure frontend preview links use the previewed URL when the changeset was saved --- js/customize-snapshots.js | 4 ++- php/class-customize-snapshot-manager.php | 4 +-- php/class-post-type.php | 32 +++++++++++++++++++++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index a82d296a..30e2baa0 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -98,6 +98,8 @@ } ); }, + parseQueryString: api.utils.parseQueryString, + /** * Update snapshot. * @@ -969,7 +971,7 @@ previewURLQueryParams = location.search.substr( 1 ); if ( previewURLQueryParams ) { - retval.customize_preview_url_query_vars = JSON.stringify( api.utils.parseQueryString( previewURLQueryParams ) ); + retval.customize_preview_url_query_vars = JSON.stringify( snapshot.parseQueryString( previewURLQueryParams ) ); } if ( snapshot.editControlSettings( 'title' ).get() ) { diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index 3cf121a3..2d798a39 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -954,9 +954,9 @@ public function save_customize_preview_url_query_vars( $post_id ) { return; } - $original_query_vars = (array) json_decode( wp_unslash( $_POST['customize_preview_url_query_vars'] ) ); + $original_query_vars = json_decode( wp_unslash( $_POST['customize_preview_url_query_vars'] ), true ); - if ( empty( $original_query_vars ) ) { + if ( empty( $original_query_vars ) || ! is_array( $original_query_vars ) ) { return; } diff --git a/php/class-post-type.php b/php/class-post-type.php index 46b5cb41..dc1fa879 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -308,7 +308,7 @@ public function filter_post_row_actions( $actions, $post ) { array( 'front-view' => sprintf( '%s', - esc_url( get_permalink( $post->ID ) ), + esc_url( $this->get_frontend_view_link( $post ) ), esc_html__( 'Preview', 'customize-snapshots' ) ), ), @@ -368,10 +368,9 @@ public function render_data_metabox( $post ) { esc_html__( 'Edit in Customizer', 'customize-snapshots' ) ); - $frontend_view_url = get_permalink( $post->ID ); echo sprintf( '%s', - esc_url( $frontend_view_url ), + esc_url( $this->get_frontend_view_link( $post ) ), esc_html__( 'Preview Snapshot', 'customize-snapshots' ) ); echo '

'; @@ -878,4 +877,31 @@ public function get_preview_url_query_vars( $post_id ) { return $preview_url_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_preview_url_query_vars( $post->ID ); + if ( isset( $preview_url_query_vars['url'] ) ) { + $frontend_view_url = add_query_arg( + array( + static::FRONT_UUID_PARAM_NAME => $post->post_name, + ), + $preview_url_query_vars['url'] + ); + } else { + $frontend_view_url = get_permalink( $post->ID ); + } + return $frontend_view_url; + } } From 65c042bda054a33ca9b1cb488b419b9215a1ee2b Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 09:46:46 -0800 Subject: [PATCH 11/21] Rename Preview Snapshot to Preview Changeset --- php/class-post-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/class-post-type.php b/php/class-post-type.php index dc1fa879..ace6f395 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -371,7 +371,7 @@ public function render_data_metabox( $post ) { echo sprintf( '%s', esc_url( $this->get_frontend_view_link( $post ) ), - esc_html__( 'Preview Snapshot', 'customize-snapshots' ) + esc_html__( 'Preview Changeset', 'customize-snapshots' ) ); echo '

'; } From e78ae0ae4ded60f435b02e307173aae996f2607e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 09:53:05 -0800 Subject: [PATCH 12/21] Fix passing of integer scroll --- js/customize-snapshots.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index 30e2baa0..92048a84 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -971,7 +971,11 @@ previewURLQueryParams = location.search.substr( 1 ); if ( previewURLQueryParams ) { - retval.customize_preview_url_query_vars = JSON.stringify( snapshot.parseQueryString( previewURLQueryParams ) ); + previewURLQueryParams = snapshot.parseQueryString( previewURLQueryParams ); + if ( previewURLQueryParams.scroll ) { + previewURLQueryParams.scroll = parseInt( previewURLQueryParams.scroll, 10 ); + } + retval.customize_preview_url_query_vars = JSON.stringify( previewURLQueryParams ); } if ( snapshot.editControlSettings( 'title' ).get() ) { From d3438ad6d74db3a0c34ff877ff28339704eb6223 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 11:36:41 -0800 Subject: [PATCH 13/21] Remove dependency on Customizer Browser History --- js/compat/customize-snapshots.js | 35 ++-------------------------- js/customize-snapshots.js | 40 ++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/js/compat/customize-snapshots.js b/js/compat/customize-snapshots.js index a98436e3..3abd8364 100644 --- a/js/compat/customize-snapshots.js +++ b/js/compat/customize-snapshots.js @@ -222,15 +222,11 @@ * @return {void} */ extendPreviewerQuery: function extendPreviewerQuery() { - var snapshot = this, originalQuery = api.previewer.query, previewURLQueryParams; + var snapshot = this, originalQuery = api.previewer.query; 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 ) ); - } + retval.customize_preview_url_query_vars = JSON.stringify( snapshot.getStateQueryVars() ); if ( api.state( 'snapshot-exists' ).get() ) { retval.customize_snapshot_uuid = snapshot.data.uuid; @@ -491,33 +487,6 @@ 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; } } ); diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index 92048a84..44f96714 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -98,7 +98,34 @@ } ); }, - parseQueryString: api.utils.parseQueryString, + /** + * 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. @@ -964,19 +991,12 @@ * @return {void} */ extendPreviewerQuery: function extendPreviewerQuery() { - var snapshot = this, originalQuery = api.previewer.query, previewURLQueryParams; + var snapshot = this, originalQuery = api.previewer.query; api.previewer.query = function() { var retval = originalQuery.apply( this, arguments ); - previewURLQueryParams = location.search.substr( 1 ); - if ( previewURLQueryParams ) { - previewURLQueryParams = snapshot.parseQueryString( previewURLQueryParams ); - if ( previewURLQueryParams.scroll ) { - previewURLQueryParams.scroll = parseInt( previewURLQueryParams.scroll, 10 ); - } - retval.customize_preview_url_query_vars = JSON.stringify( previewURLQueryParams ); - } + retval.customize_preview_url_query_vars = JSON.stringify( snapshot.getStateQueryVars() ); if ( snapshot.editControlSettings( 'title' ).get() ) { retval.customize_changeset_title = snapshot.editControlSettings( 'title' ).get(); From 353438930672490bd4cc2f2d76aa9cd0ea7cef0c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 11:41:25 -0800 Subject: [PATCH 14/21] Re-use get_frontend_view_link in filter for getting permalink --- php/class-post-type.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/php/class-post-type.php b/php/class-post-type.php index ace6f395..f06eab01 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -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; } @@ -308,7 +303,7 @@ public function filter_post_row_actions( $actions, $post ) { array( 'front-view' => sprintf( '%s', - esc_url( $this->get_frontend_view_link( $post ) ), + esc_url( get_permalink( $post->ID ) ), esc_html__( 'Preview', 'customize-snapshots' ) ), ), @@ -368,9 +363,10 @@ public function render_data_metabox( $post ) { esc_html__( 'Edit in Customizer', 'customize-snapshots' ) ); + $frontend_view_url = get_permalink( $post->ID ); echo sprintf( '%s', - esc_url( $this->get_frontend_view_link( $post ) ), + esc_url( $frontend_view_url ), esc_html__( 'Preview Changeset', 'customize-snapshots' ) ); echo '

'; @@ -900,7 +896,12 @@ public function get_frontend_view_link( $post ) { $preview_url_query_vars['url'] ); } else { - $frontend_view_url = get_permalink( $post->ID ); + $frontend_view_url = add_query_arg( + array( + static::FRONT_UUID_PARAM_NAME => $post->post_name, + ), + home_url( '/' ) + ); } return $frontend_view_url; } From beaf92ac7fce8b4610b4da7b0d4f5ee4ce0256f6 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 16:05:38 -0800 Subject: [PATCH 15/21] Update wp-dev-lib 85f9cf4...8b767f9: Add workaround for phpunit failures on PHP 7 https://github.com/xwp/wp-dev-lib/compare/85f9cf4...8b767f9 --- dev-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-lib b/dev-lib index 85f9cf48..8b767f91 160000 --- a/dev-lib +++ b/dev-lib @@ -1 +1 @@ -Subproject commit 85f9cf48ec5d2fbdeb3eb26fcee9fc2383f36805 +Subproject commit 8b767f91c1bbadab1545260cdd389ac39d88fd2b From f12a74af7443f7200fbcba4076dfad265a74d2b4 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 16:06:11 -0800 Subject: [PATCH 16/21] Update .travis.yml with latest from dev-lib --- .travis.yml | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3234f80e..70c258f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 From 4caa963c28b3c6178cd905fd403cacba3b4ed64f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 16:17:05 -0800 Subject: [PATCH 17/21] Update dev-lib with https://github.com/xwp/wp-dev-lib/pull/221 --- dev-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-lib b/dev-lib index 8b767f91..fc29c351 160000 --- a/dev-lib +++ b/dev-lib @@ -1 +1 @@ -Subproject commit 8b767f91c1bbadab1545260cdd389ac39d88fd2b +Subproject commit fc29c35122188d7c98d8949a722498c55b95d040 From 1aec71769e866ebdea4dee1854f63addbcbe8630 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 17:29:36 -0800 Subject: [PATCH 18/21] Add unit tests and improve method naming --- js/compat/customize-snapshots.js | 2 +- js/customize-snapshots.js | 2 +- php/class-customize-snapshot-manager.php | 37 ++------ php/class-post-type.php | 95 +++++++++++-------- .../test-class-customize-snapshot-manager.php | 53 +++++++++++ tests/php/test-class-post-type.php | 7 +- 6 files changed, 127 insertions(+), 69 deletions(-) diff --git a/js/compat/customize-snapshots.js b/js/compat/customize-snapshots.js index 3abd8364..5d178ec9 100644 --- a/js/compat/customize-snapshots.js +++ b/js/compat/customize-snapshots.js @@ -226,7 +226,7 @@ api.previewer.query = function() { var retval = originalQuery.apply( this, arguments ); - retval.customize_preview_url_query_vars = JSON.stringify( snapshot.getStateQueryVars() ); + retval.customizer_state_query_vars = JSON.stringify( snapshot.getStateQueryVars() ); if ( api.state( 'snapshot-exists' ).get() ) { retval.customize_snapshot_uuid = snapshot.data.uuid; diff --git a/js/customize-snapshots.js b/js/customize-snapshots.js index 44f96714..cd8ddf3f 100644 --- a/js/customize-snapshots.js +++ b/js/customize-snapshots.js @@ -996,7 +996,7 @@ api.previewer.query = function() { var retval = originalQuery.apply( this, arguments ); - retval.customize_preview_url_query_vars = JSON.stringify( snapshot.getStateQueryVars() ); + retval.customizer_state_query_vars = JSON.stringify( snapshot.getStateQueryVars() ); if ( snapshot.editControlSettings( 'title' ).get() ) { retval.customize_changeset_title = snapshot.editControlSettings( 'title' ).get(); diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index 2d798a39..f55917f3 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -105,7 +105,7 @@ function hooks() { 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_' . $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_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' ) ); } @@ -532,10 +532,10 @@ public function replace_customize_link( $wp_admin_bar ) { $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 ); - } + $args = array_merge( + $args, + $this->post_type->get_customizer_state_query_vars( $post->ID ) + ); } // Add customize_snapshot_uuid and preview url params to customize.php itself. @@ -949,36 +949,17 @@ public function get_customize_uuid_param() { * * @param int $post_id Post id. */ - public function save_customize_preview_url_query_vars( $post_id ) { - if ( ! isset( $_POST['customize_preview_url_query_vars'] ) ) { + 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['customize_preview_url_query_vars'] ), true ); + $original_query_vars = json_decode( wp_unslash( $_POST['customizer_state_query_vars'] ), true ); if ( empty( $original_query_vars ) || ! is_array( $original_query_vars ) ) { return; } - $stored_query_vars = array(); - - $autofocus_query_vars = array( 'autofocus[panel]', 'autofocus[section]', 'autofocus[control]' ); - foreach ( wp_array_slice_assoc( $original_query_vars, $autofocus_query_vars ) as $key => $value ) { - if ( preg_match( '/^[a-z|\[|\]|_|\-|0-9]+$/', $value ) ) { - $stored_query_vars[ $key ] = $value; - } - } - - if ( ! empty( $original_query_vars['url'] ) && wp_validate_redirect( $original_query_vars['url'] ) ) { - $stored_query_vars['url'] = esc_url_raw( $original_query_vars['url'] ); - } - - if ( isset( $original_query_vars['device'] ) && in_array( $original_query_vars['device'], array_keys( $this->customize_manager->get_previewable_devices() ), true ) ) { - $stored_query_vars['device'] = $original_query_vars['device']; - } - if ( isset( $original_query_vars['scroll'] ) && is_int( $original_query_vars['scroll'] ) ) { - $stored_query_vars['scroll'] = $original_query_vars['scroll']; - } - update_post_meta( $post_id, '_preview_url_query_vars', $stored_query_vars ); + $this->post_type->set_customizer_state_query_vars( $post_id, $original_query_vars ); } } diff --git a/php/class-post-type.php b/php/class-post-type.php index f06eab01..270f0a77 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -281,16 +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, + ) ); - $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( @@ -342,20 +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 '

'; - /* 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 '

'; } elseif ( 'publish' !== $post->post_status ) { echo '

'; - $preview_url_query_vars = $this->get_preview_url_query_vars( $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, + ) ); - 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( '%s ', @@ -689,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; } @@ -799,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 ] ) ) { @@ -859,12 +854,12 @@ public function filter_out_settings_if_removed_in_metabox( $content ) { } /** - * Get preview url query vars. + * 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_preview_url_query_vars( $post_id ) { + 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 ) ) { @@ -874,6 +869,39 @@ public function get_preview_url_query_vars( $post_id ) { 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]' ); + 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. * @@ -887,22 +915,13 @@ public function get_preview_url_query_vars( $post_id ) { */ public function get_frontend_view_link( $post ) { $post = get_post( $post ); - $preview_url_query_vars = $this->get_preview_url_query_vars( $post->ID ); - if ( isset( $preview_url_query_vars['url'] ) ) { - $frontend_view_url = add_query_arg( - array( - static::FRONT_UUID_PARAM_NAME => $post->post_name, - ), - $preview_url_query_vars['url'] - ); - } else { - $frontend_view_url = add_query_arg( - array( - static::FRONT_UUID_PARAM_NAME => $post->post_name, - ), - home_url( '/' ) - ); - } - return $frontend_view_url; + $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 + ); } } diff --git a/tests/php/test-class-customize-snapshot-manager.php b/tests/php/test-class-customize-snapshot-manager.php index 8d9c3c5f..9523c88a 100644 --- a/tests/php/test-class-customize-snapshot-manager.php +++ b/tests/php/test-class-customize-snapshot-manager.php @@ -241,6 +241,8 @@ function test_hooks() { $this->assertEquals( 100000, has_action( 'admin_bar_menu', array( $manager, 'remove_all_non_snapshot_admin_bar_links' ) ) ); $this->assertEquals( 10, has_action( 'wp_before_admin_bar_render', array( $manager, 'print_admin_bar_styles' ) ) ); $this->assertEquals( 10, has_filter( 'removable_query_args', array( $manager, 'filter_removable_query_args' ) ) ); + $this->assertEquals( 10, has_action( 'save_post_' . $manager->get_post_type(), array( $manager, 'create_initial_changeset_revision' ) ) ); + $this->assertEquals( 10, has_action( 'save_post_' . $manager->get_post_type(), array( $manager, 'save_customizer_state_query_vars' ) ) ); $this->assertEquals( 10, has_filter( 'wp_insert_post_data', array( $manager, 'prepare_snapshot_post_content_for_publish' ) ) ); } @@ -729,4 +731,55 @@ public function test_replace_customize_link() { $parsed_preview_url = wp_parse_url( $query_params['url'] ); $this->assertArrayNotHasKey( 'query', $parsed_preview_url ); } + + /** + * Test save_customizer_state_query_vars. + * + * @convers \CustomizeSnapshots\Customize_Snapshot_Manager::save_customizer_state_query_vars() + * @convers \CustomizeSnapshots\Post_Type::get_frontend_view_link() + * @convers \CustomizeSnapshots\Post_Type::get_customizer_state_query_vars() + * @convers \CustomizeSnapshots\Post_Type::set_customizer_state_query_vars() + */ + public function test_save_customizer_state_query_vars() { + $post_id = $this->manager->post_type->save( array( + 'uuid' => self::UUID, + 'data' => array( + 'blogname' => array( + 'value' => 'Hello', + ), + ), + 'status' => 'draft', + ) ); + + $original_query_vars = array( + 'scroll' => 123, + 'device' => 'mobile', + 'url' => home_url( 'about/' ), + 'autofocus[panel]' => 'widgets', + 'autofocus[section]' => 'sidebar-widgets-sidebar-1', + 'autofocus[control]' => 'widget_test[123]', + ); + + $this->assertContains( '?customize_changeset_uuid=' . self::UUID, get_permalink( $post_id ) ); + $this->assertEmpty( $this->manager->post_type->get_customizer_state_query_vars( $post_id ) ); + $this->manager->save_customizer_state_query_vars( $post_id ); + $this->assertEmpty( $this->manager->post_type->get_customizer_state_query_vars( $post_id ) ); + + $_POST['customizer_state_query_vars'] = wp_slash( wp_json_encode( $original_query_vars ) ); + $this->manager->save_customizer_state_query_vars( $post_id ); + $this->assertContains( 'about/?customize_changeset_uuid=' . self::UUID, get_permalink( $post_id ) ); + $this->assertEquals( $this->manager->post_type->get_customizer_state_query_vars( $post_id ), $original_query_vars ); + $this->assertEquals( $this->manager->post_type->get_frontend_view_link( $post_id ), get_permalink( $post_id ) ); + + $this->manager->post_type->set_customizer_state_query_vars( $post_id, array( + 'scroll' => 'bad', + 'device' => 'bad', + 'url' => 'http://bogus.example.com/', + 'autofocus[panel]' => 'badid!', + 'autofocus[section]' => '#sobad', + 'autofocus[control]' => '*horrible', + 'unrecognized' => 'yes', + ) ); + $this->assertEmpty( $this->manager->post_type->get_customizer_state_query_vars( $post_id ) ); + } } diff --git a/tests/php/test-class-post-type.php b/tests/php/test-class-post-type.php index 1186159d..f58f23f8 100644 --- a/tests/php/test-class-post-type.php +++ b/tests/php/test-class-post-type.php @@ -313,6 +313,9 @@ public function test_filter_post_row_actions() { 'data' => $data, 'status' => 'draft', ) ); + $post_type->set_customizer_state_query_vars( $post_id, array( + 'url' => home_url( 'hello-beautiful-world/' ), + ) ); $original_actions = array( 'inline hide-if-no-js' => '...', 'edit' => '', @@ -322,7 +325,9 @@ public function test_filter_post_row_actions() { $filtered_actions = apply_filters( 'post_row_actions', $original_actions, get_post( $post_id ) ); $this->assertArrayNotHasKey( 'inline hide-if-no-js', $filtered_actions ); $this->assertArrayHasKey( 'customize', $filtered_actions ); + $this->assertContains( 'hello-beautiful-world', $filtered_actions['customize'] ); $this->assertArrayHasKey( 'front-view', $filtered_actions ); + $this->assertContains( 'hello-beautiful-world', $filtered_actions['front-view'] ); wp_set_current_user( $subscriber_user_id ); $filtered_actions = apply_filters( 'post_row_actions', $original_actions, get_post( $post_id ) ); @@ -423,7 +428,7 @@ public function test_render_data_metabox() { ob_start(); $post_type->render_data_metabox( get_post( $post_id ) ); $metabox_content = ob_get_clean(); - $this->assertContains( 'snapshot was made when a different theme was active', $metabox_content ); + $this->assertContains( 'changeset was made when a different theme was active', $metabox_content ); } /** From f5b55cd172e6166720132a5495f9de712f9d40d9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 9 Mar 2017 17:34:21 -0800 Subject: [PATCH 19/21] Ensure the current URL being previewed on the frontend is always used for Customize admin bar link --- php/class-customize-snapshot-manager.php | 9 ++++----- php/class-post-type.php | 3 --- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/php/class-customize-snapshot-manager.php b/php/class-customize-snapshot-manager.php index f55917f3..4afbfd3d 100644 --- a/php/class-customize-snapshot-manager.php +++ b/php/class-customize-snapshot-manager.php @@ -517,7 +517,7 @@ 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 ), @@ -532,10 +532,9 @@ public function replace_customize_link( $wp_admin_bar ) { $post = $this->snapshot->post(); if ( $post ) { - $args = array_merge( - $args, - $this->post_type->get_customizer_state_query_vars( $post->ID ) - ); + $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. diff --git a/php/class-post-type.php b/php/class-post-type.php index 270f0a77..59af0951 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -880,18 +880,15 @@ public function get_customizer_state_query_vars( $post_id ) { */ 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]' ); 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']; } From 6d9e9a9f80fddb425221fd38917d0f7187219e31 Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Fri, 10 Mar 2017 13:59:38 +0530 Subject: [PATCH 20/21] Fix phpunit test for 4.6.1 --- tests/php/test-class-customize-snapshot-manager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/php/test-class-customize-snapshot-manager.php b/tests/php/test-class-customize-snapshot-manager.php index 9523c88a..d349e339 100644 --- a/tests/php/test-class-customize-snapshot-manager.php +++ b/tests/php/test-class-customize-snapshot-manager.php @@ -760,14 +760,14 @@ public function test_save_customizer_state_query_vars() { 'autofocus[control]' => 'widget_test[123]', ); - $this->assertContains( '?customize_changeset_uuid=' . self::UUID, get_permalink( $post_id ) ); + $this->assertContains( sprintf( '?%s=%s', $this->front_param, self::UUID ), get_permalink( $post_id ) ); $this->assertEmpty( $this->manager->post_type->get_customizer_state_query_vars( $post_id ) ); $this->manager->save_customizer_state_query_vars( $post_id ); $this->assertEmpty( $this->manager->post_type->get_customizer_state_query_vars( $post_id ) ); $_POST['customizer_state_query_vars'] = wp_slash( wp_json_encode( $original_query_vars ) ); $this->manager->save_customizer_state_query_vars( $post_id ); - $this->assertContains( 'about/?customize_changeset_uuid=' . self::UUID, get_permalink( $post_id ) ); + $this->assertContains( sprintf( 'about/?%s=%s', $this->front_param, self::UUID ), get_permalink( $post_id ) ); $this->assertEquals( $this->manager->post_type->get_customizer_state_query_vars( $post_id ), $original_query_vars ); $this->assertEquals( $this->manager->post_type->get_frontend_view_link( $post_id ), get_permalink( $post_id ) ); From 43bee5bd5d6d5494288352d076c11d12a96aa9c3 Mon Sep 17 00:00:00 2001 From: Sayed Taqui Date: Fri, 10 Mar 2017 17:18:41 +0530 Subject: [PATCH 21/21] Fix phpunit test for 4.6.1 --- php/class-post-type.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/php/class-post-type.php b/php/class-post-type.php index 59af0951..20b71d76 100644 --- a/php/class-post-type.php +++ b/php/class-post-type.php @@ -881,6 +881,9 @@ public function get_customizer_state_query_vars( $post_id ) { 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;