1 | <?php |
||
2 | /** |
||
3 | * LSX Currency FacetWP Class |
||
4 | * |
||
5 | * @package LSX Currencies |
||
6 | * @author LightSpeed |
||
7 | * @license GPL3 |
||
8 | * @link |
||
9 | * @copyright 2019 LightSpeed |
||
10 | */ |
||
11 | |||
12 | namespace lsx\currencies\classes; |
||
13 | |||
14 | /** |
||
15 | * Holds the WooCommerce Integrations |
||
16 | */ |
||
17 | class FacetWP { |
||
18 | |||
19 | /** |
||
20 | * Holds instance of the class |
||
21 | * |
||
22 | * @var object \lsx\currencies\classes\FacetWP() |
||
23 | */ |
||
24 | private static $instance; |
||
25 | |||
26 | /** |
||
27 | * Holds the current currency. |
||
28 | * |
||
29 | * @var boolean |
||
30 | */ |
||
31 | public $currency = false; |
||
32 | |||
33 | /** |
||
34 | * Constructor |
||
35 | */ |
||
36 | public function __construct() { |
||
37 | add_filter( 'facetwp_indexer_row_data', array( $this, 'facetwp_index_row_data' ), 20, 2 ); |
||
38 | add_action( 'lsx_currencies_rates_refreshed', array( $this, 'refresh_the_currencies' ), 20 ); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Return an instance of this class. |
||
43 | * |
||
44 | * @return object |
||
45 | */ |
||
46 | public static function init() { |
||
47 | // If the single instance hasn't been set, set it now. |
||
48 | if ( ! isset( self::$instance ) ) { |
||
49 | self::$instance = new self(); |
||
50 | } |
||
51 | return self::$instance; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Alter the rows and include extra facets rows for the continents. |
||
56 | */ |
||
57 | public function facetwp_index_row_data( $rows, $params ) { |
||
58 | switch ( $params['facet']['source'] ) { |
||
59 | case 'cf/price': |
||
60 | // only convert a price to the base currency if the setting is active. |
||
61 | // If $rows is empty then there is no base currency set. |
||
62 | if ( true === lsx_currencies()->convert_to_single && empty( $rows ) ) { |
||
63 | lsx_currencies()->frontend->set_defaults(); |
||
64 | $additional_prices = get_post_meta( $params['defaults']['post_id'], 'additional_prices', false ); |
||
65 | |||
66 | if ( ! empty( $additional_prices ) && isset( $additional_prices[0] ) && ! empty( $additional_prices[0] ) && ! empty( lsx_currencies()->frontend->rates ) ) { |
||
67 | $row_currency = $additional_prices[0]['currency']; |
||
68 | $row_value = (int) $additional_prices[0]['amount']; |
||
69 | if ( '' !== $row_value && null !== $row_value ) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
70 | $current_currency = lsx_currencies()->frontend->current_currency; |
||
71 | $usd_value = $row_value / lsx_currencies()->frontend->rates->$row_currency; |
||
72 | if ( $row_currency !== $current_currency ) { |
||
73 | $usd_value = $usd_value * lsx_currencies()->frontend->rates->$current_currency; |
||
74 | } |
||
75 | $new_row = $params['defaults']; |
||
76 | $new_row['facet_value'] = round( $usd_value, 0 ); |
||
77 | $new_row['facet_display_value'] = round( $usd_value, 0 ); |
||
78 | $rows[] = $new_row; |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | break; |
||
83 | |||
84 | default: |
||
85 | break; |
||
86 | } |
||
87 | return $rows; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * This will refresh the saved currencies that ar not the same as the base currency. |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public function refresh_the_currencies() { |
||
96 | if ( true === lsx_currencies()->convert_to_single ) { |
||
97 | add_action( 'wp_footer', array( $this, 'trigger_the_index' ) ); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Grabs the tour ids and runs them through the index. |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function trigger_the_index() { |
||
107 | $tours_args = array( |
||
108 | 'post_type' => 'tour', |
||
109 | 'post_status' => 'publish', |
||
110 | 'posts_per_page' => '-1', |
||
111 | 'nopagin' => true, |
||
112 | 'fields' => 'ids', |
||
113 | ); |
||
114 | $tours_query = new \WP_Query( $tours_args ); |
||
115 | if ( $tours_query->have_posts() ) { |
||
116 | foreach ( $tours_query->posts as $tour_id ) { |
||
117 | FWP()->indexer->index( $tour_id ); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 |