Created
September 16, 2010 15:18
-
-
Save brandondove/582593 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class kkiLadies { | |
function __construct() { | |
wp_register_script( 'kki-ladies', KKI_JS.'ladies.js', array( 'jquery' ), '0.1', true ); | |
add_action( 'init', array( &$this, 'init' ) ); | |
add_action( 'admin_menu', array( &$this, 'admin_menu' ) ); | |
add_action( 'save_post', array( &$this, 'save_post' ) ); | |
// add_action( 'admin_print_scripts-post-new.php', array( &$this, 'print_scripts' ) ); | |
add_action( 'admin_print_scripts-post.php', array( &$this, 'print_scripts' ) ); | |
add_action( 'wp_ajax_update-kki-gallery', array( &$this, 'update_kki_gallery' ) ); | |
add_action( 'wp_ajax_update-kki-profile', array( &$this, 'update_kki_profile' ) ); | |
add_action( 'wp_ajax_update-kki-video', array( &$this, 'update_kki_video' ) ); | |
add_filter( 'shopp_tag_catalog_catalog-products', array( &$this, 'shopp_debug' ), 1, 3 ); | |
} | |
function shopp_debug( $result, $options, $object ) { | |
pring_r( $result ); | |
pring_r( $options ); | |
pring_r( $object ); | |
} | |
/* custom post type & taxonomy | |
******************************************/ | |
function init() { | |
// allow for lady creation | |
register_post_type( 'kki-ladies', array( | |
'labels' => array( | |
'name' => __( 'Ladies', KKI ), | |
'singular_name' => __( 'Lady', KKI ) | |
), | |
'description' => __( 'Special content type for KKi Ladies', KKI ), | |
'public' => true, | |
'exclude_from_search' => true, | |
'publically_queryable' => true, | |
'show_ui' => true, | |
'show_in_nav_menus' => true, | |
'hierarchal' => false, | |
'supports' => array( | |
'title', | |
'editor', | |
'comments', | |
'excerpt' | |
), | |
'taxonomies' => array( | |
'country' | |
), | |
'capability_type' => 'post' | |
)); | |
// allow ladies to be tagged with a country | |
register_taxonomy( 'country', 'kki-ladies', array( | |
'labels' => array( | |
'name' => __( 'Countries', KKI ), | |
'singular_name' => __( 'Country', KKI ), | |
), | |
'rewrite' => array( | |
'with_front' => false | |
) | |
)); | |
} | |
/* meta boxes | |
******************************************/ | |
function admin_menu() { | |
add_meta_box( 'kki-lady-images', 'Profile Image', array( &$this, 'profile_image_meta_box' ), 'kki-ladies' ); | |
add_meta_box( 'kki-lady-video', 'Video', array( &$this, 'sidebar_video_meta_box' ), 'kki-ladies' ); | |
add_meta_box( 'kki-lady-gallery', 'Image Gallery', array( &$this, 'sidebar_gallery_meta_box' ), 'kki-ladies' ); | |
add_meta_box( 'kki-lady-product', 'Favorite Products', array( &$this, 'products_meta_box' ), 'kki-ladies' ); | |
} | |
/* load up scripts | |
******************************************/ | |
function print_scripts() { | |
global $post; | |
wp_enqueue_script( 'kki-ladies' ); | |
wp_localize_script( | |
'kki-ladies', | |
'kki', | |
array( | |
'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
'postid' => $post->ID, | |
'remove_text' => __( 'Remove Image', KKI ), | |
'add_text' => __( 'Add Image', KKI ), | |
'unset_text' => __( 'Unset Video Image', KKI ), | |
'set_text' => __( 'Set Video Image', KKI ), | |
'nonce' => wp_create_nonce( 'kki-ladies-postmeta' ) | |
) | |
); | |
} | |
/* profile image meta box | |
******************************************/ | |
function get_attached_pictures( $post ) { | |
return get_posts( 'post_type=attachment&post_parent='.$post->ID.'&numberposts=-1' ); | |
} | |
/* profile image meta box | |
******************************************/ | |
function profile_image_meta_box( $post ) { | |
$images = $this->get_attached_pictures( $post ); | |
echo '<p>'.__( sprintf( '%sUpload images%s and then select the image you want to use as the main profile picture for this lady.', '<a href="media-upload.php?post_id='.$post->ID.'&type=image&TB_iframe=1&width=640&height=258" class="thickbox">', '</a>' ), KKI ).'</p>'; | |
$meta = get_post_meta( $post->ID, 'profile-image', true ); | |
foreach ( $images as $image ) : | |
echo '<div style="float: left; margin: 0 10px 20px 0; width: 120px; height: 140px;">'; | |
echo '<div style="display: block; width: 100px; margin: 0 auto;">'.wp_get_attachment_image( $image->ID, array( 100, 100 ) ).'</div>'; | |
echo '<div style="display: block; margin-top: 10px; text-align: center;">'; | |
echo ( $image->ID == $meta ) ? '<a class="unset-profile-photo button-primary" style="display: none;" id="image-'.$image->ID.'">Set Profile Image</a>' : '<a href="#" class="set-profile-photo button-primary" id="image-'.$image->ID.'">Set Profile Image</a>'; | |
echo '</div>'; | |
echo '</div>'; | |
endforeach; | |
echo '<div style="clear: left;"> </div>'; | |
} | |
function update_kki_profile() { | |
if ( ! wp_verify_nonce( $_POST['nonce'], 'kki-ladies-postmeta' ) ) | |
die ( __( 'Naughty, naughty...', KKI ) ); | |
$post_id = $_POST['postid']; | |
$image_id = $_POST['imageid']; | |
add_post_meta( $post_id, 'profile-image', $image_id, true) or | |
update_post_meta( $post_id, 'profile-image', $image_id ); | |
$response = json_encode( array( 'success' => true ) ); | |
header( "Content-Type: application/json" ); | |
echo $response; | |
exit; | |
} | |
/* sidebar video meta box | |
******************************************/ | |
function sidebar_video_meta_box( $post ) { | |
$images = $this->get_attached_pictures( $post ); | |
echo '<p>'.__( sprintf( '%sUpload images%s and then select the image you want to use as the placeholder image for the video pop-up. To remove the video from the sidebar, click the "Unset Video Image" buton.', '<a href="media-upload.php?post_id='.$post->ID.'&type=image&TB_iframe=1&width=640&height=258" class="thickbox">', '</a>' ), KKI ).'</p>'; | |
$video_image = get_post_meta( $post->ID, 'video-image', true ); | |
$video_url = get_post_meta( $post->ID, 'video-url', true ); | |
foreach ( $images as $image ) : | |
echo '<div style="float: left; margin: 0 10px 20px 0; width: 120px; height: 140px;">'; | |
echo '<div style="display: block; width: 100px; margin: 0 auto;">'.wp_get_attachment_image( $image->ID, array( 100, 100 ) ).'</div>'; | |
echo '<div style="display: block; margin-top: 10px; text-align: center;">'; | |
echo ( $image->ID == $video_image ) ? '<a href="#" class="unset-video-placeholder button-secondary" id="image-'.$image->ID.'">Unset Video Image</a>' : '<a href="#" class="set-video-placeholder button-primary" id="image-'.$image->ID.'">Set Video Image</a>'; | |
echo '</div>'; | |
echo '</div>'; | |
endforeach; | |
echo '<div style="clear: left;"> </div>'; | |
echo '<label for="video-url">'.__( 'YouTube Video URL:')."</label> "; | |
echo '<input type="text" name="video-url" id="video-url" style="width: 300px;" value="'.$video_url.'" />'; | |
} | |
function update_kki_video() { | |
if ( ! wp_verify_nonce( $_POST['nonce'], 'kki-ladies-postmeta' ) ) | |
die ( __( 'Naughty, naughty...', KKI ) ); | |
$post_id = $_POST['postid']; | |
$image_id = $_POST['imageid']; | |
if( $_POST['status'] == 'unset' ) : | |
delete_post_meta( $post_id, 'video-image' ); | |
else : | |
add_post_meta( $post_id, 'video-image', $image_id, true) or | |
update_post_meta( $post_id, 'video-image', $image_id ); | |
endif; | |
$response = json_encode( array( 'success' => true ) ); | |
header( "Content-Type: application/json" ); | |
echo $response; | |
exit; | |
} | |
/* sidebar gallery meta box | |
******************************************/ | |
function sidebar_gallery_meta_box( $post ) { | |
$images = $this->get_attached_pictures( $post ); | |
echo '<p>'.__( sprintf( '%sUpload images%s and then select the images you want to use in the sidebar for this lady.', '<a href="media-upload.php?post_id='.$post->ID.'&type=image&TB_iframe=1&width=640&height=258" class="thickbox">', '</a>' ), KKI ).'</p>'; | |
$meta = get_post_meta( $post->ID, 'gallery-images', true ); | |
$meta = maybe_unserialize( $meta ); | |
foreach ( $images as $image ) : | |
echo '<div style="float: left; margin: 0 10px 20px 0; width: 120px; height: 140px;">'; | |
echo '<div style="display: block; width: 100px; margin: 0 auto;">'.wp_get_attachment_image( $image->ID, array( 100, 100 ) ).'</div>'; | |
echo '<div style="display: block; margin-top: 10px; text-align: center;">'; | |
echo ( is_array( $meta ) && array_search( $image->ID, $meta ) !== false ) ? '<a href="#" class="remove-from-gallery button-secondary" id="image-'.$image->ID.'">Remove Image</a>' : '<a href="#" class="add-to-gallery button-primary" id="image-'.$image->ID.'">Add Image</a>'; | |
echo '</div>'; | |
echo '</div>'; | |
endforeach; | |
echo '<div style="clear: left;"> </div>'; | |
} | |
function update_kki_gallery() { | |
if ( ! wp_verify_nonce( $_POST['nonce'], 'kki-ladies-postmeta' ) ) | |
die ( __( 'Naughty, naughty...', KKI ) ); | |
$post_id = $_POST['postid']; | |
$image_id = $_POST['imageid']; | |
$images = get_post_meta( $post_id, 'gallery-images', true ); | |
$images = maybe_unserialize( $images ); | |
if( is_array( $images ) ) : | |
if( array_search( $image_id, $images ) !== false && $_POST['status'] == 'remove' ) : | |
$key = array_search( $image_id, $images ); | |
array_splice( $images, $key, 1 ); | |
else : | |
array_push( $images, $image_id ); | |
endif; | |
else : | |
$images = array( $image_id ); | |
endif; | |
$images = serialize( $images ); | |
add_post_meta( $post_id, 'gallery-images', $images, true) or | |
update_post_meta( $post_id, 'gallery-images', $images ); | |
$response = json_encode( array( 'success' => true ) ); | |
header( "Content-Type: application/json" ); | |
echo $response; | |
exit; | |
} | |
/* favorite products meta box | |
******************************************/ | |
function products_meta_box( $post ) { | |
echo '<p>'.__( sprintf( '%sCreate products%s and then select the products that this lady likes the best.', '<a href="'.admin_url( 'admin.php?page=shopp-products&id=new' ).'" target="_blank">', '</a>' ), KKI ).'</p>'; | |
$meta = get_post_meta( $post->ID, 'favorite-products', true ); | |
$meta = maybe_unserialize( $meta ); | |
shopp('catalog','catalog-products','load=true'); | |
if(shopp('category','hasproducts','load=prices,images')) : | |
while(shopp('category','products')): | |
echo '<a href="'._shopp('product','url').'">'._shopp('product','coverimage').'</a>'._shopp('product','name'); | |
endwhile; | |
else: | |
echo 'none'; | |
endif; | |
} | |
/* save meta | |
******************************************/ | |
function save_post( $post_id ) { | |
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) | |
return $post_id; | |
if ( !current_user_can( 'edit_post', $post_id ) ) | |
return $post_id; | |
$video_url = $_POST['video-url']; | |
add_post_meta( $post_id, 'video-url', $video_url, true ) or | |
update_post_meta( $post_id, 'video-url', $video_url ); | |
// return $mydata; | |
} | |
} | |
$kkiladies = new kkiLadies; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment