HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux WebLive 5.15.0-79-generic #86-Ubuntu SMP Mon Jul 10 16:07:21 UTC 2023 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/wpskycap/wp-content/plugins/js_composer/modules/seo/assets/js/seo-analysis-view.js
/* =========================================================
 * Copyright 2023 Wpbakery
 *
 * WPBakery Page Builder SEO Analysis panel in the navbar
 *
 * ========================================================= */
/* global Backbone, vc, _ */
if ( !window.vc ) {
	window.vc = {};
}
( function ( $ ) {
	'use strict';

	// Update the Content Analysis panel contents via Backbone View
	vc.SeoAnalysisView = Backbone.View.extend({
		$wpbContentWrapper: null,
		$navbarIcon: null,
		currentBadge: '',
		initialize: function () {
			this.$wpbContentWrapper = this.getContentWrapper();
			this.$navbarIcon = $( '.vc_seo-button' );
			vc.seo_utils.createMeasurementElement();
			vc.seo_checks.analyzeContent( this.$wpbContentWrapper );
			this.render();
			this.setIconBadge();
			this.setEvents();
		},
		render: function () {
			vc.seo_checks.analyzeContent( this.$wpbContentWrapper );
			var resultsElements = this.getNotificationsHtml();
			this.$el.html( resultsElements );
			this.setIconBadge();
		},
		setEvents: function () {
			this.debouncedRender = _.debounce( this.render, 200 );
			this.listenTo( this.model, 'formData', this.debouncedRender );
			var isFrontendEditor = 'admin_frontend_editor' === window.vc_mode;
			if ( isFrontendEditor ) {
				vc.events.on( 'afterRender', this.debouncedRender, this );
				vc.events.on( 'shortcodeView:updated', this.debouncedRender, this );
				vc.events.on( 'afterLoadShortcode', this.debouncedRender, this );
			} else {
				vc.events.on( 'shortcodes:update', this.debouncedRender, this );
				vc.events.on( 'shortcodes:add', this.debouncedRender, this );
				vc.events.on( 'undoredo:undo', this.debouncedRender, this );
				vc.events.on( 'undoredo:redo', this.debouncedRender, this );
			}
			vc.events.on( 'shortcodes:destroy', this.debouncedRender, this );
		},
		getNotificationsHtml: function () {
			var _this = this;
			var results = this.model.get( 'results' );
			var newResults = {
				success: [],
				problems: [],
				warnings: []
			};
			// Assign each item for corresponding result type
			results.forEach( function ( result ) {
				newResults[result.state].push( result );
			});
			var resultsElements = [];
			var resultsOrder = [ 'problems', 'warnings', 'success' ];
			var resultsKeys = Object.keys( newResults );
			var sortedKeys = resultsKeys.sort( function ( a, b ) {
				// Find the index of the names in the customOrder array
				var orderA = resultsOrder.indexOf( a );
				var orderB = resultsOrder.indexOf( b );

				// Compare the order values
				return orderA - orderB;
			});
			sortedKeys.forEach( function ( key ) {
				if ( newResults[key].length ) {
					var html = _this.getResultsHtml( key, newResults[key]);
					resultsElements.push( html );
				}
			});
			return resultsElements;
		},
		setIconBadge: function () {
			var state = 'success';
			var allResults = this.model.get( 'results' );
			var getIssue = function ( state ) {
				return allResults.find( function ( result ) {
					return state === result.state;
				});
			};
			if ( getIssue( 'problems' ) ) {
				state = 'problems';
			} else if ( getIssue( 'warnings' ) ) {
				state = 'warnings';
			}
			this.$navbarIcon.removeClass( this.currentBadge );
			this.currentBadge = 'vc_ui-badge--' + state;
			this.$navbarIcon.addClass( this.currentBadge );
		},
		getResultsHtml: function ( type, data ) {
			var $section = $( '<div class="vc_ui-seo-results-section"><strong>' + window.i18nLocale[type] + '</strong><ul class="vc_ui-seo-results-list"></ul></div>' );
			var $resultList = $section.find( 'ul' );
			$.each( data, function ( i, val ) {
				var $item = $( '<li class="vc_ui-seo-results-list-item vc_ui-seo-results-list-item--' + val.state + '">' + val.title + ': ' + val.description + '</li>' );
				$resultList.append( $item );
			});
			return $section;
		},
		getContentWrapper: function () {
			return 'admin_frontend_editor' === window.vc_mode ? vc.$frame.contents().find( '.wpb-content-wrapper' ) : $( '#wpbakery_content' );
		}
	});

	// Initialize SEO Content Analysis Editor
	vc.events.on( 'app.render', function () {
		// Add timeout to let content render before analyzing it
		setTimeout( function () {
			vc.seo_analysis_view = new vc.SeoAnalysisView({
				el: '#vc_ui-seo-analysis',
				model: vc.seo_storage
			});
		}, 1000 );
	});

})( window.jQuery );