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/Templates/Templates.php
<?php

namespace WPFormsPDF\Templates;

use WPForms\Helpers\File;
use WPFormsPDF\Storage;

/**
 * Renderer class.
 *
 * @since 1.0.0
 */
class Templates {

	/**
	 * Default template.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const DEFAULT_TEMPLATE = 'notification-modern';

	/**
	 * Default email style.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const DEFAULT_EMAIL_STYLE = 'modern';

	/**
	 * Default images.
	 *
	 * @since 1.0.0
	 *
	 * @var array
	 */
	public const DEFAULT_IMG = [
		'logo_text_h'  => 'logo/sullies-flowers-h.svg',
		'logo_text_v'  => 'logo/sullies-flowers-v.svg',
		'logo_no_text' => 'logo/sullies-flower.svg',
	];

	/**
	 * Custom templates file.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public const CUSTOM_TEMPLATES_FILE = 'custom-templates.json';

	/**
	 * Categories data.
	 *
	 * @since 1.0.0
	 *
	 * @var array
	 */
	private $categories = [];

	/**
	 * Templates.
	 *
	 * @since 1.0.0
	 *
	 * @var array
	 */
	private $templates = [];

	/**
	 * Custom templates.
	 *
	 * @since 1.0.0
	 *
	 * @var array|mixed
	 */
	private $custom_templates;

	/**
	 * Themes class instance.
	 *
	 * @since 1.0.0
	 *
	 * @var Themes
	 */
	private $themes_obj;

	/**
	 * Class constructor.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function __construct() {

		$this->themes_obj = new Themes();
	}

	/**
	 * Get categories data.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_categories(): array {

		if ( ! empty( $this->categories ) ) {
			return $this->categories;
		}

		$settings_defaults = wpforms_pdf()->form_builder->get_defaults();
		$email_appearance  = $settings_defaults['appearance'] ?? [];

		// Default appearance options for all categories except Notifications.
		$default_appearance = [
			'logo_id'                    => '',
			'logo_url'                   => self::DEFAULT_IMG['logo_text_h'],
			'logo_size'                  => 'medium',
			'logo_position'              => 'left',
			'font'                       => 'default',
			'font_size'                  => '16px',
			'text_color'                 => '{color_primary}',
			'link_color'                 => '{color_accent}',
			'page_background_color'      => '{color_background}',
			'page_background_color_end'  => '{color_background_light}',
			'page_background_image'      => 'none',
			'container_background_color' => 'none',
			'container_shadow'           => 'none',
			'container_border_style'     => 'none',
			'container_border_size'      => '1px',
			'container_border_color'     => '{color_border}',
			'container_border_radius'    => '0px',
		];

		$categories = [
			'notification' => [
				'title'         => esc_html__( 'Notification', 'wpforms-pdf' ),
				'title_plural'  => esc_html__( 'Notifications', 'wpforms-pdf' ),
				'default_style' => $settings_defaults['template'] ?? 'notification-modern',
				'appearance'    => wp_parse_args(
					[
						'logo_url'                   => $email_appearance['logo_url'],
						'logo_size'                  => $email_appearance['logo_size'],
						'logo_position'              => 'center',
						'font'                       => $email_appearance['font'],
						'page_background_color'      => '{color_background_light}',
						'page_background_color_end'  => '{color_background_light}',
						'container_background_color' => '{color_background}',
					],
					$default_appearance
				),
			],
			'document'     => [
				'title'         => esc_html__( 'Document', 'wpforms-pdf' ),
				'title_plural'  => esc_html__( 'Documents', 'wpforms-pdf' ),
				'default_style' => 'document-basic',
				'appearance'    => $default_appearance,
			],
			'financial'    => [
				'title'         => esc_html__( 'Financial', 'wpforms-pdf' ),
				'title_plural'  => esc_html__( 'Financial', 'wpforms-pdf' ),
				'default_style' => 'financial-modern',
				'appearance'    => $default_appearance,
			],
			'certificate'  => [
				'title'         => esc_html__( 'Certificate', 'wpforms-pdf' ),
				'title_plural'  => esc_html__( 'Certificates', 'wpforms-pdf' ),
				'default_style' => 'certificate-modern',
				'appearance'    => $default_appearance,
			],
		];

		/**
		 * Filters PDF templates categories data.
		 *
		 * @since 1.0.0
		 *
		 * @param array $categories Categories data.
		 */
		$this->categories = (array) apply_filters( 'wpforms_pdf_templates_get_categories', $categories );

		return $this->categories;
	}

	/**
	 * Get single category data.
	 *
	 * @since 1.0.0
	 *
	 * @param string $category Category slug.
	 *
	 * @return array
	 */
	public function get_category( string $category ): array {

		$categories = $this->get_categories();

		return $categories[ $category ] ?? [];
	}

	/**
	 * Get templates data.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_templates(): array {

		if ( ! empty( $this->templates ) ) {
			return $this->templates;
		}

		$templates = [
			// Notifications.
			'notification-classic' => [
				'category' => 'notification',
				'title'    => esc_html__( 'Classic', 'wpforms-pdf' ),
			],
			'notification-compact' => [
				'category' => 'notification',
				'title'    => esc_html__( 'Compact', 'wpforms-pdf' ),
			],
			'notification-modern'  => [
				'category'   => 'notification',
				'title'      => esc_html__( 'Modern', 'wpforms-pdf' ),
				'appearance' => [
					'container_border_radius' => '12px',
				],
			],
			'notification-elegant' => [
				'category' => 'notification',
				'title'    => esc_html__( 'Elegant', 'wpforms-pdf' ),
			],
			'notification-tech'    => [
				'category'   => 'notification',
				'title'      => esc_html__( 'Tech', 'wpforms-pdf' ),
				'appearance' => [
					'logo_position' => 'left',
				],
			],

			// Documents.
			'document-basic'       => [
				'category' => 'document',
				'title'    => esc_html__( 'Basic', 'wpforms-pdf' ),
				'text'     => [
					'content'       => '{all_fields}',
					'content_color' => '{color_primary}',
				],
			],
			'document-legal'       => [
				'category'   => 'document',
				'title'      => esc_html__( 'Legal', 'wpforms-pdf' ),
				'appearance' => [
					'logo_position' => 'center',
				],
				'text'       => [
					'content'                    => '{all_fields}',
					'content_color'              => '{color_primary}',
					'business_address'           => esc_html__( '1234 Sunset Blvd, Los Angeles, CA 90026', 'wpforms-pdf' ),
					'business_address_color'     => '{color_tertiary}',
					'business_email'             => esc_html__( 'hello@sulliesflowers.com', 'wpforms-pdf' ),
					'business_email_color'       => '{color_tertiary}',
					'business_phone'             => esc_html__( '555-250-5678', 'wpforms-pdf' ),
					'business_phone_color'       => '{color_tertiary}',
					'signature_type'             => 'text',
					'signature_text'             => esc_html__( 'John Doe', 'wpforms-pdf' ),
					'signature_url'              => WPFORMS_PDF_URL . 'assets/images/signature-sample.png',
					'signature_size'             => 'medium',
					'signature_position'         => 'center',
					'signature_text_color'       => '{color_secondary}',
					'signature_subheading'       => esc_html__( 'Signature', 'wpforms-pdf' ),
					'signature_subheading_color' => '{color_tertiary}',
					'date'                       => '{date format="d-m-Y"}',
					'date_color'                 => '{color_secondary}',
					'date_subheading'            => esc_html__( 'Date', 'wpforms-pdf' ),
					'date_subheading_color'      => '{color_tertiary}',
				],
			],
			'document-application' => [
				'category'    => 'document',
				'title'       => esc_html__( 'Application', 'wpforms-pdf' ),
				'email_style' => 'compact',
				'appearance'  => [
					'text_color' => '{color_secondary}',
				],
				'text'        => [
					'content'                => '{all_fields}',
					'content_color'          => '{color_primary}',
					'business_address'       => esc_html__( '1234 Sunset Blvd, Los Angeles, CA 90026', 'wpforms-pdf' ),
					'business_address_color' => '{color_tertiary}',
					'business_email'         => esc_html__( 'hello@sulliesflowers.com', 'wpforms-pdf' ),
					'business_email_color'   => '{color_tertiary}',
					'business_phone'         => esc_html__( '555-250-5678', 'wpforms-pdf' ),
					'business_phone_color'   => '{color_tertiary}',
				],
			],
			'document-coverpage'   => [
				'category'   => 'document',
				'title'      => esc_html__( 'Cover Page', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'              => self::DEFAULT_IMG['logo_text_v'],
					'logo_position'         => 'center',
					'page_background_image' => 'triangles',
				],
				'text'       => [
					'heading_1'       => esc_html__( 'Customizable', 'wpforms-pdf' ),
					'heading_1_color' => '{color_primary}',
					'heading_2'       => esc_html__( 'Cover Page', 'wpforms-pdf' ),
					'heading_2_color' => '{color_secondary}',
					'heading_3'       => esc_html__( 'Additional Details', 'wpforms-pdf' ),
					'heading_3_color' => '{color_secondary}',
				],
			],
			'document-table'       => [
				'category'    => 'document',
				'title'       => esc_html__( 'Table', 'wpforms-pdf' ),
				'email_style' => 'compact',
				'appearance'  => [
					'logo_position' => 'center',
					'text_color'    => '{color_secondary}',
				],
				'text'        => [
					'content'                => '{all_fields}',
					'content_color'          => '{color_primary}',
					'business_address'       => esc_html__( '1234 Sunset Blvd, Los Angeles, CA 90026', 'wpforms-pdf' ),
					'business_address_color' => '{color_tertiary}',
					'business_email'         => esc_html__( 'hello@sulliesflowers.com', 'wpforms-pdf' ),
					'business_email_color'   => '{color_tertiary}',
					'business_phone'         => esc_html__( '555-250-5678', 'wpforms-pdf' ),
					'business_phone_color'   => '{color_tertiary}',
				],
			],
			'document-sidebar'     => [
				'category'   => 'document',
				'title'      => esc_html__( 'Sidebar', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'                   => self::DEFAULT_IMG['logo_text_v'],
					'logo_position'              => 'left',
					'page_background_color'      => '{color_background_light}',
					'container_background_color' => '{color_background}',
				],
				'text'       => [
					'header_address'         => esc_html__( '1234 Sunset Blvd,', 'wpforms-pdf' ),
					'header_address_color'   => '{color_secondary}',
					'header_address_2'       => esc_html__( 'Los Angeles, CA 90026', 'wpforms-pdf' ),
					'header_address_2_color' => '{color_secondary}',
					'header_email'           => esc_html__( 'hello@sulliesflowers.com', 'wpforms-pdf' ),
					'header_email_color'     => '{color_secondary}',
					'header_phone'           => esc_html__( '555-250-5678', 'wpforms-pdf' ),
					'header_phone_color'     => '{color_secondary}',
					'heading_1'              => esc_html__( 'Document Title', 'wpforms-pdf' ),
					'heading_1_color'        => '{color_primary}',
					'paragraph_1'            => esc_html__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'wpforms-pdf' ),
					'paragraph_1_color'      => '{color_secondary}',
					'content'                => '{all_fields}',
					'content_color'          => '{color_primary}',
				],
			],

			// Financial.
			'financial-classic'    => [
				'category'   => 'financial',
				'title'      => esc_html__( 'Classic', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'  => self::DEFAULT_IMG['logo_text_v'],
					'logo_size' => 'small',
				],
				'text'       => [
					'heading_1'              => esc_html__( 'Invoice', 'wpforms-pdf' ),
					'heading_1_color'        => '{color_primary}',
					'billing_heading'        => esc_html__( 'BILL TO', 'wpforms-pdf' ),
					'billing_heading_color'  => '{color_accent}',
					'billing_content'        => wp_kses_post( __( '<strong>Logan McKenzie</strong><p>567 Maple Street, Toronto, ON M5V 2H3</p><p><a href="mailto:billing@madebymckenzie.ca">billing@madebymckenzie.ca</a></p>', 'wpforms-pdf' ) ),
					'billing_content_color'  => '{color_secondary}',
					'details_heading'        => esc_html__( 'DETAILS', 'wpforms-pdf' ),
					'details_heading_color'  => '{color_accent}',
					'details_content'        => wp_kses_post( __( '<strong>Invoice #: SF0001-25</strong><p>Date Issued: February 27, 2025</p><p>Pay By: March 27, 2025</p>', 'wpforms-pdf' ) ),
					'details_content_color'  => '{color_secondary}',
					'content'                => '{order_summary}',
					'content_color'          => '{color_primary}',
					'terms_heading'          => esc_html__( 'TERMS & CONDITIONS', 'wpforms-pdf' ),
					'terms_heading_color'    => '{color_accent}',
					'terms_content'          => esc_html__( 'Payment is due within 14 days from the date of invoice unless otherwise agreed in writing. By making payment, you agree to the terms outlined herein.', 'wpforms-pdf' ),
					'terms_content_color'    => '{color_secondary}',
					'payment_heading'        => esc_html__( 'PAYMENT', 'wpforms-pdf' ),
					'payment_heading_color'  => '{color_accent}',
					'payment_content'        => wp_kses_post( __( '<strong>Royal Bank of Canada.</strong><p>Account Name: Sullie’s Flowers, LLC</p><p>Account Number: 123 456 789</p><p>Institution Number: 004</p><p>Transit Number: 642</p>', 'wpforms-pdf' ) ),
					'payment_content_color'  => '{color_secondary}',
					'business_address'       => esc_html__( '1234 Sunset Blvd, Los Angeles, CA 90026', 'wpforms-pdf' ),
					'business_address_color' => '{color_tertiary}',
					'business_email'         => esc_html__( 'hello@sulliesflowers.com', 'wpforms-pdf' ),
					'business_email_color'   => '{color_tertiary}',
					'business_phone'         => esc_html__( '555-250-5678', 'wpforms-pdf' ),
					'business_phone_color'   => '{color_tertiary}',
				],
			],
			'financial-modern'     => [
				'category'   => 'financial',
				'title'      => esc_html__( 'Modern', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'                   => self::DEFAULT_IMG['logo_text_v'],
					'logo_size'                  => 'medium',
					'page_background_color'      => '{color_background_light}',
					'container_background_color' => '{color_background}',
				],
				'text'       => [
					'heading_1'                    => esc_html__( 'Invoice', 'wpforms-pdf' ),
					'heading_1_color'              => '{color_primary}',
					'invoice_number_heading'       => esc_html__( 'Invoice #:', 'wpforms-pdf' ),
					'invoice_number_heading_color' => '{color_primary}',
					'invoice_number'               => esc_html__( 'SF0001-25', 'wpforms-pdf' ),
					'invoice_number_color'         => '{color_primary}',
					'posted_date'                  => '{date format="F j, Y"}',
					'posted_date_color'            => '{color_primary}',
					'business_address'             => esc_html__( '1234 Sunset Blvd, Los Angeles, CA 90026', 'wpforms-pdf' ),
					'business_address_color'       => '{color_background}',
					'business_email'               => esc_html__( 'hello@sulliesflowers.com', 'wpforms-pdf' ),
					'business_email_color'         => '{color_background}',
					'business_phone'               => esc_html__( '555-250-5678', 'wpforms-pdf' ),
					'business_phone_color'         => '{color_background}',
					'billing_heading'              => esc_html__( 'Bill To', 'wpforms-pdf' ),
					'billing_heading_color'        => '{color_accent}',
					'billing_content'              => wp_kses_post( __( '<strong>Logan McKenzie</strong><p>567 Maple Street, Toronto, ON M5V 2H3</p><p><a href="mailto:billing@madebymckenzie.ca">billing@madebymckenzie.ca</a></p>', 'wpforms-pdf' ) ),
					'billing_content_color'        => '{color_secondary}',
					'details_heading'              => esc_html__( 'Payment Details', 'wpforms-pdf' ),
					'details_heading_color'        => '{color_accent}',
					'details_content'              => wp_kses_post( __( '<strong>Payment Due: {date format="F j, Y"}</strong><p>Send payment to the email address<br>above or transfer using the details below.</p>', 'wpforms-pdf' ) ),
					'details_content_color'        => '{color_secondary}',
					'content'                      => '{order_summary}',
					'content_color'                => '{color_primary}',
					'payment_heading'              => esc_html__( 'Payment Info', 'wpforms-pdf' ),
					'payment_heading_color'        => '{color_accent}',
					'payment_content'              => wp_kses_post( __( '<strong>Sullie’s Flowers, LLC via Royal Bank of Canada</strong><p>Account Number: 123 456 789 — Institution Number: 004 — Transit Number: 642</p>', 'wpforms-pdf' ) ),
					'payment_content_color'        => '{color_secondary}',
					'terms_heading'                => esc_html__( 'TERMS & CONDITIONS', 'wpforms-pdf' ),
					'terms_heading_color'          => '{color_accent}',
					'terms_content'                => esc_html__( 'Payment is due within 14 days from the date of invoice unless otherwise agreed in writing. By making payment, you agree to the terms outlined herein.', 'wpforms-pdf' ),
					'terms_content_color'          => '{color_secondary}',

				],
			],
			'financial-sidebar'    => [
				'category'   => 'financial',
				'title'      => esc_html__( 'Sidebar', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'                   => self::DEFAULT_IMG['logo_no_text'],
					'logo_position'              => 'left',
					'page_background_color'      => '{color_background_light}',
					'container_background_color' => '{color_background}',
				],
				'text'       => [
					'business_name'                => esc_html__( 'Sullie\'s Flowers', 'wpforms-pdf' ),
					'business_name_color'          => '{color_primary}',
					'business_address'             => wp_kses_post( __( '1234 Sunset Blvd,<br>Los Angeles, CA 90026', 'wpforms-pdf' ) ),
					'business_address_color'       => '{color_secondary}',
					'business_email'               => esc_html__( 'hello@sulliesflowers.com', 'wpforms-pdf' ),
					'business_email_color'         => '{color_secondary}',
					'business_phone'               => esc_html__( '555-250-5678', 'wpforms-pdf' ),
					'business_phone_color'         => '{color_secondary}',
					'heading_1'                    => esc_html__( 'Invoice', 'wpforms-pdf' ),
					'heading_1_color'              => '{color_primary}',
					'invoice_number_heading'       => esc_html__( 'Invoice number', 'wpforms-pdf' ),
					'invoice_number_heading_color' => '{color_accent}',
					'invoice_number'               => esc_html__( 'SF0001-25', 'wpforms-pdf' ),
					'invoice_number_color'         => '{color_secondary}',
					'tax_heading'                  => esc_html__( 'Tax ID', 'wpforms-pdf' ),
					'tax_heading_color'            => '{color_accent}',
					'tax_id'                       => esc_html__( '987654321-001', 'wpforms-pdf' ),
					'tax_id_color'                 => '{color_secondary}',
					'posted_date_heading'          => esc_html__( 'Date Posted', 'wpforms-pdf' ),
					'posted_date_heading_color'    => '{color_accent}',
					'posted_date'                  => '{date format="F j, Y"}',
					'posted_date_color'            => '{color_secondary}',
					'due_date_heading'             => esc_html__( 'Payment Due', 'wpforms-pdf' ),
					'due_date_heading_color'       => '{color_accent}',
					'due_date'                     => '{date format="F j, Y"}',
					'due_date_color'               => '{color_secondary}',
					'billing_heading'              => esc_html__( 'BILL TO', 'wpforms-pdf' ),
					'billing_heading_color'        => '{color_accent}',
					'billing_content'              => wp_kses_post( __( '<strong>Logan McKenzie</strong><p>567 Maple Street, Toronto, ON M5V 2H3</p><p><a href="mailto:billing@madebymckenzie.ca">billing@madebymckenzie.ca</a></p><p><a href="tel:555-250-1234">555-250-1234</a></p>', 'wpforms-pdf' ) ),
					'billing_content_color'        => '{color_secondary}',
					'content'                      => '{order_summary}',
					'content_color'                => '{color_primary}',
					'payment_heading'              => esc_html__( 'Payment Info', 'wpforms-pdf' ),
					'payment_heading_color'        => '{color_accent}',
					'payment_content'              => wp_kses_post( __( '<strong>Royal Bank of Canada.</strong><p>Account Number: 123 456 789</p><p>Institution Number: 004</p><p>Transit Number: 642</p>', 'wpforms-pdf' ) ),
					'payment_content_color'        => '{color_secondary}',
					'terms_heading'                => esc_html__( 'TERMS & CONDITIONS', 'wpforms-pdf' ),
					'terms_heading_color'          => '{color_accent}',
					'terms_content'                => wp_kses_post( __( '<strong>Payment must be made no later than March 27, 2025</strong><p>Payment is due within 14 days from the date of invoice unless otherwise agreed in writing. By making payment, you agree to the terms outlined herein.', 'wpforms-pdf' ) ),
					'terms_content_color'          => '{color_secondary}',
				],
			],
			// Certificates.
			'certificate-classic'  => [
				'category'   => 'certificate',
				'title'      => esc_html__( 'Classic', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'              => self::DEFAULT_IMG['logo_text_v'],
					'logo_position'         => 'center',
					'font_size'             => '14px',
					'page_background_image' => 'classic',
				],
				'text'       => [
					'heading_1'                  => esc_html__( 'Certificate', 'wpforms-pdf' ),
					'heading_1_color'            => '{color_primary}',
					'heading_2'                  => esc_html__( 'of achievement', 'wpforms-pdf' ),
					'heading_2_color'            => '{color_secondary}',
					'heading_3'                  => esc_html__( 'proudly presented to', 'wpforms-pdf' ),
					'heading_3_color'            => '{color_tertiary}',
					'cname'                      => esc_html__( 'SULLIE ELOSO', 'wpforms-pdf' ),
					'cname_color'                => '{color_accent}',
					'content'                    => esc_html__( 'In recognition of your outstanding dedication, hard work, and invaluable contributions. Your commitment to excellence and willingness to go above and beyond have made a meaningful impact. Through your efforts, you have demonstrated professionalism, teamwork, and a passion for making a difference.', 'wpforms-pdf' ),
					'content_color'              => '{color_primary}',
					'signature_show'             => 1,
					'signature_type'             => 'text',
					'signature_text'             => esc_html__( 'John Doe', 'wpforms-pdf' ),
					'signature_url'              => WPFORMS_PDF_URL . 'assets/images/signature-sample.png',
					'signature_size'             => 'medium',
					'signature_position'         => 'center',
					'signature_text_color'       => '{color_secondary}',
					'signature_subheading'       => esc_html__( 'Co-Founder, Company Name', 'wpforms-pdf' ),
					'signature_subheading_color' => '{color_tertiary}',
					'badge_show'                 => 1,
					'badge_year'                 => '{date format="Y"}',
					'badge_year_color'           => '{color_accent}',
					'badge_subheading'           => esc_html__( 'Award', 'wpforms-pdf' ),
					'badge_subheading_color'     => '{color_accent}',
				],
			],
			'certificate-modern'   => [
				'category'   => 'certificate',
				'title'      => esc_html__( 'Modern', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'              => self::DEFAULT_IMG['logo_text_v'],
					'logo_position'         => 'center',
					'font_size'             => '14px',
					'page_background_image' => 'ribbons',
				],
				'text'       => [
					'heading_1'                  => esc_html__( 'Certificate', 'wpforms-pdf' ),
					'heading_1_color'            => '{color_primary}',
					'heading_2'                  => esc_html__( 'of achievement', 'wpforms-pdf' ),
					'heading_2_color'            => '{color_secondary}',
					'heading_3'                  => esc_html__( 'proudly presented to', 'wpforms-pdf' ),
					'heading_3_color'            => '{color_tertiary}',
					'cname'                      => esc_html__( 'SULLIE ELOSO', 'wpforms-pdf' ),
					'cname_color'                => '{color_accent}',
					'content'                    => esc_html__( 'In recognition of your outstanding dedication, hard work, and invaluable contributions. Your commitment to excellence and willingness to go above and beyond have made a meaningful impact. Through your efforts, you have demonstrated professionalism, teamwork, and a passion for making a difference.', 'wpforms-pdf' ),
					'content_color'              => '{color_primary}',
					'badge_show'                 => 1,
					'badge_year'                 => '{date format="Y"}',
					'badge_year_color'           => '{color_background}',
					'badge_subheading'           => esc_html__( 'Award', 'wpforms-pdf' ),
					'badge_subheading_color'     => '{color_background}',
					'signature_show'             => 1,
					'signature_type'             => 'text',
					'signature_text'             => esc_html__( 'John Doe', 'wpforms-pdf' ),
					'signature_url'              => WPFORMS_PDF_URL . 'assets/images/signature-sample.png',
					'signature_size'             => 'medium',
					'signature_position'         => 'center',
					'signature_text_color'       => '{color_tertiary}',
					'signature_subheading'       => esc_html__( 'Co-Founder, Company Name', 'wpforms-pdf' ),
					'signature_subheading_color' => '{color_tertiary}',
				],
			],
			'certificate-elegant'  => [
				'category'   => 'certificate',
				'title'      => esc_html__( 'Elegant', 'wpforms-pdf' ),
				'appearance' => [
					'logo_url'               => self::DEFAULT_IMG['logo_text_v'],
					'logo_position'          => 'center',
					'font_size'              => '14px',
					'page_background_image'  => 'wings',
					'container_border_style' => 'solid',
					'container_border_size'  => '3px',
					'container_border_color' => '{color_accent}',
				],
				'text'       => [
					'heading_1'                  => esc_html__( 'Certificate', 'wpforms-pdf' ),
					'heading_1_color'            => '{color_primary}',
					'heading_2'                  => esc_html__( 'of Achievement', 'wpforms-pdf' ),
					'heading_2_color'            => '{color_secondary}',
					'heading_3'                  => esc_html__( 'proudly presented to', 'wpforms-pdf' ),
					'heading_3_color'            => '{color_tertiary}',
					'cname'                      => esc_html__( 'Sullie Eloso', 'wpforms-pdf' ),
					'cname_color'                => '{color_accent}',
					'content'                    => esc_html__( 'In recognition of your outstanding dedication, hard work, and invaluable contributions. Your commitment to excellence and willingness to go above and beyond have made a meaningful impact. Through your efforts, you have demonstrated professionalism, teamwork, and a passion for making a difference.', 'wpforms-pdf' ),
					'content_color'              => '{color_primary}',
					'signature_show'             => 1,
					'signature_type'             => 'text',
					'signature_text'             => esc_html__( 'John Doe', 'wpforms-pdf' ),
					'signature_url'              => WPFORMS_PDF_URL . 'assets/images/signature-sample.png',
					'signature_size'             => 'medium',
					'signature_position'         => 'center',
					'signature_text_color'       => '{color_primary}',
					'signature_subheading'       => esc_html__( 'Co-Founder, Company Name', 'wpforms-pdf' ),
					'signature_subheading_color' => '{color_tertiary}',
					'badge_show'                 => 1,
					'badge_year'                 => '{date format="Y"}',
					'badge_year_color'           => '{color_accent}',
					'badge_subheading'           => esc_html__( 'Award', 'wpforms-pdf' ),
					'badge_subheading_color'     => '{color_accent}',
				],
			],
		];

		$templates = $this->prepare_templates( $templates );

		/**
		 * Filters PDF templates categories data.
		 *
		 * @since 1.0.0
		 *
		 * @param array $categories Categories data.
		 */
		$this->templates = (array) apply_filters( 'wpforms_pdf_templates_get_templates', $templates );

		return $this->templates;
	}

	/**
	 * Get all templates in a single array.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_all_templates(): array {

		return array_merge(
			$this->get_custom_templates(),
			$this->get_templates()
		);
	}

	/**
	 * Prepare templates data.
	 *
	 * @since 1.0.0
	 *
	 * @param array $templates Templates data.
	 *
	 * @return array
	 */
	private function prepare_templates( array $templates ): array {

		$categories = $this->get_categories();

		// Add default appearance settings.
		foreach ( $templates as $template_slug => & $template ) {
			$category = $categories[ $template['category'] ?? '_' ] ?? null;

			if ( empty( $category['appearance'] ) ) {
				continue;
			}

			$template = $this->prepare_template( $template_slug, $template, $category );
		}

		return $templates;
	}

	/**
	 * Prepare template data.
	 *
	 * @since 1.0.0
	 *
	 * @param string $template_slug Template slug.
	 * @param array  $template      Template data.
	 * @param array  $category      Category data.
	 *
	 * @return array
	 */
	protected function prepare_template( string $template_slug, array $template, array $category ): array {

		$template['slug']       = $template_slug;
		$template['appearance'] = wp_parse_args( $template['appearance'] ?? [], $category['appearance'] );
		$template['text']       = $template['text'] ?? [];

		static $default_logos = [];

		// A standard predefined logo is actually a template image,
		// we should store the raw copy for further processing in JavaScript.
		if ( strpos( $template['appearance']['logo_url'], 'logo/' ) === 0 ) {
			$logo_url                   = $template['appearance']['logo_url'];
			$default_logos[ $logo_url ] = ! empty( $default_logos[ $logo_url ] ) ?
				$default_logos[ $logo_url ] :
				File::get_contents( WPFORMS_PDF_PATH . 'assets/images/' . $logo_url );

			$template['appearance']['logo_template'] = $default_logos[ $logo_url ];
		}

		return $template;
	}

	/**
	 * Get single template raw data.
	 *
	 * @since 1.0.0
	 *
	 * @param string $template_slug Template slug.
	 *
	 * @return array
	 */
	public function get_raw_template( string $template_slug ): array {

		$templates = $this->get_all_templates();

		return $templates[ $template_slug ] ?? [];
	}

	/**
	 * Get single template compiled data.
	 *
	 * @since 1.0.0
	 *
	 * @param string $template_slug Template slug.
	 * @param string $theme_slug    Theme slug.
	 *
	 * @return array
	 */
	public function get_template( string $template_slug, string $theme_slug ): array {

		$template = $this->get_raw_template( $template_slug );

		if ( empty( $template ) ) {
			return [];
		}

		$template['style'] = explode( '-', $template_slug )[1] ?? '';

		// For notifications: use style directly.
		// For others: check custom template → base template → default.
		$template['email_style'] = $template['category'] === 'notification'
			? $template['style']
			: $this->get_template_element( $template, 'email_style', $this->get_base_template_email_style( $template ) );

		// Apply theme colors to the template.
		return $this->apply_theme_colors( $template, $theme_slug );
	}

	/**
	 * Get a template element value by key.
	 *
	 * @since 1.0.0
	 *
	 * @param array  $template   Template data.
	 * @param string $theme_slug Theme slug.
	 *
	 * @return array
	 */
	private function apply_theme_colors( array $template, string $theme_slug ): array {

		// Get theme data.
		$theme  = $this->themes_obj->get_theme( $theme_slug );
		$colors = $theme['colors'] ?? Themes::DEFAULT_COLORS;

		// Apply theme colors to the template.
		$template['appearance'] = $this->replace_colors(
			$this->get_template_element( $template, 'appearance', [] ),
			$colors
		);

		$template['text'] = $this->replace_colors(
			$this->get_template_element( $template, 'text', [] ),
			$colors
		);

		// Apply custom theme colors to the template.
		$template['text']       = array_intersect_key( array_merge( $template['text'], $theme['text'] ?? [] ), $template['text'] );
		$template['appearance'] = array_merge( $template['appearance'], $theme['appearance'] ?? [] );

		// Add theme data to the template data.
		$template['theme']         = $theme;
		$template['theme']['slug'] = $theme_slug;

		return $template;
	}

	/**
	 * Get a template element value by key.
	 *
	 * @since 1.0.0
	 *
	 * @param array  $template      Template data.
	 * @param string $element_key   Element key to get value for.
	 * @param mixed  $default_value Default value to return if the element key is not found.
	 *
	 * @return mixed
	 */
	private function get_template_element( array $template, string $element_key, $default_value ) {

		return $template[ $element_key ] ?? $default_value;
	}

	/**
	 * Get the email style from the base template.
	 *
	 * For custom templates that don't have their own email_style,
	 * this method looks up the base template's email_style.
	 *
	 * @since 1.3.0
	 *
	 * @param array $template Template data.
	 *
	 * @return string
	 */
	private function get_base_template_email_style( array $template ): string {

		$base_template_slug = $template['baseTemplate'] ?? '';

		if ( empty( $base_template_slug ) ) {
			return self::DEFAULT_EMAIL_STYLE;
		}

		$base_template = $this->get_raw_template( $base_template_slug );

		return $base_template['email_style'] ?? self::DEFAULT_EMAIL_STYLE;
	}

	/**
	 * Replace colors in the content string or in the array of strings.
	 *
	 * @since 1.0.0
	 *
	 * @param string|array $content Content string or array of strings.
	 * @param array        $colors  Colors data.
	 *
	 * @return string|array
	 */
	public function replace_colors( $content, array $colors ) {

		$color_keys   = [];
		$color_values = array_values( $colors );
		$i            = 0;

		foreach ( $colors as $color_key => $color ) {
			$color_keys[]       = '{color_' . $color_key . '}';
			$color_values[ $i ] = $color_values[ $i ] === '' ? '#ffffff00' : $color_values[ $i ];

			++$i;
		}

		// Replace colors in string.
		if ( ! is_array( $content ) ) {
			return str_replace( $color_keys, $color_values, (string) $content );
		}

		// Replace colors in an array.
		foreach ( $content as $key => $value ) {
			$content[ $key ] = str_replace( $color_keys, $color_values, (string) $value );
		}

		return $content;
	}

	/**
	 * Get custom templates file path.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	private function get_custom_templates_file(): string {

		return Storage::get_dir() . '/' . self::CUSTOM_TEMPLATES_FILE;
	}

	/**
	 * Return custom templates.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_custom_templates(): array {

		if ( $this->custom_templates !== null ) {
			return $this->custom_templates;
		}

		$file_path      = $this->get_custom_templates_file();
		$templates_json = File::get_contents( $file_path ) ?? '{}';
		$templates      = json_decode( $templates_json, true );

		$this->custom_templates = ! empty( $templates ) ? $templates : [];

		return $this->custom_templates;
	}

	/**
	 * Update custom template data.
	 *
	 * @since 1.0.0
	 *
	 * @param array $template_data Custom template data.
	 *
	 * @return bool
	 */
	public function update_custom_template( array $template_data ): bool {

		$template_slug = $template_data['slug'] ?? '';

		if ( empty( $template_slug ) ) {
			return false;
		}

		// Remove logo template data. It will be inherited from the base template in JavaScript.
		unset( $template_data['appearance']['logo_template'] );

		// Normalize toggle field values.
		$toggle_fields = [ 'badge_show', 'signature_show' ];

		foreach ( $toggle_fields as $toggle ) {
			if ( isset( $template_data['text'][ $toggle ] ) ) {
				$template_data['text'][ $toggle ] = empty( $template_data['text'][ $toggle ] ) || $template_data['text'][ $toggle ] === 'false' ? 0 : 1;
			}
		}

		$custom_templates = $this->get_custom_templates();

		// Update custom template data.
		$custom_templates[ $template_slug ] = $template_data;

		return $this->update_custom_templates( $custom_templates );
	}

	/**
	 * Delete custom template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $template_data Custom template data. Should contain a 'slug' key.
	 *
	 * @return bool
	 */
	public function delete_custom_template( array $template_data ): bool {

		$template_slug = $template_data['slug'] ?? '';

		if ( empty( $template_slug ) ) {
			return false;
		}

		$custom_templates = $this->get_custom_templates();

		// Update custom template data.
		unset( $custom_templates[ $template_slug ] );

		return $this->update_custom_templates( $custom_templates );
	}

	/**
	 * Update custom templates data.
	 *
	 * @since 1.8.8
	 *
	 * @param array $custom_templates Custom templates data.
	 *
	 * @return bool
	 */
	private function update_custom_templates( array $custom_templates ): bool {

		$this->custom_templates = $custom_templates;

		// Encode custom templates data to JSON.
		$json_data = ! empty( $custom_templates ) ? wp_json_encode( $custom_templates ) : '{}';
		$file_path = $this->get_custom_templates_file();

		// Save custom templates data and return the result.
		return File::put_contents( $file_path, $json_data );
	}
}