Query.php
52 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Reports\Categories; |
| 4 | |
| 5 | use WCML\Rest\Functions; |
| 6 | |
| 7 | class Query implements \IWPML_REST_Action, \IWPML_Backend_Action { |
| 8 | |
| 9 | var $actionWasRemoved = false; |
| 10 | |
| 11 | public function add_hooks() { |
| 12 | if ( Functions::isAnalyticsRestRequest() ) { |
| 13 | add_filter( 'woocommerce_analytics_categories_select_query', [ $this, 'translateCategoryTitles' ] ); |
| 14 | } |
| 15 | |
| 16 | add_action( 'generate_category_lookup_table', [ $this, 'removeWpmlTermClausesFilter' ], 0 ); |
| 17 | add_action( 'generate_category_lookup_table', [ $this, 'addWpmlTermClausesFilter' ], 20 ); |
| 18 | } |
| 19 | |
| 20 | public function translateCategoryTitles( $results ) { |
| 21 | $results->data = wpml_collect( $results->data ) |
| 22 | ->map( function( $row ) { |
| 23 | if ( $row['extended_info']['name'] ) { |
| 24 | $row['category_id'] = apply_filters( 'wpml_object_id', $row['category_id'], 'product_cat', true ); |
| 25 | $term = get_term( $row['category_id'] ); |
| 26 | if ( $term ) { |
| 27 | $row['extended_info']['name'] = $term->name; |
| 28 | } |
| 29 | } |
| 30 | return $row; |
| 31 | } )->toArray(); |
| 32 | |
| 33 | return $results; |
| 34 | } |
| 35 | |
| 36 | public function removeWpmlTermClausesFilter() { |
| 37 | global $sitepress; |
| 38 | |
| 39 | $this->actionWasRemoved = remove_filter( 'terms_clauses', [ $sitepress, 'terms_clauses' ] ); |
| 40 | } |
| 41 | |
| 42 | public function addWpmlTermClausesFilter() { |
| 43 | global $sitepress; |
| 44 | |
| 45 | if ( $this->actionWasRemoved ) { |
| 46 | $this->actionWasRemoved = false; |
| 47 | add_filter( 'terms_clauses', [ $sitepress, 'terms_clauses' ], 10, 3 ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | } |
| 52 |