admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE ), 'pluginBasename' => plugin_basename( AIVASTARK_WIDGET_PATH . 'aivastark-widget.php' ), 'email' => $current ? $current->user_email : '', 'reasons' => self::reasons(), 'i18n' => array( 'title' => __( 'Quick feedback before you go 🙏', 'aivastark-widget' ), 'subtitle' => __( 'Your feedback will help us improve Aivastark. If you have a minute, please tell us why you are deactivating:', 'aivastark-widget' ), 'emailLabel' => __( 'Your email', 'aivastark-widget' ), 'commentPlaceholder'=> __( 'Anything else you would like to share? (optional)', 'aivastark-widget' ), 'submit' => __( 'Submit & Deactivate', 'aivastark-widget' ), 'skip' => __( 'Skip & Deactivate', 'aivastark-widget' ), 'cancel' => __( 'Cancel', 'aivastark-widget' ), ), ) ); } /** * The preset reasons shown as radio options. `id` is stored; `label` is * shown. Keep ids stable — the admin dashboard groups on them. */ private static function reasons() { return array( array( 'id' => 'no_longer_need', 'label' => __( 'I no longer need the plugin', 'aivastark-widget' ), ), array( 'id' => 'found_better', 'label' => __( 'I found a better plugin', 'aivastark-widget' ), ), array( 'id' => 'not_working', 'label' => __( "The plugin isn't working / I had a problem", 'aivastark-widget' ), ), array( 'id' => 'temporary', 'label' => __( "It's a temporary deactivation", 'aivastark-widget' ), ), array( 'id' => 'other', 'label' => __( 'Other', 'aivastark-widget' ), ), ); } /** * admin-ajax handler: validate, gather context, forward to the edge * function. Always returns JSON — the JS proceeds to deactivate regardless. */ public static function handle_feedback() { check_ajax_referer( self::NONCE, 'nonce' ); if ( ! current_user_can( 'activate_plugins' ) ) { wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); } $reason = isset( $_POST['reason'] ) ? sanitize_text_field( wp_unslash( $_POST['reason'] ) ) : ''; $comment = isset( $_POST['comment'] ) ? sanitize_textarea_field( wp_unslash( $_POST['comment'] ) ) : ''; $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; // Nothing to record — let deactivation proceed without a call. if ( '' === $reason && '' === $comment ) { wp_send_json_success( array( 'skipped' => true ) ); } $body = array( 'reason' => $reason, 'comment' => $comment, 'email' => $email, 'site_url' => home_url(), 'plugin_version' => AIVASTARK_WIDGET_VERSION, 'wp_version' => get_bloginfo( 'version' ), 'widget_key' => trim( (string) Aivastark_Widget_Options::get( 'widget_key' ) ), ); $result = Aivastark_Widget_Api::post( 'wordpress-feedback', $body ); if ( is_wp_error( $result ) ) { // Best-effort: surface the error but don't block deactivation. wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'ok' => true ) ); } }