before release. * Sites can override it with the `acowebs_feedback_api_url` filter. */ if ( ! defined( 'ACOWEBS_FEEDBACK_API' ) ) { // Use https://audience.acowebs.com if the custom domain is live before release; both addresses work. define( 'ACOWEBS_FEEDBACK_API', 'https://audience.acowebs.com' ); } if ( ! class_exists( 'Acowebs_Feedback_Client' ) ) { /** * Sends deactivation feedback to the Acowebs feedback API. */ class Acowebs_Feedback_Client { /** * Version of this helper. Bump when you change it. */ const VERSION = '1.1.0'; /** * The API base URL, passed through the `acowebs_feedback_api_url` filter. * * @return string URL without a trailing slash. */ public static function api_url() { /** * Filters the Acowebs feedback API base URL. * * @param string $url Base URL, no trailing slash. */ $url = apply_filters( 'acowebs_feedback_api_url', ACOWEBS_FEEDBACK_API ); return untrailingslashit( (string) $url ); } /** * Information about this site, sent with every deactivation. * * @return array { * @type string $site_url Home URL. * @type string $site_name Site title. * @type string $wp_version WordPress version. * @type string|null $wc_version WooCommerce version, or null when WooCommerce is not active. * @type string $php_version PHP version. * @type string $site_language Locale, for example en_US. * } */ public static function site_info(): array { return array( 'site_url' => home_url(), 'site_name' => get_bloginfo( 'name' ), 'wp_version' => get_bloginfo( 'version' ), 'wc_version' => defined( 'WC_VERSION' ) ? WC_VERSION : null, 'php_version' => PHP_VERSION, 'site_language' => get_locale(), ); } /** * Sends deactivation feedback. Non-blocking, never throws. * * @param array $args { * Feedback details. They are merged over site_info(), so any site_info() key can be overridden. * * @type string $plugin Canonical plugin name. Free and PRO must send the same value. * @type string $edition 'free' or 'pro'. * @type string $plugin_version Plugin version. * @type string $plugin_slug Plugin slug, for example 'woo-custom-product-addons'. * @type string $reason Short reason text (the option the user picked). * @type string $reason_details Free-text details. * @type string $name Name the user typed (optional, may be empty). * @type string $email Email the user typed (optional, may be empty). * @type bool $consent True only if the user ticked "Acowebs may contact me". * @type array $meta Optional custom data, shown as "Meta fields" in the dashboard, e.g. * array( 'website_name' => 'Shop X', 'active_theme' => 'Astra' ). * Flat: at most 30 keys (letters, numbers, _ . -), string (max 1000 * characters), number, bool or null values, 4 KB in total. Sent only * when it is a non-empty array. An invalid meta makes the API reject * the whole feedback, so pass only values you control. * } * @return void */ public static function send_deactivation( array $args ): void { try { $args = wp_parse_args( $args, array( 'plugin' => '', 'edition' => 'free', 'plugin_version' => '', 'plugin_slug' => '', 'reason' => '', 'reason_details' => '', 'name' => '', 'email' => '', 'consent' => false, ) ); $payload = array_merge( self::site_info(), $args ); // Required by the API. Skip silently rather than send a record that would be rejected. if ( '' === trim( (string) $payload['plugin'] ) || '' === trim( (string) $payload['site_url'] ) ) { return; } $payload['edition'] = strtolower( trim( (string) $payload['edition'] ) ); $payload['consent'] = (bool) $payload['consent']; // Optional custom data: only a non-empty array, sent as a JSON object (an empty PHP array would be "[]"). if ( isset( $payload['meta'] ) && is_array( $payload['meta'] ) && ! empty( $payload['meta'] ) ) { $payload['meta'] = (object) $payload['meta']; } else { unset( $payload['meta'] ); } // The API rejects null for text fields (for example wc_version without WooCommerce). Leave them out. $payload = array_filter( $payload, static function ( $value ) { return null !== $value; } ); wp_remote_post( self::api_url() . '/v1/deactivation', array( 'method' => 'POST', 'timeout' => 5, 'blocking' => false, // Fire and forget: never wait for the API. 'redirection' => 0, 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $payload ), ) ); } catch ( \Throwable $e ) { // Feedback must never break deactivation. Ignore everything. return; } } } } /* * ===================================================================================== * USAGE EXAMPLE (copy into your plugin and rename the `myplugin` prefix) * ===================================================================================== * * This is for a plugin whose own deactivation modal posts to admin-ajax.php. * * ---- 1. PHP: constants (main plugin file) ---------------------------------------------- * * define( 'MYPLUGIN_NAME', 'Product Addons for WooCommerce' ); // Canonical name, same in Free and PRO. * define( 'MYPLUGIN_SLUG', 'woo-custom-product-addons' ); * define( 'MYPLUGIN_EDITION', 'free' ); // 'free' or 'pro'. * define( 'MYPLUGIN_VERSION', '6.4.2' ); * * ---- 2. PHP: the AJAX handler ----------------------------------------------------------- * * add_action( 'wp_ajax_myplugin_deactivation_feedback', 'myplugin_deactivation_feedback' ); * * function myplugin_deactivation_feedback() { * // 1. Verify the nonce (dies with a 403 if invalid). * check_ajax_referer( 'myplugin_deactivation_feedback', 'nonce' ); * * // 2. Only users who can deactivate plugins. * if ( ! current_user_can( 'activate_plugins' ) ) { * wp_send_json_error( array( 'message' => 'forbidden' ), 403 ); * } * * // 3. Sanitize everything that came from the browser. * $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'] ) ) : ''; * $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; * $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; * $consent = isset( $_POST['consent'] ) && rest_sanitize_boolean( wp_unslash( $_POST['consent'] ) ); * * // 4. Send it. Non-blocking, never throws. * Acowebs_Feedback_Client::send_deactivation( * array( * 'plugin' => MYPLUGIN_NAME, * 'edition' => MYPLUGIN_EDITION, * 'plugin_version' => MYPLUGIN_VERSION, * 'plugin_slug' => MYPLUGIN_SLUG, * 'reason' => $reason, * 'reason_details' => $reason_details, * 'name' => $name, * 'email' => $email, * 'consent' => $consent, * // Optional custom data, shown as "Meta fields" in the dashboard: * // 'meta' => array( 'website_name' => get_bloginfo( 'name' ) ), * ) * ); * * wp_send_json_success(); * } * * ---- 3. PHP: pass the nonce to the modal's script (plugins.php only) -------------------- * * add_action( 'admin_enqueue_scripts', 'myplugin_enqueue_deactivation_modal' ); * * function myplugin_enqueue_deactivation_modal( $hook ) { * if ( 'plugins.php' !== $hook ) { * return; * } * wp_enqueue_script( 'myplugin-deactivation', plugins_url( 'assets/deactivation.js', __FILE__ ), array(), MYPLUGIN_VERSION, true ); * wp_localize_script( * 'myplugin-deactivation', * 'mypluginDeactivation', * array( * 'ajaxUrl' => admin_url( 'admin-ajax.php' ), * 'nonce' => wp_create_nonce( 'myplugin_deactivation_feedback' ), * ) * ); * } * * ---- 4. JS: the modal's submit handler (assets/deactivation.js) ------------------------- * * // `deactivateUrl` is the href of the plugin's "Deactivate" link on plugins.php. * // `form` is the modal's form with fields: reason, reason_details, name, email, consent. * function submitFeedbackAndDeactivate( form, deactivateUrl ) { * var body = new URLSearchParams(); * body.append( 'action', 'myplugin_deactivation_feedback' ); * body.append( 'nonce', mypluginDeactivation.nonce ); * body.append( 'reason', form.reason.value ); * body.append( 'reason_details', form.reason_details.value ); * body.append( 'name', form.name.value ); * body.append( 'email', form.email.value ); * body.append( 'consent', form.consent.checked ? '1' : '0' ); // Ticked by default; the user may untick it. * * var done = false; * function deactivate() { * if ( done ) { return; } * done = true; * window.location.href = deactivateUrl; // Never block deactivation. * } * * // Follow the deactivate link when the request finishes, fails, or after 3 seconds, * // whichever comes first. * setTimeout( deactivate, 3000 ); * fetch( mypluginDeactivation.ajaxUrl, { method: 'POST', credentials: 'same-origin', body: body } ) * .then( deactivate, deactivate ); * } * * The modal's checkbox label should read "Acowebs can contact me about this feedback". It is ticked * by default (name and email pre-filled); the user can untick it to send anonymously, in which case * leave out name and email. If the user clicks "Skip & deactivate", send nothing. Send at most once * per deactivation. The Acowebs UI kit (docs/ui-kit/) has the matching markup and CSS. */