Feedback.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Feedback; |
| 4 | |
| 5 | use WP_User; |
| 6 | |
| 7 | /** |
| 8 | * @todo Move to src/Basic/Feedback/Feedback.php |
| 9 | */ |
| 10 | class Feedback |
| 11 | { |
| 12 | |
| 13 | /** |
| 14 | * Current page is plugins.php |
| 15 | * @global array $pagenow |
| 16 | * @return bool |
| 17 | */ |
| 18 | private function isPluginsPage(): bool |
| 19 | { |
| 20 | global $pagenow; |
| 21 | return ( $pagenow === 'plugins.php' ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Load feedback form |
| 26 | * @return void |
| 27 | */ |
| 28 | public function loadForm() |
| 29 | { |
| 30 | |
| 31 | $screen = get_current_screen(); |
| 32 | if (!is_admin() && !$this->isPluginsPage()) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | $current_user = wp_get_current_user(); |
| 37 | if (!($current_user instanceof WP_User)) { |
| 38 | $email = ''; |
| 39 | } else { |
| 40 | $email = trim($current_user->user_email); |
| 41 | } |
| 42 | |
| 43 | include WPSTG_PLUGIN_DIR . 'Backend/views/feedback/deactivate-feedback.php'; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return void |
| 48 | */ |
| 49 | public function sendMail() |
| 50 | { |
| 51 | |
| 52 | if (!empty($_POST['data'])) { |
| 53 | // phpcs:ignore |
| 54 | parse_str($_POST['data'], $form); // This is a js serialised string. It needs to be parsed first. It will be sanitised on the next lines after parsing it. |
| 55 | } |
| 56 | |
| 57 | $reasons = isset($form['wpstg_disable_reason']) ? (array)$form['wpstg_disable_reason'] : []; |
| 58 | |
| 59 | $body = ''; |
| 60 | $subject = []; |
| 61 | foreach ($reasons as $reason) { |
| 62 | $reasonText = ucwords(str_replace('_', ' ', sanitize_text_field($reason))); |
| 63 | $subject[] = $reasonText; |
| 64 | if (isset($form['wpstg_disable_text'][$reason])) { |
| 65 | $body .= $reasonText . ": " . sanitize_text_field($form['wpstg_disable_text'][$reason]) . "\n\r"; |
| 66 | } else { |
| 67 | $body .= $reasonText . "\n\r"; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $body = empty($body) ? 'No reason given' : $body; |
| 72 | |
| 73 | $headers = []; |
| 74 | |
| 75 | $from = isset($form['wpstg_disable_from']) ? sanitize_email($form['wpstg_disable_from']) : ''; |
| 76 | if ($from) { |
| 77 | $headers[] = "From: $from"; |
| 78 | $headers[] = "Reply-To: $from"; |
| 79 | } |
| 80 | |
| 81 | $subject = empty($subject) ? '(no reason given)' : (count($subject) > 1 ? '(multiple reasons given)' : $subject[0]); |
| 82 | $subject = 'WP Staging Free: ' . $subject; |
| 83 | $success = wp_mail('feedback@wp-staging.com', $subject, $body, $headers); |
| 84 | |
| 85 | if ($success) { |
| 86 | wp_die(1); |
| 87 | } |
| 88 | |
| 89 | wp_die(0); |
| 90 | } |
| 91 | } |
| 92 |