basename = plugin_basename( $plugin_file ); add_action( 'wp_ajax_' . self::AJAX_ACTION, [ $this, 'handle_ajax' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] ); add_action( 'admin_footer-plugins.php', [ $this, 'render_modal' ] ); } /** * Reasons shown in the modal. Keys are the canonical (untranslated) values sent to the API, * so the dashboard groups answers the same way whatever the admin's language. * * @return array */ private function get_reasons() { return [ "I couldn't get it to work" => [ 'label' => __( "I couldn't get it to work", 'aco-smart-ai-assistant' ), 'placeholder' => __( 'What went wrong? Any error message helps.', 'aco-smart-ai-assistant' ), ], 'I found a better plugin' => [ 'label' => __( 'I found a better plugin', 'aco-smart-ai-assistant' ), 'placeholder' => __( 'Which plugin did you switch to? What does it do better?', 'aco-smart-ai-assistant' ), ], "It's missing a feature I need" => [ 'label' => __( "It's missing a feature I need", 'aco-smart-ai-assistant' ), 'placeholder' => __( 'Which feature were you looking for?', 'aco-smart-ai-assistant' ), ], 'It broke my site or conflicts with another plugin' => [ 'label' => __( 'It broke my site or conflicts with another plugin', 'aco-smart-ai-assistant' ), 'placeholder' => __( 'What broke, or which plugin does it conflict with?', 'aco-smart-ai-assistant' ), ], 'I only needed it for a short time' => [ 'label' => __( 'I only needed it for a short time', 'aco-smart-ai-assistant' ), 'placeholder' => __( 'Thanks! Anything we could improve?', 'aco-smart-ai-assistant' ), ], 'Other' => [ 'label' => __( 'Other', 'aco-smart-ai-assistant' ), 'placeholder' => __( 'Please tell us a little more.', 'aco-smart-ai-assistant' ), ], ]; } public function handle_ajax() { check_ajax_referer( self::AJAX_ACTION, 'nonce' ); if ( ! current_user_can( 'activate_plugins' ) ) { wp_send_json_error( [ 'message' => 'forbidden' ], 403 ); } // phpcs:disable WordPress.Security.NonceVerification.Missing -- verified by check_ajax_referer() above. $reason = isset( $_POST['reason'] ) ? sanitize_text_field( wp_unslash( $_POST['reason'] ) ) : ''; $reason_details = isset( $_POST['reason_details'] ) ? sanitize_textarea_field( wp_unslash( $_POST['reason_details'] ) ) : ''; $consent = isset( $_POST['consent'] ) && rest_sanitize_boolean( wp_unslash( $_POST['consent'] ) ); $name = $consent && isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; $email = $consent && isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; // phpcs:enable // Only accept one of our own reasons. if ( ! array_key_exists( $reason, $this->get_reasons() ) ) { wp_send_json_error( [ 'message' => 'invalid_reason' ], 400 ); } Acowebs_Feedback_Client::send_deactivation( [ 'plugin' => self::PLUGIN_NAME, 'edition' => self::EDITION, 'plugin_version' => ACOSMAIA_VERSION, 'plugin_slug' => self::PLUGIN_SLUG, 'reason' => $reason, 'reason_details' => $reason_details, 'name' => $name, 'email' => $email, 'consent' => $consent, ] ); wp_send_json_success(); } public function enqueue( $hook ) { if ( 'plugins.php' !== $hook || ! current_user_can( 'activate_plugins' ) ) { return; } wp_enqueue_style( 'acosmaia-feedback-ui', ACOSMAIA_URL . 'assets/css/acowebs-feedback-ui.css', [], ACOSMAIA_VERSION ); wp_enqueue_script( 'acosmaia-deactivation', ACOSMAIA_URL . 'assets/js/deactivation-feedback.js', [], ACOSMAIA_VERSION, true ); wp_localize_script( 'acosmaia-deactivation', 'acosmaiaDeactivation', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'action' => self::AJAX_ACTION, 'nonce' => wp_create_nonce( self::AJAX_ACTION ), 'basename' => $this->basename, 'modalId' => 'acofb-' . self::PREFIX . '-deactivation', ] ); } public function render_modal() { if ( ! current_user_can( 'activate_plugins' ) ) { return; } $p = self::PREFIX; $plugin_name = self::PLUGIN_NAME; $user = wp_get_current_user(); $i = 0; ?>