Easy Digital Downloads
  • Package
  • Class
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Classes

  • edd_cart_widget
  • edd_categories_tags_widget
  • edd_purchase_history_widget

Functions

  • edd_register_widgets
  1 <?php
  2 /**
  3  * Widgets
  4  *
  5  * @package     EDD
  6  * @subpackage  Widgets
  7  * @copyright   Copyright (c) 2013, Pippin Williamson
  8  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9  * @since       1.0
 10 */
 11 
 12 // Exit if accessed directly
 13 if ( ! defined( 'ABSPATH' ) ) exit;
 14 
 15 /*
 16 |--------------------------------------------------------------------------
 17 | FRONT-END WIDGETS
 18 |--------------------------------------------------------------------------
 19 |
 20 | - Cart Widget
 21 | - Categories / Tags Widget
 22 | - Purchase History Widget
 23 |
 24 */
 25 
 26 /**
 27  * Cart Widget
 28  *
 29  * Downloads cart widget class.
 30  *
 31  * @since 1.0
 32  * @return void
 33 */
 34 class edd_cart_widget extends WP_Widget {
 35     /** Constructor */
 36     function edd_cart_widget() {
 37         parent::WP_Widget( false, __( 'Downloads Cart', 'edd' ), array( 'description' => __( 'Display the downloads shopping cart', 'edd' ) ) );
 38     }
 39 
 40     /** @see WP_Widget::widget */
 41     function widget( $args, $instance ) {
 42         extract( $args );
 43         $title = apply_filters( 'widget_title', $instance['title'] );
 44         $quantity = isset( $instance['quantity'] ) ? $instance['quantity'] : false;
 45 
 46         global $post, $edd_options;
 47 
 48         echo $before_widget;
 49         if ( $title ) {
 50             if ( $quantity == 1 ) {
 51                 $quantity = ' - <span class="edd-cart-quantity">' . edd_get_cart_quantity() . '</span>';
 52             } else {
 53                 $quantity = '';
 54             }
 55             echo $before_title . $title . $quantity . $after_title;
 56         }
 57         do_action( 'edd_before_cart_widget' );
 58         edd_shopping_cart( true );
 59         do_action( 'edd_after_cart_widget' );
 60         echo $after_widget;
 61     }
 62 
 63     /** @see WP_Widget::update */
 64     function update( $new_instance, $old_instance ) {
 65         $instance = $old_instance;
 66         $instance['title'] = strip_tags( $new_instance['title'] );
 67         $instance['quantity'] = isset( $new_instance['quantity'] ) ? strip_tags( $new_instance['quantity'] ) : '';
 68         return $instance;
 69     }
 70 
 71     /** @see WP_Widget::form */
 72     function form( $instance ) {
 73         $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
 74         $quantity = isset( $instance['quantity'] ) ? esc_attr( $instance['quantity'] ) : '';
 75         ?>
 76     <p>
 77         <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'edd' ); ?></label>
 78         <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
 79                name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>"/>
 80     </p>
 81     <p>
 82         <label for="<?php echo $this->get_field_id( 'quantity' ); ?>"><?php _e( 'Show Quantity:', 'edd' ); ?></label>
 83         <input id="<?php echo $this->get_field_id( 'quantity' ); ?>"
 84                name="<?php echo $this->get_field_name( 'quantity' ); ?>" type="checkbox"
 85                value="1" <?php checked( '1', $quantity ); ?>/>
 86     </p>
 87     <?php
 88     }
 89 }
 90 
 91 /**
 92  * Categories / Tags Widget
 93  *
 94  * Downloads categories / tags widget class.
 95  *
 96  * @since 1.0
 97  * @return void
 98 */
 99 class edd_categories_tags_widget extends WP_Widget {
100     /** Constructor */
101     function edd_categories_tags_widget() {
102         parent::WP_Widget( false, __( 'Downloads Categories / Tags', 'edd' ), array( 'description' => __( 'Display the downloads categories or tags', 'edd' ) ) );
103     }
104 
105     /** @see WP_Widget::widget */
106     function widget( $args, $instance ) {
107         extract( $args );
108         $title = apply_filters('widget_title', $instance['title']);
109         $tax = $instance['taxonomy'];
110 
111         global $post, $edd_options;
112 
113         echo $before_widget;
114         if ( $title ) {
115             echo $before_title . $title . $after_title;
116         }
117 
118         do_action( 'edd_before_taxonomy_widget' );
119         $terms = get_terms( $tax );
120 
121         if ( is_wp_error( $terms ) ) {
122             return;
123         } else {
124             echo "<ul class=\"edd-taxonomy-widget\">\n";
125             foreach ( $terms as $term ) {
126                 echo '<li><a href="' . get_term_link( $term ) . '" title="' . esc_attr( $term->name ) . '" rel="bookmark">' . $term->name . '</a></li>'."\n";
127             }
128             echo "</ul>\n";
129         }
130 
131         do_action( 'edd_after_taxonomy_widget' );
132         echo $after_widget;
133     }
134 
135     /** @see WP_Widget::update */
136     function update( $new_instance, $old_instance ) {
137         $instance = $old_instance;
138         $instance['title'] = strip_tags( $new_instance['title'] );
139         $instance['taxonomy'] = strip_tags( $new_instance['taxonomy'] );
140         return $instance;
141     }
142 
143     /** @see WP_Widget::form */
144     function form( $instance ) {
145         $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
146         $taxonomy = isset( $instance['taxonomy'] ) ? esc_attr( $instance['taxonomy'] ) : 'download_category';
147         ?>
148         <p>
149             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'edd' ); ?></label>
150             <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
151                    name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>"/>
152         </p>
153         <p>
154             <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy:', 'edd' ); ?></label>
155             <select name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>">
156                 <option value="download_category" <?php selected( 'download_category', $taxonomy ); ?>><?php _e( 'Categories', 'edd' ); ?></option>
157                 <option value="download_tag" <?php selected( 'download_tag', $taxonomy ); ?>><?php _e( 'Tags', 'edd' ); ?></option>
158             </select>
159         </p>
160     <?php
161     }
162 }
163 
164 /**
165  * Purchase History Widget
166  *
167  * Displays a user's purchase history.
168  *
169  * @since 1.2
170  * @return void
171  */
172 class edd_purchase_history_widget extends WP_Widget {
173     /** Constructor */
174     function edd_purchase_history_widget() {
175         parent::WP_Widget( false, __( 'Purchase History', 'edd' ), array( 'description' => __( 'Display a user\'s purchase history', 'edd' ) ) );
176     }
177 
178     /** @see WP_Widget::widget */
179     function widget( $args, $instance ) {
180         extract( $args );
181         $title = apply_filters( 'widget_title', $instance['title'] );
182 
183         global $user_ID, $edd_options;
184 
185         if ( is_user_logged_in() ) {
186             $purchases = edd_get_users_purchases( $user_ID );
187 
188             if ( $purchases ) {
189                 echo $before_widget;
190                 if ( $title ) {
191                     echo $before_title . $title . $after_title;
192                 }
193 
194                 foreach ( $purchases as $purchase ) {
195                     $purchase_data = edd_get_payment_meta( $purchase->ID );
196                     $downloads = edd_get_payment_meta_downloads( $purchase->ID );
197 
198                     if ( $downloads ) {
199                         foreach ( $downloads as $download ) {
200                             $id = isset( $purchase_data['cart_details'] ) ? $download['id'] : $download;
201                             $price_id = isset( $download['options']['price_id'] ) ? $download['options']['price_id'] : null;
202                             $download_files = edd_get_download_files( $id, $price_id );
203                             echo '<div class="edd-purchased-widget-purchase edd-purchased-widget-purchase-' . $purchase->ID . '" id="edd-purchased-widget-purchase-' . $id . '">';
204                                 echo '<div class="edd-purchased-widget-purchase-name">' . get_the_title( $id ) . '</div>';
205                                 echo '<ul class="edd-purchased-widget-file-list">';
206 
207                                 if ( ! edd_no_redownload() ) {
208                                     if ( $download_files ) {
209                                         foreach ( $download_files as $filekey => $file ) {
210                                             $download_url = edd_get_download_file_url( $purchase_data['key'], $purchase_data['email'], $filekey, $id, $price_id );
211                                             echo '<li class="edd-purchased-widget-file"><a href="' . $download_url . '" class="edd-purchased-widget-file-link">' .  $file['name'] . '</a></li>';
212                                         }
213                                     } else {
214                                         echo '<li class="edd-purchased-widget-no-file">' . __( 'No downloadable files found.', 'edd' );
215                                     }
216                                 }
217 
218                                 echo '</ul>';
219                             echo '</div>';
220                         }
221                     }
222 
223                 }
224             }
225             echo $after_widget;
226         }
227     }
228 
229     /** @see WP_Widget::update */
230     function update( $new_instance, $old_instance ) {
231         $instance = $old_instance;
232         $instance['title'] = strip_tags( $new_instance['title'] );
233         return $instance;
234     }
235 
236     /** @see WP_Widget::form */
237     function form( $instance ) {
238         $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
239     ?>
240         <p>
241             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'edd' ); ?></label>
242             <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; ?>"/>
243         </p>
244     <?php
245     }
246 }
247 
248 /**
249  * Register Widgets
250  *
251  * Registers the EDD Widgets.
252  *
253  * @since 1.0
254  * @return void
255  */
256 function edd_register_widgets() {
257     register_widget( 'edd_cart_widget' );
258     register_widget( 'edd_categories_tags_widget' );
259     register_widget( 'edd_purchase_history_widget' );
260 }
261 add_action( 'widgets_init', 'edd_register_widgets' );
Easy Digital Downloads API documentation generated by ApiGen 2.8.0