Code Duplication    Length = 65-80 lines in 2 locations

assets/js/admin.js 2 locations

@@ 739-818 (lines=80) @@
736
737
						// If we need more credentials, output the form sent back to us.
738
						if ( response.form ) {
739
							// Display the form to gather the users credentials.
740
							jQuery(el).slideDown('normal', function() {
741
								jQuery(this).after('<div class="monsterinsights-addon-error">' + response.form + '</div>');
742
								$this.next().hide();
743
							});
744
745
							// Add a disabled attribute the install button if the creds are needed.
746
							jQuery(button).attr('disabled', true);
747
748
							jQuery('#monsterinsights-addons').on('click.installCredsAddon', '#upgrade', function(e) {
749
								// Prevent the default action, let the user know we are attempting to install again and go with it.
750
								e.preventDefault();
751
								$this.next().hide();
752
								jQuery(this).html('<i class="monsterinsights-cloud-download"></i> ' + monsterinsights_admin.installing);
753
								jQuery(this).next().css({'display' : 'inline-block', 'margin-top' : '0px'});
754
755
								// Now let's make another Ajax request once the user has submitted their credentials.
756
								var hostname  = jQuery(this).parent().parent().find('#hostname').val();
757
								var username  = jQuery(this).parent().parent().find('#username').val();
758
								var password  = jQuery(this).parent().parent().find('#password').val();
759
								var proceed   = jQuery(this);
760
								var connect   = jQuery(this).parent().parent().parent().parent();
761
								var cred_opts = {
762
									url:      ajaxurl,
763
									type:     'post',
764
									async:    true,
765
									cache:    false,
766
									dataType: 'json',
767
									data: {
768
										action:   'monsterinsights_install_addon',
769
										nonce:    monsterinsights_admin.install_nonce,
770
										plugin:   plugin,
771
										hostname: hostname,
772
										username: username,
773
										password: password
774
									},
775
									success: function(response) {
776
										// If there is a WP Error instance, output it here and quit the script.
777
										if ( response.error ) {
778
											jQuery(el).slideDown('normal', function() {
779
												jQuery(button).parent().parent().after('<div class="monsterinsights-addon-error"><strong>' + response.error + '</strong></div>');
780
												jQuery(button).html('<i class="monsterinsights-cloud-download"></i> ' + monsterinsights_admin.install);
781
												$this.next().hide();
782
												jQuery('.monsterinsights-addon-error').delay(4000).slideUp();
783
											});
784
											return;
785
										}
786
787
										if ( response.form ) {
788
											$this.next().hide();
789
											jQuery('.monsterinsights-inline-error').remove();
790
											jQuery(proceed).val(monsterinsights_admin.proceed);
791
											jQuery(proceed).after('<span class="monsterinsights-inline-error">' + monsterinsights_admin.connect_error + '</span>');
792
											return;
793
										}
794
795
										// The Ajax request was successful, so let's update the output.
796
										jQuery(connect).remove();
797
										jQuery(button).show();
798
799
										if ( monsterinsights_admin.isnetwork ) {
800
											jQuery(button).text(monsterinsights_admin.networkactivate).removeClass('monsterinsights-install-addon').addClass('monsterinsights-activate-addon');
801
										} else {
802
											jQuery(button).text(monsterinsights_admin.activate).removeClass('monsterinsights-install-addon').addClass('monsterinsights-activate-addon');
803
										}
804
805
										jQuery(button).attr('rel', response.plugin);
806
										jQuery(button).removeAttr('disabled');
807
										jQuery(message).text(monsterinsights_admin.inactive);
808
										
809
										// Trick here to wrap a span around he last word of the status
810
										var heading = jQuery(message), word_array, last_word, first_part;
811
812
										word_array = heading.html().split(/\s+/); // split on spaces
813
										last_word = word_array.pop();             // pop the last word
814
										first_part = word_array.join(' ');        // rejoin the first words together
815
816
										heading.html([first_part, ' <span>', last_word, '</span>'].join(''));
817
										// Proceed with CSS changes
818
										jQuery(el).removeClass('monsterinsights-addon-not-installed').addClass('monsterinsights-addon-inactive');
819
										$this.next().hide();
820
									},
821
									error: function(xhr, textStatus ,e) {
@@ 624-688 (lines=65) @@
621
						jQuery(el).removeClass('monsterinsights-addon-inactive').addClass('monsterinsights-addon-active');
622
						$this.next().hide();
623
					},
624
					error: function(xhr, textStatus ,e) {
625
						$this.next().hide();
626
						return;
627
					}
628
				};
629
				$.ajax(opts);
630
			});
631
632
		// Deactivate Addon
633
			jQuery('#monsterinsights-addons').on('click.deactivateAddon', '.monsterinsights-deactivate-addon', function(e) {
634
				e.preventDefault();
635
				var $this = jQuery(this);
636
637
				// Remove any leftover error messages, output an icon and get the plugin basename that needs to be activated.
638
				jQuery('.monsterinsights-addon-error').remove();
639
				jQuery(this).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.deactivating);
640
				jQuery(this).next().css({'display' : 'inline-block', 'margin-top' : '0px'});
641
				var button  = jQuery(this);
642
				var plugin  = jQuery(this).attr('rel');
643
				var el      = jQuery(this).parent().parent();
644
				var message = jQuery(this).parent().parent().find('.addon-status');
645
646
				// Process the Ajax to perform the activation.
647
				var opts = {
648
					url:      ajaxurl,
649
					type:     'post',
650
					async:    true,
651
					cache:    false,
652
					dataType: 'json',
653
					data: {
654
						action: 'monsterinsights_deactivate_addon',
655
						nonce:  monsterinsights_admin.deactivate_nonce,
656
						plugin: plugin,
657
						isnetwork: monsterinsights_admin.isnetwork
658
					},
659
					success: function(response) {
660
						// If there is a WP Error instance, output it here and quit the script.
661
						if ( response && true !== response ) {
662
							jQuery(el).slideDown('normal', function() {
663
								jQuery(this).after('<div class="monsterinsights-addon-error"><strong>' + response.error + '</strong></div>');
664
								$this.next().hide();
665
								jQuery('.monsterinsights-addon-error').delay(3000).slideUp();
666
							});
667
							return;
668
						}
669
670
						// The Ajax request was successful, so let's update the output.
671
						if ( monsterinsights_admin.isnetwork ) {
672
							jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.networkactivate).removeClass('monsterinsights-deactivate-addon').addClass('monsterinsights-activate-addon');
673
						} else {
674
							jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.activate).removeClass('monsterinsights-deactivate-addon').addClass('monsterinsights-activate-addon');
675
						}
676
677
						jQuery(message).text(monsterinsights_admin.inactive);
678
679
						// Trick here to wrap a span around he last word of the status
680
						var heading = jQuery(message), word_array, last_word, first_part;
681
682
						word_array = heading.html().split(/\s+/); // split on spaces
683
						last_word = word_array.pop();             // pop the last word
684
						first_part = word_array.join(' ');        // rejoin the first words together
685
686
						heading.html([first_part, ' <span>', last_word, '</span>'].join(''));
687
						// Proceed with CSS changes
688
						jQuery(el).removeClass('monsterinsights-addon-active').addClass('monsterinsights-addon-inactive');
689
						$this.next().hide();
690
					},
691
					error: function(xhr, textStatus ,e) {