| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\Models; |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class RequestQuoteForm { |
| 8 |
|
| 9 |
private array $data; |
| 10 |
private array $overrides = array(); |
| 11 |
|
| 12 |
public function __construct( array $data ) { |
| 13 |
$this->data = $data; |
| 14 |
} |
| 15 |
|
| 16 |
public function setOverrides( array $overrides ) { |
| 17 |
$this->overrides = $overrides; |
| 18 |
} |
| 19 |
|
| 20 |
public function getId(): string { |
| 21 |
return isset( $this->data['id'] ) ? (string) $this->data['id'] : ''; |
| 22 |
} |
| 23 |
|
| 24 |
public function getName(): string { |
| 25 |
return isset( $this->data['name'] ) ? (string) $this->data['name'] : ''; |
| 26 |
} |
| 27 |
|
| 28 |
public function getPromptText(): string { |
| 29 |
return ! empty( $this->data['prompt_text'] ) ? (string) $this->data['prompt_text'] : __( 'Request a quote', |
| 30 |
'tier-pricing-table' ); |
| 31 |
} |
| 32 |
|
| 33 |
public function getSuccessAction(): string { |
| 34 |
return isset( $this->data['success_action'] ) ? (string) $this->data['success_action'] : 'message'; |
| 35 |
} |
| 36 |
|
| 37 |
public function getSuccessMessage(): string { |
| 38 |
return isset( $this->data['success_message'] ) ? (string) $this->data['success_message'] : __( 'Thank you for your request. We will contact you soon.', |
| 39 |
'tier-pricing-table' ); |
| 40 |
} |
| 41 |
|
| 42 |
public function getSuccessRedirectUrl(): string { |
| 43 |
return isset( $this->data['success_redirect_url'] ) ? (string) $this->data['success_redirect_url'] : ''; |
| 44 |
} |
| 45 |
|
| 46 |
public function getModalTitle(): string { |
| 47 |
return $this->data['modal_title'] ?? __( 'Request a Quote', 'tier-pricing-table' ); |
| 48 |
} |
| 49 |
|
| 50 |
public function getDisplayPosition(): string { |
| 51 |
return $this->data['display_position'] ?? 'integrated'; |
| 52 |
} |
| 53 |
|
| 54 |
public function getIntegratedLabelText(): string { |
| 55 |
if ( ! empty( $this->overrides['integrated_label_text'] ) ) { |
| 56 |
return $this->overrides['integrated_label_text']; |
| 57 |
} |
| 58 |
|
| 59 |
$label = $this->data['integrated_label_text'] ?? ''; |
| 60 |
if ( empty( $label ) ) { |
| 61 |
return __( 'Custom Quantity', 'tier-pricing-table' ); |
| 62 |
} |
| 63 |
|
| 64 |
return $label; |
| 65 |
} |
| 66 |
|
| 67 |
public function getHideAddToCart(): bool { |
| 68 |
return ! empty( $this->data['hide_add_to_cart'] ); |
| 69 |
} |
| 70 |
|
| 71 |
public function getShowForNonPurchasable(): bool { |
| 72 |
return ! empty( $this->data['show_for_non_purchasable'] ); |
| 73 |
} |
| 74 |
|
| 75 |
public function getSubmitButtonText(): string { |
| 76 |
return $this->data['submit_button_text'] ?? __( 'Submit Request', 'tier-pricing-table' ); |
| 77 |
} |
| 78 |
|
| 79 |
public function getFields(): array { |
| 80 |
return isset( $this->data['fields'] ) && is_array( $this->data['fields'] ) ? $this->data['fields'] : array(); |
| 81 |
} |
| 82 |
|
| 83 |
public function getAutoOpenQuantity(): string { |
| 84 |
if ( ! empty( $this->overrides['auto_open_quantity'] ) ) { |
| 85 |
return $this->overrides['auto_open_quantity']; |
| 86 |
} |
| 87 |
|
| 88 |
return isset( $this->data['auto_open_quantity'] ) ? (string) $this->data['auto_open_quantity'] : ''; |
| 89 |
} |
| 90 |
|
| 91 |
public function isAdminEmailEnabled(): bool { |
| 92 |
return ! isset( $this->data['enable_admin_email'] ) || $this->data['enable_admin_email'] !== false; |
| 93 |
} |
| 94 |
|
| 95 |
public function isCustomerEmailEnabled(): bool { |
| 96 |
return ! isset( $this->data['enable_customer_email'] ) || $this->data['enable_customer_email'] !== false; |
| 97 |
} |
| 98 |
|
| 99 |
public function toArray(): array { |
| 100 |
$data = $this->data; |
| 101 |
|
| 102 |
$data['id'] = $this->getId(); |
| 103 |
$data['name'] = $this->getName(); |
| 104 |
$data['prompt_text'] = $this->getPromptText(); |
| 105 |
$data['success_action'] = $this->getSuccessAction(); |
| 106 |
$data['success_message'] = $this->getSuccessMessage(); |
| 107 |
$data['success_redirect_url'] = $this->getSuccessRedirectUrl(); |
| 108 |
$data['modal_title'] = $this->getModalTitle(); |
| 109 |
$data['display_position'] = $this->getDisplayPosition(); |
| 110 |
$data['hide_add_to_cart'] = $this->getHideAddToCart(); |
| 111 |
$data['show_for_non_purchasable'] = $this->getShowForNonPurchasable(); |
| 112 |
$data['integrated_label_text'] = $this->getIntegratedLabelText(); |
| 113 |
$data['submit_button_text'] = $this->getSubmitButtonText(); |
| 114 |
$data['auto_open_quantity'] = $this->getAutoOpenQuantity(); |
| 115 |
$data['enable_admin_email'] = $this->isAdminEmailEnabled(); |
| 116 |
$data['enable_customer_email'] = $this->isCustomerEmailEnabled(); |
| 117 |
|
| 118 |
return $data; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get a form by ID. |
| 123 |
* |
| 124 |
* @param string $id |
| 125 |
* |
| 126 |
* @return ?RequestQuoteForm |
| 127 |
*/ |
| 128 |
public static function get( string $id ): ?RequestQuoteForm { |
| 129 |
$forms = self::getAll(); |
| 130 |
foreach ( $forms as $form ) { |
| 131 |
if ( $form->getId() === $id ) { |
| 132 |
return $form; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get all forms. |
| 141 |
* |
| 142 |
* @return RequestQuoteForm[] |
| 143 |
*/ |
| 144 |
public static function getAll(): array { |
| 145 |
$rawForms = get_option( 'tier_pricing_table_quote_forms', array() ); |
| 146 |
$forms = array(); |
| 147 |
|
| 148 |
if ( ! is_array( $rawForms ) ) { |
| 149 |
return $forms; |
| 150 |
} |
| 151 |
|
| 152 |
foreach ( $rawForms as $rawForm ) { |
| 153 |
if ( is_array( $rawForm ) ) { |
| 154 |
$forms[] = new self( $rawForm ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return $forms; |
| 159 |
} |
| 160 |
} |
| 161 |
|