Completed
Pull Request — master (#2094)
by Justin
07:22
created

js.php ➔ wpsc_localize_script()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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