1: <?php
2: /**
3: * Graphing Functions
4: *
5: * @package EPL
6: * @subpackage Admin/ReportsGraphing
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Show report graphs
19: *
20: * @since 3.0
21: * @param string $sold_status Listing sold status.
22: * @param string $current_status Listing current status.
23: * @param string $sold_color Listing sold hex color.
24: * @param string $current_color Listing current hex color.
25: * @return void
26: */
27: function epl_reports_graph( $sold_status = 'sold', $current_status = 'current', $sold_color = null, $current_color = null ) {
28: // Retrieve the queried dates.
29: $dates = epl_get_report_dates();
30: // phpcs:disable
31: // Determine graph options.
32: switch ( $dates['range'] ) :
33: case 'today':
34: case 'yesterday':
35: $day_by_day = true;
36: break;
37: case 'last_year':
38: case 'this_year':
39: case 'last_quarter':
40: case 'this_quarter':
41: $day_by_day = false;
42: break;
43: case 'other':
44: if ( $dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] && ( '12' !== $dates['m_start'] && '1' !== $dates['m_end'] ) ) {
45: $day_by_day = false;
46: } else {
47: $day_by_day = true;
48: }
49: break;
50: default:
51: $day_by_day = true;
52: break;
53: endswitch;
54:
55: $current_listings_totals = 0.00; // Total current_listings for time period shown.
56: $sales_totals = 0; // Total sales for time period shown.
57:
58: $listings_data = array();
59: $sales_data = array();
60:
61: if ( 'today' === $dates['range'] || 'yesterday' === $dates['range'] ) {
62: // Hour by hour.
63: $hour = 1;
64: $month = $dates['m_start'];
65: $listings_totals = 0;
66: while ( $hour <= 23 ) {
67:
68: $sales = epl_get_sales_by_date( $dates['day'], $month, $dates['year'], $hour, $sold_status, $day_by_day );
69: $current_listings = epl_get_sales_by_date( $dates['day'], $month, $dates['year'], $hour, $current_status, $day_by_day );
70:
71: $sales_totals += $sales;
72: $listings_totals += $current_listings;
73:
74: $date = mktime( $hour, 0, 0, $month, $dates['day'], $dates['year'] ) * 1000;
75: $sales_data[] = array( $date, $sales );
76: $current_listings_data[] = array( $date, $current_listings );
77:
78: $hour++;
79: } // phpcs:enable
80: } elseif ( 'this_week' === $dates['range'] || 'last_week' === $dates['range'] ) {
81:
82: $num_of_days = cal_days_in_month( CAL_GREGORIAN, $dates['m_start'], $dates['year'] );
83:
84: $report_dates = array();
85: $i = 0;
86: while ( $i <= 6 ) {
87:
88: if ( ( $dates['day'] + $i ) <= $num_of_days ) {
89: $report_dates[ $i ] = array(
90: 'day' => (string) $dates['day'] + $i,
91: 'month' => $dates['m_start'],
92: 'year' => $dates['year'],
93: );
94: } else {
95: $report_dates[ $i ] = array(
96: 'day' => (string) $i,
97: 'month' => $dates['m_end'],
98: 'year' => $dates['year_end'],
99: );
100: }
101:
102: ++$i;
103: }
104:
105: foreach ( $report_dates as $report_date ) {
106: $sales = epl_get_sales_by_date( $report_date['day'], $report_date['month'], $report_date['year'], $sold_status, $day_by_day );
107: $sales_totals += $sales;
108:
109: $current_listings = epl_get_sales_by_date( $report_date['day'], $report_date['month'], $report_date['year'], null, $current_status, $day_by_day );
110: $current_listings_totals += $current_listings;
111:
112: $date = mktime( 0, 0, 0, $report_date['month'], $report_date['day'], $report_date['year'] ) * 1000;
113: $sales_data[] = array( $date, $sales );
114: $current_listings_data[] = array( $date, $current_listings );
115: }
116: } else {
117:
118: $y = $dates['year'];
119:
120: while ( $y <= $dates['year_end'] ) {
121:
122: $last_year = false;
123:
124: if ( $dates['year'] === $dates['year_end'] ) {
125: $month_start = $dates['m_start'];
126: $month_end = $dates['m_end'];
127: $last_year = true;
128: } elseif ( $y === $dates['year'] ) {
129: $month_start = $dates['m_start'];
130: $month_end = 12;
131: } elseif ( $y === $dates['year_end'] ) {
132: $month_start = 01;
133: $month_end = $dates['m_end'];
134: } else {
135: $month_start = 01;
136: $month_end = 12;
137: }
138:
139: $i = $month_start;
140: while ( $i <= $month_end ) {
141:
142: if ( $day_by_day ) {
143:
144: $d = $dates['day'];
145:
146: if ( $i === $month_end ) {
147:
148: $num_of_days = $dates['day_end'];
149:
150: if ( $month_start < $month_end ) {
151:
152: $d = 1;
153:
154: }
155: } else {
156:
157: $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
158:
159: }
160:
161: while ( $d <= $num_of_days ) {
162:
163: $sales = epl_get_sales_by_date( $d, $i, $y, null, $sold_status, $day_by_day );
164: $sales_totals += $sales;
165:
166: $current_listings = epl_get_sales_by_date( $d, $i, $y, null, $current_status, $day_by_day );
167: $current_listings_totals += $current_listings;
168:
169: $date = mktime( 0, 0, 0, $i, $d, $y ) * 1000;
170: $sales_data[] = array( $date, $sales );
171: $current_listings_data[] = array( $date, $current_listings );
172: ++$d;
173:
174: }
175: } else {
176:
177: $sales = epl_get_sales_by_date( null, $i, $y, null, $sold_status, $day_by_day );
178: $sales_totals += $sales;
179:
180: $current_listings = epl_get_sales_by_date( null, $i, $y, null, $current_status, $day_by_day );
181: $current_listings_totals += $current_listings;
182:
183: if ( $i === $month_end && $last_year ) {
184:
185: $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
186:
187: } else {
188:
189: $num_of_days = 1;
190:
191: }
192:
193: $date = mktime( 0, 0, 0, $i, $num_of_days, $y ) * 1000;
194: $sales_data[] = array( $date, $sales );
195: $current_listings_data[] = array( $date, $current_listings );
196:
197: }
198:
199: ++$i;
200:
201: }
202:
203: ++$y;
204: }
205: }
206:
207: $data = array(
208: $current_status => array(
209: 'label' => ucfirst( $current_status ),
210: 'id' => $current_status,
211: 'data' => $current_listings_data,
212: ),
213: $sold_status => array(
214: 'label' => ucfirst( $sold_status ),
215: 'id' => $current_status,
216: 'data' => $sales_data,
217: ),
218: );
219:
220: ! is_null( $sold_color ) ? $data[ $sold_status ]['color'] = $sold_color : null;
221: ! is_null( $current_color ) ? $data[ $current_status ]['color'] = $current_color : null;
222:
223: // Start our own output buffer.
224: ob_start();
225: ?>
226: <div id="epl-dashboard-widgets-wrap">
227: <div class="metabox-holder" style="padding-top: 0;">
228: <div class="postbox">
229: <h3><span><?php esc_html_e( 'Listings Over Time', 'easy-property-listings' ); ?></span></h3>
230:
231: <div class="inside">
232: <?php
233: epl_reports_graph_controls();
234: $graph = new EPL_Graph( $data );
235: $graph->set( 'x_mode', 'time' );
236: $graph->set( 'multiple_y_axes', true );
237: $graph->display();
238:
239: ?>
240:
241: <p class="epl_graph_totals">
242: <strong>
243: <?php
244: // Translators: status label.
245: printf( esc_html__( 'Total %s listings period shown : ', 'easy-property-listings' ), esc_attr( ucfirst( $current_status ) ) );
246:
247: echo esc_attr( epl_format_amount( $current_listings_totals ) );
248: ?>
249: </strong>
250: </p>
251: <p class="epl_graph_totals">
252: <strong>
253: <?php
254: // Translators: status label.
255: printf( esc_html__( 'Total %s listings period shown : ', 'easy-property-listings' ), esc_attr( ucfirst( $sold_status ) ) );
256:
257: echo esc_attr( epl_format_amount( $sales_totals ) );
258: ?>
259: </strong>
260: </p>
261:
262:
263: <?php do_action( 'epl_reports_graph_additional_stats' ); ?>
264:
265: </div>
266: </div>
267: </div>
268: </div>
269: <?php
270: // Get output buffer contents and end our own buffer.
271: $output = ob_get_contents();
272: ob_end_clean();
273:
274: echo $output; //phpcs:ignore
275: }
276:
277: /**
278: * Grabs all of the selected date info and then redirects appropriately
279: *
280: * @since 3.3.3
281: *
282: * @param array $data Date data.
283: */
284: function epl_parse_report_dates( $data ) {
285:
286: $dates = epl_get_report_dates();
287: $view = epl_get_reporting_view();
288: wp_safe_redirect( add_query_arg( $dates, admin_url( 'admin.php?page=epl-reports&view=' . esc_attr( $view ) ) ) );
289: }
290: add_action( 'epl_filter_reports', 'epl_parse_report_dates' );
291:
292: /**
293: * Show report graph date filters
294: *
295: * @since 3.0
296: * @since 3.5 Added accessibility labels to select elements.
297: *
298: * @return void
299: */
300: function epl_reports_graph_controls() {
301: $date_options = apply_filters(
302: 'epl_report_date_options',
303: array(
304: 'today' => __( 'Today', 'easy-property-listings' ),
305: 'yesterday' => __( 'Yesterday', 'easy-property-listings' ),
306: 'this_week' => __( 'This Week', 'easy-property-listings' ),
307: 'last_week' => __( 'Last Week', 'easy-property-listings' ),
308: 'this_month' => __( 'This Month', 'easy-property-listings' ),
309: 'last_month' => __( 'Last Month', 'easy-property-listings' ),
310: 'this_quarter' => __( 'This Quarter', 'easy-property-listings' ),
311: 'last_quarter' => __( 'Last Quarter', 'easy-property-listings' ),
312: 'this_year' => __( 'This Year', 'easy-property-listings' ),
313: 'last_year' => __( 'Last Year', 'easy-property-listings' ),
314: 'other' => __( 'Custom', 'easy-property-listings' ),
315: )
316: );
317:
318: $dates = epl_get_report_dates();
319: $display = 'other' === $dates['range'] ? '' : 'style="display:none;"';
320: $view = epl_get_reporting_view();
321:
322: if ( empty( $dates['day_end'] ) ) {
323: $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, gmdate( 'n' ), gmdate( 'Y' ) );
324: }
325:
326: ?>
327: <form id="epl-graphs-filter" method="get">
328: <div class="tablenav top">
329: <div class="alignleft actions">
330:
331: <input type="hidden" name="page" value="epl-reports"/>
332: <input type="hidden" name="view" value="<?php echo esc_attr( $view ); ?>"/>
333:
334: <select aria-label="<?php esc_attr_e( 'Date Options', 'easy-property-listings' ); ?>" id="epl-graphs-date-options" name="range">
335: <?php foreach ( $date_options as $key => $option ) : ?>
336: <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $dates['range'] ); ?>><?php echo esc_html( $option ); ?></option>
337: <?php endforeach; ?>
338: </select>
339:
340: <div id="epl-date-range-options" <?php echo esc_attr( $display ); ?>>
341: <span><?php esc_html_e( 'From', 'easy-property-listings' ); ?>&nbsp;</span>
342: <select aria-label="<?php esc_attr_e( 'Start month', 'easy-property-listings' ); ?>" id="epl-graphs-month-start" name="m_start">
343: <?php for ( $i = 1; $i <= 12; $i++ ) : ?>
344: <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['m_start'] ); ?>><?php echo esc_attr( epl_month_num_to_name( $i ) ); ?></option>
345: <?php endfor; ?>
346: </select>
347: <select aria-label="<?php esc_attr_e( 'Day', 'easy-property-listings' ); ?>" id="epl-graphs-day-start" name="day">
348: <?php for ( $i = 1; $i <= 31; $i++ ) : ?>
349: <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['day'] ); ?>><?php echo esc_attr( $i ); ?></option>
350: <?php endfor; ?>
351: </select>
352: <select aria-label="<?php esc_attr_e( 'Year', 'easy-property-listings' ); ?>" id="epl-graphs-year-start" name="year">
353: <?php $curr_year = gmdate( 'Y' ); ?>
354:
355: <?php for ( $i = 2007; $i <= $curr_year; $i++ ) : ?>
356: <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['year'] ); ?>><?php echo esc_attr( $i ); ?></option>
357: <?php endfor; ?>
358: </select>
359: <span><?php esc_html_e( 'To', 'easy-property-listings' ); ?>&nbsp;</span>
360: <select aria-label="<?php esc_attr_e( 'Month End', 'easy-property-listings' ); ?>" id="epl-graphs-month-end" name="m_end">
361: <?php for ( $i = 1; $i <= 12; $i++ ) : ?>
362: <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['m_end'] ); ?>><?php echo esc_attr( epl_month_num_to_name( $i ) ); ?></option>
363: <?php endfor; ?>
364: </select>
365: <select aria-label="<?php esc_attr_e( 'Day end', 'easy-property-listings' ); ?>" id="epl-graphs-day-end" name="day_end">
366: <?php for ( $i = 1; $i <= 31; $i++ ) : ?>
367: <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['day_end'] ); ?>><?php echo esc_attr( $i ); ?></option>
368: <?php endfor; ?>
369: </select>
370: <select aria-label="<?php esc_attr_e( 'Year end', 'easy-property-listings' ); ?>" id="epl-graphs-year-end" name="year_end">
371: <?php for ( $i = 2007; $i <= $curr_year; $i++ ) : ?>
372: <option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['year_end'] ); ?>><?php echo esc_attr( $i ); ?></option>
373: <?php endfor; ?>
374: </select>
375: </div>
376:
377: <div class="epl-graph-filter-submit graph-option-section">
378: <input type="hidden" name="epl_action" value="filter_reports" />
379: <input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Filter', 'easy-property-listings' ); ?>"/>
380: </div>
381: </div>
382: </div>
383: </form>
384: <?php
385: }
386:
387: /**
388: * Sets up the dates used to filter graph data
389: *
390: * Date sent via $_GET is read first and then modified (if needed) to match the
391: * selected date-range (if any)
392: *
393: * @since 3.0
394: * @return array
395: */
396: function epl_get_report_dates() {
397:
398: // phpcs:disable WordPress.Security.NonceVerification
399:
400: $dates = array();
401:
402: $current_time = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
403:
404: $dates['range'] = isset( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : 'this_month';
405:
406: if ( 'custom' !== $dates['range'] ) {
407: $dates['year'] = isset( $_GET['year'] ) ? sanitize_text_field( wp_unslash( $_GET['year'] ) ) : gmdate( 'Y' );
408: $dates['year_end'] = isset( $_GET['year_end'] ) ? sanitize_text_field( wp_unslash( $_GET['year_end'] ) ) : gmdate( 'Y' );
409: $dates['m_start'] = isset( $_GET['m_start'] ) ? sanitize_text_field( wp_unslash( $_GET['m_start'] ) ) : 1;
410: $dates['m_end'] = isset( $_GET['m_end'] ) ? sanitize_text_field( wp_unslash( $_GET['m_end'] ) ) : 12;
411: $dates['day'] = isset( $_GET['day'] ) ? sanitize_text_field( wp_unslash( $_GET['day'] ) ) : 1;
412: $dates['day_end'] = isset( $_GET['day_end'] ) ? sanitize_text_field( wp_unslash( $_GET['day_end'] ) ) : cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
413: }
414:
415: // Modify dates based on predefined ranges.
416: switch ( $dates['range'] ) :
417:
418: case 'this_month':
419: $dates['m_start'] = gmdate( 'n', $current_time );
420: $dates['m_end'] = gmdate( 'n', $current_time );
421: $dates['day'] = 1;
422: $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
423: $dates['year'] = gmdate( 'Y' );
424: $dates['year_end'] = gmdate( 'Y' );
425: break;
426:
427: case 'last_month':
428: if ( gmdate( 'n' ) === 1 ) {
429: $dates['m_start'] = 12;
430: $dates['m_end'] = 12;
431: $dates['year'] = gmdate( 'Y', $current_time ) - 1;
432: $dates['year_end'] = gmdate( 'Y', $current_time ) - 1;
433: } else {
434: $dates['m_start'] = gmdate( 'n' ) - 1;
435: $dates['m_end'] = gmdate( 'n' ) - 1;
436: $dates['year_end'] = $dates['year'];
437: }
438: $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
439: break;
440:
441: case 'today':
442: $dates['day'] = gmdate( 'd', $current_time );
443: $dates['m_start'] = gmdate( 'n', $current_time );
444: $dates['m_end'] = gmdate( 'n', $current_time );
445: $dates['year'] = gmdate( 'Y', $current_time );
446: break;
447:
448: case 'yesterday':
449: $year = gmdate( 'Y', $current_time );
450: $month = gmdate( 'n', $current_time );
451: $day = gmdate( 'd', $current_time );
452:
453: if ( 1 === $month && 1 === $day ) {
454:
455: --$year;
456: $month = 12;
457: $day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
458:
459: } elseif ( $month > 1 && 1 === $day ) {
460:
461: --$month;
462: $day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
463:
464: } else {
465:
466: --$day;
467:
468: }
469:
470: $dates['day'] = $day;
471: $dates['m_start'] = $month;
472: $dates['m_end'] = $month;
473: $dates['year'] = $year;
474: $dates['year_end'] = $year;
475: break;
476:
477: case 'this_week':
478: case 'last_week':
479: $base_time = 'this_week' === $dates['range'] ? current_time( 'mysql' ) : gmdate( 'Y-n-d h:i:s', current_time( 'timestamp' ) - WEEK_IN_SECONDS ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
480: $start_end = get_weekstartend( $base_time, get_option( 'start_of_week' ) );
481:
482: $dates['day'] = gmdate( 'd', $start_end['start'] );
483: $dates['m_start'] = gmdate( 'n', $start_end['start'] );
484: $dates['year'] = gmdate( 'Y', $start_end['start'] );
485:
486: $dates['day_end'] = gmdate( 'd', $start_end['end'] );
487: $dates['m_end'] = gmdate( 'n', $start_end['end'] );
488: $dates['year_end'] = gmdate( 'Y', $start_end['end'] );
489: break;
490:
491: case 'this_quarter':
492: $month_now = gmdate( 'n', $current_time );
493: $dates['year'] = gmdate( 'Y', $current_time );
494: $dates['year_end'] = $dates['year'];
495:
496: if ( $month_now <= 3 ) {
497:
498: $dates['m_start'] = 1;
499: $dates['m_end'] = 4;
500:
501: } elseif ( $month_now <= 6 ) {
502:
503: $dates['m_start'] = 4;
504: $dates['m_end'] = 7;
505:
506: } elseif ( $month_now <= 9 ) {
507:
508: $dates['m_start'] = 7;
509: $dates['m_end'] = 10;
510:
511: } else {
512:
513: $dates['m_start'] = 10;
514: $dates['m_end'] = 1;
515: }
516:
517: $dates['day'] = 1;
518: $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
519:
520: break;
521:
522: case 'last_quarter':
523: $month_now = gmdate( 'n' );
524:
525: if ( $month_now <= 3 ) {
526:
527: $dates['m_start'] = 10;
528: $dates['m_end'] = 12;
529: $dates['year'] = gmdate( 'Y', $current_time ) - 1; // Previous year.
530:
531: } elseif ( $month_now <= 6 ) {
532:
533: $dates['m_start'] = 1;
534: $dates['m_end'] = 3;
535: $dates['year'] = gmdate( 'Y', $current_time );
536:
537: } elseif ( $month_now <= 9 ) {
538:
539: $dates['m_start'] = 4;
540: $dates['m_end'] = 6;
541: $dates['year'] = gmdate( 'Y', $current_time );
542:
543: } else {
544:
545: $dates['m_start'] = 7;
546: $dates['m_end'] = 9;
547: $dates['year'] = gmdate( 'Y', $current_time );
548:
549: }
550:
551: $dates['day'] = 1;
552: $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
553: $dates['year_end'] = $dates['year'];
554:
555: break;
556:
557: case 'this_year':
558: $dates['day'] = 1;
559: $dates['m_start'] = 1;
560: $dates['m_end'] = 12;
561: $dates['year'] = gmdate( 'Y', $current_time );
562: $dates['year_end'] = $dates['year'];
563:
564: break;
565:
566: case 'last_year':
567: $dates['day'] = 1;
568: $dates['m_start'] = 1;
569: $dates['m_end'] = 12;
570: $dates['year'] = gmdate( 'Y', $current_time ) - 1;
571: $dates['year_end'] = gmdate( 'Y', $current_time ) - 1;
572: break;
573: //phpcs:disable
574: endswitch;
575:
576: return apply_filters( 'epl_report_dates', $dates );
577: }
578: //phpcs:enable
579: