Completed
Pull Request — master (#2038)
by Ben
09:28
created

WPSC_Fancy_Notifications::enqueue_styles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4286
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * WP eCommerce Fancy Notifications
5
 */
6
7
add_action( 'wp_enqueue_scripts', array( 'WPSC_Fancy_Notifications', 'enqueue_styles' ) );
8
add_action( 'wp_enqueue_scripts', array( 'WPSC_Fancy_Notifications', 'enqueue_scripts' ) );
9
add_action( 'wpsc_add_to_cart_button_form_begin', array( 'WPSC_Fancy_Notifications', 'add_fancy_notifications' ) );
10
add_action( 'wpsc_theme_footer', array( 'WPSC_Fancy_Notifications', 'fancy_notifications' ) );
11
add_filter( 'wpsc_add_to_cart_json_response', array( 'WPSC_Fancy_Notifications', 'wpsc_add_to_cart_json_response' ) );
12
13
/**
14
 * WP eCommerce Fancy Notifications Class
15
 *
16
 * @since  4.0
17
 */
18
class WPSC_Fancy_Notifications {
19
20
	/**
21
	 * Fancy Notifications
22
	 *
23
	 * Container HTML for fancy notifications.
24
	 *
25
	 * @since  4.0
26
	 *
27
	 * @param   boolean  $return  Return output.
28
	 * @return  string            Output.
29
	 */
30
	public static function fancy_notifications( $return = false ) {
31
32
		static $already_output = false;
33
34
		if ( $already_output ) {
35
			return '';
36
		}
37
38
		$output = '';
39
		if ( 1 == get_option( 'fancy_notifications' ) ) {
40
			$output .= '<div id="fancy_notification">';
41
			$output .= '   <div id="loading_animation">';
42
			$output .= '      <img id="fancy_notificationimage" title="' . esc_attr__( 'Loading', 'wpsc' ) . '" alt="' . esc_attr__( 'Loading', 'wpsc' ) . '" src="' . esc_url( wpsc_loading_animation_url() ) . '" />' . esc_html__( 'Updating', 'wpsc' ) . '...';
43
			$output .= '   </div>';
44
			$output .= '   <div id="fancy_notification_content"></div>';
45
			$output .= '</div>';
46
		}
47
48
		$already_output = true;
49
50
		if ( $return ) {
51
			return $output;
52
		}
53
		echo $output;
54
55
	}
56
57
	/**
58
	 * Fancy Notification Content
59
	 *
60
	 * @since  4.0
61
	 *
62
	 * @param   array   $cart_messages  Cart message.
63
	 * @return  string                  Fancy notification content.
64
	 */
65
	public static function fancy_notification_content( $cart_messages ) {
66
67
		$output = '';
68
		foreach ( (array)$cart_messages as $cart_message ) {
69
			$output .= '<span>' . $cart_message . '</span><br />';
70
		}
71
		$output .= sprintf( '<a href="%s" class="go_to_checkout">%s</a>', esc_url( get_option( 'shopping_cart_url' ) ), esc_html__( 'Go to Checkout', 'wpsc' ) );
72
		$output .= sprintf( '<a href="#" onclick="jQuery( \'#fancy_notification\' ).css( \'display\', \'none\' ); return false;" class="continue_shopping">%s</a>', esc_html__( 'Continue Shopping', 'wpsc' ) );
73
74
		return $output;
75
76
	}
77
78
	/**
79
	 * Add To Cart JSON Response
80
	 *
81
	 * Adds 'fancy_notification' content to JSON response.
82
	 *
83
	 * @since  4.0
84
	 *
85
	 * @param   array  $json_response  JSON response.
86
	 * @return  array                  Updated JSON response.
87
	 */
88
	public static function wpsc_add_to_cart_json_response( $json_response ) {
89
90
		if ( is_numeric( $json_response['product_id'] ) && 1 == get_option( 'fancy_notifications' ) ) {
91
			$json_response['fancy_notification'] = str_replace( array( "\n", "\r" ), array( '\n', '\r' ), self::fancy_notification_content( $json_response['cart_messages'] ) );
92
		}
93
94
		return $json_response;
95
96
	}
97
98
	/**
99
	 * Add Fancy Notifications
100
	 *
101
	 * @since  4.0
102
	 */
103
	public static function add_fancy_notifications() {
104
105
		add_action( 'wp_footer', array( 'WPSC_Fancy_Notifications', 'fancy_notifications' ) );
106
107
	}
108
109
	/**
110
	 * Enqueue Styles
111
	 *
112
	 * @since  4.0
113
	 */
114
	public static function enqueue_styles() {
115
116
		wp_enqueue_style( 'wpsc-fancy-notifications', self::plugin_url() . '/css/fancy-notifications.css', false, '1.0' );
117
118
	}
119
120
	/**
121
	 * Enqueue Scripts
122
	 *
123
	 * @since  4.0
124
	 */
125
	public static function enqueue_scripts() {
126
127
		wp_enqueue_script( 'wpsc-fancy-notifications', self::plugin_url() . '/js/fancy-notifications.js', array( 'jquery' ), '1.0' );
128
129
	}
130
131
	/**
132
	 * Plugin URL
133
	 *
134
	 * @since  4.0
135
	 *
136
	 * @return  string  URL for fancy notifications directory.
137
	 */
138
	public static function plugin_url() {
139
140
		return plugins_url( '', __FILE__ );
141
142
	}
143
144
}
145