-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
136 lines (101 loc) · 4.78 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
Plugin Name: Bandcamp Woocommcerce Product Player
Plugin URI:
Description: Make your WooCommerce music products an interactive music player based on the song's Bandcamp song ID.
Version: 1.0
Author: Grayson Erhard
Author URI: https://graysonerhard.com
License: GPLv2 or later
Text Domain: bandcamp-woocommerce-product-player
*/
add_action('wp_enqueue_scripts', 'bwpp_scripts');
function bwpp_scripts() {
wp_enqueue_style('bwpp-style', WP_PLUGIN_URL.'/bandcamp-woocommerce-product-player/assets/css/bwpp-styles.css');
wp_register_script('bwpp', WP_PLUGIN_URL.'/bandcamp-woocommerce-product-player/assets/js/bwpp.js', array('jquery'), false, true);
wp_enqueue_script('bwpp');
}
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'bwpp_woo_add_custom_general_fields' );
function bwpp_woo_add_custom_general_fields() {
//TODO: MAKE RADIO BUTTON: TRACK/ALBUM
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_bandcamp_track_id_text_field',
'label' => __( 'Bandcamp Track ID', 'woocommerce' ),
'placeholder' => 'track=682966733',
'desc_tip' => 'true',
'description' => __( 'Enter the Bandcamp track ID found in the iframe embed code here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save Fields
add_action( 'woocommerce_process_product_meta', 'bwpp_woo_add_custom_general_fields_save' );
function bwpp_woo_add_custom_general_fields_save($post_id) {
// Text Field
$woocommerce_bandcamp_track_id_text_field = $_POST['_bandcamp_track_id_text_field'];
if( !empty( $woocommerce_bandcamp_track_id_text_field ) )
update_post_meta( $post_id, '_bandcamp_track_id_text_field', esc_attr( $woocommerce_bandcamp_track_id_text_field ) );
}
if (is_single()) {
add_action('woocommerce_before_shop_loop_item', 'new_product_image_iframe');
}
add_action('woocommerce_single_product_image_thumbnail_html', 'bwpp_new_product_image_iframe');
function bwpp_new_product_image_iframe() {
global $post;
$bandcampID = get_post_meta($post->ID, '_bandcamp_track_id_text_field', true);
if ($bandcampID) {
if (is_single()) {
?>
<div class="bwpp_iframe_wrap">
<iframe class="bwpp" style="border: 0;" src="https://bandcamp.com/EmbeddedPlayer/<?php echo $bandcampID; ?>/size=large/bgcol=000/linkcol=e99708/tracklist=false/transparent=true/" seamless></iframe>
</div>
<?php
} else {
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
?>
<div class="bwpp_iframe_wrap">
<iframe class="bwpp" style="border: 0;" src="https://bandcamp.com/EmbeddedPlayer/<?php echo $bandcampID; ?>/size=large/bgcol=333333/linkcol=e99708/minimal=true/transparent=true/" seamless></iframe>
</div>
<?php
}
} else {
if ( is_single() ) {
// DISPLAY USUAL PRODUCT IMAGE
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 );
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$full_size_image = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );
$image_title = get_post_field( 'post_excerpt', $post_thumbnail_id );
$placeholder = has_post_thumbnail() ? 'with-images' : 'without-images';
$wrapper_classes = apply_filters( 'woocommerce_single_product_image_gallery_classes', array(
'woocommerce-product-gallery',
'woocommerce-product-gallery--' . $placeholder,
'woocommerce-product-gallery--columns-' . absint( $columns ),
'images',
) );
$attributes = array(
'title' => $image_title,
'data-src' => $full_size_image[0],
'data-large_image' => $full_size_image[0],
'data-large_image_width' => $full_size_image[1],
'data-large_image_height' => $full_size_image[2],
);
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '</div>';
}
echo $html;
do_action( 'woocommerce_product_thumbnails' );
} else {
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
}
}
}