Passed
Push — add/multiplan ( e2cb64...80e770 )
by Virginia
07:47
created

Checkout::payment_gateway_logos()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.8333
1
<?php
2
namespace lsx_health_plan\classes\integrations\woocommerce;
3
4
/**
5
 * Contains the downloads functions post type
6
 *
7
 * @package lsx-health-plan
8
 */
9
class Checkout {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @since 1.0.0
15
	 *
16
	 * @var      object \lsx_health_plan\classes\integrations\woocommerce\Checkout()
17
	 */
18
	protected static $instance = null;
19
20
	/**
21
	 * @var string
22
	 */
23
	public $plan_id = '';
24
25
	/**
26
	 * Constructor
27
	 */
28
	public function __construct() {
29
		add_filter( 'woocommerce_order_button_text', array( $this, 'checkout_button_text' ), 10, 1 );
30
31
		// Cart Messages.
32
		add_action( 'lsx_content_wrap_before', array( $this, 'cart_notices' ) );
33
		add_filter( 'wc_add_to_cart_message_html', array( $this, 'add_to_cart_message' ), 10, 3 );
34
	}
35
36
	/**
37
	 * Return an instance of this class.
38
	 *
39
	 * @since 1.0.0
40
	 *
41
	 * @return    object \lsx_health_plan\classes\integrations\woocommerce\Checkout()    A single instance of this class.
42
	 */
43
	public static function get_instance() {
44
		// If the single instance hasn't been set, set it now.
45
		if ( null === self::$instance ) {
46
			self::$instance = new self();
47
		}
48
		return self::$instance;
49
	}
50
51
	/**
52
	 * Return the Place Order Text
53
	 *
54
	 * @param string $label
55
	 * @return void
56
	 */
57
	public function checkout_button_text( $label = '' ) {
58
		$label = __( 'Place order', 'lsx-health-plan' );
59
		return $label;
60
	}
61
62
	/**
63
	 * Saves the Plan ID to the cart data, so we can attach it to the order later.
64
	 *
65
	 * @param array $cart_item_data
66
	 * @param string $product_id
67
	 * @param string $variation_id
68
	 * @return void
69
	 */
70
	public function add_plan_id_to_cart( $cart_item_data, $product_id, $variation_id ) {
71
		$plan_id = filter_input( INPUT_GET, 'plan_id' );
72
		if ( empty( $plan_id ) || '' === $plan_id ) {
73
			return $cart_item_data;
74
		}
75
		$cart_item_data['plan_id'] = $plan_id;
76
		return $cart_item_data;
77
	}
78
79
	/**
80
	 * Output the WooCommerce Cart Notices.
81
	 *
82
	 * @return void
83
	 */
84
	public function cart_notices() {
85
		if ( function_exists( 'woocommerce_output_all_notices' ) && is_post_type_archive( 'plan' ) ) {
86
			echo wp_kses_post( '<div class="col-md-12 col-sm-12 woocommerce-notices-wrapper">' );
87
			wc_print_notices();
0 ignored issues
show
Bug introduced by
The function wc_print_notices 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

87
			/** @scrutinizer ignore-call */ 
88
   wc_print_notices();
Loading history...
88
			echo wp_kses_post( '</div>' );
89
		}
90
	}
91
92
	/**
93
	 * Changes the add to cart message and adds our course name.
94
	 *
95
	 * @param  string  $message
96
	 * @param  array   $products
97
	 * @param  boolean $show_qty
98
	 * @return string
99
	 */
100
	public function add_to_cart_message( $message, $products, $show_qty ) {
101
		if ( isset( $_GET['plan_id'] ) ) { // @codingStandardsIgnoreLine.
102
			$this->plan_id = sanitize_text_field( wp_slash( $_GET['plan_id'] ) ); // @codingStandardsIgnoreLine.
0 ignored issues
show
Bug introduced by
It seems like wp_slash($_GET['plan_id']) can also be of type string[]; however, parameter $str of sanitize_text_field() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

102
			$this->plan_id = sanitize_text_field( /** @scrutinizer ignore-type */ wp_slash( $_GET['plan_id'] ) ); // @codingStandardsIgnoreLine.
Loading history...
103
104
			$title = '<strong>' . get_the_title( $this->plan_id ) . '</strong>';
0 ignored issues
show
Bug introduced by
$this->plan_id of type string is incompatible with the type WP_Post|integer expected by parameter $post of get_the_title(). ( Ignorable by Annotation )

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

104
			$title = '<strong>' . get_the_title( /** @scrutinizer ignore-type */ $this->plan_id ) . '</strong>';
Loading history...
105
			$title = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', 1, 'lsx-health-plan' ), $title );
106
107
			// Output success messages.
108
			if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
109
				$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
0 ignored issues
show
Bug introduced by
The function wc_get_page_permalink 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

109
				$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : /** @scrutinizer ignore-call */ wc_get_page_permalink( 'shop' ) );
Loading history...
Bug introduced by
false of type false is incompatible with the type string expected by parameter $default of wp_validate_redirect(). ( Ignorable by Annotation )

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

109
				$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), /** @scrutinizer ignore-type */ false ) : wc_get_page_permalink( 'shop' ) );
Loading history...
Bug introduced by
The function wc_get_raw_referer 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

109
				$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', /** @scrutinizer ignore-call */ wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
Loading history...
110
				$message   = sprintf( '<a href="%s" tabindex="1" class="btn button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'lsx-health-plan' ), $title );
111
			} else {
112
				$message = sprintf( '<a href="%s" tabindex="1" class="btn button wc-forward">%s</a> %s', esc_url( wc_get_cart_url() ), esc_html__( 'View cart', 'lsx-health-plan' ), $title );
0 ignored issues
show
Bug introduced by
The function wc_get_cart_url 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

112
				$message = sprintf( '<a href="%s" tabindex="1" class="btn button wc-forward">%s</a> %s', esc_url( /** @scrutinizer ignore-call */ wc_get_cart_url() ), esc_html__( 'View cart', 'lsx-health-plan' ), $title );
Loading history...
113
			}
114
		}
115
		return $message;
116
	}
117
}
118