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