# suredonation/1.0.0/inc/form-editor/assets.php

SureDonation – Donation Forms, Fundraising Campaigns &amp; Donor Management, version 1.0.0. 459 lines.

- Page: https://pluginprobe.com/plugins/suredonation/1.0.0/code/inc/form-editor/assets.php
- Raw: https://pluginprobe.com/plugins/suredonation/1.0.0/raw/inc/form-editor/assets.php
- Modified: 2026-06-15T14:19:50+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/suredonation/1.0.0/code/inc/form-editor/assets.php#L10-L20`.

```php
<?php
/**
 * Form Editor Assets.
 *
 * Handles asset registration and enqueuing for the donation form editor.
 *
 * @package SureDonation
 * @since 0.0.1
 */

namespace SureDonation\Inc\FormEditor;

use SureDonation\Inc\Helper;
use SureDonation\Inc\Payments\Payment_Helper;
use SureDonation\Inc\Traits\Get_Instance;
use SureDonation\Inc\Post_Types\Donation_Form;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Assets Class.
 *
 * @since 0.0.1
 */
class Assets {
	use Get_Instance;

	/**
	 * Constructor.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] );
		add_action( 'init', [ $this, 'register_form_settings_meta' ] );
		add_action( 'init', [ $this, 'register_email_notifications_meta' ] );
	}

	/**
	 * Enqueue editor assets for the donation form editor.
	 *
	 * @return void
	 * @since 0.0.1
	 */
	public function enqueue_editor_assets() {
		$screen = get_current_screen();

		// Only load on donation form editor.
		if ( ! $screen || Donation_Form::POST_TYPE !== $screen->post_type ) {
			return;
		}

		// Get the asset file for dependencies.
		$editor_asset_file = SUREDONATION_DIR . 'assets/build/editor/editor.asset.php';
		$editor_asset      = file_exists( $editor_asset_file )
			? include $editor_asset_file
			: [
				'dependencies' => [
					'wp-plugins',
					'wp-editor',
					'wp-components',
					'wp-data',
					'wp-element',
					'wp-i18n',
				],
				'version'      => SUREDONATION_VER,
			];

		// Editor plugin JS.
		wp_enqueue_script(
			'suredonation-form-editor',
			SUREDONATION_URL . 'assets/build/editor/editor.js',
			$editor_asset['dependencies'],
			$editor_asset['version'],
			true
		);

		// Editor styles.
		wp_enqueue_style(
			'suredonation-form-editor',
			SUREDONATION_URL . 'assets/build/editor/editor.css',
			[ 'wp-components' ],
			$editor_asset['version']
		);

		// Localized data.
		$global_currency = Payment_Helper::get_global_setting( 'currency', 'USD' );
		$global_currency = is_string( $global_currency ) ? $global_currency : 'USD';

		wp_localize_script(
			'suredonation-form-editor',
			'suredonationFormEditor',
			[
				'ajaxUrl'                   => admin_url( 'admin-ajax.php' ),
				'nonce'                     => wp_create_nonce( 'suredonation_editor_nonce' ),
				'postType'                  => Donation_Form::POST_TYPE,
				'smartTags'                 => Helper::get_smart_tags()['confirmation'],
				'emailSmartTags'            => Helper::get_smart_tags()['email_grouped'],
				'defaultEmailNotifications' => $this->get_default_email_notifications(),
				'currency'                  => $global_currency,
				'currencySymbol'            => Payment_Helper::get_currency_symbol( $global_currency ),
			]
		);

		// Set script translations.
		wp_set_script_translations( 'suredonation-form-editor', 'suredonation' );
	}

	/**
	 * Register form settings meta fields.
	 *
	 * Stores form confirmation settings as a single JSON string.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register_form_settings_meta() {
		$post_type = Donation_Form::POST_TYPE;

		// Default confirmation message HTML (receipt layout with smart tags).
		$default_message = Helper::get_default_confirmation_message();

		$default_confirmation = wp_json_encode(
			[
				'confirmation_type' => 'same page',
				'message'           => $default_message,
				'submission_action' => 'hide form',
				'custom_url'        => '',
				'page_url'          => '',
			]
		);

		// Form confirmation settings (JSON string).
		register_post_meta(
			$post_type,
			'_suredonation_form_confirmation',
			[
				'type'              => 'string',
				'description'       => __( 'Form confirmation settings.', 'suredonation' ),
				'single'            => true,
				'default'           => $default_confirmation,
				'show_in_rest'      => [
					'schema' => [
						'type'    => 'string',
						'context' => [ 'edit' ],
					],
				],
				'sanitize_callback' => [ $this, 'sanitize_confirmation_settings' ],
				'auth_callback'     => static function () {
					return current_user_can( 'manage_options' );
				},
			]
		);
	}

	/**
	 * Email notification meta key.
	 *
	 * No migration from global `suredonation_options['email_notifications']` is needed:
	 * the plugin is pre-release (v0.0.1) with no production installs carrying customized
	 * global settings. New forms are seeded with defaults via the editor JS useEffect.
	 *
	 * @since 1.0.0
	 */
	public const EMAIL_NOTIFICATIONS_META_KEY = '_suredonation_form_email_notifications';

	/**
	 * Get default email notifications for new forms.
	 *
	 * Provides the initial set of notifications (donation receipt + admin) that
	 * are seeded when a form has no email notifications configured.
	 *
	 * @return array<int, array<string, mixed>> Default notifications array.
	 * @since 1.0.0
	 */
	public function get_default_email_notifications() {
		$sig = '<p>' . esc_html__( 'We truly appreciate your support', 'suredonation' ) . '</p>'
			. '<p>— {site_title}</p>';

		// phpcs:disable Generic.Strings.UnnecessaryStringConcat.Found -- Readability.
		$defaults = [
			// --- Donor Emails ---
			[
				'id'         => 1,
				'status'     => true,
				'name'       => __( 'Donation Receipt', 'suredonation' ),
				'email_to'   => '{donor_email}',
				'subject'    => __( 'Thank you for your donation to {campaign_name}', 'suredonation' ),
				'email_body' => '<p>' . esc_html__( 'Hi {donor_name},', 'suredonation' ) . '</p>'
					. '<p>' . esc_html__( 'Thank you for supporting {campaign_name}. Your contribution means a lot.', 'suredonation' ) . '</p>'
					. '<p><strong>' . esc_html__( 'Donation Details:', 'suredonation' ) . '</strong></p>'
					. '<ul>'
					. '<li>' . esc_html__( 'Amount:', 'suredonation' ) . ' {amount}</li>'
					. '<li>' . esc_html__( 'Date:', 'suredonation' ) . ' {donation_date}</li>'
					. '<li>' . esc_html__( 'Transaction ID:', 'suredonation' ) . ' {transaction_id}</li>'
					. '</ul>'
					. $sig,
				'from_name'  => '{site_title}',
				'from_email' => '{admin_email}',
				'reply_to'   => '',
				'trigger'    => 'donation_completed',
			],
			[
				'id'         => 2,
				'status'     => true,
				'name'       => __( 'Donation Processing', 'suredonation' ),
				'email_to'   => '{donor_email}',
				'subject'    => __( 'Your donation is being processed', 'suredonation' ),
				'email_body' => '<p>' . esc_html__( 'Hi {donor_name},', 'suredonation' ) . '</p>'
					. '<p>' . esc_html__( 'Your donation to {campaign_name} is currently being processed.', 'suredonation' ) . '</p>'
					. '<ul>'
					. '<li>' . esc_html__( 'Amount:', 'suredonation' ) . ' {amount}</li>'
					. '<li>' . esc_html__( 'Date:', 'suredonation' ) . ' {donation_date}</li>'
					. '</ul>'
					. '<p>' . esc_html__( "We'll notify you once it's confirmed.", 'suredonation' ) . '</p>'
					. '<p>— {site_title}</p>',
				'from_name'  => '{site_title}',
				'from_email' => '{admin_email}',
				'reply_to'   => '',
				'trigger'    => 'donation_processing',
			],
			[
				'id'         => 3,
				'status'     => true,
				'name'       => __( 'Donation Failed', 'suredonation' ),
				'email_to'   => '{donor_email}',
				'subject'    => __( "We couldn't process your donation", 'suredonation' ),
				'email_body' => '<p>' . esc_html__( 'Hi {donor_name},', 'suredonation' ) . '</p>'
					. '<p>' . esc_html__( 'Unfortunately, your donation to {campaign_name} could not be completed.', 'suredonation' ) . '</p>'
					. '<p>' . esc_html__( 'If you need help, feel free to contact us.', 'suredonation' ) . '</p>'
					. '<p>— {site_title}</p>',
				'from_name'  => '{site_title}',
				'from_email' => '{admin_email}',
				'reply_to'   => '',
				'trigger'    => 'donation_failed',
			],
			[
				'id'         => 4,
				'status'     => true,
				'name'       => __( 'Refund Processed', 'suredonation' ),
				'email_to'   => '{donor_email}',
				'subject'    => __( 'Your donation has been refunded', 'suredonation' ),
				'email_body' => '<p>' . esc_html__( 'Hi {donor_name},', 'suredonation' ) . '</p>'
					. '<p>' . esc_html__( 'Your donation has been refunded.', 'suredonation' ) . '</p>'
					. '<ul>'
					. '<li>' . esc_html__( 'Campaign:', 'suredonation' ) . ' {campaign_name}</li>'
					. '<li>' . esc_html__( 'Amount:', 'suredonation' ) . ' {refund_amount}</li>'
					. '</ul>'
					. '<p>' . esc_html__( 'If you have any questions, feel free to reach out.', 'suredonation' ) . '</p>'
					. '<p>— {site_title}</p>',
				'from_name'  => '{site_title}',
				'from_email' => '{admin_email}',
				'reply_to'   => '',
				'trigger'    => 'refund_processed',
			],

			// --- Admin Emails ---
			[
				'id'         => 9,
				'status'     => true,
				'name'       => __( 'New Donation (Admin)', 'suredonation' ),
				'email_to'   => '{admin_email}',
				'subject'    => __( 'New donation received', 'suredonation' ),
				'email_body' => '<p>' . esc_html__( 'A new donation has been received.', 'suredonation' ) . '</p>'
					. '<ul>'
					. '<li>' . esc_html__( 'Donor:', 'suredonation' ) . ' {donor_name}</li>'
					. '<li>' . esc_html__( 'Email:', 'suredonation' ) . ' {donor_email}</li>'
					. '<li>' . esc_html__( 'Campaign:', 'suredonation' ) . ' {campaign_name}</li>'
					. '<li>' . esc_html__( 'Amount:', 'suredonation' ) . ' {amount}</li>'
					. '<li>' . esc_html__( 'Date:', 'suredonation' ) . ' {donation_date}</li>'
					. '</ul>'
					. '<p>' . esc_html__( 'View details:', 'suredonation' ) . '<br />{admin_url}</p>',
				'from_name'  => '{site_title}',
				'from_email' => '{admin_email}',
				'reply_to'   => '',
				'trigger'    => 'donation_completed',
			],
			[
				'id'         => 10,
				'status'     => true,
				'name'       => __( 'Donation Failed (Admin)', 'suredonation' ),
				'email_to'   => '{admin_email}',
				'subject'    => __( 'Donation failed', 'suredonation' ),
				'email_body' => '<p>' . esc_html__( 'A donation attempt has failed.', 'suredonation' ) . '</p>'
					. '<ul>'
					. '<li>' . esc_html__( 'Donor:', 'suredonation' ) . ' {donor_name}</li>'
					. '<li>' . esc_html__( 'Campaign:', 'suredonation' ) . ' {campaign_name}</li>'
					. '<li>' . esc_html__( 'Amount:', 'suredonation' ) . ' {amount}</li>'
					. '<li>' . esc_html__( 'Date:', 'suredonation' ) . ' {donation_date}</li>'
					. '</ul>'
					. '<p>' . esc_html__( 'Check details:', 'suredonation' ) . '<br />{admin_url}</p>',
				'from_name'  => '{site_title}',
				'from_email' => '{admin_email}',
				'reply_to'   => '',
				'trigger'    => 'donation_failed',
			],
			[
				'id'         => 11,
				'status'     => true,
				'name'       => __( 'Refund Processed (Admin)', 'suredonation' ),
				'email_to'   => '{admin_email}',
				'subject'    => __( 'Refund issued', 'suredonation' ),
				'email_body' => '<p>' . esc_html__( 'A refund has been processed.', 'suredonation' ) . '</p>'
					. '<ul>'
					. '<li>' . esc_html__( 'Donor:', 'suredonation' ) . ' {donor_name}</li>'
					. '<li>' . esc_html__( 'Campaign:', 'suredonation' ) . ' {campaign_name}</li>'
					. '<li>' . esc_html__( 'Amount:', 'suredonation' ) . ' {refund_amount}</li>'
					. '</ul>'
					. '<p>' . esc_html__( 'View details:', 'suredonation' ) . '<br />{admin_url}</p>',
				'from_name'  => '{site_title}',
				'from_email' => '{admin_email}',
				'reply_to'   => '',
				'trigger'    => 'refund_processed',
			],
		];
		// phpcs:enable Generic.Strings.UnnecessaryStringConcat.Found

		/**
		 * Filter default email notifications.
		 * Pro uses this to add subscription email notification templates.
		 *
		 * @param array<int, array<string, mixed>> $defaults Default notification configs.
		 * @since 1.0.0
		 */
		return apply_filters( 'suredonation_default_email_notifications', $defaults );
	}

	/**
	 * Register email notification meta for donation forms.
	 *
	 * Stores per-form email notification settings as a JSON string.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register_email_notifications_meta() {
		register_post_meta(
			Donation_Form::POST_TYPE,
			self::EMAIL_NOTIFICATIONS_META_KEY,
			[
				'type'              => 'string',
				'description'       => __( 'Form email notification settings.', 'suredonation' ),
				'single'            => true,
				'default'           => '',
				'show_in_rest'      => [
					'schema' => [
						'type'    => 'string',
						'context' => [ 'edit' ],
					],
				],
				'sanitize_callback' => [ $this, 'sanitize_email_notifications' ],
				'auth_callback'     => static function () {
					return current_user_can( 'manage_options' );
				},
			]
		);
	}

	/**
	 * Sanitize email notification settings.
	 *
	 * @param string $value JSON string of email notification settings.
	 * @return string Sanitized JSON string.
	 * @since 1.0.0
	 */
	public function sanitize_email_notifications( $value ) {
		if ( empty( $value ) || ! is_string( $value ) ) {
			return '';
		}

		$data = json_decode( $value, true );

		if ( ! is_array( $data ) ) {
			return '';
		}

		$sanitized = [];
		foreach ( $data as $notification ) {
			if ( ! is_array( $notification ) ) {
				continue;
			}

			$sanitized[] = [
				'id'         => isset( $notification['id'] ) ? absint( $notification['id'] ) : 0,
				'status'     => isset( $notification['status'] ) ? (bool) $notification['status'] : true,
				'name'       => isset( $notification['name'] ) ? sanitize_text_field( $notification['name'] ) : '',
				'email_to'   => isset( $notification['email_to'] ) ? sanitize_text_field( $notification['email_to'] ) : '',
				'subject'    => isset( $notification['subject'] ) ? sanitize_text_field( $notification['subject'] ) : '',
				'email_body' => isset( $notification['email_body'] ) ? wp_kses_post( $notification['email_body'] ) : '',
				'from_name'  => isset( $notification['from_name'] ) ? sanitize_text_field( $notification['from_name'] ) : '',
				'from_email' => isset( $notification['from_email'] ) ? sanitize_text_field( $notification['from_email'] ) : '',
				'reply_to'   => isset( $notification['reply_to'] ) ? sanitize_text_field( $notification['reply_to'] ) : '',
				'trigger'    => isset( $notification['trigger'] ) && in_array(
					$notification['trigger'],
					apply_filters(
						'suredonation_email_notification_triggers',
						[ 'all', 'donation_completed', 'donation_processing', 'donation_failed', 'refund_processed' ]
					),
					true
				) ? $notification['trigger'] : 'all',
			];
		}

		$encoded = wp_json_encode( $sanitized );
		return is_string( $encoded ) ? $encoded : '';
	}

	/**
	 * Sanitize form confirmation settings.
	 *
	 * @param string $value JSON string of confirmation settings.
	 * @return string Sanitized JSON string.
	 * @since 1.0.0
	 */
	public function sanitize_confirmation_settings( $value ) {
		$fallback_data = [
			'confirmation_type' => 'same page',
			'message'           => '',
			'submission_action' => 'hide form',
			'custom_url'        => '',
			'page_url'          => '',
		];
		$fallback      = (string) wp_json_encode( $fallback_data );

		if ( empty( $value ) || ! is_string( $value ) ) {
			return $fallback;
		}

		$data = json_decode( $value, true );

		if ( ! is_array( $data ) ) {
			return $fallback;
		}

		$allowed_types   = [ 'same page', 'different page', 'custom url' ];
		$allowed_actions = [ 'hide form', 'reset form' ];

		$message = isset( $data['message'] ) ? wp_kses_post( $data['message'] ) : '';

		$sanitized = [
			'confirmation_type' => isset( $data['confirmation_type'] ) && in_array( $data['confirmation_type'], $allowed_types, true )
				? $data['confirmation_type']
				: 'same page',
			'message'           => $message,
			'submission_action' => isset( $data['submission_action'] ) && in_array( $data['submission_action'], $allowed_actions, true )
				? $data['submission_action']
				: 'hide form',
			'custom_url'        => isset( $data['custom_url'] ) ? esc_url_raw( $data['custom_url'] ) : '',
			'page_url'          => isset( $data['page_url'] ) ? esc_url_raw( $data['page_url'] ) : '',
		];

		$encoded = wp_json_encode( $sanitized );
		return is_string( $encoded ) ? $encoded : $fallback;
	}
}

```
