api_site_url = untrailingslashit( $api_site ); $this->slug = sanitize_title( $slug ); $this->type = $type === 'theme' ? 'theme' : 'plugin'; $this->version = $version; $default_config = [ 'logo' => '', // default logo URL 'optin_message' => 'Help improve ' . esc_html( $this->slug ) . '! Allow anonymous usage tracking?', 'deactivation_message' => 'If you have a moment, please share why you are deactivating:', 'deactivation_reasons' => [ 'no_longer_needed' => [ 'label' => 'I no longer need the plugin', ], 'found_a_better_plugin' => [ 'label' => 'I found a better plugin', 'has_custom_reason' => true, 'custom_reason_placeholder' => 'Please share which plugin', ], 'couldnt_get_the_plugin_to_work' => [ 'label' => 'I couldn\'t get the plugin to work', ], 'temporary_deactivation' => [ 'label' => 'It\'s a temporary deactivation', ], 'other' => [ 'label' => 'Other', 'has_custom_reason' => true, 'custom_reason_placeholder' => 'Please share the reason', ], ], ]; $this->config = wp_parse_args( $config, $default_config ); add_action( 'admin_notices', [ $this, 'maybe_show_optin' ] ); add_action( 'admin_init', [ $this, 'handle_optin_click' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'inject_deactivate_feedback' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_inline_css' ] ); add_action( 'wp_ajax_insights_deactivate_send', function () { if ( ! wp_verify_nonce( sanitize_text_field( $_POST['security'] ), $this->slug . '-insights-deactivate-nonce' ) ) { wp_send_json_error( 'Invalid nonce.' ); } $slug = sanitize_text_field( $_POST['slug'] ); $reason_key = sanitize_text_field( $_POST['reason_key'] ); $reason = sanitize_text_field( $_POST['reason'] ); $instance = self::$instances[ $slug ] ?? null; if ( ! $instance || empty( $slug ) ) { wp_send_json_error(); } $instance->send_to_server( 'deactivated', [ 'deactivation_reason_type' => $reason_key, 'deactivation_reason' => $reason, 'deactivation_time' => current_time( 'mysql' ), ] ); $instance->mark_sent( 'deactivated' ); wp_send_json_success(); } ); add_action( 'wp_ajax_insights_optin', function () { if ( ! wp_verify_nonce( sanitize_text_field( $_POST['security'] ), $this->slug . '_nonce' ) ) { wp_send_json_error( 'Invalid nonce.' ); } $slug = sanitize_text_field( $_POST['slug'] ); $instance = self::$instances[ $slug ] ?? null; if ( ! $instance || empty( $slug ) ) { wp_send_json_error(); } $instance->send_to_server( 'optin', [ 'optin_status' => true, 'optin_time' => current_time( 'mysql' ), ] ); $instance->mark_sent( 'optin' ); wp_send_json_success(); } ); } public static function init( $api_site, $slug, $type = 'plugin', $version = null, $config = [] ) { if ( ! isset( self::$instances[ $slug ] ) ) { self::$instances[ $slug ] = new self( $api_site, $slug, $type, $version, $config ); } } public function maybe_show_optin() { $screen = get_current_screen(); if ( $screen && (in_array( $screen->base, [ 'post', 'post-new' ], true ) || $screen->is_block_editor()) ) { return; } if ( $this->is_already_sent( 'optin' ) ) { return; } $install_time = get_option('ablocks_first_install_time'); $current_time = \ABlocks\Helper::get_time(); $three_days = 3 * DAY_IN_SECONDS; if ( ! $install_time || ! is_numeric( $install_time ) ) { return; } if ( ( $current_time - $install_time ) < $three_days ) { return; } $optin_key = 'insights_optin_' . $this->slug; $nonce = wp_create_nonce( 'insights_optin_' . $this->slug ); $optin_url = esc_url( add_query_arg( [ $optin_key => 'yes', 'security' => $nonce ] ) ); $nooptin_url = esc_url( add_query_arg( [ $optin_key => 'no', 'security' => $nonce ] ) ); echo '
logo

Connect with over 2,000+
Web Design professionals.

Join the community

💌 Help Us Improve & Get Exclusive Perks!

We’d love to stay in touch and share helpful updates, tips, and special offers to make the most of aBlocks. Your privacy is our priority. No spam — ever.

What we collect if you opt in?

We collect your site URL, plugin version, WordPress version, PHP version, and admin email to better tailor our updates and improve plugin performance.

No sensitive or personal data is collected. You can unsubscribe at any time.

Yes, keep me updated No thanks

'; } public function handle_optin_click() { $key = 'insights_optin_' . $this->slug; if ( isset( $_GET[ $key ] ) && wp_verify_nonce( sanitize_text_field( $_GET['security'] ), 'insights_optin_' . $this->slug ) ) { $optin = $_GET[ $key ] === 'yes' ? 1 : 0; if($optin){ $this->send_to_server( 'optin', [ 'optin_status' => (int) $optin, 'optin_time' => current_time( 'mysql' ), ] ); } $this->mark_sent( 'optin' ); wp_safe_redirect( remove_query_arg( $key ) ); exit; } } public function inject_deactivate_feedback( $hook ) { if ( $hook !== 'plugins.php' || $this->is_already_sent( 'deactivated' ) ) { return; } wp_enqueue_script( 'jquery-ui-dialog' ); wp_enqueue_style( 'jquery-ui-dialog' ); add_action( 'admin_footer', function () { $slug = esc_js( $this->slug ); $ajax_url = esc_url( admin_url( 'admin-ajax.php?action=insights_deactivate_send' ) ); ?> is_already_sent( $track_type ) ) { return; } $data = [ 'track_type' => $track_type, 'project_type' => $this->type, 'project_slug' => $this->slug, 'site_url' => site_url(), 'admin_email' => get_option( 'admin_email' ), 'wp_version' => get_bloginfo( 'version' ), 'php_version' => PHP_VERSION, 'project_version' => $this->version, 'deactivation_reason_type' => '', 'deactivation_reason' => '', 'deactivation_time' => current_time( 'mysql' ), ]; if ( $track_type === 'optin' ) { $data['optin_status'] = ''; $data['optin_time'] = current_time( 'mysql' ); unset( $data['deactivation_reason_type'] ); unset( $data['deactivation_reason'] ); unset( $data['deactivation_time'] ); } $payload = \wp_parse_args( $extra_data, $data ); $api_url = trailingslashit( $this->api_site_url ) . 'wp-json/insightsforwp/v1/track'; $response = wp_remote_post( $api_url, [ 'timeout' => 10, 'body' => $payload, 'sslverify' => false ] ); if ( is_wp_error( $response ) ) { // Handle error. $error_message = $response->get_error_message(); // You can log this or return an error response. error_log( "Request failed: $error_message" ); } } private function is_already_sent( $type ) { $sent = get_option( 'insightsforwp_sent_data', [] ); return ! empty( $sent[ $this->slug ][ $type ] ); } private function mark_sent( $type ) { $sent = get_option( 'insightsforwp_sent_data', [] ); $sent[ $this->slug ][ $type ] = true; update_option( 'insightsforwp_sent_data', $sent, false ); } public function enqueue_inline_css() { if ( $this->is_already_sent( 'deactivated' ) ) { return; } $css = ' .ui-widget-overlay { position: fixed; top: 0; left: 0; width: 100% !important; height: 100% !important; background: rgba(0, 0, 0, 0.5); z-index: 1000; } .insights-deactivate-dialog { display: flex; z-index: 99999; } {{wrapper}} .insights-deactivate-dialog__header { padding: 18px 15px; box-shadow: 0 0 8px rgba(0, 0, 0, 0.1); text-align: start; display: flex; } {{wrapper}} .insights-deactivate-dialog__header span { font-size: 15px; text-transform: uppercase; font-weight: bold; padding-inline-start: 5px; color: #515962; } {{wrapper}} .insights-deactivate-dialog__description { font-size: 15px; line-height: 1.4; font-weight: bold; color: #515962; } {{wrapper}} { height: auto; background-color: #ffffff; border-radius: 3px; box-shadow: 2px 8px 23px 3px rgba(0, 0, 0, 0.2); overflow: hidden; } {{wrapper}} .insights-deactivate-form { padding: 30px; text-align: start; } {{wrapper}} input[type="radio"] { margin-block: 0; margin-inline: 0 10px; box-shadow: none; } {{wrapper}} label { display: block; font-size: 13px; color: #515962; } {{wrapper}} ul { padding-block-start: 30px; padding-block-end: 15px; } {{wrapper}} .insights-deactivate-form-reason-message { display: none; } {{wrapper}} .insights-deactivate-form-reason-message input { width: 92%; margin-left: 25px; background-color: transparent; color: #515962; padding: 5px; box-shadow: none; } {{wrapper}} .insights-deactivate-form__control-button { display: flex; justify-content: space-between; } {{wrapper}} .insights-deactivate-form__control-button button { font-size: 12px; font-weight: 500; line-height: 1.2; padding: 8px 16px; outline: none; border: none; border-radius: 4px; background-color: white; color: #0c0d0e; transition: all .3s; cursor: pointer; } {{wrapper}} .insights-deactivate-form__control-button button[type="submit"] { background: #13191B; color: #fff; } {{wrapper}} .insights-deactivate-form__control-button button[type="button"] { background: transparent; color: #c4c4c5; } '; wp_add_inline_style( 'wp-admin', str_replace( '{{wrapper}}', '#insights-deactivate-dialog-' . $this->slug, $css ) ); } }