Completed
Push — master ( 9a3ee6...bca146 )
by Justin
05:42
created

js.php ➔ _wpsc_enqueue_product_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
add_action( 'wp_enqueue_scripts', '_wpsc_te2_register_scripts', 1 );
4
5
function _wpsc_te2_register_scripts() {
6
7
	$engine = WPSC_Template_Engine::get_instance();
8
9
	$scripts = apply_filters( 'wpsc_registered_scripts', $engine->get_core_scripts_data() );
10
11
	foreach ( $scripts as $handle => $script_data ) {
12
		wp_register_script(
13
			$handle,
14
			wpsc_locate_asset_uri( $script_data['path'] ),
15
			$script_data['dependencies'],
16
			$script_data['version'],
17
			! isset( $script_data['in_footer'] ) || $script_data['in_footer']
18
		);
19
	}
20
21
	$enqueued = false;
22
23
	foreach ( $engine->get_queued_scripts() as $handle => $script_data ) {
24
		$enqueued = true;
25
26
		_wpsc_enqueue_and_localize_script( $handle, $script_data );
27
	}
28
29
	// Output our namespace.
30
	?><script type='text/javascript'>/* <![CDATA[ */window.WPSC = window.WPSC || {};/* ]]> */</script><?php
31
32
	do_action( 'wpsc_register_scripts' );
33
	do_action( 'wpsc_enqueue_scripts' );
34
}
35
36
function _wpsc_enqueue_shipping_billing_scripts() {
37
	add_action(
38
		'wp_enqueue_scripts',
39
		'_wpsc_action_enqueue_shipping_billing_scripts'
40
	);
41
}
42
43
function _wpsc_action_enqueue_shipping_billing_scripts() {
44
	wpsc_enqueue_script( 'wpsc-country-region' );
45
	wpsc_enqueue_script( 'wpsc-copy-billing-info', array(
46
		'property_name' => 'copyBilling',
47
		'data' => array(
48
			'strings' => array(
49
				'billing_and_shipping' => apply_filters( 'wpsc_checkout_billing_header_label' , __( '<h2>Billing &amp; Shipping Details</h2>', 'wp-e-commerce' ) ),
50
				'shipping'             => apply_filters( 'wpsc_checkout_shipping_header_label' , __( '<h2>Shipping Details</h2>', 'wp-e-commerce' ) ),
51
				'billing'              => apply_filters( 'wpsc_checkout_billing_only_header_label', __( '<h2>Billing Details</h2>', 'wp-e-commerce' ) ),
52
			),
53
		),
54
	) );
55
}
56
57
function _wpsc_enqueue_float_label_scripts() {
58
    add_action(
59
        'wp_enqueue_scripts',
60
        '_wpsc_action_enqueue_float_label_scripts'
61
    );
62
}
63
64
function _wpsc_action_enqueue_float_label_scripts() {
65
    wpsc_enqueue_script( 'wpsc-float-labels' );
66
    wpsc_enqueue_script( 'wpsc-checkout' );
67
}
68
69
function _wpsc_enqueue_product_scripts() {
70
    add_action(
71
        'wp_enqueue_scripts',
72
        '_wpsc_action_enqueue_product_scripts'
73
    );
74
}
75
76
function _wpsc_action_enqueue_product_scripts() {
77
    wpsc_enqueue_script( 'wpsc-products' );
78
}
79
80
/**
81
 * Enqueue a registered wpsc script (and optionally localize its JS data).
82
 * If script cannot be enqueued yet, register the queued script for later enqueue.
83
 *
84
 * @see WPSC_Template_Engine::register_queued_script()
85
 * @see wp_enqueue_script()
86
 * @see wpsc_localize_script()
87
 *
88
 * @since 4.0
89
 *
90
 * @param string $handle      Name of the registered wpsc script.
91
 * @param array  $script_data (Optional) data to send to wp_localize_script under the WPSC namespace.
92
 */
93
function wpsc_enqueue_script( $handle, $script_data = array() ) {
94
	if ( ! did_action( 'wpsc_enqueue_scripts' ) ) {
95
		WPSC_Template_Engine::get_instance()->register_queued_script( $handle, $script_data );
96
	} else {
97
		_wpsc_enqueue_and_localize_script( $handle, $script_data );
98
	}
99
}
100
101
/**
102
 * Enqueue a registered wpsc script (and optionally localize its JS data).
103
 *
104
 * @see wp_enqueue_script()
105
 * @see wpsc_localize_script()
106
 *
107
 * @access private
108
 *
109
 * @since 4.0
110
 *
111
 * @param string $handle      Name of the registered wpsc script.
112
 * @param array  $script_data (Optional) data to send to wp_localize_script under the WPSC namespace.
113
 */
114
function _wpsc_enqueue_and_localize_script( $handle, $script_data = array() ) {
115
	wp_enqueue_script( $handle );
116
117
	if ( ! empty( $script_data ) && isset( $script_data['property_name'], $script_data['data'] ) ) {
118
119
		$add_to_namespace = ! isset( $script_data['add_to_namespace'] ) || $script_data['add_to_namespace'];
120
121
		wpsc_localize_script(
122
			$handle,
123
			$script_data['property_name'],
124
			$script_data['data'],
125
			$add_to_namespace
126
		);
127
	}
128
}
129
130
/**
131
 * Localize a script under the WPSC namespace.
132
 *
133
 * Works only if the script has already been registered or enqueued.
134
 *
135
 * Accepts an associative array $data and creates a JavaScript object:
136
 *
137
 *     window.WPSC.{$property_name} = {
138
 *         key: value,
139
 *         key: value,
140
 *         ...
141
 *     }
142
 *
143
 *
144
 * @see wp_localize_script()
145
 * @see WP_Dependencies::get_data()
146
 * @see WP_Dependencies::add_data()
147
 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
148
 *
149
 * @since 4.0
150
 *
151
 * @param string $handle          Script handle the data will be attached to.
152
 * @param string $property_name   Name for the property applied to the global WPSC object.
153
 *                                Passed directly, so it should be qualified JS variable.
154
 *                                Example: '/[a-zA-Z0-9_]+/'.
155
 * @param array $data             The data itself. The data can be either a single or multi-dimensional array.
156
 * @param bool  $add_to_namespace Whether to add to the WPSC object, or default wp_localize_script output.
157
 *
158
 * @return bool True if the script was successfully localized, false otherwise.
159
 */
160
function wpsc_localize_script( $handle, $property_name, $data, $add_to_namespace = true ) {
161
	global $wp_scripts;
162
163
	if ( $add_to_namespace ) {
164
165
		// Make sure this variable does not break the WPSC namespace.
166
		$property_name = 'WPSC.' . sanitize_html_class( maybe_serialize( $property_name ) );
167
	}
168
169
	$result = wp_localize_script( $handle, $property_name, $data );
170
171
	if ( $add_to_namespace ) {
172
173
		$script = $wp_scripts->get_data( $handle, 'data' );
174
175
		$script = str_replace(
176
			"var {$property_name} = {",
177
			"window.{$property_name} = window.{$property_name} || {",
178
			$script
179
		);
180
181
		$result = $wp_scripts->add_data( $handle, 'data', $script );
182
	}
183
184
	return $result;
185
}
186