Passed
Push — master ( d37306...43e20b )
by Warwick
04:55
created

Checkout::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
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
		// Thank you page links.
36
		add_filter( 'woocommerce_memberships_thank_you_message', array( $this, 'memberships_thank_you_links' ), 10, 3 );
37
		return (string) apply_filters( 'woocommerce_memberships_thank_you_message', $message, $order_id, $memberships );
38
	}
39
40
	/**
41
	 * Return an instance of this class.
42
	 *
43
	 * @since 1.0.0
44
	 *
45
	 * @return    object \lsx_health_plan\classes\integrations\woocommerce\Checkout()    A single instance of this class.
46
	 */
47
	public static function get_instance() {
48
		// If the single instance hasn't been set, set it now.
49
		if ( null === self::$instance ) {
50
			self::$instance = new self();
51
		}
52
		return self::$instance;
53
	}
54
55
	/**
56
	 * Return the Place Order Text
57
	 *
58
	 * @param string $label
59
	 * @return void
60
	 */
61
	public function checkout_button_text( $label = '' ) {
62
		$label = __( 'Place order', 'lsx-health-plan' );
63
		return $label;
64
	}
65
66
	/**
67
	 * Saves the Plan ID to the cart data, so we can attach it to the order later.
68
	 *
69
	 * @param array $cart_item_data
70
	 * @param string $product_id
71
	 * @param string $variation_id
72
	 * @return void
73
	 */
74
	public function add_plan_id_to_cart( $cart_item_data, $product_id, $variation_id ) {
75
		$plan_id = filter_input( INPUT_GET, 'plan_id' );
76
		if ( empty( $plan_id ) || '' === $plan_id ) {
77
			return $cart_item_data;
78
		}
79
		$cart_item_data['plan_id'] = $plan_id;
80
		return $cart_item_data;
81
	}
82
83
	/**
84
	 * Output the WooCommerce Cart Notices.
85
	 *
86
	 * @return void
87
	 */
88
	public function cart_notices() {
89
		if ( function_exists( 'woocommerce_output_all_notices' ) && is_post_type_archive( 'plan' ) ) {
90
			echo wp_kses_post( '<div class="col-md-12 col-sm-12 woocommerce-notices-wrapper">' );
91
			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

91
			/** @scrutinizer ignore-call */ 
92
   wc_print_notices();
Loading history...
92
			echo wp_kses_post( '</div>' );
93
		}
94
	}
95
96
	/**
97
	 * Changes the add to cart message and adds our course name.
98
	 *
99
	 * @param  string  $message
100
	 * @param  array   $products
101
	 * @param  boolean $show_qty
102
	 * @return string
103
	 */
104
	public function add_to_cart_message( $message, $products, $show_qty ) {
105
		if ( isset( $_GET['plan_id'] ) ) { // @codingStandardsIgnoreLine.
106
			$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

106
			$this->plan_id = sanitize_text_field( /** @scrutinizer ignore-type */ wp_slash( $_GET['plan_id'] ) ); // @codingStandardsIgnoreLine.
Loading history...
107
108
			$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

108
			$title = '<strong>' . get_the_title( /** @scrutinizer ignore-type */ $this->plan_id ) . '</strong>';
Loading history...
109
			$title = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', 1, 'lsx-health-plan' ), $title );
110
111
			// Output success messages.
112
			if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
113
				$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

113
				$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
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

113
				$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...
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

113
				$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...
114
				$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 );
115
			} else {
116
				$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

116
				$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...
117
			}
118
		}
119
		return $message;
120
	}
121
122
	/**
123
	 * Replaces the links on the thank you page.
124
	 *
125
	 * @param string $message
126
	 * @param int $order_id
127
	 * @param object $memberships
128
	 * @return string
129
	 */
130
	public function memberships_thank_you_links( $message, $order_id, $memberships ) {
131
		$plan_slug = \lsx_health_plan\functions\get_option( 'my_plan_slug', false );
132
		if ( false !== $plan_slug && '' !== $plan_slug ) {
133
			$message = preg_replace( '/<a(.*)href="([^"]*)"(.*)>/', '<a$1href="' . home_url( $plan_slug ) . '"$3>', $message );
0 ignored issues
show
Bug introduced by
It seems like $plan_slug can also be of type array; however, parameter $path of home_url() 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

133
			$message = preg_replace( '/<a(.*)href="([^"]*)"(.*)>/', '<a$1href="' . home_url( /** @scrutinizer ignore-type */ $plan_slug ) . '"$3>', $message );
Loading history...
134
		}
135
		return $message;
136
	}
137
}
138