Issues (575)

classes/class-facetwp.php (38 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( $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'];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 1 space but found 5 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68
						$row_value        = (int) $additional_prices[0]['amount'];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 8 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69
						if ( '' !== $row_value && null !== $row_value ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
The condition null !== $row_value is always true.
Loading history...
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 ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
73
								$usd_value = $usd_value * lsx_currencies()->frontend->rates->$current_currency;
74
							}
0 ignored issues
show
No blank line found after control structure
Loading history...
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
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
87
		return $rows;
88
	}
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...
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 ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
97
			add_action( 'wp_footer', array( $this, 'trigger_the_index' ) );
98
		}
99
	}
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...
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() ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
116
			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...
117
				FWP()->indexer->index( $tour_id );
0 ignored issues
show
The function FWP was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
				/** @scrutinizer ignore-call */ 
118
    FWP()->indexer->index( $tour_id );
Loading history...
118
			}
119
		}
120
	}
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...
121
}
122