-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: Add analytics cache modifier for seller analytics filter. (#…
…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
1 parent
07182fa
commit 29a4554
Showing
3 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters