Astra Starter Sites - Version 1.1.0

Version Description

Download this release

Release Info

Developer Nikschavan
Plugin Icon Astra Starter Sites
Version 1.1.0
Comparing to
See all releases

Code changes from version 1.0.14 to 1.1.0

astra-sites.php CHANGED
@@ -1,9 +1,9 @@
1
  <?php
2
  /**
3
- * Plugin Name: Astra Sites - Lite
4
  * Plugin URI: http://www.wpastra.com/pro/
5
  * Description: Import free sites build with Astra theme.
6
- * Version: 1.0.14
7
  * Author: Brainstorm Force
8
  * Author URI: http://www.brainstormforce.com
9
  * Text Domain: astra-sites
@@ -19,7 +19,7 @@ if ( ! defined( 'ASTRA_SITES_NAME' ) ) {
19
  }
20
 
21
  if ( ! defined( 'ASTRA_SITES_VER' ) ) {
22
- define( 'ASTRA_SITES_VER', '1.0.14' );
23
  }
24
 
25
  if ( ! defined( 'ASTRA_SITES_FILE' ) ) {
1
  <?php
2
  /**
3
+ * Plugin Name: Astra Starter Sites
4
  * Plugin URI: http://www.wpastra.com/pro/
5
  * Description: Import free sites build with Astra theme.
6
+ * Version: 1.1.0
7
  * Author: Brainstorm Force
8
  * Author URI: http://www.brainstormforce.com
9
  * Text Domain: astra-sites
19
  }
20
 
21
  if ( ! defined( 'ASTRA_SITES_VER' ) ) {
22
+ define( 'ASTRA_SITES_VER', '1.1.0' );
23
  }
24
 
25
  if ( ! defined( 'ASTRA_SITES_FILE' ) ) {
inc/assets/css/admin.css CHANGED
@@ -90,6 +90,11 @@
90
  text-align: center;
91
  }
92
 
 
 
 
 
 
93
  .wp-full-overlay-footer .installing:before {
94
  vertical-align: text-bottom;
95
  }
@@ -236,6 +241,9 @@
236
  #astra-sites-admin {
237
  height: 100vh;
238
  }
 
 
 
239
 
240
  .astra-sites-suggestions:before {
241
  border: 5px dashed #ccc;
@@ -285,4 +293,26 @@
285
 
286
  .no-themes .description {
287
  display: block;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  }
90
  text-align: center;
91
  }
92
 
93
+ .wp-core-ui .wp-full-overlay-footer .button.button-hero,
94
+ .wp-core-ui .wp-full-overlay-footer .button-group.button-hero .button {
95
+ padding: 0 10px 1px;
96
+ }
97
+
98
  .wp-full-overlay-footer .installing:before {
99
  vertical-align: text-bottom;
100
  }
241
  #astra-sites-admin {
242
  height: 100vh;
243
  }
244
+ .install-theme-info > .notice {
245
+ margin: 5px 0 10px 0;
246
+ }
247
 
248
  .astra-sites-suggestions:before {
249
  border: 5px dashed #ccc;
293
 
294
  .no-themes .description {
295
  display: block;
296
+ }
297
+
298
+ /**
299
+ * Responsive Button UI
300
+ */
301
+ .astra-sites-preview .wp-full-overlay-footer .devices button.active:before,
302
+ .astra-sites-preview .wp-full-overlay-footer .devices button:hover:before {
303
+ color: #0073aa;
304
+ }
305
+ .astra-sites-preview .wp-full-overlay-footer .devices button:before {
306
+ color: #c1c1c1;
307
+ }
308
+ .astra-sites-preview .wp-full-overlay-footer .devices button:hover {
309
+ background-color: transparent;
310
+ }
311
+ .astra-sites-preview .wp-full-overlay-footer .devices button {
312
+ border: none;
313
+ }
314
+ .astra-sites-preview .wp-full-overlay-footer .devices button:focus,
315
+ .astra-sites-preview .wp-full-overlay-footer .devices button.active:hover {
316
+ border-bottom-color: transparent;
317
+ background-color: transparent;
318
  }
inc/assets/js/admin-page.js CHANGED
@@ -74,16 +74,92 @@ var AstraSitesAjaxQueue = (function() {
74
  };
75
 
76
  }());
 
77
  (function($){
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  AstraSitesAdmin = {
80
 
 
 
 
 
 
 
81
  init: function()
82
  {
83
  this._resetPagedCount();
84
  this._bind();
85
  },
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  /**
88
  * Binds events for the Astra Sites.
89
  *
@@ -105,6 +181,244 @@ var AstraSitesAjaxQueue = (function() {
105
  $( document ).on('wp-plugin-installing' , AstraSitesAdmin._pluginInstalling);
106
  $( document ).on('wp-plugin-install-error' , AstraSitesAdmin._installError);
107
  $( document ).on('wp-plugin-install-success' , AstraSitesAdmin._installSuccess);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  },
109
 
110
  /**
@@ -136,6 +450,59 @@ var AstraSitesAjaxQueue = (function() {
136
  .attr( 'aria-pressed', true );
137
  },
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  /**
140
  * Install Now
141
  */
@@ -164,8 +531,10 @@ var AstraSitesAjaxQueue = (function() {
164
  } );
165
  }
166
 
 
 
167
  wp.updates.installPlugin( {
168
- slug: $button.data( 'slug' )
169
  } );
170
  },
171
 
@@ -176,6 +545,8 @@ var AstraSitesAjaxQueue = (function() {
176
 
177
  event.preventDefault();
178
 
 
 
179
  var $message = jQuery( '.plugin-card-' + response.slug ).find( '.button' );
180
  var $siteOptions = jQuery( '.wp-full-overlay-header').find('.astra-site-options').val();
181
  var $enabledExtensions = jQuery( '.wp-full-overlay-header').find('.astra-enabled-extensions').val();
@@ -194,7 +565,7 @@ var AstraSitesAjaxQueue = (function() {
194
  // WordPress adds "Activate" button after waiting for 1000ms. So we will run our activation after that.
195
  setTimeout( function() {
196
 
197
- jQuery.ajax({
198
  url: astraSitesAdmin.ajaxurl,
199
  type: 'POST',
200
  data: {
@@ -240,6 +611,8 @@ var AstraSitesAjaxQueue = (function() {
240
 
241
  var $card = jQuery( '.plugin-card-' + response.slug );
242
 
 
 
243
  $card
244
  .addClass( 'button-primary' )
245
  .html( wp.updates.l10n.installNow );
@@ -254,6 +627,8 @@ var AstraSitesAjaxQueue = (function() {
254
  var $card = jQuery( '.plugin-card-' + args.slug );
255
  var $button = $card.find( '.button' );
256
 
 
 
257
  $card.addClass('updating-message');
258
  $button.addClass('already-started');
259
 
@@ -274,13 +649,15 @@ var AstraSitesAjaxQueue = (function() {
274
  return;
275
  }
276
 
 
 
277
  $button.addClass('updating-message button-primary')
278
  .html( astraSitesAdmin.strings.btnActivating );
279
 
280
  var $siteOptions = jQuery( '.wp-full-overlay-header').find('.astra-site-options').val();
281
  var $enabledExtensions = jQuery( '.wp-full-overlay-header').find('.astra-enabled-extensions').val();
282
 
283
- jQuery.ajax({
284
  url: astraSitesAdmin.ajaxurl,
285
  type: 'POST',
286
  data: {
@@ -294,6 +671,8 @@ var AstraSitesAjaxQueue = (function() {
294
 
295
  if( result.success ) {
296
 
 
 
297
  var pluginsList = astraSitesAdmin.requiredPlugins.inactive;
298
 
299
  // Reset not installed plugins list.
@@ -368,10 +747,13 @@ var AstraSitesAjaxQueue = (function() {
368
  */
369
  _activateAllPlugins: function( activate_plugins ) {
370
 
371
- // Process of cloud templates - (download, remove & fetch).
 
372
  AstraSitesAjaxQueue.run();
373
 
374
- jQuery.each( activate_plugins, function(index, single_plugin) {
 
 
375
 
376
  var $card = jQuery( '.plugin-card-' + single_plugin.slug ),
377
  $button = $card.find('.button'),
@@ -384,15 +766,17 @@ var AstraSitesAjaxQueue = (function() {
384
  url: astraSitesAdmin.ajaxurl,
385
  type: 'POST',
386
  data: {
387
- 'action' : 'astra-required-plugin-activate',
388
- 'init' : single_plugin.init,
389
- 'options' : $siteOptions,
390
  'enabledExtensions' : $enabledExtensions,
391
  },
392
  success: function( result ){
393
 
394
  if( result.success ) {
395
 
 
 
396
  var $card = jQuery( '.plugin-card-' + single_plugin.slug );
397
  var $button = $card.find( '.button' );
398
  if( ! $button.hasClass('already-started') ) {
@@ -409,6 +793,8 @@ var AstraSitesAjaxQueue = (function() {
409
 
410
  // Enable Demo Import Button
411
  AstraSitesAdmin._enable_demo_import_button();
 
 
412
  }
413
  }
414
  });
@@ -419,8 +805,10 @@ var AstraSitesAjaxQueue = (function() {
419
  * Install All Plugins.
420
  */
421
  _installAllPlugins: function( not_installed ) {
 
 
422
 
423
- jQuery.each( not_installed, function(index, single_plugin) {
424
 
425
  var $card = jQuery( '.plugin-card-' + single_plugin.slug ),
426
  $button = $card.find('.button');
@@ -476,6 +864,9 @@ var AstraSitesAjaxQueue = (function() {
476
  return;
477
  }
478
 
 
 
 
479
  $('.astra-demo-import').attr('data-import', 'disabled')
480
  .addClass('updating-message installing')
481
  .text( astraSitesAdmin.strings.importingDemo );
@@ -485,57 +876,68 @@ var AstraSitesAjaxQueue = (function() {
485
  var $theme = $this.closest('.astra-sites-preview').find('.wp-full-overlay-header');
486
 
487
  var apiURL = $theme.data('demo-api') || '';
 
 
 
 
 
 
 
488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  $.ajax({
490
- url: astraSitesAdmin.ajaxurl,
491
- type: 'POST',
492
- dataType: 'json',
493
- data: {
494
- action: 'astra-import-demo',
495
- api_url: apiURL
496
  },
497
  })
498
- .done(function ( demos ) {
499
-
500
- // Success?
501
- if( demos.success ) {
502
- jQuery('.astra-demo-import').removeClass('updating-message installing')
503
- .removeAttr('data-import')
504
- .addClass('view-site')
505
- .removeClass('astra-demo-import')
506
- .text( astraSitesAdmin.strings.viewSite )
507
- .attr('target', '_blank')
508
- .append('<i class="dashicons dashicons-external"></i>')
509
- .attr('href', astraSitesAdmin.siteURL );
 
 
 
 
 
510
 
511
  } else {
512
 
513
- var output = '<div class="astra-api-error notice notice-error notice-alt is-dismissible">';
514
- output += ' <p>'+demos.message+'</p>';
515
- output += ' <button type="button" class="notice-dismiss">';
516
- output += ' <span class="screen-reader-text">'+commonL10n.dismiss+'</span>';
517
- output += ' </button>';
518
- output += '</div>';
519
 
520
- jQuery('.install-theme-info').prepend( output );
 
 
 
521
 
522
- // !important to add trigger.
523
- // Which reinitialize the dismiss error message events.
524
- jQuery(document).trigger('wp-updates-notice-added');
525
  }
526
-
527
- })
528
- .fail(function ( demos ) {
529
- jQuery('.astra-demo-import').removeClass('updating-message installing')
530
- .removeAttr('data-import')
531
- .addClass('view-site')
532
- .removeClass('astra-demo-import')
533
- .attr('target', '_blank')
534
- .attr('href', astraSitesAdmin.strings.importFailedURL );
535
-
536
- jQuery('.wp-full-overlay-header .view-site').text( astraSitesAdmin.strings.importFailedBtnSmall ).append('<i class="dashicons dashicons-external"></i>');
537
- jQuery('.footer-import-button-wrap .view-site').text( astraSitesAdmin.strings.importFailedBtnLarge ).append('<i class="dashicons dashicons-external"></i>');
538
  });
 
539
  },
540
 
541
  /**
@@ -643,6 +1045,8 @@ var AstraSitesAjaxQueue = (function() {
643
  astraSiteOptions = anchor.find('.astra-site-options').val() || '';
644
  astraEnabledExtensions = anchor.find('.astra-enabled-extensions').val() || '';
645
 
 
 
646
  var template = wp.template('astra-site-preview');
647
 
648
  templateData = [{
@@ -670,7 +1074,7 @@ var AstraSitesAjaxQueue = (function() {
670
  var descHeight = parseInt( desc.outerHeight() );
671
  var descBtn = jQuery('.theme-details-read-more');
672
 
673
- if( jQuery.isArray( requiredPlugins ) ) {
674
 
675
  if( descHeight >= 55 ) {
676
 
@@ -703,13 +1107,28 @@ var AstraSitesAjaxQueue = (function() {
703
  // or
704
  var $pluginsFilter = jQuery( '#plugin-filter' ),
705
  data = {
706
- _ajax_nonce : astraSitesAdmin._ajax_nonce,
 
707
  required_plugins : requiredPlugins
708
  };
709
 
710
  jQuery('.required-plugins').addClass('loading').html('<span class="spinner is-active"></span>');
711
 
712
- wp.ajax.post( 'astra-required-plugins', data ).done( function( response ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
713
 
714
  // Remove loader.
715
  jQuery('.required-plugins').removeClass('loading').html('');
@@ -725,12 +1144,12 @@ var AstraSitesAjaxQueue = (function() {
725
  *
726
  * List of not installed required plugins.
727
  */
728
- if ( typeof response.notinstalled !== 'undefined' ) {
729
 
730
  // Add not have installed plugins count.
731
- remaining_plugins += parseInt( response.notinstalled.length );
732
 
733
- jQuery( response.notinstalled ).each(function( index, plugin ) {
734
 
735
  var output = '<div class="plugin-card ';
736
  output += ' plugin-card-'+plugin.slug+'"';
@@ -756,12 +1175,12 @@ var AstraSitesAjaxQueue = (function() {
756
  *
757
  * List of not inactive required plugins.
758
  */
759
- if ( typeof response.inactive !== 'undefined' ) {
760
 
761
  // Add inactive plugins count.
762
- remaining_plugins += parseInt( response.inactive.length );
763
 
764
- jQuery( response.inactive ).each(function( index, plugin ) {
765
 
766
  var output = '<div class="plugin-card ';
767
  output += ' plugin-card-'+plugin.slug+'"';
@@ -787,9 +1206,9 @@ var AstraSitesAjaxQueue = (function() {
787
  *
788
  * List of not active required plugins.
789
  */
790
- if ( typeof response.active !== 'undefined' ) {
791
 
792
- jQuery( response.active ).each(function( index, plugin ) {
793
 
794
  var output = '<div class="plugin-card ';
795
  output += ' plugin-card-'+plugin.slug+'"';
@@ -813,10 +1232,10 @@ var AstraSitesAjaxQueue = (function() {
813
  * Enable Demo Import Button
814
  * @type number
815
  */
816
- astraSitesAdmin.requiredPlugins = response;
817
  AstraSitesAdmin._enable_demo_import_button();
818
 
819
- } );
820
 
821
  } else {
822
 
@@ -831,7 +1250,9 @@ var AstraSitesAjaxQueue = (function() {
831
  /**
832
  * Enable Demo Import Button.
833
  */
834
- _enable_demo_import_button: function( type = 'free' ) {
 
 
835
 
836
  switch( type ) {
837
 
74
  };
75
 
76
  }());
77
+
78
  (function($){
79
 
80
+ var AstraSSEImport = {
81
+ complete: {
82
+ posts: 0,
83
+ media: 0,
84
+ users: 0,
85
+ comments: 0,
86
+ terms: 0,
87
+ },
88
+
89
+ updateDelta: function (type, delta) {
90
+ this.complete[ type ] += delta;
91
+
92
+ var self = this;
93
+ requestAnimationFrame(function () {
94
+ self.render();
95
+ });
96
+ },
97
+ updateProgress: function ( type, complete, total ) {
98
+ var text = complete + '/' + total;
99
+
100
+ if( 'undefined' !== type && 'undefined' !== text ) {
101
+ total = parseInt( total, 10 );
102
+ if ( 0 === total || isNaN( total ) ) {
103
+ total = 1;
104
+ }
105
+ var percent = parseInt( complete, 10 ) / total;
106
+ var progress = Math.round( percent * 100 ) + '%';
107
+ var progress_bar = percent * 100;
108
+ }
109
+ },
110
+ render: function () {
111
+ var types = Object.keys( this.complete );
112
+ var complete = 0;
113
+ var total = 0;
114
+
115
+ for (var i = types.length - 1; i >= 0; i--) {
116
+ var type = types[i];
117
+ this.updateProgress( type, this.complete[ type ], this.data.count[ type ] );
118
+
119
+ complete += this.complete[ type ];
120
+ total += this.data.count[ type ];
121
+ }
122
+
123
+ this.updateProgress( 'total', complete, total );
124
+ }
125
+ };
126
+
127
  AstraSitesAdmin = {
128
 
129
+ log_file : '',
130
+ customizer_data : '',
131
+ wxr_url : '',
132
+ options_data : '',
133
+ widgets_data : '',
134
+
135
  init: function()
136
  {
137
  this._resetPagedCount();
138
  this._bind();
139
  },
140
 
141
+ /**
142
+ * Debugging.
143
+ *
144
+ * @param {mixed} data Mixed data.
145
+ */
146
+ _log: function( data ) {
147
+
148
+ if( astraSitesAdmin.debug ) {
149
+
150
+ var date = new Date();
151
+ var time = date.toLocaleTimeString();
152
+
153
+ if (typeof data == 'object') {
154
+ console.log('%c ' + JSON.stringify( data ) + ' ' + time, 'background: #ededed; color: #444');
155
+ } else {
156
+ console.log('%c ' + data + ' ' + time, 'background: #ededed; color: #444');
157
+ }
158
+
159
+
160
+ }
161
+ },
162
+
163
  /**
164
  * Binds events for the Astra Sites.
165
  *
181
  $( document ).on('wp-plugin-installing' , AstraSitesAdmin._pluginInstalling);
182
  $( document ).on('wp-plugin-install-error' , AstraSitesAdmin._installError);
183
  $( document ).on('wp-plugin-install-success' , AstraSitesAdmin._installSuccess);
184
+
185
+ $( document ).on('astra-sites-import-set-site-data-done' , AstraSitesAdmin._importCustomizerSettings );
186
+ $( document ).on('astra-sites-import-customizer-settings-done' , AstraSitesAdmin._importPrepareXML );
187
+ $( document ).on('astra-sites-import-xml-done' , AstraSitesAdmin._importSiteOptions );
188
+ $( document ).on('astra-sites-import-options-done' , AstraSitesAdmin._importWidgets );
189
+ $( document ).on('astra-sites-import-widgets-done' , AstraSitesAdmin._importEnd );
190
+ },
191
+
192
+ /**
193
+ * 5. Import Complete.
194
+ */
195
+ _importEnd: function( event ) {
196
+
197
+ $.ajax({
198
+ url : astraSitesAdmin.ajaxurl,
199
+ type : 'POST',
200
+ data : {
201
+ action : 'astra-sites-import-end',
202
+ },
203
+ beforeSend: function() {
204
+ $('.button-hero.astra-demo-import').text( astraSitesAdmin.log.importComplete );
205
+ }
206
+ })
207
+ .fail(function( jqXHR ){
208
+ AstraSitesAdmin._importFailMessage( jqXHR.status + ' ' + jqXHR.responseText );
209
+ AstraSitesAdmin._log( jqXHR.status + ' ' + jqXHR.responseText );
210
+ })
211
+ .done(function ( data ) {
212
+
213
+ // 5. Fail - Import Complete.
214
+ if( false === data.success ) {
215
+ AstraSitesAdmin._importFailMessage( data.data );
216
+ AstraSitesAdmin._log( data.data );
217
+ } else {
218
+
219
+ // 5. Pass - Import Complete.
220
+ AstraSitesAdmin._importSuccessMessage();
221
+ AstraSitesAdmin._log( astraSitesAdmin.log.success + ' ' + astraSitesAdmin.siteURL );
222
+ }
223
+ });
224
+ },
225
+
226
+ /**
227
+ * 4. Import Widgets.
228
+ */
229
+ _importWidgets: function( event ) {
230
+
231
+ $.ajax({
232
+ url : astraSitesAdmin.ajaxurl,
233
+ type : 'POST',
234
+ data : {
235
+ action : 'astra-sites-import-widgets',
236
+ widgets_data : AstraSitesAdmin.widgets_data,
237
+ },
238
+ beforeSend: function() {
239
+ AstraSitesAdmin._log( astraSitesAdmin.log.importWidgets );
240
+ $('.button-hero.astra-demo-import').text( astraSitesAdmin.log.importingWidgets );
241
+ },
242
+ })
243
+ .fail(function( jqXHR ){
244
+ AstraSitesAdmin._importFailMessage( jqXHR.status + ' ' + jqXHR.responseText );
245
+ AstraSitesAdmin._log( jqXHR.status + ' ' + jqXHR.responseText );
246
+ })
247
+ .done(function ( widgets_data ) {
248
+
249
+ // 4. Fail - Import Widgets.
250
+ if( false === widgets_data.success ) {
251
+ AstraSitesAdmin._importFailMessage( widgets_data.data );
252
+ AstraSitesAdmin._log( widgets_data.data );
253
+
254
+ } else {
255
+
256
+ // 4. Pass - Import Widgets.
257
+ AstraSitesAdmin._log( astraSitesAdmin.log.importWidgetsSuccess );
258
+ $(document).trigger( 'astra-sites-import-widgets-done' );
259
+ }
260
+ });
261
+ },
262
+
263
+ /**
264
+ * 3. Import Site Options.
265
+ */
266
+ _importSiteOptions: function( event ) {
267
+
268
+ $.ajax({
269
+ url : astraSitesAdmin.ajaxurl,
270
+ type : 'POST',
271
+ data : {
272
+ action : 'astra-sites-import-options',
273
+ options_data : AstraSitesAdmin.options_data,
274
+ },
275
+ beforeSend: function() {
276
+ AstraSitesAdmin._log( astraSitesAdmin.log.importOptions );
277
+ $('.button-hero.astra-demo-import').text( astraSitesAdmin.log.importingOptions );
278
+ },
279
+ })
280
+ .fail(function( jqXHR ){
281
+ AstraSitesAdmin._importFailMessage( jqXHR.status + ' ' + jqXHR.responseText );
282
+ AstraSitesAdmin._log( jqXHR.status + ' ' + jqXHR.responseText );
283
+ })
284
+ .done(function ( options_data ) {
285
+
286
+ // 3. Fail - Import Site Options.
287
+ if( false === options_data.success ) {
288
+ AstraSitesAdmin._log( options_data );
289
+ AstraSitesAdmin._importFailMessage( options_data.data );
290
+ AstraSitesAdmin._log( options_data.data );
291
+
292
+ } else {
293
+
294
+ // 3. Pass - Import Site Options.
295
+ AstraSitesAdmin._log( astraSitesAdmin.log.importOptionsSuccess );
296
+ $(document).trigger( 'astra-sites-import-options-done' );
297
+ }
298
+ });
299
+ },
300
+
301
+ /**
302
+ * 2. Prepare XML Data.
303
+ */
304
+ _importPrepareXML: function( event ) {
305
+
306
+ $.ajax({
307
+ url : astraSitesAdmin.ajaxurl,
308
+ type : 'POST',
309
+ data : {
310
+ action : 'astra-sites-import-prepare-xml',
311
+ wxr_url : AstraSitesAdmin.wxr_url,
312
+ },
313
+ beforeSend: function() {
314
+ AstraSitesAdmin._log( astraSitesAdmin.log.importXMLPrepare );
315
+ $('.button-hero.astra-demo-import').text( astraSitesAdmin.log.importXMLPreparing );
316
+ },
317
+ })
318
+ .fail(function( jqXHR ){
319
+ AstraSitesAdmin._importFailMessage( jqXHR.status + ' ' + jqXHR.responseText );
320
+ AstraSitesAdmin._log( jqXHR.status + ' ' + jqXHR.responseText );
321
+ })
322
+ .done(function ( xml_data ) {
323
+
324
+ // 2. Fail - Prepare XML Data.
325
+ if( false === xml_data.success ) {
326
+ AstraSitesAdmin._log( xml_data );
327
+ AstraSitesAdmin._importFailMessage( xml_data.data );
328
+ AstraSitesAdmin._log( xml_data.data );
329
+
330
+ } else {
331
+
332
+ // 2. Pass - Prepare XML Data.
333
+ AstraSitesAdmin._log( astraSitesAdmin.log.importXMLPrepareSuccess );
334
+
335
+ // Import XML though Event Source.
336
+ AstraSSEImport.data = xml_data.data;
337
+ AstraSSEImport.render();
338
+
339
+ AstraSitesAdmin._log( astraSitesAdmin.log.importXML );
340
+ $('.button-hero.astra-demo-import').text( astraSitesAdmin.log.importingXML );
341
+
342
+ var evtSource = new EventSource( AstraSSEImport.data.url );
343
+ evtSource.onmessage = function ( message ) {
344
+ var data = JSON.parse( message.data );
345
+ switch ( data.action ) {
346
+ case 'updateDelta':
347
+ AstraSSEImport.updateDelta( data.type, data.delta );
348
+ break;
349
+
350
+ case 'complete':
351
+ evtSource.close();
352
+
353
+ // 2. Pass - Import XML though "Source Event".
354
+ AstraSitesAdmin._log( astraSitesAdmin.log.importXMLSuccess );
355
+ AstraSitesAdmin._log( '----- SSE - XML import Complete -----' );
356
+
357
+ $(document).trigger( 'astra-sites-import-xml-done' );
358
+
359
+ break;
360
+ }
361
+ };
362
+ evtSource.addEventListener( 'log', function ( message ) {
363
+ var data = JSON.parse( message.data );
364
+ AstraSitesAdmin._log( data.level + ' ' + data.message );
365
+ });
366
+ }
367
+ });
368
+ },
369
+
370
+ /**
371
+ * 1. Import Customizer Options.
372
+ */
373
+ _importCustomizerSettings: function( event ) {
374
+
375
+ $.ajax({
376
+ url : astraSitesAdmin.ajaxurl,
377
+ type : 'POST',
378
+ data : {
379
+ action : 'astra-sites-import-customizer-settings',
380
+ customizer_data : AstraSitesAdmin.customizer_data,
381
+ },
382
+ beforeSend: function() {
383
+ AstraSitesAdmin._log( astraSitesAdmin.log.importCustomizer );
384
+ $('.button-hero.astra-demo-import').text( astraSitesAdmin.log.importingCustomizer );
385
+ },
386
+ })
387
+ .fail(function( jqXHR ){
388
+ AstraSitesAdmin._importFailMessage( jqXHR.status + ' ' + jqXHR.responseText );
389
+ AstraSitesAdmin._log( jqXHR.status + ' ' + jqXHR.responseText );
390
+ })
391
+ .done(function ( customizer_data ) {
392
+
393
+ // 1. Fail - Import Customizer Options.
394
+ if( false === customizer_data.success ) {
395
+ AstraSitesAdmin._importFailMessage( customizer_data.data );
396
+ AstraSitesAdmin._log( customizer_data.data );
397
+ } else {
398
+
399
+ // 1. Pass - Import Customizer Options.
400
+ AstraSitesAdmin._log( astraSitesAdmin.log.importCustomizerSuccess );
401
+
402
+ $(document).trigger( 'astra-sites-import-customizer-settings-done' );
403
+ }
404
+ });
405
+ },
406
+
407
+ /**
408
+ * Import Success Button.
409
+ *
410
+ * @param {string} data Error message.
411
+ */
412
+ _importSuccessMessage: function() {
413
+
414
+ $('.astra-demo-import').removeClass('updating-message installing')
415
+ .removeAttr('data-import')
416
+ .addClass('view-site')
417
+ .removeClass('astra-demo-import')
418
+ .text( astraSitesAdmin.strings.viewSite )
419
+ .attr('target', '_blank')
420
+ .append('<i class="dashicons dashicons-external"></i>')
421
+ .attr('href', astraSitesAdmin.siteURL );
422
  },
423
 
424
  /**
450
  .attr( 'aria-pressed', true );
451
  },
452
 
453
+ /**
454
+ * Import Error Button.
455
+ *
456
+ * @param {string} data Error message.
457
+ */
458
+ _importFailMessage: function( message, from ) {
459
+
460
+ $('.astra-demo-import')
461
+ .addClass('go-pro button-primary')
462
+ .removeClass('updating-message installing')
463
+ .removeAttr('data-import')
464
+ .attr('target', '_blank')
465
+ .append('<i class="dashicons dashicons-external"></i>')
466
+ .removeClass('astra-demo-import');
467
+
468
+ // Add the doc link due to import log file not generated.
469
+ if( 'undefined' === from ) {
470
+
471
+ $('.wp-full-overlay-header .go-pro').text( astraSitesAdmin.strings.importFailedBtnSmall );
472
+ $('.wp-full-overlay-footer .go-pro').text( astraSitesAdmin.strings.importFailedBtnLarge );
473
+ $('.go-pro').attr('href', astraSitesAdmin.log.serverConfiguration );
474
+
475
+ // Add the import log file link.
476
+ } else {
477
+
478
+ $('.wp-full-overlay-header .go-pro').text( astraSitesAdmin.strings.importFailBtn );
479
+ $('.wp-full-overlay-footer .go-pro').text( astraSitesAdmin.strings.importFailBtnLarge )
480
+
481
+ // Add the import log file link.
482
+ if( 'undefined' !== AstraSitesAdmin.log_file_url ) {
483
+ $('.go-pro').attr('href', AstraSitesAdmin.log_file_url );
484
+ } else {
485
+ $('.go-pro').attr('href', astraSitesAdmin.log.serverConfiguration );
486
+ }
487
+ }
488
+
489
+ var output = '<div class="astra-api-error notice notice-error notice-alt is-dismissible">';
490
+ output += ' <p>'+message+'</p>';
491
+ output += ' <button type="button" class="notice-dismiss">';
492
+ output += ' <span class="screen-reader-text">'+commonL10n.dismiss+'</span>';
493
+ output += ' </button>';
494
+ output += '</div>';
495
+
496
+ // Fail Notice.
497
+ $('.install-theme-info').append( output );
498
+
499
+
500
+ // !important to add trigger.
501
+ // Which reinitialize the dismiss error message events.
502
+ $(document).trigger('wp-updates-notice-added');
503
+ },
504
+
505
+
506
  /**
507
  * Install Now
508
  */
531
  } );
532
  }
533
 
534
+ AstraSitesAdmin._log( astraSitesAdmin.log.installingPlugin + ' ' + $button.data( 'slug' ) );
535
+
536
  wp.updates.installPlugin( {
537
+ slug: $button.data( 'slug' )
538
  } );
539
  },
540
 
545
 
546
  event.preventDefault();
547
 
548
+ AstraSitesAdmin._log( astraSitesAdmin.log.installed + ' ' + response.slug );
549
+
550
  var $message = jQuery( '.plugin-card-' + response.slug ).find( '.button' );
551
  var $siteOptions = jQuery( '.wp-full-overlay-header').find('.astra-site-options').val();
552
  var $enabledExtensions = jQuery( '.wp-full-overlay-header').find('.astra-enabled-extensions').val();
565
  // WordPress adds "Activate" button after waiting for 1000ms. So we will run our activation after that.
566
  setTimeout( function() {
567
 
568
+ $.ajax({
569
  url: astraSitesAdmin.ajaxurl,
570
  type: 'POST',
571
  data: {
611
 
612
  var $card = jQuery( '.plugin-card-' + response.slug );
613
 
614
+ AstraSitesAdmin._log( astraSitesAdmin.log.installError + ' ' + response.slug );
615
+
616
  $card
617
  .addClass( 'button-primary' )
618
  .html( wp.updates.l10n.installNow );
627
  var $card = jQuery( '.plugin-card-' + args.slug );
628
  var $button = $card.find( '.button' );
629
 
630
+ AstraSitesAdmin._log( astraSitesAdmin.log.installingPlugin + ' ' + args.slug );
631
+
632
  $card.addClass('updating-message');
633
  $button.addClass('already-started');
634
 
649
  return;
650
  }
651
 
652
+ AstraSitesAdmin._log( astraSitesAdmin.log.activating + ' ' + $slug );
653
+
654
  $button.addClass('updating-message button-primary')
655
  .html( astraSitesAdmin.strings.btnActivating );
656
 
657
  var $siteOptions = jQuery( '.wp-full-overlay-header').find('.astra-site-options').val();
658
  var $enabledExtensions = jQuery( '.wp-full-overlay-header').find('.astra-enabled-extensions').val();
659
 
660
+ $.ajax({
661
  url: astraSitesAdmin.ajaxurl,
662
  type: 'POST',
663
  data: {
671
 
672
  if( result.success ) {
673
 
674
+ AstraSitesAdmin._log( astraSitesAdmin.log.activated + ' ' + $slug );
675
+
676
  var pluginsList = astraSitesAdmin.requiredPlugins.inactive;
677
 
678
  // Reset not installed plugins list.
747
  */
748
  _activateAllPlugins: function( activate_plugins ) {
749
 
750
+ // Activate ALl Plugins.
751
+ AstraSitesAjaxQueue.stop();
752
  AstraSitesAjaxQueue.run();
753
 
754
+ AstraSitesAdmin._log( astraSitesAdmin.log.bulkActivation );
755
+
756
+ $.each( activate_plugins, function(index, single_plugin) {
757
 
758
  var $card = jQuery( '.plugin-card-' + single_plugin.slug ),
759
  $button = $card.find('.button'),
766
  url: astraSitesAdmin.ajaxurl,
767
  type: 'POST',
768
  data: {
769
+ 'action' : 'astra-required-plugin-activate',
770
+ 'init' : single_plugin.init,
771
+ 'options' : $siteOptions,
772
  'enabledExtensions' : $enabledExtensions,
773
  },
774
  success: function( result ){
775
 
776
  if( result.success ) {
777
 
778
+ AstraSitesAdmin._log( astraSitesAdmin.log.activate + ' ' + single_plugin.slug );
779
+
780
  var $card = jQuery( '.plugin-card-' + single_plugin.slug );
781
  var $button = $card.find( '.button' );
782
  if( ! $button.hasClass('already-started') ) {
793
 
794
  // Enable Demo Import Button
795
  AstraSitesAdmin._enable_demo_import_button();
796
+ } else {
797
+ AstraSitesAdmin._log( astraSitesAdmin.log.activationError + ' - ' + single_plugin.slug );
798
  }
799
  }
800
  });
805
  * Install All Plugins.
806
  */
807
  _installAllPlugins: function( not_installed ) {
808
+
809
+ AstraSitesAdmin._log( astraSitesAdmin.log.bulkInstall );
810
 
811
+ $.each( not_installed, function(index, single_plugin) {
812
 
813
  var $card = jQuery( '.plugin-card-' + single_plugin.slug ),
814
  $button = $card.find('.button');
864
  return;
865
  }
866
 
867
+ // Remove all notices before import start.
868
+ $('.install-theme-info > .notice').remove();
869
+
870
  $('.astra-demo-import').attr('data-import', 'disabled')
871
  .addClass('updating-message installing')
872
  .text( astraSitesAdmin.strings.importingDemo );
876
  var $theme = $this.closest('.astra-sites-preview').find('.wp-full-overlay-header');
877
 
878
  var apiURL = $theme.data('demo-api') || '';
879
+
880
+ // Site Import by API URL.
881
+ if( apiURL ) {
882
+ AstraSitesAdmin._importSite( apiURL );
883
+ }
884
+
885
+ },
886
 
887
+ /**
888
+ * Start Import Process by API URL.
889
+ *
890
+ * @param {string} apiURL Site API URL.
891
+ */
892
+ _importSite: function( apiURL ) {
893
+
894
+ AstraSitesAdmin._log( astraSitesAdmin.log.api + ' : ' + apiURL );
895
+ AstraSitesAdmin._log( astraSitesAdmin.log.importing );
896
+
897
+ $('.button-hero.astra-demo-import').text( astraSitesAdmin.log.gettingData );
898
+
899
+ // 1. Request Site Import
900
  $.ajax({
901
+ url : astraSitesAdmin.ajaxurl,
902
+ type : 'POST',
903
+ data : {
904
+ 'action' : 'astra-sites-import-set-site-data',
905
+ 'api_url' : apiURL,
 
906
  },
907
  })
908
+ .fail(function( jqXHR ){
909
+ AstraSitesAdmin._importFailMessage( jqXHR.status + ' ' + jqXHR.responseText );
910
+ AstraSitesAdmin._log( jqXHR.status + ' ' + jqXHR.responseText );
911
+ })
912
+ .done(function ( demo_data ) {
913
+
914
+ /**
915
+ * Set log file URL
916
+ */
917
+ if( astraSitesAdmin.debug && ( undefined !== demo_data.data.log_file.abs_url || null !== demo_data.data.log_file.abs_url ) ) {
918
+ AstraSitesAdmin.log_file_url = decodeURIComponent( demo_data.data.log_file.url ) || '';
919
+ }
920
+
921
+ // 1. Fail - Request Site Import
922
+ if( false === demo_data.success ) {
923
+
924
+ AstraSitesAdmin._importFailMessage( demo_data.data );
925
 
926
  } else {
927
 
928
+ // 1. Pass - Request Site Import
929
+ AstraSitesAdmin._log( astraSitesAdmin.log.processingRequest );
 
 
 
 
930
 
931
+ AstraSitesAdmin.customizer_data = JSON.stringify( demo_data.data['astra-site-customizer-data'] ) || '';
932
+ AstraSitesAdmin.wxr_url = encodeURI( demo_data.data['astra-site-wxr-path'] ) || '';
933
+ AstraSitesAdmin.options_data = JSON.stringify( demo_data.data['astra-site-options-data'] ) || '';
934
+ AstraSitesAdmin.widgets_data = JSON.stringify( demo_data.data['astra-site-widgets-data'] ) || '';
935
 
936
+ $(document).trigger( 'astra-sites-import-set-site-data-done' );
 
 
937
  }
938
+
 
 
 
 
 
 
 
 
 
 
 
939
  });
940
+
941
  },
942
 
943
  /**
1045
  astraSiteOptions = anchor.find('.astra-site-options').val() || '';
1046
  astraEnabledExtensions = anchor.find('.astra-enabled-extensions').val() || '';
1047
 
1048
+ AstraSitesAdmin._log( astraSitesAdmin.log.preview + ' "' + demo_name + '" URL : ' + demoURL );
1049
+
1050
  var template = wp.template('astra-site-preview');
1051
 
1052
  templateData = [{
1074
  var descHeight = parseInt( desc.outerHeight() );
1075
  var descBtn = jQuery('.theme-details-read-more');
1076
 
1077
+ if( $.isArray( requiredPlugins ) ) {
1078
 
1079
  if( descHeight >= 55 ) {
1080
 
1107
  // or
1108
  var $pluginsFilter = jQuery( '#plugin-filter' ),
1109
  data = {
1110
+ action : 'astra-required-plugins',
1111
+ _ajax_nonce : astraSitesAdmin._ajax_nonce,
1112
  required_plugins : requiredPlugins
1113
  };
1114
 
1115
  jQuery('.required-plugins').addClass('loading').html('<span class="spinner is-active"></span>');
1116
 
1117
+ // Required Required.
1118
+ $.ajax({
1119
+ url : astraSitesAdmin.ajaxurl,
1120
+ type : 'POST',
1121
+ data : data,
1122
+ })
1123
+ .fail(function( jqXHR ){
1124
+
1125
+ // Remove loader.
1126
+ jQuery('.required-plugins').removeClass('loading').html('');
1127
+
1128
+ AstraSitesAdmin._importFailMessage( jqXHR.status + ' ' + jqXHR.responseText, 'plugins' );
1129
+ AstraSitesAdmin._log( jqXHR.status + ' ' + jqXHR.responseText );
1130
+ })
1131
+ .done(function ( response ) {
1132
 
1133
  // Remove loader.
1134
  jQuery('.required-plugins').removeClass('loading').html('');
1144
  *
1145
  * List of not installed required plugins.
1146
  */
1147
+ if ( typeof response.data.notinstalled !== 'undefined' ) {
1148
 
1149
  // Add not have installed plugins count.
1150
+ remaining_plugins += parseInt( response.data.notinstalled.length );
1151
 
1152
+ jQuery( response.data.notinstalled ).each(function( index, plugin ) {
1153
 
1154
  var output = '<div class="plugin-card ';
1155
  output += ' plugin-card-'+plugin.slug+'"';
1175
  *
1176
  * List of not inactive required plugins.
1177
  */
1178
+ if ( typeof response.data.inactive !== 'undefined' ) {
1179
 
1180
  // Add inactive plugins count.
1181
+ remaining_plugins += parseInt( response.data.inactive.length );
1182
 
1183
+ jQuery( response.data.inactive ).each(function( index, plugin ) {
1184
 
1185
  var output = '<div class="plugin-card ';
1186
  output += ' plugin-card-'+plugin.slug+'"';
1206
  *
1207
  * List of not active required plugins.
1208
  */
1209
+ if ( typeof response.data.active !== 'undefined' ) {
1210
 
1211
+ jQuery( response.data.active ).each(function( index, plugin ) {
1212
 
1213
  var output = '<div class="plugin-card ';
1214
  output += ' plugin-card-'+plugin.slug+'"';
1232
  * Enable Demo Import Button
1233
  * @type number
1234
  */
1235
+ astraSitesAdmin.requiredPlugins = response.data;
1236
  AstraSitesAdmin._enable_demo_import_button();
1237
 
1238
+ });
1239
 
1240
  } else {
1241
 
1250
  /**
1251
  * Enable Demo Import Button.
1252
  */
1253
+ _enable_demo_import_button: function( type ) {
1254
+
1255
+ type = ( undefined !== type ) ? type : 'free';
1256
 
1257
  switch( type ) {
1258
 
inc/assets/js/eventsource.js ADDED
@@ -0,0 +1,673 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** @license
2
+ * eventsource.js
3
+ * Available under MIT License (MIT)
4
+ * https://github.com/Yaffle/EventSource/
5
+ */
6
+
7
+ /*jslint indent: 2, vars: true, plusplus: true */
8
+ /*global setTimeout, clearTimeout */
9
+
10
+ (function (global) {
11
+ "use strict";
12
+
13
+ var setTimeout = global.setTimeout;
14
+ var clearTimeout = global.clearTimeout;
15
+ var XMLHttpRequest = global.XMLHttpRequest;
16
+ var XDomainRequest = global.XDomainRequest;
17
+ var NativeEventSource = global.EventSource;
18
+ var document = global.document;
19
+
20
+ if (Object.create == null) {
21
+ Object.create = function (C) {
22
+ function F(){}
23
+ F.prototype = C;
24
+ return new F();
25
+ };
26
+ }
27
+
28
+ var k = function () {
29
+ };
30
+
31
+ function XHRWrapper(xhr) {
32
+ this.withCredentials = false;
33
+ this.responseType = "";
34
+ this.readyState = 0;
35
+ this.status = 0;
36
+ this.statusText = "";
37
+ this.responseText = "";
38
+ this.onprogress = k;
39
+ this.onreadystatechange = k;
40
+ this._contentType = "";
41
+ this._xhr = xhr;
42
+ this._sendTimeout = 0;
43
+ this._abort = k;
44
+ }
45
+
46
+ XHRWrapper.prototype.open = function (method, url) {
47
+ this._abort(true);
48
+
49
+ var that = this;
50
+ var xhr = this._xhr;
51
+ var state = 1;
52
+ var timeout = 0;
53
+
54
+ this._abort = function (silent) {
55
+ if (that._sendTimeout !== 0) {
56
+ clearTimeout(that._sendTimeout);
57
+ that._sendTimeout = 0;
58
+ }
59
+ if (state === 1 || state === 2 || state === 3) {
60
+ state = 4;
61
+ xhr.onload = k;
62
+ xhr.onerror = k;
63
+ xhr.onabort = k;
64
+ xhr.onprogress = k;
65
+ xhr.onreadystatechange = k;
66
+ // IE 8 - 9: XDomainRequest#abort() does not fire any event
67
+ // Opera < 10: XMLHttpRequest#abort() does not fire any event
68
+ xhr.abort();
69
+ if (timeout !== 0) {
70
+ clearTimeout(timeout);
71
+ timeout = 0;
72
+ }
73
+ if (!silent) {
74
+ that.readyState = 4;
75
+ that.onreadystatechange();
76
+ }
77
+ }
78
+ state = 0;
79
+ };
80
+
81
+ var onStart = function () {
82
+ if (state === 1) {
83
+ //state = 2;
84
+ var status = 0;
85
+ var statusText = "";
86
+ var contentType = undefined;
87
+ if (!("contentType" in xhr)) {
88
+ try {
89
+ status = xhr.status;
90
+ statusText = xhr.statusText;
91
+ contentType = xhr.getResponseHeader("Content-Type");
92
+ } catch (error) {
93
+ // IE < 10 throws exception for `xhr.status` when xhr.readyState === 2 || xhr.readyState === 3
94
+ // Opera < 11 throws exception for `xhr.status` when xhr.readyState === 2
95
+ // https://bugs.webkit.org/show_bug.cgi?id=29121
96
+ status = 0;
97
+ statusText = "";
98
+ contentType = undefined;
99
+ // Firefox < 14, Chrome ?, Safari ?
100
+ // https://bugs.webkit.org/show_bug.cgi?id=29658
101
+ // https://bugs.webkit.org/show_bug.cgi?id=77854
102
+ }
103
+ } else {
104
+ status = 200;
105
+ statusText = "OK";
106
+ contentType = xhr.contentType;
107
+ }
108
+ if (status !== 0) {
109
+ state = 2;
110
+ that.readyState = 2;
111
+ that.status = status;
112
+ that.statusText = statusText;
113
+ that._contentType = contentType;
114
+ that.onreadystatechange();
115
+ }
116
+ }
117
+ };
118
+ var onProgress = function () {
119
+ onStart();
120
+ if (state === 2 || state === 3) {
121
+ state = 3;
122
+ var responseText = "";
123
+ try {
124
+ responseText = xhr.responseText;
125
+ } catch (error) {
126
+ // IE 8 - 9 with XMLHttpRequest
127
+ }
128
+ that.readyState = 3;
129
+ that.responseText = responseText;
130
+ that.onprogress();
131
+ }
132
+ };
133
+ var onFinish = function () {
134
+ // Firefox 52 fires "readystatechange" (xhr.readyState === 4) without final "readystatechange" (xhr.readyState === 3)
135
+ // IE 8 fires "onload" without "onprogress"
136
+ onProgress();
137
+ if (state === 1 || state === 2 || state === 3) {
138
+ state = 4;
139
+ if (timeout !== 0) {
140
+ clearTimeout(timeout);
141
+ timeout = 0;
142
+ }
143
+ that.readyState = 4;
144
+ that.onreadystatechange();
145
+ }
146
+ };
147
+ var onReadyStateChange = function () {
148
+ if (xhr != undefined) { // Opera 12
149
+ if (xhr.readyState === 4) {
150
+ onFinish();
151
+ } else if (xhr.readyState === 3) {
152
+ onProgress();
153
+ } else if (xhr.readyState === 2) {
154
+ onStart();
155
+ }
156
+ }
157
+ };
158
+ var onTimeout = function () {
159
+ timeout = setTimeout(function () {
160
+ onTimeout();
161
+ }, 500);
162
+ if (xhr.readyState === 3) {
163
+ onProgress();
164
+ }
165
+ };
166
+
167
+ // XDomainRequest#abort removes onprogress, onerror, onload
168
+ xhr.onload = onFinish;
169
+ xhr.onerror = onFinish;
170
+ // improper fix to match Firefox behaviour, but it is better than just ignore abort
171
+ // see https://bugzilla.mozilla.org/show_bug.cgi?id=768596
172
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=880200
173
+ // https://code.google.com/p/chromium/issues/detail?id=153570
174
+ // IE 8 fires "onload" without "onprogress
175
+ xhr.onabort = onFinish;
176
+
177
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=736723
178
+ if (!("sendAsBinary" in XMLHttpRequest.prototype) && !("mozAnon" in XMLHttpRequest.prototype)) {
179
+ xhr.onprogress = onProgress;
180
+ }
181
+
182
+ // IE 8 - 9 (XMLHTTPRequest)
183
+ // Opera < 12
184
+ // Firefox < 3.5
185
+ // Firefox 3.5 - 3.6 - ? < 9.0
186
+ // onprogress is not fired sometimes or delayed
187
+ // see also #64
188
+ xhr.onreadystatechange = onReadyStateChange;
189
+
190
+ if ("contentType" in xhr) {
191
+ url += (url.indexOf("?", 0) === -1 ? "?" : "&") + "padding=true";
192
+ }
193
+ xhr.open(method, url, true);
194
+
195
+ if ("readyState" in xhr) {
196
+ // workaround for Opera 12 issue with "progress" events
197
+ // #91
198
+ timeout = setTimeout(function () {
199
+ onTimeout();
200
+ }, 0);
201
+ }
202
+ };
203
+ XHRWrapper.prototype.abort = function () {
204
+ this._abort(false);
205
+ };
206
+ XHRWrapper.prototype.getResponseHeader = function (name) {
207
+ return this._contentType;
208
+ };
209
+ XHRWrapper.prototype.setRequestHeader = function (name, value) {
210
+ var xhr = this._xhr;
211
+ if ("setRequestHeader" in xhr) {
212
+ xhr.setRequestHeader(name, value);
213
+ }
214
+ };
215
+ XHRWrapper.prototype.send = function () {
216
+ // loading indicator in Safari < ? (6), Chrome < 14, Firefox
217
+ if (!("ontimeout" in XMLHttpRequest.prototype) &&
218
+ document != undefined &&
219
+ document.readyState != undefined &&
220
+ document.readyState !== "complete") {
221
+ var that = this;
222
+ that._sendTimeout = setTimeout(function () {
223
+ that._sendTimeout = 0;
224
+ that.send();
225
+ }, 4);
226
+ return;
227
+ }
228
+
229
+ var xhr = this._xhr;
230
+ // withCredentials should be set after "open" for Safari and Chrome (< 19 ?)
231
+ xhr.withCredentials = this.withCredentials;
232
+ xhr.responseType = this.responseType;
233
+ try {
234
+ // xhr.send(); throws "Not enough arguments" in Firefox 3.0
235
+ xhr.send(undefined);
236
+ } catch (error1) {
237
+ // Safari 5.1.7, Opera 12
238
+ throw error1;
239
+ }
240
+ };
241
+
242
+ function XHRTransport(xhr) {
243
+ this._xhr = new XHRWrapper(xhr);
244
+ }
245
+
246
+ XHRTransport.prototype.open = function (onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers) {
247
+ var xhr = this._xhr;
248
+ xhr.open("GET", url);
249
+ var offset = 0;
250
+ xhr.onprogress = function () {
251
+ var responseText = xhr.responseText;
252
+ var chunk = responseText.slice(offset);
253
+ offset += chunk.length;
254
+ onProgressCallback(chunk);
255
+ };
256
+ xhr.onreadystatechange = function () {
257
+ if (xhr.readyState === 2) {
258
+ var status = xhr.status;
259
+ var statusText = xhr.statusText;
260
+ var contentType = xhr.getResponseHeader("Content-Type");
261
+ onStartCallback(status, statusText, contentType);
262
+ } else if (xhr.readyState === 4) {
263
+ onFinishCallback();
264
+ }
265
+ };
266
+ xhr.withCredentials = withCredentials;
267
+ xhr.responseType = "text";
268
+ for (var name in headers) {
269
+ if (Object.prototype.hasOwnProperty.call(headers, name)) {
270
+ xhr.setRequestHeader(name, headers[name]);
271
+ }
272
+ }
273
+ xhr.send();
274
+ };
275
+
276
+ XHRTransport.prototype.cancel = function () {
277
+ var xhr = this._xhr;
278
+ xhr.abort();
279
+ };
280
+
281
+ function EventTarget() {
282
+ this._listeners = Object.create(null);
283
+ }
284
+
285
+ function throwError(e) {
286
+ setTimeout(function () {
287
+ throw e;
288
+ }, 0);
289
+ }
290
+
291
+ EventTarget.prototype.dispatchEvent = function (event) {
292
+ event.target = this;
293
+ var typeListeners = this._listeners[event.type];
294
+ if (typeListeners != undefined) {
295
+ var length = typeListeners.length;
296
+ for (var i = 0; i < length; i += 1) {
297
+ var listener = typeListeners[i];
298
+ try {
299
+ if (typeof listener.handleEvent === "function") {
300
+ listener.handleEvent(event);
301
+ } else {
302
+ listener.call(this, event);
303
+ }
304
+ } catch (e) {
305
+ throwError(e);
306
+ }
307
+ }
308
+ }
309
+ };
310
+ EventTarget.prototype.addEventListener = function (type, listener) {
311
+ type = String(type);
312
+ var listeners = this._listeners;
313
+ var typeListeners = listeners[type];
314
+ if (typeListeners == undefined) {
315
+ typeListeners = [];
316
+ listeners[type] = typeListeners;
317
+ }
318
+ var found = false;
319
+ for (var i = 0; i < typeListeners.length; i += 1) {
320
+ if (typeListeners[i] === listener) {
321
+ found = true;
322
+ }
323
+ }
324
+ if (!found) {
325
+ typeListeners.push(listener);
326
+ }
327
+ };
328
+ EventTarget.prototype.removeEventListener = function (type, listener) {
329
+ type = String(type);
330
+ var listeners = this._listeners;
331
+ var typeListeners = listeners[type];
332
+ if (typeListeners != undefined) {
333
+ var filtered = [];
334
+ for (var i = 0; i < typeListeners.length; i += 1) {
335
+ if (typeListeners[i] !== listener) {
336
+ filtered.push(typeListeners[i]);
337
+ }
338
+ }
339
+ if (filtered.length === 0) {
340
+ delete listeners[type];
341
+ } else {
342
+ listeners[type] = filtered;
343
+ }
344
+ }
345
+ };
346
+
347
+ function Event(type) {
348
+ this.type = type;
349
+ this.target = undefined;
350
+ }
351
+
352
+ function MessageEvent(type, options) {
353
+ Event.call(this, type);
354
+ this.data = options.data;
355
+ this.lastEventId = options.lastEventId;
356
+ }
357
+
358
+ MessageEvent.prototype = Object.create(Event.prototype);
359
+
360
+ var WAITING = -1;
361
+ var CONNECTING = 0;
362
+ var OPEN = 1;
363
+ var CLOSED = 2;
364
+
365
+ var AFTER_CR = -1;
366
+ var FIELD_START = 0;
367
+ var FIELD = 1;
368
+ var VALUE_START = 2;
369
+ var VALUE = 3;
370
+
371
+ var contentTypeRegExp = /^text\/event\-stream;?(\s*charset\=utf\-8)?$/i;
372
+
373
+ var MINIMUM_DURATION = 1000;
374
+ var MAXIMUM_DURATION = 18000000;
375
+
376
+ var parseDuration = function (value, def) {
377
+ var n = parseInt(value, 10);
378
+ if (n !== n) {
379
+ n = def;
380
+ }
381
+ return clampDuration(n);
382
+ };
383
+ var clampDuration = function (n) {
384
+ return Math.min(Math.max(n, MINIMUM_DURATION), MAXIMUM_DURATION);
385
+ };
386
+
387
+ var fire = function (that, f, event) {
388
+ try {
389
+ if (typeof f === "function") {
390
+ f.call(that, event);
391
+ }
392
+ } catch (e) {
393
+ throwError(e);
394
+ }
395
+ };
396
+
397
+ function EventSourcePolyfill(url, options) {
398
+ EventTarget.call(this);
399
+
400
+ this.onopen = undefined;
401
+ this.onmessage = undefined;
402
+ this.onerror = undefined;
403
+
404
+ this.url = undefined;
405
+ this.readyState = undefined;
406
+ this.withCredentials = undefined;
407
+
408
+ this._close = undefined;
409
+
410
+ start(this, url, options);
411
+ }
412
+
413
+ function start(es, url, options) {
414
+ url = String(url);
415
+ var withCredentials = options != undefined && Boolean(options.withCredentials);
416
+
417
+ var initialRetry = clampDuration(1000);
418
+ var heartbeatTimeout = clampDuration(45000);
419
+
420
+ var lastEventId = "";
421
+ var retry = initialRetry;
422
+ var wasActivity = false;
423
+ var headers = options != undefined && options.headers != undefined ? JSON.parse(JSON.stringify(options.headers)) : undefined;
424
+ var CurrentTransport = options != undefined && options.Transport != undefined ? options.Transport : (XDomainRequest != undefined ? XDomainRequest : XMLHttpRequest);
425
+ var transport = new XHRTransport(new CurrentTransport());
426
+ var timeout = 0;
427
+ var currentState = WAITING;
428
+ var dataBuffer = "";
429
+ var lastEventIdBuffer = "";
430
+ var eventTypeBuffer = "";
431
+
432
+ var textBuffer = "";
433
+ var state = FIELD_START;
434
+ var fieldStart = 0;
435
+ var valueStart = 0;
436
+
437
+ var onStart = function (status, statusText, contentType) {
438
+ if (currentState === CONNECTING) {
439
+ if (status === 200 && contentType != undefined && contentTypeRegExp.test(contentType)) {
440
+ currentState = OPEN;
441
+ wasActivity = true;
442
+ retry = initialRetry;
443
+ es.readyState = OPEN;
444
+ var event = new Event("open");
445
+ es.dispatchEvent(event);
446
+ fire(es, es.onopen, event);
447
+ } else {
448
+ var message = "";
449
+ if (status !== 200) {
450
+ if (statusText) {
451
+ statusText = statusText.replace(/\s+/g, " ");
452
+ }
453
+ message = "EventSource's response has a status " + status + " " + statusText + " that is not 200. Aborting the connection.";
454
+ } else {
455
+ message = "EventSource's response has a Content-Type specifying an unsupported type: " + (contentType == undefined ? "-" : contentType.replace(/\s+/g, " ")) + ". Aborting the connection.";
456
+ }
457
+ throwError(new Error(message));
458
+ close();
459
+ var event = new Event("error");
460
+ es.dispatchEvent(event);
461
+ fire(es, es.onerror, event);
462
+ }
463
+ }
464
+ };
465
+
466
+ var onProgress = function (textChunk) {
467
+ if (currentState === OPEN) {
468
+ var n = -1;
469
+ for (var i = 0; i < textChunk.length; i += 1) {
470
+ var c = textChunk.charCodeAt(i);
471
+ if (c === "\n".charCodeAt(0) || c === "\r".charCodeAt(0)) {
472
+ n = i;
473
+ }
474
+ }
475
+ var chunk = (n !== -1 ? textBuffer : "") + textChunk.slice(0, n + 1);
476
+ textBuffer = (n === -1 ? textBuffer : "") + textChunk.slice(n + 1);
477
+ if (chunk !== "") {
478
+ wasActivity = true;
479
+ }
480
+ for (var position = 0; position < chunk.length; position += 1) {
481
+ var c = chunk.charCodeAt(position);
482
+ if (state === AFTER_CR && c === "\n".charCodeAt(0)) {
483
+ state = FIELD_START;
484
+ } else {
485
+ if (state === AFTER_CR) {
486
+ state = FIELD_START;
487
+ }
488
+ if (c === "\r".charCodeAt(0) || c === "\n".charCodeAt(0)) {
489
+ if (state !== FIELD_START) {
490
+ if (state === FIELD) {
491
+ valueStart = position + 1;
492
+ }
493
+ var field = chunk.slice(fieldStart, valueStart - 1);
494
+ var value = chunk.slice(valueStart + (valueStart < position && chunk.charCodeAt(valueStart) === " ".charCodeAt(0) ? 1 : 0), position);
495
+ if (field === "data") {
496
+ dataBuffer += "\n";
497
+ dataBuffer += value;
498
+ } else if (field === "id") {
499
+ lastEventIdBuffer = value;
500
+ } else if (field === "event") {
501
+ eventTypeBuffer = value;
502
+ } else if (field === "retry") {
503
+ initialRetry = parseDuration(value, initialRetry);
504
+ retry = initialRetry;
505
+ } else if (field === "heartbeatTimeout") {
506
+ heartbeatTimeout = parseDuration(value, heartbeatTimeout);
507
+ if (timeout !== 0) {
508
+ clearTimeout(timeout);
509
+ timeout = setTimeout(function () {
510
+ onTimeout();
511
+ }, heartbeatTimeout);
512
+ }
513
+ }
514
+ }
515
+ if (state === FIELD_START) {
516
+ if (dataBuffer !== "") {
517
+ lastEventId = lastEventIdBuffer;
518
+ if (eventTypeBuffer === "") {
519
+ eventTypeBuffer = "message";
520
+ }
521
+ var event = new MessageEvent(eventTypeBuffer, {
522
+ data: dataBuffer.slice(1),
523
+ lastEventId: lastEventIdBuffer
524
+ });
525
+ es.dispatchEvent(event);
526
+ if (eventTypeBuffer === "message") {
527
+ fire(es, es.onmessage, event);
528
+ }
529
+ if (currentState === CLOSED) {
530
+ return;
531
+ }
532
+ }
533
+ dataBuffer = "";
534
+ eventTypeBuffer = "";
535
+ }
536
+ state = c === "\r".charCodeAt(0) ? AFTER_CR : FIELD_START;
537
+ } else {
538
+ if (state === FIELD_START) {
539
+ fieldStart = position;
540
+ state = FIELD;
541
+ }
542
+ if (state === FIELD) {
543
+ if (c === ":".charCodeAt(0)) {
544
+ valueStart = position + 1;
545
+ state = VALUE_START;
546
+ }
547
+ } else if (state === VALUE_START) {
548