| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class SupportForm |
| 5 |
* |
| 6 |
* This class handles the support form functionality. |
| 7 |
* |
| 8 |
* @ackage WowPlugin |
| 9 |
* @subpackage Admin |
| 10 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 11 |
* @copyright 2024 Dmytro Lobov |
| 12 |
* @license GPL-2.0+ |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace FloatMenuLite\Admin; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
use FloatMenuLite\WOWP_Plugin; |
| 20 |
|
| 21 |
class SupportForm { |
| 22 |
|
| 23 |
public static function init(): void { |
| 24 |
|
| 25 |
$plugin = WOWP_Plugin::info( 'name' ) . ' v.' . WOWP_Plugin::info( 'version' ); |
| 26 |
$license = get_option( 'wow_license_key_' . WOWP_Plugin::PREFIX, 'no' ); |
| 27 |
|
| 28 |
self::send(); |
| 29 |
|
| 30 |
?> |
| 31 |
|
| 32 |
<form method="post"> |
| 33 |
|
| 34 |
<fieldset class="wpie-fieldset"> |
| 35 |
<legend> |
| 36 |
<?php esc_html_e( 'Support Form', 'float-menu' ); ?> |
| 37 |
</legend> |
| 38 |
|
| 39 |
<div class="wpie-fields is-column-2"> |
| 40 |
<div class="wpie-field"> |
| 41 |
<div class="wpie-field__title"><?php esc_html_e( 'Your Name', 'float-menu' ); ?></div> |
| 42 |
<label class="wpie-field__label has-icon"> |
| 43 |
<span class="wpie-icon wpie_icon-user"></span> |
| 44 |
<input type="text" name="support[name]" id="support-name" value=""> |
| 45 |
</label> |
| 46 |
</div> |
| 47 |
|
| 48 |
<div class="wpie-field"> |
| 49 |
<div class="wpie-field__title"><?php esc_html_e( 'Contact email', 'float-menu' ); ?></div> |
| 50 |
<label class="wpie-field__label has-icon"> |
| 51 |
<span class="wpie-icon wpie_icon-at-sign"></span> |
| 52 |
<input type="email" name="support[email]" id="support-email" |
| 53 |
value="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>"> |
| 54 |
</label> |
| 55 |
</div> |
| 56 |
</div> |
| 57 |
|
| 58 |
<div class="wpie-fields is-column-2"> |
| 59 |
|
| 60 |
<div class="wpie-field"> |
| 61 |
<div class="wpie-field__title"><?php esc_html_e( 'Link to the issue', 'float-menu' ); ?></div> |
| 62 |
<label class="wpie-field__label has-icon"> |
| 63 |
<span class="wpie-icon wpie_icon-link"></span> |
| 64 |
<input type="url" name="support[link]" id="support-link" |
| 65 |
value="<?php echo esc_url( get_option( 'home' ) ); ?>"> |
| 66 |
</label> |
| 67 |
</div> |
| 68 |
|
| 69 |
<div class="wpie-field" data-field-box="menu_open"> |
| 70 |
<div class="wpie-field__title"><?php esc_html_e( 'Message type', 'float-menu' ); ?></div> |
| 71 |
<label class="wpie-field__label has-icon"> |
| 72 |
<span class="wpie-icon wpie_icon-check"></span> |
| 73 |
<select name="support[type]" id="support-type"> |
| 74 |
<option value="Issue"><?php esc_html_e( 'Issue', 'float-menu' ); ?></option> |
| 75 |
<option value="Idea"><?php esc_html_e( 'Idea', 'float-menu' ); ?></option> |
| 76 |
</select> |
| 77 |
</label> |
| 78 |
</div> |
| 79 |
|
| 80 |
</div> |
| 81 |
<div class="wpie-fields is-column-2"> |
| 82 |
<div class="wpie-field"> |
| 83 |
<div class="wpie-field__title"><?php esc_html_e( 'Plugin', 'float-menu' ); ?></div> |
| 84 |
<label class="wpie-field__label has-icon"> |
| 85 |
<span class="wpie-icon wpie_icon-plug"></span> |
| 86 |
<input type="text" readonly name="support[plugin]" id="support-plugin" |
| 87 |
value="<?php echo esc_attr( $plugin ); ?>"> |
| 88 |
</label> |
| 89 |
</div> |
| 90 |
|
| 91 |
<div class="wpie-field"> |
| 92 |
<div class="wpie-field__title"><?php esc_html_e( 'License Key', 'float-menu' ); ?></div> |
| 93 |
<label class="wpie-field__label has-icon"> |
| 94 |
<span class="wpie-icon wpie_icon-key"></span> |
| 95 |
<input type="text" readonly name="support[license]" id="support-license" |
| 96 |
value="<?php echo esc_attr( $license ); ?>"> |
| 97 |
</label> |
| 98 |
</div> |
| 99 |
|
| 100 |
</div> |
| 101 |
<div class="wpie-fields is-column"> |
| 102 |
<?php |
| 103 |
$content = esc_attr__( 'Enter Your Message', 'float-menu' ); |
| 104 |
$editor_id = 'support-message'; |
| 105 |
$settings = array( |
| 106 |
'textarea_name' => 'support[message]', |
| 107 |
); |
| 108 |
wp_editor( $content, $editor_id, $settings ); ?> |
| 109 |
</div> |
| 110 |
|
| 111 |
<div class="wpie-fields is-column"> |
| 112 |
|
| 113 |
<div class="wpie-field"> |
| 114 |
<?php submit_button( __( 'Send to Support', 'float-menu' ), 'primary', 'submit', false ); ?> |
| 115 |
</div> |
| 116 |
|
| 117 |
</div> |
| 118 |
|
| 119 |
<?php wp_nonce_field( WOWP_Plugin::PREFIX . '_nonce_action', WOWP_Plugin::PREFIX . '_nonce_name' ); ?> |
| 120 |
</fieldset> |
| 121 |
|
| 122 |
|
| 123 |
</form> |
| 124 |
|
| 125 |
<?php |
| 126 |
|
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
private static function send(): void { |
| 131 |
if ( ! self::verify() ) { |
| 132 |
|
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
$error = self::error(); |
| 138 |
if ( ! empty( $error ) ) { |
| 139 |
echo '<p class="notice notice-error">' . esc_html( $error ) . '</p>'; |
| 140 |
|
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$support = $_POST['support']; |
| 145 |
|
| 146 |
$headers = array( |
| 147 |
'From: ' . esc_attr( $support['name'] ) . ' <' . sanitize_email( $support['email'] ) . '>', |
| 148 |
'content-type: text/html', |
| 149 |
); |
| 150 |
|
| 151 |
|
| 152 |
$message_mail = '<html> |
| 153 |
<head></head> |
| 154 |
<body> |
| 155 |
<table> |
| 156 |
<tr> |
| 157 |
<td><strong>License Key:</strong></td> |
| 158 |
<td>' . esc_attr( $support['license'] ) . '</td> |
| 159 |
</tr> |
| 160 |
<tr> |
| 161 |
<td><strong>Plugin:</strong></td> |
| 162 |
<td>' . esc_attr( $support['plugin'] ) . '</td> |
| 163 |
</tr> |
| 164 |
<tr> |
| 165 |
<td><strong>Website:</strong></td> |
| 166 |
<td><a href="' . esc_url( $support['link'] ) . '" target="_blank">' . esc_url( $support['link'] ) . '</a></td> |
| 167 |
</tr> |
| 168 |
</table> |
| 169 |
<p/> |
| 170 |
' . nl2br( wp_kses_post( $support['message'] ) ) . ' |
| 171 |
</body> |
| 172 |
</html>'; |
| 173 |
$type = sanitize_text_field( $support['type'] ); |
| 174 |
$to_mail = WOWP_Plugin::info( 'email' ); |
| 175 |
$send = wp_mail( $to_mail, 'Support Request: ' . $type, $message_mail, $headers ); |
| 176 |
|
| 177 |
if ( $send ) { |
| 178 |
$text = __( 'Your message has been sent to the support team.', 'float-menu' ); |
| 179 |
echo '<p class="notice notice-success">' . esc_html( $text ) . '</p>'; |
| 180 |
} else { |
| 181 |
$text = __( 'Sorry, but message did not send. Please, contact us via support page.', 'float-menu' ); |
| 182 |
echo '<p class="notice notice-error">' . esc_html( $text ) . '</p>'; |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
private static function error(): ?string { |
| 188 |
if ( ! self::verify() ) { |
| 189 |
return ''; |
| 190 |
} |
| 191 |
$support = $_POST['support']; |
| 192 |
$fields = [ 'name', 'email', 'link', 'type', 'plugin', 'license', 'message' ]; |
| 193 |
|
| 194 |
foreach ( $fields as $field ) { |
| 195 |
if ( empty( $support[ $field ] ) ) { |
| 196 |
return __( 'Please fill in all the form fields below.', 'float-menu' ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return ''; |
| 201 |
} |
| 202 |
|
| 203 |
private static function verify(): bool { |
| 204 |
$support = $_POST['support'] ?? []; |
| 205 |
$nonce_name = WOWP_Plugin::PREFIX . '_nonce_name'; |
| 206 |
$nonce_action = WOWP_Plugin::PREFIX . '_nonce_action'; |
| 207 |
|
| 208 |
return ! empty( $support ) && wp_verify_nonce( $_POST[ $nonce_name ], $nonce_action ); |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|