WooCommerce_Simple_Registration

Class WooCommerce_Simple_Registration.

Defined (1)

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

/woocommerce-simple-registration.php  
  1. class WooCommerce_Simple_Registration { 
  2.  
  3.  
  4. /** 
  5. * Plugin version. 
  6. * @since 1.0.0 
  7. * @var string $version Plugin version number. 
  8. */ 
  9. public $version = '1.3.0'; 
  10.  
  11.  
  12. /** 
  13. * Plugin file. 
  14. * @since 1.0.0 
  15. * @var string $file Plugin file path. 
  16. */ 
  17. public $file = __FILE__; 
  18.  
  19.  
  20. /** 
  21. * Instace of WooCommerce_Simple_Registration. 
  22. * @since 1.0.0 
  23. * @access private 
  24. * @var object $instance The instance of WooCommerce_Simple_Registration. 
  25. */ 
  26. private static $instance; 
  27.  
  28.  
  29. /** 
  30. * Construct. 
  31. * Initialize the class and plugin. 
  32. * @since 1.0.0 
  33. */ 
  34. public function __construct() { 
  35. if ( ! class_exists( 'WooCommerce' ) ) { 
  36. return; 
  37.  
  38. // Initialize plugin parts 
  39. $this->init(); 
  40.  
  41. // woocommerce_simple_registration shortcode 
  42. add_shortcode( 'woocommerce_simple_registration', array( $this, 'registration_template' ) ); 
  43.  
  44. // add a body class on this page 
  45. add_filter( 'body_class', array( $this, 'body_class' ) ); 
  46.  
  47. // add first name and last name to register form 
  48. add_action( 'woocommerce_register_form_start', array( $this, 'add_name_input' ) ); 
  49. add_action( 'woocommerce_created_customer', array( $this, 'save_name_input' ) ); 
  50.  
  51. /** 
  52. * WooCommerce Social Login Support 
  53. * @link http://www.woothemes.com/products/woocommerce-social-login/ 
  54. * @since 1.3.0 
  55. */ 
  56. if( function_exists( 'init_woocommerce_social_login' ) ) { 
  57. require_once( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'includes/wc-social-login.php' ); 
  58.  
  59. /** 
  60. * Instance. 
  61. * An global instance of the class. Used to retrieve the instance 
  62. * to use on other files/plugins/themes. 
  63. * @since 1.0.0 
  64. * @return object Instance of the class. 
  65. */ 
  66. public static function instance() { 
  67. if ( is_null( self::$instance ) ) { 
  68. self::$instance = new self(); 
  69.  
  70. return self::$instance; 
  71.  
  72. /** 
  73. * init. 
  74. * Initialize plugin parts. 
  75. * @since 1.0.0 
  76. */ 
  77. public function init() { 
  78. $this->load_textdomain(); 
  79.  
  80. /** 
  81. * Textdomain. 
  82. * Load the textdomain based on WP language. 
  83. * @since 1.0.0 
  84. */ 
  85. public function load_textdomain() { 
  86. load_plugin_textdomain( 'woocommerce-simple-registration', false, basename( dirname( __FILE__ ) ) . '/languages' ); 
  87.  
  88.  
  89. /** 
  90. * Registartion template. 
  91. * Return the registration template contents. 
  92. * @return string HTML registration form template. 
  93. */ 
  94. public function registration_template() { 
  95.  
  96. ob_start(); 
  97.  
  98. if ( ! is_user_logged_in() ) : 
  99.  
  100. $message = apply_filters( 'woocommerce_registration_message', '' ); 
  101.  
  102. if ( ! empty( $message ) ) : 
  103. wc_add_notice( $message ); 
  104. endif; 
  105.  
  106. wc_get_template( 'registration-form.php', array(), 'woocommerce-simple-registration/', plugin_dir_path( __FILE__ ) . 'templates/' ); 
  107.  
  108. else : 
  109. echo do_shortcode( '[woocommerce_my_account]' ); 
  110. endif; 
  111.  
  112. $return = ob_get_contents(); 
  113. ob_end_clean(); 
  114.  
  115. return $return; 
  116.  
  117.  
  118. /** 
  119. * Add body classes for WC Simple Register page. 
  120. * @since 1.2.0 
  121. * @param array $classes 
  122. * @return array 
  123. */ 
  124. public function body_class( $classes ) { 
  125. if( is_singular() && $post_data = get_post( get_queried_object_id() ) ) { 
  126. if ( isset( $post_data->post_content ) && has_shortcode( $post_data->post_content, 'woocommerce_simple_registration' ) ) { 
  127. $classes[] = 'woocommerce-register'; 
  128. $classes[] = 'woocommerce-account'; 
  129. $classes[] = 'woocommerce-page'; 
  130. return $classes; 
  131.  
  132. /** 
  133. * Add First Name & Last Name 
  134. * To disable this simply use this code: 
  135. * `add_filter( 'woocommerce_simple_registration_name_fields', '__return_false' );` 
  136. * @since 1.3.0 
  137. */ 
  138. public function add_name_input() { 
  139. /** Filter to disable this feature. */ 
  140. if( ! apply_filters( 'woocommerce_simple_registration_name_fields', true ) ) { 
  141. return; 
  142. ?> 
  143. <p class="woocommerce-FormRow woocommerce-FormRow--first form-row form-row-first"> 
  144. <label for="reg_sr_firstname"><?php _e( 'First Name', 'woocommerce-simple-registration' ); ?></label> 
  145. <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="sr_firstname" id="reg_sr_firstname" value="<?php if ( ! empty( $_POST['sr_firstname'] ) ) echo esc_attr( $_POST['sr_firstname'] ); ?>" /> 
  146. </p> 
  147.  
  148. <p class="woocommerce-FormRow woocommerce-FormRow--last form-row form-row-last"> 
  149. <label for="reg_sr_lastname"><?php _e( 'Last Name', 'woocommerce-simple-registration' ); ?></label> 
  150. <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="sr_lastname" id="reg_sr_lastname" value="<?php if ( ! empty( $_POST['sr_lastname'] ) ) echo esc_attr( $_POST['sr_lastname'] ); ?>" /> 
  151. </p> 
  152. <?php 
  153.  
  154. /** 
  155. * Save First Name and Last Name 
  156. * @since 1.3.0 
  157. * @see WC/includes/wc-user-functions.php line 114 
  158. */ 
  159. public function save_name_input( $customer_id ) { 
  160. /** Filter to disable this feature. */ 
  161. if( ! apply_filters( 'woocommerce_simple_registration_name_fields', true ) ) { 
  162. return; 
  163.  
  164. /** Strip slash everything */ 
  165. $request = stripslashes_deep( $_POST ); 
  166.  
  167. /** Save First Name */ 
  168. if ( isset( $request['sr_firstname'] ) && !empty( $request['sr_firstname'] ) ) { 
  169. update_user_meta( $customer_id, 'first_name', sanitize_text_field( $request['sr_firstname'] ) ); 
  170. /** Save Last Name */ 
  171. if ( isset( $request['sr_lastname'] ) && !empty( $request['sr_lastname'] ) ) { 
  172. update_user_meta( $customer_id, 'last_name', sanitize_text_field( $request['sr_lastname'] ) ); 
  173.