Advertisement
Guest User

Untitled

a guest
Jan 20th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. <?php
  2. /**
  3. * Latest Product widget class
  4. *
  5. * Takes the settings, works out if there is anything to display, if so, displays it.
  6. *
  7. * @since 3.8
  8. */
  9.  
  10. class MZ_Widget_Latest_Products extends WP_Widget {
  11.  
  12. /**
  13. * Widget Constuctor
  14. */
  15. function MZ_Widget_Latest_Products() {
  16. $widget_ops = array( 'classname' => 'widget_mz_wpsc_latest_products','description' => __( 'Phomedia Latest Products Widget', 'wpsc' ) );
  17. $this->WP_Widget( 'wpsc_latest_products', __( 'Phomedia Latest Products', 'wpsc' ), $widget_ops );
  18. }
  19.  
  20. /**
  21. * Widget Output
  22. *
  23. * @param $args (array)
  24. * @param $instance (array) Widget values.
  25. */
  26. function widget( $args, $instance ) {
  27.  
  28. global $wpdb, $table_prefix;
  29.  
  30. extract( $args );
  31.  
  32. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Products', 'wpsc' ) : $instance['title'] );
  33.  
  34. echo $before_widget;
  35.  
  36.  
  37. if ( $title )
  38. echo $before_title . $title . $after_title;
  39. wpsc_phomedia_latest_product( $args, $instance );
  40. echo $after_widget;
  41. }
  42.  
  43. /**
  44. * Update Widget
  45. *
  46. * @param $new_instance (array) New widget values.
  47. * @param $old_instance (array) Old widget values.
  48. *
  49. * @return (array) New values.
  50. */
  51. function update( $new_instance, $old_instance ) {
  52.  
  53. $instance = $old_instance;
  54. $instance['title'] = strip_tags( $new_instance['title'] );
  55. $instance['number'] = (int)$new_instance['number'];
  56. $instance['image'] = (bool)$new_instance['image'];
  57. $instance['height'] = (int)$new_instance['height'];
  58. $instance['width'] = (int)$new_instance['width'];
  59.  
  60. return $instance;
  61.  
  62. }
  63.  
  64. /**
  65. * Widget Options Form
  66. *
  67. * @param $instance (array) Widget values.
  68. */
  69. function form( $instance ) {
  70.  
  71. global $wpdb;
  72.  
  73. // Defaults
  74. $instance = wp_parse_args( (array)$instance, array(
  75. 'title' => '',
  76. 'number' => 4,
  77. 'width' => 173,
  78. 'height' => 151
  79. ) );
  80.  
  81. // Values
  82. $title = esc_attr( $instance['title'] );
  83. $number = (int)$instance['number'];
  84. $image = (bool)$instance['image'];
  85. $width = (int) $instance['width'];
  86. $height = (int) $instance['height']; ?>
  87.  
  88. <p>
  89. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'wpsc' ); ?></label>
  90. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
  91. </p>
  92.  
  93. <p>
  94. <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of products to show:', 'wpsc' ); ?></label>
  95. <input type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $number; ?>" size="3" />
  96. </p>
  97.  
  98. <p>
  99. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'image' ); ?>" name="<?php echo $this->get_field_name( 'image' ); ?>"<?php checked($image); ?> onclick="jQuery('.wpsc_latest_image').toggle()">
  100. <label for="<?php echo $this->get_field_id( 'image' ); ?>"><?php _e( 'Show Thumbnails', 'wpsc' ); ?></label>
  101. </p>
  102.  
  103. <div class="wpsc_latest_image"<?php if( !checked( $image ) ) { echo ' style="display:none;"'; } ?>>
  104. <p>
  105. <label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'wpsc'); ?></label>
  106. <input type="text" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" value="<?php echo $width ; ?>" size="3" />
  107. <label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'wpsc'); ?></label>
  108. <input type="text" id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" value="<?php echo $height ; ?>" size="3" />
  109. </p>
  110. </div>
  111. <?php
  112. }
  113. }
  114.  
  115. add_action( 'widgets_init', create_function( '', 'return register_widget("MZ_Widget_Latest_Products");' ) );
  116.  
  117. function wpsc_phomedia_latest_product( $args = null, $instance ) {
  118. global $wpdb;
  119. $args = wp_parse_args( (array)$args, array( 'number' => 4 ) );
  120. $siteurl = get_option( 'siteurl' );
  121. $options = get_option( 'wpsc-widget_latest_products' );
  122. $number = isset($instance['number']) ? (int)$instance['number'] : 4;
  123. $image = isset($instance['image']) ? (bool)$instance['image'] : FALSE;
  124.  
  125. if ( isset($instance['width'] ) )
  126. $width = $instance['width'];
  127.  
  128. if ( isset( $instance['height'] ) )
  129. $height = $instance['height'];
  130.  
  131. $latest_products = get_posts( array(
  132. 'post_type' => 'wpsc-product',
  133. 'numberposts' => $number,
  134. 'orderby' => 'post_date',
  135. 'post_parent' => 0,
  136. 'post_status' => 'publish',
  137. 'order' => 'ASC'
  138. ) );
  139. $output = '';
  140.  
  141. $total = count( $latest_products );
  142. $i = 0;
  143. if ( count( $latest_products ) > 0 ) {
  144.  
  145.  
  146. $output .= '<div class="grid no-border" ><ul>';
  147. foreach ( $latest_products as $latest_product ) {
  148.  
  149. //print_r($latest_product);
  150. $i++;
  151. if ( !($i%4)) $class = 'last'; else $class = '';
  152. $output .= '<li class="item '.$class.' ">';
  153. // Thumbnails, if required
  154. if ($image) {
  155. $output .= '<a class="product-image" href="' . wpsc_product_url( $latest_product->ID, null ) . '">';
  156. $attached_images = (array)get_posts( array(
  157. 'post_type' => 'attachment',
  158. 'numberposts' => 1,
  159. 'post_status' => null,
  160. 'post_parent' => $latest_product->ID,
  161. 'orderby' => 'menu_order',
  162. 'order' => 'ASC'
  163. ) );
  164. $attached_image = $attached_images[0];
  165. if ( $attached_image->ID > 0 )
  166. $output .= '<img class="product_image" src="' . wpsc_product_image( $attached_image->ID, $width, $height ) . '" title="' . $latest_product->post_title . '" alt="' . $latest_product->post_title . '" />';
  167. else
  168. $output .='<img class="no-image product_image" id="product_image_'.wpsc_the_product_id().'" alt="No Image" title="'.wpsc_the_product_title().'" src="'.WPSC_URL.'/wpsc-theme/images/noimage.png" width="' . $width . '" height="' . $height . '" />';
  169.  
  170. $output .= '</a>';
  171.  
  172. }
  173. // Link
  174. $output .= '<h3 class="product-name"><a href="' . wpsc_product_url( $latest_product->ID, null ) . '" class="wpsc-product-title">'.stripslashes( $latest_product->post_title ).'</a></h3>';
  175.  
  176.  
  177.  
  178. $output .= '<div class="action">';
  179.  
  180. $output .= '<div class="price-box left">';
  181. //$output .= $latest_product->ID;
  182. //$output .= wpsc_calculate_price( wpsc_the_product_id(),null,false );
  183. $output .= wpsc_currency_display(get_post_meta( $latest_product->ID, '_wpsc_price', true ));
  184. $output.= '</div>';
  185.  
  186. $output .= '<a class="button right" href="' . wpsc_product_url( $latest_product->ID, null ) . '"><span>'.__( 'Details', 'wpsc' ).'</span></a>';
  187.  
  188. $output .= '<div class="clear"></div></div>';
  189.  
  190. $output .= '</li>';
  191. }
  192. $output .= "</ul></div><div class=\"clear\"></div>";
  193. }
  194. echo $output;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement