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