Changeset 3795033
- Timestamp:
- 11/10/2026 06:35:49 PM (3 weeks ago)
- Location:
- add-customer-for-woocommerce/trunk
- Files:
-
- 7 edited
-
add-customer-for-woocommerce.php (modified) (2 diffs)
-
include/classes/wac-admin.php (modified) (5 diffs)
-
include/classes/wac-backend.php (modified) (1 diff)
-
include/tools/helper.php (modified) (25 diffs)
-
readme.txt (modified) (2 diffs)
-
templates/backend/backend-options-page-template.php (modified) (1 diff)
-
templates/order/add-checkbox.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
add-customer-for-woocommerce/trunk/add-customer-for-woocommerce.php
r3760001 r3795033 5 5 * Plugin URI: https://dev.dans-art.ch/blog/wordpress/add-customer-for-woocommerce/ 6 6 * Description: Allows you to add a customer when creating a new order on the orders page 7 * Version: 1.10. 07 * Version: 1.10.1 8 8 * Requires at least: 5.4.0 9 9 * Requires PHP: 7.4 … … 16 16 * 17 17 * WC requires at least: 7.4.1 18 * WC tested up to: 11. 1.018 * WC tested up to: 11.2.0 19 19 */ 20 20 -
add-customer-for-woocommerce/trunk/include/classes/wac-admin.php
r3760001 r3795033 43 43 add_action('woocommerce_admin_order_data_after_billing_address', [$this, 'wac_add_checkbox'], 10, 1); 44 44 add_action('woocommerce_created_customer', [$this, 'wac_disable_new_customer_mail'], 1, 1); 45 add_action('delete_user', [$this, 'wac_decrease_counter_on_user_delete'], 10, 1); 45 46 add_action('woocommerce_process_shop_order_meta', [$this, 'wac_save_order'], 99, 2); 46 47 … … 532 533 $password = wp_generate_password(); 533 534 $email_is_fake = false; 534 $user_role = (isset($_REQUEST['wac_add_customer_role'])) ? sanitize_key(wp_unslash($_REQUEST['wac_add_customer_role'])) : $this->get_default_user_role(); 535 536 //The role from the dropdown is only used if the dropdown is enabled. Otherwise the default role set by the admin is used. 537 $role_dropdown_enabled = $this->get_wac_option('wac_define_user_role') === 'yes'; 538 $requested_role = (isset($_REQUEST['wac_add_customer_role'])) ? sanitize_key(wp_unslash($_REQUEST['wac_add_customer_role'])) : ''; 539 $use_requested_role = $role_dropdown_enabled && $requested_role !== ''; 540 $user_role = ($use_requested_role) ? $requested_role : $this->get_default_user_role(); 535 541 536 542 $order = wc_get_order($order_id); … … 542 548 if (empty($email)) { 543 549 //create new 'fake' email 544 $email = $this->create_fake_email($user );550 $email = $this->create_fake_email($user, $order_id); 545 551 $email_is_fake = true; 546 552 } 547 553 548 554 //Add hook to allow to modify the email 549 $email = $this->make_email_valid($email );555 $email = $this->make_email_valid($email, $order_id); 550 556 $email = apply_filters('wac_add_customer_email', $email, $user); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public hook, renaming would break existing customizations 551 557 … … 557 563 } 558 564 559 //Validate the user role. This should never return false, except if someone alters the select selection 560 $valid_user_roles = $this->get_user_role_array(); 561 if (!isset($valid_user_roles[$user_role])) { 565 //Validate the user role. 566 //A role chosen in the dropdown must be one the current user is allowed to assign. 567 //The default role was set by the admin and is already checked by get_default_user_role(), so it only has to exist. 568 if ($use_requested_role) { 569 $valid_user_roles = $this->get_user_role_array(); 570 $is_role_allowed = isset($valid_user_roles[$user_role]); 571 } else { 572 $is_role_allowed = $this->is_existing_user_role($user_role); 573 } 574 575 if (!$is_role_allowed) { 562 576 //User role does not exist, or is not allowed 563 577 $this->log_event("user_role_not_allowed", $order_id, $user_role); 564 578 return false; 565 } else { 566 //Add the role 567 add_filter('woocommerce_new_customer_data', function ($data) { 568 $user_role = (isset($_REQUEST['wac_add_customer_role'])) ? sanitize_key(wp_unslash($_REQUEST['wac_add_customer_role'])) : $this->get_default_user_role(); 569 $data['role'] = $user_role; 570 return $data; 571 }); 572 } 579 } 580 581 //Add the validated role 582 add_filter('woocommerce_new_customer_data', function ($data) use ($user_role) { 583 $data['role'] = $user_role; 584 return $data; 585 }); 573 586 574 587 //Save the email to the order … … 583 596 584 597 //Make the username valid 585 $user = $this->make_user_valid($user );598 $user = $this->make_user_valid($user, $order_id); 586 599 587 600 if ($user !== false) { -
add-customer-for-woocommerce/trunk/include/classes/wac-backend.php
r3760001 r3795033 261 261 case 'wac_fakemail_format': 262 262 if (!empty($value)) { 263 $input[$key] = $this->sanitize_placeholder_email($value, $key); 263 $valid_format = $this->sanitize_placeholder_email($value, $key); 264 //Keep the previously saved format, if the new one is not valid 265 $previous_format = (string) $this->get_wac_option($key); 266 $input[$key] = ($valid_format !== false) ? $valid_format : $previous_format; 264 267 } else { 265 268 $input[$key] = ''; -
add-customer-for-woocommerce/trunk/include/tools/helper.php
r3760001 r3795033 38 38 * It is recommended to setup a catch-all email 39 39 * 40 * The email is normalized before the uniqueness check, so the returned address is the final one. 41 * 40 42 * @param string $username - A username to start with or null 41 * @return string A unique fake email 42 */ 43 public function create_fake_email($username = null) 43 * @param int|null $order_id - The order the notices belong to. Falls back to get_the_ID() 44 * @return string|false A unique fake email, false if no valid or unique email could be created 45 */ 46 public function create_fake_email($username = null, $order_id = null) 47 { 48 $name = $this->create_fake_email_name($username); 49 $email = $this->normalize_fake_email($name, $order_id); 50 51 if ($email === false) { 52 //Nothing valid left of the name (e.g. only non latin characters without the Intl extension). Use a random name instead 53 $email = $this->normalize_fake_email(wp_generate_password(10, false), $order_id); 54 } 55 if ($email === false) { 56 return false; 57 } 58 59 //Add a number if email already exists 60 list($name, $domain_name) = explode('@', $email, 2); 61 $number = 0; 62 while (email_exists($email) !== false) { 63 $number++; 64 if ($number >= 100) { 65 return false; 66 } 67 $email = $name . $number . '@' . $domain_name; 68 } 69 70 return $email; 71 } 72 73 /** 74 * Converts the name to a valid email with the fake email domain. 75 * Uses the same sanitizing as for the real emails and makes sure that there is room for a two digit number. 76 * 77 * @param string $name - The name part of the email 78 * @param int|null $order_id - The order the notices belong to 79 * @return string|false The valid email, false if the email is not valid 80 */ 81 private function normalize_fake_email($name, $order_id = null) 44 82 { 45 83 $domain_name = $this->get_domain_name(); 46 $number = '';47 $name = $this->create_fake_email_name($username);48 84 $email_total_characters = 100; //Maximum length of the email according to the user_email field in wp_users 85 $number_characters = 2; //Room for the number added if the email already exists 86 87 $name = str_replace(' ', '_', trim($name)); //Replace the spaces with underlines 88 $email = $this->make_email_valid($name . '@' . $domain_name, $order_id); 89 if (!is_email($email)) { 90 return false; 91 } 49 92 50 93 //Make sure that the email is not too long 51 if(strlen($name) > ($email_total_characters - strlen($domain_name) - 1)){ 52 $name = substr($name, 0, ($email_total_characters - strlen($domain_name) - 1)); 53 } 54 55 //Add a number if email already exists 56 while ((get_user_by('email', $name . $number . '@' . $domain_name) !== false) and $number < 100) { 57 if (empty($number)) { 58 $number = 0; 94 list($name, $domain_name) = explode('@', $email, 2); 95 $max_name_length = $email_total_characters - strlen($domain_name) - 1 - $number_characters; 96 $name = rtrim(substr($name, 0, $max_name_length), '.'); 97 if ($name === '') { 98 return false; 99 } 100 101 return $name . '@' . $domain_name; 102 } 103 104 /** 105 * Receives the domain name without the scheme 106 * If the option wac_fakemail_format is set and valid, the domain name will be taken from the option. 107 * 108 * @return string The Domain name. 109 */ 110 public function get_domain_name() 111 { 112 $custom_format = $this->get_valid_fakemail_format(); 113 if ($custom_format !== null) { 114 list(, $custom_domain) = explode('@', $custom_format, 2); 115 if ($custom_domain !== '') { 116 return $custom_domain; 59 117 } 60 $number++;61 }62 if ($number == 100) {63 return false;64 }65 $email = $name . $number . '@' . $domain_name;66 $email = str_replace(' ', '_', $email); //Replace the spaces with underlines67 $email = trim($email); //Removes all the other whitespaces, tabs, new-lines, etc.68 69 return $email;70 }71 72 /**73 * Receives the domain name without the scheme74 * If the option wac_fakemail_format is set, the domain name will be taken from the option.75 *76 * @return string The Domain name.77 */78 public function get_domain_name()79 {80 $custom_format = $this->get_wac_option('wac_fakemail_format');81 $exp = ($custom_format !== null) ? explode('@', $custom_format) : null;82 //$exp[0] = email Name, $exp[1] = domain name83 if (isset($exp[1])) {84 return $exp[1];85 118 } 86 119 $urlparts = wp_parse_url(home_url()); … … 89 122 90 123 /** 124 * Returns the custom fake email format from the options, but only if it is valid. 125 * 126 * @return string|null The format, null if not set or not valid 127 */ 128 private function get_valid_fakemail_format() 129 { 130 $custom_format = $this->get_wac_option('wac_fakemail_format'); 131 if (empty($custom_format)) { 132 return null; 133 } 134 135 $backend_class = new woo_add_customer_backend; 136 $format_error = $backend_class->check_option('wac_fakemail_format', $custom_format); 137 return (empty($format_error)) ? $custom_format : null; 138 } 139 140 /** 91 141 * Creates the name for the email 92 * If the option wac_fakemail_format is set , the name will be created accordingly.142 * If the option wac_fakemail_format is set and valid, the name will be created accordingly. 93 143 * 94 144 * @param string $username … … 97 147 public function create_fake_email_name($username = null) 98 148 { 99 $backend_class = new woo_add_customer_backend;100 149 $name = (!empty($username)) ? sanitize_user($username) : wp_generate_password(5, false); 101 150 102 151 //Craft the email 103 $custom_format = $this->get_wac_option('wac_fakemail_format'); 104 if (!empty($backend_class->check_option('wac_fakemail_format', $custom_format))) { 105 //Email set in the options is not valid. Return the original name 106 return $name; 107 } 108 if (!empty($custom_format)) { 152 $custom_format = $this->get_valid_fakemail_format(); 153 if ($custom_format !== null) { 109 154 //Custom format 110 155 $mail_split = explode('@', $custom_format); … … 128 173 } 129 174 130 $name = str_replace(['[', ']'], '', $custom_format_name); //Remove tags if any left 175 //Remove the placeholders that could not be filled, including their name. E.g. [last_name] 176 $name = preg_replace('/\[[^\]]*\]/', '', $custom_format_name); 177 $name = str_replace(['[', ']'], '', $name); //Remove unclosed brackets 178 //Clean up the separators left by the removed placeholders. E.g. "john..doe" => "john.doe", "john." => "john" 179 $name = preg_replace('/([._-])\1+/', '$1', $name); 180 $name = trim($name, '._-'); 131 181 } 132 182 … … 150 200 return true; 151 201 } 202 return false; 152 203 } 153 204 … … 196 247 public function wac_enqueue_admin_style() 197 248 { 198 wp_enqueue_style('wac-admin-style', get_option('siteurl') . '/wp-content/plugins/add-customer-for-woocommerce/style/admin-style.min.css', array(), $this->version); 249 wp_enqueue_style('wac-admin-style', $this->get_plugin_url('style/admin-style.min.css'), array(), $this->version); 250 } 251 252 /** 253 * Returns the URL to a file inside the plugin folder 254 * 255 * @param string $relative_path - The path of the file, relative to the plugin folder 256 * @return string The URL of the file 257 */ 258 public function get_plugin_url($relative_path) 259 { 260 return plugins_url($relative_path, $this->plugin_path . 'add-customer-for-woocommerce.php'); 199 261 } 200 262 … … 207 269 public function wac_enqueue_admin_scripts() 208 270 { 209 if (WP_DEBUG) {210 wp_enqueue_script('wac-admin-script', get_option('siteurl') . '/wp-content/plugins/add-customer-for-woocommerce/include/js/wac-main-script.js', array('jquery', 'wp-i18n'), $this->version, true);211 } else {212 wp_enqueue_script('wac-admin-script', get_option('siteurl') . '/wp-content/plugins/add-customer-for-woocommerce/include/js/wac-main-script.min.js', array('jquery', 'wp-i18n'), $this->version, true); 213 }271 //Load the unminified script, if SCRIPT_DEBUG is enabled 272 $use_unminified_script = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG; 273 $script_file = ($use_unminified_script) ? 'include/js/wac-main-script.js' : 'include/js/wac-main-script.min.js'; 274 275 wp_enqueue_script('wac-admin-script', $this->get_plugin_url($script_file), array('jquery', 'wp-i18n'), $this->version, true); 214 276 //The translations are loaded from translate.wordpress.org 215 277 wp_set_script_translations('wac-admin-script', 'add-customer-for-woocommerce'); … … 247 309 $additional_log = array(); 248 310 $type = 'null'; 311 $message_suffix = ''; //Appended after the formatting. Must not go through vsprintf, because it can contain "%" 249 312 250 313 switch ($log_type) { … … 286 349 case 'invalid_email': 287 350 /* translators: %s: Email address */ 288 $message = sprintf(__('The email "%s" is not valid.', 'add-customer-for-woocommerce'), $args[0]);351 $message = __('The email "%s" is not valid.', 'add-customer-for-woocommerce'); 289 352 $type = 'error'; 290 353 break; 291 354 case 'failed_to_add_user': 292 355 $message = __('New customer could not be added by Add Customer Plugin. Please contact the Plugin Author.', 'add-customer-for-woocommerce'); 293 $message .= (isset($args[0])) ? '<br/>' . esc_html(wp_json_encode($args[0])) : '';356 $message_suffix = (isset($args[0])) ? '<br/>' . esc_html(wp_json_encode($args[0])) : ''; 294 357 $type = 'error'; 295 358 $additional_log = array('wc_create_new_customer' => $args[0], 'user' => $args[1], 'email' => $args[2]); … … 298 361 error_log($message . " - " . htmlspecialchars(wp_json_encode($args))); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log 299 362 } 363 $args = array(); //The message has no placeholders, the args are only used for the logs 300 364 break; 301 365 case 'user_role_not_allowed': … … 335 399 $msg_trans = $message; 336 400 } 401 $msg_trans .= $message_suffix; 337 402 apply_filters('simple_history_log', "{$msg_trans} - by Add Customer", $additional_log); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Hook of the Simple History plugin 338 403 $this->wac_set_notice($msg_trans, $type, $order_id); … … 419 484 420 485 /** 421 * Gets the default user role. If no role is set, it will return "customer" 486 * Gets the default user role. Falls back to "customer" if no role is set or the role does not exist. 487 * The administrator role can only be saved as default if it was added with the wac_get_user_roles hook. 422 488 * 423 489 * @return string The user role … … 425 491 public function get_default_user_role() 426 492 { 427 $role = $this->get_wac_option('wac_default_user_role'); 428 return (empty($role)) ? 'customer' : $role; 493 $role = (string) $this->get_wac_option('wac_default_user_role'); 494 $role_exists = $role !== '' && $this->is_existing_user_role($role); 495 496 return ($role_exists) ? $role : 'customer'; 429 497 } 430 498 … … 443 511 $blog_name = html_entity_decode($blog_name, ENT_QUOTES, 'UTF-8'); 444 512 $password_reset_link = $this->get_user_reset_password_link($email); 513 if ($password_reset_link === false) { 514 return false; //No reset link available, the email would be useless without it 515 } 445 516 $message = $this->load_template_to_var('new-account', 'email/', $email, $name, $password_reset_link, $blog_name); 446 517 $from_email_option = $this->get_mail_from(); … … 564 635 565 636 $user_id = get_current_user_id(); 566 if (isset($_GET['id'])) { 637 //An explicitly requested ID always wins over the ID of the current page 638 if (!empty($id_to_get)) { 639 $order_id = $id_to_get; 640 } else if (isset($_GET['id'])) { 567 641 $order_id = absint($_GET['id']); 568 642 } else if (isset($_GET['post'])) { 569 643 $order_id = absint($_GET['post']); 570 } else if (!empty($id_to_get)) {571 $order_id = $id_to_get;572 644 } else { 573 645 return; … … 589 661 * 590 662 * @param string $user_email - The user email 591 * @return string The url to the password reset page663 * @return string|false The url to the password reset page, false if the user was not found or no reset key could be created 592 664 */ 593 665 public function get_user_reset_password_link($user_email) … … 595 667 //Create the Password reset link 596 668 $user = get_user_by('email', $user_email); 597 $user_login = $user->user_login; 669 if (!$user) { 670 return false; 671 } 672 673 //Can be a WP_Error, e.g. if password resets are disabled for this user or the key could not be saved 598 674 $reset_key = get_password_reset_key($user); 599 return network_site_url("wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode($user_login), 'login'); 675 if (is_wp_error($reset_key)) { 676 return false; 677 } 678 679 return network_site_url("wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode($user->user_login), 'login'); 600 680 } 601 681 … … 614 694 615 695 /** 696 * Decreases the add customer counter, if the deleted user was created by the plugin. 697 * This keeps the counter in sync with the users that have the wac_created_by_plugin meta (see is_customer_created_miss_match()). 698 * Runs on the delete_user hook, before the user meta gets deleted. 699 * 700 * @param int $user_id - The ID of the deleted user 701 * @return void 702 */ 703 public function wac_decrease_counter_on_user_delete($user_id) 704 { 705 $is_created_by_plugin = (bool) get_user_meta($user_id, 'wac_created_by_plugin', true); 706 if (!$is_created_by_plugin) { 707 return; 708 } 709 710 $value = (int) get_option('wac_add_customer_count'); 711 if ($value > 0) { 712 update_option('wac_add_customer_count', $value - 1); 713 } 714 } 715 716 /** 616 717 * Replaces all the email parts that are not valid 617 718 * Converts cyrillic and other non latin characters to latin 618 719 * Requires the INTL PHP extension 619 * @return void 620 */ 621 public function make_email_valid($email) 622 { 720 * @param string $email - The email to make valid 721 * @param int|null $order_id - The order the notice belongs to. Falls back to get_the_ID() 722 * @return string The sanitized email 723 */ 724 public function make_email_valid($email, $order_id = null) 725 { 726 $notice_id = ($order_id !== null) ? $order_id : get_the_ID(); 623 727 //Replaces the cyrillic letters 624 728 $orig_email = $email; … … 629 733 } elseif (!is_email($email)) { 630 734 //Display notice if email is not valid and php intl extension is not installed 631 $this->wac_set_notice(__('Intl PHP extension not installed. Please install it to validate the emails.', 'add-customer-for-woocommerce'), 'error', get_the_ID());632 } 633 634 635 $email = sanitize_email($email , true); //Remove all un-allowed characters735 $this->wac_set_notice(__('Intl PHP extension not installed. Please install it to validate the emails.', 'add-customer-for-woocommerce'), 'error', $notice_id); 736 } 737 738 739 $email = sanitize_email($email); //Remove all un-allowed characters 636 740 637 741 return apply_filters('wac_make_email_valid', $email, $orig_email); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public hook, renaming would break existing customizations … … 639 743 640 744 /** 641 * Checks if the given email is valid. Same as sanitize_email but allowing the special characters [] 642 * 643 * @param string $email 644 * @param string $fieldname 645 * @return string The sanitized email 745 * Checks if the given email format is valid. Like is_email(), but allows the placeholder characters [] in the name part. 746 * The value is not changed. If anything is invalid, an error notice is set and false is returned. 747 * 748 * @param string $email - The email format, e.g. [first_name].[last_name]@example.com 749 * @param string $fieldname - The name of the field. Used as ID for the notice 750 * @return string|false The unchanged email format, false if it is not valid 646 751 */ 647 752 public function sanitize_placeholder_email($email, $fieldname) 648 753 { 649 // Test for the minimum length the email can be. 754 $error = $this->get_placeholder_email_error($email); 755 if ($error !== '') { 756 $this->wac_set_notice(esc_html($error), "error", $fieldname); 757 return false; 758 } 759 return $email; 760 } 761 762 /** 763 * Validates the email format and returns the reason why it is not valid. 764 * 765 * @param string $email - The email format 766 * @return string The error message, empty string if the format is valid 767 */ 768 private function get_placeholder_email_error($email) 769 { 650 770 if (strlen($email) < 6) { 651 $this->wac_set_notice(esc_html__('Email is too short', 'add-customer-for-woocommerce'), "error", $fieldname); 652 return htmlspecialchars($email); 653 } 654 655 // Test for an @ character after the first position. 656 if (strpos($email, '@', 1) === false) { 657 $this->wac_set_notice(esc_html__('Email must contain one @', 'add-customer-for-woocommerce'), "error", $fieldname); 658 return htmlspecialchars($email); 659 } 660 661 // Split out the local and domain parts. 771 return __('Email is too short', 'add-customer-for-woocommerce'); 772 } 773 774 $has_one_at = substr_count($email, '@') === 1; 775 $starts_with_at = strpos($email, '@') === 0; 776 if (!$has_one_at || $starts_with_at) { 777 return __('Email must contain one @', 'add-customer-for-woocommerce'); 778 } 779 662 780 list($local, $domain) = explode('@', $email, 2); 663 781 664 /* 665 * LOCAL PART 666 * Test for invalid characters. 667 */ 668 $local = preg_replace('/[^a-zA-Z0-9!#$%&\[\]\'*+\/=?^_`{|}~\.-]/', '', $local); 669 if ('' === $local) { 670 $this->wac_set_notice(esc_html__('Email contains forbidden characters or is empty', 'add-customer-for-woocommerce'), "error", $fieldname); 671 return htmlspecialchars($email); 672 } 673 674 /* 675 * DOMAIN PART 676 * Test for sequences of periods. 677 */ 678 $domain = preg_replace('/\.{2,}/', '', $domain); 679 if ('' === $domain) { 680 $this->wac_set_notice(esc_html__('Domain parts must contain at least one dot', 'add-customer-for-woocommerce'), "error", $fieldname); 681 return htmlspecialchars($email); 682 } 683 684 // Test for leading and trailing periods and whitespace. 685 $domain = trim($domain, " \t\n\r\0\x0B."); 686 if ('' === $domain) { 687 $this->wac_set_notice(esc_html__('Domain parts are invalid', 'add-customer-for-woocommerce'), "error", $fieldname); 688 return htmlspecialchars($email); 689 } 690 691 // Split the domain into subs. 692 $subs = explode('.', $domain); 693 694 // Assume the domain will have at least two subs. 695 if (2 > count($subs)) { 696 $this->wac_set_notice(esc_html__('Domain parts are invalid', 'add-customer-for-woocommerce'), "error", $fieldname); 697 return htmlspecialchars($email); 698 } 699 700 // Create an array that will contain valid subs. 701 $new_subs = array(); 702 703 // Loop through each sub. 704 foreach ($subs as $sub) { 705 // Test for leading and trailing hyphens. 706 $sub = trim($sub, " \t\n\r\0\x0B-"); 707 708 // Test for invalid characters. 709 $sub = preg_replace('/[^a-z0-9-]+/i', '', $sub); 710 711 // If there's anything left, add it to the valid subs. 712 if ('' !== $sub) { 713 $new_subs[] = $sub; 782 //Name part: Same characters as in is_email(), plus [] for the placeholders 783 $local_is_valid = preg_match('/^[a-zA-Z0-9!#$%&\[\]\'*+\/=?^_`{|}~\.-]+$/', $local) === 1; 784 if (!$local_is_valid) { 785 return __('Email contains forbidden characters or is empty', 'add-customer-for-woocommerce'); 786 } 787 788 //Domain part: At least two parts separated by a dot. Each part only contains letters, numbers and hyphens, but does not start or end with a hyphen 789 $domain_parts = explode('.', $domain); 790 $has_enough_parts = count($domain_parts) >= 2; 791 $all_parts_valid = true; 792 foreach ($domain_parts as $domain_part) { 793 if (preg_match('/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/i', $domain_part) !== 1) { 794 $all_parts_valid = false; 795 break; 714 796 } 715 797 } 716 717 // If there aren't 2 or more valid subs. 718 if (2 > count($new_subs)) { 719 $this->wac_set_notice(esc_html__('Domain parts are invalid', 'add-customer-for-woocommerce'), "error", $fieldname); 720 return htmlspecialchars($email); 721 } 722 723 // Join valid subs into the new domain. 724 $domain = implode('.', $new_subs); 725 726 // Put the email back together. 727 $sanitized_email = $local . '@' . $domain; 728 729 // Congratulations, your email made it! 730 return $sanitized_email; 798 if (!$has_enough_parts || !$all_parts_valid) { 799 return __('Domain parts are invalid', 'add-customer-for-woocommerce'); 800 } 801 802 return ''; 731 803 } 732 804 … … 735 807 * Converts cyrillic and other non latin characters to latin 736 808 * Requires the INTL PHP extension 737 * @return void 738 */ 739 public function make_user_valid($user) 740 { 809 * @param string $user - The username to make valid 810 * @param int|null $order_id - The order the notice belongs to. Falls back to get_the_ID() 811 * @return string The sanitized username 812 */ 813 public function make_user_valid($user, $order_id = null) 814 { 815 $notice_id = ($order_id !== null) ? $order_id : get_the_ID(); 741 816 //Replaces the cyrillic letters 742 817 $orig_user = $user; … … 747 822 } elseif ($user !== sanitize_user($user, true)) { 748 823 //Display notice if user is not valid and php intl extension is not installed 749 $this->wac_set_notice(__('Intl PHP extension not installed. Please install it to validate username.', 'add-customer-for-woocommerce'), 'error', get_the_ID());824 $this->wac_set_notice(__('Intl PHP extension not installed. Please install it to validate username.', 'add-customer-for-woocommerce'), 'error', $notice_id); 750 825 } 751 826 $user = sanitize_user($user, true); //Remove all un-allowed characters 752 827 753 828 //Make sure that the username is not longer than 60 characters 754 if (strlen($user) > 60){829 if (strlen($user) > 60) { 755 830 $user = substr($user, 0, 60); 756 831 } … … 759 834 760 835 /** 761 * Returns a array with user roles. If the current user is not an admin, it will remove the admin role from the results 762 * 836 * Returns a array with user roles. It only allows the roles that the current user can edit. 837 * Users without the promote_users capability (e.g. shop managers) only get the customer role and the default role set by the admin. 838 * The customer role is always included. The administrator role is never included, not even for admins. 839 * With the filter wac_get_user_roles, you can add a custom role, or one that got previously filtered out 840 * 763 841 * @return array The roles. ['slug' => 'Name'] 764 842 */ 765 843 public function get_user_role_array() 766 844 { 767 $current_user = wp_get_current_user(); 768 $user_is_admin = (array_search('administrator', $current_user->caps) !== false) ? true : false; 769 $wp_roles = new WP_Roles(); 845 $can_promote_users = current_user_can('promote_users'); 846 847 if ($can_promote_users) { 848 $roles = $this->get_editable_role_names(); 849 } else { 850 $roles = $this->get_role_as_array($this->get_default_user_role()); 851 } 852 853 //The customer role is always available 854 $roles = $this->get_role_as_array('customer') + $roles; 855 856 //Customers should never be created as administrators 857 unset($roles['administrator']); 858 859 return apply_filters('wac_get_user_roles', $roles); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public hook, renaming would break existing customizations 860 } 861 862 /** 863 * Returns the roles the current user is allowed to edit, with translated names. 864 * 865 * @return array The roles. ['slug' => 'Name'] 866 */ 867 private function get_editable_role_names() 868 { 869 if (!function_exists('get_editable_roles')) { 870 require_once ABSPATH . 'wp-admin/includes/user.php'; 871 } 872 770 873 $roles = []; 771 foreach ($wp_roles->role_names as $key => $value) { 772 if (!$user_is_admin and $key === 'administrator') { 773 continue; 774 } 775 $roles[$key] = $value; 776 } 777 return apply_filters('wac_get_user_roles', $roles); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public hook, renaming would break existing customizations 874 foreach (get_editable_roles() as $role_slug => $role_details) { 875 $roles[$role_slug] = translate_user_role($role_details['name']); 876 } 877 return $roles; 878 } 879 880 /** 881 * Returns the given role with its translated name. 882 * 883 * @param string $role_slug - The slug of the role 884 * @return array The role. ['slug' => 'Name'] or an empty array, if the role does not exist 885 */ 886 private function get_role_as_array($role_slug) 887 { 888 $role_names = wp_roles()->get_names(); 889 890 if (!isset($role_names[$role_slug])) { 891 return []; 892 } 893 return [$role_slug => translate_user_role($role_names[$role_slug])]; 894 } 895 896 /** 897 * Checks if the given role is registered in WordPress. 898 * 899 * @param string $role_slug - The slug of the role 900 * @return bool True if the role exists, false otherwise 901 */ 902 public function is_existing_user_role($role_slug) 903 { 904 return wp_roles()->is_role($role_slug); 778 905 } 779 906 … … 787 914 { 788 915 $users_created_option = intval(get_option('wac_add_customer_count')); 789 $users_created_in_meta = count($this ->get_users_created_by_plugin());916 $users_created_in_meta = count($this->get_users_created_by_plugin()); 790 917 if ($users_created_in_meta !== $users_created_option) { 791 918 return true; //Not all users are created by the plugin … … 800 927 * @return array Empty array if no users are found, array with the user ids otherwise 801 928 */ 802 public function get_users_created_by_plugin(){ 929 public function get_users_created_by_plugin() 930 { 803 931 //Only used on the settings and users page 804 932 $args = [ -
add-customer-for-woocommerce/trunk/readme.txt
r3760001 r3795033 7 7 Tested up to: 7.1 8 8 Requires PHP: 7.4 9 Stable tag: 1.10. 09 Stable tag: 1.10.1 10 10 License: GPLv2 or later 11 11 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 92 92 93 93 == Changelog == 94 95 = [1.10.1] = 96 * Fixed: Administrator accounts could be created by shop managers. The administrator role can no longer be assigned to new customers, and users without the "promote_users" capability can only assign the default role set by the admin 97 * Changed: If the role selection is disabled, the default role set by the admin is always assigned 98 * Changed: Invalid fake email formats are rejected on save, and the previous format is kept 99 * Changed: Customer role is always available (Unless removed by filter) 100 * Fixed: Order save failed with a fatal error if no password reset link could be created 101 * Fixed: Customers were not created if the generated fake email already existed after converting special characters 102 * Fixed: Various bugfixes 94 103 95 104 = [1.10.0] = -
add-customer-for-woocommerce/trunk/templates/backend/backend-options-page-template.php
r3760001 r3795033 24 24 $user = wp_get_current_user(); 25 25 26 $message = $this->load_template_to_var('new-account', 'email/', $user->user_email, $user->display_name, $wac->get_user_reset_password_link($user->user_email), $blog_name); 26 //Dummy link for the preview. A real reset key would replace any reset link the admin requested before 27 $preview_reset_link = network_site_url('wp-login.php?action=rp&key=PREVIEW&login=' . rawurlencode($user->user_login), 'login'); 28 $message = $this->load_template_to_var('new-account', 'email/', $user->user_email, $user->display_name, $preview_reset_link, $blog_name); 27 29 //Apply the style 28 30 $woo = new WC_Email; -
add-customer-for-woocommerce/trunk/templates/order/add-checkbox.php
r3760001 r3795033 40 40 if ($set_role === 'yes') : ?> 41 41 <select name="wac_add_customer_role" id="wac_add_customer_role"> 42 <?php foreach ($wac->get_user_role_array( false) as $role_slug => $name) : ?>42 <?php foreach ($wac->get_user_role_array() as $role_slug => $name) : ?> 43 43 <?php $selected = ($role_slug === $user_role_to_set) ? 'selected' : ''; ?> 44 44 <option value="<?php echo esc_attr($role_slug); ?>" <?php echo esc_attr($selected); ?>><?php echo esc_html($name); ?></option>
Note:
See TracChangeset
for help on using the changeset viewer.