ravinderk /
Give
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Donor List Table Class. |
||
| 4 | * |
||
| 5 | * The list view under WP-Admin > Donations > Donors. |
||
| 6 | * |
||
| 7 | * @package Give |
||
| 8 | * @subpackage Admin/Reports |
||
| 9 | * @copyright Copyright (c) 2016, WordImpress |
||
| 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
||
| 11 | * @since 1.0 |
||
| 12 | */ |
||
| 13 | |||
| 14 | // Exit if accessed directly. |
||
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 16 | exit; |
||
| 17 | } |
||
| 18 | |||
| 19 | // Load WP_List_Table if not loaded |
||
| 20 | if ( ! class_exists( 'WP_List_Table' ) ) { |
||
| 21 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Give_Donor_List_Table Class. |
||
| 26 | * |
||
| 27 | * @since 1.0 |
||
| 28 | */ |
||
| 29 | class Give_Donor_List_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() { |
|
|
0 ignored issues
–
show
|
|||
| 62 | |||
| 63 | // Set parent defaults |
||
| 64 | parent::__construct( array( |
||
| 65 | 'singular' => __( 'Donor', 'give' ), // Singular name of the listed records. |
||
| 66 | 'plural' => __( 'Donors', 'give' ), // Plural name of the listed records. |
||
| 67 | 'ajax' => false,// Does this table support ajax?. |
||
| 68 | ) ); |
||
| 69 | |||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Show the search field. |
||
| 74 | * |
||
| 75 | * @since 1.0 |
||
| 76 | * @access public |
||
| 77 | * |
||
| 78 | * @param string $text Label for the search box. |
||
| 79 | * @param string $input_id ID of the search box. |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function search_box( $text, $input_id ) { |
|
| 84 | $input_id = $input_id . '-search-input'; |
||
| 85 | |||
| 86 | if ( ! empty( $_REQUEST['orderby'] ) ) { |
||
| 87 | echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
||
| 88 | } |
||
| 89 | if ( ! empty( $_REQUEST['order'] ) ) { |
||
| 90 | echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
||
| 91 | } |
||
| 92 | ?> |
||
| 93 | <p class="search-box" role="search"> |
||
| 94 | <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label> |
||
| 95 | <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/> |
||
| 96 | <?php submit_button( $text, 'button', false, false, array( |
||
| 97 | 'ID' => 'search-submit', |
||
| 98 | ) ); ?> |
||
| 99 | </p> |
||
| 100 | <?php |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * This function renders most of the columns in the list table. |
||
| 105 | * |
||
| 106 | * @access public |
||
| 107 | * @since 1.0 |
||
| 108 | * |
||
| 109 | * @param array $donor Contains all the data of the donors. |
||
| 110 | * @param string $column_name The name of the column. |
||
| 111 | * |
||
| 112 | * @return string Column Name. |
||
| 113 | */ |
||
| 114 | public function column_default( $donor, $column_name ) { |
||
| 115 | switch ( $column_name ) { |
||
| 116 | |||
| 117 | View Code Duplication | case 'num_donations' : |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
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. Loading history...
|
|||
| 118 | $value = sprintf( |
||
| 119 | '<a href="%s">%s</a>', |
||
| 120 | admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . absint( $donor['id'] ) ), |
||
| 121 | esc_html( $donor['num_donations'] ) |
||
| 122 | ); |
||
| 123 | break; |
||
| 124 | |||
| 125 | View Code Duplication | case 'amount_spent' : |
|
| 126 | $value = give_currency_filter( give_format_amount( $donor[ $column_name ], array( 'sanitize' => false ) ) ); |
||
| 127 | break; |
||
| 128 | |||
| 129 | case 'date_created' : |
||
| 130 | $value = date_i18n( give_date_format(), strtotime( $donor['date_created'] ) ); |
||
| 131 | break; |
||
| 132 | |||
| 133 | default: |
||
| 134 | $value = isset( $donor[ $column_name ] ) ? $donor[ $column_name ] : null; |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | |||
| 138 | return apply_filters( "give_report_column_{$column_name}", $value, $donor['id'] ); |
||
| 139 | |||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Column name. |
||
| 144 | * |
||
| 145 | * @param $donor |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function column_name( $donor ) { |
||
| 150 | $name = '#' . $donor['id'] . ' '; |
||
| 151 | $name .= ! empty( $donor['name'] ) ? $donor['name'] : '<em>' . esc_html__( 'Unnamed Donor', 'give' ) . '</em>'; |
||
| 152 | $view_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor['id'] ); |
||
| 153 | $actions = $this->get_row_actions( $donor ); |
||
| 154 | |||
| 155 | return '<a href="' . esc_url( $view_url ) . '">' . $name . '</a>' . $this->row_actions( $actions ); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Retrieve the table columns. |
||
| 160 | * |
||
| 161 | * @access public |
||
| 162 | * @since 1.0 |
||
| 163 | * @return array $columns Array of all the list table columns. |
||
| 164 | */ |
||
| 165 | public function get_columns() { |
||
| 166 | $columns = array( |
||
| 167 | 'name' => __( 'Name', 'give' ), |
||
| 168 | 'email' => __( 'Email', 'give' ), |
||
| 169 | 'num_donations' => __( 'Donations', 'give' ), |
||
| 170 | 'amount_spent' => __( 'Total Donated', 'give' ), |
||
| 171 | 'date_created' => __( 'Date Created', 'give' ), |
||
| 172 | ); |
||
| 173 | |||
| 174 | return apply_filters( 'give_list_donors_columns', $columns ); |
||
| 175 | |||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the sortable columns. |
||
| 180 | * |
||
| 181 | * @access public |
||
| 182 | * @since 2.1 |
||
| 183 | * @return array Array of all the sortable columns. |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function get_sortable_columns() { |
|
| 186 | |||
| 187 | $columns = array( |
||
| 188 | 'date_created' => array( 'date_created', true ), |
||
| 189 | 'name' => array( 'name', true ), |
||
| 190 | 'num_donations' => array( 'purchase_count', false ), |
||
| 191 | 'amount_spent' => array( 'purchase_value', false ), |
||
| 192 | ); |
||
| 193 | |||
| 194 | return apply_filters( 'give_list_donors_sortable_columns', $columns ); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Retrieve row actions. |
||
| 199 | * |
||
| 200 | * @since 1.7 |
||
| 201 | * @access public |
||
| 202 | * |
||
| 203 | * @param $donor |
||
| 204 | * |
||
| 205 | * @return array An array of action links. |
||
| 206 | */ |
||
| 207 | public function get_row_actions( $donor ) { |
||
| 208 | |||
| 209 | $actions = array( |
||
| 210 | |||
| 211 | 'view' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor['id'] ), sprintf( esc_attr__( 'View "%s"', 'give' ), $donor['name'] ), __( 'View Donor', 'give' ) ), |
||
| 212 | |||
| 213 | 'notes' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=notes&id=' . $donor['id'] ), sprintf( esc_attr__( 'Notes for "%s"', 'give' ), $donor['name'] ), __( 'Notes', 'give' ) ), |
||
| 214 | |||
| 215 | 'delete' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $donor['id'] ), sprintf( esc_attr__( 'Delete "%s"', 'give' ), $donor['name'] ), __( 'Delete', 'give' ) ), |
||
| 216 | |||
| 217 | ); |
||
| 218 | |||
| 219 | return apply_filters( 'give_donor_row_actions', $actions, $donor ); |
||
| 220 | |||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Outputs bulk reviews |
||
| 225 | * |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param $which |
||
| 229 | * |
||
| 230 | * @since 1.0 |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | public function bulk_actions( $which = '' ) { |
||
| 234 | // These aren't really bulk actions but this outputs the markup in the right place. |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Retrieve the current page number. |
||
| 239 | * |
||
| 240 | * @access public |
||
| 241 | * @since 1.0 |
||
| 242 | * @return int Current page number. |
||
| 243 | */ |
||
| 244 | public function get_paged() { |
||
| 245 | return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Retrieves the search query string. |
||
| 250 | * |
||
| 251 | * @access public |
||
| 252 | * @since 1.0 |
||
| 253 | * @return mixed string If search is present, false otherwise. |
||
| 254 | */ |
||
| 255 | public function get_search() { |
||
| 256 | return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Retrieves the donor data from db. |
||
| 261 | * |
||
| 262 | * @access public |
||
| 263 | * @since 1.0 |
||
| 264 | * |
||
| 265 | * @return array $data The Donor data. |
||
| 266 | */ |
||
| 267 | View Code Duplication | public function donor_data() { |
|
| 268 | |||
| 269 | $data = array(); |
||
| 270 | |||
| 271 | // Get donor query. |
||
| 272 | $args = $this->get_donor_query(); |
||
| 273 | $donors = Give()->donors->get_donors( $args ); |
||
| 274 | |||
| 275 | if ( $donors ) { |
||
| 276 | |||
| 277 | foreach ( $donors as $donor ) { |
||
| 278 | |||
| 279 | $user_id = ! empty( $donor->user_id ) ? intval( $donor->user_id ) : 0; |
||
| 280 | |||
| 281 | $data[] = array( |
||
| 282 | 'id' => $donor->id, |
||
| 283 | 'user_id' => $user_id, |
||
| 284 | 'name' => $donor->name, |
||
| 285 | 'email' => $donor->email, |
||
| 286 | 'num_donations' => $donor->purchase_count, |
||
| 287 | 'amount_spent' => $donor->purchase_value, |
||
| 288 | 'date_created' => $donor->date_created, |
||
| 289 | ); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | return apply_filters( 'give_donors_column_query_data', $data ); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get donor count. |
||
| 298 | * |
||
| 299 | * @since 1.8.1 |
||
| 300 | * @access private |
||
| 301 | */ |
||
| 302 | View Code Duplication | private function get_donor_count() { |
|
| 303 | // Get donor query. |
||
| 304 | $_donor_query = $this->get_donor_query(); |
||
| 305 | |||
| 306 | $_donor_query['number'] = - 1; |
||
| 307 | $_donor_query['offset'] = 0; |
||
| 308 | $donors = Give()->donors->get_donors( $_donor_query ); |
||
| 309 | |||
| 310 | return count( $donors ); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get donor query. |
||
| 315 | * |
||
| 316 | * @since 1.8.1 |
||
| 317 | * @access public |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function get_donor_query() { |
|
| 321 | $paged = $this->get_paged(); |
||
| 322 | $offset = $this->per_page * ( $paged - 1 ); |
||
| 323 | $search = $this->get_search(); |
||
| 324 | $order = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'DESC'; |
||
| 325 | $orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'id'; |
||
| 326 | |||
| 327 | $args = array( |
||
| 328 | 'number' => $this->per_page, |
||
| 329 | 'offset' => $offset, |
||
| 330 | 'order' => $order, |
||
| 331 | 'orderby' => $orderby, |
||
| 332 | ); |
||
| 333 | |||
| 334 | if ( $search ) { |
||
| 335 | if ( is_email( $search ) ) { |
||
| 336 | $args['email'] = $search; |
||
| 337 | } elseif ( is_numeric( $search ) ) { |
||
| 338 | $args['id'] = $search; |
||
| 339 | } else { |
||
| 340 | $args['name'] = $search; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | return $args; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Setup the final data for the table. |
||
| 349 | * |
||
| 350 | * @access public |
||
| 351 | * @since 1.0 |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | View Code Duplication | public function prepare_items() { |
|
| 355 | |||
| 356 | $columns = $this->get_columns(); |
||
| 357 | $hidden = array(); // No hidden columns. |
||
| 358 | $sortable = $this->get_sortable_columns(); |
||
| 359 | |||
| 360 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
||
| 361 | |||
| 362 | $this->items = $this->donor_data(); |
||
| 363 | |||
| 364 | $this->total = $this->get_donor_count(); |
||
| 365 | |||
| 366 | $this->set_pagination_args( array( |
||
| 367 | 'total_items' => $this->total, |
||
| 368 | 'per_page' => $this->per_page, |
||
| 369 | 'total_pages' => ceil( $this->total / $this->per_page ), |
||
| 370 | ) ); |
||
| 371 | } |
||
| 372 | } |
||
| 373 |
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.