| Conditions | 21 |
| Paths | 256 |
| Total Lines | 176 |
| Code Lines | 112 |
| Lines | 6 |
| Ratio | 3.41 % |
| 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 | View Code Duplication | if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'give_generate_pdf' ) ) { |
|
| 35 | wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
||
| 36 | } |
||
| 37 | |||
| 38 | View Code Duplication | if ( ! file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/give-pdf.php' ) ) { |
|
| 39 | wp_die( __( 'Dependency missing.', 'give' ), __( 'Error', 'give' ), array( '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 | |||
| 61 | if ( file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/tcpdf/fonts/CODE2000.TTF' ) && |
||
| 62 | in_array( give_get_currency(), array( 'RIAL', 'RUB' ) ) ) { |
||
| 63 | TCPDF_FONTS::addTTFfont( GIVE_PLUGIN_DIR . '/includes/libraries/tcpdf/fonts/CODE2000.TTF', '' ); |
||
| 64 | $custom_font = 'CODE2000'; |
||
| 65 | $font_style = 'B'; |
||
| 66 | } |
||
| 67 | |||
| 68 | $pdf->AddPage( 'L', 'A4' ); |
||
| 69 | $pdf->setImageScale( 1.5 ); |
||
| 70 | $pdf->SetTitle( utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ) ); |
||
| 71 | $pdf->SetAuthor( utf8_decode( __( 'Give - Democratizing Generosity', 'give' ) ) ); |
||
| 72 | $pdf->SetCreator( utf8_decode( __( 'Give - Democratizing Generosity', 'give' ) ) ); |
||
| 73 | |||
| 74 | $pdf->Image( apply_filters( 'give_pdf_export_logo', GIVE_PLUGIN_URL . 'assets/images/give-logo-small.png' ), 247, 8 ); |
||
| 75 | |||
| 76 | $pdf->SetMargins( 8, 8, 8 ); |
||
| 77 | $pdf->SetX( 8 ); |
||
| 78 | |||
| 79 | $pdf->SetFont( $default_font, '', 16 ); |
||
| 80 | $pdf->SetTextColor( 50, 50, 50 ); |
||
| 81 | $pdf->Cell( 0, 3, utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ), 0, 2, 'L', false ); |
||
| 82 | |||
| 83 | $pdf->SetFont( $default_font, '', 13 ); |
||
| 84 | $pdf->SetTextColor( 150, 150, 150 ); |
||
| 85 | $pdf->Ln( 1 ); |
||
| 86 | $pdf->Cell( 0, 6, utf8_decode( __( 'Date Range: ', 'give' ) ) . $daterange, 0, 2, 'L', false ); |
||
| 87 | $pdf->Ln(); |
||
| 88 | $pdf->SetTextColor( 50, 50, 50 ); |
||
| 89 | $pdf->SetFont( $default_font, '', 14 ); |
||
| 90 | $pdf->Cell( 0, 10, utf8_decode( __( 'Table View', 'give' ) ), 0, 2, 'L', false ); |
||
| 91 | $pdf->SetFont( $default_font, '', 12 ); |
||
| 92 | |||
| 93 | $pdf->SetFillColor( 238, 238, 238 ); |
||
| 94 | $pdf->SetTextColor( 0, 0, 0, 100 ); // Set Black color. |
||
| 95 | $pdf->Cell( 50, 6, utf8_decode( __( 'Form Name', 'give' ) ), 1, 0, 'L', true ); |
||
| 96 | $pdf->Cell( 50, 6, utf8_decode( __( 'Price', 'give' ) ), 1, 0, 'L', true ); |
||
| 97 | |||
| 98 | // Display Categories Heading only, if user has opted for it. |
||
| 99 | if ( $categories_enabled ) { |
||
| 100 | $pdf->Cell( 45, 6, utf8_decode( __( 'Categories', 'give' ) ), 1, 0, 'L', true ); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Display Tags Heading only, if user has opted for it. |
||
| 104 | if ( $tags_enabled ) { |
||
| 105 | $pdf->Cell( 45, 6, utf8_decode( __( 'Tags', 'give' ) ), 1, 0, 'L', true ); |
||
| 106 | } |
||
| 107 | |||
| 108 | $pdf->Cell( 45, 6, utf8_decode( __( 'Number of Donations', 'give' ) ), 1, 0, 'L', true ); |
||
| 109 | $pdf->Cell( 45, 6, utf8_decode( __( 'Income to Date', 'give' ) ), 1, 1, 'L', true ); |
||
| 110 | |||
| 111 | // Set Custom Font to support various currencies. |
||
| 112 | $pdf->SetFont( apply_filters( 'give_pdf_custom_font', $custom_font ), $font_style, 12 ); |
||
| 113 | |||
| 114 | $year = date( 'Y' ); |
||
| 115 | $give_forms = get_posts( array( |
||
| 116 | 'post_type' => 'give_forms', |
||
| 117 | 'year' => $year, |
||
| 118 | 'posts_per_page' => - 1, |
||
| 119 | 'supply_filter' => false, |
||
| 120 | ) ); |
||
| 121 | |||
| 122 | if ( $give_forms ) { |
||
| 123 | $pdf->SetWidths( array( 50, 50, 45, 45, 45, 45 ) ); |
||
| 124 | |||
| 125 | foreach ( $give_forms as $form ): |
||
| 126 | $pdf->SetFillColor( 255, 255, 255 ); |
||
| 127 | |||
| 128 | $title = $form->post_title; |
||
| 129 | |||
| 130 | if ( give_has_variable_prices( $form->ID ) ) { |
||
| 131 | $price = html_entity_decode( give_price_range( $form->ID, false ) ); |
||
| 132 | } else { |
||
| 133 | $price = give_currency_filter( give_get_form_price( $form->ID ), '', true ); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Display Categories Data only, if user has opted for it. |
||
| 137 | $categories = array(); |
||
| 138 | if ( $categories_enabled ) { |
||
| 139 | $categories = get_the_term_list( $form->ID, 'give_forms_category', '', ', ', '' ); |
||
| 140 | $categories = ! is_wp_error( $categories ) ? strip_tags( $categories ) : ''; |
||
| 141 | } |
||
| 142 | |||
| 143 | // Display Tags Data only, if user has opted for it. |
||
| 144 | $tags = array(); |
||
| 145 | if ( $tags_enabled ) { |
||
| 146 | $tags = get_the_term_list( $form->ID, 'give_forms_tag', '', ', ', '' ); |
||
| 147 | $tags = ! is_wp_error( $tags ) ? strip_tags( $tags ) : ''; |
||
| 148 | } |
||
| 149 | |||
| 150 | $sales = give_get_form_sales_stats( $form->ID ); |
||
| 151 | $earnings = give_currency_filter( give_format_amount( give_get_form_earnings_stats( $form->ID ), array( 'sanitize' => false, ) ), '', true ); |
||
| 152 | |||
| 153 | // This will help filter data before appending it to PDF Receipt. |
||
| 154 | $prepare_pdf_data = array(); |
||
| 155 | $prepare_pdf_data[] = $title; |
||
| 156 | $prepare_pdf_data[] = $price; |
||
| 157 | |||
| 158 | // Append Categories Data only, if user has opted for it. |
||
| 159 | if ( $categories_enabled ) { |
||
| 160 | $prepare_pdf_data[] = $categories; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Append Tags Data only, if user has opted for it. |
||
| 164 | if ( $tags_enabled ) { |
||
| 165 | $prepare_pdf_data[] = $tags; |
||
| 166 | } |
||
| 167 | |||
| 168 | $prepare_pdf_data[] = $sales; |
||
| 169 | $prepare_pdf_data[] = $earnings; |
||
| 170 | |||
| 171 | $pdf->Row( $prepare_pdf_data ); |
||
| 172 | |||
| 173 | endforeach; |
||
| 174 | } else { |
||
| 175 | |||
| 176 | // Fix: Minor Styling Alignment Issue for PDF. |
||
| 177 | if ( $categories_enabled && $tags_enabled ) { |
||
| 178 | $no_found_width = 280; |
||
| 179 | } elseif ( $categories_enabled || $tags_enabled ) { |
||
| 180 | $no_found_width = 235; |
||
| 181 | } else { |
||
| 182 | $no_found_width = 190; |
||
| 183 | } |
||
| 184 | $title = utf8_decode( __( 'No forms found.', 'give' ) ); |
||
| 185 | $pdf->MultiCell( $no_found_width, 5, $title, 1, 'C', false, 1, '', '', true, 0, false, true, 0, 'T', false ); |
||
| 186 | }// End if(). |
||
| 187 | $pdf->Ln(); |
||
| 188 | $pdf->SetTextColor( 50, 50, 50 ); |
||
| 189 | $pdf->SetFont( $default_font, '', 14 ); |
||
| 190 | |||
| 191 | // Output Graph on a new page. |
||
| 192 | $pdf->AddPage( 'L', 'A4' ); |
||
| 193 | $pdf->Cell( 0, 10, utf8_decode( __( 'Graph View', 'give' ) ), 0, 2, 'L', false ); |
||
| 194 | $pdf->SetFont( $default_font, '', 12 ); |
||
| 195 | |||
| 196 | $image = html_entity_decode( urldecode( give_draw_chart_image() ) ); |
||
| 197 | $image = str_replace( ' ', '%20', $image ); |
||
| 198 | |||
| 199 | $pdf->SetX( 25 ); |
||
| 200 | $pdf->Image( $image . '&file=.png' ); |
||
| 201 | $pdf->Ln( 7 ); |
||
| 202 | $pdf->Output( apply_filters( 'give_sales_earnings_pdf_export_filename', 'give-report-' . date_i18n( 'Y-m-d' ) ) . '.pdf', 'D' ); |
||
| 203 | } |
||
| 204 | |||
| 340 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.