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