astra-admin-menu-settings.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * Install Starter Templates
  3. *
  4. *
  5. * @since 1.2.4
  6. */
  7. (function($){
  8. AstraThemeAdmin = {
  9. init: function()
  10. {
  11. this._bind();
  12. },
  13. /**
  14. * Binds events for the Astra Theme.
  15. *
  16. * @since 1.0.0
  17. * @access private
  18. * @method _bind
  19. */
  20. _bind: function()
  21. {
  22. $( document ).on('ast-after-plugin-active', AstraThemeAdmin._disableActivcationNotice );
  23. $( document ).on('click' , '.astra-install-recommended-plugin', AstraThemeAdmin._installNow );
  24. $( document ).on('click' , '.astra-activate-recommended-plugin', AstraThemeAdmin._activatePlugin);
  25. $( document ).on('click' , '.astra-deactivate-recommended-plugin', AstraThemeAdmin._deactivatePlugin);
  26. $( document ).on('wp-plugin-install-success' , AstraThemeAdmin._activatePlugin);
  27. $( document ).on('wp-plugin-install-error' , AstraThemeAdmin._installError);
  28. $( document ).on('wp-plugin-installing' , AstraThemeAdmin._pluginInstalling);
  29. $( document ).on('click', '.ast-builder-migrate', AstraThemeAdmin._migrate );
  30. },
  31. _migrate: function( e ) {
  32. e.stopPropagation();
  33. e.preventDefault();
  34. var $this = $( this );
  35. if ( $this.hasClass( 'updating-message' ) ) {
  36. return;
  37. }
  38. $this.addClass( 'updating-message' );
  39. var data = {
  40. action: 'ast-migrate-to-builder',
  41. value: $(this).attr( 'data-value' ),
  42. nonce: astra.ajax_nonce,
  43. };
  44. $.ajax({
  45. url: astra.ajaxUrl,
  46. type: 'POST',
  47. data: data,
  48. success: function( response ) {
  49. $this.removeClass( 'updating-message' );
  50. if ( response.success ) {
  51. if ( data.value == '1' ) {
  52. // Change button classes & text.
  53. $this.text( astra.old_header_footer );
  54. $this.attr( 'data-value', '0' );
  55. } else {
  56. // Change button classes & text.
  57. $this.text( astra.migrate_to_builder );
  58. $this.attr( 'data-value', '1' );
  59. }
  60. }
  61. }
  62. })
  63. },
  64. /**
  65. * Plugin Installation Error.
  66. */
  67. _installError: function( event, response ) {
  68. var $card = jQuery( '.astra-install-recommended-plugin' );
  69. $card
  70. .removeClass( 'button-primary' )
  71. .addClass( 'disabled' )
  72. .html( wp.updates.l10n.installFailedShort );
  73. },
  74. /**
  75. * Installing Plugin
  76. */
  77. _pluginInstalling: function(event, args) {
  78. event.preventDefault();
  79. var slug = args.slug;
  80. var $card = jQuery( '.astra-install-recommended-plugin' );
  81. var activatingText = astra.recommendedPluiginActivatingText;
  82. $card.each(function( index, element ) {
  83. element = jQuery( element );
  84. if ( element.data('slug') === slug ) {
  85. element.addClass('updating-message');
  86. element.html( activatingText );
  87. }
  88. });
  89. },
  90. /**
  91. * Activate Success
  92. */
  93. _activatePlugin: function( event, response ) {
  94. event.preventDefault();
  95. var $message = jQuery(event.target);
  96. var $init = $message.data('init');
  97. var activatedSlug;
  98. if (typeof $init === 'undefined') {
  99. var $message = jQuery('.astra-install-recommended-plugin[data-slug=' + response.slug + ']');
  100. activatedSlug = response.slug;
  101. } else {
  102. activatedSlug = $init;
  103. }
  104. // Transform the 'Install' button into an 'Activate' button.
  105. var $init = $message.data('init');
  106. var activatingText = astra.recommendedPluiginActivatingText;
  107. var settingsLink = $message.data('settings-link');
  108. var settingsLinkText = astra.recommendedPluiginSettingsText;
  109. var deactivateText = astra.recommendedPluiginDeactivateText;
  110. var astraSitesLink = astra.astraSitesLink;
  111. var astraPluginRecommendedNonce = astra.astraPluginManagerNonce;
  112. $message.removeClass( 'install-now installed button-disabled updated-message' )
  113. .addClass('updating-message')
  114. .html( activatingText );
  115. // WordPress adds "Activate" button after waiting for 1000ms. So we will run our activation after that.
  116. setTimeout( function() {
  117. $.ajax({
  118. url: astra.ajaxUrl,
  119. type: 'POST',
  120. data: {
  121. 'action' : 'astra-sites-plugin-activate',
  122. 'nonce' : astraPluginRecommendedNonce,
  123. 'init' : $init,
  124. },
  125. })
  126. .done(function (result) {
  127. if( result.success ) {
  128. var output = '<a href="#" class="astra-deactivate-recommended-plugin" data-init="'+ $init +'" data-settings-link="'+ settingsLink +'" data-settings-link-text="'+ deactivateText +'" aria-label="'+ deactivateText +'">'+ deactivateText +'</a>';
  129. output += ( typeof settingsLink === 'string' && settingsLink != 'undefined' ) ? '<a href="' + settingsLink +'" aria-label="'+ settingsLinkText +'">' + settingsLinkText +' </a>' : '';
  130. output += ( typeof settingsLink === undefined && settingsLink != undefined ) ? '<a href="' + settingsLink +'" aria-label="'+ settingsLinkText +'">' + settingsLinkText +' </a>' : '';
  131. $message.removeClass( 'astra-activate-recommended-plugin astra-install-recommended-plugin button button-primary install-now activate-now updating-message' );
  132. $message.parent('.ast-addon-link-wrapper').parent('.astra-recommended-plugin').addClass('active');
  133. $message.parents('.ast-addon-link-wrapper').html( output );
  134. var starterSitesRedirectionUrl = astraSitesLink + result.data.starter_template_slug;
  135. jQuery(document).trigger( 'ast-after-plugin-active', [starterSitesRedirectionUrl, activatedSlug] );
  136. } else {
  137. $message.removeClass( 'updating-message' );
  138. }
  139. });
  140. }, 1200 );
  141. },
  142. /**
  143. * Activate Success
  144. */
  145. _deactivatePlugin: function( event, response ) {
  146. event.preventDefault();
  147. var $message = jQuery(event.target);
  148. var $init = $message.data('init');
  149. if (typeof $init === 'undefined') {
  150. var $message = jQuery('.astra-install-recommended-plugin[data-slug=' + response.slug + ']');
  151. }
  152. // Transform the 'Install' button into an 'Activate' button.
  153. var $init = $message.data('init');
  154. var deactivatingText = $message.data('deactivating-text') || astra.recommendedPluiginDeactivatingText;
  155. var settingsLink = $message.data('settings-link');
  156. var activateText = astra.recommendedPluiginActivateText;
  157. var astraPluginRecommendedNonce = astra.astraPluginManagerNonce;
  158. $message.removeClass( 'install-now installed button-disabled updated-message' )
  159. .addClass('updating-message')
  160. .html( deactivatingText );
  161. // WordPress adds "Activate" button after waiting for 1000ms. So we will run our activation after that.
  162. setTimeout( function() {
  163. $.ajax({
  164. url: astra.ajaxUrl,
  165. type: 'POST',
  166. data: {
  167. 'action' : 'astra-sites-plugin-deactivate',
  168. 'nonce' : astraPluginRecommendedNonce,
  169. 'init' : $init,
  170. },
  171. })
  172. .done(function (result) {
  173. if( result.success ) {
  174. var output = '<a href="#" class="astra-activate-recommended-plugin" data-init="'+ $init +'" data-settings-link="'+ settingsLink +'" data-settings-link-text="'+ activateText +'" aria-label="'+ activateText +'">'+ activateText +'</a>';
  175. $message.removeClass( 'astra-activate-recommended-plugin astra-install-recommended-plugin button button-primary install-now activate-now updating-message' );
  176. $message.parent('.ast-addon-link-wrapper').parent('.astra-recommended-plugin').removeClass('active');
  177. $message.parents('.ast-addon-link-wrapper').html( output );
  178. } else {
  179. $message.removeClass( 'updating-message' );
  180. }
  181. });
  182. }, 1200 );
  183. },
  184. /**
  185. * Install Now
  186. */
  187. _installNow: function(event)
  188. {
  189. event.preventDefault();
  190. var $button = jQuery( event.target ),
  191. $document = jQuery(document);
  192. if ( $button.hasClass( 'updating-message' ) || $button.hasClass( 'button-disabled' ) ) {
  193. return;
  194. }
  195. if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.ajaxLocked ) {
  196. wp.updates.requestFilesystemCredentials( event );
  197. $document.on( 'credential-modal-cancel', function() {
  198. var $message = $( '.astra-install-recommended-plugin.updating-message' );
  199. $message
  200. .addClass('astra-activate-recommended-plugin')
  201. .removeClass( 'updating-message astra-install-recommended-plugin' )
  202. .text( wp.updates.l10n.installNow );
  203. wp.a11y.speak( wp.updates.l10n.updateCancel, 'polite' );
  204. } );
  205. }
  206. wp.updates.installPlugin( {
  207. slug: $button.data( 'slug' )
  208. });
  209. },
  210. /**
  211. * After plugin active redirect and deactivate activation notice
  212. */
  213. _disableActivcationNotice: function( event, astraSitesLink, activatedSlug )
  214. {
  215. event.preventDefault();
  216. if ( activatedSlug.indexOf( 'astra-sites' ) >= 0 || activatedSlug.indexOf( 'astra-pro-sites' ) >= 0 ) {
  217. if ( 'undefined' != typeof AstraNotices ) {
  218. AstraNotices._ajax( 'astra-sites-on-active', '' );
  219. }
  220. window.location.href = astraSitesLink + '&ast-disable-activation-notice';
  221. }
  222. },
  223. };
  224. /**
  225. * Initialize AstraThemeAdmin
  226. */
  227. $(function(){
  228. AstraThemeAdmin.init();
  229. });
  230. })(jQuery);