| 1 |
/* global wp, gapi, FB, twttr, PaypalExpressCheckout */ |
| 2 |
|
| 3 |
/** |
| 4 |
* Utilities to work with widgets in Customizer. |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* Checks whether this Customizer supports partial widget refresh. |
| 9 |
* @returns {boolean} |
| 10 |
*/ |
| 11 |
wp.customizerHasPartialWidgetRefresh = function() { |
| 12 |
return ( |
| 13 |
'object' === typeof wp && |
| 14 |
'function' === typeof wp.customize && |
| 15 |
'object' === typeof wp.customize.selectiveRefresh && |
| 16 |
'object' === typeof wp.customize.widgetsPreview && |
| 17 |
'function' === typeof wp.customize.widgetsPreview.WidgetPartial |
| 18 |
); |
| 19 |
}; |
| 20 |
|
| 21 |
/** |
| 22 |
* Verifies that the placed widget ID contains the widget name. |
| 23 |
* @param {object} placement |
| 24 |
* @param {string} widgetName |
| 25 |
* @returns {*|boolean} |
| 26 |
*/ |
| 27 |
wp.isJetpackWidgetPlaced = function( placement, widgetName ) { |
| 28 |
return placement.partial.widgetId && 0 === placement.partial.widgetId.indexOf( widgetName ); |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
* Bind events for selective refresh in Customizer. |
| 33 |
*/ |
| 34 |
( function( $ ) { |
| 35 |
$( document ).ready( function() { |
| 36 |
if ( wp && wp.customize && wp.customizerHasPartialWidgetRefresh() ) { |
| 37 |
// Refresh widget contents when a partial is rendered. |
| 38 |
wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) { |
| 39 |
if ( placement.container ) { |
| 40 |
// Refresh Google+ |
| 41 |
if ( |
| 42 |
wp.isJetpackWidgetPlaced( placement, 'googleplus-badge' ) && |
| 43 |
'object' === typeof gapi && |
| 44 |
gapi.person && |
| 45 |
'function' === typeof gapi.person.go |
| 46 |
) { |
| 47 |
gapi.person.go( placement.container[ 0 ] ); |
| 48 |
} |
| 49 |
|
| 50 |
// Refresh Facebook XFBML |
| 51 |
else if ( |
| 52 |
wp.isJetpackWidgetPlaced( placement, 'facebook-likebox' ) && |
| 53 |
'object' === typeof FB && |
| 54 |
'object' === typeof FB.XFBML && |
| 55 |
'function' === typeof FB.XFBML.parse |
| 56 |
) { |
| 57 |
FB.XFBML.parse( placement.container[ 0 ], function() { |
| 58 |
var $fbContainer = $( placement.container[ 0 ] ).find( '.fb_iframe_widget' ), |
| 59 |
fbWidth = $fbContainer.data( 'width' ), |
| 60 |
fbHeight = $fbContainer.data( 'height' ); |
| 61 |
$fbContainer.find( 'span' ).css( { width: fbWidth, height: fbHeight } ); |
| 62 |
setTimeout( function() { |
| 63 |
$fbContainer |
| 64 |
.find( 'iframe' ) |
| 65 |
.css( { width: fbWidth, height: fbHeight, position: 'relative' } ); |
| 66 |
}, 1 ); |
| 67 |
} ); |
| 68 |
} |
| 69 |
|
| 70 |
// Refresh Twitter |
| 71 |
else if ( |
| 72 |
wp.isJetpackWidgetPlaced( placement, 'twitter_timeline' ) && |
| 73 |
'object' === typeof twttr && |
| 74 |
'object' === typeof twttr.widgets && |
| 75 |
'function' === typeof twttr.widgets.load |
| 76 |
) { |
| 77 |
twttr.widgets.load( placement.container[ 0 ] ); |
| 78 |
} else if ( wp.isJetpackWidgetPlaced( placement, 'eu_cookie_law_widget' ) ) { |
| 79 |
// Refresh EU Cookie Law |
| 80 |
if ( $( '#eu-cookie-law' ).hasClass( 'top' ) ) { |
| 81 |
$( '.widget_eu_cookie_law_widget' ).addClass( 'top' ); |
| 82 |
} else { |
| 83 |
$( '.widget_eu_cookie_law_widget' ).removeClass( 'top' ); |
| 84 |
} |
| 85 |
placement.container.fadeIn(); |
| 86 |
} else if ( wp.isJetpackWidgetPlaced( placement, 'jetpack_simple_payments_widget' ) ) { |
| 87 |
// Refresh Simple Payments Widget |
| 88 |
try { |
| 89 |
var buttonId = $( '.jetpack-simple-payments-button', placement.container ) |
| 90 |
.attr( 'id' ) |
| 91 |
.replace( '_button', '' ); |
| 92 |
PaypalExpressCheckout.renderButton( null, null, buttonId, null ); |
| 93 |
} catch ( e ) { |
| 94 |
// PaypalExpressCheckout may fail. |
| 95 |
// For the same usage, see also: |
| 96 |
// https://github.com/Automattic/jetpack/blob/6c1971e6bed7d3df793392a7a58ffe0afaeeb5fe/modules/simple-payments/simple-payments.php#L111 |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} ); |
| 101 |
|
| 102 |
// Refresh widgets when they're moved. |
| 103 |
wp.customize.selectiveRefresh.bind( 'partial-content-moved', function( placement ) { |
| 104 |
if ( placement.container ) { |
| 105 |
// Refresh Twitter timeline iframe, since it has to be re-built. |
| 106 |
if ( |
| 107 |
wp.isJetpackWidgetPlaced( placement, 'twitter_timeline' ) && |
| 108 |
placement.container.find( 'iframe.twitter-timeline:not([src]):first' ).length |
| 109 |
) { |
| 110 |
placement.partial.refresh(); |
| 111 |
} else if ( wp.isJetpackWidgetPlaced( placement, 'jetpack_simple_payments_widget' ) ) { |
| 112 |
// Refresh Simple Payments Widget |
| 113 |
placement.partial.refresh(); |
| 114 |
} |
| 115 |
} |
| 116 |
} ); |
| 117 |
} |
| 118 |
} ); |
| 119 |
} )( jQuery ); |
| 120 |
|