route = esc_url( 'https://feedback.dragwyb.com/wp-json/wpfd/v1/feedback' );
$this->plugin_name = 'TranslateSense';
$this->plugin_slug = 'translatesense';
$this->plugin_version = defined( 'TRANSLATESENSE_VERSION' ) ? TRANSLATESENSE_VERSION : '1.0.0';
add_action( 'wp_ajax_ts_send_feedback', array( $this, 'ts_send_feedback' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles_and_scripts' ) );
add_action( 'admin_head', array( $this, 'display_plugin_installed_form' ) );
}
/**
* Display feedback form on Plugins screen.
*
* @return void
*/
public function display_plugin_installed_form() {
$screen = get_current_screen();
if (
! $screen ||
'plugins' !== $screen->id ||
! current_user_can( 'manage_options' )
) {
return;
}
$deactivation_options = array(
'plugin_performance_issues' => array(
'title' => __(
'Technical difficulties with the plugin',
'translatesense'
),
'input_placeholder' => __(
'Please describe the technical issues you encountered.',
'translatesense'
),
),
'alternative_plugin_discovered' => array(
'title' => __(
'Switched to another plugin',
'translatesense'
),
'input_placeholder' => __(
'Which plugin are you using instead?',
'translatesense'
),
),
'configuration_challenges' => array(
'title' => __(
'Difficulty with plugin configuration',
'translatesense'
),
'input_placeholder' => __(
'What configuration issue did you experience?',
'translatesense'
),
),
'temporary_plugin_pause' => array(
'title' => __(
'Temporarily disabling the plugin',
'translatesense'
),
'input_placeholder' => '',
),
'other_reasons' => array(
'title' => __(
'Other reason',
'translatesense'
),
'input_placeholder' => __(
'Please tell us why you are deactivating the plugin.',
'translatesense'
),
),
);
?>
id ||
! current_user_can( 'manage_options' )
) {
return;
}
wp_enqueue_style(
'ts-deactivate-styles',
plugin_dir_url( __FILE__ ) . 'assets/css/ts-feedback-form.min.css',
array(),
$this->plugin_version
);
wp_enqueue_script(
'ts-deactivate-scripts',
plugin_dir_url( __FILE__ ) . 'assets/js/ts-feedback-form.min.js',
array( 'jquery' ),
$this->plugin_version,
true
);
wp_localize_script(
'ts-deactivate-scripts',
'tsFeedbackData',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'plugin_slug' => $this->plugin_slug,
)
);
}
/**
* Process feedback.
*
* @return void
*/
public function ts_send_feedback() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error(
array(
'message' => __(
'You do not have permission to submit feedback.',
'translatesense'
),
),
403
);
}
check_ajax_referer(
'ts_send_feedback_nonce',
'nonce'
);
$allowed_reasons = array(
'plugin_performance_issues',
'alternative_plugin_discovered',
'configuration_challenges',
'temporary_plugin_pause',
'other_reasons',
);
$reason = isset( $_POST['reason'] )
? sanitize_key( wp_unslash( $_POST['reason'] ) )
: '';
if ( ! in_array( $reason, $allowed_reasons, true ) ) {
wp_send_json_error(
array(
'message' => __(
'Please select a valid feedback reason.',
'translatesense'
),
),
400
);
}
$message = isset( $_POST['message'] )
? sanitize_textarea_field(
wp_unslash( $_POST['message'] )
)
: '';
/*
* Optional technical-details consent.
*
* Only collect technical/site information when
* the user explicitly enables this option.
*/
$share_diagnostics = isset( $_POST['share_diagnostics'] )
&& '1' === sanitize_text_field(
wp_unslash( $_POST['share_diagnostics'] )
);
/*
* Always send basic feedback information.
*/
$this->feedback_data = array(
'plugin_name' => sanitize_text_field( $this->plugin_name ),
'plugin_slug' => sanitize_text_field( $this->plugin_slug ),
'plugin_version' => sanitize_text_field( $this->plugin_version ),
'deactive_reason' => $reason,
'message' => $message,
);
/*
* Only send technical/site information when
* the user explicitly checks the optional checkbox.
*/
if ( $share_diagnostics ) {
$this->feedback_data['email'] = sanitize_email(
get_option( 'admin_email' )
);
$this->feedback_data['website_url'] = esc_url_raw(
home_url()
);
$this->feedback_data['plugin_version'] = sanitize_text_field(
$this->plugin_version
);
$this->feedback_data['wp_version'] = sanitize_text_field(
get_bloginfo( 'version' )
);
$this->feedback_data['php_version'] = sanitize_text_field(
PHP_VERSION
);
}
$response = wp_remote_post(
$this->route,
array(
'timeout' => 15,
'body' => $this->feedback_data,
'headers' => array(
'Accept' => 'application/json',
),
)
);
if ( is_wp_error( $response ) ) {
wp_send_json_error(
array(
'message' => __(
'Unable to send feedback. Please try again.',
'translatesense'
),
),
500
);
}
$status_code = wp_remote_retrieve_response_code( $response );
if (
$status_code < 200 ||
$status_code >= 300
) {
wp_send_json_error(
array(
'message' => __(
'The feedback server could not process your request.',
'translatesense'
),
),
500
);
}
wp_send_json_success(
array(
'message' => __(
'Thank you for your feedback.',
'translatesense'
),
)
);
}
}
}