Skip to content

Commit

Permalink
enhance: Add analytics cache modifier for seller analytics filter. (#…
Browse files Browse the repository at this point in the history
…2529)

* enhance: Add seller analytic stats modifier for seller filter.

* fix: phpcs issue for subquery refund method.

* enhance: add filter for analytics query argument entities.

* fix: class name stuff on CacheKeyModifier class constructor.
  • Loading branch information
MdAsifHossainNadim authored Jan 28, 2025
1 parent 07182fa commit 29a4554
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
4 changes: 2 additions & 2 deletions includes/Analytics/Reports/BaseQueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function add_where_subquery( array $clauses ): array {
* @return array The modified where clauses.
*/
protected function add_where_subquery_for_refund( array $clauses ): array {
if ( ! isset( $_GET['refunds'] ) ) {
if ( ! isset( $_GET['refunds'] ) ) { //phpcs:ignore
return $clauses;
}

Expand Down Expand Up @@ -176,6 +176,6 @@ public function get_vendor_id() {
return dokan_get_current_user_id();
}

return (int) ( wp_unslash( $_GET['sellers'] ?? 0 ) ); // phpcs:ignore
return (int) ( wp_unslash( $_GET['sellers'] ?? 0 ) ); // phpcs:ignore
}
}
115 changes: 115 additions & 0 deletions includes/Analytics/Reports/CacheKeyModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace WeDevs\Dokan\Analytics\Reports;

use WeDevs\Dokan\Contracts\Hookable;

/**
* Seller analytics data filter.
*
* @since DOKAN_SINCE
*/
class CacheKeyModifier implements Hookable {

/**
* Report entities to modify.
*
* @since DOKAN_SINCE
*
* @var array
*/
protected array $entities;

/**
* CacheKeyModifier constructor.
* Registers the hooks on instantiation.
*/
public function __construct() {
$this->setup_entities();
$this->register_hooks();
}

/**
* Setup analytics entities
*
* @since DOKAN_SINCE
*
* WC apply filters from @see https://github.com/woocommerce/woocommerce/blob/be602de39d39878085e752f30ec1dabf16b0d642/plugins/woocommerce/src/Admin/API/Reports/GenericQuery.php#L77
* WC reports generation pattern @see https://github.com/woocommerce/woocommerce/blob/be602de39d39878085e752f30ec1dabf16b0d642/plugins/woocommerce/src/Admin/API/Reports/Products/Controller.php#L53
*
* @return void
*/
protected function setup_entities(): void {
$this->entities = apply_filters(
'dokan_analytics_entities_for_query_args',
[
'categories',
'coupons',
'coupons_stats',
'customers',
'downloads',
'downloads_stats',
'orders',
'orders_stats',
'products',
'products_stats',
'revenue',
'taxes',
'taxes_stats',
'variations',
'variations_stats',
]
);
}

/**
* Register necessary hooks.
*
* @since DOKAN_SINCE
*
* @return void
*/
public function register_hooks(): void {
foreach ( $this->entities as $entity ) {
add_filter( "woocommerce_analytics_{$entity}_query_args", [ $this, 'apply_seller_filter' ] );
}
}

/**
* Apply seller filter to query arguments.
*
* Customize the WooCommerce analytics stats datastore to override the $total_query and $interval_query properties.
* This modification replaces the Automattic\WooCommerce\Admin\API\Reports\SqlQuery class with WeDevs\Dokan\Analytics\Reports\WcSqlQuery
* to apply specific filters to queries.
*
* @see https://github.com/woocommerce/woocommerce/tree/trunk/plugins/woocommerce/src/Admin/API/Reports
*
* @param array $args An array of query arguments.
*
* @return array Modified array of query arguments.
*/
public function apply_seller_filter( array $args ): array {
$seller_id = (int) dokan()->get_container()->get( \WeDevs\Dokan\Analytics\Reports\Stock\QueryFilter::class )->get_vendor_id();

// If seller ID is not set, return the original arguments.
if ( ! $this->is_valid_seller_id( $seller_id ) ) {
return $args;
}

$args['seller_id'] = $seller_id;
return $args;
}

/**
* Check if report can be filtered.
*
* @param int $seller_id Seller ID.
*
* @since DOKAN_SINCE
*
* @return bool
*/
protected function is_valid_seller_id( int $seller_id ): bool {
return $seller_id > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AnalyticsServiceProvider extends BaseServiceProvider {
\WeDevs\Dokan\Analytics\Reports\Variations\Stats\QueryFilter::class,
\WeDevs\Dokan\Analytics\Reports\Categories\QueryFilter::class,
\WeDevs\Dokan\Analytics\Reports\DataStoreModifier::class,
\WeDevs\Dokan\Analytics\Reports\CacheKeyModifier::class,
\WeDevs\Dokan\Analytics\Reports\Taxes\QueryFilter::class,
\WeDevs\Dokan\Analytics\Reports\Taxes\Stats\QueryFilter::class,
\WeDevs\Dokan\Analytics\Reports\Coupons\QueryFilter::class,
Expand Down

0 comments on commit 29a4554

Please sign in to comment.