class-donor-reports-table.php
6 years ago
class-earnings-report.php
5 years ago
class-form-reports-table.php
5 years ago
class-forms-report.php
7 years ago
class-gateways-report.php
7 years ago
class-gateways-reports-table.php
4 years ago
class-give-graph.php
1 year ago
graphing.php
4 years ago
reports.php
1 year ago
class-form-reports-table.php
356 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Download Reports Table Class |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Reports |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | // Load WP_List_Table if not loaded |
| 18 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 19 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Give_Form_Reports_Table Class |
| 24 | * |
| 25 | * Renders the Form Reports table |
| 26 | * |
| 27 | * @since 1.0 |
| 28 | */ |
| 29 | class Give_Form_Reports_Table extends WP_List_Table { |
| 30 | |
| 31 | /** |
| 32 | * @var int Number of items per page |
| 33 | * @since 1.0 |
| 34 | */ |
| 35 | public $per_page = 30; |
| 36 | |
| 37 | /** |
| 38 | * @var object Query results of all the donation forms |
| 39 | * @since 1.0 |
| 40 | */ |
| 41 | private $donation_forms; |
| 42 | |
| 43 | /** |
| 44 | * @var int Total number of Donation Forms |
| 45 | * @since 1.8.11 |
| 46 | */ |
| 47 | public $count; |
| 48 | |
| 49 | /** |
| 50 | * Get things started |
| 51 | * |
| 52 | * @since 1.0 |
| 53 | * @see WP_List_Table::__construct() |
| 54 | */ |
| 55 | public function __construct() { |
| 56 | global $status, $page; |
| 57 | |
| 58 | // Set parent defaults |
| 59 | parent::__construct( |
| 60 | [ |
| 61 | 'singular' => give_get_forms_label_singular(), // Singular name of the listed records. |
| 62 | 'plural' => give_get_forms_label_plural(), // Plural name of the listed records. |
| 63 | 'ajax' => false, // Does this table support ajax? |
| 64 | ] |
| 65 | ); |
| 66 | |
| 67 | add_action( 'give_report_view_actions', [ $this, 'category_filter' ] ); |
| 68 | $this->query(); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * This function renders most of the columns in the list table. |
| 74 | * |
| 75 | * @param array $item Contains all the data of the donation form |
| 76 | * @param string $column_name The name of the column |
| 77 | * |
| 78 | * @access public |
| 79 | * @since 1.0 |
| 80 | * |
| 81 | * @return string Column Name |
| 82 | */ |
| 83 | public function column_default( $item, $column_name ) { |
| 84 | switch ( $column_name ) { |
| 85 | case 'title': |
| 86 | $title = empty( $item['title'] ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $item['ID'] ) : $item['title']; |
| 87 | |
| 88 | return sprintf( |
| 89 | '<a href="%s">%s</a>', |
| 90 | get_edit_post_link( $item['ID'] ), |
| 91 | $title |
| 92 | ); |
| 93 | case 'sales': |
| 94 | return sprintf( |
| 95 | '<a href="%s">%s</a>', |
| 96 | admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&form_id=' . urlencode( $item['ID'] ) ), |
| 97 | $item['sales'] |
| 98 | ); |
| 99 | case 'earnings': |
| 100 | return give_currency_filter( give_format_amount( $item[ $column_name ], [ 'sanitize' => false ] ) ); |
| 101 | case 'average_sales': |
| 102 | return round( $item[ $column_name ] ); |
| 103 | case 'average_earnings': |
| 104 | return give_currency_filter( give_format_amount( $item[ $column_name ], [ 'sanitize' => false ] ) ); |
| 105 | case 'details': |
| 106 | return '<a href="' . admin_url( 'edit.php?post_type=give_forms&page=give-reports&tab=forms&form-id=' . $item['ID'] ) . '">' . esc_html__( 'View Detailed Report', 'give' ) . '</a>'; |
| 107 | default: |
| 108 | return $item[ $column_name ]; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Retrieve the table columns |
| 114 | * |
| 115 | * @access public |
| 116 | * @since 1.0 |
| 117 | * |
| 118 | * @return array $columns Array of all the list table columns |
| 119 | */ |
| 120 | public function get_columns() { |
| 121 | $columns = [ |
| 122 | 'title' => esc_html__( 'Form', 'give' ), |
| 123 | 'sales' => esc_html__( 'Donations', 'give' ), |
| 124 | 'earnings' => esc_html__( 'Revenue', 'give' ), |
| 125 | 'average_sales' => esc_html__( 'Monthly Average Donations', 'give' ), |
| 126 | 'average_earnings' => esc_html__( 'Monthly Average Revenue', 'give' ), |
| 127 | 'details' => esc_html__( 'Detailed Report', 'give' ), |
| 128 | ]; |
| 129 | |
| 130 | return $columns; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Retrieve the table's sortable columns |
| 135 | * |
| 136 | * @access public |
| 137 | * @since 1.0 |
| 138 | * |
| 139 | * @return array Array of all the sortable columns |
| 140 | */ |
| 141 | public function get_sortable_columns() { |
| 142 | return [ |
| 143 | 'title' => [ 'title', true ], |
| 144 | 'sales' => [ 'sales', false ], |
| 145 | 'earnings' => [ 'earnings', false ], |
| 146 | ]; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Retrieve the current page number |
| 151 | * |
| 152 | * @access public |
| 153 | * @since 1.0 |
| 154 | * |
| 155 | * @return int Current page number |
| 156 | */ |
| 157 | public function get_paged() { |
| 158 | return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Retrieve the category being viewed |
| 163 | * |
| 164 | * @access public |
| 165 | * @since 1.0 |
| 166 | * |
| 167 | * @return int Category ID |
| 168 | */ |
| 169 | public function get_category() { |
| 170 | return isset( $_GET['category'] ) ? absint( $_GET['category'] ) : 0; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Outputs the reporting views |
| 175 | * |
| 176 | * @access public |
| 177 | * @since 1.0 |
| 178 | * |
| 179 | * @return void |
| 180 | */ |
| 181 | public function bulk_actions( $which = '' ) { |
| 182 | |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Generate the table navigation above or below the table |
| 187 | * |
| 188 | * @since 1.0 |
| 189 | * @access protected |
| 190 | * |
| 191 | * @param string $which |
| 192 | */ |
| 193 | protected function display_tablenav( $which ) { |
| 194 | |
| 195 | if ( 'top' === $which ) { |
| 196 | wp_nonce_field( 'bulk-' . $this->_args['plural'] ); |
| 197 | } |
| 198 | ?> |
| 199 | <div class="tablenav give-clearfix <?php echo esc_attr( $which ); ?>"> |
| 200 | |
| 201 | <?php if ( 'top' === $which ) { ?> |
| 202 | <h2 class="alignleft reports-earnings-title screen-reader-text"> |
| 203 | <?php _e( 'Donation Forms Report', 'give' ); ?> |
| 204 | </h2> |
| 205 | <?php } ?> |
| 206 | |
| 207 | <div class="alignright tablenav-right"> |
| 208 | <div class="actions bulkactions"> |
| 209 | <?php $this->bulk_actions( $which ); ?> |
| 210 | </div> |
| 211 | <?php |
| 212 | $this->extra_tablenav( $which ); |
| 213 | $this->pagination( $which ); |
| 214 | ?> |
| 215 | </div> |
| 216 | |
| 217 | <br class="clear" /> |
| 218 | |
| 219 | </div> |
| 220 | <?php |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Attaches the category filter to the log views |
| 225 | * |
| 226 | * @access public |
| 227 | * @since 1.0 |
| 228 | * |
| 229 | * @return void |
| 230 | */ |
| 231 | public function category_filter() { |
| 232 | |
| 233 | $categories = get_terms( 'form_category' ); |
| 234 | if ( $categories && ! is_wp_error( $categories ) ) { |
| 235 | echo Give()->html->category_dropdown( 'category', $this->get_category() ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Performs the donation forms query |
| 241 | * |
| 242 | * @access public |
| 243 | * @since 1.0 |
| 244 | * |
| 245 | * @return void |
| 246 | */ |
| 247 | public function query() { |
| 248 | |
| 249 | $orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : 'title'; |
| 250 | $order = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC'; |
| 251 | $category = $this->get_category(); |
| 252 | |
| 253 | $args = [ |
| 254 | 'post_type' => 'give_forms', |
| 255 | 'post_status' => 'publish', |
| 256 | 'order' => $order, |
| 257 | 'fields' => 'ids', |
| 258 | 'posts_per_page' => $this->per_page, |
| 259 | 'paged' => $this->get_paged(), |
| 260 | 'suppress_filters' => true, |
| 261 | ]; |
| 262 | |
| 263 | if ( ! empty( $category ) ) { |
| 264 | $args['tax_query'] = [ |
| 265 | [ |
| 266 | 'taxonomy' => 'form_category', |
| 267 | 'terms' => $category, |
| 268 | ], |
| 269 | ]; |
| 270 | } |
| 271 | |
| 272 | switch ( $orderby ) : |
| 273 | case 'title': |
| 274 | $args['orderby'] = 'title'; |
| 275 | break; |
| 276 | |
| 277 | case 'sales': |
| 278 | $args['orderby'] = 'meta_value_num'; |
| 279 | $args['meta_key'] = '_give_form_sales'; |
| 280 | break; |
| 281 | |
| 282 | case 'earnings': |
| 283 | $args['orderby'] = 'meta_value_num'; |
| 284 | $args['meta_key'] = '_give_form_earnings'; |
| 285 | break; |
| 286 | endswitch; |
| 287 | |
| 288 | $args = apply_filters( 'give_form_reports_prepare_items_args', $args, $this ); |
| 289 | |
| 290 | $this->donation_forms = new WP_Query( $args ); |
| 291 | |
| 292 | // Store total number of donation forms count. |
| 293 | $this->count = $this->donation_forms->found_posts; |
| 294 | |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Build all the reports data |
| 299 | * |
| 300 | * @access public |
| 301 | * @since 1.0 |
| 302 | * |
| 303 | * @return array $reports_data All the data for donor reports |
| 304 | */ |
| 305 | public function reports_data() { |
| 306 | $reports_data = []; |
| 307 | |
| 308 | $give_forms = $this->donation_forms->posts; |
| 309 | |
| 310 | if ( $give_forms ) { |
| 311 | foreach ( $give_forms as $form ) { |
| 312 | $reports_data[] = [ |
| 313 | 'ID' => $form, |
| 314 | 'title' => get_the_title( $form ), |
| 315 | 'sales' => give_get_form_sales_stats( $form ), |
| 316 | 'earnings' => give_get_form_earnings_stats( $form ), |
| 317 | 'average_sales' => give_get_average_monthly_form_sales( $form ), |
| 318 | 'average_earnings' => give_get_average_monthly_form_earnings( $form ), |
| 319 | ]; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return $reports_data; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Setup the final data for the table |
| 328 | * |
| 329 | * @access public |
| 330 | * @since 1.5 |
| 331 | * |
| 332 | * @uses Give_Form_Reports_Table::get_columns() |
| 333 | * @uses Give_Form_Reports_Table::get_sortable_columns() |
| 334 | * @uses Give_Form_Reports_Table::reports_data() |
| 335 | * @uses Give_Form_Reports_Table::get_pagenum() |
| 336 | * |
| 337 | * @return void |
| 338 | */ |
| 339 | public function prepare_items() { |
| 340 | $columns = $this->get_columns(); |
| 341 | $hidden = []; // No hidden columns |
| 342 | $sortable = $this->get_sortable_columns(); |
| 343 | $this->_column_headers = [ $columns, $hidden, $sortable ]; |
| 344 | $this->items = $this->reports_data(); |
| 345 | $total_items = $this->count; |
| 346 | |
| 347 | $this->set_pagination_args( |
| 348 | [ |
| 349 | 'total_items' => $total_items, |
| 350 | 'per_page' => $this->per_page, |
| 351 | 'total_pages' => ceil( $total_items / $this->per_page ), |
| 352 | ] |
| 353 | ); |
| 354 | } |
| 355 | } |
| 356 |