1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20
21 function edd_reports_graph() {
22
23 $dates = edd_get_report_dates();
24
25
26 switch ( $dates['range'] ) :
27 case 'today' :
28 $time_format = '%d/%b';
29 $tick_size = 'hour';
30 $day_by_day = true;
31 break;
32 case 'last_year' :
33 $time_format = '%b';
34 $tick_size = 'month';
35 $day_by_day = false;
36 break;
37 case 'this_year' :
38 $time_format = '%b';
39 $tick_size = 'month';
40 $day_by_day = false;
41 break;
42 case 'last_quarter' :
43 $time_format = '%b';
44 $tick_size = 'month';
45 $day_by_day = false;
46 break;
47 case 'this_quarter' :
48 $time_format = '%b';
49 $tick_size = 'month';
50 $day_by_day = false;
51 break;
52 case 'other' :
53 if( ( $dates['m_end'] - $dates['m_start'] ) >= 2 ) {
54 $time_format = '%b';
55 $tick_size = 'month';
56 $day_by_day = false;
57 } else {
58 $time_format = '%d/%b';
59 $tick_size = 'day';
60 $day_by_day = true;
61 }
62 break;
63 default:
64 $time_format = '%d/%b';
65 $tick_size = 'day';
66 $day_by_day = true;
67 break;
68 endswitch;
69
70 $time_format = apply_filters( 'edd_graph_timeformat', $time_format );
71 $tick_size = apply_filters( 'edd_graph_ticksize', $tick_size );
72 $totals = (float) 0.00;
73 $sales_totals = 0;
74
75 ob_start(); ?>
76 <script type="text/javascript">
77 jQuery( document ).ready( function($) {
78 $.plot(
79 $("#edd_monthly_stats"),
80 [{
81 data: [
82 <?php
83
84 if( $dates['range'] == 'today' ) {
85
86 $hour = 1;
87 $month = date( 'n' );
88 while ( $hour <= 23 ) :
89 $sales = edd_get_sales_by_date( $dates['day'], $month, $dates['year'], $hour );
90 $sales_totals += $sales;
91 $date = mktime( $hour, 0, 0, $month, $dates['day'], $dates['year'] ); ?>
92 [<?php echo $date * 1000; ?>, <?php echo $sales; ?>],
93 <?php
94 $hour++;
95 endwhile;
96
97 } elseif( $dates['range'] == 'this_week' || $dates['range'] == 'last_week' ) {
98
99
100 $day = $dates['day'];
101 $day_end = $dates['day_end'];
102 $month = $dates['m_start'];
103 while ( $day <= $day_end ) :
104 $sales = edd_get_sales_by_date( $day, $month, $dates['year'] );
105 $sales_totals += $sales;
106 $date = mktime( 0, 0, 0, $month, $day, $dates['year'] ); ?>
107 [<?php echo $date * 1000; ?>, <?php echo $sales; ?>],
108 <?php
109 $day++;
110 endwhile;
111
112 } else {
113
114 $i = $dates['m_start'];
115 while ( $i <= $dates['m_end'] ) :
116 if ( $day_by_day ) :
117 $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $dates['year'] );
118 $d = 1;
119 while ( $d <= $num_of_days ) :
120 $sales = edd_get_sales_by_date( $d, $i, $dates['year'] );
121 $sales_totals += $sales;
122 $date = mktime( 0, 0, 0, $i, $d, $dates['year'] ); ?>
123 [<?php echo $date * 1000; ?>, <?php echo $sales; ?>],
124 <?php
125 $d++;
126 endwhile;
127 else :
128 $date = mktime( 0, 0, 0, $i, 1, $dates['year'] );
129 ?>
130 [<?php echo $date * 1000; ?>, <?php echo edd_get_sales_by_date( null, $i, $dates['year'] ); ?>],
131 <?php
132 endif;
133 $i++;
134 endwhile;
135
136 }
137
138 ?>,
139 ],
140 yaxis: 2,
141 label: "<?php _e( 'Sales', 'edd' ); ?>",
142 id: 'sales'
143 },
144 {
145 data: [
146 <?php
147
148 if( $dates['range'] == 'today' ) {
149
150
151 $hour = 1;
152 $month = date( 'n' );
153 while ( $hour <= 23 ) :
154 $earnings = edd_get_earnings_by_date( $dates['day'], $month, $dates['year'], $hour );
155 $totals += $earnings;
156 $date = mktime( $hour, 0, 0, $month, $dates['day'], $dates['year'] ); ?>
157 [<?php echo $date * 1000; ?>, <?php echo $earnings; ?>],
158 <?php
159 $hour++;
160 endwhile;
161
162 } elseif( $dates['range'] == 'this_week' || $dates['range'] == 'last_week' ) {
163
164
165 $day = $dates['day'];
166 $day_end = $dates['day_end'];
167 $month = $dates['m_start'];
168 while ( $day <= $day_end ) :
169 $earnings = edd_get_earnings_by_date( $day, $month, $dates['year'] );
170 $totals += $earnings;
171 $date = mktime( 0, 0, 0, $month, $day, $dates['year'] ); ?>
172 [<?php echo $date * 1000; ?>, <?php echo $earnings; ?>],
173 <?php
174 $day++;
175 endwhile;
176
177 } else {
178
179 $i = $dates['m_start'];
180 while ( $i <= $dates['m_end'] ) :
181 if ( $day_by_day ) :
182 $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $dates['year'] );
183 $d = 1;
184 while ( $d <= $num_of_days ) :
185 $date = mktime( 0, 0, 0, $i, $d, $dates['year'] );
186 $earnings = edd_get_earnings_by_date( $d, $i, $dates['year'] );
187 $totals += $earnings; ?>
188 [<?php echo $date * 1000; ?>, <?php echo $earnings ?>],
189 <?php $d++; endwhile;
190 else :
191 $date = mktime( 0, 0, 0, $i, 1, $dates['year'] );
192 $earnings = edd_get_earnings_by_date( null, $i, $dates['year'] );
193 $totals += $earnings;
194 ?>
195 [<?php echo $date * 1000; ?>, <?php echo $earnings; ?>],
196 <?php
197 endif;
198 $i++;
199 endwhile;
200
201 }
202
203 ?>
204 ],
205 label: "<?php _e( 'Earnings', 'edd' ); ?>",
206 id: 'earnings'
207 }],
208 {
209 series: {
210 lines: { show: true },
211 points: { show: true }
212 },
213 grid: {
214 show: true,
215 aboveData: false,
216 color: '#ccc',
217 backgroundColor: '#fff',
218 borderWidth: 2,
219 borderColor: '#ccc',
220 clickable: false,
221 hoverable: true
222 },
223 xaxis: {
224 mode: "time",
225 timeFormat: "<?php echo $time_format; ?>",
226 minTickSize: [1, "<?php echo $tick_size; ?>"]
227 },
228 yaxis: [
229 { min: 0, tickSize: 1, tickDecimals: 2 },
230 { min: 0, tickDecimals: 0 }
231 ]
232
233 });
234
235 function edd_flot_tooltip(x, y, contents) {
236 $('<div id="edd-flot-tooltip">' + contents + '</div>').css( {
237 position: 'absolute',
238 display: 'none',
239 top: y + 5,
240 left: x + 5,
241 border: '1px solid #fdd',
242 padding: '2px',
243 'background-color': '#fee',
244 opacity: 0.80
245 }).appendTo("body").fadeIn(200);
246 }
247
248 var previousPoint = null;
249 $("#edd_monthly_stats").bind("plothover", function (event, pos, item) {
250 $("#x").text(pos.x.toFixed(2));
251 $("#y").text(pos.y.toFixed(2));
252 if (item) {
253 if (previousPoint != item.dataIndex) {
254 previousPoint = item.dataIndex;
255 $("#edd-flot-tooltip").remove();
256 var x = item.datapoint[0].toFixed(2),
257 y = item.datapoint[1].toFixed(2);
258 if( item.series.id == 'earnings' ) {
259 if( edd_vars.currency_pos == 'before' ) {
260 edd_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + edd_vars.currency_sign + y );
261 } else {
262 edd_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y + edd_vars.currency_sign );
263 }
264 } else {
265 edd_flot_tooltip( item.pageX, item.pageY, item.series.label + ' ' + y.replace( '.00', '' ) );
266 }
267 }
268 } else {
269 $("#edd-flot-tooltip").remove();
270 previousPoint = null;
271 }
272 });
273 });
274 </script>
275
276 <div class="metabox-holder" style="padding-top: 0;">
277 <div class="postbox">
278 <h3><span><?php _e('Earnings Over Time', 'edd'); ?></span></h3>
279
280 <div class="inside">
281 <?php edd_reports_graph_controls(); ?>
282 <?php $estimated = edd_estimated_monthly_stats(); ?>
283 <div id="edd_monthly_stats" style="height: 300px;"></div>
284 <p class="edd_graph_totals"><strong><?php _e( 'Total earnings for period shown: ', 'edd' ); echo edd_currency_filter( edd_format_amount( $totals ) ); ?></strong></p>
285 <p class="edd_graph_totals"><strong><?php _e( 'Total sales for period shown: ', 'edd' ); echo $sales_totals; ?></strong></p>
286 <p class="edd_graph_totals"><strong><?php _e( 'Estimated monthly earnings: ', 'edd' ); echo edd_currency_filter( edd_format_amount( $estimated['earnings'] ) ); ?></strong></p>
287 <p class="edd_graph_totals"><strong><?php _e( 'Estimated monthly sales: ', 'edd' ); echo $estimated['sales']; ?></strong></p>
288 </div>
289 </div>
290 </div>
291 <?php
292 echo ob_get_clean();
293 }
294
295 296 297 298 299 300
301 function edd_reports_graph_controls() {
302 $date_options = apply_filters( 'edd_report_date_options', array(
303 'today' => __( 'Today', 'edd' ),
304 'this_week' => __( 'This Week', 'edd' ),
305 'last_week' => __( 'Last Week', 'edd' ),
306 'this_month' => __( 'This Month', 'edd' ),
307 'last_month' => __( 'Last Month', 'edd' ),
308 'this_quarter' => __( 'This Quarter', 'edd' ),
309 'last_quarter' => __( 'Last Quarter', 'edd' ),
310 'this_year' => __( 'This Year', 'edd' ),
311 'last_year' => __( 'Last Year', 'edd' ),
312 'other' => __( 'Custom', 'edd' )
313 ) );
314
315 $dates = edd_get_report_dates();
316
317 $display = $dates['range'] == 'other' ? '' : 'style="display:none;"';
318
319 $view = isset( $_GET['view'] ) ? $_GET['view'] : 'earnings';
320
321 ?>
322 <form id="edd-garphs-filter" method="get">
323 <div class="tablenav top">
324 <div class="alignleft actions">
325
326 <input type="hidden" name="post_type" value="download"/>
327 <input type="hidden" name="page" value="edd-reports"/>
328 <input type="hidden" name="view" value="<?php echo $view; ?>"/>
329
330 <select id="edd-graphs-date-options" name="range">
331 <?php
332 foreach ( $date_options as $key => $option ) {
333 echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $dates['range'] ) . '>' . esc_html( $option ) . '</option>';
334 }
335 ?>
336 </select>
337
338 <div id="edd-date-range-options" <?php echo $display; ?>>
339 <span><?php _e( 'From', 'edd' ); ?> </span>
340 <select id="edd-graphs-month-start" name="m_start">
341 <?php for ( $i = 1; $i <= 12; $i++ ) : ?>
342 <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['m_start'] ); ?>><?php echo edd_month_num_to_name( $i ); ?></option>
343 <?php endfor; ?>
344 </select>
345 <span><?php _e( 'To', 'edd' ); ?> </span>
346 <select id="edd-graphs-month-start" name="m_end">
347 <?php for ( $i = 1; $i <= 12; $i++ ) : ?>
348 <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['m_end'] ); ?>><?php echo edd_month_num_to_name( $i ); ?></option>
349 <?php endfor; ?>
350 </select>
351 <select id="edd-graphs-year" name="year">
352 <?php for ( $i = 2007; $i <= $dates['year_end']; $i++ ) : ?>
353 <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['year'] ); ?>><?php echo $i; ?></option>
354 <?php endfor; ?>
355 </select>
356 </div>
357
358 <input type="hidden" name="edd_action" value="filter_reports" />
359 <input type="submit" class="button-secondary" value="<?php _e( 'Filter', 'edd' ); ?>"/>
360 </div>
361 </div>
362 </form>
363 <?php
364 }
365
366 367 368 369 370 371 372 373 374
375 function edd_get_report_dates() {
376 $dates = array();
377
378 $dates['range'] = isset( $_GET['range'] ) ? $_GET['range'] : 'this_month';
379 $dates['day'] = isset( $_GET['day'] ) ? $_GET['day'] : null;
380 $dates['m_start'] = isset( $_GET['m_start'] ) ? $_GET['m_start'] : 1;
381 $dates['m_end'] = isset( $_GET['m_end'] ) ? $_GET['m_end'] : 12;
382 $dates['year'] = isset( $_GET['year'] ) ? $_GET['year'] : date( 'Y' );
383 $dates['year_end'] = date( 'Y' );
384
385
386 switch ( $dates['range'] ) :
387
388 case 'this_month' :
389 $dates['m_start'] = date( 'n' );
390 $dates['m_end'] = date( 'n' );
391 $dates['year'] = date( 'Y' );
392 break;
393
394 case 'last_month' :
395 if( $dates['m_start'] == 12 ) {
396 $dates['m_start'] = 12;
397 $dates['m_end'] = 12;
398 $dates['year'] = date( 'Y' ) - 1;
399 $dates['year_end']= date( 'Y' ) - 1;
400 } else {
401 $dates['m_start'] = date( 'n' ) - 1;
402 $dates['m_end'] = date( 'n' ) - 1;
403 $dates['year'] = date( 'Y' );
404 }
405 break;
406
407 case 'today' :
408 $dates['day'] = date( 'd' );
409 $dates['m_start'] = date( 'n' );
410 $dates['m_end'] = date( 'n' );
411 $dates['year'] = date( 'Y' );
412 break;
413
414 case 'this_week' :
415 $dates['day'] = date( 'd', time() - ( date( 'w' ) - 1 ) *60*60*24 );
416 $dates['day_end'] = $dates['day'] + 6;
417 $dates['m_start'] = date( 'n' );
418 $dates['m_end'] = date( 'n' );
419 $dates['year'] = date( 'Y' );
420 break;
421
422 case 'last_week' :
423 $dates['day'] = date( 'd', time() - ( date( 'w' ) - 1 ) *60*60*24 ) - 6;
424 $dates['day_end'] = $dates['day'] + 6;
425 $dates['m_start'] = date( 'n' );
426 $dates['m_end'] = date( 'n' );
427 $dates['year'] = date( 'Y' );
428 break;
429
430 case 'this_quarter' :
431 $month_now = date( 'n' );
432
433 if ( $month_now <= 3 ) {
434
435 $dates['m_start'] = 1;
436 $dates['m_end'] = 3;
437 $dates['year'] = date( 'Y' );
438
439 } else if ( $month_now <= 6 ) {
440
441 $dates['m_start'] = 4;
442 $dates['m_end'] = 6;
443 $dates['year'] = date( 'Y' );
444
445 } else if ( $month_now <= 9 ) {
446
447 $dates['m_start'] = 7;
448 $dates['m_end'] = 9;
449 $dates['year'] = date( 'Y' );
450
451 } else {
452
453 $dates['m_start'] = 10;
454 $dates['m_end'] = 12;
455 $dates['year'] = date( 'Y' );
456
457 }
458 break;
459
460 case 'last_quarter' :
461 $month_now = date( 'n' );
462
463 if ( $month_now <= 3 ) {
464
465 $dates['m_start'] = 10;
466 $dates['m_end'] = 12;
467 $dates['year'] = date( 'Y' ) - 1;
468
469 } else if ( $month_now <= 6 ) {
470
471 $dates['m_start'] = 1;
472 $dates['m_end'] = 3;
473 $dates['year'] = date( 'Y' );
474
475 } else if ( $month_now <= 9 ) {
476
477 $dates['m_start'] = 4;
478 $dates['m_end'] = 6;
479 $dates['year'] = date( 'Y' );
480
481 } else {
482
483 $dates['m_start'] = 7;
484 $dates['m_end'] = 9;
485 $dates['year'] = date( 'Y' );
486
487 }
488 break;
489
490 case 'this_year' :
491 $dates['m_start'] = 1;
492 $dates['m_end'] = 12;
493 $dates['year'] = date( 'Y' );
494 break;
495
496 case 'last_year' :
497 $dates['m_start'] = 1;
498 $dates['m_end'] = 12;
499 $dates['year'] = date( 'Y' ) - 1;
500 break;
501
502 endswitch;
503
504 return apply_filters( 'edd_report_dates', $dates );
505 }
506
507 508 509 510 511 512
513 function edd_parse_report_dates( $data ) {
514 $dates = edd_get_report_dates();
515
516 $view = isset( $_GET['view'] ) ? $_GET['view'] : 'earnings';
517
518 wp_redirect( add_query_arg( $dates, admin_url( 'edit.php?post_type=download&page=edd-reports&view=' . $view ) ) ); exit;
519 }
520 add_action( 'edd_filter_reports', 'edd_parse_report_dates' );