WC_GZD_Customer_Helper

The WooCommerce Germanized WC GZD Customer Helper class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-gzd-customer-helper.php  
  1. class WC_GZD_Customer_Helper { 
  2.  
  3. protected static $_instance = null; 
  4.  
  5. public static function instance() { 
  6. if ( is_null( self::$_instance ) ) 
  7. self::$_instance = new self(); 
  8. return self::$_instance; 
  9.  
  10. /** 
  11. * Cloning is forbidden. 
  12. * @since 1.0 
  13. */ 
  14. public function __clone() { 
  15. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  16.  
  17. /** 
  18. * Unserializing instances of this class is forbidden. 
  19. * @since 1.0 
  20. */ 
  21. public function __wakeup() { 
  22. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  23.  
  24. public function __construct() { 
  25.  
  26. // Customer Account checkbox 
  27. add_action( 'template_redirect', array( $this, 'init_gettext_replacement' ) ); 
  28. // Send customer account notification 
  29. add_action( 'woocommerce_email', array( $this, 'email_hooks' ), 0, 1 ); 
  30. // Add Title to user profile 
  31. add_filter( 'woocommerce_customer_meta_fields', array( $this, 'profile_field_title' ), 10, 1 );  
  32.  
  33. if ( $this->is_double_opt_in_enabled() ) { 
  34.  
  35. // Check for customer activation 
  36. add_action( 'template_redirect', array( $this, 'customer_account_activation_check' ) ); 
  37. // Cronjob to delete unactivated users 
  38. add_action( 'woocommerce_gzd_customer_cleanup', array( $this, 'account_cleanup' ) ); 
  39.  
  40. add_action( 'woocommerce_created_customer', array( $this, 'set_customer_activation_meta' ), 10, 3 ); 
  41.  
  42. if ( $this->is_double_opt_in_login_enabled() ) { 
  43. // Disable login for unactivated users 
  44. add_filter( 'wp_authenticate_user', array( $this, 'login_restriction' ) , 10, 2 ); 
  45. // Disable auto login after registration 
  46. add_filter( 'woocommerce_registration_auth_new_customer', array( $this, 'disable_registration_auto_login' ), 10, 2 );  
  47. // Redirect customers that are not logged in to customer account page 
  48. add_action( 'template_redirect', array( $this, 'disable_checkout' ), 10 ); 
  49. // Show notices on customer account page 
  50. add_action( 'template_redirect', array( $this, 'show_disabled_checkout_notice' ), 20 ); 
  51. // Redirect customers to checkout after login 
  52. add_filter( 'woocommerce_login_redirect', array( $this, 'login_redirect' ), 10, 2 ); 
  53. // Disable customer signup if customer has forced guest checkout 
  54. add_action( 'woocommerce_checkout_init', array( $this, 'disable_signup' ), 10, 1 ); 
  55. // Remove the checkout signup cookie if customer logs out 
  56. add_action( 'wp_logout', array( $this, 'delete_checkout_signup_cookie' ) ); 
  57. // WC Social Login comp 
  58. add_filter( 'wc_social_login_set_auth_cookie', array( $this, 'social_login_activation_check' ), 10, 2 ); 
  59.  
  60.  
  61.  
  62. public function social_login_activation_check( $message, $user ) { 
  63. $user_id = $user->ID; 
  64. if ( ! wc_gzd_is_customer_activated( $user_id ) ) { 
  65. return __( 'Please activate your account through clicking on the activation link received via email.', 'woocommerce-germanized' ); 
  66. return $message; 
  67.  
  68. public function profile_field_title( $fields ) { 
  69.  
  70. if ( get_option( 'woocommerce_gzd_checkout_address_field' ) !== 'yes' ) 
  71. return $fields; 
  72.  
  73. $fields[ 'billing' ][ 'fields' ][ 'billing_title' ] = array( 
  74. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  75. 'type' => 'select',  
  76. 'options' => apply_filters( 'woocommerce_gzd_title_options', array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ) ),  
  77. 'description' => '' 
  78. ); 
  79.  
  80. $fields[ 'shipping' ][ 'fields' ][ 'shipping_title' ] = array( 
  81. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  82. 'type' => 'select',  
  83. 'options' => apply_filters( 'woocommerce_gzd_title_options', array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ) ),  
  84. 'description' => '' 
  85. ); 
  86.  
  87. return $fields; 
  88.  
  89. public function is_double_opt_in_enabled() { 
  90. return get_option( 'woocommerce_gzd_customer_activation' ) === 'yes'; 
  91.  
  92. public function is_double_opt_in_login_enabled() { 
  93. return get_option( 'woocommerce_gzd_customer_activation_login_disabled' ) === 'yes'; 
  94.  
  95. public function delete_checkout_signup_cookie() { 
  96. unset( WC()->session->disable_checkout_signup ); 
  97. unset( WC()->session->login_redirect ); 
  98.  
  99. public function disable_signup( $checkout ) { 
  100.  
  101. if ( WC()->session->get( 'disable_checkout_signup' ) ) 
  102. $checkout->enable_signup = false; 
  103.  
  104.  
  105. public function login_redirect( $redirect, $user ) { 
  106.  
  107. if ( WC()->session->get( 'login_redirect' ) && 'checkout' === WC()->session->get( 'login_redirect' ) ) { 
  108. return wc_gzd_get_page_permalink( 'checkout' ); 
  109.  
  110. return $redirect; 
  111.  
  112.  
  113. public function disable_checkout() { 
  114.  
  115. $user_id = get_current_user_id(); 
  116.  
  117. if ( is_cart() ) { 
  118.  
  119. // On accessing cart - reset disable checkout signup so that the customer is rechecked before redirecting him to the checkout. 
  120. unset( WC()->session->disable_checkout_signup ); 
  121.  
  122.  
  123. if ( get_option( 'woocommerce_enable_guest_checkout' ) === 'yes' && isset( $_GET[ 'force-guest' ] ) ) { 
  124.  
  125. // Disable registration 
  126. WC()->session->set( 'disable_checkout_signup', true ); 
  127.  
  128. } elseif ( ! WC()->session->get( 'disable_checkout_signup' ) ) { 
  129.  
  130. if ( is_checkout() && ( ! is_user_logged_in() || ! wc_gzd_is_customer_activated() ) ) { 
  131.  
  132. WC()->session->set( 'login_redirect', 'checkout' ); 
  133. wp_safe_redirect( wc_gzd_get_page_permalink( 'myaccount' ) ); 
  134. exit; 
  135.  
  136. } elseif ( is_checkout() ) { 
  137.  
  138. unset( WC()->session->login_redirect ); 
  139.  
  140.  
  141. public function show_disabled_checkout_notice() { 
  142.  
  143. if ( ! is_user_logged_in() && isset( $_GET[ 'account' ] ) && 'activate' === $_GET[ 'account' ] ) { 
  144. wc_add_notice( __( 'Please activate your account through clicking on the activation link received via email.', 'woocommerce-germanized' ), 'notice' ); 
  145. return; 
  146.  
  147. if ( is_account_page() && WC()->session->get( 'login_redirect' ) ) { 
  148.  
  149. if ( ! is_user_logged_in() ) { 
  150.  
  151. if ( get_option( 'woocommerce_enable_guest_checkout' ) === 'yes' ) { 
  152. wc_add_notice( sprintf( __( 'Continue without creating an account? <a href="%s">Click here</a>', 'woocommerce-germanized' ), add_query_arg( array( 'force-guest' => 'yes' ), wc_gzd_get_page_permalink( 'checkout' ) ) ), 'notice' ); 
  153. } else { 
  154. wc_add_notice( __( 'Please create an account or login before continuing to checkout', 'woocommerce-germanized' ), 'notice' ); 
  155.  
  156. } else { 
  157.  
  158. // Redirect to checkout 
  159. unset( WC()->session->login_redirect ); 
  160. wp_safe_redirect( wc_gzd_get_page_permalink( 'checkout' ) ); 
  161. exit; 
  162.  
  163.  
  164. public function registration_redirect( $redirect ) { 
  165. return add_query_arg( array( 'account' => 'activate' ), wc_gzd_get_page_permalink( 'myaccount' ) ); 
  166.  
  167. public function disable_registration_auto_login( $result, $user_id ) { 
  168.  
  169. if ( is_a( $user_id, 'WP_User' ) ) { 
  170. $user_id = $user_id->ID; 
  171.  
  172. // Has not been activated yet 
  173. if ( ! wc_gzd_is_customer_activated( $user_id ) ) { 
  174. add_filter( 'woocommerce_registration_redirect', array( $this, 'registration_redirect' ) ); 
  175. return false; 
  176.  
  177. return true; 
  178.  
  179.  
  180. public function login_restriction( $user, $password ) { 
  181.  
  182. // Has not been activated yet 
  183. if ( ! wc_gzd_is_customer_activated( $user->ID ) ) 
  184. return new WP_Error( 'woocommerce_gzd_login', __( 'Please activate your account through clicking on the activation link received via email.', 'woocommerce-germanized' ) ); 
  185.  
  186. return $user; 
  187.  
  188.  
  189. /** 
  190. * Check for activation codes on my account page 
  191. */ 
  192. public function customer_account_activation_check() { 
  193.  
  194. if ( is_account_page() ) { 
  195.  
  196. if ( isset( $_GET[ 'activate' ] ) ) { 
  197.  
  198. $activation_code = sanitize_text_field( $_GET[ 'activate' ] ); 
  199.  
  200. if ( ! empty( $activation_code ) ) { 
  201.  
  202. if ( $this->customer_account_activate( $activation_code, true ) ) { 
  203.  
  204. wc_add_notice( __( 'Thank you. You have successfully activated your account.', 'woocommerce-germanized' ), 'notice' ); 
  205. return; 
  206.  
  207. wc_add_notice( __( 'Sorry, but this activation code cannot be found.', 'woocommerce-germanized' ), 'error' ); 
  208.  
  209. /** 
  210. * Check for customer that didn't activate their accounts within a couple of time and delete them 
  211. */ 
  212. public function account_cleanup() { 
  213.  
  214. if ( ! get_option( 'woocommerce_gzd_customer_cleanup_interval' ) || get_option( 'woocommerce_gzd_customer_cleanup_interval' ) == 0 ) 
  215. return; 
  216.  
  217. $user_query = new WP_User_Query( 
  218. array( 'role' => 'Customer', 'meta_query' => 
  219. array( 
  220. array( 
  221. 'key' => '_woocommerce_activation',  
  222. 'compare' => 'EXISTS',  
  223. ),  
  224. ),  
  225. ); 
  226.  
  227. if ( ! empty( $user_query->results ) ) { 
  228.  
  229. foreach ( $user_query->results as $user ) { 
  230.  
  231. // Check date interval 
  232. $registered = $user->data->user_registered; 
  233. $date_diff = WC_germanized()->get_date_diff( $registered, date( 'Y-m-d' ) ); 
  234. if ( $date_diff[ 'd' ] >= (int) get_option( 'woocommerce_gzd_customer_cleanup_interval' ) ) { 
  235. require_once( ABSPATH . 'wp-admin/includes/user.php' ); 
  236. wp_delete_user( $user->ID ); 
  237.  
  238.  
  239. /** 
  240. * Activate customer account based on activation code 
  241. *  
  242. * @param string $activation_code hashed activation code 
  243. * @return boolean  
  244. */ 
  245. public function customer_account_activate( $activation_code, $login = false ) { 
  246.  
  247. $user_query = new WP_User_Query( apply_filters( 'woocommerce_gzd_customer_account_activation_query', array(  
  248. 'role' => 'Customer',  
  249. 'number' => 1,  
  250. 'meta_query' => array( 
  251. array( 
  252. 'key' => '_woocommerce_activation',  
  253. 'value' => $activation_code,  
  254. 'compare' => '=',  
  255. ),  
  256. ),  
  257. ), $activation_code, $login ) ); 
  258.  
  259. if ( ! empty( $user_query->results ) ) { 
  260.  
  261. foreach ( $user_query->results as $user ) { 
  262.  
  263. do_action( 'woocommerce_gzd_customer_opted_in', $user ); 
  264. delete_user_meta( $user->ID, '_woocommerce_activation' ); 
  265. WC()->mailer()->customer_new_account( $user->ID ); 
  266.  
  267. if ( $login && ! is_user_logged_in() ) 
  268. wc_set_customer_auth_cookie( $user->ID ); 
  269.  
  270. return true; 
  271.  
  272.  
  273. return false; 
  274.  
  275. public function init_gettext_replacement() { 
  276. if ( is_checkout() && get_option( 'woocommerce_gzd_customer_account_checkout_checkbox' ) == 'yes' ) 
  277. add_filter( 'gettext', array( $this, 'set_customer_account_checkbox_text' ), 10, 3 ); 
  278.  
  279. public function set_customer_account_checkbox_text( $translated, $original, $domain ) { 
  280. $search = "Create an account?"; 
  281. if ( $domain === 'woocommerce' && $original === $search ) { 
  282. remove_filter( 'gettext', array( $this, 'set_customer_account_checkbox_text' ), 10, 3 ); 
  283. return wc_gzd_get_legal_text( get_option( 'woocommerce_gzd_customer_account_text' ) ); 
  284. return $translated; 
  285.  
  286. public function email_hooks( $mailer ) { 
  287.  
  288. // Add new customer activation 
  289. if ( get_option( 'woocommerce_gzd_customer_activation' ) == 'yes' ) { 
  290.  
  291. remove_action( 'woocommerce_created_customer_notification', array( $mailer, 'customer_new_account' ), 10 ); 
  292. add_action( 'woocommerce_created_customer_notification', array( $this, 'customer_new_account_activation' ), 9, 3 ); 
  293.  
  294.  
  295.  
  296. /** 
  297. * Customer new account activation email. 
  298. * @param int $customer_id 
  299. * @param array $new_customer_data 
  300. */ 
  301. public function customer_new_account_activation( $customer_id, $new_customer_data = array(), $password_generated = false ) { 
  302.  
  303. if ( ! $customer_id ) 
  304. return; 
  305.  
  306. if ( ! wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  307. $this->set_customer_activation_meta( $customer_id, $new_customer_data, $password_generated ); 
  308.  
  309. $user_pass = ! empty( $new_customer_data['user_pass'] ) ? $new_customer_data['user_pass'] : ''; 
  310.  
  311. $user_activation = get_user_meta( $customer_id, '_woocommerce_activation', true ); 
  312. $user_activation_url = apply_filters( 'woocommerce_gzd_customer_activation_url', add_query_arg( array( 'activate' => $user_activation ), wc_gzd_get_page_permalink( 'myaccount' ) ) ); 
  313.  
  314. if ( $email = WC_germanized()->emails->get_email_instance_by_id( 'customer_new_account_activation' ) ) 
  315. $email->trigger( $customer_id, $user_activation, $user_activation_url, $user_pass, $password_generated ); 
  316.  
  317.  
  318. public function set_customer_activation_meta( $customer_id, $new_customer_data, $password_generated ) { 
  319.  
  320. global $wp_hasher; 
  321.  
  322. if ( ! $customer_id ) 
  323. return; 
  324.  
  325. if ( empty( $wp_hasher ) ) { 
  326. require_once ABSPATH . WPINC . '/class-phpass.php'; 
  327. $wp_hasher = new PasswordHash( 8, true ); 
  328.  
  329. $user_activation = $wp_hasher->HashPassword( wp_generate_password( 20 ) ); 
  330.  
  331. add_user_meta( $customer_id, '_woocommerce_activation', $user_activation ); 
  332.