Completed
Pull Request — master (#2337)
by Ben
06:27
created

WPSC_Fancy_Notifications::setup_hooks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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