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 | } |
||||||||
38 | |||||||||
39 | /** |
||||||||
40 | * Return an instance of this class. |
||||||||
41 | * |
||||||||
42 | * @since 1.0.0 |
||||||||
43 | * |
||||||||
44 | * @return object \lsx_health_plan\classes\integrations\woocommerce\Checkout() A single instance of this class. |
||||||||
45 | */ |
||||||||
46 | public static function get_instance() { |
||||||||
47 | // If the single instance hasn't been set, set it now. |
||||||||
48 | if ( null === self::$instance ) { |
||||||||
49 | self::$instance = new self(); |
||||||||
50 | } |
||||||||
51 | return self::$instance; |
||||||||
52 | } |
||||||||
53 | |||||||||
54 | /** |
||||||||
55 | * Return the Place Order Text |
||||||||
56 | * |
||||||||
57 | * @param string $label |
||||||||
58 | * @return void |
||||||||
59 | */ |
||||||||
60 | public function checkout_button_text( $label = '' ) { |
||||||||
61 | $label = __( 'Place order', 'lsx-health-plan' ); |
||||||||
62 | return $label; |
||||||||
63 | } |
||||||||
64 | |||||||||
65 | /** |
||||||||
66 | * Saves the Plan ID to the cart data, so we can attach it to the order later. |
||||||||
67 | * |
||||||||
68 | * @param array $cart_item_data |
||||||||
69 | * @param string $product_id |
||||||||
70 | * @param string $variation_id |
||||||||
71 | * @return void |
||||||||
72 | */ |
||||||||
73 | public function add_plan_id_to_cart( $cart_item_data, $product_id, $variation_id ) { |
||||||||
74 | $plan_id = filter_input( INPUT_GET, 'plan_id' ); |
||||||||
75 | if ( empty( $plan_id ) || '' === $plan_id ) { |
||||||||
76 | return $cart_item_data; |
||||||||
77 | } |
||||||||
78 | $cart_item_data['plan_id'] = $plan_id; |
||||||||
79 | return $cart_item_data; |
||||||||
80 | } |
||||||||
81 | |||||||||
82 | /** |
||||||||
83 | * Output the WooCommerce Cart Notices. |
||||||||
84 | * |
||||||||
85 | * @return void |
||||||||
86 | */ |
||||||||
87 | public function cart_notices() { |
||||||||
88 | if ( function_exists( 'woocommerce_output_all_notices' ) && is_post_type_archive( 'plan' ) ) { |
||||||||
89 | echo wp_kses_post( '<div class="col-md-12 col-sm-12 woocommerce-notices-wrapper">' ); |
||||||||
90 | wc_print_notices(); |
||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
91 | echo wp_kses_post( '</div>' ); |
||||||||
92 | } |
||||||||
93 | } |
||||||||
94 | |||||||||
95 | /** |
||||||||
96 | * Changes the add to cart message and adds our course name. |
||||||||
97 | * |
||||||||
98 | * @param string $message |
||||||||
99 | * @param array $products |
||||||||
100 | * @param boolean $show_qty |
||||||||
101 | * @return string |
||||||||
102 | */ |
||||||||
103 | public function add_to_cart_message( $message, $products, $show_qty ) { |
||||||||
104 | if ( isset( $_GET['plan_id'] ) ) { // @codingStandardsIgnoreLine. |
||||||||
105 | $this->plan_id = sanitize_text_field( wp_slash( $_GET['plan_id'] ) ); // @codingStandardsIgnoreLine. |
||||||||
0 ignored issues
–
show
It seems like
wp_slash($_GET['plan_id']) can also be of type array ; 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
![]() |
|||||||||
106 | |||||||||
107 | $title = '<strong>' . get_the_title( $this->plan_id ) . '</strong>'; |
||||||||
0 ignored issues
–
show
$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
![]() |
|||||||||
108 | $title = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', 1, 'lsx-health-plan' ), $title ); |
||||||||
109 | |||||||||
110 | // Output success messages. |
||||||||
111 | if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { |
||||||||
112 | $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
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
![]() 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
![]() 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
![]() |
|||||||||
113 | $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 ); |
||||||||
114 | } else { |
||||||||
115 | $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
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
![]() |
|||||||||
116 | } |
||||||||
117 | } |
||||||||
118 | return $message; |
||||||||
119 | } |
||||||||
120 | |||||||||
121 | /** |
||||||||
122 | * Replaces the links on the thank you page. |
||||||||
123 | * |
||||||||
124 | * @param string $message |
||||||||
125 | * @param int $order_id |
||||||||
126 | * @param object $memberships |
||||||||
127 | * @return string |
||||||||
128 | */ |
||||||||
129 | public function memberships_thank_you_links( $message, $order_id, $memberships ) { |
||||||||
130 | $plan_slug = \lsx_health_plan\functions\get_option( 'my_plan_slug', false ); |
||||||||
131 | if ( false !== $plan_slug && '' !== $plan_slug ) { |
||||||||
132 | $message = preg_replace( '/<a(.*)href="([^"]*)"(.*)>/', '<a$1href="' . home_url( $plan_slug ) . '"$3>', $message ); |
||||||||
0 ignored issues
–
show
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
![]() |
|||||||||
133 | } |
||||||||
134 | return $message; |
||||||||
135 | } |
||||||||
136 | } |
||||||||
137 |