Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.29 KB | None | 0 0
  1. <?php
  2. class Birthday_Discount_Vouchers_Lite {
  3. public function __construct() {
  4.  
  5. //Check if woocommerce plugin is installed.
  6. add_action( 'admin_notices', array( $this, 'check_required_plugins' ) );
  7.  
  8. if( is_admin() && isset($_GET['page']) && $_GET['page'] == 'birthday-calendar' ) {
  9. add_action( 'admin_init', array($this, 'wbdv_get_users_birthday'));
  10. add_action( 'admin_footer', array( $this, 'wbdv_add_scripts') );
  11. }
  12.  
  13. //Add setting link for the admin settings
  14. add_filter( "plugin_action_links_".BDV_BASE, array( $this, 'bdv_settings_link' ) );
  15.  
  16. //Add backend settings
  17. add_filter( 'woocommerce_get_settings_pages', array( $this, 'bdv_settings_class' ) );
  18.  
  19. //Add custom field to the registration page
  20. if( get_option('wbdv_enabled') == 'yes' ) {
  21. if( get_option('woocommerce_enable_myaccount_registration') == 'yes' ) {
  22. add_action( 'woocommerce_register_form', array($this, 'wpdv_add_birthday_field') );
  23. add_action( 'woocommerce_register_post', array($this, 'wbdv_validate_date_of_birth'), 10, 3 );
  24. add_action('woocommerce_created_customer', array($this, 'wpdv_save_birthday_field'));
  25. }
  26.  
  27. if( get_option('woocommerce_enable_signup_and_login_from_checkout') == 'yes' ) {
  28. add_filter( 'woocommerce_checkout_fields', array($this, 'wpdv_filter_checkout_fields') );
  29. add_action( 'woocommerce_checkout_order_processed', array($this, 'wpdv_save_birthday_checkout_field'), 10, 2 );
  30. }
  31. }
  32.  
  33. add_action( 'wp_ajax_wbdv_ajax_products', array( $this, 'wbdv_ajax_products' ) );
  34.  
  35. add_action( 'wp_enqueue_scripts', array( $this, 'wpdv_enque_scripts' ) );
  36.  
  37. add_action('init', array( $this, 'send_birthday_emails_to_users') );
  38.  
  39. add_action('wbdv_birthday_cron', array($this, 'wbdv_run_birthday_cron') );
  40.  
  41. add_action( 'admin_enqueue_scripts', array( $this, 'wbdv_enque_admin_scripts' ) );
  42.  
  43. register_deactivation_hook( __FILE__, array( $this, 'wbdv_deactivate_emails' ) );
  44.  
  45. add_action( 'show_user_profile', array($this, 'wbdv_user_birth_day_field') );
  46.  
  47. add_action( 'edit_user_profile', array($this, 'wbdv_user_birth_day_field') );
  48.  
  49. add_action( 'personal_options_update', array($this, 'wbdv_save_user_birthday_profile_fields' ));
  50. add_action( 'edit_user_profile_update', array($this, 'wbdv_save_user_birthday_profile_fields' ));
  51.  
  52. add_action( 'woocommerce_edit_account_form', array($this, 'wbdv_edit_account_form'));
  53.  
  54. add_action( 'woocommerce_save_account_details', array($this, 'wbdv_save_account_details'), 10, 1 );
  55. }
  56.  
  57.  
  58. public function wbdv_save_account_details($user_id) {
  59. $birthday_method = get_option('wbdv_method');
  60.  
  61. if( $birthday_method == 'datepicker' && !empty($_POST['wbdv_date_confirm_submit']) ) {
  62. $birthday = sanitize_text_field($_POST['wbdv_date_confirm_submit']);
  63. }
  64.  
  65. if( $birthday_method == 'dropdown' && !empty($_POST['birthdate']) ) {
  66. $birthday = sanitize_text_field($_POST['birthdate']);
  67. }
  68.  
  69. if( $birthday_method == 'jqdatepicker' && !empty($_POST['wbdv_date_confirm']) ) {
  70. $birthday = sanitize_text_field($_POST['wbdv_date_confirm']);
  71. }
  72.  
  73. if( !empty($birthday) ) {
  74. $birthday_var = explode('-', $birthday);
  75. $birthday_var = $birthday_var[1].'-'.$birthday_var[2];
  76. if( !empty($user_id) ) {
  77. update_user_meta($user_id, 'wpdv_birthday', $birthday_var);
  78. update_user_meta($user_id, 'wpdv_birthday_calender', $birthday);
  79. }
  80. }
  81.  
  82. }
  83.  
  84.  
  85. public function wbdv_enque_admin_scripts() {
  86. if( is_admin() && isset($_GET['tab']) && $_GET['tab'] == 'birthday_discount_vouchers' ) {
  87. wp_enqueue_style( 'select2-style', plugins_url( 'assets/css/select2.css', BDV_FILE ) );
  88. wp_enqueue_script( 'wcsd-enhanced-select', plugins_url( 'assets/js/select2.min.js', BDV_FILE ) , array( 'jquery' ), '1.0.0', true );
  89. }
  90.  
  91. wp_localize_script( 'wbdv-enhanced-select', 'wbdv_enhanced_select_params', array(
  92. 'i18n_matches_1' => _x( 'One result is available, press enter to select it.', 'enhanced select', 'woocommerce' ),
  93. 'i18n_matches_n' => _x( '%qty% results are available, use up and down arrow keys to navigate.', 'enhanced select', 'woocommerce' ),
  94. 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'woocommerce' ),
  95. 'i18n_ajax_error' => _x( 'Loading failed', 'enhanced select', 'woocommerce' ),
  96. 'i18n_input_too_short_1' => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce' ),
  97. 'i18n_input_too_short_n' => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce' ),
  98. 'i18n_input_too_long_1' => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce' ),
  99. 'i18n_input_too_long_n' => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce' ),
  100. 'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce' ),
  101. 'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce' ),
  102. 'i18n_load_more' => _x( 'Loading more results&hellip;', 'enhanced select', 'woocommerce' ),
  103. 'i18n_searching' => _x( 'Searching&hellip;', 'enhanced select', 'woocommerce' ),
  104. ) );
  105.  
  106. wp_register_style( 'jquery-ui', plugins_url( 'assets/css/jquery-ui.css', BDV_FILE ) );
  107.  
  108. wp_enqueue_style( 'jquery-ui' );
  109.  
  110. wp_enqueue_script('wbdv-admin-script', plugins_url( 'assets/js/wbdv-admin-profile.js', BDV_FILE ), array( 'jquery' ), '1.0.0', true );
  111.  
  112. wp_enqueue_script( 'jquery-ui-datepicker' );
  113. }
  114.  
  115. /**
  116. *
  117. * Check if woocommerce is installed and activated and if not
  118. *
  119. */
  120. public function check_required_plugins() {
  121. if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) { ?>
  122. <div id="message" class="error">
  123. <p><?php echo BDV_PLUGIN_NAME; ?> requires <a href="http://www.woothemes.com/woocommerce/" target="_blank">WooCommerce</a> to be activated in order to work. Please install and activate <a href="<?php echo admin_url('/plugin-install.php?tab=search&amp;type=term&amp;s=WooCommerce'); ?>" target="">WooCommerce</a> first.</p>
  124. </div>
  125.  
  126. <?php
  127. deactivate_plugins( '/birthday-discount-vouchers/birthday-discount-vouchers.php' );
  128. }
  129. }
  130.  
  131. /**
  132. * Add new link for the settings under plugin links
  133. *
  134. * @param array $links an array of existing links.
  135. * @return array of links along with birthday discount vouchers settings link.
  136. *
  137. */
  138. public function bdv_settings_link($links) {
  139. $settings_link = '<a href="'.admin_url('admin.php?page=wc-settings&tab=birthday_discount_vouchers').'">Settings</a>';
  140. array_unshift( $links, $settings_link );
  141. return $links;
  142. }
  143.  
  144. public function wbdv_get_users_birthday() {
  145. if ( get_option('wbdv_enabled') == 'yes' ) {
  146. $this->get_users_birthdays();
  147. }
  148. }
  149.  
  150. public function get_users_birthdays() {
  151. $args = array(
  152. 'meta_key' => 'wpdv_birthday_calender',
  153. );
  154. $birthday_users = get_users($args);
  155. $user_array = array();
  156. if( is_array($birthday_users) ) {
  157. $data = array();
  158. foreach( $birthday_users as $k => $birthday_user ) {
  159. $user_id = $birthday_user->data->ID;
  160. $user_name = get_user_meta($user_id, 'first_name', true );
  161. if( empty($user_name) ) {
  162. $user_obj = get_user_by('id', $user_id);
  163. $user_name = $user_obj->user_login;
  164. }
  165.  
  166. $birthday = get_user_meta($user_id, 'wpdv_birthday_calender', true );
  167.  
  168. if( !empty($birthday) ) {
  169. $data[$k]['title'] = $user_name;
  170. $data[$k]['start'] = $birthday;
  171. $data[$k]['end'] = $birthday;
  172. }
  173. }
  174. }
  175. return json_encode($data);
  176. }
  177.  
  178. public function wbdv_edit_account_form() {
  179. ?>
  180. <fieldset>
  181. <legend><?php echo __('Personal information'); ?></legend>
  182. <?php $this->wpdv_add_birthday_in_account(); ?>
  183.  
  184. </fieldset>
  185. <?php
  186. }
  187.  
  188. public function wbdv_add_scripts() {
  189. wp_enqueue_style( 'wbdv-calender-stylesheet', plugins_url( 'assets/css/fullcalendar.min.css', BDV_FILE ));
  190. wp_enqueue_script('moment', plugins_url( 'assets/js/moment.min.js', BDV_FILE ));
  191. wp_enqueue_script('fullcaljs', plugins_url( 'assets/js/fullcalendar.min.js', BDV_FILE ));
  192. wp_enqueue_style( 'wbdv-custom-style', plugins_url( 'assets/css/wbdv-admin.css', BDV_FILE ));
  193.  
  194. ?>
  195.  
  196. <script type="text/javascript">
  197. jQuery(document).ready(function() {
  198. jQuery('#wbdv-calendar').fullCalendar({
  199. events: <?php echo $this->get_users_birthdays(); ?>
  200. });
  201. });
  202. </script>
  203. <?php
  204. }
  205.  
  206. public function wbdv_run_birthday_cron() {
  207. if( get_option('wbdv_enabled') == 'yes' ) {
  208. $this->wbdv_check_user_birthday();
  209. }
  210. }
  211.  
  212. public function wbdv_check_user_birthday() {
  213.  
  214. $coupon_send_times = get_option('wbdv_coupon_number_of_years');
  215. $coupon_send_times = intval($coupon_send_times);
  216.  
  217. global $woocommerce;
  218.  
  219. $today = date("Y-m-d");
  220. $store_name = get_bloginfo('name');
  221. $store_url = get_bloginfo('url');
  222. $mail_before_days = get_option('wbdv_days_before');
  223.  
  224. if( $mail_before_days == 0 || empty($mail_before_days) )
  225. $mail_before_days = 0;
  226.  
  227. $args = array(
  228. 'meta_key' => 'wpdv_birthday',
  229. );
  230.  
  231. $birthday_users = get_users($args);
  232.  
  233. if( is_array($birthday_users) && !empty($birthday_users) ) {
  234. foreach( $birthday_users as $birthday_user ) {
  235. $get_user_birthday = get_user_meta($birthday_user->data->ID, 'wpdv_birthday', true );
  236.  
  237.  
  238. if( !empty($get_user_birthday) ) {
  239. $new_birthday_year = date('Y').'-'.$get_user_birthday;
  240.  
  241.  
  242. $date = date('Y-m-d', strtotime($new_birthday_year . " -".$mail_before_days." days"));
  243.  
  244.  
  245. if( $today == $date ) {
  246. $user_info = get_userdata($birthday_user->data->ID);
  247.  
  248. //check user is eligible for send birthday this time
  249. $check_coupon_year = get_user_meta($birthday_user->data->ID, 'coupon_year', true );
  250.  
  251. if( empty($check_coupon_year) || $check_coupon_year == '' ) {
  252. $check_coupon_year = 0;
  253. }
  254. $check_coupon_year = intval($check_coupon_year);
  255.  
  256. if( $coupon_send_times == 0 || $check_coupon_year <= $coupon_send_times ) {
  257. $user_email = $birthday_user->data->user_email;
  258. $first_name = !empty($birthday_user->data->first_name) ? $birthday_user->data->first_name : '' ;
  259. $last_name = !empty($birthday_user->data->last_name) ? $birthday_user->data->last_name : '';
  260.  
  261. $username = $birthday_user->data->user_login;
  262.  
  263. if( !empty($user_email) ) {
  264. //create coupon based on settings
  265. $code_length = get_option( 'wbdv_code_length' );
  266. if( $code_length == '' )
  267. $code_length = 12;
  268.  
  269. $prefix = get_option( 'wbdv_prefix' );
  270. $code = $prefix . strtoupper( substr( str_shuffle( md5( time() ) ), 0, $code_length ) );
  271. $type = get_option( 'wbdv_dis_type' );
  272. $amount = get_option( 'wbdv_amount' );
  273. $product_ids = get_option( 'wbdv_products' );
  274. $allowed_products = '';
  275. $excluded_products = '';
  276.  
  277. if ( is_array( $product_ids ) ) {
  278. foreach ( $product_ids as $product_id ) {
  279. $product = wc_get_product( $product_id );
  280. $allowed_products .= '<a href="'.$product->get_permalink().'">'.$product->get_title().'</a>,';
  281. }
  282. $allowed_products = rtrim( $allowed_products, ',' );
  283. $product_ids = implode( ',', $product_ids );
  284. }
  285.  
  286. $exclude_product_ids = get_option( 'wbdv_exclude_products' );
  287. if ( is_array( $exclude_product_ids ) ) {
  288. foreach ( $exclude_product_ids as $product_id ) {
  289. $product = wc_get_product( $product_id );
  290. $excluded_products .= '<a href="'.$product->get_permalink().'">'.$product->get_title().'</a>,';
  291. }
  292. $excluded_products = rtrim( $excluded_products, ',' );
  293. $exclude_product_ids = implode( ',', $exclude_product_ids );
  294. }
  295.  
  296. $product_categories = get_option( 'wbdv_categories' );
  297. $allowed_cats = '';
  298. $excluded_cats = '';
  299. if ( is_array( $product_categories ) ) {
  300. foreach ( $product_categories as $cat_id ) {
  301. $cat = get_term_by( 'id', $cat_id, 'product_cat' );
  302. $allowed_cats .= '<a href="'.get_term_link( $cat->slug, 'product_cat' ).'">'.$cat->name.'</a>,';
  303. }
  304. $allowed_cats = rtrim( $allowed_cats, ',' );
  305. }
  306. else
  307. $product_categories = array();
  308.  
  309. $exclude_product_categories = get_option( 'wbdv_exclude_categories' );
  310. if ( is_array( $exclude_product_categories ) ) {
  311. foreach ( $exclude_product_categories as $cat_id ) {
  312. $cat = get_term_by( 'id', $cat_id, 'product_cat' );
  313. $excluded_cats .= '<a href="'.get_term_link( $cat->slug, 'product_cat' ).'">'.$cat->name.'</a>,';
  314. }
  315. $excluded_cats = rtrim( $excluded_cats, ',' );
  316. }
  317. else
  318. $exclude_product_categories = array();
  319.  
  320. $days = get_option( 'wbdv_days' );
  321. $date = '';
  322. $expire = '';
  323. $format = get_option( 'wbdv_date_format' ) == '' ? 'jS F Y' : get_option( 'wbdv_date_format' );
  324.  
  325. if ( $days ) {
  326. $date = date( 'Y-m-d', strtotime( '+'.$days.' days' ) );
  327. $expire = date_i18n( $format, strtotime( '+'.$days.' days' ) );
  328. }
  329.  
  330. $free_shipping = get_option( 'wbdv_shipping' );
  331. $exclude_sale_items = get_option( 'wbdv_sale' );
  332. $minimum_amount = get_option( 'wbdv_min_purchase' );
  333. $maximum_amount = get_option( 'wbdv_max_purchase' );
  334. $customer_email = '';
  335.  
  336. if ( get_option( 'wbdv_restrict' ) == 'yes' )
  337. $customer_email = $user_email;
  338.  
  339. //Add a new coupon when user registers
  340. $coupon = array(
  341. 'post_title' => $code,
  342. 'post_content' => '',
  343. 'post_status' => 'publish',
  344. 'post_author' => 1,
  345. 'post_type' => 'shop_coupon'
  346. );
  347. $coupon_id = wp_insert_post( $coupon );
  348.  
  349. //Add coupon meta data
  350. update_post_meta( $coupon_id, 'discount_type', $type );
  351. update_post_meta( $coupon_id, 'coupon_amount', $amount );
  352. update_post_meta( $coupon_id, 'individual_use', 'yes' );
  353. update_post_meta( $coupon_id, 'product_ids', $product_ids );
  354. update_post_meta( $coupon_id, 'exclude_product_ids', $exclude_product_ids );
  355. update_post_meta( $coupon_id, 'usage_limit', '1' );
  356. update_post_meta( $coupon_id, 'usage_limit_per_user', '1' );
  357. update_post_meta( $coupon_id, 'limit_usage_to_x_items', '' );
  358. update_post_meta( $coupon_id, 'expiry_date', $date );
  359. update_post_meta( $coupon_id, 'apply_before_tax', 'no' );
  360. update_post_meta( $coupon_id, 'free_shipping', $free_shipping );
  361. update_post_meta( $coupon_id, 'exclude_sale_items', $exclude_sale_items );
  362. update_post_meta( $coupon_id, 'product_categories', $product_categories );
  363. update_post_meta( $coupon_id, 'exclude_product_categories', $exclude_product_categories );
  364. update_post_meta( $coupon_id, 'minimum_amount', $minimum_amount );
  365. update_post_meta( $coupon_id, 'maximum_amount', $maximum_amount );
  366. update_post_meta( $coupon_id, 'customer_email', $customer_email );
  367.  
  368. $search = array( '{COUPONCODE}', '{COUPONEXPIRY}', '{ALLOWEDCATEGORIES}', '{EXCLUDEDCATEGORIES}', '{ALLOWEDPRODUCTS}', '{EXCLUDEDPRODUCTS}'
  369. ,'{USERNAME}', '{STORENAME}', '{STOREURL}', '{FIRSTNAME}', '{LASTNAME}' );
  370. $replace = array( $code, $expire, $allowed_cats, $excluded_cats, $allowed_products, $excluded_products, $username,
  371. $store_name, $store_url, $first_name, $last_name );
  372. $subject = str_replace( $search, $replace, get_option( 'wbdv_email_sub' ) );
  373. $body = str_replace( $search, $replace, get_option( 'wbdv_email' ) );
  374. $body = stripslashes( $body );
  375.  
  376. add_filter( 'wp_mail_content_type', array( $this, 'mail_content_type' ) );
  377. add_filter( 'wp_mail_from', array( $this, 'mail_from' ) );
  378. add_filter( 'wp_mail_from_name', array( $this, 'mail_from_name' ) );
  379. $headers = array('Content-Type: text/html; charset=UTF-8');
  380.  
  381. if ( version_compare( $woocommerce->version, '2.3', ">=" ) ) {
  382. $mailer = WC()->mailer();
  383. $mailer->send( $user_email, $subject, $mailer->wrap_message( $subject, $body ), $headers, '' );
  384. }
  385. else
  386. wp_mail( $user_email, $subject, wpautop( $body ), $headers );
  387.  
  388. remove_filter( 'wp_mail_content_type', array( $this, 'mail_content_type' ) );
  389. remove_filter( 'wp_mail_from', array( $this, 'mail_from' ) );
  390. remove_filter( 'wp_mail_from_name', array( $this, 'mail_from_name' ) );
  391.  
  392.  
  393.  
  394. $new_coupon_year = $check_coupon_year + 1;
  395.  
  396. update_user_meta($birthday_user->data->ID, 'coupon_year', $new_coupon_year );
  397. }
  398. }
  399. }
  400. }
  401. }
  402. }
  403. }
  404.  
  405. /**
  406. *
  407. * Set default email from address set from the admin.
  408. *
  409. * @return string $from_email email address from which the email should be sent.
  410. *
  411. */
  412. public function mail_from() {
  413. $from_email = get_option( 'wbdv_email_id' );
  414. return $from_email;
  415. }
  416.  
  417. /**
  418. *
  419. * Set default email from name set from the admin.
  420. *
  421. * @return string $from_name name from which the email should be sent.
  422. *
  423. */
  424. public function mail_from_name() {
  425. $from_name = get_option( 'wbdv_email_name' );
  426. return $from_name;
  427. }
  428.  
  429. /**
  430. *
  431. * Set email content type
  432. *
  433. * @return string content type for the email to be sent.
  434. *
  435. */
  436. public function mail_content_type() {
  437. return "text/html";
  438. }
  439.  
  440.  
  441. public function wpdv_enque_scripts() {
  442. $method = get_option('wbdv_method');
  443. $pick_style = get_option( 'wbdv_dp_style' ) . '.css';
  444.  
  445. wp_enqueue_style( 'wbdv-custom-stylesheet', plugins_url( 'assets/css/wbdv-custom.css', BDV_FILE ) );
  446.  
  447. wp_enqueue_script( 'wbdv-birthday-picker', plugins_url( 'assets/js/bday-picker.min.js', BDV_FILE ) , array( 'jquery' ), '1.0.0', true);
  448. wp_enqueue_script( 'jquery-ui-datepicker' );
  449.  
  450. wp_enqueue_style( 'wbdv-picker-stylesheet', plugins_url( 'assets/css/' . $pick_style, BDV_FILE ));
  451. wp_enqueue_script( 'wbdv-picker', plugins_url( 'assets/js/picker.js', BDV_FILE ) , array( 'jquery' ), '1.0.0', true);
  452. wp_register_style( 'jquery-ui', '//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css' );
  453. wp_enqueue_style( 'jquery-ui' );
  454. wp_enqueue_script( 'wbdv-datepicker', plugins_url( 'assets/js/picker.date.js', BDV_FILE ) , array( 'jquery', 'wbdv-picker' ), '1.0.0', true);
  455.  
  456. wp_enqueue_script('custom-script', plugins_url( 'assets/js/custom.js', BDV_FILE ), array( 'jquery' ), '1.0.0', true );
  457.  
  458. wp_localize_script('custom-script', 'wbdv',
  459. array('method' => $method)
  460. );
  461. }
  462.  
  463.  
  464. public function wpdv_add_birthday_in_account() {
  465. $birthday_label = get_option('wbdv_birthday_label');
  466. $birthday_required = get_option('wbdv_birthday_required');
  467. ?>
  468.  
  469. <p class="form-row form-row-wide">
  470. <label for="wpdv_birthday"><?php _e( $birthday_label, 'wpdv' ); ?>
  471. <?php if( $birthday_required == 'yes' ) : ?>
  472. <span class="required">*</span>
  473. <?php endif; ?>
  474. </label>
  475.  
  476. <?php
  477. $method = get_option( 'wbdv_method' );
  478. $wpdv_birthday = ( isset( $_POST['wpdv_birthday'] ) ) ? sanitize_text_field($_POST['wpdv_birthday']) : '';
  479. $user_id = get_current_user_id();
  480. $enable_user_edit = (get_option('wbdv_user_bday_enabled') == 'yes') ? 'yes' : 'no';
  481. $enable_user_edit = $enable_user_edit !== 'yes' ? 'disabled' : 'yes';
  482.  
  483. if( !empty($user_id) ) {
  484. $check_birthday_set = get_user_meta($user_id, 'wpdv_birthday_calender', true );
  485. if( !empty($check_birthday_set) ) {
  486. if( $enable_user_edit == 'yes' ) {
  487. if( $method == 'dropdown') : ?>
  488. <div id="wbdv_date_confirm" name="wbdv_date_confirm"><?php echo $check_birthday_set; ?></div>
  489. <?php endif; ?>
  490. <?php if( $method == 'datepicker' ): ?>
  491. <input id="wbdv_date_confirm" name="wbdv_date_confirm" class="input-text" value="<?php echo $check_birthday_set; ?>">
  492. <?php endif; ?>
  493. <?php if( $method == 'jqdatepicker' ): ?>
  494. <input id="wbdv_jquery_confirm" value="<?php echo $check_birthday_set; ?>" name="wbdv_date_confirm" class="input-text">
  495. <?php endif;
  496. } else {
  497. ?>
  498. <input value="<?php echo $check_birthday_set; ?>"
  499. <?php echo $enable_user_edit; ?> class="input-text">
  500. <?php
  501. }
  502. } else {
  503. if( $method == 'dropdown') : ?>
  504. <div id="wbdv_date_confirm" name="wbdv_date_confirm"><?php echo $check_birthday_set; ?></div>
  505. <?php endif; ?>
  506. <?php if( $method == 'datepicker' ): ?>
  507. <input id="wbdv_date_confirm" name="wbdv_date_confirm" class="input-text" value="<?php echo $check_birthday_set; ?>">
  508. <?php endif; ?>
  509. <?php if( $method == 'jqdatepicker' ): ?>
  510. <input id="wbdv_jquery_confirm" value="<?php echo $check_birthday_set; ?>" name="wbdv_date_confirm" class="input-text">
  511. <?php endif;
  512. }
  513. }
  514. else {
  515. if( $method == 'dropdown') : ?>
  516. <div id="wbdv_date_confirm" name="wbdv_date_confirm"></div>
  517. <?php endif; ?>
  518. <?php if( $method == 'datepicker' ): ?>
  519. <input id="wbdv_date_confirm" name="wbdv_date_confirm" class="input-text">
  520. <?php endif; ?>
  521. <?php if( $method == 'jqdatepicker' ): ?>
  522. <input id="wbdv_jquery_confirm" name="wbdv_date_confirm" class="input-text">
  523. <?php endif;
  524. }
  525. ?>
  526. </p>
  527. <?php
  528. }
  529.  
  530. public function wpdv_add_birthday_field() {
  531. // $first_name_label = get_option('wbdv_first_name');
  532. // $last_name_label = get_option('wbdv_last_name');
  533. $birthday_label = get_option('wbdv_birthday_label');
  534.  
  535. $first_name_required = get_option('wbdv_firstname_required');
  536. $last_name_required = get_option('wbdv_lastname_required');
  537. $birthday_required = get_option('wbdv_birthday_required');
  538.  
  539.  
  540. // if( empty($first_name_label) )
  541. // $first_name_label = 'First Name';
  542.  
  543. // if( empty($last_name_label) )
  544. // $last_name_label = 'Last Name';
  545.  
  546. if( empty($birthday_label) )
  547. $birthday_label = 'Birthday';
  548. ?>
  549. <!-- <p class="form-row form-row-wide">
  550. <label for="wpdv_birthday"><?php _e( $first_name_label, 'wpdv' ); ?>
  551. <?php if( $first_name_required == 'yes' ) : ?>
  552. <span class="required">*</span>
  553. <?php endif; ?>
  554. </label>
  555. <input name="wbdv_first_name" type="text" class="input-text">
  556. </p> -->
  557.  
  558. <!-- <p class="form-row form-row-wide">
  559. <label for="wpdv_birthday"><?php _e( $last_name_label, 'wpdv' ); ?>
  560. <?php if( $last_name_required == 'yes' ) : ?>
  561. <span class="required">*</span>
  562. <?php endif; ?>
  563. </label>
  564. <input name="wbdv_last_name" type="text" class="input-text">
  565. </p> -->
  566.  
  567. <p class="form-row form-row-wide">
  568. <label for="wpdv_birthday"><?php _e( $birthday_label, 'wpdv' ); ?>
  569. <?php if( $birthday_required == 'yes' ) : ?>
  570. <span class="required">*</span>
  571. <?php endif; ?>
  572. </label>
  573.  
  574. <?php
  575. $method = get_option( 'wbdv_method' );
  576. $wpdv_birthday = ( isset( $_POST['wpdv_birthday'] ) ) ? sanitize_text_field($_POST['wpdv_birthday']) : '';
  577. if( $method == 'dropdown') : ?>
  578. <div id="wbdv_date_confirm" name="wbdv_date_confirm"></div>
  579. <?php endif; ?>
  580. <?php if( $method == 'datepicker' ): ?>
  581. <input id="wbdv_date_confirm" name="wbdv_date_confirm" class="input-text">
  582. <?php endif; ?>
  583. <?php if( $method == 'jqdatepicker' ): ?>
  584. <input id="wbdv_jquery_confirm" name="wbdv_date_confirm" class="input-text">
  585. <?php endif; ?>
  586. </p>
  587. <?php
  588. }
  589.  
  590. public function wpdv_filter_checkout_fields($fields) {
  591. $birthday_method = get_option('wbdv_method');
  592. $birthday_label = get_option('wbdv_birthday_label');
  593. $birthday_required = get_option('wbdv_birthday_required');
  594.  
  595. // $firstname_label = get_option('wbdv_first_name');
  596. // $lastname_label = get_option('wbdv_last_name');
  597.  
  598. // $first_name_required = get_option('wbdv_firstname_required');
  599. // $last_name_required = get_option('wbdv_lastname_required');
  600.  
  601. if( is_user_logged_in() ) {
  602. $user_id = get_current_user_id();
  603.  
  604. $check_birthday_set = get_user_meta($user_id, 'wpdv_birthday_calender', true );
  605.  
  606. if( !empty($check_birthday_set) || $check_birthday_set !=='' ) {
  607. return $fields;
  608. }
  609. }
  610.  
  611.  
  612. $bday_required_field = false;
  613. // $fname_required_field = false;
  614. // $lname_required_field = false;
  615.  
  616. if( $birthday_required == 'yes' ) {
  617. $bday_required_field = true;
  618. }
  619.  
  620. // if( $first_name_required == 'yes' ) {
  621. // $fname_required_field = true;
  622. // }
  623.  
  624. // if( $last_name_required == 'yes' ) {
  625. // $lname_required_field = true;
  626. // }
  627.  
  628. // $fields['billing']['billing_first_name'] = array(
  629. // 'label' => __($firstname_label, 'woocommerce'),
  630. // 'required' => $fname_required_field,
  631. // 'class' => array('form-row-first'),
  632. // );
  633.  
  634. // $fields['billing']['billing_last_name'] = array(
  635. // 'label' => __($lastname_label, 'woocommerce'),
  636. // 'required' => $lname_required_field,
  637. // 'class' => array('form-row-last'),
  638. // );
  639.  
  640.  
  641. if( $birthday_method == 'datepicker' ) {
  642. $fields['billing']['wpdv_birthday_field'] = array(
  643. 'label' => __($birthday_label, 'woocommerce'),
  644. 'placeholder' => _x('', 'placeholder', 'woocommerce'),
  645. 'required' => $bday_required_field,
  646. 'class' => array('form-row-wide'),
  647. 'clear' => true
  648. );
  649. }
  650. else {
  651. $fields['billing']['wpdv_birthday_picker'] = array(
  652. 'label' => __($birthday_label, 'woocommerce'),
  653. 'placeholder' => _x('', 'placeholder', 'woocommerce'),
  654. 'required' => $bday_required_field,
  655. 'class' => array('form-row-wide'),
  656. 'clear' => true
  657. );
  658. }
  659.  
  660. return $fields;
  661. }
  662.  
  663. public function wpdv_save_birthday_checkout_field( $order_id, $posted ){
  664. $birthday_method = get_option('wbdv_method');
  665.  
  666. if( $birthday_method == 'datepicker' && !empty($order_id) && !empty($_POST['wpdv_birthday_field_submit']) ) {
  667. $birthday = sanitize_text_field($_POST['wpdv_birthday_field_submit']);
  668. }
  669.  
  670. if( $birthday_method != 'datepicker' && !empty($order_id) && !empty($_POST['wpdv_birthday_picker']) ) {
  671. $birthday = sanitize_text_field($_POST['wpdv_birthday_picker']);
  672. }
  673.  
  674. if( !empty($order_id) && !empty($birthday) ) {
  675. $get_user_id = get_post_meta($order_id, '_customer_user', true);
  676. $birthday_var = explode('-', $birthday);
  677. $birthday_var = $birthday_var[1].'-'.$birthday_var[2];
  678. if( !empty($get_user_id) ) {
  679. update_user_meta($get_user_id, 'wpdv_birthday', $birthday_var);
  680. // update_user_meta($get_user_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
  681. // update_user_meta($get_user_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
  682. update_user_meta($get_user_id, 'wpdv_birthday_calender', $birthday);
  683. }
  684.  
  685. }
  686.  
  687. }
  688.  
  689.  
  690. public function wbdv_validate_date_of_birth($username, $email, $validation_errors) {
  691. $birthday_label = get_option('wbdv_birthday_label');
  692. // $first_name_required = get_option('wbdv_firstname_required');
  693. // $last_name_required = get_option('wbdv_lastname_required');
  694. $birthday_required = get_option('wbdv_birthday_required');
  695.  
  696.  
  697. if( empty($birthday_label) )
  698. $birthday_label = __( 'Birthday' );
  699.  
  700. // if( isset($_POST['wbdv_first_name']) && empty($_POST['wbdv_first_name']) && $first_name_required == 'yes' ) {
  701. // $validation_errors->add('billing_first_name_error', __(' First name is required!', 'text_domain'));
  702. // }
  703.  
  704. // if( isset($_POST['wbdv_last_name']) && empty($_POST['wbdv_last_name']) && $last_name_required == 'yes' ) {
  705. // $validation_errors->add('billing_last_name_error', __(' Last name is required!', 'text_domain'));
  706. // }
  707.  
  708. if (isset($_POST['wpdv_birthday']) && empty($_POST['wpdv_birthday']) && $birthday_required == 'yes' ) {
  709. $validation_errors->add('billing_phone_error', __($birthday_label.' is required!', 'text_domain'));
  710. }
  711.  
  712.  
  713. }
  714.  
  715. public function wpdv_save_birthday_field($customer_id) {
  716. $datepicker_method = get_option('wbdv_method');
  717.  
  718. if( $datepicker_method == 'datepicker' ) {
  719. $birthday = sanitize_text_field($_POST['wbdv_date_confirm_submit']);
  720. }
  721. if( $datepicker_method == 'dropdown' ) {
  722. $birthday = sanitize_text_field($_POST['birthdate']);
  723. }
  724. if( $datepicker_method == 'jqdatepicker' ) {
  725. $birthday = sanitize_text_field($_POST['wbdv_date_confirm']);
  726. }
  727.  
  728.  
  729. if( empty($birthday) )
  730. $birthday = '';
  731.  
  732.  
  733. // $first_name = sanitize_text_field($_POST['wbdv_first_name']);
  734. // $last_name = sanitize_text_field($_POST['wbdv_last_name']);
  735.  
  736. if (!empty($birthday)) {
  737. update_user_meta($customer_id, 'wpdv_birthday_calender', $birthday);
  738. $wpdv_birthday = explode('-', $birthday);
  739. $birthday_var = $wpdv_birthday[1].'-'.$wpdv_birthday[2];
  740. update_user_meta($customer_id, 'wpdv_birthday', $birthday_var);
  741. }
  742.  
  743. // update_user_meta($customer_id, 'first_name', $first_name);
  744. // update_user_meta($customer_id, 'last_name', $last_name);
  745. }
  746.  
  747. /**
  748. *
  749. * Output products for the ajax search on admin.
  750. *
  751. * @return json matched products
  752. *
  753. */
  754. public function wbdv_ajax_products() {
  755. global $wpdb;
  756. $post_types = array( 'product' );
  757. ob_start();
  758.  
  759. if ( empty( $term ) ) {
  760. $term = wc_clean( stripslashes( $_GET['term'] ) );
  761. } else {
  762. $term = wc_clean( $term );
  763. }
  764.  
  765. if ( empty( $term ) ) {
  766. die();
  767. }
  768.  
  769. $like_term = '%' . $wpdb->esc_like( $term ) . '%';
  770.  
  771. if ( is_numeric( $term ) ) {
  772. $query = $wpdb->prepare( "
  773. SELECT ID FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id
  774. WHERE posts.post_status = 'publish'
  775. AND (
  776. posts.post_parent = %s
  777. OR posts.ID = %s
  778. OR posts.post_title LIKE %s
  779. OR (
  780. postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s
  781. )
  782. )
  783. ", $term, $term, $term, $like_term );
  784. } else {
  785. $query = $wpdb->prepare( "
  786. SELECT ID FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id
  787. WHERE posts.post_status = 'publish'
  788. AND (
  789. posts.post_title LIKE %s
  790. or posts.post_content LIKE %s
  791. OR (
  792. postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s
  793. )
  794. )
  795. ", $like_term, $like_term, $like_term );
  796. }
  797.  
  798. $query .= " AND posts.post_type IN ('" . implode( "','", array_map( 'esc_sql', $post_types ) ) . "')";
  799.  
  800. if ( ! empty( $_GET['exclude'] ) ) {
  801. $query .= " AND posts.ID NOT IN (" . implode( ',', array_map( 'intval', explode( ',', $_GET['exclude'] ) ) ) . ")";
  802. }
  803.  
  804. if ( ! empty( $_GET['include'] ) ) {
  805. $query .= " AND posts.ID IN (" . implode( ',', array_map( 'intval', explode( ',', $_GET['include'] ) ) ) . ")";
  806. }
  807.  
  808. if ( ! empty( $_GET['limit'] ) ) {
  809. $query .= " LIMIT " . intval( $_GET['limit'] );
  810. }
  811.  
  812. $posts = array_unique( $wpdb->get_col( $query ) );
  813. $found_products = array();
  814.  
  815. if ( ! empty( $posts ) ) {
  816. foreach ( $posts as $post ) {
  817. $product = wc_get_product( $post );
  818.  
  819. if ( ! current_user_can( 'read_product', $post ) ) {
  820. continue;
  821. }
  822.  
  823. if ( ! $product || ( $product->is_type( 'variation' ) && empty( $product->parent ) ) ) {
  824. continue;
  825. }
  826.  
  827. $found_products[ $post ] = rawurldecode( $product->get_formatted_name() );
  828. }
  829. }
  830.  
  831. wp_send_json( $found_products );
  832. }
  833.  
  834. public function send_birthday_emails_to_users() {
  835. if(!wp_next_scheduled('wbdv_birthday_cron')) {
  836. $customer_time = '00:00';
  837. wp_schedule_event(strtotime( date( 'Y-m-d' ) .' '. $customer_time ), 'daily', 'wbdv_birthday_cron');
  838. }
  839. }
  840.  
  841.  
  842. public function wbdv_deactivate_emails() {
  843. wp_clear_scheduled_hook('wbdv_birthday_cron' );
  844. wp_die();
  845. }
  846.  
  847. /**
  848. * Add new admin setting page for birthday discount voucher settings.
  849. *
  850. * @param array $settings an array of existing setting pages.
  851. * @return array of setting pages along with birthday discount voucher settings page.
  852. *
  853. */
  854. public function bdv_settings_class( $settings ) {
  855. $settings[] = include 'class-wc-settings-birthday-discount-vouchers.php';
  856. return $settings;
  857. }
  858.  
  859. public function wbdv_user_birth_day_field($user) {
  860. ?>
  861. <table class="form-table">
  862. <tr>
  863. <th><label for="birthday"><?php _e("Дата рождения"); ?></label></th>
  864. <td>
  865. <?php if( is_admin() ) : ?>
  866. <input type="text" name="wbdv_profile_datepicker" id="wbdv_profile_datepicker" value="<?php echo esc_attr( get_the_author_meta( 'wpdv_birthday_calender', $user->ID ) ); ?>">
  867. <?php else: ?>
  868. <input type="text" readonly name="wbdv_profile_datepicker" id="wbdv_profile_datepicker" value="<?php echo esc_attr( get_the_author_meta( 'wpdv_birthday', $user->ID ) ); ?>">
  869. <?php endif; ?>
  870. <span class="description"><?php _e("Select date of birth for the user"); ?></span>
  871. </td>
  872. </tr>
  873. </table>
  874. <?php }
  875.  
  876. public function wbdv_save_user_birthday_profile_fields($user_id) {
  877. if ( !is_admin() )
  878. return false;
  879.  
  880. $birthday = sanitize_text_field($_POST['wbdv_profile_datepicker']);
  881. $birthday_var = explode('-', $birthday);
  882. $birthday_var = $birthday_var[1].'-'.$birthday_var[2];
  883. if( !empty($user_id) ) {
  884. update_user_meta($user_id, 'wpdv_birthday', $birthday_var);
  885. update_user_meta($user_id, 'wpdv_birthday_calender', $birthday);
  886. }
  887. }
  888.  
  889. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement