Skip to content

Commit ee671ed

Browse files
fix: add WooCommerce request verification token
1 parent 1be891c commit ee671ed

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

classes/Visualizer/Source/Json.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ function( $headers ) {
457457
}
458458
}
459459

460+
// Check if this is a WooCommerce endpoint request and add verification token.
461+
if ( $this->is_woocommerce_request( $url ) ) {
462+
// Generate a unique token for this specific request.
463+
$token = wp_generate_password( 32, false );
464+
set_transient( 'visualizer_wc_token_' . $token, time(), 60 );
465+
if ( ! isset( $args['headers'] ) ) {
466+
$args['headers'] = array();
467+
}
468+
$args['headers']['X-Visualizer-Token'] = $token;
469+
}
470+
460471
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Connecting to %s with args = %s ', $url, print_r( $args, true ) ), 'debug', __FILE__, __LINE__ );
461472
return wp_remote_request( $url, $args );
462473
}
@@ -488,6 +499,31 @@ public function refresh( $series ) {
488499
return true;
489500
}
490501

502+
/**
503+
* Check if the URL is a WooCommerce endpoint request.
504+
*
505+
* @access private
506+
* @param string $url The URL to check.
507+
* @return bool True if it's a WooCommerce request, false otherwise.
508+
*/
509+
private function is_woocommerce_request( $url ) {
510+
// Check if the URL contains WooCommerce API patterns.
511+
$wc_patterns = array(
512+
'/wp-json/wc/',
513+
'/wc-analytics/',
514+
'/wc/v',
515+
'/reports/',
516+
);
517+
518+
foreach ( $wc_patterns as $pattern ) {
519+
if ( strpos( $url, $pattern ) !== false ) {
520+
return true;
521+
}
522+
}
523+
524+
return false;
525+
}
526+
491527
/**
492528
* Returns source name.
493529
*

0 commit comments

Comments
 (0)