Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 28 | class Give_Sales_Log_Table extends WP_List_Table { |
||
| 29 | /** |
||
| 30 | * Number of results to show per page |
||
| 31 | * |
||
| 32 | * @since 1.0 |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | public $per_page = 30; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get things started |
||
| 39 | * |
||
| 40 | * @since 1.0 |
||
| 41 | * @see WP_List_Table::__construct() |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function __construct() { |
|
|
|
|||
| 44 | global $status, $page; |
||
| 45 | |||
| 46 | // Set parent defaults |
||
| 47 | parent::__construct( array( |
||
| 48 | 'singular' => give_get_forms_label_singular(), // Singular name of the listed records |
||
| 49 | 'plural' => give_get_forms_label_plural(), // Plural name of the listed records |
||
| 50 | 'ajax' => false,// Does this table support ajax? |
||
| 51 | ) ); |
||
| 52 | |||
| 53 | add_action( 'give_log_view_actions', array( $this, 'give_forms_filter' ) ); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * This function renders most of the columns in the list table. |
||
| 58 | * |
||
| 59 | * @access public |
||
| 60 | * @since 1.0 |
||
| 61 | * |
||
| 62 | * @param array $item Contains all the data of the discount code |
||
| 63 | * @param string $column_name The name of the column |
||
| 64 | * |
||
| 65 | * @return string Column Name |
||
| 66 | */ |
||
| 67 | public function column_default( $item, $column_name ) { |
||
| 68 | |||
| 69 | $payment = give_get_payment_by( 'id', $item['payment_id'] ); |
||
| 70 | |||
| 71 | switch ( $column_name ) { |
||
| 72 | case 'form' : |
||
| 73 | $form_title = get_the_title( $item[ $column_name ] ); |
||
| 74 | $form_title = empty( $form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $item[ $column_name ] ) : $form_title; |
||
| 75 | |||
| 76 | return '<a href="' . esc_url( add_query_arg( 'form', $item[ $column_name ] ) ) . '" >' . $form_title . '</a>'; |
||
| 77 | |||
| 78 | case 'amount' : |
||
| 79 | $value = give_currency_filter( give_format_amount( $item['amount'], array( 'sanitize' => false ) ) ); |
||
| 80 | $value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) ); |
||
| 81 | |||
| 82 | return $value; |
||
| 83 | |||
| 84 | case 'status' : |
||
| 85 | |||
| 86 | $value = '<div class="give-donation-status status-' . sanitize_title( give_get_payment_status( $payment, true ) ) . '"><span class="give-donation-status-icon"></span> ' . give_get_payment_status( $payment, true ) . '</div>'; |
||
| 87 | |||
| 88 | View Code Duplication | if ( $payment->mode == 'test' ) { |
|
| 89 | $value .= Give()->tooltips->render_span( array( |
||
| 90 | 'label' => __( 'This donation was made in test mode.', 'give' ), |
||
| 91 | 'tag_content' => __( 'Test', 'give' ), |
||
| 92 | 'attributes' => array( |
||
| 93 | 'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label', |
||
| 94 | ), |
||
| 95 | ) ); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $value; |
||
| 99 | |||
| 100 | case 'donation' : |
||
| 101 | $value = Give()->tooltips->render_link( array( |
||
| 102 | 'label' => sprintf( esc_attr__( 'View Donation #%s', 'give' ), $payment->ID ), |
||
| 103 | 'tag_content' => "#$payment->ID", |
||
| 104 | 'link' => esc_url( add_query_arg( 'id', $payment->ID, admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details' ) ) ), |
||
| 105 | ) ); |
||
| 106 | |||
| 107 | if ( ! empty( $item['donor_id'] ) ) { |
||
| 108 | $value .= sprintf( |
||
| 109 | ' %1$s <a href="%2$s">%3$s</a><br>', |
||
| 110 | esc_html__( 'by', 'give' ), |
||
| 111 | admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . $item['donor_id'] ), |
||
| 112 | $item['donor_name'] |
||
| 113 | ); |
||
| 114 | } else { |
||
| 115 | $value .= sprintf( |
||
| 116 | ' %1$s %2$s<br>', |
||
| 117 | esc_html__( 'by', 'give' ), |
||
| 118 | __( 'No donor attached', 'give' ) |
||
| 119 | );; |
||
| 120 | } |
||
| 121 | |||
| 122 | return $value; |
||
| 123 | |||
| 124 | default: |
||
| 125 | return $item[ $column_name ]; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Retrieve the table columns |
||
| 131 | * |
||
| 132 | * @access public |
||
| 133 | * @since 1.0 |
||
| 134 | * @return array $columns Array of all the list table columns |
||
| 135 | */ |
||
| 136 | public function get_columns() { |
||
| 137 | $columns = array( |
||
| 138 | 'ID' => __( 'Log ID', 'give' ), |
||
| 139 | 'donation' => __( 'Donation', 'give' ), |
||
| 140 | 'form' => __( 'Form', 'give' ), |
||
| 141 | 'status' => __( 'Status', 'give' ), |
||
| 142 | 'amount' => __( 'Donation Amount', 'give' ), |
||
| 143 | 'date' => __( 'Date', 'give' ), |
||
| 144 | ); |
||
| 145 | |||
| 146 | return $columns; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Retrieve the current page number |
||
| 151 | * |
||
| 152 | * @access public |
||
| 153 | * @since 1.0 |
||
| 154 | * @return int Current page number |
||
| 155 | */ |
||
| 156 | public function get_paged() { |
||
| 157 | return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Retrieves the user we are filtering logs by, if any |
||
| 162 | * |
||
| 163 | * @access public |
||
| 164 | * @since 1.0 |
||
| 165 | * @return mixed int If User ID, string If Email/Login |
||
| 166 | */ |
||
| 167 | public function get_filtered_user() { |
||
| 168 | return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Retrieves the ID of the give_form we're filtering logs by |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * @since 1.0 |
||
| 176 | * @return int Download ID |
||
| 177 | */ |
||
| 178 | public function get_filtered_give_form() { |
||
| 179 | return ! empty( $_GET['form'] ) ? absint( $_GET['form'] ) : false; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Retrieves the search query string |
||
| 184 | * |
||
| 185 | * @access public |
||
| 186 | * @since 1.0 |
||
| 187 | * @return string|bool string If search is present, false otherwise |
||
| 188 | */ |
||
| 189 | public function get_search() { |
||
| 190 | return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false; |
||
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Display Tablenav (extended) |
||
| 196 | * |
||
| 197 | * Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear |
||
| 198 | * |
||
| 199 | * @see : https://github.com/WordImpress/Give/issues/564 |
||
| 200 | * |
||
| 201 | * @since 1.4.1 |
||
| 202 | * @access protected |
||
| 203 | * |
||
| 204 | * @param string $which |
||
| 205 | */ |
||
| 206 | protected function display_tablenav( $which ) { |
||
| 207 | ?> |
||
| 208 | <div class="tablenav <?php echo esc_attr( $which ); ?>"> |
||
| 209 | |||
| 210 | <?php if ( 'top' === $which ) : ?> |
||
| 211 | <div class="alignleft actions bulkactions"> |
||
| 212 | <?php $this->bulk_actions( $which ); ?> |
||
| 213 | </div> |
||
| 214 | <?php endif; ?> |
||
| 215 | |||
| 216 | <?php |
||
| 217 | $this->extra_tablenav( $which ); |
||
| 218 | $this->pagination( $which ); |
||
| 219 | ?> |
||
| 220 | |||
| 221 | <br class="clear"/> |
||
| 222 | </div> |
||
| 223 | <?php |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets the meta query for the log query |
||
| 229 | * |
||
| 230 | * This is used to return log entries that match our search query, user query, or form query |
||
| 231 | * |
||
| 232 | * @since 1.0 |
||
| 233 | * @access public |
||
| 234 | * |
||
| 235 | * @return array $meta_query |
||
| 236 | */ |
||
| 237 | public function get_meta_query() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Outputs the log views |
||
| 310 | * |
||
| 311 | * @access public |
||
| 312 | * @since 1.0 |
||
| 313 | * @param string $which |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | function bulk_actions( $which = '' ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Sets up the forms filter |
||
| 322 | * |
||
| 323 | * @access public |
||
| 324 | * @since 1.0 |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | public function give_forms_filter() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Gets the log entries for the current view |
||
| 338 | * |
||
| 339 | * @access public |
||
| 340 | * @since 1.0 |
||
| 341 | * |
||
| 342 | * @return array $logs_data Array of all the Log entires |
||
| 343 | */ |
||
| 344 | public function get_logs() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Setup the final data for the table |
||
| 375 | * |
||
| 376 | * @access public |
||
| 377 | * @since 1.0 |
||
| 378 | * @uses Give_Sales_Log_Table::get_columns() |
||
| 379 | * @uses WP_List_Table::get_sortable_columns() |
||
| 380 | * @uses Give_Sales_Log_Table::get_pagenum() |
||
| 381 | * @uses Give_Sales_Log_Table::get_logs() |
||
| 382 | * @uses Give_Sales_Log_Table::get_log_count() |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | public function prepare_items() { |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * Get log query param. |
||
| 406 | * |
||
| 407 | * @since 2.0 |
||
| 408 | * @access public |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | public function get_query_params() { |
||
| 425 | } |
||
| 426 |
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.