| Conditions | 5 |
| Paths | 5 |
| Total Lines | 79 |
| Code Lines | 36 |
| 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 |
||
| 154 | function give_get_preview_email_header() { |
||
| 155 | |||
| 156 | //Payment receipt switcher |
||
| 157 | $payment_count = give_count_payments()->publish; |
||
| 158 | $payment_id = give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ); |
||
| 159 | |||
| 160 | if ( $payment_count <= 0 ) { |
||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | //Get payments. |
||
| 165 | $payments = new Give_Payments_Query( array( |
||
| 166 | 'number' => 100, |
||
| 167 | ) ); |
||
| 168 | $payments = $payments->get_payments(); |
||
| 169 | $options = array(); |
||
| 170 | |||
| 171 | // Default option. |
||
| 172 | $options[0] = esc_html__( 'No donations found.', 'give' ); |
||
| 173 | |||
| 174 | //Provide nice human readable options. |
||
| 175 | if ( $payments ) { |
||
| 176 | $options[0] = esc_html__( '- Select a donation -', 'give' ); |
||
| 177 | foreach ( $payments as $payment ) { |
||
| 178 | |||
| 179 | $options[ $payment->ID ] = esc_html( '#' . $payment->ID . ' - ' . $payment->email . ' - ' . $payment->form_title ); |
||
| 180 | |||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | //Start constructing HTML output. |
||
| 185 | $transaction_header = '<div style="margin:0;padding:10px 0;width:100%;background-color:#FFF;border-bottom:1px solid #eee; text-align:center;">'; |
||
| 186 | |||
| 187 | //Inline JS function for switching donations. |
||
| 188 | $request_url = $_SERVER['REQUEST_URI']; |
||
| 189 | |||
| 190 | // Remove payment id query param if set from request url. |
||
| 191 | if ( $payment_id ) { |
||
| 192 | $request_url_data = wp_parse_url( $_SERVER['REQUEST_URI'] ); |
||
| 193 | $query = $request_url_data['query']; |
||
| 194 | $query = str_replace( "&preview_id={$payment_id}", '', $query ); |
||
| 195 | |||
| 196 | $request_url = home_url( '/?' . str_replace( '', '', $query ) ); |
||
| 197 | } |
||
| 198 | |||
| 199 | |||
| 200 | $transaction_header .= '<script> |
||
| 201 | function change_preview(){ |
||
| 202 | var transactions = document.getElementById("give_preview_email_payment_id"); |
||
| 203 | var selected_trans = transactions.options[transactions.selectedIndex]; |
||
| 204 | console.log(selected_trans); |
||
| 205 | if (selected_trans){ |
||
| 206 | var url_string = "' . $request_url . '&preview_id=" + selected_trans.value; |
||
| 207 | window.location = url_string; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | </script>'; |
||
| 211 | |||
| 212 | $transaction_header .= '<label for="give_preview_email_payment_id" style="font-size:12px;color:#333;margin:0 4px 0 0;">' . esc_html__( 'Preview email with a donation:', 'give' ) . '</label>'; |
||
| 213 | |||
| 214 | //The select field with 100 latest transactions |
||
| 215 | $transaction_header .= Give()->html->select( array( |
||
| 216 | 'name' => 'preview_email_payment_id', |
||
| 217 | 'selected' => $payment_id, |
||
| 218 | 'id' => 'give_preview_email_payment_id', |
||
| 219 | 'class' => 'give-preview-email-payment-id', |
||
| 220 | 'options' => $options, |
||
| 221 | 'chosen' => false, |
||
| 222 | 'select_atts' => 'onchange="change_preview()"', |
||
| 223 | 'show_option_all' => false, |
||
| 224 | 'show_option_none' => false, |
||
| 225 | ) ); |
||
| 226 | |||
| 227 | //Closing tag |
||
| 228 | $transaction_header .= '</div>'; |
||
| 229 | |||
| 230 | return apply_filters( 'give_preview_email_receipt_header', $transaction_header ); |
||
| 231 | |||
| 232 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.