Passed
Push — master ( 0db704...ff60ed )
by Chris
04:09
created

classes/class-facetwp.php (32 issues)

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() {
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
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
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
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 ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
49
			self::$instance = new self();
50
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
51
		return self::$instance;
52
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
53
54
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$rows" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$params" missing
Loading history...
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'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
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 ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
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( lsx_currencies()->frontend->rates ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
67
						$row_currency     = $additional_prices[0]['currency'];
68
						$row_value        = $additional_prices[0]['amount'];
69
						$current_currency = lsx_currencies()->frontend->current_currency;
70
						$usd_value        = $row_value / lsx_currencies()->frontend->rates->$row_currency;
71
						if ( $row_currency !== $current_currency ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
72
							$usd_value = $usd_value * lsx_currencies()->frontend->rates->$current_currency;
73
						}
0 ignored issues
show
No blank line found after control structure
Loading history...
74
						$new_row                        = $params['defaults'];
75
						$new_row['facet_value']         = round( $usd_value, 0 );
76
						$new_row['facet_display_value'] = round( $usd_value, 0 );
77
						$rows[]                         = $new_row;
78
					}
79
				}
80
				break;
81
82
			default:
83
				break;
84
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
85
		return $rows;
86
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
87
88
	/**
89
	 * This will refresh the saved currencies that ar not the same as the base currency.
90
	 *
91
	 * @return void
92
	 */
93
	public function refresh_the_currencies() {
94
		if ( true === lsx_currencies()->convert_to_single ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
95
			add_action( 'wp_footer', array( $this, 'trigger_the_index' ) );
96
		}
97
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
98
99
	/**
100
	 * Grabs the tour ids and runs them through the index.
101
	 *
102
	 * @return void
103
	 */
104
	public function trigger_the_index() {
105
		$tours_args  = array(
106
			'post_type'      => 'tour',
107
			'post_status'    => 'publish',
108
			'posts_per_page' => '-1',
109
			'nopagin'        => true,
110
			'fields'         => 'ids',
111
		);
112
		$tours_query = new \WP_Query( $tours_args );
113
		if ( $tours_query->have_posts() ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
114
			foreach ( $tours_query->posts as $tour_id ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
115
				FWP()->indexer->index( $tour_id );
116
			}
117
		}
118
	}
0 ignored issues
show
Expected 2 blank lines after function; 0 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
119
}
120