Completed
Push — master ( 2253a0...b13776 )
by
unknown
07:38
created

media.php ➔ _wpsc_action_enqueue_media_scripts()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 6
nop 0
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
add_action( 'admin_enqueue_scripts', '_wpsc_action_enqueue_media_scripts' );
4
add_action( 'admin_enqueue_scripts', '_wpsc_action_enqueue_media_styles' );
5
add_action( 'admin_footer', '_wpsc_action_print_media_templates' );
6
7
function _wpsc_action_enqueue_media_scripts() {
8
	$current_screen = get_current_screen();
9
10
	if ( in_array( $current_screen->id, array( 'wpsc-product-variations-iframe', 'wpsc-product' ) ) ) {
11
		$post = get_post();
12
		if ( ! $post )
13
			$id = absint( $_REQUEST['product_id'] );
14
		else
15
			$id = $post->ID;
16
17
		$gallery = _wpsc_get_product_gallery_json( $id );
18
		wp_enqueue_script( 'wpsc-media', WPSC_URL . '/wpsc-admin/js/media.js', array( 'media-editor', 'wp-e-commerce-admin', 'jquery-ui-sortable', 'post' ), WPSC_VERSION );
19
		wp_localize_script( 'wpsc-media', 'WPSC_Media', array(
20
			'l10n' => array(
21
				'productMediaTitle' => __( 'Add Images to Product Gallery', 'wp-e-commerce' ),
22
				'saveGallery'       => __( 'Set Product Images', 'wp-e-commerce' ),
23
			),
24
			'gallery' => $gallery,
25
			'updateGalleryNonce' => wp_create_nonce( 'wpsc_ajax_update_gallery_' . $id ),
26
			'getGalleryNonce'    => wp_create_nonce( 'wpsc_ajax_get_gallery_' . $id )
27
		) );
28
	}
29
	
30
	if ( 'edit-wpsc-product' == $current_screen->id ) {
31
		wp_enqueue_script( 'wpsc-quick-edit', WPSC_URL . '/wpsc-admin/js/quick-edit.js', array( 'jquery', 'inline-edit-post' ), WPSC_VERSION, true );
32
	}
33
}
34
35
function _wpsc_action_enqueue_media_styles() {
36
	$current_screen = get_current_screen();
37
38
	if ( 'wpsc-product' == $current_screen->id )
39
		wp_enqueue_style( 'wpsc-media', WPSC_URL . '/wpsc-admin/css/media.css', array( 'media-views' ), WPSC_VERSION );
40
}
41
42
function _wpsc_action_print_media_templates() {
43
	?>
44
	<script type="text/html" id="tmpl-wpsc-featured-image">
45
		<div class="wpsc-media-featured-image">
46
			<span class="title"><?php _e( 'Featured image', 'wp-e-commerce' ); ?></span>
47
			<a class="edit-selection" href="#"><?php _ex( 'Edit', 'edit featured image', 'wp-e-commerce' ); ?></a>
48
		</div>
49
		<div class="wpsc-selection-view"></div>
50
	</script>
51
	<?php
52
}
53
54
function _wpsc_ajax_verify_get_variation_gallery() {
55
	return _wpsc_ajax_verify_nonce( 'get_variation_gallery_' . absint( $_REQUEST['id'] ) );
56
}
57
58
function _wpsc_ajax_get_variation_gallery() {
59
	$id = absint( $_REQUEST['id'] );
60
61
	$gallery = _wpsc_get_product_gallery_json( $id );
62
63
	return array(
64
		'models' => $gallery,
65
		'featuredId' => wpsc_the_product_thumbnail_id( $id )
66
	);
67
}
68
69
/**
70
 * Verifies the save product gallery AJAX nonce.
71
 *
72
 * @return WP_Error|boolean True if nonce is valid. WP_Error if otherwise.
73
 */
74
function _wpsc_ajax_verify_save_product_gallery() {
75
	return _wpsc_ajax_verify_nonce( 'update_gallery_' . absint( $_REQUEST['postId'] ) );
76
}
77
78
function _wpsc_ajax_verify_get_product_gallery() {
79
	return _wpsc_ajax_verify_nonce( 'get_gallery_' . absint( $_REQUEST['postId'] ) );
80
}
81
82
function _wpsc_ajax_save_product_gallery() {
83
	$id = absint( $_REQUEST['postId'] );
84
	$items = array_map( 'absint', $_REQUEST['items'] );
85
	$thumb = get_post_thumbnail_id( $id );
86
87
	// always make sure the thumbnail is included
88
	if ( $thumb && ! in_array( $thumb, $items ) )
89
		$items[] = $thumb;
90
91
	$result = wpsc_set_product_gallery( $id, $items );
92
93
	return _wpsc_get_product_gallery_json( $id );
94
}
95
96
function _wpsc_ajax_get_product_gallery() {
97
	$id = absint( $_REQUEST['postId'] );
98
	return _wpsc_get_product_gallery_json( $id );
99
}
100
101
function _wpsc_get_product_gallery_json( $id ) {
102
	$attachments = wpsc_get_product_gallery( $id );
103
	return array_map( 'wp_prepare_attachment_for_js', $attachments );
104
}
105