| Conditions | 20 | 
| Paths | 64 | 
| Total Lines | 171 | 
| Code Lines | 107 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 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' ), array( 'response' => 403 ) ); | ||
| 32 | } | ||
| 33 | |||
| 34 | 	if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'give_generate_pdf' ) ) { | ||
| 35 | wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); | ||
| 36 | } | ||
| 37 | |||
| 38 | require_once GIVE_PLUGIN_DIR . '/includes/libraries/fpdf/fpdf.php'; | ||
| 39 | require_once GIVE_PLUGIN_DIR . '/includes/libraries/fpdf/give_pdf.php'; | ||
| 40 | |||
| 41 | $daterange = utf8_decode( | ||
| 42 | sprintf( | ||
| 43 | /* translators: 1: start date 2: end date */ | ||
| 44 | __( '%1$s to %2$s', 'give' ), | ||
| 45 | date_i18n( give_date_format(), mktime( 0, 0, 0, 1, 1, date( 'Y' ) ) ), | ||
| 46 | date_i18n( give_date_format() ) | ||
| 47 | ) | ||
| 48 | ); | ||
| 49 | |||
| 50 | $categories_enabled = give_is_setting_enabled( give_get_option( 'categories', 'disabled' ) ); | ||
| 51 | $tags_enabled = give_is_setting_enabled( give_get_option( 'tags', 'disabled' ) ); | ||
| 52 | |||
| 53 | $pdf = new give_pdf(); | ||
| 54 | $pdf->AddPage( 'L', 'A4' ); | ||
| 55 | |||
| 56 | $pdf->SetTitle( utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ) ); | ||
| 57 | $pdf->SetAuthor( utf8_decode( __( 'Give - Democratizing Generosity', 'give' ) ) ); | ||
| 58 | $pdf->SetCreator( utf8_decode( __( 'Give - Democratizing Generosity', 'give' ) ) ); | ||
| 59 | |||
| 60 | $pdf->Image( apply_filters( 'give_pdf_export_logo', GIVE_PLUGIN_URL . 'assets/images/give-logo-small.png' ), 247, 8 ); | ||
| 61 | |||
| 62 | $pdf->SetMargins( 8, 8, 8 ); | ||
| 63 | $pdf->SetX( 8 ); | ||
| 64 | |||
| 65 | $pdf->SetFont( 'Helvetica', '', 16 ); | ||
| 66 | $pdf->SetTextColor( 50, 50, 50 ); | ||
| 67 | $pdf->Cell( 0, 3, utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ), 0, 2, 'L', false ); | ||
| 68 | |||
| 69 | $pdf->SetFont( 'Helvetica', '', 13 ); | ||
| 70 | $pdf->Ln(); | ||
| 71 | $pdf->SetTextColor( 150, 150, 150 ); | ||
| 72 | $pdf->Cell( 0, 6, utf8_decode( __( 'Date Range: ', 'give' ) ) . $daterange, 0, 2, 'L', false ); | ||
| 73 | $pdf->Ln(); | ||
| 74 | $pdf->SetTextColor( 50, 50, 50 ); | ||
| 75 | $pdf->SetFont( 'Helvetica', '', 14 ); | ||
| 76 | $pdf->Cell( 0, 10, utf8_decode( __( 'Table View', 'give' ) ), 0, 2, 'L', false ); | ||
| 77 | $pdf->SetFont( 'Helvetica', '', 12 ); | ||
| 78 | |||
| 79 | $pdf->SetFillColor( 238, 238, 238 ); | ||
| 80 | $pdf->Cell( 70, 6, utf8_decode( __( 'Form Name', 'give' ) ), 1, 0, 'L', true ); | ||
| 81 | $pdf->Cell( 30, 6, utf8_decode( __( 'Price', 'give' ) ), 1, 0, 'L', true ); | ||
| 82 | |||
| 83 | // Display Categories Heading only, if user has opted for it. | ||
| 84 |     if ( $categories_enabled ) { | ||
| 85 | $pdf->Cell( 45, 6, utf8_decode( __( 'Categories', 'give' ) ), 1, 0, 'L', true ); | ||
| 86 | } | ||
| 87 | |||
| 88 | // Display Tags Heading only, if user has opted for it. | ||
| 89 |     if ( $tags_enabled ) { | ||
| 90 | $pdf->Cell( 45, 6, utf8_decode( __( 'Tags', 'give' ) ), 1, 0, 'L', true ); | ||
| 91 | } | ||
| 92 | |||
| 93 | $pdf->Cell( 45, 6, utf8_decode( __( 'Number of Donations', 'give' ) ), 1, 0, 'L', true ); | ||
| 94 | $pdf->Cell( 45, 6, utf8_decode( __( 'Income to Date', 'give' ) ), 1, 1, 'L', true ); | ||
| 95 | |||
| 96 | $year = date( 'Y' ); | ||
| 97 | $give_forms = get_posts( array( 'post_type' => 'give_forms', 'year' => $year, 'posts_per_page' => - 1 ) ); | ||
| 98 | |||
| 99 | 	if ( $give_forms ) { | ||
| 100 | $pdf->SetWidths( array( 70, 30, 45, 45, 45, 45 ) ); | ||
| 101 | |||
| 102 | foreach ( $give_forms as $form ): | ||
| 103 | $pdf->SetFillColor( 255, 255, 255 ); | ||
| 104 | |||
| 105 | $title = $form->post_title; | ||
| 106 | |||
| 107 | 			if ( give_has_variable_prices( $form->ID ) ) { | ||
| 108 | |||
| 109 | $prices = give_get_variable_prices( $form->ID ); | ||
| 110 | |||
| 111 | $first = $prices[0]['_give_amount']; | ||
| 112 | $last = array_pop( $prices ); | ||
| 113 | $last = $last['_give_amount']; | ||
| 114 | |||
| 115 | 				if ( $first < $last ) { | ||
| 116 | $min = $first; | ||
| 117 | $max = $last; | ||
| 118 | 				} else { | ||
| 119 | $min = $last; | ||
| 120 | $max = $first; | ||
| 121 | } | ||
| 122 | |||
| 123 | $price = give_currency_filter( give_format_amount( $min ), '', true ) . ' - ' . give_currency_filter( give_format_amount( $max ), '', true ); | ||
| 124 | 			} else { | ||
| 125 | $price = give_currency_filter( give_get_form_price( $form->ID ), '', true ); | ||
| 126 | } | ||
| 127 | |||
| 128 | // Display Categories Data only, if user has opted for it. | ||
| 129 |             if ( $categories_enabled ) { | ||
| 130 | $categories = get_the_term_list( $form->ID, 'give_forms_category', '', ', ', '' ); | ||
| 131 | $categories = ! is_wp_error( $categories ) ? strip_tags( $categories ) : ''; | ||
| 132 | } | ||
| 133 | |||
| 134 | // Display Tags Data only, if user has opted for it. | ||
| 135 |             if ( $tags_enabled ) { | ||
| 136 | $tags = get_the_term_list( $form->ID, 'give_forms_tag', '', ', ', '' ); | ||
| 137 | $tags = ! is_wp_error( $tags ) ? strip_tags( $tags ) : ''; | ||
| 138 | } | ||
| 139 | |||
| 140 | $sales = give_get_form_sales_stats( $form->ID ); | ||
| 141 | $link = get_permalink( $form->ID ); | ||
| 142 | $earnings = give_currency_filter( give_get_form_earnings_stats( $form->ID ), '', true ); | ||
| 143 | |||
| 144 | 			if ( function_exists( 'iconv' ) ) { | ||
| 145 | // Ensure characters like euro; are properly converted. | ||
| 146 | $price = iconv( 'UTF-8', 'windows-1252', utf8_encode( $price ) ); | ||
| 147 | $earnings = iconv( 'UTF-8', 'windows-1252', utf8_encode( $earnings ) ); | ||
| 148 | } | ||
| 149 | |||
| 150 | // This will help filter data before appending it to PDF Receipt. | ||
| 151 | $prepare_pdf_data = array(); | ||
| 152 | $prepare_pdf_data[] = $title; | ||
| 153 | $prepare_pdf_data[] = $price; | ||
| 154 | |||
| 155 | // Append Categories Data only, if user has opted for it. | ||
| 156 |             if ( $categories_enabled ) { | ||
| 157 | $prepare_pdf_data[] = $categories; | ||
| 158 | } | ||
| 159 | |||
| 160 | // Append Tags Data only, if user has opted for it. | ||
| 161 |             if ( $tags_enabled ) { | ||
| 162 | $prepare_pdf_data[] = $tags; | ||
| 163 | } | ||
| 164 | |||
| 165 | $prepare_pdf_data[] = $sales; | ||
| 166 | $prepare_pdf_data[] = $earnings; | ||
| 167 | |||
| 168 | $pdf->Row( $prepare_pdf_data ); | ||
| 169 | endforeach; | ||
| 170 | 	} else { | ||
| 171 | |||
| 172 | // Fix: Minor Styling Alignment Issue for PDF | ||
| 173 |         if( $categories_enabled && $tags_enabled ) { | ||
| 174 | $pdf->SetWidths( array( 280 ) ); | ||
| 175 |         } elseif( $categories_enabled || $tags_enabled ) { | ||
| 176 | $pdf->SetWidths( array( 235 ) ); | ||
| 177 |         } else { | ||
| 178 | $pdf->SetWidths( array( 190 ) ); | ||
| 179 | } | ||
| 180 | |||
| 181 | $title = utf8_decode( __( 'No forms found.', 'give' ) ); | ||
| 182 | $pdf->Row( array( $title ) ); | ||
| 183 | } | ||
| 184 | |||
| 185 | $pdf->Ln(); | ||
| 186 | $pdf->SetTextColor( 50, 50, 50 ); | ||
| 187 | $pdf->SetFont( 'Helvetica', '', 14 ); | ||
| 188 | $pdf->Cell( 0, 10, utf8_decode( __( 'Graph View', 'give' ) ), 0, 2, 'L', false ); | ||
| 189 | $pdf->SetFont( 'Helvetica', '', 12 ); | ||
| 190 | |||
| 191 | $image = html_entity_decode( urldecode( give_draw_chart_image() ) ); | ||
| 192 | $image = str_replace( ' ', '%20', $image ); | ||
| 193 | |||
| 194 | $pdf->SetX( 25 ); | ||
| 195 | $pdf->Image( $image . '&file=.png' ); | ||
| 196 | $pdf->Ln( 7 ); | ||
| 197 | $pdf->Output( apply_filters( 'give_sales_earnings_pdf_export_filename', 'give-report-' . date_i18n( 'Y-m-d' ) ) . '.pdf', 'D' ); | ||
| 198 | } | ||
| 199 | |||
| 335 | 
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.