HEX
Server: Apache/2
System: Linux server-27-254-144-72.da.direct 5.10.0-33-amd64 #1 SMP Debian 5.10.226-1 (2024-10-03) x86_64
User: pokaorgani (1114)
PHP: 8.1.33
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/pokaorgani/public_html/wp-content/plugins/so-widgets-bundle/base/inc/fields/js/posts-field.js
/* global jQuery, window.top.window.top.soWidgets, sowbForms */

( function( $ ) {

	const sowSetupPostsField = function( e ) {
		const $postsField = $( this );
		const hasCount = $postsField.find( '.sow-current-count' ).length > 0;
		const postId = parseInt( jQuery( '#post_ID' ).val() );

		if ( ! hasCount ) {
			return;
		}

		let debounceTimer;
		let currentRequest;
		let lastQuery = '';

		/**
		 * Debounced function to handle the posts count request.
		 *
		 * This function retrieves widget form values, builds a query string,
		 * prevents duplicate requests, and makes an AJAX call to get the posts count.
		 */
		const handlePostsCountRequest = function() {
			const postsValues = sowbForms.getWidgetFormValues( $postsField );
			const queryObj = postsValues.hasOwnProperty( 'posts' ) ? postsValues.posts : null;

			let query = '';
			for ( const key in queryObj ) {
				if ( query !== '' ) {
					query += '&';
				}
				query += key + '=' + queryObj[ key ];
			}

			// Prevent duplicate requests with same query.
			if ( query === lastQuery ) {
				return;
			}

			// Abort previous request if still pending.
			if ( currentRequest && currentRequest.readyState !== 4 ) {
				currentRequest.abort();
			}

			lastQuery = query;

			currentRequest = $.post(
				window.top.soWidgets.ajaxurl,
				{
					action: 'sow_get_posts_count',
					query: query,
					postId: postId,
				},
				function( data ) {
					$postsField.find( '.sow-current-count' ).text( data.posts_count );
				}
			);
		};

		$postsField.on( 'change', function() {
			clearTimeout( debounceTimer );
			debounceTimer = setTimeout( handlePostsCountRequest, 300 );
		} );
	}

	 // If the current page isn't the site editor, set up the Posts field now.
	 if (
		 window.top === window.self &&
		 (
			 typeof pagenow === 'string' &&
			 pagenow !== 'site-editor'
		 )
	 ) {
		 $( document ).on( 'sowsetupform', '.siteorigin-widget-field-type-posts', sowSetupPostsField );
	 }

	// Add support for the Site Editor.
	window.addEventListener( 'message', function( e ) {
		if ( e.data && e.data.action === 'sowbBlockFormInit' ) {
			$( '.siteorigin-widget-field-type-posts' ).each( function() {
				sowSetupPostsField.call( this );
			} );
		}
	} );

} )( jQuery );