Changeset 3828439
- Timestamp:
- 11/26/2026 11:22:09 PM (73 minutes ago)
- Location:
- churnanchor
- Files:
-
- 24 edited
- 1 copied
-
tags/0.2.4 (copied) (copied from churnanchor/trunk )
-
tags/0.2.4/assets/admin.css (modified) (1 diff)
-
tags/0.2.4/churnanchor.php (modified) (2 diffs)
-
tags/0.2.4/includes/class-admin.php (modified) (4 diffs)
-
tags/0.2.4/includes/class-cancel-flow.php (modified) (7 diffs)
-
tags/0.2.4/includes/class-db.php (modified) (5 diffs)
-
tags/0.2.4/includes/class-offers.php (modified) (2 diffs)
-
tags/0.2.4/includes/class-plugin.php (modified) (1 diff)
-
tags/0.2.4/includes/class-privacy.php (modified) (1 diff)
-
tags/0.2.4/includes/class-saves.php (modified) (2 diffs)
-
tags/0.2.4/readme.txt (modified) (2 diffs)
-
tags/0.2.4/templates/cancel-flow.php (modified) (1 diff)
-
tags/0.2.4/uninstall.php (modified) (1 diff)
-
trunk/assets/admin.css (modified) (1 diff)
-
trunk/churnanchor.php (modified) (2 diffs)
-
trunk/includes/class-admin.php (modified) (4 diffs)
-
trunk/includes/class-cancel-flow.php (modified) (7 diffs)
-
trunk/includes/class-db.php (modified) (5 diffs)
-
trunk/includes/class-offers.php (modified) (2 diffs)
-
trunk/includes/class-plugin.php (modified) (1 diff)
-
trunk/includes/class-privacy.php (modified) (1 diff)
-
trunk/includes/class-saves.php (modified) (2 diffs)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/templates/cancel-flow.php (modified) (1 diff)
-
trunk/uninstall.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
churnanchor/tags/0.2.4/assets/admin.css
r3828412 r3828439 48 48 } 49 49 @media (forced-colors: active) { .churnanchor-admin .ca-bar-fill { background: Highlight; } } 50 51 .churnanchor-admin .ca-comments{margin:0;padding:0;list-style:none} 52 .churnanchor-admin .ca-comments li{padding:12px 0;border-top:1px solid #e2e4e7} 53 .churnanchor-admin .ca-comments li:first-child{border-top:0;padding-top:0} 54 .churnanchor-admin .ca-comment-text{margin:0 0 4px;font-size:14px;white-space:pre-line} 55 .churnanchor-admin .ca-comments .ca-muted{margin:0} -
churnanchor/tags/0.2.4/churnanchor.php
r3828412 r3828439 4 4 * Plugin URI: https://churnanchor.marul.co/ 5 5 * Description: A fair, dark-pattern-free cancellation flow for WooCommerce Subscriptions: ask why, offer a skip or a pause, and track the revenue you actually keep. 6 * Version: 0.2. 36 * Version: 0.2.4 7 7 * Requires at least: 6.5 8 8 * Requires PHP: 7.4 … … 22 22 defined( 'ABSPATH' ) || exit; 23 23 24 define( 'CHURNANCHOR_VERSION', '0.2. 3' );24 define( 'CHURNANCHOR_VERSION', '0.2.4' ); 25 25 define( 'CHURNANCHOR_FILE', __FILE__ ); 26 26 define( 'CHURNANCHOR_DIR', plugin_dir_path( __FILE__ ) ); -
churnanchor/tags/0.2.4/includes/class-admin.php
r3828412 r3828439 60 60 */ 61 61 private function money( $cents, $currency ) { 62 if ( ! function_exists( 'wc_price' ) ) { 63 return number_format_i18n( $cents / 100, 2 ) . ' ' . $currency; 64 } 62 65 return wp_strip_all_tags( wc_price( $cents / 100, array( 'currency' => $currency ) ) ); 66 } 67 68 /** 69 * Names of enabled subscription payment methods that cannot change payment dates. 70 * 71 * @return string[] 72 */ 73 private static function gateways_without_date_changes() { 74 if ( ! function_exists( 'WC' ) || ! WC()->payment_gateways() ) { 75 return array(); 76 } 77 $names = array(); 78 foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) { 79 if ( 'yes' === $gateway->enabled && $gateway->supports( 'subscriptions' ) && ! $gateway->supports( 'subscription_date_changes' ) ) { 80 $names[] = wp_strip_all_tags( $gateway->get_title() ); 81 } 82 } 83 return $names; 63 84 } 64 85 … … 102 123 } 103 124 $total_r = (int) $stats['reasons_total']; 125 $comments = DB::recent_comments( gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ) ); 126 $no_dates = self::gateways_without_date_changes(); 104 127 $is_empty = 0 === $stats['attempts'] && 0 === $stats['accepted'] && 0 === $stats['cancelled'] && empty( $stats['money'] ); 105 128 $base_url = admin_url( 'admin.php?page=' . self::SLUG ); … … 126 149 <?php if ( ! Plugin::subscriptions_available() ) : ?> 127 150 <div class="notice notice-warning inline"><p><?php esc_html_e( 'WooCommerce Subscriptions was not detected. Activate it to use the cancellation flow.', 'churnanchor' ); ?></p></div> 151 <?php endif; ?> 152 153 <?php if ( $no_dates && Plugin::subscriptions_available() ) : ?> 154 <div class="notice notice-info inline"><p> 155 <?php 156 /* translators: %s: payment method names */ 157 echo esc_html( sprintf( __( 'Skip and pause move the next payment date, which these enabled payment methods do not support: %s. Customers who pay with them still see the reason step and can cancel, but get no offer.', 'churnanchor' ), implode( ', ', $no_dates ) ) ); 158 ?> 159 </p></div> 128 160 <?php endif; ?> 129 161 … … 199 231 </section> 200 232 <?php endif; ?> 233 234 <?php if ( $comments ) : ?> 235 <section class="ca-panel"> 236 <h2><?php esc_html_e( 'What customers wrote', 'churnanchor' ); ?></h2> 237 <ul class="ca-comments"> 238 <?php foreach ( $comments as $c ) : ?> 239 <?php 240 $label = isset( $reasons[ $c->reason_key ]['label'] ) ? $reasons[ $c->reason_key ]['label'] : ''; 241 $sub = (int) $c->subscription_id > 0 && function_exists( 'wcs_get_subscription' ) ? wcs_get_subscription( (int) $c->subscription_id ) : false; 242 ?> 243 <li> 244 <p class="ca-comment-text"><?php echo esc_html( $c->reason_text ); ?></p> 245 <p class="ca-muted"> 246 <?php 247 echo esc_html( wp_date( get_option( 'date_format' ), (int) strtotime( $c->created_at . ' UTC' ) ) ); 248 if ( $label ) { 249 echo ' · ' . esc_html( $label ); 250 } 251 if ( $sub ) { 252 /* translators: %s: subscription number */ 253 echo ' · <a href="' . esc_url( $sub->get_edit_order_url() ) . '">' . esc_html( sprintf( __( 'Subscription #%s', 'churnanchor' ), $sub->get_order_number() ) ) . '</a>'; 254 } 255 ?> 256 </p> 257 </li> 258 <?php endforeach; ?> 259 </ul> 260 </section> 261 <?php endif; ?> 201 262 <?php endif; ?> 202 263 -
churnanchor/tags/0.2.4/includes/class-cancel-flow.php
r3828412 r3828439 19 19 class Cancel_Flow { 20 20 21 const ENDPOINT = 'churnanchor-cancel'; 21 const ENDPOINT = 'churnanchor-cancel'; 22 const META_NOTES = '_churnanchor_feedback_notes'; 22 23 23 24 /** … … 154 155 */ 155 156 private function can_cancel( $sub ) { 156 return \WCS_User_Change_Status_Handler::validate_request( get_current_user_id(), $sub, 'cancelled' ); 157 // A pending-cancel subscription is already cancelled; cancelling "again" would end the 158 // prepaid term immediately, so the flow is not offered for it. 159 return ! $sub->has_status( 'pending-cancel' ) 160 && \WCS_User_Change_Status_Handler::validate_request( get_current_user_id(), $sub, 'cancelled' ); 157 161 } 158 162 … … 179 183 180 184 $view_url = $sub->get_view_order_url(); 185 if ( $sub->has_status( 'pending-cancel' ) ) { 186 $end = $sub->get_time( 'end' ); 187 wc_print_notice( 188 $end 189 /* translators: %s: date */ 190 ? sprintf( __( 'This subscription is already cancelled. You keep access until %s and will not be charged again.', 'churnanchor' ), wp_date( wc_date_format(), $end ) ) 191 : __( 'This subscription is already cancelled.', 'churnanchor' ), 192 'notice' 193 ); 194 } 181 195 if ( ! $this->can_cancel( $sub ) ) { 182 196 wc_print_notices(); … … 254 268 255 269 /** 270 * Add the customer's reason and comment to the subscription notes, so the store sees them 271 * on the subscription screen. 272 * 273 * @param \WC_Subscription $sub Subscription. 274 * @param string $flow_id Flow UUID. 275 * @param string $reason Reason key from the request ('' if none). 276 * @param string $text Comment from the request ('' if none). 277 */ 278 private static function note_feedback( $sub, $flow_id, $reason, $text ) { 279 if ( ! $sub ) { 280 return; 281 } 282 $saved = DB::flow_reason( $flow_id, $sub->get_id() ); 283 if ( ! $reason && $saved ) { 284 $reason = $saved->reason_key; 285 } 286 if ( '' === $text && $saved ) { 287 $text = (string) $saved->reason_text; 288 } 289 if ( ! $reason && '' === $text ) { 290 return; 291 } 292 $reasons = Settings::get( 'reasons' ); 293 $label = isset( $reasons[ $reason ]['label'] ) ? $reasons[ $reason ]['label'] : __( 'none given', 'churnanchor' ); 294 /* translators: %s: reason label */ 295 $note = sprintf( __( 'ChurnAnchor: cancellation reason: %s', 'churnanchor' ), $label ); 296 if ( '' !== $text ) { 297 /* translators: %s: customer comment */ 298 $note .= "\n" . sprintf( __( 'Comment: %s', 'churnanchor' ), $text ); 299 } 300 $note_id = $sub->add_order_note( $note ); 301 // Remember the note so a personal data erasure can remove the customer's words (Privacy::erase). 302 $ids = array_filter( array_map( 'absint', (array) $sub->get_meta( self::META_NOTES ) ) ); 303 $ids[] = (int) $note_id; 304 $sub->update_meta_data( self::META_NOTES, $ids ); 305 $sub->save_meta_data(); 306 } 307 308 /** 256 309 * Handle POSTed flow actions. 257 310 */ … … 331 384 case 'accept': 332 385 $offer = isset( $_POST['churnanchor_offer'] ) ? sanitize_key( wp_unslash( $_POST['churnanchor_offer'] ) ) : ''; 333 // Only an offer this flow actually showed, and that is still valid for the reason, can be accepted. 334 if ( ! $reason || ! $offer || ! in_array( $offer, Offers::offers_for_reason( $reason, $sub ), true ) || ! DB::flow_has_event( $flow_id, 'offer_shown', $sub->get_id() ) ) { 386 // Only an offer this flow actually showed, and that is still valid for the reason, can be 387 // accepted, and only once: a finished flow (old tab, bookmark) cannot change the subscription again. 388 if ( ! $reason || ! $offer || ! in_array( $offer, Offers::offers_for_reason( $reason, $sub ), true ) 389 || ! DB::flow_has_event( $flow_id, 'offer_shown', $sub->get_id() ) 390 || DB::flow_has_event( $flow_id, 'offer_accepted', $sub->get_id() ) 391 || DB::flow_has_event( $flow_id, 'cancelled', $sub->get_id() ) ) { 335 392 wp_safe_redirect( self::flow_url( $sub_id ) ); 336 393 exit; … … 344 401 if ( DB::log_event( $base + array( 'event' => 'offer_accepted', 'offer_key' => $offer ) ) ) { 345 402 DB::create_save( $base + array( 'offer_key' => $offer ) ); 403 $updated = wcs_get_subscription( $sub->get_id() ); // The copy with the new dates. 404 $sub = $updated ? $updated : $sub; 405 self::note_feedback( $sub, $flow_id, $reason, '' ); 346 406 /** 347 407 * Fires after a customer accepts a save offer. … … 358 418 359 419 case 'cancel': 420 // "Cancel now" on the first step still records a reason or comment the customer gave. 421 $text = isset( $_POST['churnanchor_reason_text'] ) ? sanitize_textarea_field( wp_unslash( $_POST['churnanchor_reason_text'] ) ) : ''; 422 $text = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, 1000 ) : substr( $text, 0, 1000 ); 423 if ( ( $reason || '' !== $text ) && ! DB::flow_has_event( $flow_id, 'reason', $sub->get_id() ) ) { 424 DB::log_event( $base + array( 'event' => 'reason', 'reason_text' => $text ) ); 425 } 360 426 \WCS_User_Change_Status_Handler::change_users_subscription( $sub, 'cancelled' ); 361 427 $fresh = wcs_get_subscription( $sub->get_id() ); 362 428 if ( $fresh && $fresh->has_status( array( 'pending-cancel', 'cancelled' ) ) ) { 363 429 DB::log_event( $base + array( 'event' => 'cancelled' ) ); 430 self::note_feedback( $fresh, $flow_id, $reason, $text ); 364 431 /** 365 432 * Fires after a customer cancels through the flow. -
churnanchor/tags/0.2.4/includes/class-db.php
r3828412 r3828439 223 223 224 224 /** 225 * The reason step of a flow (reason key and comment), or null. 226 * 227 * @param string $flow_id Flow UUID. 228 * @param int $subscription_id Subscription id. 229 * @return object|null 230 */ 231 public static function flow_reason( $flow_id, $subscription_id ) { 232 global $wpdb; 233 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table, must be fresh. 234 return $wpdb->get_row( 235 $wpdb->prepare( 236 'SELECT reason_key, reason_text FROM %i WHERE flow_id = %s AND event_type = %s AND subscription_id = %d LIMIT 1', 237 self::events_table(), 238 $flow_id, 239 'reason', 240 $subscription_id 241 ) 242 ); 243 } 244 245 /** 246 * Latest written comments from the reason step. 247 * 248 * @param string $since_gmt MySQL datetime (GMT). 249 * @param int $limit Rows. 250 * @return array 251 */ 252 public static function recent_comments( $since_gmt, $limit = 10 ) { 253 global $wpdb; 254 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- reporting on a custom table. 255 $rows = $wpdb->get_results( 256 $wpdb->prepare( 257 'SELECT subscription_id, reason_key, reason_text, created_at FROM %i WHERE event_type = %s AND reason_text <> \'\' AND created_at >= %s ORDER BY id DESC LIMIT %d', 258 self::events_table(), 259 'reason', 260 $since_gmt, 261 $limit 262 ) 263 ); 264 return is_array( $rows ) ? $rows : array(); 265 } 266 267 /** 225 268 * Create a pending save. 226 269 * … … 279 322 global $wpdb; 280 323 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table read. 281 return$wpdb->get_results(324 $rows = $wpdb->get_results( 282 325 $wpdb->prepare( 283 326 'SELECT id, subscription_id, created_at FROM %i WHERE status = %s AND id > %d ORDER BY id ASC LIMIT %d', … … 288 331 ) 289 332 ); 333 return is_array( $rows ) ? $rows : array(); 290 334 } 291 335 … … 382 426 $reasons = $wpdb->get_results( 383 427 $wpdb->prepare( 384 'SELECT reason_key, COUNT(*) AS n FROM %i WHERE event_type = %s AND reason_key <> \'\' AND created_at >= %s GROUP BY reason_key ORDER BY n DESC LIMIT 5',428 'SELECT reason_key, COUNT(*) AS n FROM %i WHERE event_type = %s AND reason_key <> \'\' AND created_at >= %s GROUP BY reason_key ORDER BY n DESC LIMIT 12', 385 429 $events, 386 430 'reason', … … 398 442 'cancelled' => $cancelled, 399 443 'accepted' => $accepted, 400 'money' => $money,401 'reasons' => $reasons,444 'money' => is_array( $money ) ? $money : array(), 445 'reasons' => is_array( $reasons ) ? $reasons : array(), 402 446 ); 403 447 } -
churnanchor/tags/0.2.4/includes/class-offers.php
r3828412 r3828439 261 261 return true; 262 262 } catch ( \Exception $e ) { 263 return new \WP_Error( 'churnanchor_apply_failed', $e->getMessage() ); 263 if ( function_exists( 'wc_get_logger' ) ) { 264 wc_get_logger()->error( 'Offer ' . $offer . ' failed on subscription ' . $sub->get_id() . ': ' . $e->getMessage(), array( 'source' => 'churnanchor' ) ); 265 } 266 return new \WP_Error( 'churnanchor_apply_failed', __( 'Sorry, we could not change your subscription. Please try again or contact us.', 'churnanchor' ) ); 264 267 } finally { 265 268 DB::release_lock( $lock_key, $token ); … … 297 300 */ 298 301 public static function access_end_if_cancelled( $sub ) { 302 // Mirrors WC_Subscription::cancel_order(): only these subscriptions keep access until the 303 // end of the prepaid term; any other status is cancelled immediately. 304 if ( ! ( $sub->has_status( 'active' ) || ( $sub->has_status( 'on-hold' ) && ! $sub->needs_payment() ) ) ) { 305 return 0; 306 } 299 307 $end = $sub->calculate_date( 'end_of_prepaid_term' ); // Filtered by Saves::cap_prepaid_term(). 300 308 $time = $end ? (int) wcs_date_to_time( $end ) : 0; -
churnanchor/tags/0.2.4/includes/class-plugin.php
r3828412 r3828439 81 81 DB::install(); 82 82 Cancel_Flow::add_endpoint(); 83 flush_rewrite_rules(); 84 update_option( 'churnanchor_rewrite_version', CHURNANCHOR_VERSION, true ); 83 flush_rewrite_rules( false ); 84 // Not recorded here: if WooCommerce Subscriptions is activated later, the flow registers its 85 // endpoint then and flushes once (Cancel_Flow::maybe_flush_rewrites). 86 delete_option( 'churnanchor_rewrite_version' ); 85 87 Saves::schedule(); 86 88 } -
churnanchor/tags/0.2.4/includes/class-privacy.php
r3828412 r3828439 152 152 ) 153 153 ); 154 // The cancellation notes ChurnAnchor added to this customer's subscriptions quote their comment. 155 if ( function_exists( 'wcs_get_users_subscriptions' ) ) { 156 foreach ( wcs_get_users_subscriptions( $user->ID ) as $sub ) { 157 $ids = array_filter( array_map( 'absint', (array) $sub->get_meta( Cancel_Flow::META_NOTES ) ) ); 158 foreach ( $ids as $note_id ) { 159 wc_delete_order_note( $note_id ); 160 ++$removed; 161 } 162 if ( $ids ) { 163 $sub->delete_meta_data( Cancel_Flow::META_NOTES ); 164 $sub->save_meta_data(); 165 } 166 } 167 } 154 168 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table. 155 $removed = (int) $wpdb->query(169 $removed += (int) $wpdb->query( 156 170 $wpdb->prepare( "UPDATE %i SET customer_id = 0, subscription_id = 0, reason_text = '' WHERE customer_id = %d", DB::events_table(), $user->ID ) 157 171 ); -
churnanchor/tags/0.2.4/includes/class-saves.php
r3828412 r3828439 34 34 add_action( self::DAILY_HOOK, array( $this, 'reconcile' ) ); 35 35 add_action( 'admin_init', array( __CLASS__, 'schedule' ) ); 36 add_filter( 'wc_subscriptions_renewal_order_data', array( __CLASS__, 'do_not_copy_meta' ) ); 37 } 38 39 /** 40 * Keep ChurnAnchor's subscription bookkeeping off renewal orders. 41 * 42 * @param array $data Meta copied from the subscription to the renewal order. 43 * @return array 44 */ 45 public static function do_not_copy_meta( $data ) { 46 foreach ( array( Offers::META_LAST_OFFER, Offers::META_PAID_UNTIL, Cancel_Flow::META_NOTES ) as $key ) { 47 unset( $data[ $key ] ); 48 } 49 return $data; 36 50 } 37 51 … … 113 127 foreach ( $rows as $row ) { 114 128 $last_id = (int) $row->id; 115 $sub = wcs_get_subscription( (int) $row->subscription_id ); 129 if ( 0 === (int) $row->subscription_id ) { 130 // Unlinked by a privacy erasure: the outcome can no longer be checked. 131 DB::set_save_status( $last_id, 'anonymised' ); 132 continue; 133 } 134 $sub = wcs_get_subscription( (int) $row->subscription_id ); 116 135 if ( ! $sub || $sub->has_status( array( 'pending-cancel', 'cancelled', 'expired', 'trash' ) ) ) { 117 136 DB::set_save_status( $last_id, 'clawed_back' ); -
churnanchor/tags/0.2.4/readme.txt
r3828412 r3828439 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 0.2. 37 Stable tag: 0.2.4 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 94 94 == Changelog == 95 95 96 = 0.2.4 = 97 * New: the reason and comment a customer gives are added as a note on the subscription, and the dashboard lists what customers wrote. 98 * New: the dashboard tells you when an enabled payment method cannot move payment dates (those customers see no skip/pause offer). 99 * Fix: a subscription that is already pending cancellation can no longer be cancelled again from an old flow link (which ended the paid period early). 100 * Fix: "Cancel subscription now" on the first step keeps the reason and comment the customer entered. 101 * Fix: an offer from a finished flow (old tab or bookmark) cannot be accepted again. 102 * Fix: the confirm screen no longer promises access until the end of the period when the subscription would end immediately (unpaid renewal). 103 * Fix: saves unlinked by a privacy erasure are no longer counted as "lost"; the erasure also removes ChurnAnchor's notes that quote the customer. 104 * Fix: "Keep my subscription" link on the offer step; all reasons are shown on the dashboard; errors are logged instead of shown to customers. 105 96 106 = 0.2.3 = 97 107 * Database: the events table column `event` is renamed to `event_type` (existing data is kept). The schema version is only saved once the tables exist. -
churnanchor/tags/0.2.4/templates/cancel-flow.php
r3828412 r3828439 147 147 </form> 148 148 </div> 149 <p class="ca-keep"><a href="<?php echo esc_url( $view_url ); ?>"><?php esc_html_e( 'Keep my subscription', 'churnanchor' ); ?></a></p> 149 150 150 151 <?php else : ?> -
churnanchor/tags/0.2.4/uninstall.php
r3828412 r3828439 14 14 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'churnanchor_events' ) ); 15 15 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'churnanchor_saves' ) ); 16 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s )', $wpdb->postmeta, '_churnanchor_last_offer_at', '_churnanchor_paid_until' ) );17 if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb-> prefix . 'wc_orders_meta') ) ) {18 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s )', $wpdb->prefix . 'wc_orders_meta', '_churnanchor_last_offer_at', '_churnanchor_paid_until' ) );16 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s, %s)', $wpdb->postmeta, '_churnanchor_last_offer_at', '_churnanchor_paid_until', '_churnanchor_feedback_notes' ) ); 17 if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->prefix . 'wc_orders_meta' ) ) ) ) { 18 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s, %s)', $wpdb->prefix . 'wc_orders_meta', '_churnanchor_last_offer_at', '_churnanchor_paid_until', '_churnanchor_feedback_notes' ) ); 19 19 } 20 20 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE option_name LIKE %s', $wpdb->options, $wpdb->esc_like( 'churnanchor_lock_' ) . '%' ) ); -
churnanchor/trunk/assets/admin.css
r3828412 r3828439 48 48 } 49 49 @media (forced-colors: active) { .churnanchor-admin .ca-bar-fill { background: Highlight; } } 50 51 .churnanchor-admin .ca-comments{margin:0;padding:0;list-style:none} 52 .churnanchor-admin .ca-comments li{padding:12px 0;border-top:1px solid #e2e4e7} 53 .churnanchor-admin .ca-comments li:first-child{border-top:0;padding-top:0} 54 .churnanchor-admin .ca-comment-text{margin:0 0 4px;font-size:14px;white-space:pre-line} 55 .churnanchor-admin .ca-comments .ca-muted{margin:0} -
churnanchor/trunk/churnanchor.php
r3828412 r3828439 4 4 * Plugin URI: https://churnanchor.marul.co/ 5 5 * Description: A fair, dark-pattern-free cancellation flow for WooCommerce Subscriptions: ask why, offer a skip or a pause, and track the revenue you actually keep. 6 * Version: 0.2. 36 * Version: 0.2.4 7 7 * Requires at least: 6.5 8 8 * Requires PHP: 7.4 … … 22 22 defined( 'ABSPATH' ) || exit; 23 23 24 define( 'CHURNANCHOR_VERSION', '0.2. 3' );24 define( 'CHURNANCHOR_VERSION', '0.2.4' ); 25 25 define( 'CHURNANCHOR_FILE', __FILE__ ); 26 26 define( 'CHURNANCHOR_DIR', plugin_dir_path( __FILE__ ) ); -
churnanchor/trunk/includes/class-admin.php
r3828412 r3828439 60 60 */ 61 61 private function money( $cents, $currency ) { 62 if ( ! function_exists( 'wc_price' ) ) { 63 return number_format_i18n( $cents / 100, 2 ) . ' ' . $currency; 64 } 62 65 return wp_strip_all_tags( wc_price( $cents / 100, array( 'currency' => $currency ) ) ); 66 } 67 68 /** 69 * Names of enabled subscription payment methods that cannot change payment dates. 70 * 71 * @return string[] 72 */ 73 private static function gateways_without_date_changes() { 74 if ( ! function_exists( 'WC' ) || ! WC()->payment_gateways() ) { 75 return array(); 76 } 77 $names = array(); 78 foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) { 79 if ( 'yes' === $gateway->enabled && $gateway->supports( 'subscriptions' ) && ! $gateway->supports( 'subscription_date_changes' ) ) { 80 $names[] = wp_strip_all_tags( $gateway->get_title() ); 81 } 82 } 83 return $names; 63 84 } 64 85 … … 102 123 } 103 124 $total_r = (int) $stats['reasons_total']; 125 $comments = DB::recent_comments( gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ) ); 126 $no_dates = self::gateways_without_date_changes(); 104 127 $is_empty = 0 === $stats['attempts'] && 0 === $stats['accepted'] && 0 === $stats['cancelled'] && empty( $stats['money'] ); 105 128 $base_url = admin_url( 'admin.php?page=' . self::SLUG ); … … 126 149 <?php if ( ! Plugin::subscriptions_available() ) : ?> 127 150 <div class="notice notice-warning inline"><p><?php esc_html_e( 'WooCommerce Subscriptions was not detected. Activate it to use the cancellation flow.', 'churnanchor' ); ?></p></div> 151 <?php endif; ?> 152 153 <?php if ( $no_dates && Plugin::subscriptions_available() ) : ?> 154 <div class="notice notice-info inline"><p> 155 <?php 156 /* translators: %s: payment method names */ 157 echo esc_html( sprintf( __( 'Skip and pause move the next payment date, which these enabled payment methods do not support: %s. Customers who pay with them still see the reason step and can cancel, but get no offer.', 'churnanchor' ), implode( ', ', $no_dates ) ) ); 158 ?> 159 </p></div> 128 160 <?php endif; ?> 129 161 … … 199 231 </section> 200 232 <?php endif; ?> 233 234 <?php if ( $comments ) : ?> 235 <section class="ca-panel"> 236 <h2><?php esc_html_e( 'What customers wrote', 'churnanchor' ); ?></h2> 237 <ul class="ca-comments"> 238 <?php foreach ( $comments as $c ) : ?> 239 <?php 240 $label = isset( $reasons[ $c->reason_key ]['label'] ) ? $reasons[ $c->reason_key ]['label'] : ''; 241 $sub = (int) $c->subscription_id > 0 && function_exists( 'wcs_get_subscription' ) ? wcs_get_subscription( (int) $c->subscription_id ) : false; 242 ?> 243 <li> 244 <p class="ca-comment-text"><?php echo esc_html( $c->reason_text ); ?></p> 245 <p class="ca-muted"> 246 <?php 247 echo esc_html( wp_date( get_option( 'date_format' ), (int) strtotime( $c->created_at . ' UTC' ) ) ); 248 if ( $label ) { 249 echo ' · ' . esc_html( $label ); 250 } 251 if ( $sub ) { 252 /* translators: %s: subscription number */ 253 echo ' · <a href="' . esc_url( $sub->get_edit_order_url() ) . '">' . esc_html( sprintf( __( 'Subscription #%s', 'churnanchor' ), $sub->get_order_number() ) ) . '</a>'; 254 } 255 ?> 256 </p> 257 </li> 258 <?php endforeach; ?> 259 </ul> 260 </section> 261 <?php endif; ?> 201 262 <?php endif; ?> 202 263 -
churnanchor/trunk/includes/class-cancel-flow.php
r3828412 r3828439 19 19 class Cancel_Flow { 20 20 21 const ENDPOINT = 'churnanchor-cancel'; 21 const ENDPOINT = 'churnanchor-cancel'; 22 const META_NOTES = '_churnanchor_feedback_notes'; 22 23 23 24 /** … … 154 155 */ 155 156 private function can_cancel( $sub ) { 156 return \WCS_User_Change_Status_Handler::validate_request( get_current_user_id(), $sub, 'cancelled' ); 157 // A pending-cancel subscription is already cancelled; cancelling "again" would end the 158 // prepaid term immediately, so the flow is not offered for it. 159 return ! $sub->has_status( 'pending-cancel' ) 160 && \WCS_User_Change_Status_Handler::validate_request( get_current_user_id(), $sub, 'cancelled' ); 157 161 } 158 162 … … 179 183 180 184 $view_url = $sub->get_view_order_url(); 185 if ( $sub->has_status( 'pending-cancel' ) ) { 186 $end = $sub->get_time( 'end' ); 187 wc_print_notice( 188 $end 189 /* translators: %s: date */ 190 ? sprintf( __( 'This subscription is already cancelled. You keep access until %s and will not be charged again.', 'churnanchor' ), wp_date( wc_date_format(), $end ) ) 191 : __( 'This subscription is already cancelled.', 'churnanchor' ), 192 'notice' 193 ); 194 } 181 195 if ( ! $this->can_cancel( $sub ) ) { 182 196 wc_print_notices(); … … 254 268 255 269 /** 270 * Add the customer's reason and comment to the subscription notes, so the store sees them 271 * on the subscription screen. 272 * 273 * @param \WC_Subscription $sub Subscription. 274 * @param string $flow_id Flow UUID. 275 * @param string $reason Reason key from the request ('' if none). 276 * @param string $text Comment from the request ('' if none). 277 */ 278 private static function note_feedback( $sub, $flow_id, $reason, $text ) { 279 if ( ! $sub ) { 280 return; 281 } 282 $saved = DB::flow_reason( $flow_id, $sub->get_id() ); 283 if ( ! $reason && $saved ) { 284 $reason = $saved->reason_key; 285 } 286 if ( '' === $text && $saved ) { 287 $text = (string) $saved->reason_text; 288 } 289 if ( ! $reason && '' === $text ) { 290 return; 291 } 292 $reasons = Settings::get( 'reasons' ); 293 $label = isset( $reasons[ $reason ]['label'] ) ? $reasons[ $reason ]['label'] : __( 'none given', 'churnanchor' ); 294 /* translators: %s: reason label */ 295 $note = sprintf( __( 'ChurnAnchor: cancellation reason: %s', 'churnanchor' ), $label ); 296 if ( '' !== $text ) { 297 /* translators: %s: customer comment */ 298 $note .= "\n" . sprintf( __( 'Comment: %s', 'churnanchor' ), $text ); 299 } 300 $note_id = $sub->add_order_note( $note ); 301 // Remember the note so a personal data erasure can remove the customer's words (Privacy::erase). 302 $ids = array_filter( array_map( 'absint', (array) $sub->get_meta( self::META_NOTES ) ) ); 303 $ids[] = (int) $note_id; 304 $sub->update_meta_data( self::META_NOTES, $ids ); 305 $sub->save_meta_data(); 306 } 307 308 /** 256 309 * Handle POSTed flow actions. 257 310 */ … … 331 384 case 'accept': 332 385 $offer = isset( $_POST['churnanchor_offer'] ) ? sanitize_key( wp_unslash( $_POST['churnanchor_offer'] ) ) : ''; 333 // Only an offer this flow actually showed, and that is still valid for the reason, can be accepted. 334 if ( ! $reason || ! $offer || ! in_array( $offer, Offers::offers_for_reason( $reason, $sub ), true ) || ! DB::flow_has_event( $flow_id, 'offer_shown', $sub->get_id() ) ) { 386 // Only an offer this flow actually showed, and that is still valid for the reason, can be 387 // accepted, and only once: a finished flow (old tab, bookmark) cannot change the subscription again. 388 if ( ! $reason || ! $offer || ! in_array( $offer, Offers::offers_for_reason( $reason, $sub ), true ) 389 || ! DB::flow_has_event( $flow_id, 'offer_shown', $sub->get_id() ) 390 || DB::flow_has_event( $flow_id, 'offer_accepted', $sub->get_id() ) 391 || DB::flow_has_event( $flow_id, 'cancelled', $sub->get_id() ) ) { 335 392 wp_safe_redirect( self::flow_url( $sub_id ) ); 336 393 exit; … … 344 401 if ( DB::log_event( $base + array( 'event' => 'offer_accepted', 'offer_key' => $offer ) ) ) { 345 402 DB::create_save( $base + array( 'offer_key' => $offer ) ); 403 $updated = wcs_get_subscription( $sub->get_id() ); // The copy with the new dates. 404 $sub = $updated ? $updated : $sub; 405 self::note_feedback( $sub, $flow_id, $reason, '' ); 346 406 /** 347 407 * Fires after a customer accepts a save offer. … … 358 418 359 419 case 'cancel': 420 // "Cancel now" on the first step still records a reason or comment the customer gave. 421 $text = isset( $_POST['churnanchor_reason_text'] ) ? sanitize_textarea_field( wp_unslash( $_POST['churnanchor_reason_text'] ) ) : ''; 422 $text = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, 1000 ) : substr( $text, 0, 1000 ); 423 if ( ( $reason || '' !== $text ) && ! DB::flow_has_event( $flow_id, 'reason', $sub->get_id() ) ) { 424 DB::log_event( $base + array( 'event' => 'reason', 'reason_text' => $text ) ); 425 } 360 426 \WCS_User_Change_Status_Handler::change_users_subscription( $sub, 'cancelled' ); 361 427 $fresh = wcs_get_subscription( $sub->get_id() ); 362 428 if ( $fresh && $fresh->has_status( array( 'pending-cancel', 'cancelled' ) ) ) { 363 429 DB::log_event( $base + array( 'event' => 'cancelled' ) ); 430 self::note_feedback( $fresh, $flow_id, $reason, $text ); 364 431 /** 365 432 * Fires after a customer cancels through the flow. -
churnanchor/trunk/includes/class-db.php
r3828412 r3828439 223 223 224 224 /** 225 * The reason step of a flow (reason key and comment), or null. 226 * 227 * @param string $flow_id Flow UUID. 228 * @param int $subscription_id Subscription id. 229 * @return object|null 230 */ 231 public static function flow_reason( $flow_id, $subscription_id ) { 232 global $wpdb; 233 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table, must be fresh. 234 return $wpdb->get_row( 235 $wpdb->prepare( 236 'SELECT reason_key, reason_text FROM %i WHERE flow_id = %s AND event_type = %s AND subscription_id = %d LIMIT 1', 237 self::events_table(), 238 $flow_id, 239 'reason', 240 $subscription_id 241 ) 242 ); 243 } 244 245 /** 246 * Latest written comments from the reason step. 247 * 248 * @param string $since_gmt MySQL datetime (GMT). 249 * @param int $limit Rows. 250 * @return array 251 */ 252 public static function recent_comments( $since_gmt, $limit = 10 ) { 253 global $wpdb; 254 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- reporting on a custom table. 255 $rows = $wpdb->get_results( 256 $wpdb->prepare( 257 'SELECT subscription_id, reason_key, reason_text, created_at FROM %i WHERE event_type = %s AND reason_text <> \'\' AND created_at >= %s ORDER BY id DESC LIMIT %d', 258 self::events_table(), 259 'reason', 260 $since_gmt, 261 $limit 262 ) 263 ); 264 return is_array( $rows ) ? $rows : array(); 265 } 266 267 /** 225 268 * Create a pending save. 226 269 * … … 279 322 global $wpdb; 280 323 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table read. 281 return$wpdb->get_results(324 $rows = $wpdb->get_results( 282 325 $wpdb->prepare( 283 326 'SELECT id, subscription_id, created_at FROM %i WHERE status = %s AND id > %d ORDER BY id ASC LIMIT %d', … … 288 331 ) 289 332 ); 333 return is_array( $rows ) ? $rows : array(); 290 334 } 291 335 … … 382 426 $reasons = $wpdb->get_results( 383 427 $wpdb->prepare( 384 'SELECT reason_key, COUNT(*) AS n FROM %i WHERE event_type = %s AND reason_key <> \'\' AND created_at >= %s GROUP BY reason_key ORDER BY n DESC LIMIT 5',428 'SELECT reason_key, COUNT(*) AS n FROM %i WHERE event_type = %s AND reason_key <> \'\' AND created_at >= %s GROUP BY reason_key ORDER BY n DESC LIMIT 12', 385 429 $events, 386 430 'reason', … … 398 442 'cancelled' => $cancelled, 399 443 'accepted' => $accepted, 400 'money' => $money,401 'reasons' => $reasons,444 'money' => is_array( $money ) ? $money : array(), 445 'reasons' => is_array( $reasons ) ? $reasons : array(), 402 446 ); 403 447 } -
churnanchor/trunk/includes/class-offers.php
r3828412 r3828439 261 261 return true; 262 262 } catch ( \Exception $e ) { 263 return new \WP_Error( 'churnanchor_apply_failed', $e->getMessage() ); 263 if ( function_exists( 'wc_get_logger' ) ) { 264 wc_get_logger()->error( 'Offer ' . $offer . ' failed on subscription ' . $sub->get_id() . ': ' . $e->getMessage(), array( 'source' => 'churnanchor' ) ); 265 } 266 return new \WP_Error( 'churnanchor_apply_failed', __( 'Sorry, we could not change your subscription. Please try again or contact us.', 'churnanchor' ) ); 264 267 } finally { 265 268 DB::release_lock( $lock_key, $token ); … … 297 300 */ 298 301 public static function access_end_if_cancelled( $sub ) { 302 // Mirrors WC_Subscription::cancel_order(): only these subscriptions keep access until the 303 // end of the prepaid term; any other status is cancelled immediately. 304 if ( ! ( $sub->has_status( 'active' ) || ( $sub->has_status( 'on-hold' ) && ! $sub->needs_payment() ) ) ) { 305 return 0; 306 } 299 307 $end = $sub->calculate_date( 'end_of_prepaid_term' ); // Filtered by Saves::cap_prepaid_term(). 300 308 $time = $end ? (int) wcs_date_to_time( $end ) : 0; -
churnanchor/trunk/includes/class-plugin.php
r3828412 r3828439 81 81 DB::install(); 82 82 Cancel_Flow::add_endpoint(); 83 flush_rewrite_rules(); 84 update_option( 'churnanchor_rewrite_version', CHURNANCHOR_VERSION, true ); 83 flush_rewrite_rules( false ); 84 // Not recorded here: if WooCommerce Subscriptions is activated later, the flow registers its 85 // endpoint then and flushes once (Cancel_Flow::maybe_flush_rewrites). 86 delete_option( 'churnanchor_rewrite_version' ); 85 87 Saves::schedule(); 86 88 } -
churnanchor/trunk/includes/class-privacy.php
r3828412 r3828439 152 152 ) 153 153 ); 154 // The cancellation notes ChurnAnchor added to this customer's subscriptions quote their comment. 155 if ( function_exists( 'wcs_get_users_subscriptions' ) ) { 156 foreach ( wcs_get_users_subscriptions( $user->ID ) as $sub ) { 157 $ids = array_filter( array_map( 'absint', (array) $sub->get_meta( Cancel_Flow::META_NOTES ) ) ); 158 foreach ( $ids as $note_id ) { 159 wc_delete_order_note( $note_id ); 160 ++$removed; 161 } 162 if ( $ids ) { 163 $sub->delete_meta_data( Cancel_Flow::META_NOTES ); 164 $sub->save_meta_data(); 165 } 166 } 167 } 154 168 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table. 155 $removed = (int) $wpdb->query(169 $removed += (int) $wpdb->query( 156 170 $wpdb->prepare( "UPDATE %i SET customer_id = 0, subscription_id = 0, reason_text = '' WHERE customer_id = %d", DB::events_table(), $user->ID ) 157 171 ); -
churnanchor/trunk/includes/class-saves.php
r3828412 r3828439 34 34 add_action( self::DAILY_HOOK, array( $this, 'reconcile' ) ); 35 35 add_action( 'admin_init', array( __CLASS__, 'schedule' ) ); 36 add_filter( 'wc_subscriptions_renewal_order_data', array( __CLASS__, 'do_not_copy_meta' ) ); 37 } 38 39 /** 40 * Keep ChurnAnchor's subscription bookkeeping off renewal orders. 41 * 42 * @param array $data Meta copied from the subscription to the renewal order. 43 * @return array 44 */ 45 public static function do_not_copy_meta( $data ) { 46 foreach ( array( Offers::META_LAST_OFFER, Offers::META_PAID_UNTIL, Cancel_Flow::META_NOTES ) as $key ) { 47 unset( $data[ $key ] ); 48 } 49 return $data; 36 50 } 37 51 … … 113 127 foreach ( $rows as $row ) { 114 128 $last_id = (int) $row->id; 115 $sub = wcs_get_subscription( (int) $row->subscription_id ); 129 if ( 0 === (int) $row->subscription_id ) { 130 // Unlinked by a privacy erasure: the outcome can no longer be checked. 131 DB::set_save_status( $last_id, 'anonymised' ); 132 continue; 133 } 134 $sub = wcs_get_subscription( (int) $row->subscription_id ); 116 135 if ( ! $sub || $sub->has_status( array( 'pending-cancel', 'cancelled', 'expired', 'trash' ) ) ) { 117 136 DB::set_save_status( $last_id, 'clawed_back' ); -
churnanchor/trunk/readme.txt
r3828412 r3828439 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 0.2. 37 Stable tag: 0.2.4 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 94 94 == Changelog == 95 95 96 = 0.2.4 = 97 * New: the reason and comment a customer gives are added as a note on the subscription, and the dashboard lists what customers wrote. 98 * New: the dashboard tells you when an enabled payment method cannot move payment dates (those customers see no skip/pause offer). 99 * Fix: a subscription that is already pending cancellation can no longer be cancelled again from an old flow link (which ended the paid period early). 100 * Fix: "Cancel subscription now" on the first step keeps the reason and comment the customer entered. 101 * Fix: an offer from a finished flow (old tab or bookmark) cannot be accepted again. 102 * Fix: the confirm screen no longer promises access until the end of the period when the subscription would end immediately (unpaid renewal). 103 * Fix: saves unlinked by a privacy erasure are no longer counted as "lost"; the erasure also removes ChurnAnchor's notes that quote the customer. 104 * Fix: "Keep my subscription" link on the offer step; all reasons are shown on the dashboard; errors are logged instead of shown to customers. 105 96 106 = 0.2.3 = 97 107 * Database: the events table column `event` is renamed to `event_type` (existing data is kept). The schema version is only saved once the tables exist. -
churnanchor/trunk/templates/cancel-flow.php
r3828412 r3828439 147 147 </form> 148 148 </div> 149 <p class="ca-keep"><a href="<?php echo esc_url( $view_url ); ?>"><?php esc_html_e( 'Keep my subscription', 'churnanchor' ); ?></a></p> 149 150 150 151 <?php else : ?> -
churnanchor/trunk/uninstall.php
r3828412 r3828439 14 14 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'churnanchor_events' ) ); 15 15 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'churnanchor_saves' ) ); 16 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s )', $wpdb->postmeta, '_churnanchor_last_offer_at', '_churnanchor_paid_until' ) );17 if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb-> prefix . 'wc_orders_meta') ) ) {18 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s )', $wpdb->prefix . 'wc_orders_meta', '_churnanchor_last_offer_at', '_churnanchor_paid_until' ) );16 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s, %s)', $wpdb->postmeta, '_churnanchor_last_offer_at', '_churnanchor_paid_until', '_churnanchor_feedback_notes' ) ); 17 if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->prefix . 'wc_orders_meta' ) ) ) ) { 18 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key IN (%s, %s, %s)', $wpdb->prefix . 'wc_orders_meta', '_churnanchor_last_offer_at', '_churnanchor_paid_until', '_churnanchor_feedback_notes' ) ); 19 19 } 20 20 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE option_name LIKE %s', $wpdb->options, $wpdb->esc_like( 'churnanchor_lock_' ) . '%' ) );
Note:
See TracChangeset
for help on using the changeset viewer.