Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 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\Assets;
use WeDevs\Dokan\Utilities\AdminSettings;

/**
Expand Down Expand Up @@ -151,12 +152,14 @@
if ( $require_dompurify && ! wp_script_is( 'dompurify', 'registered' ) ) {
wp_register_script( 'dompurify', WC()->plugin_url() . '/assets/js/dompurify/purify' . $suffix . '.js', array(), WC()->version, false );
}
$jquery_blockui = Assets::get_wc_handler( 'jquery-blockui' );
$jquery_tiptip = Assets::get_wc_handler( 'jquery-tiptip' );

if ( ! wp_script_is( 'jquery-tiptip', 'registered' ) ) {
wp_register_script( 'jquery-tiptip', WC()->plugin_url() . '/assets/js/jquery-tiptip/jquery.tipTip.min.js', $require_dompurify ? [ 'jquery', 'dompurify' ] : [ 'jquery' ], WC()->version, true );
if ( ! wp_script_is( $jquery_tiptip, 'registered' ) ) {
wp_register_script( $jquery_tiptip, WC()->plugin_url() . '/assets/js/jquery-tiptip/jquery.tipTip.min.js', $require_dompurify ? [ 'jquery', 'dompurify' ] : [ 'jquery' ], WC()->version, true );
}

wp_register_script( 'wc-setup', WC()->plugin_url() . '/assets/js/admin/wc-setup.min.js', [ 'jquery', 'wc-enhanced-select', 'jquery-blockui', 'wp-util', 'jquery-tiptip', 'dokan-util-helper' ], WC_VERSION, true );
wp_register_script( 'wc-setup', WC()->plugin_url() . '/assets/js/admin/wc-setup.min.js', [ 'jquery', 'wc-enhanced-select', $jquery_blockui, 'wp-util', $jquery_tiptip, 'dokan-util-helper' ], WC_VERSION, true );

wp_localize_script(
'wc-setup',
Expand Down Expand Up @@ -655,7 +658,7 @@
$options['admin_percentage'] = $dokan_commission_percentage;
$options['additional_fee'] = isset( $_POST['dokan_commission_flat'] ) ? sanitize_text_field( wp_unslash( $_POST['dokan_commission_flat'] ) ) : 0;
$options['commission_category_based_values'] = isset( $_POST['dokan_commission_category_based'] ) ? wc_clean( json_decode( sanitize_text_field( wp_unslash( $_POST['dokan_commission_category_based'] ) ), true ) ) : [];
$options['reset_sub_category_when_edit_all_category'] = isset( $_POST['reset_sub_category'] ) && false === dokan_string_to_bool( $_POST['reset_sub_category'] ) ? 'off' : 'on';

Check failure on line 661 in includes/Admin/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Detected usage of a non-sanitized input variable: $_POST['reset_sub_category']

Check failure on line 661 in includes/Admin/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

$_POST['reset_sub_category'] not unslashed before sanitization. Use wp_unslash() or similar

update_option( 'dokan_selling', $options );

Expand Down
6 changes: 4 additions & 2 deletions includes/Admin/SetupWizardNoWC.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WeDevs\Dokan\Admin;

use WeDevs\Dokan\Admin\SetupWizard as DokanSetupWizard;
use WeDevs\Dokan\Assets;

class SetupWizardNoWC extends DokanSetupWizard {

Expand Down Expand Up @@ -42,11 +43,12 @@ protected function should_show_recommended_step() {
* @return void
*/
public function enqueue_scripts() {
wp_register_script( 'jquery-blockui', DOKAN_PLUGIN_ASSEST . '/vendors/jquery-blockui/jquery.blockUI.min.js', [ 'jquery' ], '2.70', true );
$jquery_blockui = Assets::get_wc_handler( 'jquery-blockui' );
wp_register_script( $jquery_blockui, DOKAN_PLUGIN_ASSEST . '/vendors/jquery-blockui/jquery.blockUI.min.js', [ 'jquery' ], '2.70', true );

wp_enqueue_style( 'dokan-setup', DOKAN_PLUGIN_ASSEST . '/css/setup-no-wc-style.css', [ 'install' ], DOKAN_PLUGIN_VERSION );

wp_enqueue_script( 'wc-setup', DOKAN_PLUGIN_ASSEST . '/js/dokan-setup-no-wc.js', [ 'jquery', 'jquery-blockui' ], DOKAN_PLUGIN_VERSION, true );
wp_enqueue_script( 'wc-setup', DOKAN_PLUGIN_ASSEST . '/js/dokan-setup-no-wc.js', [ 'jquery', $jquery_blockui ], DOKAN_PLUGIN_VERSION, true );
wp_localize_script(
'wc-setup',
'wc_setup_params',
Expand Down
19 changes: 16 additions & 3 deletions includes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
}
}

public static function get_wc_handler( $handler ): string {
// map legacy handlers to new ones
$handlers = [
'jquery-blockui' => 'jquery-blockui',
'jquery-tiptip' => 'jquery-tiptip',
];
if ( version_compare( WC()->version, '10.3.0', '>=' ) ) {
return 'wc-' . $handlers[ $handler ];
}
Comment on lines +38 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did not check the $handlers[ $handler ]; isset or not.

Pls check the coderabbit suggestion.

return $handlers[ $handler ] ?? $handler;
}
Comment on lines +32 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Fix undefined array key access and refactor handler mapping.

The current implementation has a critical bug when an unknown handler is passed with WooCommerce >= 10.3.0. On line 39, $handlers[$handler] will trigger an undefined array key warning if the handler isn't in the mapping, and concatenating with 'wc-' will produce an invalid handle like 'wc-' (empty suffix).

Additionally, the mapping array is redundant since keys and values are identical—it's not actually "mapping" anything, just whitelisting known handlers.

Apply this refactored implementation:

 public static function get_wc_handler( $handler ): string {
-    // map legacy handlers to new ones
-    $handlers = [
+    // Whitelist of WC handlers that require version-specific prefixing
+    $supported_handlers = [
         'jquery-blockui' => 'jquery-blockui',
         'jquery-tiptip' => 'jquery-tiptip',
     ];
+    
+    // Return original handler if not in our supported list
+    if ( ! isset( $supported_handlers[ $handler ] ) ) {
+        return $handler;
+    }
+    
+    // For WC 10.3.0+, use 'wc-' prefix
     if ( version_compare( WC()->version, '10.3.0', '>=' ) ) {
-        return 'wc-' . $handlers[ $handler ];
+        return 'wc-' . $supported_handlers[ $handler ];
     }
-    return $handlers[ $handler ] ?? $handler;
+    
+    return $supported_handlers[ $handler ];
 }

Alternatively, simplify further by using an array of strings:

 public static function get_wc_handler( $handler ): string {
-    // map legacy handlers to new ones
-    $handlers = [
-        'jquery-blockui' => 'jquery-blockui',
-        'jquery-tiptip' => 'jquery-tiptip',
-    ];
+    // Whitelist of WC handlers that require version-specific prefixing
+    $supported_handlers = [ 'jquery-blockui', 'jquery-tiptip' ];
+    
+    // Return original handler if not in our supported list
+    if ( ! in_array( $handler, $supported_handlers, true ) ) {
+        return $handler;
+    }
+    
+    // For WC 10.3.0+, use 'wc-' prefix
     if ( version_compare( WC()->version, '10.3.0', '>=' ) ) {
-        return 'wc-' . $handlers[ $handler ];
+        return 'wc-' . $handler;
     }
-    return $handlers[ $handler ] ?? $handler;
+    
+    return $handler;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static function get_wc_handler( $handler ): string {
// map legacy handlers to new ones
$handlers = [
'jquery-blockui' => 'jquery-blockui',
'jquery-tiptip' => 'jquery-tiptip',
];
if ( version_compare( WC()->version, '10.3.0', '>=' ) ) {
return 'wc-' . $handlers[ $handler ];
}
return $handlers[ $handler ] ?? $handler;
}
public static function get_wc_handler( $handler ): string {
// Whitelist of WC handlers that require version-specific prefixing
$supported_handlers = [
'jquery-blockui' => 'jquery-blockui',
'jquery-tiptip' => 'jquery-tiptip',
];
// Return original handler if not in our supported list
if ( ! isset( $supported_handlers[ $handler ] ) ) {
return $handler;
}
// For WC 10.3.0+, use 'wc-' prefix
if ( version_compare( WC()->version, '10.3.0', '>=' ) ) {
return 'wc-' . $supported_handlers[ $handler ];
}
return $supported_handlers[ $handler ];
}
🤖 Prompt for AI Agents
In includes/Assets.php around lines 32-42, replace the current associative
$handlers usage that accesses $handlers[$handler] (causing undefined key
warnings) with a whitelist array of handler names (simple strings) and guard the
WC version branch with an existence check; specifically, create an array like
$allowed = ['jquery-blockui','jquery-tiptip']; then if
version_compare(WC()->version,'10.3.0','>=' ) return 'wc-'.$handler only when
in_array($handler,$allowed,true) else return $handler; for older versions just
return $handler (or return $handler when not in the whitelist) so no undefined
array key access occurs and the mapping is simplified.


/**
* Load global admin and promo notices scripts
*
Expand Down Expand Up @@ -163,7 +175,7 @@
public function get_localized_price() {
return [
'precision' => wc_get_price_decimals(),
'symbol' => html_entity_decode( get_woocommerce_currency_symbol() ),

Check failure on line 178 in includes/Assets.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

The default value of the $flags parameter for html_entity_decode() was changed from ENT_COMPAT to ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 in PHP 8.1. For cross-version compatibility, the $flags parameter should be explicitly set.
'decimal' => esc_attr( wc_get_price_decimal_separator() ),
'thousand' => esc_attr( wc_get_price_thousand_separator() ),
'position' => esc_attr( get_option( 'woocommerce_currency_pos' ) ),
Expand Down Expand Up @@ -376,6 +388,7 @@
*/
public function get_scripts() {
global $wp_version;
$jquery_tiptip = self::get_wc_handler( 'jquery-tiptip' );

$frontend_shipping_asset = require DOKAN_DIR . '/assets/js/frontend.asset.php';

Expand Down Expand Up @@ -493,7 +506,7 @@
],
'dokan-script' => [
'src' => $asset_url . '/js/dokan.js',
'deps' => [ 'imgareaselect', 'customize-base', 'customize-model', 'wp-i18n', 'jquery-tiptip', 'moment', 'dokan-date-range-picker', 'dokan-accounting' ],
'deps' => [ 'imgareaselect', 'customize-base', 'customize-model', 'wp-i18n', $jquery_tiptip, 'moment', 'dokan-date-range-picker', 'dokan-accounting' ],
'version' => filemtime( $asset_path . 'js/dokan.js' ),
],
'dokan-vue-vendor' => [
Expand Down Expand Up @@ -584,8 +597,8 @@
];
}

if ( ! wp_script_is( 'jquery-tiptip', 'registered' ) ) {
$scripts['jquery-tiptip'] = [
if ( ! wp_script_is( $jquery_tiptip, 'registered' ) ) {
$scripts[ $jquery_tiptip ] = [
'src' => WC()->plugin_url() . '/assets/js/jquery-tiptip/jquery.tipTip' . $suffix . '.js',
'deps' => $require_dompurify ? [ 'jquery', 'dompurify' ] : [ 'jquery' ],
];
Expand Down
7 changes: 5 additions & 2 deletions includes/Vendor/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WC_Countries;
use WeDevs\Dokan\Admin\SetupWizard as DokanSetupWizard;
use WeDevs\Dokan\Assets;

/**
* Seller setup wizard class
Expand Down Expand Up @@ -100,11 +101,13 @@ public function setup_wizard() {
* @return void
*/
public function frontend_enqueue_scripts() {
$jquery_blockui = Assets::get_wc_handler( 'jquery-blockui' );
$jquery_tiptip = Assets::get_wc_handler( 'jquery-tiptip' );
wp_enqueue_style( 'jquery-ui' );
wp_enqueue_emoji_styles();
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-tiptip' );
wp_enqueue_script( 'jquery-blockui' );
wp_enqueue_script( $jquery_tiptip );
wp_enqueue_script( $jquery_blockui );
wp_enqueue_script( 'jquery-ui-autocomplete' );
wp_enqueue_script( 'wc-enhanced-select' );

Expand Down
Loading