Skip to content

Commit

Permalink
fix: validate boolean value while updating vendor (#592)
Browse files Browse the repository at this point in the history
* fix: validate boolean value while updating vendor

* fix: validate boolean value while updating vendor

* refactor: use get_avatar_id & get_banner_id method instead of array

* feat: populate admin commission data in rest for admin

* feat: add store url checking rest api and make debounce global function

* feat: add dokan.addFilterComponent function to mimic WP do_action

* feat: send email to admin and vendor on new vendor registration

* refactor: Dokan_Vendor create method

* feat(api): check if user email is available or not

* refactor: update banner and gravatar id
  • Loading branch information
saimonh3 authored and sabbir1991 committed Apr 17, 2019
1 parent 37ea552 commit bcfa6de
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 34 deletions.
2 changes: 1 addition & 1 deletion classes/admin-user-profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ function save_meta_fields( $user_id ) {
$social = $post_data['dokan_social'];
$social_fields = dokan_get_social_profile_fields();

$store_settings['banner'] = intval( $post_data['dokan_banner'] );
$store_settings['banner_id'] = intval( $post_data['dokan_banner'] );
$store_settings['store_name'] = sanitize_text_field( $post_data['dokan_store_name'] );
$store_settings['address'] = isset( $post_data['dokan_store_address'] ) ? array_map( 'sanitize_text_field', $post_data['dokan_store_address'] ) : array();
$store_settings['phone'] = sanitize_text_field( $post_data['dokan_store_phone'] );
Expand Down
4 changes: 2 additions & 2 deletions classes/template-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,11 @@ function insert_settings_info() {
'address' => isset( $post_data['dokan_address'] ) ? array_map( 'sanitize_text_field', $post_data['dokan_address'] ) : $prev_dokan_settings['address'],
'location' => sanitize_text_field( $post_data['location'] ),
'find_address' => sanitize_text_field( $post_data['find_address'] ),
'banner' => isset( $post_data['dokan_banner'] ) ? absint( $post_data['dokan_banner'] ) : null,
'banner_id' => isset( $post_data['dokan_banner'] ) ? absint( $post_data['dokan_banner'] ) : 0,
'phone' => sanitize_text_field( $post_data['setting_phone'] ),
'show_email' => sanitize_text_field( $post_data['setting_show_email'] ),
'show_more_ptab' => sanitize_text_field( $post_data['setting_show_more_ptab'] ),
'gravatar' => absint( $post_data['dokan_gravatar'] ),
'gravatar_id' => isset( $post_data['dokan_gravatar'] ) ? absint( $post_data['dokan_gravatar'] ) : 0,
'enable_tnc' => isset( $post_data['dokan_store_tnc_enable'] ) && 'on' == $post_data['dokan_store_tnc_enable'] ? 'on' : 'off',
'store_tnc' => isset( $post_data['dokan_store_tnc'] ) ? wp_kses_post( $post_data['dokan_store_tnc'] ) : '',
'dokan_store_time' => array(
Expand Down
94 changes: 94 additions & 0 deletions includes/api/class-store-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public function register_routes() {
'args' => $this->get_collection_params()
),
) );

register_rest_route( $this->namespace, '/' . $this->base . '/check', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'check_store_availability' ]
]
] );
}

/**
Expand Down Expand Up @@ -547,4 +554,91 @@ public function prepare_reviews_for_response( $item, $request, $additional_field
return $data;
}

/**
* Check store availability
*
* @param array $request
*
* @since DOKAN_SINCE
*
* @return reponse
*/
public function check_store_availability( $request ) {
$params = $request->get_params();

// check whether store name is available or not
if ( ! empty( $params['store_slug'] ) ) {
$store_slug = sanitize_text_field( $params['store_slug'] );

if ( get_user_by( 'slug', $store_slug ) ) {
$response = [
'url' => $store_slug,
'available' => false
];

return rest_ensure_response( $response );
}

$response = [
'url' => sanitize_title( $store_slug ),
'available' => true
];

return rest_ensure_response( $response );
}

// check whether username is available or not
if ( ! empty( $params['username'] ) ) {
$username = sanitize_user( $params['username'] );

if ( get_user_by( 'login', $username ) ) {
$response = [
'username' => $username,
'available' => false
];

return rest_ensure_response( $response );
}

$response = [
'username' => $username,
'available' => true
];

return rest_ensure_response( $response );
}

// check whether email is available or not
if ( ! empty( $params['user_email'] ) ) {
$user_email = $params['user_email'];

if ( ! is_email( $user_email ) ) {
$response = [
'user_email' => $user_email,
'available' => false,
'message' => __( 'This email address is not valid', 'dokan-lite' )
];

return rest_ensure_response( $response );
}

if ( email_exists( $user_email ) ) {
$response = [
'user_email' => $user_email,
'available' => false
];

return rest_ensure_response( $response );
}

$response = [
'user_email' => $user_email,
'available' => true
];

return rest_ensure_response( $response );
}

return rest_ensure_response( [] );
}
}
32 changes: 32 additions & 0 deletions includes/class-api-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function __construct() {
// Init REST API routes.
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'prepeare_product_response' ), 10, 3 );

// populate admin commission data for admin
add_filter( 'dokan_rest_store_additional_fields', array( $this, 'populate_admin_commission' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -78,4 +81,33 @@ public function prepeare_product_response( $response, $object, $request ) {
$response->set_data( $data );
return $response;
}

/**
* Populate admin commission
*
* @param array $data
* @param array $store
*
* @since DOKAN_SINCE
*
* @return data
*/
public function populate_admin_commission( $data, $store ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return $data;
}

$store_id = $store->get_id();

if ( ! $store_id ) {
return $data;
}

$commission = get_user_meta( $store_id, 'dokan_admin_percentage', true );
$commission_type = get_user_meta( $store_id, 'dokan_admin_percentage_type', true );
$data['admin_commission'] = $commission;
$data['admin_commission_type'] = $commission_type;

return $data;
}
}
65 changes: 51 additions & 14 deletions includes/class-vendor-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,17 @@ public function create( $data = [] ) {
];

$vendor_data = wp_parse_args( $data, $defaults );
$vendor = wp_insert_user( $vendor_data );
$vendor_id = wp_insert_user( $vendor_data );

if ( is_wp_error( $vendor ) ) {
return $vendor;
if ( is_wp_error( $vendor_id ) ) {
return $vendor_id;
}

// send vendor registration email to admin and vendor
if ( isset( $data['notify_vendor'] ) && dokan_validate_boolean( $data['notify_vendor' ] ) ) {
wp_send_new_user_notifications( $vendor_id, 'both' );
} else {
wp_send_new_user_notifications( $vendor_id, 'admin' );
}

$store_data = apply_filters( 'dokan_vendor_create_data', [
Expand All @@ -142,8 +149,10 @@ public function create( $data = [] ) {
'address' => ! empty( $data['address'] ) ? $data['address'] : [],
'location' => ! empty( $data['location'] ) ? $data['location'] : '',
'banner' => ! empty( $data['banner'] ) ? $data['banner'] : 0,
'banner_id' => ! empty( $data['banner_id'] ) ? $data['banner_id'] : 0,
'icon' => ! empty( $data['icon'] ) ? $data['icon'] : '',
'gravatar' => ! empty( $data['gravatar'] ) ? $data['gravatar'] : 0,
'gravatar_id' => ! empty( $data['gravatar_id'] ) ? $data['gravatar_id'] : 0,
'show_more_ptab' => ! empty( $data['show_more_ptab'] ) ? $data['show_more_ptab'] : 'yes',
'store_ppp' => ! empty( $data['store_ppp'] ) ? $data['store_ppp'] : 10,
'enable_tnc' => ! empty( $data['enable_tnc'] ) ? $data['enable_tnc'] : 'off',
Expand All @@ -152,11 +161,27 @@ public function create( $data = [] ) {
'store_seo' => ! empty( $data['store_seo'] ) ? $data['store_seo'] : []
] );

update_user_meta( $vendor, 'dokan_profile_settings', $store_data );
if ( current_user_can( 'manage_woocommerce' ) ) {
$vendor = dokan()->vendor->get( $vendor_id );

if ( isset( $data['enabled'] ) && dokan_validate_boolean( $data['enabled'] ) ) {
$vendor->update_meta( 'dokan_enable_selling', 'yes' );
}

if ( isset( $data['featured'] ) && dokan_validate_boolean( $data['featured'] ) ) {
$vendor->update_meta( 'dokan_feature_seller', 'yes' );
}

if ( isset( $data['trusted'] ) && dokan_validate_boolean( $data['trusted'] ) ) {
$vendor->update_meta( 'dokan_publishing', 'yes' );
}
}

do_action( 'dokan_new_vendor', $vendor );
update_user_meta( $vendor_id, 'dokan_profile_settings', $store_data );

return $this->get( $vendor );
do_action( 'dokan_new_vendor', $vendor_id );

return $this->get( $vendor_id );
}

/**
Expand Down Expand Up @@ -245,6 +270,14 @@ public function update( $vendor_id, $data = [] ) {
} else {
$vendor->update_meta( 'dokan_publishing', 'no' );
}

if ( isset( $data['admin_commission'] ) && is_numeric( $data['admin_commission'] ) ) {
$vendor->update_meta( 'dokan_admin_percentage', $data['admin_commission'] );
}

if ( ! empty( $data['admin_commission_type'] ) ) {
$vendor->update_meta( 'dokan_admin_percentage_type', $data['admin_commission_type'] );
}
}

// update vendor store data
Expand All @@ -256,20 +289,24 @@ public function update( $vendor_id, $data = [] ) {
$vendor->set_phone( $data['phone'] );
}

if ( ! empty( $data['show_email'] ) ) {
$vendor->set_show_email( $data['show_email'] );
if ( isset( $data['show_email'] ) && dokan_validate_boolean( $data['show_email'] ) ) {
$vendor->set_show_email( 'yes' );
} else {
$vendor->set_show_email( 'no' );
}

if ( ! empty( $data['gravatar'] ) ) {
$vendor->set_gravatar( $data['gravatar'] );
if ( ! empty( $data['gravatar_id'] ) && is_numeric( $data['gravatar_id'] ) ) {
$vendor->set_gravatar_id( $data['gravatar_id'] );
}

if ( ! empty( $data['banner'] ) ) {
$vendor->set_banner( $data['banner'] );
if ( ! empty( $data['banner_id'] ) && is_numeric( $data['banner_id'] ) ) {
$vendor->set_banner_id( $data['banner_id'] );
}

if ( ! empty( $data['enable_tnc'] ) ) {
$vendor->set_enable_tnc( $data['enable_tnc'] );
if ( isset( $data['enable_tnc'] ) && dokan_validate_boolean( $data['enable_tnc'] ) ) {
$vendor->set_enable_tnc( 'on' );
} else {
$vendor->set_enable_tnc( 'off' );
}

if ( ! empty( $data['icon'] ) ) {
Expand Down
42 changes: 36 additions & 6 deletions includes/class-vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public function to_array() {
'address' => $this->get_address(),
'location' => $this->get_location(),
'banner' => $this->get_banner(),
'banner_id' => $this->get_banner_id(),
'gravatar' => $this->get_avatar(),
'gravatar_id' => $this->get_avatar_id(),
'shop_url' => $this->get_shop_url(),
'products_per_page' => $this->get_per_page(),
'show_more_product_tab' => $this->show_more_products_tab(),
Expand Down Expand Up @@ -181,8 +183,10 @@ public function popluate_store_data() {
'address' => array(),
'location' => '',
'banner' => 0,
'banner_id' => 0,
'icon' => 0,
'gravatar' => 0,
'gravatar_id' => 0,
'show_more_ptab' => 'yes',
'store_ppp' => 10,
'enable_tnc' => 'off',
Expand Down Expand Up @@ -389,7 +393,7 @@ public function get_location() {
* @return string
*/
public function get_banner() {
$banner_id = (int) $this->get_info_part( 'banner' );
$banner_id = $this->get_banner_id();

if ( ! $banner_id ) {
return false;
Expand All @@ -398,6 +402,19 @@ public function get_banner() {
return wp_get_attachment_url( $banner_id );
}

/**
* Get the shop banner id
*
* @since DOKAN_SINCE
*
* @return int
*/
public function get_banner_id() {
$banner_id = (int) $this->get_info_part( 'banner_id' );

return $banner_id ? $banner_id : 0;
}

/**
* Get the shop profile icon
*
Expand All @@ -406,7 +423,7 @@ public function get_banner() {
* @return string
*/
public function get_avatar() {
$avatar_id = (int) $this->get_info_part( 'gravatar' );
$avatar_id = $this->get_avatar_id();

if ( ! $avatar_id && ! empty( $this->data->user_email ) ) {
return get_avatar_url( $this->data->user_email, 96 );
Expand All @@ -415,6 +432,19 @@ public function get_avatar() {
return wp_get_attachment_url( $avatar_id );
}

/**
* Get shop gravatar id
*
* @since DOKAN_SINCE
*
* @return int
*/
public function get_avatar_id() {
$avatar_id = (int) $this->get_info_part( 'gravatar_id' );

return $avatar_id ? $avatar_id : 0;
}

/**
* Get per page pagination
*
Expand Down Expand Up @@ -779,17 +809,17 @@ public function set_enable_tnc( $value ) {
*
* @param int value
*/
public function set_gravatar( $value ) {
$this->set_prop( 'gravatar', (int) $value );
public function set_gravatar_id( $value ) {
$this->set_prop( 'gravatar_id', (int) $value );
}

/**
* Set banner
*
* @param int value
*/
public function set_banner( $value ) {
$this->set_prop( 'banner', (int) $value );
public function set_banner_id( $value ) {
$this->set_prop( 'banner_id', (int) $value );
}

/**
Expand Down
Loading

0 comments on commit bcfa6de

Please sign in to comment.