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/public_html/wp-content/plugins/wpforms-pdf/src/Builder/Ajax/SaveForm.php
<?php

namespace WPFormsPDF\Builder\Ajax;

/**
 * TemplateFields AJAX class.
 *
 * @since 1.0.0
 */
class SaveForm extends Base {

	/**
	 * Register hooks.
	 *
	 * @since 1.0.0
	 */
	protected function hooks(): void {

		add_filter( 'wpforms_save_form_args', [ $this, 'save_form' ], 10, 3 );
	}

	/**
	 * Process form data before saving.
	 *
	 * @since 1.0.0
	 *
	 * @param array $form Form data.
	 * @param array $data Data submitted from form builder.
	 * @param array $args Additional arguments.
	 *
	 * @noinspection PhpMissingParamTypeInspection
	 *
	 * @return array
	 */
	public function save_form( $form, array $data, array $args ): array {

		$form = (array) $form;

		// Only process during form save.
		if ( empty( $args['context'] ) || $args['context'] !== 'save_form' ) {
			return $form;
		}

		// Decode form content.
		$form_data = json_decode( stripslashes( $form['post_content'] ), true );

		if ( $form_data === null ) {
			wpforms_pdf()->helpers->log(
				[
					'message' => 'Error decoding form content.',
					'form'    => $form,
				],
				'error'
			);
		}

		// If no valid form data or no PDF settings, return the original form.
		if ( ! $form_data || empty( $form_data['settings']['pdfs'] ) || ! is_array( $form_data['settings']['pdfs'] ) ) {
			return $form;
		}

		// Process PDF configurations.
		$form_data = $this->process_pdf_configurations( $form_data, $data );

		// Update form content with processed data.
		$form['post_content'] = wpforms_encode( $form_data );

		return $form;
	}

	/**
	 * Process PDF configurations from submitted data.
	 *
	 * @since 1.0.0
	 *
	 * @param array $form_data Form data.
	 * @param array $data      Submitted data.
	 *
	 * @return array Updated form data.
	 */
	private function process_pdf_configurations( array $form_data, array $data ): array {

		foreach ( $form_data['settings']['pdfs'] as $pdf_id => $pdf_settings ) {
			$form_data['settings']['pdfs'][ $pdf_id ] = $this->sanitize_array( $pdf_settings );

			// Skip if no notifications JSON data.
			if ( empty( $data['settings']['pdfs'][ $pdf_id ]['notifications_json'] ) ) {
				continue;
			}

			$notifications = $this->process_notifications_json( $data['settings']['pdfs'][ $pdf_id ]['notifications_json'] );

			if ( $notifications !== false ) {
				$form_data['settings']['pdfs'][ $pdf_id ]['notifications'] = $notifications;
			}
		}

		return $form_data;
	}

	/**
	 * Process notifications JSON data.
	 *
	 * @since 1.0.0
	 *
	 * @param string $notifications_json Notifications JSON string.
	 *
	 * @return array|false Processed notifications or false on failure.
	 */
	private function process_notifications_json( string $notifications_json ) {

		$notifications = json_decode( wp_unslash( $notifications_json ), true );

		if ( ! is_array( $notifications ) ) {
			return false;
		}

		return array_map(
			static function ( $value ) {

				return is_numeric( $value ) ? (int) $value : $value;
			},
			$notifications
		);
	}
}