PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.33.4
GiveWP – Donation Plugin and Fundraising Platform v2.33.4
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 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / tools / export / pdf-reports.php
give / includes / admin / tools / export Last commit date
class-batch-export-donors.php 5 years ago class-batch-export.php 4 years ago class-core-settings-export.php 7 years ago class-export-earnings.php 3 years ago class-export.php 3 years ago class-give-export-donations.php 5 years ago export-actions.php 3 years ago export-functions.php 4 years ago give-export-donations-exporter.php 3 years ago give-export-donations-functions.php 5 years ago pdf-reports.php 5 years ago
pdf-reports.php
355 lines
1 <?php
2 /**
3 * PDF Report Generation Functions.
4 *
5 * @package Give
6 * @subpackage Admin/Reports
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly..
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Generate PDF Reports.
19 *
20 * Generates PDF report on donations and income for all forms for the current year.
21 *
22 * @param string $data Data.
23 *
24 * @since 1.0
25 *
26 * @uses give_pdf
27 */
28 function give_generate_pdf( $data ) {
29
30 if ( ! current_user_can( 'view_give_reports' ) ) {
31 wp_die( __( 'You do not have permission to generate PDF sales reports.', 'give' ), __( 'Error', 'give' ), [ 'response' => 403 ] );
32 }
33
34 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'give_generate_pdf' ) ) {
35 wp_die( __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), __( 'Error', 'give' ), [ 'response' => 403 ] );
36 }
37
38 if ( ! file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/give-pdf.php' ) ) {
39 wp_die( __( 'Dependency missing.', 'give' ), __( 'Error', 'give' ), [ 'response' => 403 ] );
40 }
41
42 require_once GIVE_PLUGIN_DIR . '/includes/libraries/give-pdf.php';
43
44 $daterange = utf8_decode(
45 sprintf(
46 /* translators: 1: start date 2: end date */
47 __( '%1$s to %2$s', 'give' ),
48 date_i18n( give_date_format(), mktime( 0, 0, 0, 1, 1, date( 'Y' ) ) ),
49 date_i18n( give_date_format() )
50 )
51 );
52
53 $categories_enabled = give_is_setting_enabled( give_get_option( 'categories', 'disabled' ) );
54 $tags_enabled = give_is_setting_enabled( give_get_option( 'tags', 'disabled' ) );
55
56 $pdf = new Give_PDF( 'L', 'mm', 'A', true, 'UTF-8', false );
57 $default_font = apply_filters( 'give_pdf_default_font', 'Helvetica' );
58 $custom_font = 'dejavusans';
59 $font_style = '';
60 $font_path = GIVE_PLUGIN_DIR . '/vendor/tecnickcom/tcpdf/fonts/CODE2000.TTF';
61
62 if (
63 file_exists( $font_path ) &&
64 // RIAL exist for backward compatibility.
65 in_array( give_get_currency(), [ 'RIAL', 'RUB', 'IRR' ] )
66 ) {
67 TCPDF_FONTS::addTTFfont( $font_path, '' );
68 $custom_font = 'CODE2000';
69 $font_style = 'B';
70 }
71
72 $pdf->AddPage( 'L', 'A4' );
73 $pdf->setImageScale( 1.5 );
74 $pdf->SetTitle( utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ) );
75 $pdf->SetAuthor( utf8_decode( __( 'GiveWP - Democratizing Generosity', 'give' ) ) );
76 $pdf->SetCreator( utf8_decode( __( 'GiveWP - Democratizing Generosity', 'give' ) ) );
77
78 // Image URL should have absolute path. @see https://tcpdf.org/examples/example_009/.
79 $pdf->Image( apply_filters( 'give_pdf_export_logo', GIVE_PLUGIN_DIR . 'assets/dist/images/give-logo-small.png' ), 247, 8 );
80
81 $pdf->SetMargins( 8, 8, 8 );
82 $pdf->SetX( 8 );
83
84 $pdf->SetFont( $default_font, '', 16 );
85 $pdf->SetTextColor( 50, 50, 50 );
86 $pdf->Cell( 0, 3, utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ), 0, 2, 'L', false );
87
88 $pdf->SetFont( $default_font, '', 13 );
89 $pdf->SetTextColor( 150, 150, 150 );
90 $pdf->Ln( 1 );
91 $pdf->Cell( 0, 6, utf8_decode( __( 'Date Range: ', 'give' ) ) . $daterange, 0, 2, 'L', false );
92 $pdf->Ln();
93 $pdf->SetTextColor( 50, 50, 50 );
94 $pdf->SetFont( $default_font, '', 14 );
95 $pdf->Cell( 0, 10, utf8_decode( __( 'Table View', 'give' ) ), 0, 2, 'L', false );
96 $pdf->SetFont( $default_font, '', 12 );
97
98 $pdf->SetFillColor( 238, 238, 238 );
99 $pdf->SetTextColor( 0, 0, 0, 100 ); // Set Black color.
100 $pdf->Cell( 50, 6, utf8_decode( __( 'Form Name', 'give' ) ), 1, 0, 'L', true );
101 $pdf->Cell( 50, 6, utf8_decode( __( 'Price', 'give' ) ), 1, 0, 'L', true );
102
103 // Display Categories Heading only, if user has opted for it.
104 if ( $categories_enabled ) {
105 $pdf->Cell( 45, 6, utf8_decode( __( 'Categories', 'give' ) ), 1, 0, 'L', true );
106 }
107
108 // Display Tags Heading only, if user has opted for it.
109 if ( $tags_enabled ) {
110 $pdf->Cell( 45, 6, utf8_decode( __( 'Tags', 'give' ) ), 1, 0, 'L', true );
111 }
112
113 $pdf->Cell( 45, 6, utf8_decode( __( 'Number of Donations', 'give' ) ), 1, 0, 'L', true );
114 $pdf->Cell( 45, 6, utf8_decode( __( 'Revenue to Date', 'give' ) ), 1, 1, 'L', true );
115
116 // Set Custom Font to support various currencies.
117 $pdf->SetFont( apply_filters( 'give_pdf_custom_font', $custom_font ), $font_style, 12 );
118
119 // Object for getting stats.
120 $donation_stats = new Give_Payment_Stats();
121
122 $give_forms = get_posts(
123 [
124 'post_type' => 'give_forms',
125 'posts_per_page' => - 1,
126 'suppress_filters' => false,
127 ]
128 );
129
130 if ( $give_forms ) {
131 $pdf->SetWidths( [ 50, 50, 45, 45, 45, 45 ] );
132
133 foreach ( $give_forms as $form ) :
134 $pdf->SetFillColor( 255, 255, 255 );
135
136 $title = $form->post_title;
137
138 if ( give_has_variable_prices( $form->ID ) ) {
139 $price = html_entity_decode( give_price_range( $form->ID, false ), ENT_COMPAT, 'UTF-8' );
140 } else {
141 $price = give_currency_filter( give_get_form_price( $form->ID ), [ 'decode_currency' => true ] );
142 }
143
144 // Display Categories Data only, if user has opted for it.
145 $categories = [];
146 if ( $categories_enabled ) {
147 $categories = get_the_term_list( $form->ID, 'give_forms_category', '', ', ', '' );
148 $categories = ! is_wp_error( $categories ) ? strip_tags( $categories ) : '';
149 }
150
151 // Display Tags Data only, if user has opted for it.
152 $tags = [];
153 if ( $tags_enabled ) {
154 $tags = get_the_term_list( $form->ID, 'give_forms_tag', '', ', ', '' );
155 $tags = ! is_wp_error( $tags ) ? strip_tags( $tags ) : '';
156 }
157
158 $sales = $donation_stats->get_sales( $form->ID, 'this_year' );
159 $earnings = give_currency_filter( give_format_amount( $donation_stats->get_earnings( $form->ID, 'this_year' ), [ 'sanitize' => false ] ), [ 'decode_currency' => true ] );
160
161 // This will help filter data before appending it to PDF Receipt.
162 $prepare_pdf_data = [];
163 $prepare_pdf_data[] = $title;
164 $prepare_pdf_data[] = $price;
165
166 // Append Categories Data only, if user has opted for it.
167 if ( $categories_enabled ) {
168 $prepare_pdf_data[] = $categories;
169 }
170
171 // Append Tags Data only, if user has opted for it.
172 if ( $tags_enabled ) {
173 $prepare_pdf_data[] = $tags;
174 }
175
176 $prepare_pdf_data[] = $sales;
177 $prepare_pdf_data[] = $earnings;
178
179 $pdf->Row( $prepare_pdf_data );
180
181 endforeach;
182 } else {
183
184 // Fix: Minor Styling Alignment Issue for PDF.
185 if ( $categories_enabled && $tags_enabled ) {
186 $no_found_width = 280;
187 } elseif ( $categories_enabled || $tags_enabled ) {
188 $no_found_width = 235;
189 } else {
190 $no_found_width = 190;
191 }
192 $title = utf8_decode( __( 'No forms found.', 'give' ) );
193 $pdf->MultiCell( $no_found_width, 5, $title, 1, 'C', false, 1, '', '', true, 0, false, true, 0, 'T', false );
194 }// End if().
195 $pdf->Ln();
196 $pdf->SetTextColor( 50, 50, 50 );
197 $pdf->SetFont( $default_font, '', 14 );
198
199 // Output Graph on a new page.
200 $pdf->AddPage( 'L', 'A4' );
201 $pdf->Cell( 0, 10, utf8_decode( __( 'Graph View', 'give' ) ), 0, 2, 'L', false );
202 $pdf->SetFont( $default_font, '', 12 );
203
204 $image = html_entity_decode( urldecode( give_draw_chart_image() ) );
205 $image = str_replace( ' ', '%20', $image );
206
207 $pdf->SetX( 25 );
208 $pdf->Image( $image . '&file=.png' );
209 $pdf->Ln( 7 );
210 $pdf->Output( apply_filters( 'give_sales_earnings_pdf_export_filename', 'give-report-' . date_i18n( 'Y-m-d' ) ) . '.pdf', 'D' );
211 exit();
212 }
213
214 add_action( 'give_generate_pdf', 'give_generate_pdf' );
215
216 /**
217 * Draws Chart for PDF Report.
218 *
219 * Draws the sales and earnings chart for the PDF report and then retrieves the
220 * URL of that chart to display on the PDF Report.
221 *
222 * @return string $chart->getUrl() URL for the Google Chart
223 * @uses GoogleChart
224 * @uses GoogleChartData
225 * @uses GoogleChartShapeMarker
226 * @uses GoogleChartTextMarker
227 * @uses GoogleChartAxis
228 * @since 1.1.4.0
229 */
230 function give_draw_chart_image() {
231 require_once GIVE_PLUGIN_DIR . '/includes/libraries/googlechartlib/GoogleChart.php';
232 require_once GIVE_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartShapeMarker.php';
233 require_once GIVE_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartTextMarker.php';
234
235 $chart = new GoogleChart( 'lc', 900, 330 );
236
237 $i = 1;
238 $earnings = '';
239 $sales = '';
240
241 while ( $i <= 12 ) :
242 $earnings .= give_get_earnings_by_date( null, $i, date( 'Y' ) ) . ',';
243 $sales .= give_get_sales_by_date( null, $i, date( 'Y' ) ) . ',';
244 $i ++;
245 endwhile;
246
247 $earnings_array = explode( ',', $earnings );
248 $sales_array = explode( ',', $sales );
249
250 $i = 0;
251 while ( $i <= 11 ) {
252 if ( empty( $sales_array[ $i ] ) ) {
253 $sales_array[ $i ] = 0;
254 }
255 $i ++;
256 }
257
258 $min_earnings = 0;
259 $max_earnings = max( $earnings_array );
260 $earnings_scale = round( $max_earnings, - 1 );
261
262 $data = new GoogleChartData(
263 [
264 $earnings_array[0],
265 $earnings_array[1],
266 $earnings_array[2],
267 $earnings_array[3],
268 $earnings_array[4],
269 $earnings_array[5],
270 $earnings_array[6],
271 $earnings_array[7],
272 $earnings_array[8],
273 $earnings_array[9],
274 $earnings_array[10],
275 $earnings_array[11],
276 ]
277 );
278
279 $data->setLegend( __( 'Revenue', 'give' ) );
280 $data->setColor( '1b58a3' );
281 $chart->addData( $data );
282
283 $shape_marker = new GoogleChartShapeMarker( GoogleChartShapeMarker::CIRCLE );
284 $shape_marker->setColor( '000000' );
285 $shape_marker->setSize( 7 );
286 $shape_marker->setBorder( 2 );
287 $shape_marker->setData( $data );
288 $chart->addMarker( $shape_marker );
289
290 $value_marker = new GoogleChartTextMarker( GoogleChartTextMarker::VALUE );
291 $value_marker->setColor( '000000' );
292 $value_marker->setData( $data );
293 $chart->addMarker( $value_marker );
294
295 $data = new GoogleChartData(
296 [
297 $sales_array[0],
298 $sales_array[1],
299 $sales_array[2],
300 $sales_array[3],
301 $sales_array[4],
302 $sales_array[5],
303 $sales_array[6],
304 $sales_array[7],
305 $sales_array[8],
306 $sales_array[9],
307 $sales_array[10],
308 $sales_array[11],
309 ]
310 );
311 $data->setLegend( __( 'Donations', 'give' ) );
312 $data->setColor( 'ff6c1c' );
313 $chart->addData( $data );
314
315 $chart->setTitle( __( 'Donations by Month for all GiveWP Forms', 'give' ), '336699', 18 );
316
317 $chart->setScale( 0, $max_earnings );
318
319 $y_axis = new GoogleChartAxis( 'y' );
320 $y_axis->setDrawTickMarks( true )->setLabels( [ 0, $max_earnings ] );
321 $chart->addAxis( $y_axis );
322
323 $x_axis = new GoogleChartAxis( 'x' );
324 $x_axis->setTickMarks( 5 );
325 $x_axis->setLabels(
326 [
327 __( 'Jan', 'give' ),
328 __( 'Feb', 'give' ),
329 __( 'Mar', 'give' ),
330 __( 'Apr', 'give' ),
331 __( 'May', 'give' ),
332 __( 'June', 'give' ),
333 __( 'July', 'give' ),
334 __( 'Aug', 'give' ),
335 __( 'Sept', 'give' ),
336 __( 'Oct', 'give' ),
337 __( 'Nov', 'give' ),
338 __( 'Dec', 'give' ),
339 ]
340 );
341 $chart->addAxis( $x_axis );
342
343 $shape_marker = new GoogleChartShapeMarker( GoogleChartShapeMarker::CIRCLE );
344 $shape_marker->setSize( 6 );
345 $shape_marker->setBorder( 2 );
346 $shape_marker->setData( $data );
347 $chart->addMarker( $shape_marker );
348
349 $value_marker = new GoogleChartTextMarker( GoogleChartTextMarker::VALUE );
350 $value_marker->setData( $data );
351 $chart->addMarker( $value_marker );
352
353 return $chart->getUrl();
354 }
355