Skip to content

Instantly share code, notes, and snippets.

@norcross
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/df2381dcfff55de95982 to your computer and use it in GitHub Desktop.
Save norcross/df2381dcfff55de95982 to your computer and use it in GitHub Desktop.
code to fix shit for WPEC gallery
function wpsc_update_product_gallery_tab(obj){
var output;
output = '<div id="wpsc_product_gallery">';
output += '<ul>';
for (var i = 0; i < obj.length; i++){
output += '<li>';
output += '<img src="' + obj[i].sizes.thumbnail.url + '">';
output += '<input type="hidden" name="wpsc-product-gallery-imgs[]" value="' + obj[i].id + '">'; // new item to include hidden field with ID
output += '</li>';
}
output += '</ul>';
output += '<div class="clear"></div>';
output += '</div>';
jQuery('#wpsc_product_gallery').replaceWith(output);
wpsc_update_product_details_metabox_live_title();
}
<?php
/**
* We really need to either bake this functionality in for 3.9.0 or rip it out into Gold Cart or something else.
* So not awesome to have this exposed and unusable.
*
* @param WP_Post $post Product
* @return void
*/
function wpsc_product_gallery( $post ) {
$upload_iframe_src = esc_url( get_upload_iframe_src( 'image', $post->ID ) );
$images = wpsc_get_admin_product_gallery( $post->ID ); // new function call to get images from meta field
$output = '<div id="wpsc_product_gallery">';
$output .= '<ul>';
if ( $images ) {
foreach ( $images as $image_id ) {
$output .= '<li>';
$output .= '<img src="' . wp_get_attachment_thumb_url( absint( $image_id ) ) . '">';
$output .= '<input type="hidden" name="wpsc-product-gallery-imgs[]" value="' . absint( $image_id ) . '">'; // new item to include hidden field with ID
$output .= '</li>';
}
}
$output .= '</ul>';
$output .= '<div class="clear"></div>';
$output .= '</div>';
$output .= '<p class="hide-if-no-js">';
$output .= '<a class="button button-small thickbox" title="' . esc_attr__( 'Manage Product Image Gallery...', 'wpsc' ).'" href="' . $upload_iframe_src . '" id="wpsc-manage-product-gallery">';
$output .= esc_html__( 'Manage Product Image Gallery...', 'wpsc' );
$output .= '</a>';
$output .= '</p>';
echo $output;
}
//******************************************************************************************
// start the engine
//******************************************************************************************
jQuery(document).ready( function($) {
//******************************************************************************************
// make the image gallery drag / drop / sort
//******************************************************************************************
$( 'div#wpsc_product_gallery' ).each(function() {
$( this ).find( 'ul' ).sortable({
cursor: 'move',
revert: 300,
}).disableSelection();
});
//******************************************************************************************
// we are done here. go home
//******************************************************************************************
});
<?php
/* call my functions */
add_action ( 'save_post', 'wpec_new_gallery_save' );
add_action ( 'admin_enqueue_scripts', 'wpec_new_gallery_script' );
/**
* [wpec_new_gallery_save description]
* @param [type] $post_id [description]
* @return [type] [description]
*/
function wpec_new_gallery_save( $post_id ) {
// make sure we aren't using autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// run our post type check
if ( 'wpsc-product' !== get_post_type( $post_id ) ) {
return $post_id;
}
// and make sure the user has the ability to do shit
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// all clear. get data via $_POST and cast as an array
if ( ! empty( $_POST['wpsc-product-gallery-imgs'] ) ) {
$image_ids = (array) $_POST['wpsc-product-gallery-imgs'];
update_post_meta( $post_id, '_wpsc_product_gallery', $image_ids );
} else {
delete_post_meta( $post_id, '_wpsc_product_gallery' );
}
}
/**
* [wpec_new_gallery_script description]
* @param [type] $hook [description]
* @return [type] [description]
*/
function wpec_new_gallery_script( $hook ) {
$screen = get_current_screen();
// load media on individual items
if ( is_object( $screen ) && $screen->base == 'post' && $screen->post_type == 'wpsc-product' ) {
wp_enqueue_script( 'wpec-fix-shit', plugins_url('js/wpec-fix-shit.js', __FILE__ ), array( 'jquery', 'jquery-ui-sortable' ), null, true );
}
}
/**
* [wpsc_get_admin_product_gallery description]
* @param integer $post_id [description]
* @return [type] [description]
*/
function wpsc_get_admin_product_gallery( $post_id = 0 ) {
$meta = get_post_meta( $post_id, '_wpsc_product_gallery', true );
// no custom gallery was created, so return nothing
if ( ! $meta ) {
return;
}
// now make sure the IDs present are actual images
foreach( $meta as $key => $image_id ) {
if ( get_post_type( $image_id ) !== 'attachment' ) {
unset( $meta[$key] );
}
}
// send it back
if ( ! empty( $meta ) ) {
return $meta;
}
// nothing left.
return;
}
## notes regarding changes made to WPEC core files ##
#### wpsc-admin/js/admin.js
* line 574: added hidden field to output
#### wpsc-admin/includes/display-items-functions.php
* line 883: used new wpsc_get_admin_product_gallery function
* line 891: added hidden field with output
@norcross
Copy link
Author

may be worth adding a small amount of CSS for the image hovering and also possibly an ajax call to update the gallery meta when dragging is complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment