Changeset 3807599
- Timestamp:
- 11/17/2026 02:32:48 PM (4 days ago)
- Location:
- takeed-refusal-guard
- Files:
-
- 6 added
- 24 edited
- 1 copied
-
assets/screenshot-1.png (modified) ( previous)
-
assets/screenshot-2.png (modified) ( previous)
-
tags/1.1.0 (copied) (copied from takeed-refusal-guard/trunk )
-
tags/1.1.0/includes/class-takrg-admin.php (modified) (8 diffs)
-
tags/1.1.0/includes/class-takrg-breaker.php (added)
-
tags/1.1.0/includes/class-takrg-checkout.php (modified) (14 diffs)
-
tags/1.1.0/includes/class-takrg-log.php (added)
-
tags/1.1.0/includes/class-takrg-plugin.php (modified) (2 diffs)
-
tags/1.1.0/includes/class-takrg-privacy.php (modified) (3 diffs)
-
tags/1.1.0/includes/class-takrg-recorder.php (modified) (2 diffs)
-
tags/1.1.0/includes/class-takrg-rules.php (added)
-
tags/1.1.0/includes/class-takrg-settings.php (modified) (2 diffs)
-
tags/1.1.0/includes/class-takrg-store.php (modified) (2 diffs)
-
tags/1.1.0/languages/takeed-refusal-guard.pot (modified) (6 diffs)
-
tags/1.1.0/readme.txt (modified) (6 diffs)
-
tags/1.1.0/takeed-refusal-guard.php (modified) (4 diffs)
-
tags/1.1.0/uninstall.php (modified) (1 diff)
-
trunk/includes/class-takrg-admin.php (modified) (8 diffs)
-
trunk/includes/class-takrg-breaker.php (added)
-
trunk/includes/class-takrg-checkout.php (modified) (14 diffs)
-
trunk/includes/class-takrg-log.php (added)
-
trunk/includes/class-takrg-plugin.php (modified) (2 diffs)
-
trunk/includes/class-takrg-privacy.php (modified) (3 diffs)
-
trunk/includes/class-takrg-recorder.php (modified) (2 diffs)
-
trunk/includes/class-takrg-rules.php (added)
-
trunk/includes/class-takrg-settings.php (modified) (2 diffs)
-
trunk/includes/class-takrg-store.php (modified) (2 diffs)
-
trunk/languages/takeed-refusal-guard.pot (modified) (6 diffs)
-
trunk/readme.txt (modified) (6 diffs)
-
trunk/takeed-refusal-guard.php (modified) (4 diffs)
-
trunk/uninstall.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
takeed-refusal-guard/tags/1.1.0/includes/class-takrg-admin.php
r3806545 r3807599 30 30 add_action( 'admin_post_takrg_import', array( __CLASS__, 'handle_import' ) ); 31 31 add_action( 'admin_post_takrg_export', array( __CLASS__, 'handle_export' ) ); 32 add_action( 'admin_post_takrg_rule_add', array( __CLASS__, 'handle_rule_add' ) ); 33 add_action( 'admin_post_takrg_rule_delete', array( __CLASS__, 'handle_rule_delete' ) ); 34 add_action( 'admin_post_takrg_scan', array( __CLASS__, 'handle_scan' ) ); 35 add_action( 'admin_post_takrg_breaker_reset', array( __CLASS__, 'handle_breaker_reset' ) ); 36 add_action( 'admin_notices', array( __CLASS__, 'breaker_notice' ) ); 32 37 // Orders list: legacy (posts) screen and HPOS screen. 33 38 add_filter( 'manage_edit-shop_order_columns', array( __CLASS__, 'add_column' ) ); … … 314 319 315 320 /** 321 * Add an allow-list or block-list rule (exact value or wildcard pattern). 322 */ 323 public static function handle_rule_add() { 324 self::guard( 'takrg_rule_add' ); 325 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified in guard(). 326 $type = isset( $_POST['takrg_type'] ) && TAKRG_Rules::BLOCK === sanitize_key( wp_unslash( $_POST['takrg_type'] ) ) ? TAKRG_Rules::BLOCK : TAKRG_Rules::ALLOW; 327 $value = isset( $_POST['takrg_value'] ) ? sanitize_text_field( wp_unslash( $_POST['takrg_value'] ) ) : ''; 328 $country = TAKRG_Recorder::country_for( isset( $_POST['takrg_country'] ) ? sanitize_text_field( wp_unslash( $_POST['takrg_country'] ) ) : '' ); 329 // phpcs:enable WordPress.Security.NonceVerification.Missing 330 $id = TAKRG_Rules::add( $type, $value, $country ); 331 TAKRG_Checkout::reset_cache(); 332 self::back( $id ? 'rule_added' : 'rule_invalid' ); 333 } 334 335 /** 336 * Delete a rule. 337 */ 338 public static function handle_rule_delete() { 339 self::guard( 'takrg_rule_delete' ); 340 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in guard(). 341 TAKRG_Rules::delete( isset( $_POST['takrg_id'] ) ? absint( $_POST['takrg_id'] ) : 0 ); 342 TAKRG_Checkout::reset_cache(); 343 self::back( 'removed', 1 ); 344 } 345 346 /** 347 * Turns the protection back on after the safety breaker tripped. 348 */ 349 public static function handle_breaker_reset() { 350 self::guard( 'takrg_breaker_reset' ); 351 TAKRG_Breaker::reset(); 352 self::back( 'breaker_reset' ); 353 } 354 355 /** 356 * Adds the refusals of orders that are already in a counted status (500 orders per click, newest page first). 357 * Safe to repeat: an order that already has a record is skipped. 358 */ 359 public static function handle_scan() { 360 self::guard( 'takrg_scan' ); 361 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in guard(). 362 $page = isset( $_POST['takrg_page'] ) ? max( 1, absint( $_POST['takrg_page'] ) ) : 1; 363 $added = 0; 364 $more = false; 365 for ( $i = 0; $i < 5; $i++, $page++ ) { 366 $orders = wc_get_orders( 367 array( 368 'status' => TAKRG_Settings::counted_statuses(), 369 'limit' => 100, 370 'paged' => $page, 371 'orderby' => 'ID', 372 'order' => 'ASC', 373 'return' => 'objects', 374 ) 375 ); 376 foreach ( $orders as $order ) { 377 list( $phone, $email ) = TAKRG_Recorder::identity( $order ); 378 $added += TAKRG_Store::add( $phone, $email, $order->get_id(), 'scan' ) ? 1 : 0; 379 } 380 if ( count( $orders ) < 100 ) { 381 break; 382 } 383 $more = true; 384 } 385 TAKRG_Checkout::reset_cache(); 386 wp_safe_redirect( 387 add_query_arg( 388 array( 389 'page' => self::SLUG, 390 'takrg_msg' => ( $more && count( $orders ) >= 100 ) ? 'scan_more' : 'scanned', 391 'takrg_num' => $added, 392 'takrg_pg' => $page, 393 ), 394 admin_url( 'admin.php' ) 395 ) 396 ); 397 exit; 398 } 399 400 /** 401 * Admin notice when the safety breaker has tripped (every admin screen, for shop managers). 402 */ 403 public static function breaker_notice() { 404 if ( ! current_user_can( self::CAP ) || ! TAKRG_Breaker::is_tripped() ) { 405 return; 406 } 407 echo '<div class="notice notice-warning"><p><strong>' . esc_html__( 'Takeed Refusal Guard switched itself to "tag only".', 'takeed-refusal-guard' ) . '</strong> ' 408 . esc_html__( 'More than half of the recent checkout attempts were restricted, so the plugin stopped restricting Cash on Delivery to protect your sales.', 'takeed-refusal-guard' ) 409 . ' <a href="' . esc_url( admin_url( 'admin.php?page=' . self::SLUG ) ) . '">' . esc_html__( 'Review and turn it back on', 'takeed-refusal-guard' ) . '</a></p></div>'; 410 } 411 412 /** 316 413 * Notice texts by key. 317 414 * … … 331 428 /* translators: %d: number of records. */ 332 429 return array( 'success', sprintf( _n( '%d record imported.', '%d records imported.', $num, 'takeed-refusal-guard' ), $num ) ); 430 case 'rule_added': 431 return array( 'success', __( 'Rule added.', 'takeed-refusal-guard' ) ); 432 case 'rule_invalid': 433 return array( 'error', __( 'Enter an e-mail address, a phone number, or a pattern such as *@example.com or +21620* (not already in the list).', 'takeed-refusal-guard' ) ); 434 case 'breaker_reset': 435 return array( 'success', __( 'Protection turned back on.', 'takeed-refusal-guard' ) ); 436 case 'scanned': 437 /* translators: %d: number of records. */ 438 return array( 'success', sprintf( _n( 'Scan finished: %d record added.', 'Scan finished: %d records added.', $num, 'takeed-refusal-guard' ), $num ) ); 439 case 'scan_more': 440 /* translators: %d: number of records. */ 441 return array( 'success', sprintf( _n( '%d record added. More orders remain: press "Continue scan".', '%d records added. More orders remain: press "Continue scan".', $num, 'takeed-refusal-guard' ), $num ) ); 333 442 case 'invalid': 334 443 return array( 'error', __( 'Enter a valid phone number or e-mail address.', 'takeed-refusal-guard' ) ); … … 375 484 // phpcs:enable WordPress.Security.NonceVerification.Recommended 376 485 $per_page = 50; 377 $total = TAKRG_Store::total(); 378 $rows = TAKRG_Store::all( $per_page, ( $paged - 1 ) * $per_page ); 379 $statuses = TAKRG_Settings::selectable_statuses(); 380 $post_url = admin_url( 'admin-post.php' ); 381 $actions = array( 486 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only: a page number. 487 $scan_page = isset( $_GET['takrg_pg'] ) ? max( 1, absint( $_GET['takrg_pg'] ) ) : 1; 488 $total = TAKRG_Store::total(); 489 $rows = TAKRG_Store::all( $per_page, ( $paged - 1 ) * $per_page ); 490 $statuses = TAKRG_Settings::selectable_statuses(); 491 $post_url = admin_url( 'admin-post.php' ); 492 $actions = array( 382 493 'tag' => array( __( 'Tag only (default)', 'takeed-refusal-guard' ), __( 'Adds an order note and the meta "_takrg_risk". Nothing is blocked.', 'takeed-refusal-guard' ) ), 383 494 'hide' => array( __( 'Hide Cash on Delivery', 'takeed-refusal-guard' ), __( 'Cash on Delivery is not offered to this customer; other payment methods stay available.', 'takeed-refusal-guard' ) ), … … 391 502 <?php endif; ?> 392 503 504 <?php if ( TAKRG_Breaker::is_tripped() ) : ?> 505 <div class="notice notice-warning inline"> 506 <p><strong><?php esc_html_e( 'The safety breaker switched the plugin to "tag only".', 'takeed-refusal-guard' ); ?></strong> 507 <?php esc_html_e( 'More than half of the checkout attempts in an hour were restricted. Check the activity log below, then turn the protection back on.', 'takeed-refusal-guard' ); ?></p> 508 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 509 <input type="hidden" name="action" value="takrg_breaker_reset" /> 510 <?php wp_nonce_field( 'takrg_breaker_reset' ); ?> 511 <p><button type="submit" class="button"><?php esc_html_e( 'Turn protection back on', 'takeed-refusal-guard' ); ?></button></p> 512 </form> 513 </div> 514 <?php endif; ?> 515 <?php if ( 'live' !== $s['mode'] ) : ?> 516 <div class="notice notice-info inline"><p><strong><?php esc_html_e( 'Test mode is on.', 'takeed-refusal-guard' ); ?></strong> 517 <?php esc_html_e( 'Nothing is hidden or refused: orders are only tagged, and the activity log below shows what the plugin would have done. When the log looks right, switch to Live.', 'takeed-refusal-guard' ); ?></p></div> 518 <?php endif; ?> 519 393 520 <form method="post" action="options.php"> 394 521 <?php settings_fields( 'takrg_settings_group' ); ?> 395 522 <table class="form-table" role="presentation"> 523 <tr> 524 <th scope="row"><?php esc_html_e( 'Mode', 'takeed-refusal-guard' ); ?></th> 525 <td> 526 <fieldset> 527 <label><input type="radio" name="takrg_settings[mode]" value="test" <?php checked( $s['mode'], 'test' ); ?> /> <strong><?php esc_html_e( 'Test mode (default)', 'takeed-refusal-guard' ); ?></strong></label> 528 <p class="description"><?php esc_html_e( 'Only tags orders and writes what it would have done to the activity log.', 'takeed-refusal-guard' ); ?></p> 529 <label><input type="radio" name="takrg_settings[mode]" value="live" <?php checked( $s['mode'], 'live' ); ?> /> <strong><?php esc_html_e( 'Live', 'takeed-refusal-guard' ); ?></strong></label> 530 <p class="description"><?php esc_html_e( 'Applies the action below. A safety breaker returns to "tag only" if more than 50% of the checkout attempts in an hour (at least 5) are restricted.', 'takeed-refusal-guard' ); ?></p> 531 </fieldset> 532 </td> 533 </tr> 396 534 <tr> 397 535 <th scope="row"><label for="takrg_threshold"><?php esc_html_e( 'Refusals before acting', 'takeed-refusal-guard' ); ?></label></th> … … 431 569 </td> 432 570 </tr> 571 <tr> 572 <th scope="row"><label for="takrg_retention"><?php esc_html_e( 'Forget refusals after (months)', 'takeed-refusal-guard' ); ?></label></th> 573 <td> 574 <input type="number" min="0" max="120" id="takrg_retention" name="takrg_settings[retention]" value="<?php echo esc_attr( (string) $s['retention'] ); ?>" class="small-text" /> 575 <p class="description"><?php esc_html_e( '0 keeps them until you delete them. Older records and log entries are removed once a day.', 'takeed-refusal-guard' ); ?></p> 576 </td> 577 </tr> 433 578 </table> 434 579 <?php submit_button(); ?> … … 462 607 <button type="submit" class="button"><?php esc_html_e( 'Remove all records of this number or e-mail', 'takeed-refusal-guard' ); ?></button> 463 608 </p> 609 </form> 610 611 <h2><?php esc_html_e( 'Allow-list and block-list', 'takeed-refusal-guard' ); ?></h2> 612 <p class="description"><?php esc_html_e( 'Allow: never act on this customer, whatever the refusals. Block: always treat this customer as having reached the threshold. Use an e-mail, a phone number, or a pattern with * such as *@example.com or +21620*.', 'takeed-refusal-guard' ); ?></p> 613 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 614 <input type="hidden" name="action" value="takrg_rule_add" /> 615 <?php wp_nonce_field( 'takrg_rule_add' ); ?> 616 <p> 617 <select name="takrg_type" aria-label="<?php esc_attr_e( 'List', 'takeed-refusal-guard' ); ?>"> 618 <option value="allow"><?php esc_html_e( 'Allow', 'takeed-refusal-guard' ); ?></option> 619 <option value="block"><?php esc_html_e( 'Block', 'takeed-refusal-guard' ); ?></option> 620 </select> 621 <input type="text" name="takrg_value" class="regular-text" dir="ltr" aria-label="<?php esc_attr_e( 'Value', 'takeed-refusal-guard' ); ?>" /> 622 <select name="takrg_country" aria-label="<?php esc_attr_e( 'Country', 'takeed-refusal-guard' ); ?>"><?php self::country_options( (string) $s['default_country'] ); ?></select> 623 <button type="submit" class="button"><?php esc_html_e( 'Add rule', 'takeed-refusal-guard' ); ?></button> 624 </p> 625 </form> 626 <?php $rules = TAKRG_Rules::all( 100 ); ?> 627 <?php if ( $rules ) : ?> 628 <table class="widefat striped"> 629 <thead><tr> 630 <th><?php esc_html_e( 'List', 'takeed-refusal-guard' ); ?></th> 631 <th><?php esc_html_e( 'Value', 'takeed-refusal-guard' ); ?></th> 632 <th><?php esc_html_e( 'Date (UTC)', 'takeed-refusal-guard' ); ?></th> 633 <th></th> 634 </tr></thead> 635 <tbody> 636 <?php foreach ( $rules as $r ) : ?> 637 <tr> 638 <td><?php echo 'block' === $r['type'] ? esc_html__( 'Block', 'takeed-refusal-guard' ) : esc_html__( 'Allow', 'takeed-refusal-guard' ); ?></td> 639 <td dir="ltr"><?php echo esc_html( 'phone' === $r['kind'] ? '+' . $r['value'] : $r['value'] ); ?></td> 640 <td><?php echo esc_html( $r['created_at'] ); ?></td> 641 <td> 642 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 643 <input type="hidden" name="action" value="takrg_rule_delete" /> 644 <input type="hidden" name="takrg_id" value="<?php echo esc_attr( (string) $r['id'] ); ?>" /> 645 <?php wp_nonce_field( 'takrg_rule_delete' ); ?> 646 <button type="submit" class="button-link button-link-delete"><?php esc_html_e( 'Delete', 'takeed-refusal-guard' ); ?></button> 647 </form> 648 </td> 649 </tr> 650 <?php endforeach; ?> 651 </tbody> 652 </table> 653 <?php endif; ?> 654 655 <h2><?php esc_html_e( 'Count refusals from existing orders', 'takeed-refusal-guard' ); ?></h2> 656 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 657 <input type="hidden" name="action" value="takrg_scan" /> 658 <input type="hidden" name="takrg_page" value="<?php echo esc_attr( (string) $scan_page ); ?>" /> 659 <?php wp_nonce_field( 'takrg_scan' ); ?> 660 <p> 661 <button type="submit" class="button"><?php echo $scan_page > 1 ? esc_html__( 'Continue scan', 'takeed-refusal-guard' ) : esc_html__( 'Scan existing orders', 'takeed-refusal-guard' ); ?></button> 662 </p> 663 <p class="description"><?php esc_html_e( 'Adds a record for every order that is already in a counted status. Orders that already have a record are skipped, so it is safe to repeat.', 'takeed-refusal-guard' ); ?></p> 464 664 </form> 465 665 … … 533 733 } 534 734 ?> 735 736 <h2><?php esc_html_e( 'Activity log', 'takeed-refusal-guard' ); ?></h2> 737 <p class="description"><?php esc_html_e( 'The last events. Customers are masked. In test mode, "Would apply" is what Live mode would have done.', 'takeed-refusal-guard' ); ?></p> 738 <table class="widefat striped"> 739 <thead><tr> 740 <th><?php esc_html_e( 'Date (UTC)', 'takeed-refusal-guard' ); ?></th> 741 <th><?php esc_html_e( 'Event', 'takeed-refusal-guard' ); ?></th> 742 <th><?php esc_html_e( 'Action', 'takeed-refusal-guard' ); ?></th> 743 <th><?php esc_html_e( 'Customer', 'takeed-refusal-guard' ); ?></th> 744 <th><?php esc_html_e( 'Refusals', 'takeed-refusal-guard' ); ?></th> 745 <th><?php esc_html_e( 'Order', 'takeed-refusal-guard' ); ?></th> 746 <th><?php esc_html_e( 'Details', 'takeed-refusal-guard' ); ?></th> 747 </tr></thead> 748 <tbody> 749 <?php $events = TAKRG_Log::all( 50 ); ?> 750 <?php if ( ! $events ) : ?> 751 <tr><td colspan="7"><?php esc_html_e( 'Nothing yet.', 'takeed-refusal-guard' ); ?></td></tr> 752 <?php endif; ?> 753 <?php foreach ( $events as $e ) : ?> 754 <tr> 755 <td><?php echo esc_html( $e['created_at'] ); ?></td> 756 <td><?php echo esc_html( self::event_label( $e['event'] ) ); ?></td> 757 <td><?php echo esc_html( $e['action'] ); ?></td> 758 <td dir="ltr"><?php echo esc_html( $e['who'] ); ?></td> 759 <td><?php echo esc_html( (string) $e['refusals'] ); ?></td> 760 <td><?php echo $e['order_id'] ? esc_html( '#' . $e['order_id'] ) : '–'; ?></td> 761 <td><?php echo esc_html( $e['detail'] ); ?></td> 762 </tr> 763 <?php endforeach; ?> 764 </tbody> 765 </table> 535 766 </div> 536 767 <?php 537 768 } 769 770 /** 771 * Readable name of a log event. 772 * 773 * @param string $event Event key. 774 * @return string 775 */ 776 private static function event_label( $event ) { 777 $labels = array( 778 'tagged' => __( 'Order tagged', 'takeed-refusal-guard' ), 779 'would_apply' => __( 'Would apply (test mode)', 'takeed-refusal-guard' ), 780 'applied' => __( 'Applied', 'takeed-refusal-guard' ), 781 'breaker' => __( 'Safety breaker', 'takeed-refusal-guard' ), 782 'error' => __( 'Error (order allowed)', 'takeed-refusal-guard' ), 783 ); 784 return isset( $labels[ $event ] ) ? $labels[ $event ] : $event; 785 } 538 786 } -
takeed-refusal-guard/tags/1.1.0/includes/class-takrg-checkout.php
r3806545 r3807599 29 29 add_action( 'woocommerce_after_checkout_validation', array( __CLASS__, 'validate_classic' ), 10, 2 ); 30 30 add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( __CLASS__, 'validate_store_api' ), 10, 2 ); 31 add_filter( 'rest_request_before_callbacks', array( __CLASS__, 'record_store_api_attempt' ), 10, 3 ); 31 32 add_action( 'woocommerce_review_order_before_payment', array( __CLASS__, 'render_notice' ) ); 32 33 add_action( 'wp_enqueue_scripts', array( __CLASS__, 'refresh_script' ), 20 ); … … 44 45 45 46 /** 46 * Refusal count for raw (not yet normalised) checkout values, and the settings that apply. 47 * Judges a customer given as typed at the checkout: refusal count, allow-list, block rules, threshold. 48 * 49 * @param string $phone Phone as typed. 50 * @param string $email E-mail as typed. 51 * @param string $country Billing country. 52 * @return array count, risky, allowed, key (opaque customer key), who (masked). 53 */ 54 public static function assess( $phone, $email, $country = '' ) { 55 $np = TAKRG_Phone::normalize( $phone, TAKRG_Recorder::country_for( $country ) ); 56 $ne = TAKRG_Phone::normalize_email( $email ); 57 $key = $np . '|' . $ne; 58 if ( isset( self::$counts[ $key ] ) ) { 59 return self::$counts[ $key ]; 60 } 61 $allowed = ( '' !== $np || '' !== $ne ) && TAKRG_Rules::matches( TAKRG_Rules::ALLOW, $np, $ne ); 62 $count = TAKRG_Store::count( $np, $ne ); 63 if ( ! $allowed && ( '' !== $np || '' !== $ne ) && TAKRG_Rules::matches( TAKRG_Rules::BLOCK, $np, $ne ) ) { 64 $count = max( $count, (int) TAKRG_Settings::get( 'threshold' ) ); 65 } 66 $risky = ! $allowed && self::is_risky( $count, $phone, $email ); 67 self::$counts[ $key ] = array( 68 'count' => $count, 69 'risky' => $risky, 70 'allowed' => $allowed, 71 'key' => substr( hash( 'sha256', $key . wp_salt( 'auth' ) ), 0, 12 ), 72 'who' => TAKRG_Log::mask( $np, $ne ), 73 ); 74 return self::$counts[ $key ]; 75 } 76 77 /** 78 * Refusal count for raw (not yet normalised) checkout values. 47 79 * 48 80 * @param string $phone Phone as typed. … … 52 84 */ 53 85 public static function count_for( $phone, $email, $country = '' ) { 54 $phone = TAKRG_Phone::normalize( $phone, TAKRG_Recorder::country_for( $country ) ); 55 $email = TAKRG_Phone::normalize_email( $email ); 56 $key = $phone . '|' . $email; 57 if ( ! isset( self::$counts[ $key ] ) ) { 58 self::$counts[ $key ] = TAKRG_Store::count( $phone, $email ); 59 } 60 return self::$counts[ $key ]; 86 return self::assess( $phone, $email, $country )['count']; 61 87 } 62 88 … … 72 98 $risky = $count >= (int) TAKRG_Settings::get( 'threshold' ) && $count > 0; 73 99 return (bool) apply_filters( 'takrg_is_risky', $risky, $count, $phone, $email ); 100 } 101 102 /** 103 * Runs a check. Any exception means "allow": a failure of this plugin must never stop a sale. 104 * 105 * @param callable $check The check. 106 * @param mixed $fallback What to return when the check fails. 107 * @return mixed 108 */ 109 private static function guard( $check, $fallback ) { 110 try { 111 return $check(); 112 } catch ( \Throwable $e ) { 113 TAKRG_Log::add( 114 'error', 115 array( 116 'detail' => get_class( $e ) . ': ' . $e->getMessage(), 117 ) 118 ); 119 return $fallback; 120 } 74 121 } 75 122 … … 126 173 private static function request_risk() { 127 174 list( $phone, $email, $country ) = self::request_identity(); 128 $ count = self::count_for( $phone, $email, $country );129 return array( self::is_risky( $count, $phone, $email ), $count);175 $a = self::assess( $phone, $email, $country ); 176 return array( $a['risky'], $a['count'] ); 130 177 } 131 178 … … 138 185 */ 139 186 public static function filter_gateways( $gateways ) { 140 if ( ! is_array( $gateways ) || ! isset( $gateways[ self::COD ] ) || 'hide' !== TAKRG_Settings::get( 'action' )) {187 if ( ! is_array( $gateways ) || ! isset( $gateways[ self::COD ] ) ) { 141 188 return $gateways; 142 189 } 143 if ( is_admin() && ! wp_doing_ajax() ) { 144 return $gateways; 145 } 146 list( $risky ) = self::request_risk(); 147 if ( $risky ) { 148 unset( $gateways[ self::COD ] ); 149 } 150 return $gateways; 190 return self::guard( 191 function () use ( $gateways ) { 192 if ( 'hide' !== TAKRG_Settings::effective_action() || ( is_admin() && ! wp_doing_ajax() ) ) { 193 return $gateways; 194 } 195 list( $risky ) = self::request_risk(); 196 if ( $risky ) { 197 unset( $gateways[ self::COD ] ); 198 } 199 return $gateways; 200 }, 201 $gateways 202 ); 151 203 } 152 204 … … 161 213 162 214 /** 163 * Whether this shop's action restricts cash on delivery. 164 * 165 * @return bool 166 */ 167 private static function restricts() { 168 return in_array( TAKRG_Settings::get( 'action' ), array( 'hide', 'notice' ), true ); 215 * Decides one checkout attempt: should cash on delivery be refused for this customer? Also feeds the safety 216 * breaker and the activity log. Never throws. 217 * 218 * @param string $phone Phone as typed. 219 * @param string $email E-mail as typed. 220 * @param string $country Billing country. 221 * @param string $method Chosen payment method. 222 * @param bool $record Whether this attempt still has to be counted by the safety breaker. 223 * @return bool True when the plugin must refuse cash on delivery. 224 */ 225 private static function refuse_cod( $phone, $email, $country, $method, $record = true ) { 226 return (bool) self::guard( 227 function () use ( $phone, $email, $country, $method, $record ) { 228 $intended = (string) TAKRG_Settings::get( 'action' ); 229 $live = 'live' === TAKRG_Settings::get( 'mode' ) && ! TAKRG_Breaker::is_tripped(); 230 $a = self::assess( $phone, $email, $country ); 231 $acts = $a['risky'] && in_array( $intended, array( 'hide', 'notice' ), true ); 232 if ( $record && 'live' === TAKRG_Settings::get( 'mode' ) ) { 233 TAKRG_Breaker::record( $a['key'], $acts ); 234 } 235 if ( ! $acts ) { 236 return false; 237 } 238 if ( self::COD !== $method ) { 239 return false; 240 } 241 TAKRG_Log::add( 242 $live ? 'applied' : 'would_apply', 243 array( 244 'action' => $intended, 245 'who' => $a['who'], 246 'refusals' => $a['count'], 247 ) 248 ); 249 return $live; 250 }, 251 false 252 ); 169 253 } 170 254 … … 176 260 */ 177 261 public static function validate_classic( $data, $errors ) { 178 if ( ! self::restricts() || ! is_array( $data ) || ! isset( $data['payment_method'] ) || self::COD !== $data['payment_method']) {262 if ( ! is_array( $data ) ) { 179 263 return; 180 264 } 181 $phone = isset( $data['billing_phone'] ) ? (string) $data['billing_phone'] : ''; 182 $email = isset( $data['billing_email'] ) ? (string) $data['billing_email'] : ''; 183 $country = isset( $data['billing_country'] ) ? (string) $data['billing_country'] : ''; 184 $count = self::count_for( $phone, $email, $country ); 185 if ( self::is_risky( $count, $phone, $email ) ) { 265 $method = isset( $data['payment_method'] ) ? (string) $data['payment_method'] : ''; 266 if ( self::refuse_cod( isset( $data['billing_phone'] ) ? (string) $data['billing_phone'] : '', isset( $data['billing_email'] ) ? (string) $data['billing_email'] : '', isset( $data['billing_country'] ) ? (string) $data['billing_country'] : '', $method ) ) { 186 267 $errors->add( 'takrg_cod_unavailable', self::message() ); 187 268 } 269 } 270 271 /** 272 * Block checkout: counts the attempt for the safety breaker as soon as the Store API receives it, before 273 * WooCommerce itself may refuse the payment method (action "hide"). 274 * 275 * @param mixed $response Response so far. 276 * @param array $handler Route handler. 277 * @param WP_REST_Request $request Request. 278 * @return mixed The response, unchanged. 279 */ 280 public static function record_store_api_attempt( $response, $handler, $request ) { 281 unset( $handler ); 282 if ( $request instanceof WP_REST_Request && 'POST' === $request->get_method() && 1 === preg_match( '#^/wc/store/v\d+/checkout$#', $request->get_route() ) ) { 283 self::guard( 284 function () use ( $request ) { 285 $b = $request->get_param( 'billing_address' ); 286 $b = is_array( $b ) ? $b : array(); 287 $a = self::assess( isset( $b['phone'] ) ? (string) $b['phone'] : '', isset( $b['email'] ) ? (string) $b['email'] : '', isset( $b['country'] ) ? (string) $b['country'] : '' ); 288 if ( 'live' === TAKRG_Settings::get( 'mode' ) ) { 289 TAKRG_Breaker::record( $a['key'], $a['risky'] && in_array( TAKRG_Settings::get( 'action' ), array( 'hide', 'notice' ), true ) ); 290 } 291 }, 292 null 293 ); 294 } 295 return $response; 188 296 } 189 297 … … 196 304 */ 197 305 public static function validate_store_api( $order, $request ) { 198 if ( ! self::restricts() || self::COD !== (string) $request['payment_method'] ) { 199 return; 200 } 201 $phone = (string) $order->get_billing_phone(); 202 $email = (string) $order->get_billing_email(); 203 $country = (string) $order->get_billing_country(); 204 $count = self::count_for( $phone, $email, $country ); 205 if ( self::is_risky( $count, $phone, $email ) && class_exists( '\Automattic\WooCommerce\StoreApi\Exceptions\RouteException' ) ) { 306 $refuse = self::guard( 307 function () use ( $order, $request ) { 308 return self::refuse_cod( (string) $order->get_billing_phone(), (string) $order->get_billing_email(), (string) $order->get_billing_country(), (string) $request['payment_method'], false ); 309 }, 310 false 311 ); 312 if ( $refuse && class_exists( '\Automattic\WooCommerce\StoreApi\Exceptions\RouteException' ) ) { 206 313 throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'takrg_cod_unavailable', esc_html( self::message() ), 400 ); 207 314 } … … 214 321 */ 215 322 public static function refresh_script() { 216 if ( 'tag' === TAKRG_Settings:: get( 'action') || ! function_exists( 'is_checkout' ) || ! is_checkout() ) {323 if ( 'tag' === TAKRG_Settings::effective_action() || ! function_exists( 'is_checkout' ) || ! is_checkout() ) { 217 324 return; 218 325 } … … 227 334 private static function notice_html() { 228 335 $inner = ''; 229 if ( 'notice' === TAKRG_Settings:: get( 'action') ) {336 if ( 'notice' === TAKRG_Settings::effective_action() ) { 230 337 list( $risky ) = self::request_risk(); 231 338 if ( $risky ) { … … 241 348 */ 242 349 public static function render_notice() { 243 if ( 'notice' === TAKRG_Settings:: get( 'action') ) {244 echo wp_kses_post( self:: notice_html() );350 if ( 'notice' === TAKRG_Settings::effective_action() ) { 351 echo wp_kses_post( self::guard( array( __CLASS__, 'notice_html' ), '' ) ); 245 352 } 246 353 } … … 253 360 */ 254 361 public static function notice_fragment( $fragments ) { 255 if ( 'notice' === TAKRG_Settings::get( 'action' ) && is_array( $fragments ) ) { 256 $fragments['.takrg-notice-slot'] = wp_kses_post( self::notice_html() ); 362 if ( 'notice' === TAKRG_Settings::effective_action() && is_array( $fragments ) ) { 363 $html = self::guard( array( __CLASS__, 'notice_html' ), '' ); 364 if ( '' !== $html ) { 365 $fragments['.takrg-notice-slot'] = wp_kses_post( $html ); 366 } 257 367 } 258 368 return $fragments; … … 277 387 */ 278 388 public static function tag_order( $order ) { 279 if ( ! $order || $order->get_meta( '_takrg_risk' ) ) { 280 return; 281 } 282 list( $phone, $email ) = TAKRG_Recorder::identity( $order ); 283 $count = TAKRG_Store::count( $phone, $email ); 284 if ( ! self::is_risky( $count, $phone, $email ) ) { 285 return; 286 } 287 $order->update_meta_data( '_takrg_risk', $count ); 288 /* translators: %d: number of refused cash-on-delivery orders. */ 289 $order->add_order_note( sprintf( _n( 'COD risk: this customer (phone or e-mail) refused %d earlier order.', 'COD risk: this customer (phone or e-mail) refused %d earlier orders.', $count, 'takeed-refusal-guard' ), $count ) ); 290 $order->save(); 291 do_action( 'takrg_order_tagged', $order, $count ); 389 self::guard( 390 function () use ( $order ) { 391 if ( ! $order || $order->get_meta( '_takrg_risk' ) ) { 392 return; 393 } 394 $a = self::assess( $order->get_billing_phone(), $order->get_billing_email(), $order->get_billing_country() ); 395 if ( ! $a['risky'] ) { 396 return; 397 } 398 $count = $a['count']; 399 $order->update_meta_data( '_takrg_risk', $count ); 400 /* translators: %d: number of refused cash-on-delivery orders. */ 401 $order->add_order_note( sprintf( _n( 'COD risk: this customer (phone or e-mail) refused %d earlier order.', 'COD risk: this customer (phone or e-mail) refused %d earlier orders.', $count, 'takeed-refusal-guard' ), $count ) ); 402 $order->save(); 403 TAKRG_Log::add( 404 'tagged', 405 array( 406 'action' => 'tag', 407 'who' => $a['who'], 408 'refusals' => $count, 409 'order_id' => $order->get_id(), 410 ) 411 ); 412 do_action( 'takrg_order_tagged', $order, $count ); 413 }, 414 null 415 ); 292 416 } 293 417 } -
takeed-refusal-guard/tags/1.1.0/includes/class-takrg-plugin.php
r3806545 r3807599 13 13 final class TAKRG_Plugin { 14 14 15 const CRON = 'takrg_daily_cleanup'; 16 15 17 /** 16 * Activation: create the table .18 * Activation: create the tables and schedule the daily cleanup. 17 19 */ 18 20 public static function activate() { 19 21 TAKRG_Store::install(); 22 self::schedule(); 23 } 24 25 /** 26 * Deactivation: stop the daily cleanup. 27 */ 28 public static function deactivate() { 29 wp_clear_scheduled_hook( self::CRON ); 30 } 31 32 /** 33 * Schedules the daily cleanup (retention) once. 34 */ 35 public static function schedule() { 36 if ( ! wp_next_scheduled( self::CRON ) ) { 37 wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON ); 38 } 39 } 40 41 /** 42 * Daily cleanup: records older than the retention the administrator chose. 43 */ 44 public static function cleanup() { 45 TAKRG_Store::purge_older_than( (int) TAKRG_Settings::get( 'retention' ) ); 46 } 47 48 /** 49 * Update from an older version. Shops that already use 1.0.x keep the behaviour they chose (live); only a 50 * fresh installation starts in test mode. 51 */ 52 public static function upgrade() { 53 $from = (string) get_option( TAKRG_Store::DB_VERSION_OPTION, '' ); 54 TAKRG_Store::install(); 55 if ( '' !== $from && version_compare( $from, '2', '<' ) ) { 56 $saved = get_option( TAKRG_Settings::OPTION, null ); 57 if ( is_array( $saved ) && ! isset( $saved['mode'] ) ) { 58 $saved['mode'] = 'live'; 59 update_option( TAKRG_Settings::OPTION, $saved ); 60 } 61 } 20 62 } 21 63 … … 28 70 } 29 71 if ( get_option( TAKRG_Store::DB_VERSION_OPTION ) !== TAKRG_Store::DB_VERSION ) { 30 TAKRG_Store::install();72 self::upgrade(); 31 73 } 74 self::schedule(); 75 add_action( self::CRON, array( __CLASS__, 'cleanup' ) ); 32 76 TAKRG_Recorder::init(); 33 77 TAKRG_Checkout::init(); -
takeed-refusal-guard/tags/1.1.0/includes/class-takrg-privacy.php
r3806545 r3807599 109 109 ); 110 110 } 111 if ( 1 === $page && $email ) { // The person's own allow-list / block-list entries. 112 foreach ( TAKRG_Rules::for_person( $email, self::phones_of( $email ) ) as $rule ) { 113 $export[] = array( 114 'group_id' => 'takrg-rules', 115 'group_label' => __( 'COD allow-list and block-list entries', 'takeed-refusal-guard' ), 116 'group_description' => __( 'Entries added by the shop administrator.', 'takeed-refusal-guard' ), 117 'item_id' => 'takrg-rule-' . $rule['id'], 118 'data' => array( 119 array( 120 'name' => __( 'List', 'takeed-refusal-guard' ), 121 'value' => $rule['type'], 122 ), 123 array( 124 'name' => __( 'Value', 'takeed-refusal-guard' ), 125 'value' => $rule['value'], 126 ), 127 array( 128 'name' => __( 'Date (UTC)', 'takeed-refusal-guard' ), 129 'value' => $rule['created_at'], 130 ), 131 ), 132 ); 133 } 134 } 111 135 return array( 112 136 'data' => $export, … … 132 156 } 133 157 $removed += TAKRG_Store::delete_by_identity( '', $email ); 158 $removed += TAKRG_Rules::delete_exact( $email, $phones ); 134 159 TAKRG_Checkout::reset_cache(); 135 160 } … … 152 177 'Takeed Refusal Guard for WooCommerce', 153 178 wp_kses_post( 154 '<p class="privacy-policy-tutorial">' . __( 'Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery . Nothing is sent to the plugin author or to any third party.', 'takeed-refusal-guard' ) . '</p>' .179 '<p class="privacy-policy-tutorial">' . __( 'Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery, the entries you add to its allow-list and block-list, and an activity log in which customers appear masked. Nothing is sent to the plugin author or to any third party.', 'takeed-refusal-guard' ) . '</p>' . 155 180 '<p><strong class="privacy-policy-tutorial">' . __( 'Suggested text:', 'takeed-refusal-guard' ) . '</strong> ' . __( 'If you refuse a cash-on-delivery parcel, we keep your phone number and e-mail address together with the order number so that we can offer other payment methods for your next orders. You can ask us to erase this record.', 'takeed-refusal-guard' ) . '</p>' 156 181 ) -
takeed-refusal-guard/tags/1.1.0/includes/class-takrg-recorder.php
r3806545 r3807599 27 27 add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', array( __CLASS__, 'handle_bulk' ), 10, 3 ); 28 28 add_action( 'takrg_signal', array( __CLASS__, 'on_signal' ), 10, 4 ); 29 add_filter( 'woocommerce_order_actions', array( __CLASS__, 'order_actions' ) ); 30 add_action( 'woocommerce_order_action_takrg_mark_refused', array( __CLASS__, 'action_mark_refused' ) ); 31 add_action( 'woocommerce_order_action_takrg_clear_refusal', array( __CLASS__, 'action_clear_refusal' ) ); 29 32 } 30 33 … … 91 94 92 95 /** 96 * Entries of the "Order actions" box on the order screen (legacy and HPOS). 97 * 98 * @param array $actions Order actions. 99 * @return array 100 */ 101 public static function order_actions( $actions ) { 102 $actions['takrg_mark_refused'] = __( 'Mark as refused at delivery', 'takeed-refusal-guard' ); 103 $actions['takrg_clear_refusal'] = __( 'Clear the refusal record of this order', 'takeed-refusal-guard' ); 104 return $actions; 105 } 106 107 /** 108 * Order action: set the refused status. 109 * 110 * @param WC_Order $order Order. 111 */ 112 public static function action_mark_refused( $order ) { 113 if ( $order && current_user_can( TAKRG_Admin::CAP ) ) { 114 $order->update_status( self::STATUS, __( 'Marked as refused at delivery.', 'takeed-refusal-guard' ) ); 115 } 116 } 117 118 /** 119 * Order action: forget this order's refusal (it was a mistake). 120 * 121 * @param WC_Order $order Order. 122 */ 123 public static function action_clear_refusal( $order ) { 124 if ( $order && current_user_can( TAKRG_Admin::CAP ) ) { 125 TAKRG_Store::remove_for_order( $order->get_id() ); 126 TAKRG_Checkout::reset_cache(); 127 $order->add_order_note( __( 'Refusal record cleared.', 'takeed-refusal-guard' ) ); 128 } 129 } 130 131 /** 93 132 * Country used to read a number typed without a country code: the order's own billing country when we 94 133 * support it, else the configured default. -
takeed-refusal-guard/tags/1.1.0/includes/class-takrg-settings.php
r3806545 r3807599 23 23 public static function defaults() { 24 24 return array( 25 'mode' => 'test', 25 26 'threshold' => 1, 26 27 'action' => 'tag', 27 28 'statuses' => array( 'takeed-refused' ), 28 29 'default_country' => self::guess_country(), 30 'retention' => 0, 29 31 ); 32 } 33 34 /** 35 * The action that is really applied now. Test mode and a tripped safety breaker both mean "tag only". 36 * 37 * @return string tag | hide | notice. 38 */ 39 public static function effective_action() { 40 if ( 'live' !== self::get( 'mode' ) || TAKRG_Breaker::is_tripped() ) { 41 return 'tag'; 42 } 43 $action = self::get( 'action' ); 44 return in_array( $action, self::ACTIONS, true ) ? $action : 'tag'; 30 45 } 31 46 … … 93 108 $out = $defaults; 94 109 110 $mode = isset( $input['mode'] ) ? sanitize_key( $input['mode'] ) : ''; 111 $out['mode'] = in_array( $mode, array( 'test', 'live' ), true ) ? $mode : 'test'; 112 113 $out['retention'] = isset( $input['retention'] ) ? min( 120, absint( $input['retention'] ) ) : 0; 114 95 115 $out['threshold'] = isset( $input['threshold'] ) ? max( 1, min( 100, absint( $input['threshold'] ) ) ) : $defaults['threshold']; 96 116 -
takeed-refusal-guard/tags/1.1.0/includes/class-takrg-store.php
r3806545 r3807599 15 15 16 16 const DB_VERSION_OPTION = 'takrg_db_version'; 17 const DB_VERSION = ' 1';17 const DB_VERSION = '2'; 18 18 19 19 /** … … 49 49 ) {$charset};" 50 50 ); 51 TAKRG_Rules::install(); 52 TAKRG_Log::install(); 51 53 update_option( self::DB_VERSION_OPTION, self::DB_VERSION ); 54 } 55 56 /** 57 * Deletes records older than a number of months (retention). 0 keeps everything. 58 * 59 * @param int $months Months to keep. 60 */ 61 public static function purge_older_than( $months ) { 62 global $wpdb; 63 $months = absint( $months ); 64 if ( $months < 1 ) { 65 return; 66 } 67 $before = gmdate( 'Y-m-d H:i:s', time() - $months * 30 * DAY_IN_SECONDS ); 68 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Our own table. 69 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE created_at < %s', self::table(), $before ) ); 70 TAKRG_Log::purge_before( $before ); 52 71 } 53 72 -
takeed-refusal-guard/tags/1.1.0/languages/takeed-refusal-guard.pot
r3806545 r3807599 10 10 "X-Domain: takeed-refusal-guard\n" 11 11 12 #: includes/class-takrg-admin.php:5 2 includes/class-takrg-admin.php:38812 #: includes/class-takrg-admin.php:57 includes/class-takrg-admin.php:499 13 13 msgid "Refusal Guard" 14 14 msgstr "" 15 15 16 #: includes/class-takrg-admin.php:8 1 includes/class-takrg-admin.php:8516 #: includes/class-takrg-admin.php:86 includes/class-takrg-admin.php:90 17 17 msgid "COD risk" 18 18 msgstr "" 19 19 20 #: includes/class-takrg-admin.php:1 2820 #: includes/class-takrg-admin.php:133 21 21 msgid "No refusals" 22 22 msgstr "" 23 23 24 #: includes/class-takrg-admin.php:1 39 includes/class-takrg-admin.php:36924 #: includes/class-takrg-admin.php:144 includes/class-takrg-admin.php:478 25 25 msgid "You are not allowed to do this." 26 26 msgstr "" 27 27 28 #: includes/class-takrg-admin.php:326 28 #: includes/class-takrg-admin.php:407 29 msgid "Takeed Refusal Guard switched itself to \"tag only\"." 30 msgstr "" 31 32 #: includes/class-takrg-admin.php:408 33 msgid "More than half of the recent checkout attempts were restricted, so the plugin stopped restricting Cash on Delivery to protect your sales." 34 msgstr "" 35 36 #: includes/class-takrg-admin.php:409 37 msgid "Review and turn it back on" 38 msgstr "" 39 40 #: includes/class-takrg-admin.php:423 29 41 msgid "%d record added." 30 42 msgid_plural "%d records added." … … 32 44 msgstr[1] "" 33 45 34 #: includes/class-takrg-admin.php: 32946 #: includes/class-takrg-admin.php:426 35 47 msgid "%d record removed." 36 48 msgid_plural "%d records removed." … … 38 50 msgstr[1] "" 39 51 40 #: includes/class-takrg-admin.php: 33252 #: includes/class-takrg-admin.php:429 41 53 msgid "%d record imported." 42 54 msgid_plural "%d records imported." … … 44 56 msgstr[1] "" 45 57 46 #: includes/class-takrg-admin.php:334 58 #: includes/class-takrg-admin.php:431 59 msgid "Rule added." 60 msgstr "" 61 62 #: includes/class-takrg-admin.php:433 63 msgid "Enter an e-mail address, a phone number, or a pattern such as *@example.com or +21620* (not already in the list)." 64 msgstr "" 65 66 #: includes/class-takrg-admin.php:435 67 msgid "Protection turned back on." 68 msgstr "" 69 70 #: includes/class-takrg-admin.php:438 71 msgid "Scan finished: %d record added." 72 msgid_plural "Scan finished: %d records added." 73 msgstr[0] "" 74 msgstr[1] "" 75 76 #: includes/class-takrg-admin.php:441 77 msgid "%d record added. More orders remain: press \"Continue scan\"." 78 msgid_plural "%d records added. More orders remain: press \"Continue scan\"." 79 msgstr[0] "" 80 msgstr[1] "" 81 82 #: includes/class-takrg-admin.php:443 47 83 msgid "Enter a valid phone number or e-mail address." 48 84 msgstr "" 49 85 50 #: includes/class-takrg-admin.php: 33686 #: includes/class-takrg-admin.php:445 51 87 msgid "Choose a .csv file of 1 MB or less." 52 88 msgstr "" 53 89 54 #: includes/class-takrg-admin.php: 34890 #: includes/class-takrg-admin.php:457 55 91 msgid "Tunisia (+216)" 56 92 msgstr "" 57 93 58 #: includes/class-takrg-admin.php: 34994 #: includes/class-takrg-admin.php:458 59 95 msgid "Morocco (+212)" 60 96 msgstr "" 61 97 62 #: includes/class-takrg-admin.php: 35098 #: includes/class-takrg-admin.php:459 63 99 msgid "Algeria (+213)" 64 100 msgstr "" 65 101 66 #: includes/class-takrg-admin.php: 351102 #: includes/class-takrg-admin.php:460 67 103 msgid "Egypt (+20)" 68 104 msgstr "" 69 105 70 #: includes/class-takrg-admin.php: 352106 #: includes/class-takrg-admin.php:461 71 107 msgid "Saudi Arabia (+966)" 72 108 msgstr "" 73 109 74 #: includes/class-takrg-admin.php: 353110 #: includes/class-takrg-admin.php:462 75 111 msgid "United Arab Emirates (+971)" 76 112 msgstr "" 77 113 78 #: includes/class-takrg-admin.php: 354114 #: includes/class-takrg-admin.php:463 79 115 msgid "Kuwait (+965)" 80 116 msgstr "" 81 117 82 #: includes/class-takrg-admin.php: 355118 #: includes/class-takrg-admin.php:464 83 119 msgid "Qatar (+974)" 84 120 msgstr "" 85 121 86 #: includes/class-takrg-admin.php: 356122 #: includes/class-takrg-admin.php:465 87 123 msgid "Oman (+968)" 88 124 msgstr "" 89 125 90 #: includes/class-takrg-admin.php: 357126 #: includes/class-takrg-admin.php:466 91 127 msgid "Bahrain (+973)" 92 128 msgstr "" 93 129 94 #: includes/class-takrg-admin.php: 382130 #: includes/class-takrg-admin.php:493 95 131 msgid "Tag only (default)" 96 132 msgstr "" 97 133 98 #: includes/class-takrg-admin.php: 382134 #: includes/class-takrg-admin.php:493 99 135 msgid "Adds an order note and the meta \"_takrg_risk\". Nothing is blocked." 100 136 msgstr "" 101 137 102 #: includes/class-takrg-admin.php: 383138 #: includes/class-takrg-admin.php:494 103 139 msgid "Hide Cash on Delivery" 104 140 msgstr "" 105 141 106 #: includes/class-takrg-admin.php: 383142 #: includes/class-takrg-admin.php:494 107 143 msgid "Cash on Delivery is not offered to this customer; other payment methods stay available." 108 144 msgstr "" 109 145 110 #: includes/class-takrg-admin.php: 384146 #: includes/class-takrg-admin.php:495 111 147 msgid "Show a notice" 112 148 msgstr "" 113 149 114 #: includes/class-takrg-admin.php: 384150 #: includes/class-takrg-admin.php:495 115 151 msgid "Shows a neutral message asking the customer to choose another payment method, and refuses Cash on Delivery on submit." 116 152 msgstr "" 117 153 118 #: includes/class-takrg-admin.php:397 154 #: includes/class-takrg-admin.php:506 155 msgid "The safety breaker switched the plugin to \"tag only\"." 156 msgstr "" 157 158 #: includes/class-takrg-admin.php:507 159 msgid "More than half of the checkout attempts in an hour were restricted. Check the activity log below, then turn the protection back on." 160 msgstr "" 161 162 #: includes/class-takrg-admin.php:511 163 msgid "Turn protection back on" 164 msgstr "" 165 166 #: includes/class-takrg-admin.php:516 167 msgid "Test mode is on." 168 msgstr "" 169 170 #: includes/class-takrg-admin.php:517 171 msgid "Nothing is hidden or refused: orders are only tagged, and the activity log below shows what the plugin would have done. When the log looks right, switch to Live." 172 msgstr "" 173 174 #: includes/class-takrg-admin.php:524 175 msgid "Mode" 176 msgstr "" 177 178 #: includes/class-takrg-admin.php:527 179 msgid "Test mode (default)" 180 msgstr "" 181 182 #: includes/class-takrg-admin.php:528 183 msgid "Only tags orders and writes what it would have done to the activity log." 184 msgstr "" 185 186 #: includes/class-takrg-admin.php:529 187 msgid "Live" 188 msgstr "" 189 190 #: includes/class-takrg-admin.php:530 191 msgid "Applies the action below. A safety breaker returns to \"tag only\" if more than 50% of the checkout attempts in an hour (at least 5) are restricted." 192 msgstr "" 193 194 #: includes/class-takrg-admin.php:535 119 195 msgid "Refusals before acting" 120 196 msgstr "" 121 197 122 #: includes/class-takrg-admin.php: 400198 #: includes/class-takrg-admin.php:538 123 199 msgid "A customer (same phone number or same e-mail) with at least this many refusals is treated as a risk." 124 200 msgstr "" 125 201 126 #: includes/class-takrg-admin.php: 404202 #: includes/class-takrg-admin.php:542 127 203 msgid "Action at checkout" 128 204 msgstr "" 129 205 130 #: includes/class-takrg-admin.php: 415206 #: includes/class-takrg-admin.php:553 131 207 msgid "Statuses that count as a refusal" 132 208 msgstr "" 133 209 134 #: includes/class-takrg-admin.php: 423210 #: includes/class-takrg-admin.php:561 135 211 msgid "Default: only \"Refused at delivery\". Be careful with statuses such as Cancelled: customers cancel for many reasons." 136 212 msgstr "" 137 213 138 #: includes/class-takrg-admin.php: 427214 #: includes/class-takrg-admin.php:565 139 215 msgid "Default country" 140 216 msgstr "" 141 217 142 #: includes/class-takrg-admin.php: 430218 #: includes/class-takrg-admin.php:568 143 219 msgid "Used to read phone numbers typed without a country code." 144 220 msgstr "" 145 221 146 #: includes/class-takrg-admin.php:438 222 #: includes/class-takrg-admin.php:572 223 msgid "Forget refusals after (months)" 224 msgstr "" 225 226 #: includes/class-takrg-admin.php:575 227 msgid "0 keeps them until you delete them. Older records and log entries are removed once a day." 228 msgstr "" 229 230 #: includes/class-takrg-admin.php:583 147 231 msgid "Add or remove a number" 148 232 msgstr "" 149 233 150 #: includes/class-takrg-admin.php: 443 includes/class-takrg-admin.php:457 includes/class-takrg-admin.php:490 includes/class-takrg-privacy.php:93234 #: includes/class-takrg-admin.php:588 includes/class-takrg-admin.php:602 includes/class-takrg-admin.php:690 includes/class-takrg-privacy.php:93 151 235 msgid "Phone" 152 236 msgstr "" 153 237 154 #: includes/class-takrg-admin.php: 445 includes/class-takrg-admin.php:459238 #: includes/class-takrg-admin.php:590 includes/class-takrg-admin.php:604 includes/class-takrg-admin.php:622 155 239 msgid "Country" 156 240 msgstr "" 157 241 158 #: includes/class-takrg-admin.php: 446 includes/class-takrg-admin.php:460 includes/class-takrg-admin.php:491 includes/class-takrg-privacy.php:97242 #: includes/class-takrg-admin.php:591 includes/class-takrg-admin.php:605 includes/class-takrg-admin.php:691 includes/class-takrg-privacy.php:97 159 243 msgid "E-mail" 160 244 msgstr "" 161 245 162 #: includes/class-takrg-admin.php: 448246 #: includes/class-takrg-admin.php:593 includes/class-takrg-admin.php:744 163 247 msgid "Refusals" 164 248 msgstr "" 165 249 166 #: includes/class-takrg-admin.php: 450250 #: includes/class-takrg-admin.php:595 167 251 msgid "Add" 168 252 msgstr "" 169 253 170 #: includes/class-takrg-admin.php: 462254 #: includes/class-takrg-admin.php:607 171 255 msgid "Remove all records of this number or e-mail" 172 256 msgstr "" 173 257 174 #: includes/class-takrg-admin.php:466 258 #: includes/class-takrg-admin.php:611 259 msgid "Allow-list and block-list" 260 msgstr "" 261 262 #: includes/class-takrg-admin.php:612 263 msgid "Allow: never act on this customer, whatever the refusals. Block: always treat this customer as having reached the threshold. Use an e-mail, a phone number, or a pattern with * such as *@example.com or +21620*." 264 msgstr "" 265 266 #: includes/class-takrg-admin.php:617 includes/class-takrg-admin.php:630 includes/class-takrg-privacy.php:120 267 msgid "List" 268 msgstr "" 269 270 #: includes/class-takrg-admin.php:618 includes/class-takrg-admin.php:638 271 msgid "Allow" 272 msgstr "" 273 274 #: includes/class-takrg-admin.php:619 includes/class-takrg-admin.php:638 275 msgid "Block" 276 msgstr "" 277 278 #: includes/class-takrg-admin.php:621 includes/class-takrg-admin.php:631 includes/class-takrg-privacy.php:124 279 msgid "Value" 280 msgstr "" 281 282 #: includes/class-takrg-admin.php:623 283 msgid "Add rule" 284 msgstr "" 285 286 #: includes/class-takrg-admin.php:632 includes/class-takrg-admin.php:694 includes/class-takrg-admin.php:740 includes/class-takrg-privacy.php:105 includes/class-takrg-privacy.php:128 287 msgid "Date (UTC)" 288 msgstr "" 289 290 #: includes/class-takrg-admin.php:646 includes/class-takrg-admin.php:713 291 msgid "Delete" 292 msgstr "" 293 294 #: includes/class-takrg-admin.php:655 295 msgid "Count refusals from existing orders" 296 msgstr "" 297 298 #: includes/class-takrg-admin.php:661 299 msgid "Continue scan" 300 msgstr "" 301 302 #: includes/class-takrg-admin.php:661 303 msgid "Scan existing orders" 304 msgstr "" 305 306 #: includes/class-takrg-admin.php:663 307 msgid "Adds a record for every order that is already in a counted status. Orders that already have a record are skipped, so it is safe to repeat." 308 msgstr "" 309 310 #: includes/class-takrg-admin.php:666 175 311 msgid "Import and export" 176 312 msgstr "" 177 313 178 #: includes/class-takrg-admin.php: 472314 #: includes/class-takrg-admin.php:672 179 315 msgid "Import CSV" 180 316 msgstr "" 181 317 182 #: includes/class-takrg-admin.php: 474318 #: includes/class-takrg-admin.php:674 183 319 msgid "One row per refusal: phone,email (a header row is optional). Numbers without a country code use the default country." 184 320 msgstr "" 185 321 186 #: includes/class-takrg-admin.php: 479322 #: includes/class-takrg-admin.php:679 187 323 msgid "Export CSV" 188 324 msgstr "" 189 325 190 #: includes/class-takrg-admin.php: 485326 #: includes/class-takrg-admin.php:685 191 327 msgid "Stored refusals (%d)" 192 328 msgstr "" 193 329 194 #: includes/class-takrg-admin.php: 492includes/class-takrg-privacy.php:101330 #: includes/class-takrg-admin.php:692 includes/class-takrg-admin.php:745 includes/class-takrg-privacy.php:101 195 331 msgid "Order" 196 332 msgstr "" 197 333 198 #: includes/class-takrg-admin.php: 493334 #: includes/class-takrg-admin.php:693 199 335 msgid "Source" 200 336 msgstr "" 201 337 202 #: includes/class-takrg-admin.php:494 includes/class-takrg-privacy.php:105 203 msgid "Date (UTC)" 204 msgstr "" 205 206 #: includes/class-takrg-admin.php:499 338 #: includes/class-takrg-admin.php:699 207 339 msgid "No refusals recorded yet." 208 340 msgstr "" 209 341 210 #: includes/class-takrg-admin.php:513 211 msgid "Delete" 212 msgstr "" 213 214 #: includes/class-takrg-checkout.php:159 342 #: includes/class-takrg-admin.php:736 343 msgid "Activity log" 344 msgstr "" 345 346 #: includes/class-takrg-admin.php:737 347 msgid "The last events. Customers are masked. In test mode, \"Would apply\" is what Live mode would have done." 348 msgstr "" 349 350 #: includes/class-takrg-admin.php:741 351 msgid "Event" 352 msgstr "" 353 354 #: includes/class-takrg-admin.php:742 355 msgid "Action" 356 msgstr "" 357 358 #: includes/class-takrg-admin.php:743 359 msgid "Customer" 360 msgstr "" 361 362 #: includes/class-takrg-admin.php:746 363 msgid "Details" 364 msgstr "" 365 366 #: includes/class-takrg-admin.php:751 367 msgid "Nothing yet." 368 msgstr "" 369 370 #: includes/class-takrg-admin.php:778 371 msgid "Order tagged" 372 msgstr "" 373 374 #: includes/class-takrg-admin.php:779 375 msgid "Would apply (test mode)" 376 msgstr "" 377 378 #: includes/class-takrg-admin.php:780 379 msgid "Applied" 380 msgstr "" 381 382 #: includes/class-takrg-admin.php:781 383 msgid "Safety breaker" 384 msgstr "" 385 386 #: includes/class-takrg-admin.php:782 387 msgid "Error (order allowed)" 388 msgstr "" 389 390 #: includes/class-takrg-breaker.php:158 391 msgid "Takeed Refusal Guard switched itself to \"tag only\"" 392 msgstr "" 393 394 #: includes/class-takrg-breaker.php:161 395 msgid "%1$d of the last %2$d checkout attempts were restricted, which is more than half. To protect your sales the plugin now only tags orders. Review the activity log under WooCommerce > Refusal Guard, then turn the protection back on there." 396 msgstr "" 397 398 #: includes/class-takrg-checkout.php:211 215 399 msgid "Cash on delivery is not available for this order. Please choose another payment method." 216 400 msgstr "" 217 401 218 #: includes/class-takrg-checkout.php: 289402 #: includes/class-takrg-checkout.php:401 219 403 msgid "COD risk: this customer (phone or e-mail) refused %d earlier order." 220 404 msgid_plural "COD risk: this customer (phone or e-mail) refused %d earlier orders." … … 230 414 msgstr "" 231 415 232 #: includes/class-takrg-privacy.php:154 233 msgid "Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery. Nothing is sent to the plugin author or to any third party." 234 msgstr "" 235 236 #: includes/class-takrg-privacy.php:155 416 #: includes/class-takrg-privacy.php:115 417 msgid "COD allow-list and block-list entries" 418 msgstr "" 419 420 #: includes/class-takrg-privacy.php:116 421 msgid "Entries added by the shop administrator." 422 msgstr "" 423 424 #: includes/class-takrg-privacy.php:179 425 msgid "Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery, the entries you add to its allow-list and block-list, and an activity log in which customers appear masked. Nothing is sent to the plugin author or to any third party." 426 msgstr "" 427 428 #: includes/class-takrg-privacy.php:180 237 429 msgid "Suggested text:" 238 430 msgstr "" 239 431 240 #: includes/class-takrg-privacy.php:1 55432 #: includes/class-takrg-privacy.php:180 241 433 msgid "If you refuse a cash-on-delivery parcel, we keep your phone number and e-mail address together with the order number so that we can offer other payment methods for your next orders. You can ask us to erase this record." 242 434 msgstr "" 243 435 244 #: includes/class-takrg-recorder.php: 38 includes/class-takrg-recorder.php:56436 #: includes/class-takrg-recorder.php:41 includes/class-takrg-recorder.php:59 245 437 msgctxt "Order status" 246 438 msgid "Refused at delivery" 247 439 msgstr "" 248 440 249 #: includes/class-takrg-recorder.php:4 4441 #: includes/class-takrg-recorder.php:47 250 442 msgid "Refused at delivery <span class=\"count\">(%s)</span>" 251 443 msgid_plural "Refused at delivery <span class=\"count\">(%s)</span>" … … 253 445 msgstr[1] "" 254 446 255 #: includes/class-takrg-recorder.php: 67447 #: includes/class-takrg-recorder.php:70 256 448 msgid "Change status to refused at delivery" 257 449 msgstr "" 258 450 259 #: includes/class-takrg-recorder.php:8 6451 #: includes/class-takrg-recorder.php:89 includes/class-takrg-recorder.php:114 260 452 msgid "Marked as refused at delivery." 261 453 msgstr "" 262 454 263 #: includes/class-takrg-recorder.php:130 455 #: includes/class-takrg-recorder.php:102 456 msgid "Mark as refused at delivery" 457 msgstr "" 458 459 #: includes/class-takrg-recorder.php:103 460 msgid "Clear the refusal record of this order" 461 msgstr "" 462 463 #: includes/class-takrg-recorder.php:127 464 msgid "Refusal record cleared." 465 msgstr "" 466 467 #: includes/class-takrg-recorder.php:169 264 468 msgid "Refusal recorded by Takeed Refusal Guard." 265 469 msgstr "" -
takeed-refusal-guard/tags/1.1.0/readme.txt
r3807459 r3807599 8 8 WC requires at least: 7.1 9 9 WC tested up to: 11.3 10 Stable tag: 1. 0.010 Stable tag: 1.1.0 11 11 License: GPLv2 or later 12 12 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 27 27 * **Show a neutral notice** asking the customer to choose another payment method. 28 28 29 **Safe by design (new in 1.1)** 30 31 * **Test mode is the default on a new installation.** Orders are only tagged, and an activity log shows what the plugin would have done. Switch to Live when the log looks right. 32 * **Safety breaker.** If more than 50% of the checkout attempts in an hour are restricted (and there are at least 5 attempts), the plugin returns to "tag only" by itself and tells the administrator. It stays there until you turn it back on. 33 * **Fail-open.** If anything goes wrong while the plugin checks a customer, the order goes through. 34 * **Allow-list.** Customers you trust are never touched, whatever their refusals. 35 29 36 **What makes it different** 30 37 … … 38 45 39 46 * A **COD risk** column in the orders list (legacy and HPOS screens) showing the refusal count of the customer. 47 * **Block-list and patterns**: add an e-mail, a phone number or a pattern such as `*@example.com` or `+21620*`. 48 * **Order actions** on the order screen: mark as refused, or clear a refusal that was a mistake. 49 * **Count refusals from existing orders** with one click, and **forget old refusals** automatically after a number of months you choose. 50 * **Activity log** of the last 500 events, with customers masked. 40 51 * Settings under **WooCommerce > Refusal Guard**: threshold, action, counted statuses, default country, add or remove numbers by hand, CSV import and export. 41 52 * WordPress personal data exporter and eraser for the stored records. … … 88 99 There is no hard dependency between this plugin and any other Takeed plugin. 89 100 101 = What is the safety breaker? = 102 103 A circuit breaker against mistakes. If more than half of the checkout attempts of the last hour were restricted (with at least 5 attempts, and one customer counted once), something is wrong, for example a rule that is too wide. The plugin then only tags orders, shows a notice in the admin and sends the administrator an e-mail (through your own mail setup). The restriction comes back only when you press "Turn protection back on". The attempt that trips the breaker is still restricted; from the next checkout on, Cash on Delivery is available again. 104 105 = What happens if the plugin fails during checkout? = 106 107 The order goes through. Any error while checking a customer lets the order continue, and the error is written to the activity log. 108 109 = I update from 1.0. Will my shop change? = 110 111 No. Sites that already used 1.0 keep the action they chose and stay in Live mode. Only new installations start in Test mode. 112 90 113 = Does it send data anywhere? = 91 114 … … 105 128 * The records are included in the WordPress personal data export (Tools > Export Personal Data) and removed by the erasure tool (Tools > Erase Personal Data), matched by e-mail address and by the phone numbers tied to it. 106 129 * A suggested paragraph is added to your privacy policy page (Settings > Privacy). 107 * Deleting the plugin removes its table and its options. 130 * The activity log shows customers masked (the first and last digits of a phone number; the first letter and the domain of an e-mail address). Allow-list and block-list entries are included in the export and the erasure when they are exact matches of the person. 131 * With "Forget refusals after (months)" you choose how long records are kept; the default keeps them until you delete them. 132 * Deleting the plugin removes its tables (refusals, rules, log) and its options. 108 133 109 134 == Changelog == 135 136 = 1.1.0 = 137 * New installations start in Test mode: orders are tagged and an activity log shows what the plugin would have done. 138 * Safety breaker: more than 50% of the checkout attempts in an hour (at least 5) returns the plugin to "tag only" and notifies the administrator. 139 * Fail-open: any error while checking a customer lets the order through. 140 * Allow-list, block-list and wildcard patterns (e-mail and phone prefix). 141 * Order screen actions: mark as refused, clear a refusal. Bulk action unchanged. 142 * Count refusals from existing orders. Optional retention in months. Activity log with masked customers. 143 * Privacy exporter and eraser include allow-list and block-list entries. 144 * Sites updating from 1.0 keep their action and stay in Live mode. 110 145 111 146 = 1.0.0 = … … 114 149 == Upgrade Notice == 115 150 151 = 1.1.0 = 152 Adds Test mode (default for new installations), a safety breaker, an allow-list and an activity log. Existing sites keep their current behaviour. 153 116 154 = 1.0.0 = 117 155 First release. -
takeed-refusal-guard/tags/1.1.0/takeed-refusal-guard.php
r3806545 r3807599 3 3 * Plugin Name: Takeed Refusal Guard for WooCommerce 4 4 * Description: Remembers customers who refused a cash-on-delivery parcel and flags, or limits, cash on delivery on their next order. Built-in phone formats for Tunisia, Morocco, Algeria, Egypt and the Gulf. 5 * Version: 1. 0.05 * Version: 1.1.0 6 6 * Requires at least: 6.2 7 7 * Requires PHP: 7.4 … … 21 21 defined( 'ABSPATH' ) || exit; 22 22 23 define( 'TAKRG_VERSION', '1. 0.0' );23 define( 'TAKRG_VERSION', '1.1.0' ); 24 24 define( 'TAKRG_FILE', __FILE__ ); 25 25 define( 'TAKRG_DIR', plugin_dir_path( __FILE__ ) ); … … 27 27 require_once TAKRG_DIR . 'includes/class-takrg-phone.php'; 28 28 require_once TAKRG_DIR . 'includes/class-takrg-settings.php'; 29 require_once TAKRG_DIR . 'includes/class-takrg-rules.php'; 30 require_once TAKRG_DIR . 'includes/class-takrg-log.php'; 31 require_once TAKRG_DIR . 'includes/class-takrg-breaker.php'; 29 32 require_once TAKRG_DIR . 'includes/class-takrg-store.php'; 30 33 require_once TAKRG_DIR . 'includes/class-takrg-recorder.php'; … … 47 50 48 51 register_activation_hook( __FILE__, array( 'TAKRG_Plugin', 'activate' ) ); 52 register_deactivation_hook( __FILE__, array( 'TAKRG_Plugin', 'deactivate' ) ); 49 53 50 54 add_action( 'plugins_loaded', array( 'TAKRG_Plugin', 'boot' ) ); -
takeed-refusal-guard/tags/1.1.0/uninstall.php
r3806545 r3807599 15 15 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall removes our own table. 16 16 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'takrg_refusals' ) ); 17 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall removes our own table. 18 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'takrg_rules' ) ); 19 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall removes our own table. 20 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'takrg_log' ) ); 17 21 delete_option( 'takrg_settings' ); 18 22 delete_option( 'takrg_db_version' ); 23 delete_option( 'takrg_breaker' ); 24 wp_clear_scheduled_hook( 'takrg_daily_cleanup' ); 19 25 } 20 26 -
takeed-refusal-guard/trunk/includes/class-takrg-admin.php
r3806545 r3807599 30 30 add_action( 'admin_post_takrg_import', array( __CLASS__, 'handle_import' ) ); 31 31 add_action( 'admin_post_takrg_export', array( __CLASS__, 'handle_export' ) ); 32 add_action( 'admin_post_takrg_rule_add', array( __CLASS__, 'handle_rule_add' ) ); 33 add_action( 'admin_post_takrg_rule_delete', array( __CLASS__, 'handle_rule_delete' ) ); 34 add_action( 'admin_post_takrg_scan', array( __CLASS__, 'handle_scan' ) ); 35 add_action( 'admin_post_takrg_breaker_reset', array( __CLASS__, 'handle_breaker_reset' ) ); 36 add_action( 'admin_notices', array( __CLASS__, 'breaker_notice' ) ); 32 37 // Orders list: legacy (posts) screen and HPOS screen. 33 38 add_filter( 'manage_edit-shop_order_columns', array( __CLASS__, 'add_column' ) ); … … 314 319 315 320 /** 321 * Add an allow-list or block-list rule (exact value or wildcard pattern). 322 */ 323 public static function handle_rule_add() { 324 self::guard( 'takrg_rule_add' ); 325 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified in guard(). 326 $type = isset( $_POST['takrg_type'] ) && TAKRG_Rules::BLOCK === sanitize_key( wp_unslash( $_POST['takrg_type'] ) ) ? TAKRG_Rules::BLOCK : TAKRG_Rules::ALLOW; 327 $value = isset( $_POST['takrg_value'] ) ? sanitize_text_field( wp_unslash( $_POST['takrg_value'] ) ) : ''; 328 $country = TAKRG_Recorder::country_for( isset( $_POST['takrg_country'] ) ? sanitize_text_field( wp_unslash( $_POST['takrg_country'] ) ) : '' ); 329 // phpcs:enable WordPress.Security.NonceVerification.Missing 330 $id = TAKRG_Rules::add( $type, $value, $country ); 331 TAKRG_Checkout::reset_cache(); 332 self::back( $id ? 'rule_added' : 'rule_invalid' ); 333 } 334 335 /** 336 * Delete a rule. 337 */ 338 public static function handle_rule_delete() { 339 self::guard( 'takrg_rule_delete' ); 340 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in guard(). 341 TAKRG_Rules::delete( isset( $_POST['takrg_id'] ) ? absint( $_POST['takrg_id'] ) : 0 ); 342 TAKRG_Checkout::reset_cache(); 343 self::back( 'removed', 1 ); 344 } 345 346 /** 347 * Turns the protection back on after the safety breaker tripped. 348 */ 349 public static function handle_breaker_reset() { 350 self::guard( 'takrg_breaker_reset' ); 351 TAKRG_Breaker::reset(); 352 self::back( 'breaker_reset' ); 353 } 354 355 /** 356 * Adds the refusals of orders that are already in a counted status (500 orders per click, newest page first). 357 * Safe to repeat: an order that already has a record is skipped. 358 */ 359 public static function handle_scan() { 360 self::guard( 'takrg_scan' ); 361 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in guard(). 362 $page = isset( $_POST['takrg_page'] ) ? max( 1, absint( $_POST['takrg_page'] ) ) : 1; 363 $added = 0; 364 $more = false; 365 for ( $i = 0; $i < 5; $i++, $page++ ) { 366 $orders = wc_get_orders( 367 array( 368 'status' => TAKRG_Settings::counted_statuses(), 369 'limit' => 100, 370 'paged' => $page, 371 'orderby' => 'ID', 372 'order' => 'ASC', 373 'return' => 'objects', 374 ) 375 ); 376 foreach ( $orders as $order ) { 377 list( $phone, $email ) = TAKRG_Recorder::identity( $order ); 378 $added += TAKRG_Store::add( $phone, $email, $order->get_id(), 'scan' ) ? 1 : 0; 379 } 380 if ( count( $orders ) < 100 ) { 381 break; 382 } 383 $more = true; 384 } 385 TAKRG_Checkout::reset_cache(); 386 wp_safe_redirect( 387 add_query_arg( 388 array( 389 'page' => self::SLUG, 390 'takrg_msg' => ( $more && count( $orders ) >= 100 ) ? 'scan_more' : 'scanned', 391 'takrg_num' => $added, 392 'takrg_pg' => $page, 393 ), 394 admin_url( 'admin.php' ) 395 ) 396 ); 397 exit; 398 } 399 400 /** 401 * Admin notice when the safety breaker has tripped (every admin screen, for shop managers). 402 */ 403 public static function breaker_notice() { 404 if ( ! current_user_can( self::CAP ) || ! TAKRG_Breaker::is_tripped() ) { 405 return; 406 } 407 echo '<div class="notice notice-warning"><p><strong>' . esc_html__( 'Takeed Refusal Guard switched itself to "tag only".', 'takeed-refusal-guard' ) . '</strong> ' 408 . esc_html__( 'More than half of the recent checkout attempts were restricted, so the plugin stopped restricting Cash on Delivery to protect your sales.', 'takeed-refusal-guard' ) 409 . ' <a href="' . esc_url( admin_url( 'admin.php?page=' . self::SLUG ) ) . '">' . esc_html__( 'Review and turn it back on', 'takeed-refusal-guard' ) . '</a></p></div>'; 410 } 411 412 /** 316 413 * Notice texts by key. 317 414 * … … 331 428 /* translators: %d: number of records. */ 332 429 return array( 'success', sprintf( _n( '%d record imported.', '%d records imported.', $num, 'takeed-refusal-guard' ), $num ) ); 430 case 'rule_added': 431 return array( 'success', __( 'Rule added.', 'takeed-refusal-guard' ) ); 432 case 'rule_invalid': 433 return array( 'error', __( 'Enter an e-mail address, a phone number, or a pattern such as *@example.com or +21620* (not already in the list).', 'takeed-refusal-guard' ) ); 434 case 'breaker_reset': 435 return array( 'success', __( 'Protection turned back on.', 'takeed-refusal-guard' ) ); 436 case 'scanned': 437 /* translators: %d: number of records. */ 438 return array( 'success', sprintf( _n( 'Scan finished: %d record added.', 'Scan finished: %d records added.', $num, 'takeed-refusal-guard' ), $num ) ); 439 case 'scan_more': 440 /* translators: %d: number of records. */ 441 return array( 'success', sprintf( _n( '%d record added. More orders remain: press "Continue scan".', '%d records added. More orders remain: press "Continue scan".', $num, 'takeed-refusal-guard' ), $num ) ); 333 442 case 'invalid': 334 443 return array( 'error', __( 'Enter a valid phone number or e-mail address.', 'takeed-refusal-guard' ) ); … … 375 484 // phpcs:enable WordPress.Security.NonceVerification.Recommended 376 485 $per_page = 50; 377 $total = TAKRG_Store::total(); 378 $rows = TAKRG_Store::all( $per_page, ( $paged - 1 ) * $per_page ); 379 $statuses = TAKRG_Settings::selectable_statuses(); 380 $post_url = admin_url( 'admin-post.php' ); 381 $actions = array( 486 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only: a page number. 487 $scan_page = isset( $_GET['takrg_pg'] ) ? max( 1, absint( $_GET['takrg_pg'] ) ) : 1; 488 $total = TAKRG_Store::total(); 489 $rows = TAKRG_Store::all( $per_page, ( $paged - 1 ) * $per_page ); 490 $statuses = TAKRG_Settings::selectable_statuses(); 491 $post_url = admin_url( 'admin-post.php' ); 492 $actions = array( 382 493 'tag' => array( __( 'Tag only (default)', 'takeed-refusal-guard' ), __( 'Adds an order note and the meta "_takrg_risk". Nothing is blocked.', 'takeed-refusal-guard' ) ), 383 494 'hide' => array( __( 'Hide Cash on Delivery', 'takeed-refusal-guard' ), __( 'Cash on Delivery is not offered to this customer; other payment methods stay available.', 'takeed-refusal-guard' ) ), … … 391 502 <?php endif; ?> 392 503 504 <?php if ( TAKRG_Breaker::is_tripped() ) : ?> 505 <div class="notice notice-warning inline"> 506 <p><strong><?php esc_html_e( 'The safety breaker switched the plugin to "tag only".', 'takeed-refusal-guard' ); ?></strong> 507 <?php esc_html_e( 'More than half of the checkout attempts in an hour were restricted. Check the activity log below, then turn the protection back on.', 'takeed-refusal-guard' ); ?></p> 508 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 509 <input type="hidden" name="action" value="takrg_breaker_reset" /> 510 <?php wp_nonce_field( 'takrg_breaker_reset' ); ?> 511 <p><button type="submit" class="button"><?php esc_html_e( 'Turn protection back on', 'takeed-refusal-guard' ); ?></button></p> 512 </form> 513 </div> 514 <?php endif; ?> 515 <?php if ( 'live' !== $s['mode'] ) : ?> 516 <div class="notice notice-info inline"><p><strong><?php esc_html_e( 'Test mode is on.', 'takeed-refusal-guard' ); ?></strong> 517 <?php esc_html_e( 'Nothing is hidden or refused: orders are only tagged, and the activity log below shows what the plugin would have done. When the log looks right, switch to Live.', 'takeed-refusal-guard' ); ?></p></div> 518 <?php endif; ?> 519 393 520 <form method="post" action="options.php"> 394 521 <?php settings_fields( 'takrg_settings_group' ); ?> 395 522 <table class="form-table" role="presentation"> 523 <tr> 524 <th scope="row"><?php esc_html_e( 'Mode', 'takeed-refusal-guard' ); ?></th> 525 <td> 526 <fieldset> 527 <label><input type="radio" name="takrg_settings[mode]" value="test" <?php checked( $s['mode'], 'test' ); ?> /> <strong><?php esc_html_e( 'Test mode (default)', 'takeed-refusal-guard' ); ?></strong></label> 528 <p class="description"><?php esc_html_e( 'Only tags orders and writes what it would have done to the activity log.', 'takeed-refusal-guard' ); ?></p> 529 <label><input type="radio" name="takrg_settings[mode]" value="live" <?php checked( $s['mode'], 'live' ); ?> /> <strong><?php esc_html_e( 'Live', 'takeed-refusal-guard' ); ?></strong></label> 530 <p class="description"><?php esc_html_e( 'Applies the action below. A safety breaker returns to "tag only" if more than 50% of the checkout attempts in an hour (at least 5) are restricted.', 'takeed-refusal-guard' ); ?></p> 531 </fieldset> 532 </td> 533 </tr> 396 534 <tr> 397 535 <th scope="row"><label for="takrg_threshold"><?php esc_html_e( 'Refusals before acting', 'takeed-refusal-guard' ); ?></label></th> … … 431 569 </td> 432 570 </tr> 571 <tr> 572 <th scope="row"><label for="takrg_retention"><?php esc_html_e( 'Forget refusals after (months)', 'takeed-refusal-guard' ); ?></label></th> 573 <td> 574 <input type="number" min="0" max="120" id="takrg_retention" name="takrg_settings[retention]" value="<?php echo esc_attr( (string) $s['retention'] ); ?>" class="small-text" /> 575 <p class="description"><?php esc_html_e( '0 keeps them until you delete them. Older records and log entries are removed once a day.', 'takeed-refusal-guard' ); ?></p> 576 </td> 577 </tr> 433 578 </table> 434 579 <?php submit_button(); ?> … … 462 607 <button type="submit" class="button"><?php esc_html_e( 'Remove all records of this number or e-mail', 'takeed-refusal-guard' ); ?></button> 463 608 </p> 609 </form> 610 611 <h2><?php esc_html_e( 'Allow-list and block-list', 'takeed-refusal-guard' ); ?></h2> 612 <p class="description"><?php esc_html_e( 'Allow: never act on this customer, whatever the refusals. Block: always treat this customer as having reached the threshold. Use an e-mail, a phone number, or a pattern with * such as *@example.com or +21620*.', 'takeed-refusal-guard' ); ?></p> 613 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 614 <input type="hidden" name="action" value="takrg_rule_add" /> 615 <?php wp_nonce_field( 'takrg_rule_add' ); ?> 616 <p> 617 <select name="takrg_type" aria-label="<?php esc_attr_e( 'List', 'takeed-refusal-guard' ); ?>"> 618 <option value="allow"><?php esc_html_e( 'Allow', 'takeed-refusal-guard' ); ?></option> 619 <option value="block"><?php esc_html_e( 'Block', 'takeed-refusal-guard' ); ?></option> 620 </select> 621 <input type="text" name="takrg_value" class="regular-text" dir="ltr" aria-label="<?php esc_attr_e( 'Value', 'takeed-refusal-guard' ); ?>" /> 622 <select name="takrg_country" aria-label="<?php esc_attr_e( 'Country', 'takeed-refusal-guard' ); ?>"><?php self::country_options( (string) $s['default_country'] ); ?></select> 623 <button type="submit" class="button"><?php esc_html_e( 'Add rule', 'takeed-refusal-guard' ); ?></button> 624 </p> 625 </form> 626 <?php $rules = TAKRG_Rules::all( 100 ); ?> 627 <?php if ( $rules ) : ?> 628 <table class="widefat striped"> 629 <thead><tr> 630 <th><?php esc_html_e( 'List', 'takeed-refusal-guard' ); ?></th> 631 <th><?php esc_html_e( 'Value', 'takeed-refusal-guard' ); ?></th> 632 <th><?php esc_html_e( 'Date (UTC)', 'takeed-refusal-guard' ); ?></th> 633 <th></th> 634 </tr></thead> 635 <tbody> 636 <?php foreach ( $rules as $r ) : ?> 637 <tr> 638 <td><?php echo 'block' === $r['type'] ? esc_html__( 'Block', 'takeed-refusal-guard' ) : esc_html__( 'Allow', 'takeed-refusal-guard' ); ?></td> 639 <td dir="ltr"><?php echo esc_html( 'phone' === $r['kind'] ? '+' . $r['value'] : $r['value'] ); ?></td> 640 <td><?php echo esc_html( $r['created_at'] ); ?></td> 641 <td> 642 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 643 <input type="hidden" name="action" value="takrg_rule_delete" /> 644 <input type="hidden" name="takrg_id" value="<?php echo esc_attr( (string) $r['id'] ); ?>" /> 645 <?php wp_nonce_field( 'takrg_rule_delete' ); ?> 646 <button type="submit" class="button-link button-link-delete"><?php esc_html_e( 'Delete', 'takeed-refusal-guard' ); ?></button> 647 </form> 648 </td> 649 </tr> 650 <?php endforeach; ?> 651 </tbody> 652 </table> 653 <?php endif; ?> 654 655 <h2><?php esc_html_e( 'Count refusals from existing orders', 'takeed-refusal-guard' ); ?></h2> 656 <form method="post" action="<?php echo esc_url( $post_url ); ?>"> 657 <input type="hidden" name="action" value="takrg_scan" /> 658 <input type="hidden" name="takrg_page" value="<?php echo esc_attr( (string) $scan_page ); ?>" /> 659 <?php wp_nonce_field( 'takrg_scan' ); ?> 660 <p> 661 <button type="submit" class="button"><?php echo $scan_page > 1 ? esc_html__( 'Continue scan', 'takeed-refusal-guard' ) : esc_html__( 'Scan existing orders', 'takeed-refusal-guard' ); ?></button> 662 </p> 663 <p class="description"><?php esc_html_e( 'Adds a record for every order that is already in a counted status. Orders that already have a record are skipped, so it is safe to repeat.', 'takeed-refusal-guard' ); ?></p> 464 664 </form> 465 665 … … 533 733 } 534 734 ?> 735 736 <h2><?php esc_html_e( 'Activity log', 'takeed-refusal-guard' ); ?></h2> 737 <p class="description"><?php esc_html_e( 'The last events. Customers are masked. In test mode, "Would apply" is what Live mode would have done.', 'takeed-refusal-guard' ); ?></p> 738 <table class="widefat striped"> 739 <thead><tr> 740 <th><?php esc_html_e( 'Date (UTC)', 'takeed-refusal-guard' ); ?></th> 741 <th><?php esc_html_e( 'Event', 'takeed-refusal-guard' ); ?></th> 742 <th><?php esc_html_e( 'Action', 'takeed-refusal-guard' ); ?></th> 743 <th><?php esc_html_e( 'Customer', 'takeed-refusal-guard' ); ?></th> 744 <th><?php esc_html_e( 'Refusals', 'takeed-refusal-guard' ); ?></th> 745 <th><?php esc_html_e( 'Order', 'takeed-refusal-guard' ); ?></th> 746 <th><?php esc_html_e( 'Details', 'takeed-refusal-guard' ); ?></th> 747 </tr></thead> 748 <tbody> 749 <?php $events = TAKRG_Log::all( 50 ); ?> 750 <?php if ( ! $events ) : ?> 751 <tr><td colspan="7"><?php esc_html_e( 'Nothing yet.', 'takeed-refusal-guard' ); ?></td></tr> 752 <?php endif; ?> 753 <?php foreach ( $events as $e ) : ?> 754 <tr> 755 <td><?php echo esc_html( $e['created_at'] ); ?></td> 756 <td><?php echo esc_html( self::event_label( $e['event'] ) ); ?></td> 757 <td><?php echo esc_html( $e['action'] ); ?></td> 758 <td dir="ltr"><?php echo esc_html( $e['who'] ); ?></td> 759 <td><?php echo esc_html( (string) $e['refusals'] ); ?></td> 760 <td><?php echo $e['order_id'] ? esc_html( '#' . $e['order_id'] ) : '–'; ?></td> 761 <td><?php echo esc_html( $e['detail'] ); ?></td> 762 </tr> 763 <?php endforeach; ?> 764 </tbody> 765 </table> 535 766 </div> 536 767 <?php 537 768 } 769 770 /** 771 * Readable name of a log event. 772 * 773 * @param string $event Event key. 774 * @return string 775 */ 776 private static function event_label( $event ) { 777 $labels = array( 778 'tagged' => __( 'Order tagged', 'takeed-refusal-guard' ), 779 'would_apply' => __( 'Would apply (test mode)', 'takeed-refusal-guard' ), 780 'applied' => __( 'Applied', 'takeed-refusal-guard' ), 781 'breaker' => __( 'Safety breaker', 'takeed-refusal-guard' ), 782 'error' => __( 'Error (order allowed)', 'takeed-refusal-guard' ), 783 ); 784 return isset( $labels[ $event ] ) ? $labels[ $event ] : $event; 785 } 538 786 } -
takeed-refusal-guard/trunk/includes/class-takrg-checkout.php
r3806545 r3807599 29 29 add_action( 'woocommerce_after_checkout_validation', array( __CLASS__, 'validate_classic' ), 10, 2 ); 30 30 add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( __CLASS__, 'validate_store_api' ), 10, 2 ); 31 add_filter( 'rest_request_before_callbacks', array( __CLASS__, 'record_store_api_attempt' ), 10, 3 ); 31 32 add_action( 'woocommerce_review_order_before_payment', array( __CLASS__, 'render_notice' ) ); 32 33 add_action( 'wp_enqueue_scripts', array( __CLASS__, 'refresh_script' ), 20 ); … … 44 45 45 46 /** 46 * Refusal count for raw (not yet normalised) checkout values, and the settings that apply. 47 * Judges a customer given as typed at the checkout: refusal count, allow-list, block rules, threshold. 48 * 49 * @param string $phone Phone as typed. 50 * @param string $email E-mail as typed. 51 * @param string $country Billing country. 52 * @return array count, risky, allowed, key (opaque customer key), who (masked). 53 */ 54 public static function assess( $phone, $email, $country = '' ) { 55 $np = TAKRG_Phone::normalize( $phone, TAKRG_Recorder::country_for( $country ) ); 56 $ne = TAKRG_Phone::normalize_email( $email ); 57 $key = $np . '|' . $ne; 58 if ( isset( self::$counts[ $key ] ) ) { 59 return self::$counts[ $key ]; 60 } 61 $allowed = ( '' !== $np || '' !== $ne ) && TAKRG_Rules::matches( TAKRG_Rules::ALLOW, $np, $ne ); 62 $count = TAKRG_Store::count( $np, $ne ); 63 if ( ! $allowed && ( '' !== $np || '' !== $ne ) && TAKRG_Rules::matches( TAKRG_Rules::BLOCK, $np, $ne ) ) { 64 $count = max( $count, (int) TAKRG_Settings::get( 'threshold' ) ); 65 } 66 $risky = ! $allowed && self::is_risky( $count, $phone, $email ); 67 self::$counts[ $key ] = array( 68 'count' => $count, 69 'risky' => $risky, 70 'allowed' => $allowed, 71 'key' => substr( hash( 'sha256', $key . wp_salt( 'auth' ) ), 0, 12 ), 72 'who' => TAKRG_Log::mask( $np, $ne ), 73 ); 74 return self::$counts[ $key ]; 75 } 76 77 /** 78 * Refusal count for raw (not yet normalised) checkout values. 47 79 * 48 80 * @param string $phone Phone as typed. … … 52 84 */ 53 85 public static function count_for( $phone, $email, $country = '' ) { 54 $phone = TAKRG_Phone::normalize( $phone, TAKRG_Recorder::country_for( $country ) ); 55 $email = TAKRG_Phone::normalize_email( $email ); 56 $key = $phone . '|' . $email; 57 if ( ! isset( self::$counts[ $key ] ) ) { 58 self::$counts[ $key ] = TAKRG_Store::count( $phone, $email ); 59 } 60 return self::$counts[ $key ]; 86 return self::assess( $phone, $email, $country )['count']; 61 87 } 62 88 … … 72 98 $risky = $count >= (int) TAKRG_Settings::get( 'threshold' ) && $count > 0; 73 99 return (bool) apply_filters( 'takrg_is_risky', $risky, $count, $phone, $email ); 100 } 101 102 /** 103 * Runs a check. Any exception means "allow": a failure of this plugin must never stop a sale. 104 * 105 * @param callable $check The check. 106 * @param mixed $fallback What to return when the check fails. 107 * @return mixed 108 */ 109 private static function guard( $check, $fallback ) { 110 try { 111 return $check(); 112 } catch ( \Throwable $e ) { 113 TAKRG_Log::add( 114 'error', 115 array( 116 'detail' => get_class( $e ) . ': ' . $e->getMessage(), 117 ) 118 ); 119 return $fallback; 120 } 74 121 } 75 122 … … 126 173 private static function request_risk() { 127 174 list( $phone, $email, $country ) = self::request_identity(); 128 $ count = self::count_for( $phone, $email, $country );129 return array( self::is_risky( $count, $phone, $email ), $count);175 $a = self::assess( $phone, $email, $country ); 176 return array( $a['risky'], $a['count'] ); 130 177 } 131 178 … … 138 185 */ 139 186 public static function filter_gateways( $gateways ) { 140 if ( ! is_array( $gateways ) || ! isset( $gateways[ self::COD ] ) || 'hide' !== TAKRG_Settings::get( 'action' )) {187 if ( ! is_array( $gateways ) || ! isset( $gateways[ self::COD ] ) ) { 141 188 return $gateways; 142 189 } 143 if ( is_admin() && ! wp_doing_ajax() ) { 144 return $gateways; 145 } 146 list( $risky ) = self::request_risk(); 147 if ( $risky ) { 148 unset( $gateways[ self::COD ] ); 149 } 150 return $gateways; 190 return self::guard( 191 function () use ( $gateways ) { 192 if ( 'hide' !== TAKRG_Settings::effective_action() || ( is_admin() && ! wp_doing_ajax() ) ) { 193 return $gateways; 194 } 195 list( $risky ) = self::request_risk(); 196 if ( $risky ) { 197 unset( $gateways[ self::COD ] ); 198 } 199 return $gateways; 200 }, 201 $gateways 202 ); 151 203 } 152 204 … … 161 213 162 214 /** 163 * Whether this shop's action restricts cash on delivery. 164 * 165 * @return bool 166 */ 167 private static function restricts() { 168 return in_array( TAKRG_Settings::get( 'action' ), array( 'hide', 'notice' ), true ); 215 * Decides one checkout attempt: should cash on delivery be refused for this customer? Also feeds the safety 216 * breaker and the activity log. Never throws. 217 * 218 * @param string $phone Phone as typed. 219 * @param string $email E-mail as typed. 220 * @param string $country Billing country. 221 * @param string $method Chosen payment method. 222 * @param bool $record Whether this attempt still has to be counted by the safety breaker. 223 * @return bool True when the plugin must refuse cash on delivery. 224 */ 225 private static function refuse_cod( $phone, $email, $country, $method, $record = true ) { 226 return (bool) self::guard( 227 function () use ( $phone, $email, $country, $method, $record ) { 228 $intended = (string) TAKRG_Settings::get( 'action' ); 229 $live = 'live' === TAKRG_Settings::get( 'mode' ) && ! TAKRG_Breaker::is_tripped(); 230 $a = self::assess( $phone, $email, $country ); 231 $acts = $a['risky'] && in_array( $intended, array( 'hide', 'notice' ), true ); 232 if ( $record && 'live' === TAKRG_Settings::get( 'mode' ) ) { 233 TAKRG_Breaker::record( $a['key'], $acts ); 234 } 235 if ( ! $acts ) { 236 return false; 237 } 238 if ( self::COD !== $method ) { 239 return false; 240 } 241 TAKRG_Log::add( 242 $live ? 'applied' : 'would_apply', 243 array( 244 'action' => $intended, 245 'who' => $a['who'], 246 'refusals' => $a['count'], 247 ) 248 ); 249 return $live; 250 }, 251 false 252 ); 169 253 } 170 254 … … 176 260 */ 177 261 public static function validate_classic( $data, $errors ) { 178 if ( ! self::restricts() || ! is_array( $data ) || ! isset( $data['payment_method'] ) || self::COD !== $data['payment_method']) {262 if ( ! is_array( $data ) ) { 179 263 return; 180 264 } 181 $phone = isset( $data['billing_phone'] ) ? (string) $data['billing_phone'] : ''; 182 $email = isset( $data['billing_email'] ) ? (string) $data['billing_email'] : ''; 183 $country = isset( $data['billing_country'] ) ? (string) $data['billing_country'] : ''; 184 $count = self::count_for( $phone, $email, $country ); 185 if ( self::is_risky( $count, $phone, $email ) ) { 265 $method = isset( $data['payment_method'] ) ? (string) $data['payment_method'] : ''; 266 if ( self::refuse_cod( isset( $data['billing_phone'] ) ? (string) $data['billing_phone'] : '', isset( $data['billing_email'] ) ? (string) $data['billing_email'] : '', isset( $data['billing_country'] ) ? (string) $data['billing_country'] : '', $method ) ) { 186 267 $errors->add( 'takrg_cod_unavailable', self::message() ); 187 268 } 269 } 270 271 /** 272 * Block checkout: counts the attempt for the safety breaker as soon as the Store API receives it, before 273 * WooCommerce itself may refuse the payment method (action "hide"). 274 * 275 * @param mixed $response Response so far. 276 * @param array $handler Route handler. 277 * @param WP_REST_Request $request Request. 278 * @return mixed The response, unchanged. 279 */ 280 public static function record_store_api_attempt( $response, $handler, $request ) { 281 unset( $handler ); 282 if ( $request instanceof WP_REST_Request && 'POST' === $request->get_method() && 1 === preg_match( '#^/wc/store/v\d+/checkout$#', $request->get_route() ) ) { 283 self::guard( 284 function () use ( $request ) { 285 $b = $request->get_param( 'billing_address' ); 286 $b = is_array( $b ) ? $b : array(); 287 $a = self::assess( isset( $b['phone'] ) ? (string) $b['phone'] : '', isset( $b['email'] ) ? (string) $b['email'] : '', isset( $b['country'] ) ? (string) $b['country'] : '' ); 288 if ( 'live' === TAKRG_Settings::get( 'mode' ) ) { 289 TAKRG_Breaker::record( $a['key'], $a['risky'] && in_array( TAKRG_Settings::get( 'action' ), array( 'hide', 'notice' ), true ) ); 290 } 291 }, 292 null 293 ); 294 } 295 return $response; 188 296 } 189 297 … … 196 304 */ 197 305 public static function validate_store_api( $order, $request ) { 198 if ( ! self::restricts() || self::COD !== (string) $request['payment_method'] ) { 199 return; 200 } 201 $phone = (string) $order->get_billing_phone(); 202 $email = (string) $order->get_billing_email(); 203 $country = (string) $order->get_billing_country(); 204 $count = self::count_for( $phone, $email, $country ); 205 if ( self::is_risky( $count, $phone, $email ) && class_exists( '\Automattic\WooCommerce\StoreApi\Exceptions\RouteException' ) ) { 306 $refuse = self::guard( 307 function () use ( $order, $request ) { 308 return self::refuse_cod( (string) $order->get_billing_phone(), (string) $order->get_billing_email(), (string) $order->get_billing_country(), (string) $request['payment_method'], false ); 309 }, 310 false 311 ); 312 if ( $refuse && class_exists( '\Automattic\WooCommerce\StoreApi\Exceptions\RouteException' ) ) { 206 313 throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'takrg_cod_unavailable', esc_html( self::message() ), 400 ); 207 314 } … … 214 321 */ 215 322 public static function refresh_script() { 216 if ( 'tag' === TAKRG_Settings:: get( 'action') || ! function_exists( 'is_checkout' ) || ! is_checkout() ) {323 if ( 'tag' === TAKRG_Settings::effective_action() || ! function_exists( 'is_checkout' ) || ! is_checkout() ) { 217 324 return; 218 325 } … … 227 334 private static function notice_html() { 228 335 $inner = ''; 229 if ( 'notice' === TAKRG_Settings:: get( 'action') ) {336 if ( 'notice' === TAKRG_Settings::effective_action() ) { 230 337 list( $risky ) = self::request_risk(); 231 338 if ( $risky ) { … … 241 348 */ 242 349 public static function render_notice() { 243 if ( 'notice' === TAKRG_Settings:: get( 'action') ) {244 echo wp_kses_post( self:: notice_html() );350 if ( 'notice' === TAKRG_Settings::effective_action() ) { 351 echo wp_kses_post( self::guard( array( __CLASS__, 'notice_html' ), '' ) ); 245 352 } 246 353 } … … 253 360 */ 254 361 public static function notice_fragment( $fragments ) { 255 if ( 'notice' === TAKRG_Settings::get( 'action' ) && is_array( $fragments ) ) { 256 $fragments['.takrg-notice-slot'] = wp_kses_post( self::notice_html() ); 362 if ( 'notice' === TAKRG_Settings::effective_action() && is_array( $fragments ) ) { 363 $html = self::guard( array( __CLASS__, 'notice_html' ), '' ); 364 if ( '' !== $html ) { 365 $fragments['.takrg-notice-slot'] = wp_kses_post( $html ); 366 } 257 367 } 258 368 return $fragments; … … 277 387 */ 278 388 public static function tag_order( $order ) { 279 if ( ! $order || $order->get_meta( '_takrg_risk' ) ) { 280 return; 281 } 282 list( $phone, $email ) = TAKRG_Recorder::identity( $order ); 283 $count = TAKRG_Store::count( $phone, $email ); 284 if ( ! self::is_risky( $count, $phone, $email ) ) { 285 return; 286 } 287 $order->update_meta_data( '_takrg_risk', $count ); 288 /* translators: %d: number of refused cash-on-delivery orders. */ 289 $order->add_order_note( sprintf( _n( 'COD risk: this customer (phone or e-mail) refused %d earlier order.', 'COD risk: this customer (phone or e-mail) refused %d earlier orders.', $count, 'takeed-refusal-guard' ), $count ) ); 290 $order->save(); 291 do_action( 'takrg_order_tagged', $order, $count ); 389 self::guard( 390 function () use ( $order ) { 391 if ( ! $order || $order->get_meta( '_takrg_risk' ) ) { 392 return; 393 } 394 $a = self::assess( $order->get_billing_phone(), $order->get_billing_email(), $order->get_billing_country() ); 395 if ( ! $a['risky'] ) { 396 return; 397 } 398 $count = $a['count']; 399 $order->update_meta_data( '_takrg_risk', $count ); 400 /* translators: %d: number of refused cash-on-delivery orders. */ 401 $order->add_order_note( sprintf( _n( 'COD risk: this customer (phone or e-mail) refused %d earlier order.', 'COD risk: this customer (phone or e-mail) refused %d earlier orders.', $count, 'takeed-refusal-guard' ), $count ) ); 402 $order->save(); 403 TAKRG_Log::add( 404 'tagged', 405 array( 406 'action' => 'tag', 407 'who' => $a['who'], 408 'refusals' => $count, 409 'order_id' => $order->get_id(), 410 ) 411 ); 412 do_action( 'takrg_order_tagged', $order, $count ); 413 }, 414 null 415 ); 292 416 } 293 417 } -
takeed-refusal-guard/trunk/includes/class-takrg-plugin.php
r3806545 r3807599 13 13 final class TAKRG_Plugin { 14 14 15 const CRON = 'takrg_daily_cleanup'; 16 15 17 /** 16 * Activation: create the table .18 * Activation: create the tables and schedule the daily cleanup. 17 19 */ 18 20 public static function activate() { 19 21 TAKRG_Store::install(); 22 self::schedule(); 23 } 24 25 /** 26 * Deactivation: stop the daily cleanup. 27 */ 28 public static function deactivate() { 29 wp_clear_scheduled_hook( self::CRON ); 30 } 31 32 /** 33 * Schedules the daily cleanup (retention) once. 34 */ 35 public static function schedule() { 36 if ( ! wp_next_scheduled( self::CRON ) ) { 37 wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON ); 38 } 39 } 40 41 /** 42 * Daily cleanup: records older than the retention the administrator chose. 43 */ 44 public static function cleanup() { 45 TAKRG_Store::purge_older_than( (int) TAKRG_Settings::get( 'retention' ) ); 46 } 47 48 /** 49 * Update from an older version. Shops that already use 1.0.x keep the behaviour they chose (live); only a 50 * fresh installation starts in test mode. 51 */ 52 public static function upgrade() { 53 $from = (string) get_option( TAKRG_Store::DB_VERSION_OPTION, '' ); 54 TAKRG_Store::install(); 55 if ( '' !== $from && version_compare( $from, '2', '<' ) ) { 56 $saved = get_option( TAKRG_Settings::OPTION, null ); 57 if ( is_array( $saved ) && ! isset( $saved['mode'] ) ) { 58 $saved['mode'] = 'live'; 59 update_option( TAKRG_Settings::OPTION, $saved ); 60 } 61 } 20 62 } 21 63 … … 28 70 } 29 71 if ( get_option( TAKRG_Store::DB_VERSION_OPTION ) !== TAKRG_Store::DB_VERSION ) { 30 TAKRG_Store::install();72 self::upgrade(); 31 73 } 74 self::schedule(); 75 add_action( self::CRON, array( __CLASS__, 'cleanup' ) ); 32 76 TAKRG_Recorder::init(); 33 77 TAKRG_Checkout::init(); -
takeed-refusal-guard/trunk/includes/class-takrg-privacy.php
r3806545 r3807599 109 109 ); 110 110 } 111 if ( 1 === $page && $email ) { // The person's own allow-list / block-list entries. 112 foreach ( TAKRG_Rules::for_person( $email, self::phones_of( $email ) ) as $rule ) { 113 $export[] = array( 114 'group_id' => 'takrg-rules', 115 'group_label' => __( 'COD allow-list and block-list entries', 'takeed-refusal-guard' ), 116 'group_description' => __( 'Entries added by the shop administrator.', 'takeed-refusal-guard' ), 117 'item_id' => 'takrg-rule-' . $rule['id'], 118 'data' => array( 119 array( 120 'name' => __( 'List', 'takeed-refusal-guard' ), 121 'value' => $rule['type'], 122 ), 123 array( 124 'name' => __( 'Value', 'takeed-refusal-guard' ), 125 'value' => $rule['value'], 126 ), 127 array( 128 'name' => __( 'Date (UTC)', 'takeed-refusal-guard' ), 129 'value' => $rule['created_at'], 130 ), 131 ), 132 ); 133 } 134 } 111 135 return array( 112 136 'data' => $export, … … 132 156 } 133 157 $removed += TAKRG_Store::delete_by_identity( '', $email ); 158 $removed += TAKRG_Rules::delete_exact( $email, $phones ); 134 159 TAKRG_Checkout::reset_cache(); 135 160 } … … 152 177 'Takeed Refusal Guard for WooCommerce', 153 178 wp_kses_post( 154 '<p class="privacy-policy-tutorial">' . __( 'Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery . Nothing is sent to the plugin author or to any third party.', 'takeed-refusal-guard' ) . '</p>' .179 '<p class="privacy-policy-tutorial">' . __( 'Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery, the entries you add to its allow-list and block-list, and an activity log in which customers appear masked. Nothing is sent to the plugin author or to any third party.', 'takeed-refusal-guard' ) . '</p>' . 155 180 '<p><strong class="privacy-policy-tutorial">' . __( 'Suggested text:', 'takeed-refusal-guard' ) . '</strong> ' . __( 'If you refuse a cash-on-delivery parcel, we keep your phone number and e-mail address together with the order number so that we can offer other payment methods for your next orders. You can ask us to erase this record.', 'takeed-refusal-guard' ) . '</p>' 156 181 ) -
takeed-refusal-guard/trunk/includes/class-takrg-recorder.php
r3806545 r3807599 27 27 add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', array( __CLASS__, 'handle_bulk' ), 10, 3 ); 28 28 add_action( 'takrg_signal', array( __CLASS__, 'on_signal' ), 10, 4 ); 29 add_filter( 'woocommerce_order_actions', array( __CLASS__, 'order_actions' ) ); 30 add_action( 'woocommerce_order_action_takrg_mark_refused', array( __CLASS__, 'action_mark_refused' ) ); 31 add_action( 'woocommerce_order_action_takrg_clear_refusal', array( __CLASS__, 'action_clear_refusal' ) ); 29 32 } 30 33 … … 91 94 92 95 /** 96 * Entries of the "Order actions" box on the order screen (legacy and HPOS). 97 * 98 * @param array $actions Order actions. 99 * @return array 100 */ 101 public static function order_actions( $actions ) { 102 $actions['takrg_mark_refused'] = __( 'Mark as refused at delivery', 'takeed-refusal-guard' ); 103 $actions['takrg_clear_refusal'] = __( 'Clear the refusal record of this order', 'takeed-refusal-guard' ); 104 return $actions; 105 } 106 107 /** 108 * Order action: set the refused status. 109 * 110 * @param WC_Order $order Order. 111 */ 112 public static function action_mark_refused( $order ) { 113 if ( $order && current_user_can( TAKRG_Admin::CAP ) ) { 114 $order->update_status( self::STATUS, __( 'Marked as refused at delivery.', 'takeed-refusal-guard' ) ); 115 } 116 } 117 118 /** 119 * Order action: forget this order's refusal (it was a mistake). 120 * 121 * @param WC_Order $order Order. 122 */ 123 public static function action_clear_refusal( $order ) { 124 if ( $order && current_user_can( TAKRG_Admin::CAP ) ) { 125 TAKRG_Store::remove_for_order( $order->get_id() ); 126 TAKRG_Checkout::reset_cache(); 127 $order->add_order_note( __( 'Refusal record cleared.', 'takeed-refusal-guard' ) ); 128 } 129 } 130 131 /** 93 132 * Country used to read a number typed without a country code: the order's own billing country when we 94 133 * support it, else the configured default. -
takeed-refusal-guard/trunk/includes/class-takrg-settings.php
r3806545 r3807599 23 23 public static function defaults() { 24 24 return array( 25 'mode' => 'test', 25 26 'threshold' => 1, 26 27 'action' => 'tag', 27 28 'statuses' => array( 'takeed-refused' ), 28 29 'default_country' => self::guess_country(), 30 'retention' => 0, 29 31 ); 32 } 33 34 /** 35 * The action that is really applied now. Test mode and a tripped safety breaker both mean "tag only". 36 * 37 * @return string tag | hide | notice. 38 */ 39 public static function effective_action() { 40 if ( 'live' !== self::get( 'mode' ) || TAKRG_Breaker::is_tripped() ) { 41 return 'tag'; 42 } 43 $action = self::get( 'action' ); 44 return in_array( $action, self::ACTIONS, true ) ? $action : 'tag'; 30 45 } 31 46 … … 93 108 $out = $defaults; 94 109 110 $mode = isset( $input['mode'] ) ? sanitize_key( $input['mode'] ) : ''; 111 $out['mode'] = in_array( $mode, array( 'test', 'live' ), true ) ? $mode : 'test'; 112 113 $out['retention'] = isset( $input['retention'] ) ? min( 120, absint( $input['retention'] ) ) : 0; 114 95 115 $out['threshold'] = isset( $input['threshold'] ) ? max( 1, min( 100, absint( $input['threshold'] ) ) ) : $defaults['threshold']; 96 116 -
takeed-refusal-guard/trunk/includes/class-takrg-store.php
r3806545 r3807599 15 15 16 16 const DB_VERSION_OPTION = 'takrg_db_version'; 17 const DB_VERSION = ' 1';17 const DB_VERSION = '2'; 18 18 19 19 /** … … 49 49 ) {$charset};" 50 50 ); 51 TAKRG_Rules::install(); 52 TAKRG_Log::install(); 51 53 update_option( self::DB_VERSION_OPTION, self::DB_VERSION ); 54 } 55 56 /** 57 * Deletes records older than a number of months (retention). 0 keeps everything. 58 * 59 * @param int $months Months to keep. 60 */ 61 public static function purge_older_than( $months ) { 62 global $wpdb; 63 $months = absint( $months ); 64 if ( $months < 1 ) { 65 return; 66 } 67 $before = gmdate( 'Y-m-d H:i:s', time() - $months * 30 * DAY_IN_SECONDS ); 68 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Our own table. 69 $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE created_at < %s', self::table(), $before ) ); 70 TAKRG_Log::purge_before( $before ); 52 71 } 53 72 -
takeed-refusal-guard/trunk/languages/takeed-refusal-guard.pot
r3806545 r3807599 10 10 "X-Domain: takeed-refusal-guard\n" 11 11 12 #: includes/class-takrg-admin.php:5 2 includes/class-takrg-admin.php:38812 #: includes/class-takrg-admin.php:57 includes/class-takrg-admin.php:499 13 13 msgid "Refusal Guard" 14 14 msgstr "" 15 15 16 #: includes/class-takrg-admin.php:8 1 includes/class-takrg-admin.php:8516 #: includes/class-takrg-admin.php:86 includes/class-takrg-admin.php:90 17 17 msgid "COD risk" 18 18 msgstr "" 19 19 20 #: includes/class-takrg-admin.php:1 2820 #: includes/class-takrg-admin.php:133 21 21 msgid "No refusals" 22 22 msgstr "" 23 23 24 #: includes/class-takrg-admin.php:1 39 includes/class-takrg-admin.php:36924 #: includes/class-takrg-admin.php:144 includes/class-takrg-admin.php:478 25 25 msgid "You are not allowed to do this." 26 26 msgstr "" 27 27 28 #: includes/class-takrg-admin.php:326 28 #: includes/class-takrg-admin.php:407 29 msgid "Takeed Refusal Guard switched itself to \"tag only\"." 30 msgstr "" 31 32 #: includes/class-takrg-admin.php:408 33 msgid "More than half of the recent checkout attempts were restricted, so the plugin stopped restricting Cash on Delivery to protect your sales." 34 msgstr "" 35 36 #: includes/class-takrg-admin.php:409 37 msgid "Review and turn it back on" 38 msgstr "" 39 40 #: includes/class-takrg-admin.php:423 29 41 msgid "%d record added." 30 42 msgid_plural "%d records added." … … 32 44 msgstr[1] "" 33 45 34 #: includes/class-takrg-admin.php: 32946 #: includes/class-takrg-admin.php:426 35 47 msgid "%d record removed." 36 48 msgid_plural "%d records removed." … … 38 50 msgstr[1] "" 39 51 40 #: includes/class-takrg-admin.php: 33252 #: includes/class-takrg-admin.php:429 41 53 msgid "%d record imported." 42 54 msgid_plural "%d records imported." … … 44 56 msgstr[1] "" 45 57 46 #: includes/class-takrg-admin.php:334 58 #: includes/class-takrg-admin.php:431 59 msgid "Rule added." 60 msgstr "" 61 62 #: includes/class-takrg-admin.php:433 63 msgid "Enter an e-mail address, a phone number, or a pattern such as *@example.com or +21620* (not already in the list)." 64 msgstr "" 65 66 #: includes/class-takrg-admin.php:435 67 msgid "Protection turned back on." 68 msgstr "" 69 70 #: includes/class-takrg-admin.php:438 71 msgid "Scan finished: %d record added." 72 msgid_plural "Scan finished: %d records added." 73 msgstr[0] "" 74 msgstr[1] "" 75 76 #: includes/class-takrg-admin.php:441 77 msgid "%d record added. More orders remain: press \"Continue scan\"." 78 msgid_plural "%d records added. More orders remain: press \"Continue scan\"." 79 msgstr[0] "" 80 msgstr[1] "" 81 82 #: includes/class-takrg-admin.php:443 47 83 msgid "Enter a valid phone number or e-mail address." 48 84 msgstr "" 49 85 50 #: includes/class-takrg-admin.php: 33686 #: includes/class-takrg-admin.php:445 51 87 msgid "Choose a .csv file of 1 MB or less." 52 88 msgstr "" 53 89 54 #: includes/class-takrg-admin.php: 34890 #: includes/class-takrg-admin.php:457 55 91 msgid "Tunisia (+216)" 56 92 msgstr "" 57 93 58 #: includes/class-takrg-admin.php: 34994 #: includes/class-takrg-admin.php:458 59 95 msgid "Morocco (+212)" 60 96 msgstr "" 61 97 62 #: includes/class-takrg-admin.php: 35098 #: includes/class-takrg-admin.php:459 63 99 msgid "Algeria (+213)" 64 100 msgstr "" 65 101 66 #: includes/class-takrg-admin.php: 351102 #: includes/class-takrg-admin.php:460 67 103 msgid "Egypt (+20)" 68 104 msgstr "" 69 105 70 #: includes/class-takrg-admin.php: 352106 #: includes/class-takrg-admin.php:461 71 107 msgid "Saudi Arabia (+966)" 72 108 msgstr "" 73 109 74 #: includes/class-takrg-admin.php: 353110 #: includes/class-takrg-admin.php:462 75 111 msgid "United Arab Emirates (+971)" 76 112 msgstr "" 77 113 78 #: includes/class-takrg-admin.php: 354114 #: includes/class-takrg-admin.php:463 79 115 msgid "Kuwait (+965)" 80 116 msgstr "" 81 117 82 #: includes/class-takrg-admin.php: 355118 #: includes/class-takrg-admin.php:464 83 119 msgid "Qatar (+974)" 84 120 msgstr "" 85 121 86 #: includes/class-takrg-admin.php: 356122 #: includes/class-takrg-admin.php:465 87 123 msgid "Oman (+968)" 88 124 msgstr "" 89 125 90 #: includes/class-takrg-admin.php: 357126 #: includes/class-takrg-admin.php:466 91 127 msgid "Bahrain (+973)" 92 128 msgstr "" 93 129 94 #: includes/class-takrg-admin.php: 382130 #: includes/class-takrg-admin.php:493 95 131 msgid "Tag only (default)" 96 132 msgstr "" 97 133 98 #: includes/class-takrg-admin.php: 382134 #: includes/class-takrg-admin.php:493 99 135 msgid "Adds an order note and the meta \"_takrg_risk\". Nothing is blocked." 100 136 msgstr "" 101 137 102 #: includes/class-takrg-admin.php: 383138 #: includes/class-takrg-admin.php:494 103 139 msgid "Hide Cash on Delivery" 104 140 msgstr "" 105 141 106 #: includes/class-takrg-admin.php: 383142 #: includes/class-takrg-admin.php:494 107 143 msgid "Cash on Delivery is not offered to this customer; other payment methods stay available." 108 144 msgstr "" 109 145 110 #: includes/class-takrg-admin.php: 384146 #: includes/class-takrg-admin.php:495 111 147 msgid "Show a notice" 112 148 msgstr "" 113 149 114 #: includes/class-takrg-admin.php: 384150 #: includes/class-takrg-admin.php:495 115 151 msgid "Shows a neutral message asking the customer to choose another payment method, and refuses Cash on Delivery on submit." 116 152 msgstr "" 117 153 118 #: includes/class-takrg-admin.php:397 154 #: includes/class-takrg-admin.php:506 155 msgid "The safety breaker switched the plugin to \"tag only\"." 156 msgstr "" 157 158 #: includes/class-takrg-admin.php:507 159 msgid "More than half of the checkout attempts in an hour were restricted. Check the activity log below, then turn the protection back on." 160 msgstr "" 161 162 #: includes/class-takrg-admin.php:511 163 msgid "Turn protection back on" 164 msgstr "" 165 166 #: includes/class-takrg-admin.php:516 167 msgid "Test mode is on." 168 msgstr "" 169 170 #: includes/class-takrg-admin.php:517 171 msgid "Nothing is hidden or refused: orders are only tagged, and the activity log below shows what the plugin would have done. When the log looks right, switch to Live." 172 msgstr "" 173 174 #: includes/class-takrg-admin.php:524 175 msgid "Mode" 176 msgstr "" 177 178 #: includes/class-takrg-admin.php:527 179 msgid "Test mode (default)" 180 msgstr "" 181 182 #: includes/class-takrg-admin.php:528 183 msgid "Only tags orders and writes what it would have done to the activity log." 184 msgstr "" 185 186 #: includes/class-takrg-admin.php:529 187 msgid "Live" 188 msgstr "" 189 190 #: includes/class-takrg-admin.php:530 191 msgid "Applies the action below. A safety breaker returns to \"tag only\" if more than 50% of the checkout attempts in an hour (at least 5) are restricted." 192 msgstr "" 193 194 #: includes/class-takrg-admin.php:535 119 195 msgid "Refusals before acting" 120 196 msgstr "" 121 197 122 #: includes/class-takrg-admin.php: 400198 #: includes/class-takrg-admin.php:538 123 199 msgid "A customer (same phone number or same e-mail) with at least this many refusals is treated as a risk." 124 200 msgstr "" 125 201 126 #: includes/class-takrg-admin.php: 404202 #: includes/class-takrg-admin.php:542 127 203 msgid "Action at checkout" 128 204 msgstr "" 129 205 130 #: includes/class-takrg-admin.php: 415206 #: includes/class-takrg-admin.php:553 131 207 msgid "Statuses that count as a refusal" 132 208 msgstr "" 133 209 134 #: includes/class-takrg-admin.php: 423210 #: includes/class-takrg-admin.php:561 135 211 msgid "Default: only \"Refused at delivery\". Be careful with statuses such as Cancelled: customers cancel for many reasons." 136 212 msgstr "" 137 213 138 #: includes/class-takrg-admin.php: 427214 #: includes/class-takrg-admin.php:565 139 215 msgid "Default country" 140 216 msgstr "" 141 217 142 #: includes/class-takrg-admin.php: 430218 #: includes/class-takrg-admin.php:568 143 219 msgid "Used to read phone numbers typed without a country code." 144 220 msgstr "" 145 221 146 #: includes/class-takrg-admin.php:438 222 #: includes/class-takrg-admin.php:572 223 msgid "Forget refusals after (months)" 224 msgstr "" 225 226 #: includes/class-takrg-admin.php:575 227 msgid "0 keeps them until you delete them. Older records and log entries are removed once a day." 228 msgstr "" 229 230 #: includes/class-takrg-admin.php:583 147 231 msgid "Add or remove a number" 148 232 msgstr "" 149 233 150 #: includes/class-takrg-admin.php: 443 includes/class-takrg-admin.php:457 includes/class-takrg-admin.php:490 includes/class-takrg-privacy.php:93234 #: includes/class-takrg-admin.php:588 includes/class-takrg-admin.php:602 includes/class-takrg-admin.php:690 includes/class-takrg-privacy.php:93 151 235 msgid "Phone" 152 236 msgstr "" 153 237 154 #: includes/class-takrg-admin.php: 445 includes/class-takrg-admin.php:459238 #: includes/class-takrg-admin.php:590 includes/class-takrg-admin.php:604 includes/class-takrg-admin.php:622 155 239 msgid "Country" 156 240 msgstr "" 157 241 158 #: includes/class-takrg-admin.php: 446 includes/class-takrg-admin.php:460 includes/class-takrg-admin.php:491 includes/class-takrg-privacy.php:97242 #: includes/class-takrg-admin.php:591 includes/class-takrg-admin.php:605 includes/class-takrg-admin.php:691 includes/class-takrg-privacy.php:97 159 243 msgid "E-mail" 160 244 msgstr "" 161 245 162 #: includes/class-takrg-admin.php: 448246 #: includes/class-takrg-admin.php:593 includes/class-takrg-admin.php:744 163 247 msgid "Refusals" 164 248 msgstr "" 165 249 166 #: includes/class-takrg-admin.php: 450250 #: includes/class-takrg-admin.php:595 167 251 msgid "Add" 168 252 msgstr "" 169 253 170 #: includes/class-takrg-admin.php: 462254 #: includes/class-takrg-admin.php:607 171 255 msgid "Remove all records of this number or e-mail" 172 256 msgstr "" 173 257 174 #: includes/class-takrg-admin.php:466 258 #: includes/class-takrg-admin.php:611 259 msgid "Allow-list and block-list" 260 msgstr "" 261 262 #: includes/class-takrg-admin.php:612 263 msgid "Allow: never act on this customer, whatever the refusals. Block: always treat this customer as having reached the threshold. Use an e-mail, a phone number, or a pattern with * such as *@example.com or +21620*." 264 msgstr "" 265 266 #: includes/class-takrg-admin.php:617 includes/class-takrg-admin.php:630 includes/class-takrg-privacy.php:120 267 msgid "List" 268 msgstr "" 269 270 #: includes/class-takrg-admin.php:618 includes/class-takrg-admin.php:638 271 msgid "Allow" 272 msgstr "" 273 274 #: includes/class-takrg-admin.php:619 includes/class-takrg-admin.php:638 275 msgid "Block" 276 msgstr "" 277 278 #: includes/class-takrg-admin.php:621 includes/class-takrg-admin.php:631 includes/class-takrg-privacy.php:124 279 msgid "Value" 280 msgstr "" 281 282 #: includes/class-takrg-admin.php:623 283 msgid "Add rule" 284 msgstr "" 285 286 #: includes/class-takrg-admin.php:632 includes/class-takrg-admin.php:694 includes/class-takrg-admin.php:740 includes/class-takrg-privacy.php:105 includes/class-takrg-privacy.php:128 287 msgid "Date (UTC)" 288 msgstr "" 289 290 #: includes/class-takrg-admin.php:646 includes/class-takrg-admin.php:713 291 msgid "Delete" 292 msgstr "" 293 294 #: includes/class-takrg-admin.php:655 295 msgid "Count refusals from existing orders" 296 msgstr "" 297 298 #: includes/class-takrg-admin.php:661 299 msgid "Continue scan" 300 msgstr "" 301 302 #: includes/class-takrg-admin.php:661 303 msgid "Scan existing orders" 304 msgstr "" 305 306 #: includes/class-takrg-admin.php:663 307 msgid "Adds a record for every order that is already in a counted status. Orders that already have a record are skipped, so it is safe to repeat." 308 msgstr "" 309 310 #: includes/class-takrg-admin.php:666 175 311 msgid "Import and export" 176 312 msgstr "" 177 313 178 #: includes/class-takrg-admin.php: 472314 #: includes/class-takrg-admin.php:672 179 315 msgid "Import CSV" 180 316 msgstr "" 181 317 182 #: includes/class-takrg-admin.php: 474318 #: includes/class-takrg-admin.php:674 183 319 msgid "One row per refusal: phone,email (a header row is optional). Numbers without a country code use the default country." 184 320 msgstr "" 185 321 186 #: includes/class-takrg-admin.php: 479322 #: includes/class-takrg-admin.php:679 187 323 msgid "Export CSV" 188 324 msgstr "" 189 325 190 #: includes/class-takrg-admin.php: 485326 #: includes/class-takrg-admin.php:685 191 327 msgid "Stored refusals (%d)" 192 328 msgstr "" 193 329 194 #: includes/class-takrg-admin.php: 492includes/class-takrg-privacy.php:101330 #: includes/class-takrg-admin.php:692 includes/class-takrg-admin.php:745 includes/class-takrg-privacy.php:101 195 331 msgid "Order" 196 332 msgstr "" 197 333 198 #: includes/class-takrg-admin.php: 493334 #: includes/class-takrg-admin.php:693 199 335 msgid "Source" 200 336 msgstr "" 201 337 202 #: includes/class-takrg-admin.php:494 includes/class-takrg-privacy.php:105 203 msgid "Date (UTC)" 204 msgstr "" 205 206 #: includes/class-takrg-admin.php:499 338 #: includes/class-takrg-admin.php:699 207 339 msgid "No refusals recorded yet." 208 340 msgstr "" 209 341 210 #: includes/class-takrg-admin.php:513 211 msgid "Delete" 212 msgstr "" 213 214 #: includes/class-takrg-checkout.php:159 342 #: includes/class-takrg-admin.php:736 343 msgid "Activity log" 344 msgstr "" 345 346 #: includes/class-takrg-admin.php:737 347 msgid "The last events. Customers are masked. In test mode, \"Would apply\" is what Live mode would have done." 348 msgstr "" 349 350 #: includes/class-takrg-admin.php:741 351 msgid "Event" 352 msgstr "" 353 354 #: includes/class-takrg-admin.php:742 355 msgid "Action" 356 msgstr "" 357 358 #: includes/class-takrg-admin.php:743 359 msgid "Customer" 360 msgstr "" 361 362 #: includes/class-takrg-admin.php:746 363 msgid "Details" 364 msgstr "" 365 366 #: includes/class-takrg-admin.php:751 367 msgid "Nothing yet." 368 msgstr "" 369 370 #: includes/class-takrg-admin.php:778 371 msgid "Order tagged" 372 msgstr "" 373 374 #: includes/class-takrg-admin.php:779 375 msgid "Would apply (test mode)" 376 msgstr "" 377 378 #: includes/class-takrg-admin.php:780 379 msgid "Applied" 380 msgstr "" 381 382 #: includes/class-takrg-admin.php:781 383 msgid "Safety breaker" 384 msgstr "" 385 386 #: includes/class-takrg-admin.php:782 387 msgid "Error (order allowed)" 388 msgstr "" 389 390 #: includes/class-takrg-breaker.php:158 391 msgid "Takeed Refusal Guard switched itself to \"tag only\"" 392 msgstr "" 393 394 #: includes/class-takrg-breaker.php:161 395 msgid "%1$d of the last %2$d checkout attempts were restricted, which is more than half. To protect your sales the plugin now only tags orders. Review the activity log under WooCommerce > Refusal Guard, then turn the protection back on there." 396 msgstr "" 397 398 #: includes/class-takrg-checkout.php:211 215 399 msgid "Cash on delivery is not available for this order. Please choose another payment method." 216 400 msgstr "" 217 401 218 #: includes/class-takrg-checkout.php: 289402 #: includes/class-takrg-checkout.php:401 219 403 msgid "COD risk: this customer (phone or e-mail) refused %d earlier order." 220 404 msgid_plural "COD risk: this customer (phone or e-mail) refused %d earlier orders." … … 230 414 msgstr "" 231 415 232 #: includes/class-takrg-privacy.php:154 233 msgid "Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery. Nothing is sent to the plugin author or to any third party." 234 msgstr "" 235 236 #: includes/class-takrg-privacy.php:155 416 #: includes/class-takrg-privacy.php:115 417 msgid "COD allow-list and block-list entries" 418 msgstr "" 419 420 #: includes/class-takrg-privacy.php:116 421 msgid "Entries added by the shop administrator." 422 msgstr "" 423 424 #: includes/class-takrg-privacy.php:179 425 msgid "Takeed Refusal Guard stores, in your own database, the phone number (digits only), the e-mail address, the order number and the date of each order that was refused at delivery, the entries you add to its allow-list and block-list, and an activity log in which customers appear masked. Nothing is sent to the plugin author or to any third party." 426 msgstr "" 427 428 #: includes/class-takrg-privacy.php:180 237 429 msgid "Suggested text:" 238 430 msgstr "" 239 431 240 #: includes/class-takrg-privacy.php:1 55432 #: includes/class-takrg-privacy.php:180 241 433 msgid "If you refuse a cash-on-delivery parcel, we keep your phone number and e-mail address together with the order number so that we can offer other payment methods for your next orders. You can ask us to erase this record." 242 434 msgstr "" 243 435 244 #: includes/class-takrg-recorder.php: 38 includes/class-takrg-recorder.php:56436 #: includes/class-takrg-recorder.php:41 includes/class-takrg-recorder.php:59 245 437 msgctxt "Order status" 246 438 msgid "Refused at delivery" 247 439 msgstr "" 248 440 249 #: includes/class-takrg-recorder.php:4 4441 #: includes/class-takrg-recorder.php:47 250 442 msgid "Refused at delivery <span class=\"count\">(%s)</span>" 251 443 msgid_plural "Refused at delivery <span class=\"count\">(%s)</span>" … … 253 445 msgstr[1] "" 254 446 255 #: includes/class-takrg-recorder.php: 67447 #: includes/class-takrg-recorder.php:70 256 448 msgid "Change status to refused at delivery" 257 449 msgstr "" 258 450 259 #: includes/class-takrg-recorder.php:8 6451 #: includes/class-takrg-recorder.php:89 includes/class-takrg-recorder.php:114 260 452 msgid "Marked as refused at delivery." 261 453 msgstr "" 262 454 263 #: includes/class-takrg-recorder.php:130 455 #: includes/class-takrg-recorder.php:102 456 msgid "Mark as refused at delivery" 457 msgstr "" 458 459 #: includes/class-takrg-recorder.php:103 460 msgid "Clear the refusal record of this order" 461 msgstr "" 462 463 #: includes/class-takrg-recorder.php:127 464 msgid "Refusal record cleared." 465 msgstr "" 466 467 #: includes/class-takrg-recorder.php:169 264 468 msgid "Refusal recorded by Takeed Refusal Guard." 265 469 msgstr "" -
takeed-refusal-guard/trunk/readme.txt
r3807459 r3807599 8 8 WC requires at least: 7.1 9 9 WC tested up to: 11.3 10 Stable tag: 1. 0.010 Stable tag: 1.1.0 11 11 License: GPLv2 or later 12 12 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 27 27 * **Show a neutral notice** asking the customer to choose another payment method. 28 28 29 **Safe by design (new in 1.1)** 30 31 * **Test mode is the default on a new installation.** Orders are only tagged, and an activity log shows what the plugin would have done. Switch to Live when the log looks right. 32 * **Safety breaker.** If more than 50% of the checkout attempts in an hour are restricted (and there are at least 5 attempts), the plugin returns to "tag only" by itself and tells the administrator. It stays there until you turn it back on. 33 * **Fail-open.** If anything goes wrong while the plugin checks a customer, the order goes through. 34 * **Allow-list.** Customers you trust are never touched, whatever their refusals. 35 29 36 **What makes it different** 30 37 … … 38 45 39 46 * A **COD risk** column in the orders list (legacy and HPOS screens) showing the refusal count of the customer. 47 * **Block-list and patterns**: add an e-mail, a phone number or a pattern such as `*@example.com` or `+21620*`. 48 * **Order actions** on the order screen: mark as refused, or clear a refusal that was a mistake. 49 * **Count refusals from existing orders** with one click, and **forget old refusals** automatically after a number of months you choose. 50 * **Activity log** of the last 500 events, with customers masked. 40 51 * Settings under **WooCommerce > Refusal Guard**: threshold, action, counted statuses, default country, add or remove numbers by hand, CSV import and export. 41 52 * WordPress personal data exporter and eraser for the stored records. … … 88 99 There is no hard dependency between this plugin and any other Takeed plugin. 89 100 101 = What is the safety breaker? = 102 103 A circuit breaker against mistakes. If more than half of the checkout attempts of the last hour were restricted (with at least 5 attempts, and one customer counted once), something is wrong, for example a rule that is too wide. The plugin then only tags orders, shows a notice in the admin and sends the administrator an e-mail (through your own mail setup). The restriction comes back only when you press "Turn protection back on". The attempt that trips the breaker is still restricted; from the next checkout on, Cash on Delivery is available again. 104 105 = What happens if the plugin fails during checkout? = 106 107 The order goes through. Any error while checking a customer lets the order continue, and the error is written to the activity log. 108 109 = I update from 1.0. Will my shop change? = 110 111 No. Sites that already used 1.0 keep the action they chose and stay in Live mode. Only new installations start in Test mode. 112 90 113 = Does it send data anywhere? = 91 114 … … 105 128 * The records are included in the WordPress personal data export (Tools > Export Personal Data) and removed by the erasure tool (Tools > Erase Personal Data), matched by e-mail address and by the phone numbers tied to it. 106 129 * A suggested paragraph is added to your privacy policy page (Settings > Privacy). 107 * Deleting the plugin removes its table and its options. 130 * The activity log shows customers masked (the first and last digits of a phone number; the first letter and the domain of an e-mail address). Allow-list and block-list entries are included in the export and the erasure when they are exact matches of the person. 131 * With "Forget refusals after (months)" you choose how long records are kept; the default keeps them until you delete them. 132 * Deleting the plugin removes its tables (refusals, rules, log) and its options. 108 133 109 134 == Changelog == 135 136 = 1.1.0 = 137 * New installations start in Test mode: orders are tagged and an activity log shows what the plugin would have done. 138 * Safety breaker: more than 50% of the checkout attempts in an hour (at least 5) returns the plugin to "tag only" and notifies the administrator. 139 * Fail-open: any error while checking a customer lets the order through. 140 * Allow-list, block-list and wildcard patterns (e-mail and phone prefix). 141 * Order screen actions: mark as refused, clear a refusal. Bulk action unchanged. 142 * Count refusals from existing orders. Optional retention in months. Activity log with masked customers. 143 * Privacy exporter and eraser include allow-list and block-list entries. 144 * Sites updating from 1.0 keep their action and stay in Live mode. 110 145 111 146 = 1.0.0 = … … 114 149 == Upgrade Notice == 115 150 151 = 1.1.0 = 152 Adds Test mode (default for new installations), a safety breaker, an allow-list and an activity log. Existing sites keep their current behaviour. 153 116 154 = 1.0.0 = 117 155 First release. -
takeed-refusal-guard/trunk/takeed-refusal-guard.php
r3806545 r3807599 3 3 * Plugin Name: Takeed Refusal Guard for WooCommerce 4 4 * Description: Remembers customers who refused a cash-on-delivery parcel and flags, or limits, cash on delivery on their next order. Built-in phone formats for Tunisia, Morocco, Algeria, Egypt and the Gulf. 5 * Version: 1. 0.05 * Version: 1.1.0 6 6 * Requires at least: 6.2 7 7 * Requires PHP: 7.4 … … 21 21 defined( 'ABSPATH' ) || exit; 22 22 23 define( 'TAKRG_VERSION', '1. 0.0' );23 define( 'TAKRG_VERSION', '1.1.0' ); 24 24 define( 'TAKRG_FILE', __FILE__ ); 25 25 define( 'TAKRG_DIR', plugin_dir_path( __FILE__ ) ); … … 27 27 require_once TAKRG_DIR . 'includes/class-takrg-phone.php'; 28 28 require_once TAKRG_DIR . 'includes/class-takrg-settings.php'; 29 require_once TAKRG_DIR . 'includes/class-takrg-rules.php'; 30 require_once TAKRG_DIR . 'includes/class-takrg-log.php'; 31 require_once TAKRG_DIR . 'includes/class-takrg-breaker.php'; 29 32 require_once TAKRG_DIR . 'includes/class-takrg-store.php'; 30 33 require_once TAKRG_DIR . 'includes/class-takrg-recorder.php'; … … 47 50 48 51 register_activation_hook( __FILE__, array( 'TAKRG_Plugin', 'activate' ) ); 52 register_deactivation_hook( __FILE__, array( 'TAKRG_Plugin', 'deactivate' ) ); 49 53 50 54 add_action( 'plugins_loaded', array( 'TAKRG_Plugin', 'boot' ) ); -
takeed-refusal-guard/trunk/uninstall.php
r3806545 r3807599 15 15 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall removes our own table. 16 16 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'takrg_refusals' ) ); 17 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall removes our own table. 18 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'takrg_rules' ) ); 19 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall removes our own table. 20 $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'takrg_log' ) ); 17 21 delete_option( 'takrg_settings' ); 18 22 delete_option( 'takrg_db_version' ); 23 delete_option( 'takrg_breaker' ); 24 wp_clear_scheduled_hook( 'takrg_daily_cleanup' ); 19 25 } 20 26
Note:
See TracChangeset
for help on using the changeset viewer.