Feedback.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Basic\Feedback; |
| 4 | |
| 5 | use WP_User; |
| 6 | use WPStaging\Core\WPStaging; |
| 7 | use WPStaging\Notifications\Notifications; |
| 8 | |
| 9 | class Feedback |
| 10 | { |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | const WPSTG_FEEDBACK_EMAIL = "feedback@wp-staging.com"; |
| 15 | |
| 16 | /** |
| 17 | * Current page is plugins.php |
| 18 | * @global array $pagenow |
| 19 | * @return bool |
| 20 | */ |
| 21 | private function isPluginsPage(): bool |
| 22 | { |
| 23 | global $pagenow; |
| 24 | return ( $pagenow === 'plugins.php' ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Load feedback form |
| 29 | * @return void |
| 30 | */ |
| 31 | public function loadForm() |
| 32 | { |
| 33 | $screen = get_current_screen(); |
| 34 | if (!is_admin() && !$this->isPluginsPage()) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | $current_user = wp_get_current_user(); |
| 39 | if (!($current_user instanceof WP_User)) { |
| 40 | $email = ''; |
| 41 | } else { |
| 42 | $email = trim($current_user->user_email); |
| 43 | } |
| 44 | |
| 45 | $feedbackViewPath = WPSTG_VIEWS_DIR . 'feedback/deactivate-feedback.php'; |
| 46 | if (file_exists($feedbackViewPath)) { |
| 47 | include $feedbackViewPath; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return void |
| 53 | */ |
| 54 | public function sendDeactivateFeedback() |
| 55 | { |
| 56 | if (!empty($_POST['data'])) { |
| 57 | // phpcs:ignore |
| 58 | 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. |
| 59 | } |
| 60 | |
| 61 | $reasons = isset($form['wpstg_disable_reason']) ? (array)$form['wpstg_disable_reason'] : []; |
| 62 | |
| 63 | $body = ''; |
| 64 | $subject = []; |
| 65 | foreach ($reasons as $reason) { |
| 66 | $reasonText = ucwords(str_replace('_', ' ', sanitize_text_field($reason))); |
| 67 | $subject[] = $reasonText; |
| 68 | if (isset($form['wpstg_disable_text'][$reason])) { |
| 69 | $body .= $reasonText . ": " . sanitize_text_field($form['wpstg_disable_text'][$reason]) . "\n\r"; |
| 70 | } else { |
| 71 | $body .= $reasonText . "\n\r"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $message = empty($body) ? 'No reason given' : $body; |
| 76 | $from = isset($form['wpstg_disable_from']) ? sanitize_email($form['wpstg_disable_from']) : ''; |
| 77 | $subject = empty($subject) ? '(no reason given)' : (count($subject) > 1 ? '(multiple reasons given)' : $subject[0]); |
| 78 | $success = WPStaging::make(Notifications::class)->sendEmail(self::WPSTG_FEEDBACK_EMAIL, $subject, $message, $from, [], false); |
| 79 | |
| 80 | if ($success) { |
| 81 | wp_die('1'); |
| 82 | } |
| 83 | |
| 84 | wp_die('0'); |
| 85 | } |
| 86 | } |
| 87 |