/wpsc-widgets/donations_widget.php

https://github.com/Matmon/WP-e-Commerce · PHP · 181 lines · 98 code · 40 blank · 43 comment · 6 complexity · 67c5f9d164bd0e62490e3af183e6b66b MD5 · raw file

  1. <?php
  2. /**
  3. * Donations widget class
  4. *
  5. * @since 3.8
  6. */
  7. class WP_Widget_Donations extends WP_Widget {
  8. /**
  9. * Widget Constuctor
  10. */
  11. function WP_Widget_Donations() {
  12. $widget_ops = array(
  13. 'classname' => 'widget_wpsc_donations',
  14. 'description' => __( 'Donations Widget', 'wpsc' )
  15. );
  16. $this->WP_Widget( 'wpsc_donations', __( 'Product Donations', 'wpsc' ), $widget_ops );
  17. }
  18. /**
  19. * Widget Output
  20. *
  21. * @param $args (array)
  22. * @param $instance (array) Widget values.
  23. *
  24. */
  25. function widget( $args, $instance ) {
  26. global $wpdb, $table_prefix;
  27. extract( $args );
  28. $donation_count = $wpdb->get_var( "SELECT COUNT(DISTINCT `p`.`ID`) AS `count`
  29. FROM `" . $wpdb->postmeta . "` AS `m`
  30. JOIN `" . $wpdb->posts . "` AS `p` ON `m`.`post_id` = `p`.`ID`
  31. WHERE `p`.`post_parent` IN ('0')
  32. AND `m`.`meta_key` IN ('_wpsc_is_donation')
  33. AND `m`.`meta_value` IN( '1' )
  34. AND `p`.`post_status` = 'publish'" );
  35. if ( $donation_count > 0 ) {
  36. echo $before_widget;
  37. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Product Donations', 'wpsc' ) : $instance['title'] );
  38. if ( $title ) {
  39. echo $before_title . $title . $after_title;
  40. }
  41. wpsc_donations();
  42. echo $after_widget;
  43. }
  44. }
  45. /**
  46. * Update Widget
  47. *
  48. * @param $new_instance (array) New widget values.
  49. * @param $old_instance (array) Old widget values.
  50. *
  51. * @return (array) New values.
  52. */
  53. function update( $new_instance, $old_instance ) {
  54. $instance = $old_instance;
  55. $instance['title'] = strip_tags( $new_instance['title'] );
  56. return $instance;
  57. }
  58. /**
  59. * Widget Options Form
  60. *
  61. * @param $instance (array) Widget values.
  62. */
  63. function form( $instance ) {
  64. global $wpdb;
  65. // Defaults
  66. $instance = wp_parse_args( (array)$instance, array(
  67. 'title' => __( 'Product Donations', 'wpsc' )
  68. ) );
  69. // Values
  70. $title = esc_attr( $instance['title'] );
  71. ?>
  72. <p>
  73. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'wpsc' ); ?></label>
  74. <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; ?>" />
  75. </p>
  76. <?php
  77. }
  78. }
  79. add_action( 'widgets_init', create_function( '', 'return register_widget("WP_Widget_Donations");' ) );
  80. /*
  81. * Specials Widget content function
  82. * Displays the products
  83. *
  84. * @todo make this use wp_query and a theme file if possible
  85. *
  86. * Changes made in 3.8 that may affect users:
  87. *
  88. * 1. $input does not get prepended to output.
  89. */
  90. function wpsc_donations( $args = null ) {
  91. global $wpdb;
  92. // Args not used yet but this is ready for when it is
  93. $args = wp_parse_args( (array)$args, array() );
  94. $siteurl = get_option( 'siteurl' );
  95. $products = $wpdb->get_results( "SELECT DISTINCT `p` . * , `m`.`meta_value` AS `special_price`
  96. FROM `" . $wpdb->postmeta . "` AS `m`
  97. JOIN `" . $wpdb->posts . "` AS `p` ON `m`.`post_id` = `p`.`ID`
  98. WHERE `p`.`post_parent` IN ('0')
  99. AND `m`.`meta_key` IN ('_wpsc_is_donation')
  100. AND `m`.`meta_value` IN( '1' )
  101. ORDER BY RAND( )
  102. LIMIT 1", ARRAY_A );
  103. $output = '';
  104. if ( $products != null ) {
  105. foreach ( $products as $product ) {
  106. $attached_images = (array)get_posts( array(
  107. 'post_type' => 'attachment',
  108. 'numberposts' => 1,
  109. 'post_status' => null,
  110. 'post_parent' => $product['ID'],
  111. 'orderby' => 'menu_order',
  112. 'order' => 'ASC'
  113. ) );
  114. $attached_image = $attached_images[0];
  115. $output .= "<div class='wpsc_product_donation'>";
  116. if ( ( $attached_image->ID > 0 ) ) {
  117. $output .= "<img src='" . wpsc_product_image( $attached_image->ID, get_option( 'product_image_width' ), get_option( 'product_image_height' ) ) . "' title='" . $product['post_title'] . "' alt='" . esc_attr( $product['post_title'] ) . "' /><br />";
  118. }
  119. // Get currency options
  120. $currency_sign_location = get_option( 'currency_sign_location' );
  121. $currency_type = get_option( 'currency_type' );
  122. $currency_symbol = $wpdb->get_var( "SELECT `symbol_html` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id`='" . $currency_type . "' LIMIT 1" );
  123. $price = get_post_meta( $product['ID'] , '_wpsc_price', true );
  124. // Output
  125. $output .= "<strong>" . $product['post_title'] . "</strong><br />";
  126. $output .= $product['post_content'] . "<br />";
  127. $output .= "<form class='product_form' name='donation_widget_" . $product['ID'] . "' method='post' action='' id='donation_widget_" . $product['ID'] . "'>";
  128. $output .= "<input type='hidden' name='product_id' value='" . $product['ID'] . "'/>";
  129. $output .= "<input type='hidden' name='item' value='" . $product['ID'] . "' />";
  130. $output .= "<input type='hidden' name='wpsc_ajax_action' value='add_to_cart' />";
  131. $output .= "<label for='donation_widget_price_" . $product['ID'] . "'>" . __( 'Donation', 'wpsc' ) . ":</label> $currency_symbol<input type='text' id='donation_widget_price_" . $product['ID'] . "' name='donation_price' value='" . esc_attr( number_format( $price, 2 ) ) . "' size='6' /><br />";
  132. $output .= "<input type='submit' id='donation_widget_" . $product['ID'] . "_submit_button' name='Buy' class='wpsc_buy_button' value='" . __( 'Add To Cart', 'wpsc' ) . "' />";
  133. $output .= "</form>";
  134. $output .= "</div>";
  135. }
  136. } else {
  137. $output = '';
  138. }
  139. echo $output;
  140. }
  141. ?>