Hooks.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\AdminDashboard; |
| 4 | |
| 5 | use WPML\Core\ISitePress; |
| 6 | use WCML\StandAlone\NullSitePress; |
| 7 | use SitePress; |
| 8 | use wpdb; |
| 9 | use IWPML_DIC_Action; |
| 10 | use IWPML_Backend_Action; |
| 11 | |
| 12 | class Hooks implements IWPML_Backend_Action, IWPML_DIC_Action { |
| 13 | |
| 14 | private $sitepress; |
| 15 | |
| 16 | private $wpdb; |
| 17 | |
| 18 | public function __construct( ISitePress $sitepress, wpdb $wpdb ) { |
| 19 | $this->sitepress = $sitepress; |
| 20 | $this->wpdb = $wpdb; |
| 21 | } |
| 22 | |
| 23 | public function add_hooks() { |
| 24 | add_action( 'wp_dashboard_setup', [ $this, 'clearStockTransients' ] ); |
| 25 | add_filter( 'woocommerce_status_widget_low_in_stock_count_query', [ $this, 'addLanguageQuery' ] ); |
| 26 | add_filter( 'woocommerce_status_widget_out_of_stock_count_query', [ $this, 'addLanguageQuery' ] ); |
| 27 | } |
| 28 | |
| 29 | public function clearStockTransients() { |
| 30 | delete_transient( 'wc_outofstock_count' ); |
| 31 | delete_transient( 'wc_low_stock_count' ); |
| 32 | } |
| 33 | |
| 34 | public function addLanguageQuery( $query ) { |
| 35 | |
| 36 | $currentLanguage = $this->sitepress->get_current_language(); |
| 37 | |
| 38 | if ( $currentLanguage !== 'all' ) { |
| 39 | $languageQuery = $this->wpdb->prepare( |
| 40 | " INNER JOIN {$this->wpdb->prefix}icl_translations AS t |
| 41 | ON posts.ID = t.element_id AND t.element_type IN ( 'post_product', 'post_product_variation' ) |
| 42 | WHERE t.language_code = %s AND ", |
| 43 | $currentLanguage ); |
| 44 | |
| 45 | return str_replace( 'WHERE', $languageQuery, $query ); |
| 46 | } |
| 47 | |
| 48 | return $query; |
| 49 | } |
| 50 | } |
| 51 |