is_callable( $coupon, 'get_meta' ); $user_role_ids = ( true === $is_callable_coupon_get_meta ) ? $coupon->get_meta( 'wc_sc_user_role_ids' ) : get_post_meta( $coupon_id, 'wc_sc_user_role_ids', true ); if ( empty( $user_role_ids ) || ! is_array( $user_role_ids ) ) { $user_role_ids = array(); } $exclude_user_role_ids = ( true === $is_callable_coupon_get_meta ) ? $coupon->get_meta( 'wc_sc_exclude_user_role_ids' ) : get_post_meta( $coupon_id, 'wc_sc_exclude_user_role_ids', true ); if ( empty( $exclude_user_role_ids ) || ! is_array( $exclude_user_role_ids ) ) { $exclude_user_role_ids = array(); } } $available_user_roles = $this->get_available_user_roles(); ?>

is_callable( $coupon, 'update_meta_data' ) && $this->is_callable( $coupon, 'save' ) ) { $coupon->update_meta_data( 'wc_sc_user_role_ids', $user_role_ids ); $coupon->update_meta_data( 'wc_sc_exclude_user_role_ids', $exclude_user_role_ids ); $coupon->save(); } else { update_post_meta( $post_id, 'wc_sc_user_role_ids', $user_role_ids ); update_post_meta( $post_id, 'wc_sc_exclude_user_role_ids', $exclude_user_role_ids ); } } /** * Validate the coupon based on user role * * @param boolean $valid Is valid or not. * @param WC_Coupon $coupon The coupon object. * @param WC_Discounts $discounts The discount object. * * @throws Exception If the coupon is invalid. * @return boolean Is valid or not */ public function validate( $valid = false, $coupon = object, $discounts = null ) { // If coupon is invalid already, no need for further checks. if ( false === $valid ) { return $valid; } $coupon_id = ( $this->is_wc_gte_30() ) ? $coupon->get_id() : $coupon->id; if ( ! is_a( $coupon, 'WC_Coupon' ) ) { $coupon = new WC_Coupon( $coupon_id ); } if ( $this->is_callable( $coupon, 'get_meta' ) ) { $user_role_ids = $coupon->get_meta( 'wc_sc_user_role_ids' ); $exclude_user_role_ids = $coupon->get_meta( 'wc_sc_exclude_user_role_ids' ); } else { $user_role_ids = get_post_meta( $coupon_id, 'wc_sc_user_role_ids', true ); $exclude_user_role_ids = get_post_meta( $coupon_id, 'wc_sc_exclude_user_role_ids', true ); } $current_user = wp_get_current_user(); $post_action = ( ! empty( $_POST['action'] ) ) ? wc_clean( wp_unslash( $_POST['action'] ) ) : ''; // phpcs:ignore if ( is_admin() && wp_doing_ajax() && 'woocommerce_add_coupon_discount' === $post_action ) { // This condition will allow the addition of coupon from admin side, in the order even if the user role is not matching. return true; } if ( is_array( $user_role_ids ) && ! empty( $user_role_ids ) ) { // Check if current user's role is allowed. if ( ! array_intersect( $current_user->roles, $user_role_ids ) ) { throw new Exception( __( 'This coupon is not valid for you.', 'woocommerce-smart-coupons' ) ); } } if ( is_array( $exclude_user_role_ids ) && ! empty( $exclude_user_role_ids ) ) { // Check if current user's role is excluded. if ( array_intersect( $current_user->roles, $exclude_user_role_ids ) ) { throw new Exception( __( 'This coupon is not valid for you.', 'woocommerce-smart-coupons' ) ); } } return $valid; } /** * Add meta in export headers * * @param array $headers Existing headers. * @return array */ public function export_headers( $headers = array() ) { $headers['wc_sc_user_role_ids'] = __( 'User Role', 'woocommerce-smart-coupons' ); $headers['wc_sc_exclude_user_role_ids'] = __( 'Exclude User Role', 'woocommerce-smart-coupons' ); return $headers; } /** * Function to handle coupon meta data during export of existing coupons * * @param mixed $meta_value The meta value. * @param array $args Additional arguments. * @return string Processed meta value */ public function export_coupon_meta_data( $meta_value = '', $args = array() ) { if ( ! empty( $args['meta_key'] ) ) { if ( 'wc_sc_user_role_ids' === $args['meta_key'] ) { if ( isset( $args['meta_value'] ) && ! empty( $args['meta_value'] ) ) { $user_role_ids = maybe_unserialize( stripslashes( $args['meta_value'] ) ); if ( is_array( $user_role_ids ) && ! empty( $user_role_ids ) ) { $user_role_names = $this->get_user_role_names_by_ids( $user_role_ids ); if ( is_array( $user_role_names ) && ! empty( $user_role_names ) ) { $meta_value = implode( '|', wc_clean( wp_unslash( $user_role_names ) ) ); // Replace user role ids with their respective role name. } } } } elseif ( 'wc_sc_exclude_user_role_ids' === $args['meta_key'] ) { if ( isset( $args['meta_value'] ) && ! empty( $args['meta_value'] ) ) { $exclude_user_role_ids = maybe_unserialize( stripslashes( $args['meta_value'] ) ); if ( is_array( $exclude_user_role_ids ) && ! empty( $exclude_user_role_ids ) ) { $exclude_user_role_names = $this->get_user_role_names_by_ids( $exclude_user_role_ids ); if ( is_array( $exclude_user_role_names ) && ! empty( $exclude_user_role_names ) ) { $meta_value = implode( '|', wc_clean( wp_unslash( $exclude_user_role_names ) ) ); // Replace user role ids with their respective role name. } } } } } return $meta_value; } /** * Post meta defaults for user role ids meta * * @param array $defaults Existing postmeta defaults. * @return array */ public function postmeta_defaults( $defaults = array() ) { $defaults['wc_sc_user_role_ids'] = ''; $defaults['wc_sc_exclude_user_role_ids'] = ''; return $defaults; } /** * Add user role's meta with value in coupon meta * * @param array $data The row data. * @param array $post The POST values. * @return array Modified data */ public function generate_coupon_meta( $data = array(), $post = array() ) { $user_role_names = ''; $exclude_user_role_names = ''; if ( ! empty( $post['wc_sc_user_role_ids'] ) && is_array( $post['wc_sc_user_role_ids'] ) ) { $user_role_names = $this->get_user_role_names_by_ids( $post['wc_sc_user_role_ids'] ); if ( is_array( $user_role_names ) && ! empty( $user_role_names ) ) { $user_role_names = implode( '|', wc_clean( wp_unslash( $user_role_names ) ) ); } } if ( ! empty( $post['wc_sc_exclude_user_role_ids'] ) && is_array( $post['wc_sc_exclude_user_role_ids'] ) ) { $exclude_user_role_names = $this->get_user_role_names_by_ids( $post['wc_sc_exclude_user_role_ids'] ); if ( is_array( $exclude_user_role_names ) && ! empty( $exclude_user_role_names ) ) { $exclude_user_role_names = implode( '|', wc_clean( wp_unslash( $exclude_user_role_names ) ) ); } } $data['wc_sc_user_role_ids'] = $user_role_names; // Replace user role ids with their respective role name. $data['wc_sc_exclude_user_role_ids'] = $exclude_user_role_names; // Replace user role ids with their respective role name. return $data; } /** * Function to get user role titles for given user role ids * * @param array $user_role_ids ids of user roles. * @return array $user_role_names titles of user roles */ public function get_user_role_names_by_ids( $user_role_ids = array() ) { $user_role_names = array(); if ( is_array( $user_role_ids ) && ! empty( $user_role_ids ) ) { $available_user_roles = $this->get_available_user_roles(); foreach ( $user_role_ids as $index => $user_role_id ) { $user_role = ( isset( $available_user_roles[ $user_role_id ] ) && ! empty( $available_user_roles[ $user_role_id ] ) ) ? $available_user_roles[ $user_role_id ] : ''; if ( is_array( $user_role ) && ! empty( $user_role ) ) { $user_role_name = ! empty( $user_role['name'] ) ? $user_role['name'] : ''; if ( ! empty( $user_role_name ) ) { $user_role_names[ $index ] = $user_role_name; // Replace user role id with it's repective name. } else { $user_role_names[ $index ] = $user_role_id; // In case of empty user role name replace it with role id. } } } } return $user_role_names; } /** * Process coupon meta value for import * * @param mixed $meta_value The meta value. * @param array $args Additional Arguments. * @return mixed $meta_value */ public function process_coupon_meta_value_for_import( $meta_value = null, $args = array() ) { if ( ! empty( $args['meta_key'] ) ) { $available_user_roles = $this->get_available_user_roles(); if ( 'wc_sc_user_role_ids' === $args['meta_key'] ) { $meta_value = ( ! empty( $args['postmeta']['wc_sc_user_role_ids'] ) ) ? explode( '|', wc_clean( wp_unslash( $args['postmeta']['wc_sc_user_role_ids'] ) ) ) : array(); if ( is_array( $meta_value ) && ! empty( $meta_value ) ) { if ( is_array( $available_user_roles ) && ! empty( $available_user_roles ) ) { foreach ( $meta_value as $index => $user_role_name ) { foreach ( $available_user_roles as $role_id => $user_role ) { $role_name = isset( $user_role['name'] ) ? $user_role['name'] : ''; if ( $role_name === $user_role_name ) { $meta_value[ $index ] = $role_id; // Replace user role title with it's repective id. } } } } } } elseif ( 'wc_sc_exclude_user_role_ids' === $args['meta_key'] ) { $meta_value = ( ! empty( $args['postmeta']['wc_sc_exclude_user_role_ids'] ) ) ? explode( '|', wc_clean( wp_unslash( $args['postmeta']['wc_sc_exclude_user_role_ids'] ) ) ) : array(); if ( is_array( $meta_value ) && ! empty( $meta_value ) ) { if ( is_array( $available_user_roles ) && ! empty( $available_user_roles ) ) { foreach ( $meta_value as $index => $user_role_name ) { foreach ( $available_user_roles as $role_id => $user_role ) { $role_name = isset( $user_role['name'] ) ? $user_role['name'] : ''; if ( $role_name === $user_role_name ) { $meta_value[ $index ] = $role_id; // Replace user role title with it's repective id. } } } } } } } return $meta_value; } /** * Make meta data of user role ids protected * * @param bool $protected Is protected. * @param string $meta_key The meta key. * @param string $meta_type The meta type. * @return bool $protected */ public function make_action_meta_protected( $protected = false, $meta_key = '', $meta_type = '' ) { if ( in_array( $meta_key, array( 'wc_sc_user_role_ids', 'wc_sc_exclude_user_role_ids' ), true ) ) { return true; } return $protected; } /** * Function to get available user roles which current user use. * * @return array $available_user_roles Available user roles */ public function get_available_user_roles() { $available_user_roles = array(); if ( ! function_exists( 'get_editable_roles' ) ) { require_once ABSPATH . 'wp-admin/includes/user.php'; } if ( function_exists( 'get_editable_roles' ) ) { $available_user_roles = get_editable_roles(); } return $available_user_roles; } /** * Function to copy user role restriction meta in newly generated coupon * * @param array $args The arguments. */ public function copy_coupon_user_role_meta( $args = array() ) { // Copy meta data to new coupon. $this->copy_coupon_meta_data( $args, array( 'wc_sc_user_role_ids', 'wc_sc_exclude_user_role_ids' ) ); } /** * Validate user role after checkout. * * @param array $posted Post data. * @param WP_Error $errors Validation errors. * @return void */ public function validate_after_checkout( $posted = array(), $errors = object ) { $current_user_id = get_current_user_id(); if ( ! empty( $current_user_id ) ) { return; } $billing_email = ! empty( $posted['billing_email'] ) ? $posted['billing_email'] : ''; if ( empty( $posted['billing_email'] ) ) { return; } $cart = ( function_exists( 'WC' ) && isset( WC()->cart ) ) ? WC()->cart : null; if ( is_a( $cart, 'WC_Cart' ) ) { $is_cart_empty = is_callable( array( $cart, 'is_empty' ) ) && $cart->is_empty(); if ( false === $is_cart_empty ) { $applied_coupons = ( is_callable( array( $cart, 'get_applied_coupons' ) ) ) ? $cart->get_applied_coupons() : array(); if ( ! empty( $applied_coupons ) ) { foreach ( $applied_coupons as $code ) { $coupon = new WC_Coupon( $code ); if ( ! is_object( $coupon ) ) { continue; } if ( is_callable( array( $coupon, 'get_meta' ) ) ) { $user_role_ids = $coupon->get_meta( 'wc_sc_user_role_ids' ); $exclude_user_role_ids = $coupon->get_meta( 'wc_sc_exclude_user_role_ids' ); } else { if ( is_callable( array( $coupon, 'get_id' ) ) ) { $coupon_id = $coupon->get_id(); } else { $coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; } if ( empty( $coupon_id ) ) { continue; } $user_role_ids = get_post_meta( $coupon_id, 'wc_sc_user_role_ids', true ); $exclude_user_role_ids = get_post_meta( $coupon_id, 'wc_sc_exclude_user_role_ids', true ); } if ( empty( $exclude_user_role_ids ) && empty( $user_role_ids ) ) { continue; } $current_user = get_user_by( 'email', $billing_email ); $current_user_roles = ! empty( $current_user->roles ) ? $current_user->roles : array(); $is_message = is_callable( array( $coupon, 'add_coupon_message' ) ); $is_remove = is_callable( array( $cart, 'remove_coupon' ) ); if ( is_array( $user_role_ids ) && ! empty( $user_role_ids ) ) { // Check if current user's role is allowed. if ( ! array_intersect( $current_user_roles, $user_role_ids ) ) { if ( true === $is_message ) { $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED ); } if ( true === $is_remove ) { $cart->remove_coupon( $code ); } } } if ( is_array( $exclude_user_role_ids ) && ! empty( $exclude_user_role_ids ) ) { // Check if current user's role is excluded. if ( array_intersect( $current_user_roles, $exclude_user_role_ids ) ) { if ( true === $is_message ) { $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED ); } if ( true === $is_remove ) { $cart->remove_coupon( $code ); } } } } } } } } } } WC_SC_Coupons_By_User_Role::get_instance();