Skip to content
Open
15 changes: 13 additions & 2 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WeDevs\Dokan\Admin;

use Exception;
use WeDevs\Dokan\Utilities\AdminSettings;
use WP_Error;
use WeDevs\Dokan\Exceptions\DokanException;
use WeDevs\Dokan\Traits\AjaxResponseError;
Expand Down Expand Up @@ -126,6 +127,15 @@ public function get_settings_value() {
$settings[ $section['id'] ] = apply_filters( 'dokan_get_settings_values', $this->sanitize_options( get_option( $section['id'], [] ), 'read' ), $section['id'] );
}

$new_seller_enable_selling_statuses = ! empty( $settings['dokan_selling']['new_seller_enable_selling'] ) ? $settings['dokan_selling']['new_seller_enable_selling'] : 'automatically';

/**
* This is the mapper of enabled selling admin setting option for before and after of DOKAN_SINCE
*/
if ( ! in_array( $new_seller_enable_selling_statuses, $settings, true ) ) {
$settings['dokan_selling']['new_seller_enable_selling'] = dokan_get_container()->get( AdminSettings::class )->get_new_seller_enable_selling_status( $settings['dokan_selling']['new_seller_enable_selling'] );
}

wp_send_json_success( $settings );
}

Expand Down Expand Up @@ -630,8 +640,9 @@ public function get_settings_fields() {
'name' => 'new_seller_enable_selling',
'label' => __( 'Enable Selling', 'dokan-lite' ),
'desc' => __( 'Immediately enable selling for newly registered vendors', 'dokan-lite' ),
'type' => 'switcher',
'default' => 'on',
'type' => 'select',
'options' => dokan_get_container()->get( AdminSettings::class )->new_seller_enable_selling_statuses(),
'default' => 'automatically',
'tooltip' => __( 'If checked, vendors will have permission to sell immediately after registration. If unchecked, newly registered vendors cannot add products until selling capability is activated manually from admin dashboard.', 'dokan-lite' ),
],
'one_step_product_create' => [
Expand Down
10 changes: 6 additions & 4 deletions includes/Admin/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WeDevs\Dokan\Admin;

use stdClass;
use WeDevs\Dokan\Utilities\AdminSettings;

/**
* Setup wizard class
Expand Down Expand Up @@ -484,19 +485,19 @@ public function dokan_setup_selling() {
$options = get_option( 'dokan_selling', [ 'admin_percentage' => 10 ] );
$admin_percentage = isset( $options['admin_percentage'] ) ? $options['admin_percentage'] : 10;

$new_seller_enable_selling = ! empty( $options['new_seller_enable_selling'] ) ? $options['new_seller_enable_selling'] : '';
$commission_type = ! empty( $options['commission_type'] ) ? $options['commission_type'] : 'percentage';
$order_status_change = ! empty( $options['order_status_change'] ) ? $options['order_status_change'] : '';
$dokan_commission_types = dokan_commission_types();

$args = apply_filters(
'dokan_admin_setup_wizard_step_setup_selling_template_args', [
'new_seller_enable_selling' => $new_seller_enable_selling,
'new_seller_enable_selling' => dokan_get_container()->get( AdminSettings::class )->get_new_seller_enable_selling_status(),
'commission_type' => $commission_type,
'admin_percentage' => $admin_percentage,
'order_status_change' => $order_status_change,
'dokan_commission_types' => $dokan_commission_types,
'setup_wizard' => $this,
'new_seller_enable_selling_statuses' => dokan_get_container()->get( AdminSettings::class )->new_seller_enable_selling_statuses(),
]
);

Expand Down Expand Up @@ -538,8 +539,9 @@ public function dokan_setup_commission() {
public function dokan_setup_selling_save() {
check_admin_referer( 'dokan-setup' );

$options = get_option( 'dokan_selling', [] );
$options['new_seller_enable_selling'] = isset( $_POST['new_seller_enable_selling'] ) ? 'on' : 'off';
$options = get_option( 'dokan_selling', [] );
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
$options['new_seller_enable_selling'] = dokan_get_container()->get( AdminSettings::class )->get_new_seller_enable_selling_status( sanitize_text_field( wp_unslash( $_POST['new_seller_enable_selling'] ) ) ?? 'automatically' );
$options['order_status_change'] = isset( $_POST['order_status_change'] ) ? 'on' : 'off';

update_option( 'dokan_selling', $options );
Expand Down
6 changes: 5 additions & 1 deletion includes/Admin/UserProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,11 @@ public function save_meta_fields( $user_id ) {
}

update_user_meta( $user_id, 'dokan_profile_settings', $store_settings );
update_user_meta( $user_id, 'dokan_enable_selling', $selling );
if ( 'yes' === $selling ) {
dokan()->vendor->active( $user_id );
} else {
dokan()->vendor->inactive( $user_id );
}
update_user_meta( $user_id, 'dokan_publishing', $publishing );
update_user_meta( $user_id, 'dokan_admin_percentage', wc_format_decimal( $percentage ) );
update_user_meta( $user_id, 'dokan_admin_percentage_type', $percentage_type );
Expand Down
3 changes: 2 additions & 1 deletion includes/DependencyManagement/Providers/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ServiceProvider extends BootableServiceProvider {
public const TAG = 'container-service';

protected $services = [
'product_block' => \WeDevs\Dokan\Blocks\ProductBlock::class,
'product_block' => \WeDevs\Dokan\Blocks\ProductBlock::class,
'pageview' => \WeDevs\Dokan\PageViews::class,
'seller_wizard' => \WeDevs\Dokan\Vendor\SetupWizard::class,
'core' => \WeDevs\Dokan\Core::class,
Expand Down Expand Up @@ -62,6 +62,7 @@ public function boot(): void {
$this->getContainer()->addServiceProvider( new FrontendServiceProvider() );
$this->getContainer()->addServiceProvider( new AjaxServiceProvider() );
$this->getContainer()->addServiceProvider( new AnalyticsServiceProvider() );
$this->getContainer()->addServiceProvider( new UtilsServiceProvider() );
}

/**
Expand Down
26 changes: 26 additions & 0 deletions includes/DependencyManagement/Providers/UtilsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WeDevs\Dokan\DependencyManagement\Providers;

use WeDevs\Dokan\DependencyManagement\BaseServiceProvider;

class UtilsServiceProvider extends BaseServiceProvider {
/**
* Tag for services added to the container.
*/
protected $tags = [ 'utils' ];

protected $services = [
\WeDevs\Dokan\Utilities\AdminSettings::class,
];

/**
* Register the classes.
*/
public function register(): void {
foreach ( $this->services as $service ) {
$definition = $this->getContainer()->add( $service );
$this->add_tags( $definition, $this->tags );
}
}
}
47 changes: 47 additions & 0 deletions includes/Utilities/AdminSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace WeDevs\Dokan\Utilities;

class AdminSettings {

/**
* Get new seller selling status setting.
* We are placing this function here because this function may access from admin and front-end both.
*
* @since DOKAN_SINCE
*
* @param string $status
*
* @return string
*/
public function get_new_seller_enable_selling_status( $status = '' ) {
// Before this feature the default was 'on'
if ( empty( $status ) ) {
$status = dokan_get_option( 'new_seller_enable_selling', 'dokan_selling', 'on' );
}

if ( $status === 'on' ) {
$status = 'automatically';
} elseif ( $status === 'off' ) {
$status = 'manually';
}

return apply_filters( 'dokan_new_seller_enable_selling_status', $status );
}

/**
* Dokan new seller enable selling statuses.
*
* @since DOKAN_SINCE
*
* @return array
*/
public function new_seller_enable_selling_statuses() {
return apply_filters(
'dokan_new_seller_enable_selling_statuses', [
'automatically' => __( 'Automatically', 'dokan-lite' ),
'manually' => __( 'Manually', 'dokan-lite' ),
]
);
}
}
34 changes: 33 additions & 1 deletion includes/Vendor/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function create( $data = [] ) {

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

if ( isset( $data['featured'] ) && dokan_validate_boolean( $data['featured'] ) ) {
Expand Down Expand Up @@ -502,4 +502,36 @@ public function get_featured( $args = [] ) {

return $this->get_vendors( $args );
}

/**
* Enable a vendor.
*
* @since DOKAN_SINCE
*
* @param $vendor_id
*
* @return array
*/
public function active( $vendor_id ) {
$vendor = new Vendor();
$vendor->id = $vendor_id;

return $vendor->make_active();
}

/**
* Disable a vendor.
*
* @since DOKAN_SINCE
*
* @param $vendor_id
*
* @return array
*/
public function inactive( $vendor_id ) {
$vendor = new Vendor();
$vendor->id = $vendor_id;

return $vendor->make_inactive();
}
}
12 changes: 10 additions & 2 deletions includes/Vendor/Vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,11 @@ public function make_active() {

do_action( 'dokan_vendor_enabled', $this->get_id() );

return $this->to_array();
if ( ! empty( $this->data ) ) {
return $this->to_array();
}

return [];
}

/**
Expand All @@ -967,7 +971,11 @@ public function make_inactive() {

do_action( 'dokan_vendor_disabled', $this->get_id() );

return $this->to_array();
if ( ! empty( $this->data ) ) {
return $this->to_array();
}

return [];
}

/**
Expand Down
13 changes: 5 additions & 8 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1378,11 +1378,8 @@ function dokan_admin_user_register( $user_id ) {
$role = reset( $user->roles );

if ( $role === 'seller' ) {
if ( dokan_get_option( 'new_seller_enable_selling', 'dokan_selling' ) === 'off' ) {
update_user_meta( $user_id, 'dokan_enable_selling', 'no' );
} else {
update_user_meta( $user_id, 'dokan_enable_selling', 'yes' );
}
$enabled = 'automatically' === dokan_get_container()->get( \WeDevs\Dokan\Utilities\AdminSettings::class )->get_new_seller_enable_selling_status();
$enabled ? dokan()->vendor->active( $user_id ) : dokan()->vendor->inactive( $user_id );
}
}

Expand Down Expand Up @@ -4245,10 +4242,10 @@ function dokan_user_update_to_seller( $user, $data ) {
$vendor->set_address( $data['address'] );
$vendor->save();

if ( 'off' === dokan_get_option( 'new_seller_enable_selling', 'dokan_selling', 'on' ) ) {
$vendor->make_inactive();
} else {
if ( 'automatically' === dokan_get_container()->get( \WeDevs\Dokan\Utilities\AdminSettings::class )->get_new_seller_enable_selling_status() ) {
$vendor->make_active();
} else {
$vendor->make_inactive();
}

update_user_meta( $user_id, 'dokan_publishing', 'no' );
Expand Down
14 changes: 8 additions & 6 deletions templates/admin-setup-wizard/step-selling.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
<tr>
<th scope="row"><label for="new_seller_enable_selling"><?php esc_html_e( 'New Vendor Enable Selling', 'dokan-lite' ); ?></label></th>
<td>
<input type="checkbox" name="new_seller_enable_selling" id="new_seller_enable_selling" class="switch-input" <?php checked( $new_seller_enable_selling, 'on', true ); ?>>
<label for="new_seller_enable_selling" class="switch-label">
<span class="toggle--on"><?php esc_html_e( 'On', 'dokan-lite' ); ?></span>
<span class="toggle--off"><?php esc_html_e( 'Off', 'dokan-lite' ); ?></span>
</label>
<select class="wc-enhanced-select" id="new_seller_enable_selling" name="new_seller_enable_selling">
<?php foreach ( $new_seller_enable_selling_statuses as $key => $value ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $new_seller_enable_selling ); ?>>
<?php echo esc_html( $value ); ?>
</option>
<?php endforeach; ?>
</select>
<span class="description">
<?php esc_html_e( 'Make selling status enable for new registred vendor', 'dokan-lite' ); ?>
<?php esc_html_e( 'Set selling status for new registered vendor', 'dokan-lite' ); ?>
</span>
</td>
</tr>
Expand Down
Loading
Loading