| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC; |
| 4 |
|
| 5 |
use ISC\User; |
| 6 |
use WP_User; |
| 7 |
|
| 8 |
/** |
| 9 |
* Handle feedback voluntarily sent on deactivation |
| 10 |
*/ |
| 11 |
class Feedback { |
| 12 |
|
| 13 |
/** |
| 14 |
* Register hooks |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 20 |
add_filter( 'admin_footer', [ $this, 'add_deactivation_popup' ] ); |
| 21 |
add_action( 'wp_ajax_isc_send_feedback', [ $this, 'send_feedback' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Check if we are on the Plugins page |
| 26 |
* |
| 27 |
* @return bool |
| 28 |
*/ |
| 29 |
private function is_plugins_page(): bool { |
| 30 |
$screen = get_current_screen(); |
| 31 |
|
| 32 |
return isset( $screen->id ) && in_array( $screen->id, [ 'plugins', 'plugins-network' ], true ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue scripts |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function enqueue_scripts(): void { |
| 41 |
if ( ! $this->is_plugins_page() ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
Helpers::enqueue_script( 'isc-feedback', 'admin/assets/js/feedback.js' ); |
| 46 |
wp_enqueue_style( 'isc-feedback', ISCBASEURL . 'admin/assets/css/feedback.css', [], ISCVERSION ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Display deactivation modal on the Plugins page |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function add_deactivation_popup(): void { |
| 55 |
if ( ! $this->is_plugins_page() ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$from = ''; |
| 60 |
$email = User::get_email(); |
| 61 |
if ( $email ) { |
| 62 |
$from = sprintf( '%1$s <%2$s>', User::get_name(), sanitize_email( $email ) ); |
| 63 |
} |
| 64 |
|
| 65 |
include ISCPATH . 'admin/templates/feedback.php'; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Send feedback via email |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function send_feedback() { |
| 74 |
// phpcs:ignore |
| 75 |
if ( ! array_key_exists( 'isc-feedback-form-nonce', $_POST ) || ! wp_verify_nonce( $_POST['isc-feedback-form-nonce'], 'isc-feedback-form' ) ) { |
| 76 |
wp_die(); |
| 77 |
} |
| 78 |
|
| 79 |
$data = $this->prepare_feedback_data( $_POST ); |
| 80 |
|
| 81 |
$from = $data['from']; |
| 82 |
$subject = 'Image Source Control Feedback'; |
| 83 |
$text = $data['text']; |
| 84 |
$text .= "\n\n" . home_url() . ' (' . $data['activated'] . ')'; |
| 85 |
|
| 86 |
// The user sent feedback with a reply request |
| 87 |
if ( |
| 88 |
! empty( $_POST['isc-feedback-send-reply'] ) |
| 89 |
) { |
| 90 |
$name = User::get_name(); |
| 91 |
$email = $data['email']; |
| 92 |
$from = $name . ' <' . $email . '>'; |
| 93 |
$text .= "\n\n" . 'Feedback: ✓'; |
| 94 |
} |
| 95 |
|
| 96 |
if ( class_exists( 'ISC_Pro_Admin', false ) ) { |
| 97 |
$text .= "\n\n" . 'Pro: ✓'; |
| 98 |
} |
| 99 |
|
| 100 |
$headers[] = "From: $from"; |
| 101 |
$headers[] = "Reply-To: $from"; |
| 102 |
|
| 103 |
$to = User::has_german_backend() ? 'support@imagesourcecontrol.de' : 'support@imagesourcecontrol.com'; |
| 104 |
|
| 105 |
wp_mail( $to, $subject, $text, $headers ); |
| 106 |
die(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Prepare feedback data |
| 111 |
* |
| 112 |
* @param string[] $data Submitted feedback data. |
| 113 |
* |
| 114 |
* @return string[] |
| 115 |
*/ |
| 116 |
private function prepare_feedback_data( array $data ): array { |
| 117 |
$feedback = [ |
| 118 |
'from' => $data['isc-feedback-from'] ?? '', |
| 119 |
'text' => isset( $data['isc-feedback-text'] ) ? sanitize_text_field( $data['isc-feedback-text'] ) : '', |
| 120 |
]; |
| 121 |
|
| 122 |
$feedback['email'] = ! array_key_exists( 'isc-feedback-reply-email', $data ) || ! is_email( $data['isc-feedback-reply-email'] ) ? '' : trim( $data['isc-feedback-reply-email'] ); |
| 123 |
|
| 124 |
$options = Plugin::get_options(); |
| 125 |
$feedback['activated'] = isset( $options['activated'] ) ? gmdate( 'd.m.Y', $options['activated'] ) : '–'; |
| 126 |
|
| 127 |
return $feedback; |
| 128 |
} |
| 129 |
} |
| 130 |
|