PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
← All changes | includes/admin/tools/export/class-export-earnings.php +143 -94 2.3.1 → 4.18.0 View file →
@@ -4,17 +4,18 @@
4 4 *
5 5 * This class handles earnings export
6 6 *
7 7 * @package Give
8 - * @subpackage Admin/Reports
8 + * @since 1.0
9 9 * @copyright Copyright (c) 2016, GiveWP
10 10 * @license https://opensource.org/licenses/gpl-license GNU Public License
11 - * @since 1.0
11 + * @subpackage Admin/Reports
12 12 */
13 13
14 14 // Exit if accessed directly.
15 -if ( ! defined( 'ABSPATH' ) ) {
16 - exit;
15 +
16 +if (!defined('ABSPATH')) {
17 + exit;
17 18 }
18 19
19 20 /**
20 21 * Give_Earnings_Export Class
@@ -20,117 +21,165 @@
20 21 * Give_Earnings_Export Class
21 22 *
22 23 * @since 1.0
23 24 */
24 -class Give_Earnings_Export extends Give_Export {
25 +class Give_Earnings_Export extends Give_Export
26 +{
25 27
26 - /**
27 - * Our export type. Used for export-type specific filters/actions
28 - * @var string
29 - * @since 1.0
30 - */
31 - public $export_type = 'earnings';
28 + /**
29 + * Our export type. Used for export-type specific filters/actions
30 + *
31 + * @since 1.0
32 + * @var string
33 + */
34 + public $export_type = 'earnings';
32 35
33 - /**
34 - * Set the export headers
35 - *
36 - * @access public
37 - * @since 1.6
38 - * @return void
39 - */
40 - public function headers() {
41 - give_ignore_user_abort();
36 + /**
37 + * Set the export headers
38 + *
39 + * @access public
40 + * @since 1.6
41 + * @return void
42 + */
43 + public function headers()
44 + {
45 + give_ignore_user_abort();
42 46
43 - nocache_headers();
44 - header( 'Content-Type: text/csv; charset=utf-8' );
45 - header( 'Content-Disposition: attachment; filename=' . apply_filters( 'give_earnings_export_filename', 'give-export-' . $this->export_type . '-' . date( 'n' ) . '-' . date( 'Y' ) ) . '.csv' );
46 - header( "Expires: 0" );
47 + nocache_headers();
48 + header('Content-Type: text/csv; charset=utf-8');
49 + header(
50 + 'Content-Disposition: attachment; filename=' . apply_filters(
51 + 'give_earnings_export_filename',
52 + 'give-export-' . $this->export_type . '-' . date('n') . '-' . date('Y')
53 + ) . '.csv'
54 + );
55 + header('Expires: 0');
56 + }
47 57
48 - }
58 + /**
59 + * Set the CSV columns
60 + *
61 + * @access public
62 + * @since 1.0
63 + * @return array $cols All the columns
64 + */
65 + public function csv_cols()
66 + {
67 + $cols = [
68 + 'date' => __('Date', 'give'),
69 + 'donations' => __('Donations', 'give'),
70 + /* translators: %s: currency */
71 + 'earnings' => sprintf(__('Revenue (%s)', 'give'), give_currency_symbol('', true)),
72 + ];
49 73
50 - /**
51 - * Set the CSV columns
52 - *
53 - * @access public
54 - * @since 1.0
55 - * @return array $cols All the columns
56 - */
57 - public function csv_cols() {
74 + return $cols;
75 + }
58 76
59 - $cols = array(
60 - 'date' => __( 'Date', 'give' ),
61 - 'donations' => __( 'Donations', 'give' ),
62 - /* translators: %s: currency */
63 - 'earnings' => sprintf( __( 'Income (%s)', 'give' ), give_currency_symbol('', true) )
64 - );
77 + /**
78 + * Include a nonce check when authenticating
79 + *
80 + * @since 2.21.4
81 + *
82 + * @return bool
83 + */
84 + public function can_export() {
85 + return (bool) apply_filters( 'give_export_capability', current_user_can( 'export_give_reports' ) && wp_verify_nonce($_REQUEST['give-nonce'], 'give_earnings_export'));
86 + }
65 87
66 - return $cols;
67 - }
88 + /**
89 + * Get the Export Data
90 + *
91 + * @access public
92 + * @since 1.0
93 + * @return array $data The data for the CSV file
94 + */
95 + public function get_data()
96 + {
97 + $dates = $this->getDatesFromRequest();
68 98
69 - /**
70 - * Get the Export Data
71 - *
72 - * @access public
73 - * @since 1.0
74 - * @return array $data The data for the CSV file
75 - */
76 - public function get_data() {
99 + $data = [];
100 + $year = $dates->startYear;
101 + $stats = new Give_Payment_Stats();
77 102
78 - $start_year = isset( $_POST['start_year'] ) ? absint( $_POST['start_year'] ) : date( 'Y' );
79 - $end_year = isset( $_POST['end_year'] ) ? absint( $_POST['end_year'] ) : date( 'Y' );
80 - $start_month = isset( $_POST['start_month'] ) ? absint( $_POST['start_month'] ) : date( 'n' );
81 - $end_month = isset( $_POST['end_month'] ) ? absint( $_POST['end_month'] ) : date( 'n' );
103 + while ($year <= $dates->endYear) {
104 + if ($year === $dates->startYear && $year === $dates->endYear) {
105 + $m1 = $dates->startMonth;
106 + $m2 = $dates->endMonth;
107 + } elseif ($year === $dates->startYear) {
108 + $m1 = $dates->startMonth;
109 + $m2 = 12;
110 + } elseif ($year === $dates->endYear) {
111 + $m1 = 1;
112 + $m2 = $dates->endMonth;
113 + } else {
114 + $m1 = 1;
115 + $m2 = 12;
116 + }
82 117
83 - $data = array();
84 - $year = $start_year;
85 - $stats = new Give_Payment_Stats;
118 + while ($m1 <= $m2) {
119 + $date1 = mktime(0, 0, 0, $m1, 1, $year);
120 + $date2 = mktime(0, 0, 0, $m1, cal_days_in_month(CAL_GREGORIAN, $m1, $year), $year);
86 121
87 - while ( $year <= $end_year ) {
122 + $data[] = [
123 + 'date' => date_i18n('F Y', $date1),
124 + 'donations' => $stats->get_sales(0, $date1, $date2),
125 + 'earnings' => give_format_amount($stats->get_earnings(0, $date1, $date2), ['sanitize' => false]),
126 + ];
88 127
89 - if ( $year == $start_year && $year == $end_year ) {
128 + $m1++;
129 + }
90 130
91 - $m1 = $start_month;
92 - $m2 = $end_month;
131 + $year++;
132 + }
93 133
94 - } elseif ( $year == $start_year ) {
134 + $data = apply_filters('give_export_get_data', $data);
135 + $data = apply_filters("give_export_get_data_{$this->export_type}", $data);
95 136
96 - $m1 = $start_month;
97 - $m2 = 12;
137 + return $data;
138 + }
98 139
99 - } elseif ( $year == $end_year ) {
140 + /**
141 + * @since 2.21.2
142 + *
143 + * @return object|null
144 + */
145 + private function getDatesFromRequest()
146 + {
147 + $dates = new stdClass();
148 + $firstDonation = give()->donations->getFirstDonation();
149 + $lastDonation = give()->donations->getLatestDonation();
100 150
101 - $m1 = 1;
102 - $m2 = $end_month;
151 + if ($firstDonation === null ) {
152 + return (object)[
153 + 'startYear' => date('Y'),
154 + 'endYear' => date('Y' ),
155 + 'startMonth' => date('m'),
156 + 'endMonth' => date('m')
157 + ];
158 + }
103 159
104 - } else {
160 + if (!isset($_POST['start_year'], $_POST['end_year'], $_POST['start_month'], $_POST['end_month'])) {
161 + throw new \Give\Framework\Exceptions\Primitives\InvalidArgumentException(
162 + 'Start year & month, End year & month can not be empty. Please enter validate dates to export revenue and donation stats.'
163 + );
164 + }
105 165
106 - $m1 = 1;
107 - $m2 = 12;
166 + $dates->startYear = (string)absint($_POST['start_year']);
167 + $dates->endYear = (string)absint($_POST['end_year']);
168 + $dates->startMonth = (string)absint($_POST['start_month']);
169 + $dates->endMonth = (string)absint($_POST['end_month']);
108 170
109 - }
171 + if ($firstDonation && $firstDonation->createdAt->format('Y') > $dates->startYear) {
172 + throw new \Give\Framework\Exceptions\Primitives\InvalidArgumentException(
173 + 'Start year cannot be less than first donation year'
174 + );
175 + }
110 176
111 - while ( $m1 <= $m2 ) {
177 + if ($lastDonation && $lastDonation->createdAt->format('Y') < $dates->endYear) {
178 + throw new \Give\Framework\Exceptions\Primitives\InvalidArgumentException(
179 + 'End year cannot be greater than last donation year'
180 + );
181 + }
112 182
113 - $date1 = mktime( 0, 0, 0, $m1, 1, $year );
114 - $date2 = mktime( 0, 0, 0, $m1, cal_days_in_month( CAL_GREGORIAN, $m1, $year ), $year );
115 -
116 - $data[] = array(
117 - 'date' => date_i18n( 'F Y', $date1 ),
118 - 'donations' => $stats->get_sales( 0, $date1, $date2 ),
119 - 'earnings' => give_format_amount( $stats->get_earnings( 0, $date1, $date2 ), array( 'sanitize' => false ) ),
120 - );
121 -
122 - $m1 ++;
123 -
124 - }
125 -
126 -
127 - $year ++;
128 -
129 - }
130 -
131 - $data = apply_filters( 'give_export_get_data', $data );
132 - $data = apply_filters( "give_export_get_data_{$this->export_type}", $data );
133 -
134 - return $data;
135 - }
183 + return $dates;
184 + }
136 185 }