| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Reports Page |
| 4 |
* |
| 5 |
* Language Changes from EDD: |
| 6 |
* 1. "Report Type" stays |
| 7 |
* 2. "Earnings" changes to "Income" |
| 8 |
* 3. "Donors" changes to "Donors" |
| 9 |
* 4. "Payment Method" stays. |
| 10 |
* |
| 11 |
* @package Give |
| 12 |
* @subpackage Admin/Reports |
| 13 |
* @copyright Copyright (c) 2016, GiveWP |
| 14 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 15 |
* @since 1.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Reports Page |
| 25 |
* |
| 26 |
* Renders the reports page contents. |
| 27 |
* |
| 28 |
* @since 1.0 |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
function give_reports_page() { |
| 32 |
$current_page = admin_url( 'edit.php?post_type=give_forms&page=give-reports' ); |
| 33 |
$active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings'; |
| 34 |
$views = give_reports_default_views(); |
| 35 |
?> |
| 36 |
<div class="wrap give-settings-page"> |
| 37 |
|
| 38 |
<h1 class="screen-reader-text"><?php echo get_admin_page_title(); ?></h1> |
| 39 |
|
| 40 |
<h2 class="nav-tab-wrapper"> |
| 41 |
<?php foreach ( $views as $tab => $label ) { ?> |
| 42 |
<a href=" |
| 43 |
<?php |
| 44 |
echo esc_url( |
| 45 |
add_query_arg( |
| 46 |
[ |
| 47 |
'tab' => $tab, |
| 48 |
'settings-updated' => false, |
| 49 |
], |
| 50 |
$current_page |
| 51 |
) |
| 52 |
); |
| 53 |
?> |
| 54 |
" class="nav-tab <?php echo $tab === $active_tab ? esc_attr( 'nav-tab-active' ) : ''; ?>"><?php echo esc_html( $label ); ?></a> |
| 55 |
<?php } ?> |
| 56 |
<?php if ( current_user_can( 'export_give_reports' ) ) { ?> |
| 57 |
<a href=" |
| 58 |
<?php |
| 59 |
echo esc_url( |
| 60 |
add_query_arg( |
| 61 |
[ |
| 62 |
'tab' => 'export', |
| 63 |
'settings-updated' => false, |
| 64 |
], |
| 65 |
$current_page |
| 66 |
) |
| 67 |
); |
| 68 |
?> |
| 69 |
" class="nav-tab <?php echo 'export' === $active_tab ? esc_attr( 'nav-tab-active' ) : ''; ?>"><?php esc_html_e( 'Export', 'give' ); ?></a> |
| 70 |
<?php |
| 71 |
} |
| 72 |
/** |
| 73 |
* Fires in the report tabs. |
| 74 |
* |
| 75 |
* Allows you to add new report tabs. |
| 76 |
* |
| 77 |
* @since 1.0 |
| 78 |
*/ |
| 79 |
do_action( 'give_reports_tabs' ); |
| 80 |
?> |
| 81 |
</h2> |
| 82 |
|
| 83 |
<?php |
| 84 |
/** |
| 85 |
* Fires before the report page. |
| 86 |
* |
| 87 |
* @since 1.0 |
| 88 |
*/ |
| 89 |
do_action( 'give_reports_page_top' ); |
| 90 |
|
| 91 |
// Set $active_tab prior to hook firing. |
| 92 |
if ( in_array( $active_tab, array_keys( $views ) ) ) { |
| 93 |
$active_tab = 'reports'; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Fires the report page active tab. |
| 98 |
* |
| 99 |
* @since 1.0 |
| 100 |
*/ |
| 101 |
do_action( "give_reports_tab_{$active_tab}" ); |
| 102 |
|
| 103 |
/** |
| 104 |
* Fires after the report page. |
| 105 |
* |
| 106 |
* @since 1.0 |
| 107 |
*/ |
| 108 |
do_action( 'give_reports_page_bottom' ); |
| 109 |
?> |
| 110 |
</div><!-- .wrap --> |
| 111 |
<?php |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Default Report Views |
| 116 |
* |
| 117 |
* @since 1.0 |
| 118 |
* @return array $views Report Views |
| 119 |
*/ |
| 120 |
function give_reports_default_views() { |
| 121 |
$views = [ |
| 122 |
'earnings' => esc_html__( 'Revenue', 'give' ), |
| 123 |
'forms' => esc_html__( 'Forms', 'give' ), |
| 124 |
'gateways' => esc_html__( 'Donation Methods', 'give' ), |
| 125 |
]; |
| 126 |
|
| 127 |
$views = apply_filters( 'give_report_views', $views ); |
| 128 |
|
| 129 |
return $views; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Default Report Views |
| 134 |
* |
| 135 |
* Checks the $_GET['view'] parameter to ensure it exists within the default allowed views. |
| 136 |
* |
| 137 |
* @param string $default Default view to use. |
| 138 |
* |
| 139 |
* @since 1.0 |
| 140 |
* @return string $view Report View |
| 141 |
*/ |
| 142 |
function give_get_reporting_view( $default = 'earnings' ) { |
| 143 |
|
| 144 |
if ( ! isset( $_GET['view'] ) || ! in_array( $_GET['view'], array_keys( give_reports_default_views() ) ) ) { |
| 145 |
$view = $default; |
| 146 |
} else { |
| 147 |
$view = $_GET['view']; |
| 148 |
} |
| 149 |
|
| 150 |
return apply_filters( 'give_get_reporting_view', $view ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Renders the Reports page |
| 155 |
* |
| 156 |
* @since 1.0 |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
function give_reports_tab_reports() { |
| 160 |
|
| 161 |
if ( ! current_user_can( 'view_give_reports' ) ) { |
| 162 |
wp_die( __( 'You do not have permission to access this report', 'give' ), __( 'Error', 'give' ), [ 'response' => 403 ] ); |
| 163 |
} |
| 164 |
|
| 165 |
$current_view = 'earnings'; |
| 166 |
$views = give_reports_default_views(); |
| 167 |
|
| 168 |
if ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $views ) ) { |
| 169 |
$current_view = $_GET['tab']; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Fires the report page view. |
| 174 |
* |
| 175 |
* @since 1.0 |
| 176 |
*/ |
| 177 |
do_action( "give_reports_view_{$current_view}" ); |
| 178 |
} |
| 179 |
|
| 180 |
add_action( 'give_reports_tab_reports', 'give_reports_tab_reports' ); |
| 181 |
|
| 182 |
/** |
| 183 |
* Renders the Reports Page Views Drop Downs |
| 184 |
* |
| 185 |
* @since 1.0 |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
function give_report_views() { |
| 189 |
$views = give_reports_default_views(); |
| 190 |
$current_view = isset( $_GET['view'] ) ? $_GET['view'] : 'earnings'; |
| 191 |
/** |
| 192 |
* Fires before the report page actions form. |
| 193 |
* |
| 194 |
* @since 1.0 |
| 195 |
*/ |
| 196 |
do_action( 'give_report_view_actions_before' ); |
| 197 |
?> |
| 198 |
<form id="give-reports-filter" method="get"> |
| 199 |
<select id="give-reports-view" name="view"> |
| 200 |
<option value="-1"><?php esc_html_e( 'Report Type', 'give' ); ?></option> |
| 201 |
<?php foreach ( $views as $view_id => $label ) : ?> |
| 202 |
<option value="<?php echo esc_attr( $view_id ); ?>" <?php selected( $view_id, $current_view ); ?>><?php echo $label; ?></option> |
| 203 |
<?php endforeach; ?> |
| 204 |
</select> |
| 205 |
|
| 206 |
<?php |
| 207 |
/** |
| 208 |
* Fires in the report page actions area. |
| 209 |
* |
| 210 |
* Allows you to add new elements/actions after the "Report Type" drop down. |
| 211 |
* |
| 212 |
* @since 1.0 |
| 213 |
*/ |
| 214 |
do_action( 'give_report_view_actions' ); |
| 215 |
?> |
| 216 |
|
| 217 |
<input type="hidden" name="post_type" value="give_forms"/> |
| 218 |
<input type="hidden" name="page" value="give-reports"/> |
| 219 |
<?php submit_button( esc_html__( 'Show', 'give' ), 'secondary', 'submit', false ); ?> |
| 220 |
</form> |
| 221 |
<?php |
| 222 |
/** |
| 223 |
* Fires after the report page actions form. |
| 224 |
* |
| 225 |
* @since 1.0 |
| 226 |
*/ |
| 227 |
do_action( 'give_report_view_actions_after' ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Renders the Reports Give Form Table |
| 232 |
* |
| 233 |
* @since 1.0 |
| 234 |
* @uses Give_Form_Reports_Table::prepare_items() |
| 235 |
* @uses Give_Form_Reports_Table::display() |
| 236 |
* @return void |
| 237 |
*/ |
| 238 |
function give_reports_forms_table() { |
| 239 |
|
| 240 |
if ( isset( $_GET['form-id'] ) ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
include GIVE_PLUGIN_DIR . 'includes/admin/reports/class-form-reports-table.php'; |
| 245 |
|
| 246 |
$give_table = new Give_Form_Reports_Table(); |
| 247 |
$give_table->prepare_items(); |
| 248 |
$give_table->display(); |
| 249 |
?> |
| 250 |
<input type="hidden" name="post_type" value="give_forms"/> |
| 251 |
<input type="hidden" name="page" value="give-reports"/> |
| 252 |
<input type="hidden" name="tab" value="forms"/> |
| 253 |
<?php |
| 254 |
} |
| 255 |
|
| 256 |
add_action( 'give_reports_view_forms', 'give_reports_forms_table' ); |
| 257 |
|
| 258 |
/** |
| 259 |
* Renders the detailed report for a specific give form. |
| 260 |
* |
| 261 |
* @since 1.0 |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
function give_reports_form_details() { |
| 265 |
if ( ! isset( $_GET['form-id'] ) ) { |
| 266 |
return; |
| 267 |
} |
| 268 |
?> |
| 269 |
<div class="tablenav top reports-forms-details-wrap"> |
| 270 |
<div class="actions bulkactions"> |
| 271 |
<button onclick="history.go(-1);" class="button-secondary"><?php esc_html_e( 'Go Back', 'give' ); ?></button> |
| 272 |
</div> |
| 273 |
</div> |
| 274 |
<?php |
| 275 |
give_reports_graph_of_form( absint( $_GET['form-id'] ) ); |
| 276 |
} |
| 277 |
|
| 278 |
add_action( 'give_reports_view_forms', 'give_reports_form_details' ); |
| 279 |
|
| 280 |
/** |
| 281 |
* Renders the Gateways Table |
| 282 |
* |
| 283 |
* @since 1.3 |
| 284 |
* @uses Give_Gateway_Reports_Table::prepare_items() |
| 285 |
* @uses Give_Gateway_Reports_Table::display() |
| 286 |
* @return void |
| 287 |
*/ |
| 288 |
function give_reports_gateways_table() { |
| 289 |
include GIVE_PLUGIN_DIR . 'includes/admin/reports/class-gateways-reports-table.php'; |
| 290 |
|
| 291 |
$give_table = new Give_Gateway_Reports_Table(); |
| 292 |
$give_table->prepare_items(); |
| 293 |
$give_table->display(); |
| 294 |
} |
| 295 |
|
| 296 |
add_action( 'give_reports_view_gateways', 'give_reports_gateways_table' ); |
| 297 |
|
| 298 |
/** |
| 299 |
* Renders the Reports Earnings Graphs |
| 300 |
* |
| 301 |
* @since 3.22.1 added permissions check |
| 302 |
* @since 1.0 |
| 303 |
* @return void |
| 304 |
*/ |
| 305 |
function give_reports_earnings() { |
| 306 |
if (!current_user_can('view_give_reports')){ |
| 307 |
wp_die(__('You do not have permission to access this report', 'give'), __('Error', 'give'), ['response' => 403]); |
| 308 |
} |
| 309 |
|
| 310 |
?> |
| 311 |
<div class="tablenav top reports-table-nav"> |
| 312 |
<h2 class="reports-earnings-title screen-reader-text"><?php _e( 'Revenue Report', 'give' ); ?></h2> |
| 313 |
</div> |
| 314 |
<?php |
| 315 |
give_reports_graph(); |
| 316 |
} |
| 317 |
|
| 318 |
add_action( 'give_reports_view_earnings', 'give_reports_earnings' ); |
| 319 |
|
| 320 |
|
| 321 |
/** |
| 322 |
* Retrieves estimated monthly earnings and sales |
| 323 |
* |
| 324 |
* @since 1.0 |
| 325 |
* @return array |
| 326 |
*/ |
| 327 |
function give_estimated_monthly_stats() { |
| 328 |
|
| 329 |
$estimated = Give_Cache::get( 'give_estimated_monthly_stats', true ); |
| 330 |
|
| 331 |
if ( false === $estimated ) { |
| 332 |
|
| 333 |
$estimated = [ |
| 334 |
'earnings' => 0, |
| 335 |
'sales' => 0, |
| 336 |
]; |
| 337 |
|
| 338 |
$stats = new Give_Payment_Stats(); |
| 339 |
|
| 340 |
$to_date_earnings = $stats->get_earnings( 0, 'this_month' ); |
| 341 |
$to_date_sales = $stats->get_sales( 0, 'this_month' ); |
| 342 |
|
| 343 |
$current_day = date( 'd', current_time( 'timestamp' ) ); |
| 344 |
$current_month = date( 'n', current_time( 'timestamp' ) ); |
| 345 |
$current_year = date( 'Y', current_time( 'timestamp' ) ); |
| 346 |
$days_in_month = cal_days_in_month( CAL_GREGORIAN, $current_month, $current_year ); |
| 347 |
|
| 348 |
$estimated['earnings'] = ( $to_date_earnings / $current_day ) * $days_in_month; |
| 349 |
$estimated['sales'] = ( $to_date_sales / $current_day ) * $days_in_month; |
| 350 |
|
| 351 |
// Cache for one day |
| 352 |
Give_Cache::set( 'give_estimated_monthly_stats', $estimated, DAY_IN_SECONDS, true ); |
| 353 |
} |
| 354 |
|
| 355 |
return maybe_unserialize( $estimated ); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Assign Get form method for reporting tabs |
| 360 |
* |
| 361 |
* @since 1.8.12 |
| 362 |
* |
| 363 |
* @return string |
| 364 |
*/ |
| 365 |
function give_reports_set_form_method() { |
| 366 |
return 'get'; |
| 367 |
} |
| 368 |
add_filter( 'give-reports_form_method_tab_forms', 'give_reports_set_form_method', 10 ); |
| 369 |
add_filter( 'give-reports_form_method_tab_donors', 'give_reports_set_form_method', 10 ); |
| 370 |
|
| 371 |
// @TODO: After release 1.8 Donations -> Reports generates with new setting api, so we can remove some old code from this file. |
| 372 |
|