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 |
||
| 29 | class Give_Donor_Reports_Table extends WP_List_Table { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Number of items per page |
||
| 33 | * |
||
| 34 | * @var int |
||
| 35 | * @since 1.0 |
||
| 36 | */ |
||
| 37 | public $per_page = 30; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Number of donors found |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | * @since 1.0 |
||
| 44 | */ |
||
| 45 | public $count = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Total donors |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | * @since 1.0 |
||
| 52 | */ |
||
| 53 | public $total = 0; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get things started |
||
| 57 | * |
||
| 58 | * @since 1.0 |
||
| 59 | * @see WP_List_Table::__construct() |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function __construct() { |
|
|
|
|||
| 62 | global $status, $page; |
||
| 63 | |||
| 64 | // Set parent defaults |
||
| 65 | parent::__construct( array( |
||
| 66 | 'singular' => esc_html__( 'Donor', 'give' ), // Singular name of the listed records |
||
| 67 | 'plural' => esc_html__( 'Donors', 'give' ), // Plural name of the listed records |
||
| 68 | 'ajax' => false,// Does this table support ajax? |
||
| 69 | ) ); |
||
| 70 | |||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Remove default search field in favor for repositioned location |
||
| 75 | * |
||
| 76 | * Reposition the search field |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @param string $text Label for the search box |
||
| 82 | * @param string $input_id ID of the search box |
||
| 83 | * |
||
| 84 | * @return false |
||
| 85 | */ |
||
| 86 | public function search_box( $text, $input_id ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Show the search field |
||
| 92 | * |
||
| 93 | * @since 1.0 |
||
| 94 | * @access public |
||
| 95 | * |
||
| 96 | * @param string $text Label for the search box |
||
| 97 | * @param string $input_id ID of the search box |
||
| 98 | * |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function give_search_box( $text, $input_id ) { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Generate the table navigation above or below the table |
||
| 121 | * |
||
| 122 | * @since 1.0 |
||
| 123 | * @access protected |
||
| 124 | * |
||
| 125 | * @param string $which |
||
| 126 | */ |
||
| 127 | protected function display_tablenav( $which ) { |
||
| 128 | |||
| 129 | if ( 'top' === $which ) { |
||
| 130 | wp_nonce_field( 'bulk-' . $this->_args['plural'] ); |
||
| 131 | } |
||
| 132 | ?> |
||
| 133 | <div class="tablenav give-clearfix <?php echo esc_attr( $which ); ?>"> |
||
| 134 | |||
| 135 | <?php if ( 'top' === $which ) { ?> |
||
| 136 | <h2 class="alignleft reports-earnings-title"> |
||
| 137 | <?php esc_html_e( 'Donors Report', 'give' ); ?> |
||
| 138 | </h2> |
||
| 139 | <?php } ?> |
||
| 140 | |||
| 141 | <div class="alignright tablenav-right"> |
||
| 142 | <div class="actions bulkactions"> |
||
| 143 | <?php |
||
| 144 | if ( 'top' === $which ) { |
||
| 145 | $this->give_search_box( esc_html__( 'Search Donors', 'give' ), 'give-donors-report-search' ); |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->bulk_actions( $which ); ?> |
||
| 149 | |||
| 150 | </div> |
||
| 151 | <?php |
||
| 152 | $this->extra_tablenav( $which ); |
||
| 153 | $this->pagination( $which ); |
||
| 154 | ?> |
||
| 155 | </div> |
||
| 156 | |||
| 157 | |||
| 158 | <br class="clear"/> |
||
| 159 | |||
| 160 | </div> |
||
| 161 | <?php |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * This function renders most of the columns in the list table. |
||
| 166 | * |
||
| 167 | * @access public |
||
| 168 | * @since 1.0 |
||
| 169 | * |
||
| 170 | * @param array $item Contains all the data of the donors |
||
| 171 | * @param string $column_name The name of the column |
||
| 172 | * |
||
| 173 | * @return string Column Name |
||
| 174 | */ |
||
| 175 | public function column_default( $item, $column_name ) { |
||
| 176 | |||
| 177 | switch ( $column_name ) { |
||
| 178 | |||
| 179 | case 'name' : |
||
| 180 | $name = '#' . $item['id'] . ' '; |
||
| 181 | $name .= ! empty( $item['name'] ) ? $item['name'] : '<em>' . esc_html__( 'Unnamed Donor', 'give' ) . '</em>'; |
||
| 182 | $view_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $item['id'] ); |
||
| 183 | $value = '<a href="' . esc_url( $view_url ) . '">' . $name . '</a>'; |
||
| 184 | break; |
||
| 185 | |||
| 186 | View Code Duplication | case 'num_donations' : |
|
| 187 | $value = sprintf( |
||
| 188 | '<a href="%s">%s</a>', |
||
| 189 | admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . absint( $item['id'] ) ), |
||
| 190 | esc_html( $item['num_donations'] ) |
||
| 191 | ); |
||
| 192 | break; |
||
| 193 | |||
| 194 | View Code Duplication | case 'amount_spent' : |
|
| 195 | $value = give_currency_filter( give_format_amount( $item[ $column_name ], array( 'sanitize' => false ) ) ); |
||
| 196 | break; |
||
| 197 | |||
| 198 | default: |
||
| 199 | $value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null; |
||
| 200 | break; |
||
| 201 | } |
||
| 202 | |||
| 203 | return apply_filters( "give_report_column_{$column_name}", $value, $item['id'] ); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Retrieve the table columns |
||
| 208 | * |
||
| 209 | * @access public |
||
| 210 | * @since 1.0 |
||
| 211 | * @return array $columns Array of all the list table columns |
||
| 212 | */ |
||
| 213 | public function get_columns() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the sortable columns |
||
| 227 | * |
||
| 228 | * @access public |
||
| 229 | * @since 1.0 |
||
| 230 | * @return array Array of all the sortable columns |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function get_sortable_columns() { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Outputs the reporting views |
||
| 243 | * |
||
| 244 | * @access public |
||
| 245 | * @since 1.0 |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | public function bulk_actions( $which = '' ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Retrieve the current page number |
||
| 254 | * |
||
| 255 | * @access public |
||
| 256 | * @since 1.0 |
||
| 257 | * @return int Current page number |
||
| 258 | */ |
||
| 259 | public function get_paged() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Retrieves the search query string |
||
| 265 | * |
||
| 266 | * @access public |
||
| 267 | * @since 1.0 |
||
| 268 | * @return mixed string If search is present, false otherwise |
||
| 269 | */ |
||
| 270 | public function get_search() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Build all the reports data |
||
| 276 | * |
||
| 277 | * @access public |
||
| 278 | * @since 1.0 |
||
| 279 | * @global object $wpdb Used to query the database using the WordPress |
||
| 280 | * Database API |
||
| 281 | * @return array $reports_data All the data for donor reports |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function reports_data() { |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Get donor count. |
||
| 316 | * |
||
| 317 | * @since 1.8.1 |
||
| 318 | * @access private |
||
| 319 | */ |
||
| 320 | View Code Duplication | private function get_donor_count() { |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get donor query. |
||
| 333 | * |
||
| 334 | * @since 1.8.1 |
||
| 335 | * @access public |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function get_donor_query() { |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Setup the final data for the table |
||
| 367 | * |
||
| 368 | * @access public |
||
| 369 | * @since 1.0 |
||
| 370 | * @uses Give_Donor_Reports_Table::get_columns() |
||
| 371 | * @uses WP_List_Table::get_sortable_columns() |
||
| 372 | * @uses Give_Donor_Reports_Table::get_pagenum() |
||
| 373 | * @uses Give_Donor_Reports_Table::get_total_donors() |
||
| 374 | * @return void |
||
| 375 | */ |
||
| 376 | View Code Duplication | public function prepare_items() { |
|
| 394 | } |
||
| 395 |
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.