Açıklama Yok

customizer-utils.js 4.1KB

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