| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\Emails; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\RequestAQuote\CPT\QuoteRequestCPT; |
| 4 |
use TierPricingTable\Addons\RequestAQuote\Models\RequestQuoteForm; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class AdminQuoteEmail extends \WC_Email { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
$this->id = 'tier_pricing_table_quote_request_admin'; |
| 14 |
$this->title = __( 'Quote Request (Admin)', 'tier-pricing-table' ); |
| 15 |
$this->description = __( 'This email is sent to the administrator when a customer requests a quote.', |
| 16 |
'tier-pricing-table' ); |
| 17 |
$this->template_html = 'emails/admin-quote-request.php'; |
| 18 |
$this->template_plain = 'emails/plain/admin-quote-request.php'; |
| 19 |
$this->template_base = dirname( __DIR__ ) . '/views/'; |
| 20 |
|
| 21 |
// Call parent constructor |
| 22 |
parent::__construct(); |
| 23 |
|
| 24 |
$this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) ); |
| 25 |
|
| 26 |
add_action( 'tiered_pricing_table/request_quote/quote_request_submitted', array( $this, 'trigger' ), 10, 1 ); |
| 27 |
} |
| 28 |
|
| 29 |
public function trigger( $postId ) { |
| 30 |
$this->object = new \TierPricingTable\Addons\RequestAQuote\Models\QuoteRequest( $postId ); |
| 31 |
|
| 32 |
if ( ! $this->object || ! $this->object->getId() ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Check form settings |
| 37 |
$formId = $this->object->getMeta('_form_id'); |
| 38 |
$form = RequestQuoteForm::get( $formId ); |
| 39 |
|
| 40 |
if ( $form && ! $form->isAdminEmailEnabled() ) { |
| 41 |
return; // Admin email is disabled for this form |
| 42 |
} |
| 43 |
|
| 44 |
$this->setup_locale(); |
| 45 |
|
| 46 |
$productId = $this->object->getProductId(); |
| 47 |
$product = wc_get_product( $productId ); |
| 48 |
$productName = $product ? $product->get_name() : __( 'Unknown Product', 'tier-pricing-table' ); |
| 49 |
|
| 50 |
if ( $this->is_enabled() && $this->get_recipient() ) { |
| 51 |
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), |
| 52 |
$this->get_attachments() ); |
| 53 |
} |
| 54 |
|
| 55 |
$this->restore_locale(); |
| 56 |
} |
| 57 |
|
| 58 |
public function get_default_subject() { |
| 59 |
return __( '[{site_title}] New Quote Request for {product_name}', 'tier-pricing-table' ); |
| 60 |
} |
| 61 |
|
| 62 |
public function get_default_heading() { |
| 63 |
return __( 'New Quote Request', 'tier-pricing-table' ); |
| 64 |
} |
| 65 |
|
| 66 |
public function get_content_html() { |
| 67 |
return wc_get_template_html( $this->template_html, array( |
| 68 |
'quotePost' => $this->object, |
| 69 |
'emailHeading' => $this->get_heading(), |
| 70 |
'sent_to_admin' => true, |
| 71 |
'plain_text' => false, |
| 72 |
'email' => $this, |
| 73 |
), '', $this->template_base ); |
| 74 |
} |
| 75 |
|
| 76 |
public function get_content_plain() { |
| 77 |
return wc_get_template_html( $this->template_plain, array( |
| 78 |
'quotePost' => $this->object, |
| 79 |
'emailHeading' => $this->get_heading(), |
| 80 |
'sent_to_admin' => true, |
| 81 |
'plain_text' => true, |
| 82 |
'email' => $this, |
| 83 |
), '', $this->template_base ); |
| 84 |
} |
| 85 |
|
| 86 |
public function init_form_fields() { |
| 87 |
$this->form_fields = array( |
| 88 |
'enabled' => array( |
| 89 |
'title' => __( 'Enable/Disable', 'tier-pricing-table' ), |
| 90 |
'type' => 'checkbox', |
| 91 |
'label' => __( 'Enable this email notification', 'tier-pricing-table' ), |
| 92 |
'default' => 'yes', |
| 93 |
), |
| 94 |
'recipient' => array( |
| 95 |
'title' => __( 'Recipient(s)', 'tier-pricing-table' ), |
| 96 |
'type' => 'text', |
| 97 |
// translators: %s: Admin email. |
| 98 |
'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to %s.', |
| 99 |
'tier-pricing-table' ), '<code>' . esc_attr( get_option( 'admin_email' ) ) . '</code>' ), |
| 100 |
'placeholder' => '', |
| 101 |
'default' => '', |
| 102 |
'desc_tip' => true, |
| 103 |
), |
| 104 |
'subject' => array( |
| 105 |
'title' => __( 'Subject', 'tier-pricing-table' ), |
| 106 |
'type' => 'text', |
| 107 |
'desc_tip' => true, |
| 108 |
'description' => __( 'This controls the email subject line. Leave blank to use the default subject: ', |
| 109 |
'tier-pricing-table' ) . '<code>' . $this->get_default_subject() . '</code>', |
| 110 |
'placeholder' => $this->get_default_subject(), |
| 111 |
'default' => '', |
| 112 |
), |
| 113 |
'heading' => array( |
| 114 |
'title' => __( 'Email heading', 'tier-pricing-table' ), |
| 115 |
'type' => 'text', |
| 116 |
'desc_tip' => true, |
| 117 |
'description' => __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: ', |
| 118 |
'tier-pricing-table' ) . '<code>' . $this->get_default_heading() . '</code>', |
| 119 |
'placeholder' => $this->get_default_heading(), |
| 120 |
'default' => '', |
| 121 |
), |
| 122 |
'email_type' => array( |
| 123 |
'title' => __( 'Email type', 'tier-pricing-table' ), |
| 124 |
'type' => 'select', |
| 125 |
'description' => __( 'Choose which format of email to send.', 'tier-pricing-table' ), |
| 126 |
'default' => 'html', |
| 127 |
'class' => 'email_type wc-enhanced-select', |
| 128 |
'options' => $this->get_email_type_options(), |
| 129 |
'desc_tip' => true, |
| 130 |
), |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
public function format_string( $string ) { |
| 135 |
$productName = __( 'Sample Product', 'tier-pricing-table' ); |
| 136 |
|
| 137 |
if ( $this->object && $this->object instanceof \TierPricingTable\Addons\RequestAQuote\Models\QuoteRequest ) { |
| 138 |
$productId = $this->object->getProductId(); |
| 139 |
$product = wc_get_product( $productId ); |
| 140 |
if ( $product ) { |
| 141 |
$productName = $product->get_name(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$string = str_replace( '{product_name}', $productName, $string ); |
| 146 |
|
| 147 |
return parent::format_string( $string ); |
| 148 |
} |
| 149 |
|
| 150 |
public function get_submitted_fields() { |
| 151 |
if ( ! $this->object || ! ( $this->object instanceof \TierPricingTable\Addons\RequestAQuote\Models\QuoteRequest ) ) { |
| 152 |
return array( |
| 153 |
array( 'label' => __( 'Sample Name', 'tier-pricing-table' ), 'value' => 'John Doe' ), |
| 154 |
array( 'label' => __( 'Sample Email', 'tier-pricing-table' ), 'value' => '[email protected]' ), |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
$customFields = $this->object->getCustomFields(); |
| 159 |
$fields = array(); |
| 160 |
|
| 161 |
foreach ( $customFields as $key => $fieldData ) { |
| 162 |
if ( ! is_array( $fieldData ) ) { |
| 163 |
$fieldData = array( 'label' => ucfirst( str_replace('_', ' ', $key) ), 'value' => $fieldData ); |
| 164 |
} |
| 165 |
$label = isset($fieldData['label']) && !empty($fieldData['label']) ? $fieldData['label'] : ucfirst( str_replace('_', ' ', $key) ); |
| 166 |
$value = isset($fieldData['value']) ? $fieldData['value'] : ''; |
| 167 |
|
| 168 |
$fields[] = array( |
| 169 |
'label' => $label, |
| 170 |
'value' => $value, |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
return $fields; |
| 175 |
} |
| 176 |
} |
| 177 |
|