Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
736 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
service_name: travis-ci | ||
src_dir: . | ||
coverage_clover: build/logs/clover.xml | ||
json_path: build/logs/coveralls-upload.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"preset": "wordpress", | ||
"excludeFiles": [ | ||
"**/vendor/**", | ||
"**/*.min.js", | ||
"**/customize-partial-refresh-base.js", | ||
"**/customize-partial-refresh-widgets-*.js", | ||
"**/twentythirteen.js", | ||
"**/node_modules/**" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
**/vendor/** | ||
**/*.min.js | ||
**/customize-partial-refresh-base.js | ||
**/customize-partial-refresh-widgets-*.js | ||
**/twentythirteen.js | ||
**/node_modules/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require-dev": { | ||
"satooshi/php-coveralls": "dev-master" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule dev-lib
updated
from a233c7 to 429841
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* global jQuery, _customizePartialRefreshSettings, JSON, _ */ | ||
/* exported customizePartialRefreshSettings */ | ||
var customizePartialRefreshSettings = ( function( $ ) { | ||
'use strict'; | ||
|
||
var self, api = wp.customize; | ||
|
||
self = { | ||
setting: null | ||
}; | ||
|
||
$.extend( self, _customizePartialRefreshSettings ); | ||
|
||
/** | ||
* Inject the functionality. | ||
*/ | ||
self.init = function() { | ||
api.bind( 'ready', function() { | ||
self.onChangeSetting(); | ||
} ); | ||
|
||
api.bind( 'setting-partial', function( response ) { | ||
var that = $( 'iframe' ).contents(), | ||
value = $.trim( response ); | ||
|
||
$( self.setting.selector, that ).html( value ); | ||
} ); | ||
}; | ||
|
||
/** | ||
* Change event for selective settings, requests a new value. | ||
*/ | ||
self.onChangeSetting = function() { | ||
$.each( self.settings, function( id, selector ) { | ||
|
||
api( id, function( setting ) { | ||
|
||
setting.bind( function() { | ||
self.setting = { | ||
id: id, | ||
selector: selector | ||
}; | ||
self.request(); | ||
} ); | ||
} ); | ||
} ); | ||
}; | ||
|
||
/** | ||
* Request a new setting value. | ||
* | ||
* @return {jQuery.Deferred} | ||
*/ | ||
self.request = function() { | ||
var spinner = $( '#customize-header-actions .spinner' ), | ||
active = 'is-active', | ||
deferred = $.Deferred(), | ||
req = self.debounceRequest(); | ||
|
||
spinner.addClass( active ); | ||
|
||
req.done( function( response ) { | ||
deferred.resolve(); | ||
api.trigger( 'setting-partial', response ); | ||
spinner.removeClass( active ); | ||
} ); | ||
|
||
req.fail( function() { | ||
deferred.reject.apply( deferred, arguments ); | ||
api.previewer.refresh(); | ||
spinner.removeClass( active ); | ||
} ); | ||
|
||
return deferred; | ||
}; | ||
|
||
/** | ||
* Debounce the requests, allowing setting changes made back-to-back to be sent together. | ||
* | ||
* @return {jQuery.Deferred} | ||
*/ | ||
self.debounceRequest = ( function() { | ||
var request, debouncedDeferreds = []; | ||
|
||
request = _.debounce( function() { | ||
var req, dirtyCustomized = {}; | ||
|
||
api.each( function( value, key ) { | ||
if ( value._dirty ) { | ||
dirtyCustomized[ key ] = value(); | ||
} | ||
} ); | ||
|
||
req = wp.ajax.post( self.action, { | ||
nonce: self.nonce, | ||
setting_id: self.setting.id, | ||
wp_customize: 'on', | ||
customized: JSON.stringify( dirtyCustomized ) | ||
} ); | ||
|
||
req.done( function() { | ||
var deferred; | ||
while ( debouncedDeferreds.length ) { | ||
deferred = debouncedDeferreds.shift(); | ||
deferred.resolveWith( req, arguments ); | ||
} | ||
} ); | ||
|
||
req.fail( function() { | ||
var deferred; | ||
while ( debouncedDeferreds.length ) { | ||
deferred = debouncedDeferreds.shift(); | ||
deferred.rejectWith( req, arguments ); | ||
} | ||
} ); | ||
|
||
} ); | ||
|
||
return function() { | ||
var deferred = $.Deferred(); | ||
debouncedDeferreds.push( deferred ); | ||
request(); | ||
return deferred; | ||
}; | ||
}() ); | ||
|
||
self.init(); | ||
}( jQuery ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
/** | ||
* Implements partial refresh for selective settings. | ||
*/ | ||
class WP_Customize_Partial_Refresh_Settings { | ||
|
||
/** | ||
* Alias for the AJAX action. | ||
* | ||
* @type string | ||
*/ | ||
const AJAX_ACTION = 'customize_partial_refresh_settings'; | ||
|
||
/** | ||
* WP_Customize_Partial_Refresh_Plugin instance. | ||
* | ||
* @var WP_Customize_Partial_Refresh_Plugin | ||
*/ | ||
public $plugin; | ||
|
||
/** | ||
* Contructor. | ||
*/ | ||
public function __construct( WP_Customize_Partial_Refresh_Plugin $plugin ) { | ||
$this->plugin = $plugin; | ||
add_action( 'after_setup_theme', array( $this, 'init' ), 11 ); | ||
} | ||
|
||
/** | ||
* Initialize selective partial refresh. | ||
* | ||
* @action after_setup_theme | ||
*/ | ||
public function init() { | ||
if ( ! current_theme_supports( 'customize-partial-refresh', 'settings' ) ) { | ||
return; | ||
} | ||
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'refresh' ) ); | ||
add_action( 'customize_controls_print_footer_scripts', array( $this, 'enqueue_scripts' ) ); | ||
} | ||
|
||
/** | ||
* Enqueue scripts. | ||
* | ||
* @action customize_controls_enqueue_scripts | ||
*/ | ||
function enqueue_scripts() { | ||
wp_enqueue_script( $this->plugin->script_handles['settings'] ); | ||
|
||
// Script data array. | ||
$exports = array( | ||
'nonce' => wp_create_nonce( self::AJAX_ACTION ), | ||
'action' => self::AJAX_ACTION, | ||
'settings' => $this->settings(), | ||
); | ||
|
||
// Export data to JS. | ||
wp_scripts()->add_data( | ||
$this->plugin->script_handles['settings'], | ||
'data', | ||
sprintf( 'var _customizePartialRefreshSettings = %s;', wp_json_encode( $exports ) ) | ||
); | ||
} | ||
|
||
/** | ||
* An array of all the partial refresh settings. | ||
* | ||
* @return array | ||
*/ | ||
public function settings() { | ||
global $wp_customize; | ||
|
||
$settings = array(); | ||
if ( $wp_customize ) { | ||
foreach ( $wp_customize->settings() as $setting ) { | ||
if ( empty( $setting->selector ) || 'partialRefresh' !== $setting->transport ) { | ||
continue; | ||
} | ||
if ( $setting->check_capabilities() ) { | ||
$settings[ $setting->id ] = $setting->selector; | ||
} | ||
} | ||
} | ||
return $settings; | ||
} | ||
|
||
/** | ||
* Ajax request to return the settings partial value. | ||
* | ||
* @action wp_ajax_customize_partial_refresh_settings | ||
*/ | ||
public function refresh() { | ||
if ( ! check_ajax_referer( self::AJAX_ACTION, 'nonce', false ) ) { | ||
status_header( 400 ); | ||
wp_send_json_error( 'bad_nonce' ); | ||
} else if ( ! current_user_can( 'customize' ) || ! is_customize_preview() ) { | ||
status_header( 403 ); | ||
wp_send_json_error( 'customize_not_allowed' ); | ||
} else if ( ! isset( $_POST['setting_id'] ) ) { | ||
status_header( 400 ); | ||
wp_send_json_error( 'missing_setting_id' ); | ||
} else if ( ! isset( $_POST['customized'] ) ) { | ||
status_header( 400 ); | ||
wp_send_json_error( 'missing_customized' ); | ||
} | ||
|
||
global $wp_customize; | ||
$setting = $wp_customize->get_setting( $_POST['setting_id'] ); | ||
|
||
if ( ! $setting ) { | ||
wp_send_json_error( 'setting_not_found' ); | ||
} else if ( empty( $setting->selector ) ) { | ||
wp_send_json_error( 'setting_selector_not_found' ); | ||
} | ||
|
||
/** | ||
* Filter partial value for a successful AJAX request. | ||
* | ||
* @param mixed $partial The partial value. Default null. | ||
* @param WP_Customize_Setting $setting WP_Customize_Setting instance. | ||
*/ | ||
$partial = apply_filters( 'customize_partial_refresh_settings', null, $setting ); | ||
|
||
/** | ||
* Filter partial value by setting ID for a successful AJAX request. | ||
* | ||
* @param mixed $partial The partial value. Default null. | ||
* @param WP_Customize_Setting $setting WP_Customize_Setting instance. | ||
*/ | ||
$partial = apply_filters( "customize_partial_refresh_{$setting->id}", $partial, $setting ); | ||
|
||
if ( null !== $partial ) { | ||
wp_send_json_success( $partial ); | ||
} else { | ||
wp_send_json_error( 'missing_required_filter' ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dev-lib/phpcs.ruleset.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dev-lib/phpunit-plugin.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.