HEX
Server: LiteSpeed
System: Linux server315.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User: globfdxw (6114)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: /home/globfdxw/www/wp-content/plugins/wpforms-pdf/assets/js/modules/blocks.js
/* global wpforms_builder, WPForms, wpformsPDF */

/**
 * @param wpforms_builder.server_error
 * @param wpforms_builder.error_loading_settings
 * @param wpforms_builder.pdf.settings.file_name_duplicates_alert_title
 * @param wpforms_builder.pdf.settings.file_name_duplicates_alert_content
 * @param wpforms_builder.pdf.settings.file_name_empty_alert_title
 * @param wpforms_builder.pdf.settings.file_name_empty_alert_content
 */

/**
 * WPForms PDF: Blocks module.
 *
 * @since 1.0.0
 *
 * @param {Object} document Document object.
 * @param {Object} window   Window object.
 * @param {jQuery} $        jQuery object.
 *
 * @return {Object} Public functions and properties.
 */
export default function( document, window, $ ) { // eslint-disable-line no-unused-vars, max-lines-per-function
	/**
	 * Elements holder.
	 *
	 * @since 1.0.0
	 *
	 * @type {Object}
	 */
	const el = {};

	/**
	 * Public functions and properties.
	 *
	 * @since 1.0.0
	 */
	const app = {
		/**
		 * Start the engine.
		 *
		 * @since 1.0.0
		 */
		init() {
			app.ready();
		},

		/**
		 * Start the engine.
		 *
		 * @since 1.0.0
		 */
		ready() {
			app.setup();
			app.events();
		},

		/**
		 * Setup. Prepare some variables.
		 *
		 * @since 1.0.0
		 */
		setup() {
			// Cache DOM elements.
			el.$builder = $( '#wpforms-builder' );
			el.$emptyState = $( '.wpforms-panel-content-section-pdf .wpforms-builder-provider-connections-default' );
		},

		/**
		 * Bind events.
		 *
		 * @since 1.0.0
		 */
		events() {
			el.$builder
				.on( 'wpformsSettingsBlockAdded', app.pdfAdded )
				.on( 'wpformsSettingsBlockDeleted wpformsSettingsBlockAdded', app.toggleEmptyState )
				.on( 'wpformsSettingsBlockAdded wpformsSettingsBlockCloned', app.updateFileName )
				.on( 'click', '.wpforms-pdf .wpforms-status-button', app.changeStatus )
				.on( 'wpformsBeforeSave', app.checkFileNames );
		},

		/**
		 * The PDF block was added.
		 *
		 * @since 1.0.0
		 *
		 * @param {Event}  e      The event object.
		 * @param {jQuery} $block The block element.
		 */
		pdfAdded( e, $block ) {
			if ( $block.data( 'block-type' ) !== 'pdf' ) {
				return;
			}

			const pdfId = $block.data( 'block-id' );
			const prefix = `#wpforms-panel-field-pdfs-${ pdfId }`;

			// Set default theme.
			$( `${ prefix }-theme` ).val( wpformsPDF.defaultTheme );

			// Set `enabled` block status.
			$( `${ prefix }-enable` ).val( '1' );

			// Set A4 default paper size.
			$( `${ prefix }-paper_size` ).val( 'A4' );
		},

		/**
		 * Toggle the empty state.
		 *
		 * @since 1.0.0
		 */
		toggleEmptyState() {
			el.$emptyState.toggleClass( 'wpforms-hidden', $( '.wpforms-pdf' ).length !== 0 );
		},

		/**
		 * Change the status of a PDF block.
		 *
		 * @since 1.0.0
		 */
		changeStatus() {
			const $pdf = $( this ).closest( '.wpforms-pdf' );
			const pdfId = $pdf.data( 'block-id' );
			const $enable = $( `#wpforms-panel-field-pdfs-${ pdfId }-enable` );
			const isActive = Number( $enable.val() ) === 1;

			$enable.val( isActive ? '0' : '1' );
		},

		/**
		 * Update the PDF file name when there are duplicate file names.
		 *
		 * This method ensures that each PDF block has a unique filename by appending
		 * the block ID to the filename when duplicates are detected.
		 *
		 * @since 1.0.0
		 *
		 * @param {Object} e      Event object.
		 * @param {Object} $block jQuery object of the current PDF block.
		 */
		updateFileName( e, $block ) {
			// Get all sibling PDF blocks.
			const $allPDFs = $block.siblings( '.wpforms-pdf' );

			// If no other PDF blocks exist, no need to update the filename.
			if ( ! $allPDFs.length ) {
				return;
			}

			// Get the input field and current filename for this block.
			const $input = $block.find( '.wpforms-pdf-file-name .wpforms-smart-tags-widget-original' );
			const fileName = $input.val();
			const fileNames = [];

			// Collect all filenames from sibling PDF blocks.
			$allPDFs.find( '.wpforms-pdf-file-name .wpforms-smart-tags-widget-original' ).each( function() {
				fileNames.push( $( this ).val() );
			} );

			// If the current filename is unique (not in the collected list), no need to update.
			if ( ! fileNames.includes( fileName ) ) {
				return;
			}

			// Get the unique block ID to use in the new filename.
			const pdfId = $block.data( 'block-id' );

			// Remove any existing block ID pattern from the filename.
			// Pattern matches: space + hyphen + space + 1-6 digits + .pdf + optional whitespace at the end.
			let newFileName = fileName.replace( /(\s-\s\d{1,6})*\.pdf\s*$/g, '' );

			// Append the block ID to create a unique filename.
			newFileName += ` - ${ pdfId }.pdf`;

			// Get the widget display element.
			const $widget = $block.find( '.wpforms-pdf-file-name .wpforms-smart-tags-widget-input' );

			// Update both the visible text and the input value with the new filename.
			$widget.text( newFileName );
			$input.val( newFileName );

			// Re-render the smart tags widget to ensure proper display.
			WPForms.Admin.Builder
				.SmartTags.renderWidgetContent( { target: $widget[ 0 ] }, true );
		},

		/**
		 * Check whether the File Names are unique.
		 *
		 * @since 1.0.0
		 *
		 * @param {Object} e Event object.
		 */
		checkFileNames( e ) {
			const $fileNames = $( '.wpforms-pdf-file-name input.wpforms-smart-tags-widget-original' );

			if ( ! $fileNames.length ) {
				return;
			}

			const fileNamesArray = [];
			const emptyFileNamesPdfs = [];

			$fileNames.each( function() {
				const $fileName = $( this );
				const fileName = $fileName.val();

				// Collect all file names.
				fileNamesArray.push( fileName );

				if ( fileName.length ) {
					return;
				}

				// Collect empty file names.
				emptyFileNamesPdfs.push(
					$fileName.closest( '.wpforms-pdf' ).find( '.wpforms-builder-settings-block-name' ).text()
				);
			} );

			const hasDuplicates = fileNamesArray.length !== new Set( fileNamesArray ).size;
			const hasEmptyFileNames = emptyFileNamesPdfs.length;

			if ( hasEmptyFileNames ) {
				e.preventDefault();
				app.displayFileNameEmptyAlert();
				return;
			}

			if ( hasDuplicates ) {
				e.preventDefault();
				app.displayFileNameDuplicatesAlert();
			}
		},

		/**
		 * Display an alert for duplicated FIle Names.
		 *
		 * @since 1.0.0
		 */
		displayFileNameDuplicatesAlert() {
			app.displayAlert(
				wpforms_builder.pdf.settings.file_name_duplicates_alert_title,
				wpforms_builder.pdf.settings.file_name_duplicates_alert_content
			);
		},

		/**
		 * Display an alert for an empty FIle Name(s).
		 *
		 * @since 1.0.0
		 */
		displayFileNameEmptyAlert() {
			app.displayAlert(
				wpforms_builder.pdf.settings.file_name_empty_alert_title,
				wpforms_builder.pdf.settings.file_name_empty_alert_content
			);
		},

		/**
		 * Display an alert with the given title and content.
		 *
		 * @since 1.0.0
		 *
		 * @param {string} title   The title of the alert.
		 * @param {string} content The content of the alert.
		 */
		displayAlert( title, content ) {
			$.alert( {
				title,
				content,
				type: 'red',
				icon: 'fa fa-exclamation-circle',
				buttons: {
					confirm: {
						text: wpforms_builder.close,
						btnClass: 'btn-confirm',
						keys: [ 'enter' ],
					},
				},
			} );
		},
	};

	// Return the public facing methods.
	return app;
}